diff --git a/ruoyi-admin/src/main/java/com/ruoyi/system/controller/SysDiffLogController.java b/ruoyi-admin/src/main/java/com/ruoyi/system/controller/SysDiffLogController.java new file mode 100644 index 00000000..9fa7df98 --- /dev/null +++ b/ruoyi-admin/src/main/java/com/ruoyi/system/controller/SysDiffLogController.java @@ -0,0 +1,60 @@ +package com.ruoyi.system.controller; + +import java.util.List; + +import com.ruoyi.system.domain.Vo.SysDiffLogVo; +import org.apache.shiro.authz.annotation.RequiresPermissions; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.*; +import com.ruoyi.common.annotation.Log; +import com.ruoyi.common.enums.BusinessType; +import com.ruoyi.system.domain.SysDiffLog; +import com.ruoyi.system.service.ISysDiffLogService; +import com.ruoyi.common.core.controller.BaseController; +import com.ruoyi.common.core.domain.AjaxResult; +import com.ruoyi.common.utils.poi.ExcelUtil; +import com.ruoyi.common.core.page.TableDataInfo; + +/** + * 数据修改记录日志Controller + * + * @author 刘晓旭 + * @date 2024-10-15 + */ +@Controller +@RequestMapping("/system/diffLog") +public class SysDiffLogController extends BaseController +{ + private String prefix = "system/diffLog"; + + @Autowired + private ISysDiffLogService sysDiffLogService; + + /** + * 查询数据修改记录日志列表 + */ + @PostMapping("/list") + @ResponseBody + public TableDataInfo list( + @RequestParam Long businessId, + @RequestParam String businessKey + ) { + startPage(); + List list = sysDiffLogService.getDiffLogList(businessId,businessKey); + return getDataTable(list); + } + + + /** + * 打开数据修改记录前端页面 + * */ + @GetMapping("/getDiffDataList/{businessId}/{businessKey}") + public String getDiffDataList(@PathVariable Long businessId, @PathVariable String businessKey,ModelMap mmap){ + mmap.put("businessId", businessId); + mmap.put("businessKey", businessKey); + return prefix + "/diffLog"; + } + +} diff --git a/ruoyi-admin/src/main/java/com/ruoyi/system/controller/SysProductModelController.java b/ruoyi-admin/src/main/java/com/ruoyi/system/controller/SysProductModelController.java index f2ef2c85..d14cd034 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/system/controller/SysProductModelController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/system/controller/SysProductModelController.java @@ -3,6 +3,7 @@ package com.ruoyi.system.controller; import com.ruoyi.common.annotation.Log; import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.AjaxResult; +import com.ruoyi.common.core.domain.entity.SysFieldDifferent; import com.ruoyi.common.core.domain.entity.SysUser; import com.ruoyi.common.core.page.TableDataInfo; import com.ruoyi.common.enums.BusinessType; @@ -206,4 +207,5 @@ public class SysProductModelController extends BaseController + } diff --git a/ruoyi-admin/src/main/java/com/ruoyi/system/domain/SysDiffLog.java b/ruoyi-admin/src/main/java/com/ruoyi/system/domain/SysDiffLog.java new file mode 100644 index 00000000..0b4c6d1b --- /dev/null +++ b/ruoyi-admin/src/main/java/com/ruoyi/system/domain/SysDiffLog.java @@ -0,0 +1,84 @@ +package com.ruoyi.system.domain; + +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import com.ruoyi.common.annotation.Excel; +import com.ruoyi.common.core.domain.BaseEntity; + +/** + * 数据修改记录日志对象 sys_diff_log + * + * @author 刘晓旭 + * @date 2024-10-15 + */ +public class SysDiffLog extends BaseEntity +{ + private static final long serialVersionUID = 1L; + + /** 日志主键 */ + private Long diffId; + + /** 业务id */ + @Excel(name = "业务id") + private Long businessId; + + /** 业务名称 */ + @Excel(name = "业务名称") + private String businessKey; + + /** 业务更改数据 */ + @Excel(name = "业务更改数据") + private String diffData; + + public void setDiffId(Long diffId) + { + this.diffId = diffId; + } + + public Long getDiffId() + { + return diffId; + } + + public Long getBusinessId() { + return businessId; + } + + public void setBusinessId(Long businessId) { + this.businessId = businessId; + } + + public void setBusinessKey(String businessKey) + { + this.businessKey = businessKey; + } + + public String getBusinessKey() + { + return businessKey; + } + public void setDiffData(String diffData) + { + this.diffData = diffData; + } + + public String getDiffData() + { + return diffData; + } + + @Override + public String toString() { + return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE) + .append("diffId", getDiffId()) + .append("businessId", getBusinessId()) + .append("businessKey", getBusinessKey()) + .append("diffData", getDiffData()) + .append("createBy", getCreateBy()) + .append("createTime", getCreateTime()) + .append("updateBy", getUpdateBy()) + .append("updateTime", getUpdateTime()) + .append("remark", getRemark()) + .toString(); + } +} diff --git a/ruoyi-admin/src/main/java/com/ruoyi/system/domain/SysProductModel.java b/ruoyi-admin/src/main/java/com/ruoyi/system/domain/SysProductModel.java index f40afd92..e8443b75 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/system/domain/SysProductModel.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/system/domain/SysProductModel.java @@ -3,6 +3,7 @@ package com.ruoyi.system.domain; import cn.hutool.core.date.DateTime; import com.fasterxml.jackson.annotation.JsonFormat; import com.ruoyi.common.annotation.Excel; +import com.ruoyi.common.annotation.FieldCompare; import com.ruoyi.common.core.domain.BaseEntity; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; @@ -31,21 +32,26 @@ public class SysProductModel extends BaseEntity private String engineer; /** 设备型号 */ + @FieldCompare(chineseName = "设备型号") @Excel(name = "设备型号") private String equipModel; + @FieldCompare(chineseName = "设备名称") /** 设备名称 */ @Excel(name = "设备名称") private String equipName; + @FieldCompare(chineseName = "规格说明") /** 规格说明 */ @Excel(name = "规格说明") private String specification; + @FieldCompare(chineseName = "差异说明") /** 差异说明 */ @Excel(name = "差异说明") private String differences; + @FieldCompare(chineseName = "备注") /** 备注 */ @Excel(name = "备注") private String remark; diff --git a/ruoyi-admin/src/main/java/com/ruoyi/system/domain/Vo/SysDiffLogVo.java b/ruoyi-admin/src/main/java/com/ruoyi/system/domain/Vo/SysDiffLogVo.java new file mode 100644 index 00000000..00ce40bf --- /dev/null +++ b/ruoyi-admin/src/main/java/com/ruoyi/system/domain/Vo/SysDiffLogVo.java @@ -0,0 +1,35 @@ +package com.ruoyi.system.domain.Vo; + +import com.ruoyi.common.core.domain.BaseEntity; +import lombok.Data; + +import java.util.Date; + + +/** + * 用于展示修改记录日志 + * + * @author 刘晓旭 + * @date 2024-10-16 + */ +@Data +public class SysDiffLogVo{ + + + //修改的字段 + private String fieldName; + + //修改前 + private String before; + + //修改后 + private String after; + + //修改的时间 + private Date updateTime; + + //修改的人 + private String updateBy; + + +} diff --git a/ruoyi-admin/src/main/java/com/ruoyi/system/mapper/SysDiffLogMapper.java b/ruoyi-admin/src/main/java/com/ruoyi/system/mapper/SysDiffLogMapper.java new file mode 100644 index 00000000..5178dab7 --- /dev/null +++ b/ruoyi-admin/src/main/java/com/ruoyi/system/mapper/SysDiffLogMapper.java @@ -0,0 +1,85 @@ +package com.ruoyi.system.mapper; + +import java.util.List; +import com.ruoyi.system.domain.SysDiffLog; + +/** + * 数据修改记录日志Mapper接口 + * + * @author 刘晓旭 + * @date 2024-10-15 + */ +public interface SysDiffLogMapper +{ + /** + * 查询数据修改记录日志 + * + * @param diffId 数据修改记录日志ID + * @return 数据修改记录日志 + */ + public SysDiffLog selectSysDiffLogById(Long diffId); + + /** + * 查询数据修改记录日志列表 + * + * @param sysDiffLog 数据修改记录日志 + * @return 数据修改记录日志集合 + */ + public List selectSysDiffLogList(SysDiffLog sysDiffLog); + + /** + * 查询数据修改记录日志列表 + * + * @param sysDiffLog 数据修改记录日志 + * @return 数据修改记录日志集合 + */ + public SysDiffLog selectSysDiffLogByBusiness(SysDiffLog sysDiffLog); + + /** + * 新增数据修改记录日志 + * + * @param sysDiffLog 数据修改记录日志 + * @return 结果 + */ + public int insertSysDiffLog(SysDiffLog sysDiffLog); + + /** + * 修改数据修改记录日志 + * + * @param sysDiffLog 数据修改记录日志 + * @return 结果 + */ + public int updateSysDiffLog(SysDiffLog sysDiffLog); + + /** + * 删除数据修改记录日志 + * + * @param diffId 数据修改记录日志ID + * @return 结果 + */ + public int deleteSysDiffLogById(Long diffId); + + /** + * 批量删除数据修改记录日志 + * + * @param diffIds 需要删除的数据ID + * @return 结果 + */ + public int deleteSysDiffLogByIds(String[] diffIds); + + /** + * 作废数据修改记录日志 + * + * @param diffId 数据修改记录日志ID + * @return 结果 + */ + public int cancelSysDiffLogById(Long diffId); + + /** + * 恢复数据修改记录日志 + * + * @param diffId 数据修改记录日志ID + * @return 结果 + */ + public int restoreSysDiffLogById(Long diffId); +} diff --git a/ruoyi-admin/src/main/java/com/ruoyi/system/service/ISysDiffLogService.java b/ruoyi-admin/src/main/java/com/ruoyi/system/service/ISysDiffLogService.java new file mode 100644 index 00000000..bed7a500 --- /dev/null +++ b/ruoyi-admin/src/main/java/com/ruoyi/system/service/ISysDiffLogService.java @@ -0,0 +1,104 @@ +package com.ruoyi.system.service; + +import java.util.List; + +import com.ruoyi.common.core.domain.entity.SysFieldDifferent; +import com.ruoyi.system.domain.SysDiffLog; +import com.ruoyi.system.domain.Vo.SysDiffLogVo; + +/** + * 数据修改记录日志Service接口 + * + * @author 刘晓旭 + * @date 2024-10-15 + */ +public interface ISysDiffLogService +{ + /** + * 查询数据修改记录日志 + * + * @param diffId 数据修改记录日志ID + * @return 数据修改记录日志 + */ + public SysDiffLog selectSysDiffLogById(Long diffId); + + /** + * 查询数据修改记录日志列表 + * + * @param sysDiffLog 数据修改记录日志 + * @return 数据修改记录日志集合 + */ + public List selectSysDiffLogList(SysDiffLog sysDiffLog); + + + /** + * 查询数据修改记录日志列表 + * + * @param sysDiffLog 数据修改记录日志 + * @return 数据修改记录日志集合 + */ + public SysDiffLog selectSysDiffLogByBusiness(SysDiffLog sysDiffLog); + + /** + * 新增数据修改记录日志 + * + * @param sysDiffLog 数据修改记录日志 + * @return 结果 + */ + public int insertSysDiffLog(SysDiffLog sysDiffLog); + + /** + * 修改数据修改记录日志 + * + * @param sysDiffLog 数据修改记录日志 + * @return 结果 + */ + public int updateSysDiffLog(SysDiffLog sysDiffLog); + + + /** + * 修改数据修改记录日志通过其他模块 + * + */ + public int updateSysDiffLogByBusiness(Long businessId, String businessKey, List changes); + + /** + * 批量删除数据修改记录日志 + * + * @param ids 需要删除的数据ID + * @return 结果 + */ + public int deleteSysDiffLogByIds(String ids); + + /** + * 删除数据修改记录日志信息 + * + * @param diffId 数据修改记录日志ID + * @return 结果 + */ + public int deleteSysDiffLogById(Long diffId); + + /** + * 作废数据修改记录日志 + * @param diffId 数据修改记录日志ID + * @return + */ + int cancelSysDiffLogById(Long diffId); + + /** + * 恢复数据修改记录日志 + * @param diffId 数据修改记录日志ID + * @return + */ + int restoreSysDiffLogById(Long diffId); + + /** + * diff_data 把 数据通过JSON 序列化和反序列化 + * */ + public List parseDiffData(String diffData); + + /** + * 展示数据修改记录日志 + * */ + List getDiffLogList(Long businessId,String businessKey); +} diff --git a/ruoyi-admin/src/main/java/com/ruoyi/system/service/ISysProductModelService.java b/ruoyi-admin/src/main/java/com/ruoyi/system/service/ISysProductModelService.java index 097c52fe..97454558 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/system/service/ISysProductModelService.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/system/service/ISysProductModelService.java @@ -1,5 +1,6 @@ package com.ruoyi.system.service; +import com.ruoyi.common.core.domain.entity.SysFieldDifferent; import com.ruoyi.system.domain.SysProductModel; import java.util.List; @@ -83,4 +84,5 @@ public interface ISysProductModelService * 根据产品型号id数组查询产品型号列表集合 * */ List selectSysProductModelListByCodes(String[] pcodes); + } diff --git a/ruoyi-admin/src/main/java/com/ruoyi/system/service/impl/SysDiffLogServiceImpl.java b/ruoyi-admin/src/main/java/com/ruoyi/system/service/impl/SysDiffLogServiceImpl.java new file mode 100644 index 00000000..57f4a140 --- /dev/null +++ b/ruoyi-admin/src/main/java/com/ruoyi/system/service/impl/SysDiffLogServiceImpl.java @@ -0,0 +1,272 @@ +package com.ruoyi.system.service.impl; + +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.TypeReference; +import com.ruoyi.common.constant.BusinessKeysConstants; +import com.ruoyi.common.core.domain.entity.SysFieldDifferent; +import com.ruoyi.common.utils.DateUtils; +import com.ruoyi.common.utils.ShiroUtils; +import com.ruoyi.system.domain.Vo.SysDiffLogVo; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import com.ruoyi.system.mapper.SysDiffLogMapper; +import com.ruoyi.system.domain.SysDiffLog; +import com.ruoyi.system.service.ISysDiffLogService; +import com.ruoyi.common.core.text.Convert; + +/** + * 数据修改记录日志Service业务层处理 + * + * @author 刘晓旭 + * @date 2024-10-15 + */ +@Service +public class SysDiffLogServiceImpl implements ISysDiffLogService +{ + @Autowired + private SysDiffLogMapper sysDiffLogMapper; + + + /** + * 查询数据修改记录日志 + * + * @param diffId 数据修改记录日志ID + * @return 数据修改记录日志 + */ + @Override + public SysDiffLog selectSysDiffLogById(Long diffId) + { + return sysDiffLogMapper.selectSysDiffLogById(diffId); + } + + /** + * 查询数据修改记录日志列表 + * + * @param sysDiffLog 数据修改记录日志 + * @return 数据修改记录日志 + */ + @Override + public List selectSysDiffLogList(SysDiffLog sysDiffLog) + { + return sysDiffLogMapper.selectSysDiffLogList(sysDiffLog); + } + + + /** + * 查询数据修改记录日志列表 + * + * @param sysDiffLog 数据修改记录日志 + * @return 数据修改记录日志集合 + */ + @Override + public SysDiffLog selectSysDiffLogByBusiness(SysDiffLog sysDiffLog){ + return sysDiffLogMapper.selectSysDiffLogByBusiness(sysDiffLog); + } + + /** + * 新增数据修改记录日志 + * + * @param sysDiffLog 数据修改记录日志 + * @return 结果 + */ + @Override + public int insertSysDiffLog(SysDiffLog sysDiffLog) + { + String loginName = ShiroUtils.getLoginName(); + sysDiffLog.setCreateBy(loginName); + sysDiffLog.setCreateTime(DateUtils.getNowDate()); + return sysDiffLogMapper.insertSysDiffLog(sysDiffLog); + } + + /** + * 修改数据修改记录日志 + * + * @param sysDiffLog 数据修改记录日志 + * @return 结果 + */ + @Override + public int updateSysDiffLog(SysDiffLog sysDiffLog) + { + String loginName = ShiroUtils.getLoginName(); + sysDiffLog.setUpdateBy(loginName); + sysDiffLog.setUpdateTime(DateUtils.getNowDate()); + return sysDiffLogMapper.updateSysDiffLog(sysDiffLog); + } + + + /** + * 修改数据修改记录日志 + * + * @param businessId 业务ID + * @param businessKey 业务键 + * @param changes 字段变化列表 + * @return 更新行数 + */ + @Override + public int updateSysDiffLogByBusiness(Long businessId, String businessKey, List changes) { + SysDiffLog sysDiffLog = new SysDiffLog(); + sysDiffLog.setBusinessId(businessId); + sysDiffLog.setBusinessKey(businessKey); + SysDiffLog existingLog = sysDiffLogMapper.selectSysDiffLogByBusiness(sysDiffLog); + + if (existingLog != null) { + existingLog.setDiffData(changesToString(changes)); + existingLog.setUpdateBy(ShiroUtils.getLoginName()); + existingLog.setUpdateTime(DateUtils.getNowDate()); + return sysDiffLogMapper.updateSysDiffLog(existingLog); + } else { + sysDiffLog.setDiffData(changesToString(changes)); + sysDiffLog.setCreateBy(ShiroUtils.getLoginName()); + sysDiffLog.setCreateTime(DateUtils.getNowDate()); + sysDiffLog.setUpdateBy(ShiroUtils.getLoginName()); + sysDiffLog.setUpdateTime(DateUtils.getNowDate()); + return sysDiffLogMapper.insertSysDiffLog(sysDiffLog); + } + } + + private String changesToString(List changes) { + return changes.stream() + .map(Object::toString) + .collect(Collectors.joining(", ")); + } + + + /** + * 删除数据修改记录日志对象 + * + * @param ids 需要删除的数据ID + * @return 结果 + */ + @Override + public int deleteSysDiffLogByIds(String ids) + { + return sysDiffLogMapper.deleteSysDiffLogByIds(Convert.toStrArray(ids)); + } + + /** + * 删除数据修改记录日志信息 + * + * @param diffId 数据修改记录日志ID + * @return 结果 + */ + @Override + public int deleteSysDiffLogById(Long diffId) + { + return sysDiffLogMapper.deleteSysDiffLogById(diffId); + } + + /** + * 作废数据修改记录日志 + * + * @param diffId 数据修改记录日志ID + * @return 结果 + */ + @Override + public int cancelSysDiffLogById(Long diffId) + { + return sysDiffLogMapper.cancelSysDiffLogById(diffId); + } + + /** + * 恢复数据修改记录日志信息 + * + * @param diffId 数据修改记录日志ID + * @return 结果 + */ + @Override + public int restoreSysDiffLogById(Long diffId) + { + return sysDiffLogMapper.restoreSysDiffLogById(diffId); + } + + + + /** + * diff_data 把 数据通过JSON 序列化和反序列化 + * */ + public List parseDiffData(String diffData) { + if (diffData == null || diffData.trim().isEmpty()) { + return new ArrayList<>(); + } + + List sysFieldDifferents = new ArrayList<>(); + Pattern pattern = Pattern.compile("\\{(.*?)\\}"); + Matcher matcher = pattern.matcher(diffData); + + while (matcher.find()) { + String entry = matcher.group(1); + SysFieldDifferent sysFieldDifferent = parseSingleEntry(entry); + if (sysFieldDifferent != null) { + sysFieldDifferents.add(sysFieldDifferent); + } + } + + return sysFieldDifferents; + + } + + // 解析单个条目 + private static SysFieldDifferent parseSingleEntry(String entryStr) { + SysFieldDifferent sysFieldDifferent = new SysFieldDifferent(); + String[] parts = entryStr.split(", "); + for (String part : parts) { + String[] keyValue = part.split("="); + if (keyValue.length == 2) { + String key = keyValue[0].trim(); + String value = keyValue[1].trim(); + value = value.replaceAll("^'|'$", ""); // 去掉首尾的单引号 + switch (key) { + case "fieldName": + sysFieldDifferent.setFieldName(value); + break; + case "before": + sysFieldDifferent.setBefore(value); + break; + case "after": + sysFieldDifferent.setAfter(value); + break; + default: + break; + } + } + } + return sysFieldDifferent; + } + + + /** + * 展示数据修改记录日志 + * */ + @Override + public List getDiffLogList(Long businessId,String businessKey) { + + List sysDiffLogVos = new ArrayList<>(); + SysDiffLog sysDiffLog = new SysDiffLog(); + sysDiffLog.setBusinessId(businessId); + sysDiffLog.setBusinessKey(businessKey); + SysDiffLog tempDiffLog = sysDiffLogMapper.selectSysDiffLogByBusiness(sysDiffLog); + // 检查查询结果是否为空 + if (tempDiffLog == null || tempDiffLog.getDiffData() == null) { + return sysDiffLogVos; // 直接返回空列表 + } + List sysFieldDifferents = parseDiffData(tempDiffLog.getDiffData()); + + sysFieldDifferents.forEach(sysFieldDifferent -> { + SysDiffLogVo sysDiffLogVo = new SysDiffLogVo(); + sysDiffLogVo.setFieldName(sysFieldDifferent.getFieldName()); + sysDiffLogVo.setBefore(sysFieldDifferent.getBefore()); + sysDiffLogVo.setAfter(sysFieldDifferent.getAfter()); + sysDiffLogVo.setUpdateTime(tempDiffLog.getUpdateTime()); + sysDiffLogVo.setUpdateBy(tempDiffLog.getUpdateBy()); + + sysDiffLogVos.add(sysDiffLogVo); + }); + return sysDiffLogVos; + } +} diff --git a/ruoyi-admin/src/main/java/com/ruoyi/system/service/impl/SysProductModelServiceImpl.java b/ruoyi-admin/src/main/java/com/ruoyi/system/service/impl/SysProductModelServiceImpl.java index ae0f3017..c44650b2 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/system/service/impl/SysProductModelServiceImpl.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/system/service/impl/SysProductModelServiceImpl.java @@ -1,20 +1,27 @@ package com.ruoyi.system.service.impl; +import com.ruoyi.common.constant.BusinessKeysConstants; +import com.ruoyi.common.core.domain.entity.SysFieldDifferent; import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.common.core.text.Convert; import com.ruoyi.common.utils.DateUtils; +import com.ruoyi.common.utils.FieldCompareUtil; import com.ruoyi.common.utils.ShiroUtils; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.system.domain.SysAttach; +import com.ruoyi.system.domain.SysDiffLog; import com.ruoyi.system.domain.SysProductModel; import com.ruoyi.system.mapper.SysProductModelMapper; import com.ruoyi.system.service.ISysAttachFileService; import com.ruoyi.system.service.ISysAttachService; +import com.ruoyi.system.service.ISysDiffLogService; import com.ruoyi.system.service.ISysProductModelService; +import lombok.SneakyThrows; import org.apache.shiro.SecurityUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.CollectionUtils; import java.util.Arrays; import java.util.Date; @@ -40,6 +47,10 @@ public class SysProductModelServiceImpl implements ISysProductModelService @Autowired private ISysAttachFileService attachFileService; + + @Autowired + private ISysDiffLogService diffLogService; + /** * 查询产品型号管理 * @@ -80,6 +91,14 @@ public class SysProductModelServiceImpl implements ISysProductModelService sysProductModel.setCreateTime(DateUtils.getNowDate()); int result = sysProductModelMapper.insertSysProductModel(sysProductModel); Long pid = sysProductModel.getPid(); + + SysDiffLog sysDiffLog = new SysDiffLog(); + sysDiffLog.setBusinessId(pid); + sysDiffLog.setBusinessKey(BusinessKeysConstants.PRODUCT_MODEL); + int insertSysDiffLog = diffLogService.insertSysDiffLog(sysDiffLog); + if (insertSysDiffLog <= 0){ + throw new RuntimeException("新增产品型号管理数据修改记录失败"); + } String fileIdStr = sysProductModel.getFileIdStr(); if (StringUtils.isNotBlank(fileIdStr)){ //保存附件关联 @@ -104,11 +123,27 @@ public class SysProductModelServiceImpl implements ISysProductModelService * @param sysProductModel 产品型号管理 * @return 结果 */ + @SneakyThrows + @Transactional(rollbackFor = Exception.class) @Override public int updateSysProductModel(SysProductModel sysProductModel) { + String loginName = ShiroUtils.getLoginName(); sysProductModel.setUpdateTime(DateUtils.getNowDate()); - sysProductModel.setUpdateBy(ShiroUtils.getLoginName()); + sysProductModel.setUpdateBy(loginName); + Long pid = sysProductModel.getPid(); + SysProductModel oldProductModel = sysProductModelMapper.selectSysProductModelById(pid); + if (oldProductModel == null){ + throw new RuntimeException("产品型号管理数据不存在"); + } + List compare = FieldCompareUtil.compare(SysProductModel.class, sysProductModel,oldProductModel); + if (!CollectionUtils.isEmpty(compare)){ + int updateSysDiffLog = diffLogService.updateSysDiffLogByBusiness(pid,BusinessKeysConstants.PRODUCT_MODEL,compare); + if (updateSysDiffLog <= 0){ + throw new RuntimeException("修改产品型号管理数据修改记录失败"); + } + } + return sysProductModelMapper.updateSysProductModel(sysProductModel); } @@ -178,4 +213,5 @@ public class SysProductModelServiceImpl implements ISysProductModelService public List selectSysProductModelListByCodes(String[] pcodes) { return sysProductModelMapper.selectSysProductModelListByCodes(pcodes); } + } diff --git a/ruoyi-admin/src/main/resources/mapper/system/SysDiffLogMapper.xml b/ruoyi-admin/src/main/resources/mapper/system/SysDiffLogMapper.xml new file mode 100644 index 00000000..9e4cc6f4 --- /dev/null +++ b/ruoyi-admin/src/main/resources/mapper/system/SysDiffLogMapper.xml @@ -0,0 +1,100 @@ + + + + + + + + + + + + + + + + + + select diff_id, business_id, business_key, diff_data, create_by, create_time, update_by, update_time, remark from sys_diff_log + + + + + + + + + + insert into sys_diff_log + + business_id, + business_key, + diff_data, + create_by, + create_time, + update_by, + update_time, + remark, + + + #{businessId}, + #{businessKey}, + #{diffData}, + #{createBy}, + #{createTime}, + #{updateBy}, + #{updateTime}, + #{remark}, + + + + + update sys_diff_log + + business_id = #{businessId}, + business_key = #{businessKey}, + diff_data = #{diffData}, + create_by = #{createBy}, + create_time = #{createTime}, + update_by = #{updateBy}, + update_time = #{updateTime}, + remark = #{remark}, + + where diff_id = #{diffId} + + + + delete from sys_diff_log where diff_id = #{diffId} + + + + delete from sys_diff_log where diff_id in + + #{diffId} + + + + + update sys_diff_log set del_flag = '1' where diff_id = #{diffId} + + + + update sys_diff_log set del_flag = '0' where diff_id = #{diffId} + + + \ No newline at end of file diff --git a/ruoyi-admin/src/main/resources/templates/system/diffLog/diffLog.html b/ruoyi-admin/src/main/resources/templates/system/diffLog/diffLog.html new file mode 100644 index 00000000..7d7b984e --- /dev/null +++ b/ruoyi-admin/src/main/resources/templates/system/diffLog/diffLog.html @@ -0,0 +1,59 @@ + + + + + + +
+
+
+
+
+
+
+ + + + \ No newline at end of file diff --git a/ruoyi-admin/src/main/resources/templates/system/model/model.html b/ruoyi-admin/src/main/resources/templates/system/model/model.html index bd32b036..5a1bdcac 100644 --- a/ruoyi-admin/src/main/resources/templates/system/model/model.html +++ b/ruoyi-admin/src/main/resources/templates/system/model/model.html @@ -86,6 +86,15 @@ modalName: "产品型号管理", fixedColumns: true, // 启用冻结列 fixedRightNumber: 1, // 冻结右列个数 + onDblClickCell: function(field, value, row, $element) { + var businessId = row.pid; + var businessKey = "1"; + if(field == 'updateTime'){ + var url = ctx + "system/diffLog/getDiffDataList/" + businessId + "/" + businessKey; + $.modal.open("数据修改记录", url); + } + + }, //双击单列字段触发事件 columns: [ {checkbox: true}, { title: '产品型号管理表id',field: 'pid',visible: false}, diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/annotation/FieldCompare.java b/ruoyi-common/src/main/java/com/ruoyi/common/annotation/FieldCompare.java new file mode 100644 index 00000000..0693a5a3 --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/common/annotation/FieldCompare.java @@ -0,0 +1,25 @@ +package com.ruoyi.common.annotation; + + +import java.lang.annotation.*; + +/** + * 自定义注解:字段对比 + * + * @author 刘晓旭 + * @date 2024-10-16 +* */ +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +@Inherited +@Documented +public @interface FieldCompare { + /** 字段对应的中文 */ + String chineseName(); + + /** + * 类型映射 + * 使用方式 1:男,0:女 + */ + String properties() default ""; +} diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/constant/BusinessKeysConstants.java b/ruoyi-common/src/main/java/com/ruoyi/common/constant/BusinessKeysConstants.java new file mode 100644 index 00000000..47d6fee1 --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/common/constant/BusinessKeysConstants.java @@ -0,0 +1,15 @@ +package com.ruoyi.common.constant; + + +/** + * 修改数据记录日志业务模块常量 + * + * @author 刘晓旭 + * @date 2024-10-15 + */ +public class BusinessKeysConstants { + + + /** 测试产品型号管理 */ + public static final String PRODUCT_MODEL = "1"; +} diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/entity/SysFieldDifferent.java b/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/entity/SysFieldDifferent.java new file mode 100644 index 00000000..2747a052 --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/entity/SysFieldDifferent.java @@ -0,0 +1,62 @@ +package com.ruoyi.common.core.domain.entity; + + +/** + * 字段差异实体类 + * + * @author 刘晓旭 + * @date 2024-10-15 + */ +public class SysFieldDifferent { + //差异字段名称 + private String fieldName; + + //修改前 + private String before; + + //修改后 + private String after; + + public SysFieldDifferent() { + } + + public SysFieldDifferent(String fieldName, String before, String after) { + this.fieldName = fieldName; + this.before = before; + this.after = after; + } + + + public String getFieldName() { + return fieldName; + } + + public void setFieldName(String fieldName) { + this.fieldName = fieldName; + } + + public String getBefore() { + return before; + } + + public void setBefore(String before) { + this.before = before; + } + + public String getAfter() { + return after; + } + + public void setAfter(String after) { + this.after = after; + } + + @Override + public String toString() { + return "{" + + "fieldName='" + fieldName + '\'' + + ", before='" + before + '\'' + + ", after='" + after + '\'' + + '}'; + } +} diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/FieldCompareUtil.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/FieldCompareUtil.java new file mode 100644 index 00000000..1609af2b --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/FieldCompareUtil.java @@ -0,0 +1,92 @@ +package com.ruoyi.common.utils; + +import com.ruoyi.common.annotation.FieldCompare; +import com.ruoyi.common.core.domain.entity.SysFieldDifferent; +import com.ruoyi.common.exception.BusinessException; + +import java.beans.IntrospectionException; +import java.beans.PropertyDescriptor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.math.BigDecimal; +import java.util.*; + +/** + * 字段对比工具类 + * + * @author 刘晓旭 + * @date 2024-10-16 + * */ +public class FieldCompareUtil { + + public static List compare(Class type, T newObject, T oldObject) throws Exception { + // 变更的记录列表 + List changeLogFields = new ArrayList<>(); + + Class newObj = newObject.getClass(); + Field[] newFields = type.getDeclaredFields(); + for (int i = 0; i < newFields.length; i++) { + FieldCompare newAnnotation = newFields[i].getAnnotation(FieldCompare.class); + if (null != newAnnotation) { + PropertyDescriptor newPd = new PropertyDescriptor(newFields[i].getName(), newObj); + Method getMethodNew = newPd.getReadMethod(); + Object oldVal = getMethodNew.invoke(oldObject); + Object newVal = getMethodNew.invoke(newObject); + boolean eq = false; + if (oldVal instanceof String) { + String s1 = String.valueOf(oldVal).trim(); + String s2 = String.valueOf(newVal).trim(); + eq = !s1.equals(s2); + } else if (oldVal instanceof Integer) { + int i1 = (Integer) oldVal; + int i2 = (Integer) newVal; + eq = i1 != i2; + } else if (oldVal instanceof Double) { + double d1 = (Double) oldVal; + double d2 = (Double) newVal; + eq = d1 != d2; + } else if (oldVal instanceof BigDecimal) { + BigDecimal b1 = (BigDecimal) oldVal; + BigDecimal b2 = (BigDecimal) newVal; + eq = b1.compareTo(b2) != 0; + } else if (oldVal instanceof Long) { + long l1 = (Long) oldVal; + long l2 = (Long) newVal; + eq = l1 != l2; + } else { + eq = !oldVal.equals(newVal); + } + String s1 = oldVal == null ? "" : oldVal.toString().trim(); + String s2 = newVal == null ? "" : newVal.toString().trim(); + if (eq) { + Map map = codeToNameMap(newAnnotation); + if (map.size() > 0) { + changeLogFields.add(new SysFieldDifferent(newAnnotation.chineseName(), map.get(s1), map.get(s2))); + } else { + changeLogFields.add(new SysFieldDifferent(newAnnotation.chineseName(), s1, s2)); + } + } + } + } + return changeLogFields; + } + + /** + * 字典映射 使用方式: + * @param newAnnotation + * @return + */ + private static Map codeToNameMap(FieldCompare newAnnotation) { + Map map = new HashMap<>(); + String properties = newAnnotation.properties(); + if (properties != null && properties.length() > 0) { + String[] propertiesArr = properties.split(","); + for (String propertie : propertiesArr) { + String[] split = propertie.split(":"); + map.put(split[0], split[1]); + } + } + return map; + } + +}