ny
昨天 b6f169fe43a2b13f351aefc152374fc7f0bc8cb7
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package jnpf.onlinedev.service.impl;
 
import cn.hutool.core.bean.BeanUtil;
import jnpf.base.entity.VisualdevReleaseEntity;
import jnpf.base.model.VisualLogModel;
import jnpf.base.service.SuperServiceImpl;
import jnpf.base.service.VisualdevReleaseService;
import jnpf.constant.JnpfConst;
import jnpf.event.ProjectEventListener;
import jnpf.model.visualJson.FieLdsModel;
import jnpf.model.visualJson.FormDataModel;
import jnpf.model.visualJson.TableFields;
import jnpf.model.visualJson.TableModel;
import jnpf.model.visualJson.config.ConfigModel;
import jnpf.module.ProjectEventBuilder;
import jnpf.module.ProjectEventInstance;
import jnpf.onlinedev.entity.VisualLogEntity;
import jnpf.onlinedev.mapper.VisualLogMapper;
import jnpf.onlinedev.model.OnlineDevEnum.OnlineDataTypeEnum;
import jnpf.onlinedev.model.log.VisualLogForm;
import jnpf.onlinedev.model.log.VisualLogPage;
import jnpf.onlinedev.service.VisualLogService;
import jnpf.onlinedev.util.onlineDevUtil.OnlinePublicUtils;
import jnpf.onlinedev.util.onlineDevUtil.OnlineSwapDataUtils;
import jnpf.util.JsonUtil;
import jnpf.util.PublishEventUtil;
import jnpf.util.TableFeildsEnum;
import jnpf.util.visiual.JnpfKeyConsts;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 数据日志实现
 *
 * @author JNPF开发平台组
 * @version v5.1.0
 * @copyright 引迈信息技术有限公司
 * @date 2024/8/27 18:24:42
 */
@Service
public class VisualLogServiceImpl extends SuperServiceImpl<VisualLogMapper, VisualLogEntity> implements VisualLogService {
    @Autowired
    private VisualdevReleaseService visualdevReleaseService;
    @Autowired
    private OnlineSwapDataUtils onlineSwapDataUtils;
 
    /**
     * 创建日志事件
     *
     * @param form 数据id
     */
    @Override
    public void createEventLog(VisualLogForm form) {
        PublishEventUtil.publishLocalEvent(new ProjectEventBuilder(JnpfConst.VSLOG_EVENT_KEY, form));
    }
 
    @Override
    public List<VisualLogEntity> getList(VisualLogPage pagination) {
        return this.baseMapper.getList(pagination);
    }
 
    /**
     * 监听日志插入
     *
     * @param redisEvent
     */
    @ProjectEventListener(channelRegex = JnpfConst.VSLOG_EVENT_KEY + ".*")
    public void createLogByEvent(ProjectEventInstance redisEvent) {
        VisualLogForm form = (VisualLogForm) redisEvent.getSource();
        VisualLogEntity visualLogEntity = new VisualLogEntity();
        visualLogEntity.setType(form.getType());
        visualLogEntity.setModelId(form.getModelId());
        visualLogEntity.setDataId(form.getDataId());
 
        //修改数据
        if (Objects.equals(form.getType(), 1)) {
            List<VisualLogModel> listLog = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(form.getListLog())) {
                listLog = form.getListLog();
            } else {
                addLog(form, listLog);
            }
            visualLogEntity.setDataLog(JsonUtil.getObjectToString(listLog));
            if (CollectionUtils.isNotEmpty(listLog)) {
                this.save(visualLogEntity);
            }
        } else {
            //新增数据
            this.save(visualLogEntity);
        }
    }
 
    public void addLog(VisualLogForm formSource, List<VisualLogModel> listLog) {
        VisualLogForm form = BeanUtil.copyProperties(formSource, VisualLogForm.class);
        VisualdevReleaseEntity visualdevEntity = visualdevReleaseService.getById(form.getModelId());
        FormDataModel formData = JsonUtil.getJsonToBean(visualdevEntity.getFormData(), FormDataModel.class);
        List<FieLdsModel> fieLdsModels = JsonUtil.getJsonToList(formData.getFields(), FieLdsModel.class);
        List<TableModel> tableModels = JsonUtil.getJsonToList(visualdevEntity.getVisualTables(), TableModel.class);
 
        List<FieLdsModel> fields = new ArrayList<>();
        OnlinePublicUtils.recursionFields(fields, fieLdsModels);
 
        List<Map<String, Object>> swapInfoOld = onlineSwapDataUtils.getSwapInfo(new ArrayList<Map<String, Object>>() {{
            add(form.getOldData());
        }}, fields, visualdevEntity.getId(), false, null);
        Map<String, Object> oldData = swapInfoOld.get(0);
        List<Map<String, Object>> swapInfoNew = onlineSwapDataUtils.getSwapInfo(new ArrayList<Map<String, Object>>() {{
            add(form.getNewData());
        }}, fields, visualdevEntity.getId(), false, null);
        Map<String, Object> newData = swapInfoNew.get(0);
 
        for (FieLdsModel item : fields) {
            ConfigModel config = item.getConfig();
            String jnpfKey = config.getJnpfKey();
            String label = config.getLabel();
            String dataType = config.getDataType();
            //移除系统字段
            if (isSkipFields(item)) continue;
            if (JnpfKeyConsts.CHILD_TABLE.equals(jnpfKey)) {
                TableModel tableModel = tableModels.stream().filter(t -> t.getTable().equals(config.getTableName())).findFirst().orElse(null);
                TableFields mainField = tableModel.getFields().stream().filter(t ->
                        Objects.equals(t.getPrimaryKey(), 1) && !t.getField().equalsIgnoreCase(TableFeildsEnum.TENANTID.getField())).findFirst().orElse(null);
                String mainKey = mainField.getField();
 
                List<FieLdsModel> children = item.getConfig().getChildren();
                String vModel = item.getVModel();
                //子表数据
                List<Map<String, Object>> childData = new ArrayList<>();
                //子表表头
                List<Map<String, Object>> childField = new ArrayList<>();
                for (FieLdsModel childItem : children) {
                    String jnpfKeyChild = childItem.getConfig().getJnpfKey();
                    if (isSkipFields(childItem))
                        continue;
                    Map<String, Object> childItemMap = new HashMap<>();
                    childItemMap.put("prop", childItem.getVModel());
                    childItemMap.put("label", childItem.getConfig().getLabel());
                    childItemMap.put("jnpfKey", jnpfKeyChild);
                    boolean nameModified = false;
                    if (JnpfKeyConsts.getNameModified().contains(jnpfKeyChild)) {
                        if (JnpfKeyConsts.getNameModifiedNotDynamic().contains(jnpfKeyChild) || OnlineDataTypeEnum.DYNAMIC.getType().equals(childItem.getConfig().getDataType())) {
                            nameModified = true;
                        }
                    }
                    childItemMap.put("nameModified", nameModified);
                    childField.add(childItemMap);
                }
 
                List<Map<String, Object>> childOld = oldData.get(vModel) == null ? new ArrayList<>() : (List) oldData.get(vModel);
                List<Map<String, Object>> childNew = newData.get(vModel) == null ? new ArrayList<>() : (List) newData.get(vModel);
                List<Object> newIds = childNew.stream().map(t -> t.get(mainKey)).collect(Collectors.toList());
                List<Map<String, Object>> deleteMap = childOld.stream().filter(t -> !newIds.contains(t.get(mainKey))).collect(Collectors.toList());
                for (Map<String, Object> chilMap : deleteMap) {
                    Map<String, Object> childDataMap = new HashMap<>();
                    for (FieLdsModel childItem : children) {
                        String childJnpfKey = childItem.getConfig().getJnpfKey();
                        //移除系统字段
                        if (isSkipFields(childItem)) continue;
                        String childVmodel = childItem.getVModel();
                        String childVmodel_old = "jnpf_old_" + childItem.getVModel();
                        String oldValue = getValueByType(chilMap, childVmodel, childJnpfKey);
                        childDataMap.put(childVmodel, null);
                        childDataMap.put(childVmodel_old, oldValue);
                        childDataMap.put("jnpf_type", 2);
                    }
                    childData.add(childDataMap);
                }
                for (Map<String, Object> chilMap : childNew) {
                    Object mainId = chilMap.get(mainKey);
                    Map<String, Object> oldMap = childOld.stream().filter(t -> t.get(mainKey).equals(mainId)).findFirst().orElse(null);
                    Integer jnpf_type = 1;
                    if (oldMap == null) {
                        jnpf_type = 0;
                    }
                    Map<String, Object> childDataMap = new HashMap<>();
                    boolean hasChanged = false;
                    for (FieLdsModel childItem : children) {
                        String childJnpfKey = childItem.getConfig().getJnpfKey();
                        //移除系统字段
                        if (isSkipFields(childItem)) continue;
                        String childVmodel = childItem.getVModel();
                        String childVmodel_old = "jnpf_old_" + childItem.getVModel();
                        String newValue = getValueByType(chilMap, childVmodel, childJnpfKey);
                        String oldValue = getValueByType(oldMap, childVmodel, childJnpfKey);
                        if (!Objects.equals(newValue, oldValue)) {
                            hasChanged = true;
                        }
                        childDataMap.put(childVmodel, newValue);
                        childDataMap.put(childVmodel_old, oldValue);
                        childDataMap.put("jnpf_type", jnpf_type);
                    }
                    if (hasChanged) {
                        childData.add(childDataMap);
                    }
                }
                if (CollectionUtils.isNotEmpty(childData)) {
                    VisualLogModel vlogModel = new VisualLogModel();
                    vlogModel.setField(vModel);
                    vlogModel.setFieldName(label);
                    vlogModel.setJnpfKey(jnpfKey);
                    vlogModel.setChidData(childData);
                    vlogModel.setChidField(childField);
                    vlogModel.setType(1);
                    listLog.add(vlogModel);
                }
            } else {
                String vModel = item.getVModel();
                if (!Objects.equals(oldData.get(vModel), newData.get(vModel))) {
                    Integer actionType = 1;//0-新增,1-修改
                    if (oldData.get(vModel) == null || oldData.get(vModel).toString().trim().isEmpty()) {
                        actionType = 0;
                    }
                    boolean nameModified = false;
                    if (JnpfKeyConsts.getNameModified().contains(jnpfKey)) {
                        if (JnpfKeyConsts.getNameModifiedNotDynamic().contains(jnpfKey) || OnlineDataTypeEnum.DYNAMIC.getType().equals(dataType)) {
                            nameModified = true;
                        }
                    }
 
                    String newValue = getValueByType(newData, vModel, jnpfKey);
                    String oldValue = getValueByType(oldData, vModel, jnpfKey);
                    VisualLogModel vlogModel = new VisualLogModel();
                    vlogModel.setField(vModel);
                    vlogModel.setFieldName(label);
                    vlogModel.setJnpfKey(jnpfKey);
                    vlogModel.setNewData(newValue);
                    vlogModel.setOldData(oldValue);
                    vlogModel.setType(actionType);
                    vlogModel.setNameModified(nameModified);
                    listLog.add(vlogModel);
                }
            }
        }
    }
 
    /**
     * 跳过字段不处理
     *
     * @param childItem
     * @return
     */
    private boolean isSkipFields(FieLdsModel childItem) {
        //字段不处理直接跳过 :(JnpfKeyConsts.CALCULATE 计算公式仅展示也跳过)
        List<String> skipFields = new ArrayList<>();
        skipFields.addAll(JnpfKeyConsts.getSystemKey());
        skipFields.add(JnpfKeyConsts.POPUPSELECT_ATTR);
        skipFields.add(JnpfKeyConsts.RELATIONFORM_ATTR);
        String jnpfKey = childItem.getConfig().getJnpfKey();
        return skipFields.contains(jnpfKey) || (JnpfKeyConsts.CALCULATE.equals(jnpfKey) && Objects.equals(childItem.getIsStorage(), 0));
    }
 
    private String getValueByType(Map<String, Object> map, String vModel, String jnpfKey) {
        if (map != null) {
            Object o = map.get(vModel);
            String value = o == null ? "" : o.toString();
            switch (jnpfKey) {
                case JnpfKeyConsts.UPLOADFZ:
                    List<Map<String, String>> listM = (List) o;
                    if (listM != null) {
                        StringJoiner sj = new StringJoiner(",");
                        listM.stream().forEach(t -> sj.add(t.get("name").toString()));
                        value = sj.toString();
                    }
                    break;
                case JnpfKeyConsts.LOCATION:
                    Map<String, Object> lcationMap = JsonUtil.stringToMap(value);
                    if (MapUtils.isNotEmpty(lcationMap)) {
                        value = lcationMap.get("fullAddress") != null ? lcationMap.get("fullAddress").toString() : "";
                    }
                    break;
                default:
                    break;
            }
            return value;
        }
        return null;
    }
}