EntityBase.java 3.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
package cn.ibizlab.util.domain;

import cn.ibizlab.util.helper.DEFieldCacheMap;
import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.data.annotation.Transient;
import org.springframework.util.AlternativeJdkIdGenerator;
12
import org.springframework.util.ObjectUtils;
13
import java.io.Serializable;
14
import java.lang.reflect.Field;
15 16 17 18 19 20 21 22 23 24 25 26
import org.springframework.util.StringUtils;
import java.util.*;

public class EntityBase implements Serializable {

    @JsonIgnore
    @JSONField(serialize = false)
    @TableField(exist = false)
    @Transient
    private Set<String> focusNull;

    public Set<String> getFocusNull() {
27
        if(focusNull==null) {
28
            focusNull=new HashSet<>();
29
        }
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
        return focusNull;
    }

    public void setFocusNull(Set<String> focusNull) {
        this.focusNull = focusNull;
    }

    public void modify(String field,Object val) {

    }

    public Serializable getDefaultKey(boolean gen) {
        String Id=(new AlternativeJdkIdGenerator()).generateId().toString();
        return gen?Id.replace("-", ""):Id;
    }

    @JsonIgnore
    @JSONField(serialize = false)
    @TableField(exist = false)
    @Transient
    private BeanMap map;

    private BeanMap getMap()
    {
54 55 56
        if(map==null) {
           map=BeanMap.create(this);
        }
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
        return  map;
    }

    @JsonIgnore
    @TableField(exist = false)
    @Transient
    private Map<String,Object> extensionparams=new HashMap<String,Object>();

    public Map<String, Object> getExtensionparams() {
        return extensionparams;
    }

    public void setExtensionparams(Map<String, Object> extensionparams) {
        this.extensionparams = extensionparams;
    }

    public Object get(String field) {
        String fieldRealName=DEFieldCacheMap.getFieldRealName(this.getClass(),field);
75
        if(!StringUtils.isEmpty(fieldRealName)) {
76
            return getMap().get(fieldRealName);
77 78
        }
        else {
79
            return this.extensionparams.get(field.toLowerCase());
80
        }
81 82 83 84 85 86 87 88 89 90 91 92
    }

    @JsonAnyGetter
    public Map<String , Object> any() {
        return extensionparams;
    }

    @JsonAnySetter
    public void set(String field, Object value) {
        field=field.toLowerCase();
        String fieldRealName=DEFieldCacheMap.getFieldRealName(this.getClass(),field);
        if(!StringUtils.isEmpty(fieldRealName)) {
93
            if (value == null) {
94
                getMap().put(fieldRealName, null);
95 96
            }
            else {
97
                getMap().put(fieldRealName, DEFieldCacheMap.fieldValueOf(this.getClass(), fieldRealName, value));
98
            }
99
        }
100
        else {
101
            this.extensionparams.put(field.toLowerCase(),value);
102
        }
103 104
    }

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    /**
    * 复制当前对象数据到目标对象
    * @param targetEntity 目标数据对象
    * @param bIncEmpty  是否包括空值
    * @param <T>
    * @return
    */
    public <T> T copyTo(T targetEntity, boolean bIncEmpty){
        if(targetEntity instanceof EntityBase){
            EntityBase target= (EntityBase) targetEntity;
            Hashtable<String, Field> sourceFields=DEFieldCacheMap.getFieldMap(this.getClass());
            for(String field : sourceFields.keySet()){
                Object value=this.get(field);
                if( !ObjectUtils.isEmpty(value) ||  ObjectUtils.isEmpty(value) &&  getFocusNull().contains(field) &&  bIncEmpty ){
                    target.set(field,value);
                }
            }
        }
        return targetEntity;
    }

    /**
    * 重置当前数据对象属性值
    * @param field
    */
    public void reset(String field){

    }
133
}