VisualLogServiceImpl.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. package jnpf.onlinedev.service.impl;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4. import com.baomidou.mybatisplus.core.metadata.IPage;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import jnpf.base.entity.VisualdevReleaseEntity;
  7. import jnpf.base.model.VisualLogModel;
  8. import jnpf.base.service.SuperServiceImpl;
  9. import jnpf.base.service.VisualdevReleaseService;
  10. import jnpf.constant.JnpfConst;
  11. import jnpf.event.ProjectEventListener;
  12. import jnpf.model.visualJson.FieLdsModel;
  13. import jnpf.model.visualJson.FormDataModel;
  14. import jnpf.model.visualJson.TableFields;
  15. import jnpf.model.visualJson.TableModel;
  16. import jnpf.model.visualJson.config.ConfigModel;
  17. import jnpf.module.ProjectEventBuilder;
  18. import jnpf.module.ProjectEventInstance;
  19. import jnpf.onlinedev.entity.VisualLogEntity;
  20. import jnpf.onlinedev.mapper.VisualLogMapper;
  21. import jnpf.onlinedev.model.OnlineDevEnum.OnlineDataTypeEnum;
  22. import jnpf.onlinedev.model.log.VisualLogForm;
  23. import jnpf.onlinedev.model.log.VisualLogPage;
  24. import jnpf.onlinedev.service.VisualLogService;
  25. import jnpf.onlinedev.util.onlineDevUtil.OnlinePublicUtils;
  26. import jnpf.onlinedev.util.onlineDevUtil.OnlineSwapDataUtils;
  27. import jnpf.util.JsonUtil;
  28. import jnpf.util.PublishEventUtil;
  29. import jnpf.util.TableFeildsEnum;
  30. import jnpf.util.visiual.JnpfKeyConsts;
  31. import org.apache.commons.collections4.CollectionUtils;
  32. import org.apache.commons.collections4.MapUtils;
  33. import org.springframework.beans.factory.annotation.Autowired;
  34. import org.springframework.stereotype.Service;
  35. import java.util.*;
  36. import java.util.stream.Collectors;
  37. /**
  38. * 数据日志实现
  39. *
  40. * @author JNPF开发平台组
  41. * @version v5.1.0
  42. * @copyright 引迈信息技术有限公司
  43. * @date 2024/8/27 18:24:42
  44. */
  45. @Service
  46. public class VisualLogServiceImpl extends SuperServiceImpl<VisualLogMapper, VisualLogEntity> implements VisualLogService {
  47. @Autowired
  48. private VisualdevReleaseService visualdevReleaseService;
  49. @Autowired
  50. private OnlineSwapDataUtils onlineSwapDataUtils;
  51. /**
  52. * 创建日志事件
  53. *
  54. * @param form 数据id
  55. */
  56. @Override
  57. public void createEventLog(VisualLogForm form) {
  58. PublishEventUtil.publishLocalEvent(new ProjectEventBuilder(JnpfConst.VSLOG_EVENT_KEY, form));
  59. }
  60. @Override
  61. public List<VisualLogEntity> getList(VisualLogPage pagination) {
  62. QueryWrapper<VisualLogEntity> queryWrapper = new QueryWrapper<>();
  63. queryWrapper.lambda().eq(VisualLogEntity::getModelId, pagination.getModelId());
  64. queryWrapper.lambda().eq(VisualLogEntity::getDataId, pagination.getDataId());
  65. //排序
  66. queryWrapper.lambda().orderByDesc(VisualLogEntity::getCreatorTime);
  67. Page<VisualLogEntity> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize());
  68. IPage<VisualLogEntity> iPage = this.page(page, queryWrapper);
  69. return pagination.setData(iPage.getRecords(), iPage.getTotal());
  70. }
  71. /**
  72. * 监听日志插入
  73. *
  74. * @param redisEvent
  75. */
  76. @ProjectEventListener(channelRegex = JnpfConst.VSLOG_EVENT_KEY + ".*")
  77. public void createLogByEvent(ProjectEventInstance redisEvent) {
  78. VisualLogForm form = (VisualLogForm) redisEvent.getSource();
  79. VisualLogEntity visualLogEntity = new VisualLogEntity();
  80. visualLogEntity.setType(form.getType());
  81. visualLogEntity.setModelId(form.getModelId());
  82. visualLogEntity.setDataId(form.getDataId());
  83. //修改数据
  84. if (Objects.equals(form.getType(), 1)) {
  85. List<VisualLogModel> listLog = new ArrayList<>();
  86. if (CollectionUtils.isNotEmpty(form.getListLog())) {
  87. listLog = form.getListLog();
  88. } else {
  89. addLog(form, listLog);
  90. }
  91. visualLogEntity.setDataLog(JsonUtil.getObjectToString(listLog));
  92. if (CollectionUtils.isNotEmpty(listLog)) {
  93. this.save(visualLogEntity);
  94. }
  95. } else {
  96. //新增数据
  97. this.save(visualLogEntity);
  98. }
  99. }
  100. public void addLog(VisualLogForm formSource, List<VisualLogModel> listLog) {
  101. VisualLogForm form = BeanUtil.copyProperties(formSource, VisualLogForm.class);
  102. VisualdevReleaseEntity visualdevEntity = visualdevReleaseService.getById(form.getModelId());
  103. FormDataModel formData = JsonUtil.getJsonToBean(visualdevEntity.getFormData(), FormDataModel.class);
  104. List<FieLdsModel> fieLdsModels = JsonUtil.getJsonToList(formData.getFields(), FieLdsModel.class);
  105. List<TableModel> tableModels = JsonUtil.getJsonToList(visualdevEntity.getVisualTables(), TableModel.class);
  106. List<FieLdsModel> fields = new ArrayList<>();
  107. OnlinePublicUtils.recursionFields(fields, fieLdsModels);
  108. List<Map<String, Object>> swapInfoOld = onlineSwapDataUtils.getSwapInfo(new ArrayList<Map<String, Object>>() {{
  109. add(form.getOldData());
  110. }}, fields, visualdevEntity.getId(), false, null);
  111. Map<String, Object> oldData = swapInfoOld.get(0);
  112. List<Map<String, Object>> swapInfoNew = onlineSwapDataUtils.getSwapInfo(new ArrayList<Map<String, Object>>() {{
  113. add(form.getNewData());
  114. }}, fields, visualdevEntity.getId(), false, null);
  115. Map<String, Object> newData = swapInfoNew.get(0);
  116. for (FieLdsModel item : fields) {
  117. ConfigModel config = item.getConfig();
  118. String jnpfKey = config.getJnpfKey();
  119. String label = config.getLabel();
  120. String dataType = config.getDataType();
  121. //移除系统字段
  122. if (isSkipFields(item)) continue;
  123. if (JnpfKeyConsts.CHILD_TABLE.equals(jnpfKey)) {
  124. TableModel tableModel = tableModels.stream().filter(t -> t.getTable().equals(config.getTableName())).findFirst().orElse(null);
  125. TableFields mainField = tableModel.getFields().stream().filter(t ->
  126. Objects.equals(t.getPrimaryKey(), 1) && !t.getField().equalsIgnoreCase(TableFeildsEnum.TENANTID.getField())).findFirst().orElse(null);
  127. String mainKey = mainField.getField();
  128. List<FieLdsModel> children = item.getConfig().getChildren();
  129. String vModel = item.getVModel();
  130. //子表数据
  131. List<Map<String, Object>> childData = new ArrayList<>();
  132. //子表表头
  133. List<Map<String, Object>> childField = new ArrayList<>();
  134. for (FieLdsModel childItem : children) {
  135. String jnpfKeyChild = childItem.getConfig().getJnpfKey();
  136. if (isSkipFields(childItem))
  137. continue;
  138. Map<String, Object> childItemMap = new HashMap<>();
  139. childItemMap.put("prop", childItem.getVModel());
  140. childItemMap.put("label", childItem.getConfig().getLabel());
  141. childItemMap.put("jnpfKey", jnpfKeyChild);
  142. boolean nameModified = false;
  143. if (JnpfKeyConsts.getNameModified().contains(jnpfKeyChild)) {
  144. if (JnpfKeyConsts.getNameModifiedNotDynamic().contains(jnpfKeyChild) || OnlineDataTypeEnum.DYNAMIC.getType().equals(childItem.getConfig().getDataType())) {
  145. nameModified = true;
  146. }
  147. }
  148. childItemMap.put("nameModified", nameModified);
  149. childField.add(childItemMap);
  150. }
  151. List<Map<String, Object>> childOld = oldData.get(vModel) == null ? new ArrayList<>() : (List) oldData.get(vModel);
  152. List<Map<String, Object>> childNew = newData.get(vModel) == null ? new ArrayList<>() : (List) newData.get(vModel);
  153. List<Object> newIds = childNew.stream().map(t -> t.get(mainKey)).collect(Collectors.toList());
  154. List<Map<String, Object>> deleteMap = childOld.stream().filter(t -> !newIds.contains(t.get(mainKey))).collect(Collectors.toList());
  155. for (Map<String, Object> chilMap : deleteMap) {
  156. Map<String, Object> childDataMap = new HashMap<>();
  157. for (FieLdsModel childItem : children) {
  158. String childJnpfKey = childItem.getConfig().getJnpfKey();
  159. //移除系统字段
  160. if (isSkipFields(childItem)) continue;
  161. String childVmodel = childItem.getVModel();
  162. String childVmodel_old = "jnpf_old_" + childItem.getVModel();
  163. String oldValue = getValueByType(chilMap, childVmodel, childJnpfKey);
  164. childDataMap.put(childVmodel, null);
  165. childDataMap.put(childVmodel_old, oldValue);
  166. childDataMap.put("jnpf_type", 2);
  167. }
  168. childData.add(childDataMap);
  169. }
  170. for (Map<String, Object> chilMap : childNew) {
  171. Object mainId = chilMap.get(mainKey);
  172. Map<String, Object> oldMap = childOld.stream().filter(t -> t.get(mainKey).equals(mainId)).findFirst().orElse(null);
  173. Integer jnpf_type = 1;
  174. if (oldMap == null) {
  175. jnpf_type = 0;
  176. }
  177. Map<String, Object> childDataMap = new HashMap<>();
  178. boolean hasChanged = false;
  179. for (FieLdsModel childItem : children) {
  180. String childJnpfKey = childItem.getConfig().getJnpfKey();
  181. //移除系统字段
  182. if (isSkipFields(childItem)) continue;
  183. String childVmodel = childItem.getVModel();
  184. String childVmodel_old = "jnpf_old_" + childItem.getVModel();
  185. String newValue = getValueByType(chilMap, childVmodel, childJnpfKey);
  186. String oldValue = getValueByType(oldMap, childVmodel, childJnpfKey);
  187. if (!Objects.equals(newValue, oldValue)) {
  188. hasChanged = true;
  189. }
  190. childDataMap.put(childVmodel, newValue);
  191. childDataMap.put(childVmodel_old, oldValue);
  192. childDataMap.put("jnpf_type", jnpf_type);
  193. }
  194. if (hasChanged) {
  195. childData.add(childDataMap);
  196. }
  197. }
  198. if (CollectionUtils.isNotEmpty(childData)) {
  199. VisualLogModel vlogModel = new VisualLogModel();
  200. vlogModel.setField(vModel);
  201. vlogModel.setFieldName(label);
  202. vlogModel.setJnpfKey(jnpfKey);
  203. vlogModel.setChidData(childData);
  204. vlogModel.setChidField(childField);
  205. vlogModel.setType(1);
  206. listLog.add(vlogModel);
  207. }
  208. } else {
  209. String vModel = item.getVModel();
  210. if (!Objects.equals(oldData.get(vModel), newData.get(vModel))) {
  211. Integer actionType = 1;//0-新增,1-修改
  212. if (oldData.get(vModel) == null || oldData.get(vModel).toString().trim().isEmpty()) {
  213. actionType = 0;
  214. }
  215. boolean nameModified = false;
  216. if (JnpfKeyConsts.getNameModified().contains(jnpfKey)) {
  217. if (JnpfKeyConsts.getNameModifiedNotDynamic().contains(jnpfKey) || OnlineDataTypeEnum.DYNAMIC.getType().equals(dataType)) {
  218. nameModified = true;
  219. }
  220. }
  221. String newValue = getValueByType(newData, vModel, jnpfKey);
  222. String oldValue = getValueByType(oldData, vModel, jnpfKey);
  223. VisualLogModel vlogModel = new VisualLogModel();
  224. vlogModel.setField(vModel);
  225. vlogModel.setFieldName(label);
  226. vlogModel.setJnpfKey(jnpfKey);
  227. vlogModel.setNewData(newValue);
  228. vlogModel.setOldData(oldValue);
  229. vlogModel.setType(actionType);
  230. vlogModel.setNameModified(nameModified);
  231. listLog.add(vlogModel);
  232. }
  233. }
  234. }
  235. }
  236. /**
  237. * 跳过字段不处理
  238. *
  239. * @param childItem
  240. * @return
  241. */
  242. private boolean isSkipFields(FieLdsModel childItem) {
  243. //字段不处理直接跳过 :(JnpfKeyConsts.CALCULATE 计算公式仅展示也跳过)
  244. List<String> skipFields = new ArrayList<>();
  245. skipFields.addAll(JnpfKeyConsts.getSystemKey());
  246. skipFields.add(JnpfKeyConsts.POPUPSELECT_ATTR);
  247. skipFields.add(JnpfKeyConsts.RELATIONFORM_ATTR);
  248. String jnpfKey = childItem.getConfig().getJnpfKey();
  249. return skipFields.contains(jnpfKey) || (JnpfKeyConsts.CALCULATE.equals(jnpfKey) && Objects.equals(childItem.getIsStorage(), 0));
  250. }
  251. private String getValueByType(Map<String, Object> map, String vModel, String jnpfKey) {
  252. if (map != null) {
  253. Object o = map.get(vModel);
  254. String value = o == null ? "" : o.toString();
  255. switch (jnpfKey) {
  256. case JnpfKeyConsts.UPLOADFZ:
  257. List<Map<String, String>> listM = (List) o;
  258. if (listM != null) {
  259. StringJoiner sj = new StringJoiner(",");
  260. listM.stream().forEach(t -> sj.add(t.get("name").toString()));
  261. value = sj.toString();
  262. }
  263. break;
  264. case JnpfKeyConsts.LOCATION:
  265. Map<String, Object> lcationMap = JsonUtil.stringToMap(value);
  266. if (MapUtils.isNotEmpty(lcationMap)) {
  267. value = lcationMap.get("fullAddress") != null ? lcationMap.get("fullAddress").toString() : "";
  268. }
  269. break;
  270. default:
  271. break;
  272. }
  273. return value;
  274. }
  275. return null;
  276. }
  277. }