Jelajahi Sumber

fix(sysCustomer): 优化权限校验及日志记录逻辑

- 调整Admin权限判断逻辑,非管理员仅允许操作本人数据
- 导入客户数据接口新增权限校验调用
- 优化客户与联系人变更内容字符串拼接,避免空串结尾
- 修改操作日志记录,只有变更内容非空时才插入日志
- 注释原有多余用户小组与区域绑定代码,简化查询逻辑
- 修正客户及联系人更新操作中的权限判别条件
- 统一格式和空格,优化代码可读性
zhangxf 3 minggu lalu
induk
melakukan
2bf1073744

+ 4 - 4
src/main/java/com/jtzx/crm/module/api/web/sys/CustomerDataPublicController.java

@@ -13,10 +13,7 @@ import com.jtzx.crm.module.api.util.CrmAdminStpUtil;
 import com.jtzx.crm.module.sys.entity.SysParamBaseDataEntity;
 import com.jtzx.crm.module.sys.model.request.*;
 import com.jtzx.crm.module.sys.model.response.*;
-import com.jtzx.crm.module.sys.service.DictCityRegionService;
-import com.jtzx.crm.module.sys.service.DictProvinceRegionService;
-import com.jtzx.crm.module.sys.service.SysCustomerDataPublicService;
-import com.jtzx.crm.module.sys.service.SysCustomerUserCommonService;
+import com.jtzx.crm.module.sys.service.*;
 import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.Parameter;
 import io.swagger.v3.oas.annotations.tags.Tag;
@@ -46,6 +43,8 @@ public class CustomerDataPublicController {
 
     private final DictCityRegionService dictCityRegionService;
 
+    private final SysCustomerDataPermissionService permissionService;
+
     @Operation(summary = "查询顶部筛选条件信息接口")
     @GetMapping(value = "/head/select/data")
     public ApiResult<SysCustomerPublicSelectBO> queryHeadSelectData() {
@@ -136,6 +135,7 @@ public class CustomerDataPublicController {
     @Operation(summary = "导入客户数据接口")
     @PostMapping(value = "/importCustomerData")
     public ApiResult<Void> importCustomerData(@Parameter(description = "文件参数") @RequestPart MultipartFile file) throws BusinessException{
+        permissionService.publicPrivateCustomerPermission();
         this.handleImportCustomerData(file);
         return ApiResult.success();
     }

+ 4 - 1
src/main/java/com/jtzx/crm/module/sys/entity/SysCustomerDataEntity.java

@@ -144,7 +144,10 @@ public class SysCustomerDataEntity extends HelioBaseEntity<Long> {
             update.append("地址【").append(dto.getAddress()).append("】");
         }
         // 3. 用逗号连接所有变更,生成最终字符串
-        return original + "," + update;
+        if(!original.isEmpty() && !update.isEmpty()){
+            return original + "," + update;
+        }
+        return null;
     }
 
     /**

+ 4 - 2
src/main/java/com/jtzx/crm/module/sys/entity/SysCustomerPersonEntity.java

@@ -101,8 +101,10 @@ public class SysCustomerPersonEntity extends HelioBaseEntity<Long> {
             original.append("备注【").append(this.remark).append("】");
             update.append("备注【").append(dto.getRemark()).append("】");
         }
-
-        return original + "," + update;
+        if(!original.isEmpty() && !update.isEmpty()){
+            return original + "," + update;
+        }
+        return null;
     }
 
 

+ 9 - 2
src/main/java/com/jtzx/crm/module/sys/model/request/AdminSysCustomerPersonInsertOrUpdateDTO.java

@@ -1,5 +1,6 @@
 package com.jtzx.crm.module.sys.model.request;
 
+import cn.hutool.core.util.StrUtil;
 import io.swagger.v3.oas.annotations.media.Schema;
 import jakarta.validation.constraints.Size;
 import lombok.AllArgsConstructor;
@@ -55,8 +56,14 @@ public class AdminSysCustomerPersonInsertOrUpdateDTO implements Serializable {
      * 新增联系人时使用
      */
     public String getStr() {
+        String str = "姓名【" + this.name + "】职务名称【" + this.positionName + "】手机号【" + this.phoneNo+"】";
         //把参数名和值转为字符串
-        return "姓名【" + this.name + "】职务名称【" + this.positionName + "】手机号【" + this.phoneNo
-                + "】办公电话【" + this.officePhoneNo + "】备注【" + this.remark + "】";
+        if(StrUtil.isNotEmpty(this.officePhoneNo)){
+            str += "办公电话【" + this.officePhoneNo + "】";
+        }
+        if(StrUtil.isNotEmpty(this.remark)){
+            str += "备注【" + this.remark + "】";
+        }
+        return str;
     }
 }

+ 25 - 21
src/main/java/com/jtzx/crm/module/sys/service/SysCustomerDataFollowService.java

@@ -2,6 +2,7 @@ package com.jtzx.crm.module.sys.service;
 
 
 import cc.uncarbon.framework.core.context.UserContextHolder;
+import cc.uncarbon.framework.core.exception.BusinessException;
 import cc.uncarbon.framework.core.page.PageResult;
 import cn.hutool.core.bean.BeanUtil;
 import cn.hutool.core.collection.CollUtil;
@@ -156,6 +157,9 @@ public class SysCustomerDataFollowService {
      * 操作日志列表查询
      */
     public SysCustomerOperLogDataBO queryOperLogData(AdminSysCustomerOperLogListDTO dto) {
+        if(!UserContextHolder.getUserContext().getRoles().contains("Admin")){
+            throw new BusinessException(400,"无权操作");
+        }
         List<Long> userIds = Lists.newArrayList();
         if (dto.getOperUserId() != null) {
             userIds.add(dto.getOperUserId());
@@ -164,27 +168,27 @@ public class SysCustomerDataFollowService {
         if (dto.getGroupId() != null) {
             groupIds.add(dto.getGroupId());
         }
-        SysCustomerSelectBO selectBO = sysCustomerUserCommonService.querySelectDataByType2(true);
-        if (selectBO != null && CollUtil.isNotEmpty(selectBO.getRegionData())) {
-            List<Long> cityIds = Lists.newArrayList();
-            selectBO.getRegionData()
-                    .forEach(provinceRegion ->
-                            provinceRegion.getSubData()
-                                    .forEach(city -> cityIds.add(city.getId())));
-            List<BindUserBO> bindUserBOS = sysCustomerUserCommonService.queryBindUserListByCityId(cityIds);
-            if (CollUtil.isNotEmpty(bindUserBOS)) {
-                if (CollUtil.isEmpty(userIds)) {
-                    userIds.addAll(
-                            bindUserBOS.stream()
-                                    .map(BindUserBO::getId).toList());
-                }
-                if (CollUtil.isEmpty(groupIds)) {
-                    groupIds.addAll(
-                            bindUserBOS.stream()
-                                    .map(BindUserBO::getGroupId).distinct().toList());
-                }
-            }
-        }
+//        SysCustomerSelectBO selectBO = sysCustomerUserCommonService.querySelectDataByType2(true);
+//        if (selectBO != null && CollUtil.isNotEmpty(selectBO.getRegionData())) {
+//            List<Long> cityIds = Lists.newArrayList();
+//            selectBO.getRegionData()
+//                    .forEach(provinceRegion ->
+//                            provinceRegion.getSubData()
+//                                    .forEach(city -> cityIds.add(city.getId())));
+//            List<BindUserBO> bindUserBOS = sysCustomerUserCommonService.queryBindUserListByCityId(cityIds);
+//            if (CollUtil.isNotEmpty(bindUserBOS)) {
+//                if (CollUtil.isEmpty(userIds)) {
+//                    userIds.addAll(
+//                            bindUserBOS.stream()
+//                                    .map(BindUserBO::getId).toList());
+//                }
+//                if (CollUtil.isEmpty(groupIds)) {
+//                    groupIds.addAll(
+//                            bindUserBOS.stream()
+//                                    .map(BindUserBO::getGroupId).distinct().toList());
+//                }
+//            }
+//        }
         return sysCustomerOperLogService.adminList(dto, userIds, groupIds, true, true);
     }
 

+ 25 - 21
src/main/java/com/jtzx/crm/module/sys/service/SysCustomerDataPrivateService.java

@@ -295,32 +295,36 @@ public class SysCustomerDataPrivateService {
         List<Long> userIds = Lists.newArrayList();
         if (dto.getOperUserId() != null) {
             userIds.add(dto.getOperUserId());
+        }else {
+            if(!UserContextHolder.getUserContext().getRoles().contains("Admin")){
+                userIds.add(UserContextHolder.getUserId());
+            }
         }
         List<Long> groupIds = Lists.newArrayList();
         if (dto.getGroupId() != null) {
             groupIds.add(dto.getGroupId());
         }
-        SysCustomerSelectBO selectBO = sysCustomerUserCommonService.querySelectDataByType1( true);
-        if (selectBO != null && CollUtil.isNotEmpty(selectBO.getRegionData())) {
-            List<Long> cityIds = Lists.newArrayList();
-            selectBO.getRegionData()
-                    .forEach(provinceRegion ->
-                            provinceRegion.getSubData()
-                                    .forEach(city -> cityIds.add(city.getId())));
-            List<BindUserBO> bindUserBOS = sysCustomerUserCommonService.queryBindUserListByCityId(cityIds);
-            if (CollUtil.isNotEmpty(bindUserBOS)) {
-                if (CollUtil.isEmpty(userIds)) {
-                    userIds.addAll(
-                            bindUserBOS.stream()
-                                    .map(BindUserBO::getId).toList());
-                }
-                if (CollUtil.isEmpty(groupIds)) {
-                    groupIds.addAll(
-                            bindUserBOS.stream()
-                                    .map(BindUserBO::getGroupId).distinct().toList());
-                }
-            }
-        }
+//        SysCustomerSelectBO selectBO = sysCustomerUserCommonService.querySelectDataByType1( true);
+//        if (selectBO != null && CollUtil.isNotEmpty(selectBO.getRegionData())) {
+//            List<Long> cityIds = Lists.newArrayList();
+//            selectBO.getRegionData()
+//                    .forEach(provinceRegion ->
+//                            provinceRegion.getSubData()
+//                                    .forEach(city -> cityIds.add(city.getId())));
+//            List<BindUserBO> bindUserBOS = sysCustomerUserCommonService.queryBindUserListByCityId(cityIds);
+//            if (CollUtil.isNotEmpty(bindUserBOS)) {
+//                if (CollUtil.isEmpty(userIds)) {
+//                    userIds.addAll(
+//                            bindUserBOS.stream()
+//                                    .map(BindUserBO::getId).toList());
+//                }
+//                if (CollUtil.isEmpty(groupIds)) {
+//                    groupIds.addAll(
+//                            bindUserBOS.stream()
+//                                    .map(BindUserBO::getGroupId).distinct().toList());
+//                }
+//            }
+//        }
         return sysCustomerOperLogService.adminList(dto, userIds, groupIds, true, false);
     }
 

+ 6 - 4
src/main/java/com/jtzx/crm/module/sys/service/SysCustomerDataPublicService.java

@@ -158,7 +158,7 @@ public class SysCustomerDataPublicService {
      */
     @Transactional(rollbackFor = Exception.class)
     public void adminInsertCustomerPerson(AdminSysCustomerPersonInsertOrUpdateDTO dto) throws BusinessException{
-//        permissionService.publicPrivateCustomerPermission();
+        permissionService.publicPrivateCustomerPermission();
         sysCustomerPersonService.adminInsert(dto);
     }
 
@@ -170,7 +170,6 @@ public class SysCustomerDataPublicService {
      */
     @Transactional(rollbackFor = Exception.class)
     public void adminUpdateCustomerPerson(AdminSysCustomerPersonInsertOrUpdateDTO dto) throws BusinessException{
-//        permissionService.publicPrivateCustomerPermission();
         sysCustomerPersonService.adminUpdate(dto);
     }
 
@@ -181,7 +180,6 @@ public class SysCustomerDataPublicService {
      */
     @Transactional(rollbackFor = Exception.class)
     public void adminDeleteCustomerPerson(Long id) throws BusinessException{
-//        permissionService.publicPrivateCustomerPermission();
         sysCustomerPersonService.adminDelete(id);
     }
 
@@ -233,10 +231,14 @@ public class SysCustomerDataPublicService {
      * 操作日志列表查询
      */
     public SysCustomerOperLogDataBO queryOperLogData(AdminSysCustomerOperLogListDTO dto) throws BusinessException{
-//        permissionService.publicPrivateCustomerPermission();
+        permissionService.publicPrivateCustomerPermission();
         List<Long> userIds = Lists.newArrayList();
         if (dto.getOperUserId() != null) {
             userIds.add(dto.getOperUserId());
+        }else {
+            if(!UserContextHolder.getUserContext().getRoles().contains("Admin")){
+                userIds.add(UserContextHolder.getUserId());
+            }
         }
         List<Long> groupIds = Lists.newArrayList();
         if (dto.getGroupId() != null) {

+ 21 - 17
src/main/java/com/jtzx/crm/module/sys/service/SysCustomerDataService.java

@@ -147,24 +147,28 @@ public class SysCustomerDataService {
                         .eq(SysUserGroupRelationEntity::getCityId, dto.getCityId())
                         .last("limit 1")
         );
-        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(entity.compareTo(dto))
-                .operDataType(0)
-                .build();
-        if (userGroupRelation != null) {
-            logEntity.setOperUserGroupId(userGroupRelation.getGroupId());
+        String changeData = entity.compareTo(dto);
+        if(StrUtil.isNotEmpty(changeData)){
+            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(changeData)
+                    .operDataType(0)
+                    .build();
+            if (userGroupRelation != null) {
+                logEntity.setOperUserGroupId(userGroupRelation.getGroupId());
+            }
+            sysCustomerOperLogMapper.insert(logEntity);
         }
-        sysCustomerOperLogMapper.insert(logEntity);
+
 
         BeanUtil.copyProperties(dto, entity);
         sysCustomerDataMapper.updateById(entity);

+ 1 - 1
src/main/java/com/jtzx/crm/module/sys/service/SysCustomerOperLogService.java

@@ -90,7 +90,7 @@ public class SysCustomerOperLogService {
                                         SysCustomerOperLogEntity::getOperName,
                                         SysCustomerOperLogEntity::getOperUserGroupId
                                 )
-                                .eq(dto.getCustomerId() != null, SysCustomerOperLogEntity::getCustomerId, dto.getCustomerId())
+                                .eq(dto.getCustomerId()!=null,SysCustomerOperLogEntity::getCustomerId,dto.getCustomerId())
                                 .orderByDesc(SysCustomerOperLogEntity::getCreatedAt)
                 );
         if (CollUtil.isNotEmpty(logEntityList)) {

+ 25 - 21
src/main/java/com/jtzx/crm/module/sys/service/SysCustomerPersonService.java

@@ -127,8 +127,8 @@ public class SysCustomerPersonService {
             throw new BusinessException(400, "无效客户(学校)数据ID");
         }
         SysCustomerPersonEntity sysCustomerPersonEntity = sysCustomerPersonMapper.selectById(dto.getId());
-        if (!UserContextHolder.getUserContext().getRoles().contains("Admin") ||
-                sysCustomerPersonEntity.getOperUserId().equals(UserContextHolder.getUserId())) {
+        if (!UserContextHolder.getUserContext().getRoles().contains("Admin") &&
+                !sysCustomerPersonEntity.getOperUserId().equals(UserContextHolder.getUserId())) {
             throw new BusinessException(400, "无权操作");
         }
         //查询用户的小组数据
@@ -144,24 +144,28 @@ public class SysCustomerPersonService {
                         .last("limit 1")
         );
 
-        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(sysCustomerPersonEntity.compareTo(dto))
-                .operDataType(1)
-                .build();
-        if (userGroupRelation != null) {
-            logEntity.setOperUserGroupId(userGroupRelation.getGroupId());
+        String string = sysCustomerPersonEntity.compareTo(dto);
+        if(StrUtil.isNotEmpty(string)){
+            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(string)
+                    .operDataType(1)
+                    .build();
+            if (userGroupRelation != null) {
+                logEntity.setOperUserGroupId(userGroupRelation.getGroupId());
+            }
+            sysCustomerOperLogMapper.insert(logEntity);
         }
-        sysCustomerOperLogMapper.insert(logEntity);
+
 
         BeanUtil.copyProperties(dto, sysCustomerPersonEntity);
         sysCustomerPersonMapper.updateById(sysCustomerPersonEntity);
@@ -177,8 +181,8 @@ public class SysCustomerPersonService {
         if (sysCustomerPersonEntity == null) {
             throw new BusinessException(400, "联系人不存在");
         }
-        if (!UserContextHolder.getUserContext().getRoles().contains("Admin") ||
-                sysCustomerPersonEntity.getOperUserId().equals(UserContextHolder.getUserId())) {
+        if (!UserContextHolder.getUserContext().getRoles().contains("Admin") &&
+                !sysCustomerPersonEntity.getOperUserId().equals(UserContextHolder.getUserId())) {
             throw new BusinessException(400, "无权操作");
         }