21 changed files with 626 additions and 12 deletions
@ -0,0 +1,20 @@ |
|||||
|
package com.qs.serve.common.model.annotation; |
||||
|
|
||||
|
import java.lang.annotation.*; |
||||
|
|
||||
|
/** |
||||
|
* @author YenHex |
||||
|
* @since 2023/4/25 |
||||
|
*/ |
||||
|
@Target({ElementType.FIELD}) |
||||
|
@Retention(RetentionPolicy.RUNTIME) |
||||
|
@Documented |
||||
|
public @interface BusinessDifference { |
||||
|
|
||||
|
/** |
||||
|
* 字段备注 |
||||
|
* @return |
||||
|
*/ |
||||
|
String value() default ""; |
||||
|
|
||||
|
} |
@ -0,0 +1,17 @@ |
|||||
|
package com.qs.serve.common.model.dto; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
/** |
||||
|
* @author YenHex |
||||
|
* @since 2023/4/25 |
||||
|
*/ |
||||
|
@Data |
||||
|
public class DiffFieldVal { |
||||
|
|
||||
|
String field; |
||||
|
String comment; |
||||
|
String orgValue; |
||||
|
String newValue; |
||||
|
|
||||
|
} |
@ -0,0 +1,79 @@ |
|||||
|
package com.qs.serve.common.util; |
||||
|
|
||||
|
import com.qs.serve.common.framework.mybatis.join.annotations.BindEntity; |
||||
|
import com.qs.serve.common.model.annotation.BusinessDifference; |
||||
|
import com.qs.serve.common.model.dto.DiffFieldVal; |
||||
|
import com.qs.serve.modules.bms.entity.BmsSupplierContacts; |
||||
|
import com.qs.serve.modules.sys.entity.SysBusinessLog; |
||||
|
import lombok.SneakyThrows; |
||||
|
|
||||
|
import java.lang.annotation.Annotation; |
||||
|
import java.lang.reflect.Field; |
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
|
||||
|
/** |
||||
|
* @author YenHex |
||||
|
* @since 2023/4/25 |
||||
|
*/ |
||||
|
public class BusinessDifferenceUtil { |
||||
|
|
||||
|
@SneakyThrows |
||||
|
public static <T> List<DiffFieldVal> getDifferenceList(T orgVal, T newVal){ |
||||
|
List<DiffFieldVal> diffFieldValList = new ArrayList<>(); |
||||
|
if(orgVal==null||newVal==null){ |
||||
|
return diffFieldValList; |
||||
|
} |
||||
|
Class<?> clazz = null; |
||||
|
try { |
||||
|
clazz = Class.forName(orgVal.getClass().getName()); |
||||
|
} catch (ClassNotFoundException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
if(clazz==null){return diffFieldValList;} |
||||
|
Field[] fields = clazz.getDeclaredFields(); |
||||
|
for (Field field : fields) { |
||||
|
for (Annotation annotation : field.getAnnotations()) { |
||||
|
if(annotation instanceof BusinessDifference){ |
||||
|
BusinessDifference busDiff = (BusinessDifference) annotation; |
||||
|
field.setAccessible(true); |
||||
|
Object orgValue = field.get(orgVal); |
||||
|
Object newValue = field.get(newVal); |
||||
|
if(newValue==null){ |
||||
|
continue; |
||||
|
} |
||||
|
if(orgValue==null){ |
||||
|
orgValue = ""; |
||||
|
} |
||||
|
if(!orgValue.equals(newValue)){ |
||||
|
DiffFieldVal fieldVal = new DiffFieldVal(); |
||||
|
fieldVal.setField(field.getName()); |
||||
|
fieldVal.setNewValue(newValue.toString()); |
||||
|
fieldVal.setOrgValue(orgValue.toString()); |
||||
|
fieldVal.setComment(busDiff.value()); |
||||
|
diffFieldValList.add(fieldVal); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
return diffFieldValList; |
||||
|
} |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
BmsSupplierContacts supplierContacts = new BmsSupplierContacts(); |
||||
|
supplierContacts.setContactsName("名称1"); |
||||
|
supplierContacts.setContactsPost("setContactsPost"); |
||||
|
|
||||
|
|
||||
|
BmsSupplierContacts supplierContacts2 = new BmsSupplierContacts(); |
||||
|
supplierContacts2.setContactsName("名称2"); |
||||
|
supplierContacts2.setContactsNumber("123456"); |
||||
|
supplierContacts2.setContactsPost("setContactsPost"); |
||||
|
|
||||
|
List<DiffFieldVal> diffFieldValList =BusinessDifferenceUtil.getDifferenceList(supplierContacts,supplierContacts2); |
||||
|
for (DiffFieldVal fieldVal : diffFieldValList) { |
||||
|
System.out.println(JsonUtil.objectToJson(fieldVal)); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,19 @@ |
|||||
|
package com.qs.serve.modules.sys.common.enums; |
||||
|
|
||||
|
import lombok.AllArgsConstructor; |
||||
|
import lombok.Getter; |
||||
|
|
||||
|
/** |
||||
|
* @author YenHex |
||||
|
* @since 2023/4/25 |
||||
|
*/ |
||||
|
@Getter |
||||
|
@AllArgsConstructor |
||||
|
public enum BusinessLogOption { |
||||
|
|
||||
|
SAVE("新增"), |
||||
|
UPDATE("修改"), |
||||
|
DELETE("删除"); |
||||
|
String label; |
||||
|
|
||||
|
} |
@ -0,0 +1,20 @@ |
|||||
|
package com.qs.serve.modules.sys.common.enums; |
||||
|
|
||||
|
import lombok.AllArgsConstructor; |
||||
|
import lombok.Getter; |
||||
|
|
||||
|
/** |
||||
|
* @author YenHex |
||||
|
* @since 2023/4/25 |
||||
|
*/ |
||||
|
@Getter |
||||
|
@AllArgsConstructor |
||||
|
public enum BusinessLogType { |
||||
|
|
||||
|
SupplierContacts("客户联系人"), |
||||
|
SupplierAddress("客户地址"), |
||||
|
Point("网点信息"); |
||||
|
|
||||
|
String label; |
||||
|
|
||||
|
} |
@ -0,0 +1,73 @@ |
|||||
|
package com.qs.serve.modules.sys.controller; |
||||
|
|
||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
||||
|
import com.qs.serve.common.model.annotation.SysLog; |
||||
|
import com.qs.serve.common.model.dto.PageVo; |
||||
|
import com.qs.serve.common.model.dto.R; |
||||
|
import com.qs.serve.common.model.enums.BizType; |
||||
|
import com.qs.serve.common.model.enums.SystemModule; |
||||
|
import com.qs.serve.common.util.PageUtil; |
||||
|
import com.qs.serve.common.util.CopierUtil; |
||||
|
import com.qs.serve.common.util.StringUtils; |
||||
|
import lombok.AllArgsConstructor; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.web.bind.annotation.*; |
||||
|
|
||||
|
import com.qs.serve.modules.sys.entity.SysBusinessLog; |
||||
|
import com.qs.serve.modules.sys.service.SysBusinessLogService; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
/** |
||||
|
* 系统 系统业务日志 |
||||
|
* @author YenHex |
||||
|
* @since 2023-04-25 |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
@AllArgsConstructor |
||||
|
@RestController |
||||
|
@RequestMapping("sys/businessLog") |
||||
|
public class SysBusinessLogController { |
||||
|
|
||||
|
private SysBusinessLogService sysBusinessLogService; |
||||
|
|
||||
|
/** |
||||
|
* 翻页 |
||||
|
* @param param |
||||
|
* @return |
||||
|
*/ |
||||
|
@GetMapping("/page") |
||||
|
public R<PageVo<SysBusinessLog>> getPage(SysBusinessLog param){ |
||||
|
LambdaQueryWrapper<SysBusinessLog> lqw = new LambdaQueryWrapper<>(param); |
||||
|
PageUtil.startPage(); |
||||
|
List<SysBusinessLog> list = sysBusinessLogService.list(lqw); |
||||
|
return R.byPageHelperList(list); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* ID查询 |
||||
|
* @param id |
||||
|
* @return |
||||
|
*/ |
||||
|
@GetMapping("/getById/{id}") |
||||
|
public R<SysBusinessLog> getById(@PathVariable("id") String id){ |
||||
|
SysBusinessLog sysBusinessLog = sysBusinessLogService.getById(id); |
||||
|
return R.ok(sysBusinessLog); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* 删除 |
||||
|
* @param ids |
||||
|
* @return |
||||
|
*/ |
||||
|
@DeleteMapping("/deleteById/{ids}") |
||||
|
public R<?> deleteById(@PathVariable("ids") String ids){ |
||||
|
List<Long> idsLong = StringUtils.splitIdLong(ids); |
||||
|
boolean result = sysBusinessLogService.removeByIds(idsLong); |
||||
|
return R.isTrue(result); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
@ -0,0 +1,147 @@ |
|||||
|
package com.qs.serve.modules.sys.entity; |
||||
|
|
||||
|
import java.time.LocalDate; |
||||
|
import java.io.Serializable; |
||||
|
import java.math.BigDecimal; |
||||
|
import java.time.LocalDateTime; |
||||
|
|
||||
|
import com.baomidou.mybatisplus.annotation.*; |
||||
|
import com.fasterxml.jackson.annotation.JsonFormat; |
||||
|
import com.fasterxml.jackson.annotation.JsonIgnore; |
||||
|
import com.fasterxml.jackson.annotation.JsonProperty; |
||||
|
import lombok.Data; |
||||
|
import org.hibernate.validator.constraints.Length; |
||||
|
import org.springframework.format.annotation.DateTimeFormat; |
||||
|
|
||||
|
import javax.validation.constraints.NotNull; |
||||
|
import javax.validation.constraints.NotBlank; |
||||
|
|
||||
|
/** |
||||
|
* 实体类 |
||||
|
* @author YenHex |
||||
|
* @since 2023-04-25 |
||||
|
*/ |
||||
|
@Data |
||||
|
@TableName("sys_business_log") |
||||
|
public class SysBusinessLog implements Serializable { |
||||
|
|
||||
|
private static final long serialVersionUID = 1L; |
||||
|
|
||||
|
/** id */ |
||||
|
@TableId(type = IdType.AUTO) |
||||
|
private Long id; |
||||
|
|
||||
|
/** 用户id */ |
||||
|
@Length(max = 255,message = "用户id长度不能超过255字") |
||||
|
private String userId; |
||||
|
|
||||
|
/** 用户编码 */ |
||||
|
@Length(max = 255,message = "用户编码长度不能超过255字") |
||||
|
@TableField(condition = SqlCondition.LIKE) |
||||
|
private String userCode; |
||||
|
|
||||
|
/** 用户 */ |
||||
|
@Length(max = 255,message = "用户长度不能超过255字") |
||||
|
@TableField(condition = SqlCondition.LIKE) |
||||
|
private String userName; |
||||
|
|
||||
|
/** 目标类型 */ |
||||
|
@Length(max = 255,message = "目标类型长度不能超过255字") |
||||
|
private String targetType; |
||||
|
|
||||
|
/** 目标id */ |
||||
|
@Length(max = 255,message = "目标id长度不能超过255字") |
||||
|
private String targetId; |
||||
|
|
||||
|
/** 目标 */ |
||||
|
@Length(max = 255,message = "目标长度不能超过255字") |
||||
|
@TableField(condition = SqlCondition.LIKE) |
||||
|
private String targetCode; |
||||
|
|
||||
|
/** 目标 */ |
||||
|
@Length(max = 255,message = "目标长度不能超过255字") |
||||
|
@TableField(condition = SqlCondition.LIKE) |
||||
|
private String targetName; |
||||
|
|
||||
|
/** 目标关联id */ |
||||
|
@Length(max = 255,message = "目标关联id长度不能超过255字") |
||||
|
private String targetRelateId; |
||||
|
|
||||
|
/** 操作类型 */ |
||||
|
@Length(max = 255,message = "操作类型长度不能超过255字") |
||||
|
private String optType; |
||||
|
|
||||
|
/** 原来值 */ |
||||
|
@Length(max = 255,message = "原来值长度不能超过255字") |
||||
|
private String orgJson; |
||||
|
|
||||
|
/** 新值 */ |
||||
|
@Length(max = 255,message = "新值长度不能超过255字") |
||||
|
private String newJson; |
||||
|
|
||||
|
/** 差异值 */ |
||||
|
@Length(max = 255,message = "差异值长度不能超过255字") |
||||
|
private String diffJson; |
||||
|
|
||||
|
/** 备注 */ |
||||
|
@Length(max = 255,message = "备注长度不能超过255字") |
||||
|
private String remark; |
||||
|
|
||||
|
/** 创建时间 */ |
||||
|
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") |
||||
|
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") |
||||
|
@TableField(fill = FieldFill.INSERT) |
||||
|
private LocalDateTime createTime; |
||||
|
|
||||
|
/** 最后更新时间 */ |
||||
|
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") |
||||
|
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") |
||||
|
@TableField(fill = FieldFill.UPDATE) |
||||
|
private LocalDateTime updateTime; |
||||
|
|
||||
|
/** 所属租户 */ |
||||
|
@JsonIgnore |
||||
|
@JsonProperty |
||||
|
private String tenantId; |
||||
|
|
||||
|
/** 逻辑删除标记(0:显示;1:隐藏) */ |
||||
|
@JsonIgnore |
||||
|
@JsonProperty |
||||
|
private String delFlag; |
||||
|
|
||||
|
/** 创建人 */ |
||||
|
@TableField(fill = FieldFill.INSERT) |
||||
|
private String createBy; |
||||
|
|
||||
|
/** 更新人 */ |
||||
|
@TableField(fill = FieldFill.UPDATE) |
||||
|
private String updateBy; |
||||
|
|
||||
|
|
||||
|
public static SysBusinessLog toNewObject(SysBusinessLog source){ |
||||
|
SysBusinessLog businessLog = new SysBusinessLog(); |
||||
|
businessLog.setId(source.getId()); |
||||
|
businessLog.setUserId(source.getUserId()); |
||||
|
businessLog.setUserCode(source.getUserCode()); |
||||
|
businessLog.setUserName(source.getUserName()); |
||||
|
businessLog.setTargetType(source.getTargetType()); |
||||
|
businessLog.setTargetId(source.getTargetId()); |
||||
|
businessLog.setTargetCode(source.getTargetCode()); |
||||
|
businessLog.setTargetName(source.getTargetName()); |
||||
|
businessLog.setTargetRelateId(source.getTargetRelateId()); |
||||
|
businessLog.setOptType(source.getOptType()); |
||||
|
businessLog.setOrgJson(source.getOrgJson()); |
||||
|
businessLog.setNewJson(source.getNewJson()); |
||||
|
businessLog.setDiffJson(source.getDiffJson()); |
||||
|
businessLog.setRemark(source.getRemark()); |
||||
|
businessLog.setCreateTime(source.getCreateTime()); |
||||
|
businessLog.setUpdateTime(source.getUpdateTime()); |
||||
|
businessLog.setTenantId(source.getTenantId()); |
||||
|
businessLog.setDelFlag(source.getDelFlag()); |
||||
|
businessLog.setCreateBy(source.getCreateBy()); |
||||
|
businessLog.setUpdateBy(source.getUpdateBy()); |
||||
|
return businessLog; |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
@ -0,0 +1,14 @@ |
|||||
|
package com.qs.serve.modules.sys.mapper; |
||||
|
|
||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
||||
|
import com.qs.serve.modules.sys.entity.SysBusinessLog; |
||||
|
|
||||
|
/** |
||||
|
* Mapper |
||||
|
* @author YenHex |
||||
|
* @date 2023-04-25 |
||||
|
*/ |
||||
|
public interface SysBusinessLogMapper extends BaseMapper<SysBusinessLog> { |
||||
|
|
||||
|
} |
||||
|
|
@ -0,0 +1,25 @@ |
|||||
|
package com.qs.serve.modules.sys.service; |
||||
|
|
||||
|
import com.baomidou.mybatisplus.extension.service.IService; |
||||
|
import com.qs.serve.modules.sys.common.enums.BusinessLogOption; |
||||
|
import com.qs.serve.modules.sys.common.enums.BusinessLogType; |
||||
|
import com.qs.serve.modules.sys.entity.SysBusinessLog; |
||||
|
|
||||
|
/** |
||||
|
* 服务接口 |
||||
|
* @author YenHex |
||||
|
* @date 2023-04-25 |
||||
|
*/ |
||||
|
public interface SysBusinessLogService extends IService<SysBusinessLog> { |
||||
|
|
||||
|
|
||||
|
void buildLog4Save(BusinessLogType logType, String targetId,String targetCode,String targetName, String targetRelateId,Object newVal); |
||||
|
|
||||
|
void buildLog4Delete(BusinessLogType logType, String targetId,String targetCode,String targetName,String targetRelateId); |
||||
|
|
||||
|
void buildLog4Change(BusinessLogType logType, String targetId,String targetCode,String targetName,String targetRelateId, Object orgVal, Object newVal); |
||||
|
|
||||
|
void baseBuildLog(BusinessLogType logType, String targetId,String targetCode,String targetName,String targetRelateId, Object orgVal, Object newVal,BusinessLogOption option); |
||||
|
|
||||
|
} |
||||
|
|
@ -0,0 +1,82 @@ |
|||||
|
package com.qs.serve.modules.sys.service.impl; |
||||
|
|
||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
||||
|
import com.qs.serve.common.model.dto.DiffFieldVal; |
||||
|
import com.qs.serve.common.util.AuthContextUtils; |
||||
|
import com.qs.serve.common.util.BusinessDifferenceUtil; |
||||
|
import com.qs.serve.common.util.JsonUtil; |
||||
|
import com.qs.serve.modules.sys.common.enums.BusinessLogOption; |
||||
|
import com.qs.serve.modules.sys.common.enums.BusinessLogType; |
||||
|
import com.qs.serve.modules.sys.entity.SysUser; |
||||
|
import com.qs.serve.modules.sys.mapper.SysUserMapper; |
||||
|
import lombok.AllArgsConstructor; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import com.qs.serve.modules.sys.entity.SysBusinessLog; |
||||
|
import com.qs.serve.modules.sys.service.SysBusinessLogService; |
||||
|
import com.qs.serve.modules.sys.mapper.SysBusinessLogMapper; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
/** |
||||
|
* 服务实现类 |
||||
|
* @author YenHex |
||||
|
* @since 2023-04-25 |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
@Service |
||||
|
@AllArgsConstructor |
||||
|
public class SysBusinessLogServiceImpl extends ServiceImpl<SysBusinessLogMapper,SysBusinessLog> implements SysBusinessLogService { |
||||
|
|
||||
|
SysUserMapper sysUserMapper; |
||||
|
|
||||
|
@Override |
||||
|
public void buildLog4Save(BusinessLogType logType, String targetId,String targetCode,String targetName,String targetRelateId, Object newVal) { |
||||
|
BusinessLogOption option = BusinessLogOption.SAVE; |
||||
|
this.baseBuildLog(logType,targetId,targetCode,targetName,targetRelateId,null,newVal,option); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void buildLog4Delete(BusinessLogType logType, String targetId,String targetCode,String targetName,String targetRelateId) { |
||||
|
BusinessLogOption option = BusinessLogOption.DELETE; |
||||
|
this.baseBuildLog(logType,targetId,targetCode,targetName,targetRelateId,null,null,option); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void buildLog4Change(BusinessLogType logType, String targetId,String targetCode,String targetName,String targetRelateId, Object orgVal, Object newVal) { |
||||
|
BusinessLogOption option = BusinessLogOption.UPDATE; |
||||
|
this.baseBuildLog(logType,targetId,targetCode,targetName,targetRelateId,orgVal,newVal,option); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void baseBuildLog(BusinessLogType logType, String targetId,String targetCode,String targetName, String targetRelateId,Object orgVal, Object newVal, BusinessLogOption option) { |
||||
|
try { |
||||
|
List<DiffFieldVal> diffFieldValList = BusinessDifferenceUtil.getDifferenceList(orgVal,newVal); |
||||
|
if(option.equals(BusinessLogOption.UPDATE)&&diffFieldValList.size()<1){ |
||||
|
return; |
||||
|
} |
||||
|
SysBusinessLog businessLog = new SysBusinessLog(); |
||||
|
SysUser sysUser = sysUserMapper.selectById(AuthContextUtils.getSysUserId()); |
||||
|
businessLog.setUserId(sysUser.getId()); |
||||
|
businessLog.setUserCode(sysUser.getCode()); |
||||
|
businessLog.setUserName(sysUser.getName()); |
||||
|
businessLog.setTargetType(logType.getLabel()); |
||||
|
businessLog.setTargetId(targetId); |
||||
|
businessLog.setOptType(option.getLabel()); |
||||
|
if(orgVal!=null){ |
||||
|
businessLog.setOrgJson(JsonUtil.objectToJson(orgVal)); |
||||
|
} |
||||
|
if(newVal!=null){ |
||||
|
businessLog.setNewJson(JsonUtil.objectToJson(newVal)); |
||||
|
} |
||||
|
businessLog.setDiffJson(JsonUtil.objectToJson(diffFieldValList)); |
||||
|
businessLog.setTargetCode(targetCode); |
||||
|
businessLog.setTargetName(targetName); |
||||
|
businessLog.setTargetRelateId(targetRelateId); |
||||
|
this.save(businessLog); |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
Loading…
Reference in new issue