Bladeren bron

feat: implement operation logging for customer data creation, update, and deletion in SysCustomerDataService

张旭锋 1 maand geleden
bovenliggende
commit
cdbfe243ea

+ 48 - 0
src/main/java/com/jtzx/crm/module/sys/entity/SysCustomerDataEntity.java

@@ -3,6 +3,8 @@ package com.jtzx.crm.module.sys.entity;
 import cc.uncarbon.framework.crud.entity.HelioBaseEntity;
 import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableName;
+import com.google.common.collect.Lists;
+import com.jtzx.crm.module.sys.model.request.AdminSysCustomerDataInsertOrUpdateDTO;
 import io.swagger.v3.oas.annotations.media.Schema;
 import lombok.AllArgsConstructor;
 import lombok.Data;
@@ -12,6 +14,7 @@ import lombok.experimental.Accessors;
 import lombok.experimental.SuperBuilder;
 
 import java.time.LocalDateTime;
+import java.util.List;
 
 
 /**
@@ -102,4 +105,49 @@ public class SysCustomerDataEntity extends HelioBaseEntity<Long> {
     @TableField(value = "success_status")
     private Integer successStatus;
 
+    /**
+     * 比较实体变化
+     * @param dto 更新的数据
+     * @return 变化的字符串,分为两个部分,前部分为原来的信息,后部分为修改后的信息,用","隔开
+     *         例如:学校名称【ABC】学段名称【初中】, 学校名称【XYZ】学段名称【高中】
+     */
+    public String compareTo(AdminSysCustomerDataInsertOrUpdateDTO dto) {
+        // 1. 先收集所有的变更,存放在集合中,如果为空,则说明没有变更,直接返回空字符串
+        List<String> changesOld = Lists.newArrayList();
+        List<String> changesNew = Lists.newArrayList();
+
+        // 2. 挨个比较字段,把不一致的变更加到集合里
+        if (this.name != null && !this.name.equals(dto.getName())) {
+            changesOld.add("学校名称【" + this.name + "】");
+            changesNew.add("学校名称【" + dto.getName() + "】");
+        }
+
+        if (this.stageName != null && !this.stageName.equals(dto.getStageName())) {
+            changesOld.add("学段名称【" + this.stageName + "】");
+            changesNew.add("学段名称【" + dto.getStageName() + "】");
+        }
+
+        if (this.provinceName != null && !this.provinceName.equals(dto.getProvinceName())) {
+            changesOld.add("省份名称【" + this.provinceName + "】");
+            changesNew.add("省份名称【" + dto.getProvinceName() + "】");
+        }
+
+        if (this.cityName != null && !this.cityName.equals(dto.getCityName())) {
+            changesOld.add("城市名称【" + this.cityName + "】");
+            changesNew.add("城市名称【" + dto.getCityName() + "】");
+        }
+
+        if (this.address != null && !this.address.equals(dto.getAddress())) {
+            changesOld.add("地址【" + this.address + "】");
+            changesNew.add("地址【" + dto.getAddress() + "】");
+        }
+
+        // 3. 用逗号连接所有变更,生成最终字符串
+        if (changesOld.isEmpty() && changesNew.isEmpty()) {
+            return null;
+        } else {
+            return String.join("", changesOld) + "," + String.join("", changesNew);
+        }
+    }
+
 }

+ 5 - 0
src/main/java/com/jtzx/crm/module/sys/model/request/AdminSysCustomerDataInsertOrUpdateDTO.java

@@ -53,4 +53,9 @@ public class AdminSysCustomerDataInsertOrUpdateDTO implements Serializable {
     @Schema(description = "地址")
     @Size(max = 255, message = "【地址】最长255位")
     private String address;
+
+    public String getStr() {
+        return "学校名称【" + this.name + "】学段名称【" + this.stageName + "】省份名称【" + this.provinceName
+                + "】城市名称【" + this.cityName + "】地址【" + this.address + "】";
+    }
 }

+ 110 - 6
src/main/java/com/jtzx/crm/module/sys/service/SysCustomerDataService.java

@@ -1,9 +1,16 @@
 package com.jtzx.crm.module.sys.service;
 
+import cc.uncarbon.framework.core.context.UserContextHolder;
 import cc.uncarbon.framework.core.exception.BusinessException;
 import cn.hutool.core.bean.BeanUtil;
+
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.jtzx.crm.module.sys.entity.SysCustomerDataEntity;
+import com.jtzx.crm.module.sys.entity.SysCustomerOperLogEntity;
+import com.jtzx.crm.module.sys.entity.SysUserGroupRelationEntity;
 import com.jtzx.crm.module.sys.mapper.SysCustomerDataMapper;
+import com.jtzx.crm.module.sys.mapper.SysCustomerOperLogMapper;
+import com.jtzx.crm.module.sys.mapper.SysUserGroupRelationMapper;
 import com.jtzx.crm.module.sys.model.request.AdminSysCustomerDataInsertOrUpdateDTO;
 import com.jtzx.crm.module.sys.model.response.SysCustomerDataBO;
 import lombok.RequiredArgsConstructor;
@@ -25,6 +32,10 @@ public class SysCustomerDataService {
 
     private final SysCustomerDataMapper sysCustomerDataMapper;
 
+    private final SysCustomerOperLogMapper sysCustomerOperLogMapper;
+
+    private final SysUserGroupRelationMapper sysUserGroupRelationMapper;
+
 
     /**
      * 根据 ID 取详情
@@ -53,7 +64,37 @@ public class SysCustomerDataService {
         entity.setClaimStatus(0);
         entity.setSuccessStatus(0);
         sysCustomerDataMapper.insert(entity);
-        //TODO 添加操作日志
+
+        SysUserGroupRelationEntity userGroupRelation = sysUserGroupRelationMapper.selectOne(
+                new LambdaQueryWrapper<SysUserGroupRelationEntity>()
+                        .select(
+                                SysUserGroupRelationEntity::getId,
+                                SysUserGroupRelationEntity::getGroupId
+                        )
+                        .eq(SysUserGroupRelationEntity::getUserId, UserContextHolder.getUserId())
+                        .eq(SysUserGroupRelationEntity::getProvinceId, entity.getProvinceId())
+                        .eq(SysUserGroupRelationEntity::getCityId, entity.getCityId())
+        );
+
+        //添加操作日志
+        String operData = dto.getStr();
+        SysCustomerOperLogEntity logEntity = SysCustomerOperLogEntity.builder()
+                .customerId(entity.getId())
+                .customerName(entity.getName())
+                .operUserId(UserContextHolder.getUserId())
+                .operName(UserContextHolder.getUserName())
+                .operAction(11)
+                .operDatetime(LocalDateTime.now())
+                .provinceId(entity.getProvinceId())
+                .provinceName(entity.getProvinceName())
+                .cityId(entity.getCityId())
+                .cityName(entity.getCityName())
+                .content(operData)
+                .build();
+        if (userGroupRelation != null) {
+            logEntity.setOperUserGroupId(userGroupRelation.getGroupId());
+        }
+        sysCustomerOperLogMapper.insert(logEntity);
         return entity.getId();
     }
 
@@ -63,19 +104,82 @@ public class SysCustomerDataService {
     @Transactional(rollbackFor = Exception.class)
     public void adminUpdate(AdminSysCustomerDataInsertOrUpdateDTO dto) {
         log.info("[后台管理-客户(学校)数据-编辑] >> DTO={}", dto);
-        SysCustomerDataEntity entity = new SysCustomerDataEntity();
+        SysCustomerDataEntity entity = sysCustomerDataMapper.selectById(dto.getId());
+        if (entity == null) {
+            throw new BusinessException(400, "无效客户(学校)数据ID");
+        }
+        String compareTo = entity.compareTo(dto);
         BeanUtil.copyProperties(dto, entity);
         sysCustomerDataMapper.updateById(entity);
-        //TODO 添加操作日志
+
+        //添加操作日志
+        SysUserGroupRelationEntity userGroupRelation = sysUserGroupRelationMapper.selectOne(
+                new LambdaQueryWrapper<SysUserGroupRelationEntity>()
+                        .select(
+                                SysUserGroupRelationEntity::getId,
+                                SysUserGroupRelationEntity::getGroupId
+                        )
+                        .eq(SysUserGroupRelationEntity::getUserId, UserContextHolder.getUserId())
+                        .eq(SysUserGroupRelationEntity::getProvinceId, entity.getProvinceId())
+                        .eq(SysUserGroupRelationEntity::getCityId, entity.getCityId())
+        );
+        SysCustomerOperLogEntity logEntity = SysCustomerOperLogEntity.builder()
+                .customerId(entity.getId())
+                .customerName(entity.getName())
+                .operUserId(UserContextHolder.getUserId())
+                .operName(UserContextHolder.getUserName())
+                .operAction(12)
+                .operDatetime(LocalDateTime.now())
+                .provinceId(entity.getProvinceId())
+                .provinceName(entity.getProvinceName())
+                .cityId(entity.getCityId())
+                .cityName(entity.getCityName())
+                .content(compareTo)
+                .build();
+        if (userGroupRelation != null) {
+            logEntity.setOperUserGroupId(userGroupRelation.getGroupId());
+        }
+        sysCustomerOperLogMapper.insert(logEntity);
     }
 
     /**
      * 后台管理-删除
      */
     @Transactional(rollbackFor = Exception.class)
-    public void adminDelete(Collection<Long> ids) {
-        log.info("[后台管理-客户(学校)数据-删除] >> ids={}", ids);
-        sysCustomerDataMapper.deleteByIds(ids);
+    public void adminDelete(Long id) {
+        log.info("[后台管理-客户(学校)数据-删除] >> id={}", id);
+        SysCustomerDataEntity entity = sysCustomerDataMapper.selectById(id);
+        if (entity != null) {
+            sysCustomerDataMapper.deleteById(id);
+
+            SysUserGroupRelationEntity userGroupRelation = sysUserGroupRelationMapper.selectOne(
+                    new LambdaQueryWrapper<SysUserGroupRelationEntity>()
+                            .select(
+                                    SysUserGroupRelationEntity::getId,
+                                    SysUserGroupRelationEntity::getGroupId
+                            )
+                            .eq(SysUserGroupRelationEntity::getUserId, UserContextHolder.getUserId())
+                            .eq(SysUserGroupRelationEntity::getProvinceId, entity.getProvinceId())
+                            .eq(SysUserGroupRelationEntity::getCityId, entity.getCityId())
+            );
+            SysCustomerOperLogEntity logEntity = SysCustomerOperLogEntity.builder()
+                    .customerId(entity.getId())
+                    .customerName(entity.getName())
+                    .operUserId(UserContextHolder.getUserId())
+                    .operName(UserContextHolder.getUserName())
+                    .operAction(13)
+                    .operDatetime(LocalDateTime.now())
+                    .provinceId(entity.getProvinceId())
+                    .provinceName(entity.getProvinceName())
+                    .cityId(entity.getCityId())
+                    .cityName(entity.getCityName())
+                    .content(entity.getName())
+                    .build();
+            if (userGroupRelation != null) {
+                logEntity.setOperUserGroupId(userGroupRelation.getGroupId());
+            }
+            sysCustomerOperLogMapper.insert(logEntity);
+        }
     }