测试用户
2023-04-13 43393f2bb11cbf9e6af40077bbc5284660e8a754
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package com.common.core.domain;
 
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONType;
import com.common.core.exception.BizException;
import com.common.core.utils.DateUtils;
import com.common.security.utils.SecurityUtils;
import com.jfinal.plugin.activerecord.TableMapping;
import lombok.extern.slf4j.Slf4j;
import java.lang.reflect.Field;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * @author 廖振钦
 * @date 2022-01-29
 */
@Slf4j
@JSONType(ignores = {"tableName","id","isDelete","updateTime","createTime","createBy","updateBy"})
public abstract class EntityModel<M extends EntityModel> extends BaseModel<M> {
 
    @Override
    public boolean save(){
        Field[] fields = getClass().getDeclaredFields();
        for(Field f : fields){
            f.setAccessible(true);
            try {
                Object o = f.get(this);
                if(null != o) {
                    if (RecordAttrHas(humpToLine2(f.getName()))) {
                        set(humpToLine2(f.getName()), f.get(this));
                    } else if (RecordAttrHas(f.getName())) {
                        set(f.getName(), f.get(this));
                    }
                }
            } catch (IllegalAccessException e) {
 
            }
        }
        String appid = SecurityUtils.byId();
        setIsDelete(0);
        setCreateTime(DateUtils.now());
        setUpdateTime(DateUtils.now());
        setCreateBy(appid);
        setUpdateBy(appid);
        return super.save();
    }
 
    @Override
    public boolean update(){
        Field[] fields = getClass().getDeclaredFields();
        for(Field f : fields){
            f.setAccessible(true);
            if(!"id".equals(f.getName())) {
                try {
                    Object o = f.get(this);
                    if(null != o) {
                        if (RecordAttrHas(humpToLine2(f.getName()))) {
                            set(humpToLine2(f.getName()), f.get(this));
                        } else if (RecordAttrHas(f.getName())) {
                            set(f.getName(), f.get(this));
                        }
                    }
                } catch (IllegalAccessException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
        String appid = SecurityUtils.byId();
        setIsDelete(0);
        setCreateTime(DateUtils.now());
        setUpdateTime(DateUtils.now());
        setCreateBy(appid);
        setUpdateBy(appid);
        return super.update();
    }
 
    @Override
    public boolean saveOrUpdate(){
        if(null == getId()){
            return this.save();
        } else{
            return this.update();
        }
    }
 
    @Override
    public M findFirst(String sql){
        M m  = super.findFirst(sql);
        if(null == m){
            return null;
        }
        RelectionField(m);
        return m;
    }
 
    @Override
    public M findFirst(String sql,Object...params){
        M m  = super.findFirst(sql, params);
        if(null == m){
            return null;
        }
        RelectionField(m);
        return m;
    }
 
    @Override
    public M findById(Object id){
        return this.findFirst("select * from "+getTableName()+" where id = ? and is_delete='0'",id);
    }
 
    @Override
    public List<M> findAll(){
        List<M> list = super.findAll();
        List<M> returnlist = new ArrayList<M>();
        for(M m : list){
            RelectionField(m);
            returnlist.add(m);
        }
        return returnlist;
    }
 
    @Override
    public List<M> find(String sql){
        List<M> list  = super.find(sql);
        List<M> returnlist = Collections.emptyList();
        for(M m : list){
            RelectionField(m);
            returnlist.add(m);
        }
        return returnlist;
    }
 
    @Override
    public List<M> find(String sql,Object...params){
        List<M> list  = super.find(sql, params);
        List<M> returnlist = new ArrayList<M>();
        for(M m : list){
            RelectionField(m);
            returnlist.add(m);
        }
        return returnlist;
    }
 
    private void RelectionField(M m){
        Field[] fields = m.getClass().getDeclaredFields();
        for(Field f : fields) {
            f.setAccessible(true);
            try {
                if(RecordAttrHas(humpToLine2(f.getName()))){
                    f.set(m, m.get(humpToLine2(f.getName())));
                }else if(RecordAttrHas(f.getName())){
                    f.set(m, m.get(f.getName()));
                }
            } catch (IllegalAccessException e) {
 
            }
        }
    }
 
 
 
 
    private String humpToLine2(String str) {
        Pattern humpPattern = Pattern.compile("[A-Z]");
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
 
    }
 
    private String lineToHump(String str) {
        Pattern linePattern = Pattern.compile("_(\\w)");
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
 
    private Boolean RecordAttrHas(String columnName){
        Set<String> names = TableMapping.me().getTable(this.getClass()).getColumnNameSet();
        if(names.contains(columnName)){
            return true;
        }
        return false;
    }
 
    public String json(){
        return JSONObject.toJSONString(this);
    }
 
    public JSONObject jsonObject(){
        return JSONObject.parseObject(json());
    }
 
}