All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.soento.core.lang.Pojo Maven / Gradle / Ivy

package com.soento.core.lang;

import com.soento.core.enums.DateFormat;
import com.soento.core.util.JsonUtil;
import com.soento.core.util.RandomUtil;
import com.soento.core.util.StringUtil;

import java.math.BigDecimal;
import java.util.Date;
import java.util.LinkedHashMap;

/**
 * 实体对象
 *
 * @author: yantao.zeng
 */
public class Pojo extends LinkedHashMap {


    public static String nextId() {
        return DateFormat.YYYYMMDDHHMISSMS.instance().format(new Date()) + RandomUtil.simpleUUID();
    }

    public void initId(String id) {
        setField("id", id);
    }

    public void initId() {
        this.initId(Pojo.nextId());
    }

    public void initEntity(String userId) {
        setField("createBy", userId);
        setField("modifyBy", userId);
        setField("createDate", new Date());
        setField("modifyDate", new Date());
        setField("deleteFlag", 0);
    }

    public void initEntity() {
        initEntity("SYSTEM");
    }

    public void initEntity(LoginUser user) {
        if (user != null && StringUtil.isNotBlank(user.getUserId())) {
            initEntity(user.getUserId());
        } else {
            initEntity();
        }
    }

    public void updateEntity(String userId) {
        setField("modifyBy", userId);
        setField("modifyDate", new Date());
    }

    public void updateEntity() {
        updateEntity("SYSTEM");
    }

    public void updateEntity(LoginUser user) {
        if (user != null && StringUtil.isNotBlank(user.getUserId())) {
            updateEntity(user.getUserId());
        } else {
            updateEntity();
        }
    }

    public boolean hasField(String field) {
        return keySet().contains(field);
    }

    public void setField(String field, Object value) {
        this.put(field, value);
    }

    public String getString(String field) {
        Object obj = get(field);
        if (obj == null) {
            return null;
        }
        if (obj instanceof BigDecimal) {
            return ((BigDecimal) obj).toPlainString();
        } else {
            return obj.toString();
        }
    }

    public  T get(String field, Class clazz) {
        return (T) get(field);
    }

    public String toJson() {
        return JsonUtil.toJson(this);
    }

    public String toJsonp(String function) {
        return JsonUtil.toJsonp(function, this);
    }

    public  T toObject(Class clazz) {
        return JsonUtil.toObject(toJson(), clazz);
    }

    /**
     * 对象构建
     *
     * @param from 拷贝源
     * @return 结果对象
     */
    public static Pojo build(Object from) {
        if (from == null) {
            return null;
        }
        String json = JsonUtil.toJson(from);
        return JsonUtil.toObject(json, Pojo.class);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy