Browse Source

add:派单划定

caozixuan 1 năm trước cách đây
mục cha
commit
eb4a810efc

+ 32 - 32
sop-api/src/main/java/com/qmth/sop/server/api/TBCrmController.java

@@ -5,7 +5,8 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.qmth.boot.api.annotation.Aac;
 import com.qmth.boot.api.constant.ApiConstant;
 import com.qmth.sop.business.bean.params.TBCrmParam;
-import com.qmth.sop.business.bean.result.DingElementResult;
+import com.qmth.sop.business.bean.result.CrmBindResult;
+import com.qmth.sop.business.bean.result.CrmProjectResult;
 import com.qmth.sop.business.bean.result.ServiceRegionDetailResult;
 import com.qmth.sop.business.bean.result.TBCrmResult;
 import com.qmth.sop.business.entity.TBCrm;
@@ -42,18 +43,16 @@ import java.util.Objects;
 @RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_CRM)
 public class TBCrmController {
 
-
     @Resource
     TBCrmService tBCrmService;
+
     @Resource
     TBServiceRegionDetailService tbServiceRegionDetailService;
 
-
     @ApiOperation(value = "派单信息表查询接口")
     @RequestMapping(value = "/query", method = RequestMethod.POST)
-    @ApiResponses({@ApiResponse(code = 200, message = "派单信息表查询结果", response = TBCrmResult.class)})
-    public Result query(
-            @ApiParam(value = "服务单元") @RequestParam(required = false) Long serviceId,
+    @ApiResponses({ @ApiResponse(code = 200, message = "派单信息表查询结果", response = TBCrmResult.class) })
+    public Result query(@ApiParam(value = "服务单元") @RequestParam(required = false) Long serviceId,
             @ApiParam(value = "是否绑定") @RequestParam(required = false) Boolean isBind,
             @ApiParam(value = "大区经理") @RequestParam(required = false) Long leadId,
             @ApiParam(value = "派单人") @RequestParam(required = false) Long crmUserId,
@@ -64,36 +63,35 @@ public class TBCrmController {
             @ApiParam(value = "派单时间结束") @RequestParam(required = false) Long endTime,
             @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
             @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
-        IPage<TBCrmResult> resultIPage = tBCrmService.query(new Page<>(pageNumber, pageSize), serviceId, isBind, leadId, crmUserId, type, custom, crmNo, startTime, endTime);
+        IPage<TBCrmResult> resultIPage = tBCrmService.query(new Page<>(pageNumber, pageSize), serviceId, isBind, leadId,
+                crmUserId, type, custom, crmNo, startTime, endTime);
 
         return ResultUtil.ok(resultIPage);
     }
 
-
     @ApiOperation(value = "派单信息表新增修改接口")
     @RequestMapping(value = "/save", method = RequestMethod.POST)
-    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
+    @ApiResponses({ @ApiResponse(code = 200, message = "返回信息", response = Object.class) })
     @OperationLog(logType = LogTypeEnum.EDIT)
-    public Result save(@Valid @ApiParam(value = "派单信息表信息", required = true) @RequestBody TBCrmParam tbCrmParam, BindingResult bindingResult) throws InterruptedException {
+    public Result save(@Valid @ApiParam(value = "派单信息表信息", required = true) @RequestBody TBCrmParam tbCrmParam,
+            BindingResult bindingResult) throws InterruptedException {
         if (bindingResult.hasErrors()) {
             return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
         }
         return ResultUtil.ok(tBCrmService.saveTBCrm(tbCrmParam));
     }
 
-
     @ApiOperation(value = "派单信息表删除接口")
     @RequestMapping(value = "/delete", method = RequestMethod.POST)
-    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
+    @ApiResponses({ @ApiResponse(code = 200, message = "返回信息", response = Object.class) })
     @OperationLog(logType = LogTypeEnum.DELETE)
-    public Result delete(@ApiParam(value = "派单信息表id", required = true) @RequestParam(required = true) long id) {
+    public Result delete(@ApiParam(value = "派单信息表id", required = true) @RequestParam long id) {
         return ResultUtil.ok(tBCrmService.delete(id));
     }
 
-
     @ApiOperation(value = "派单信息表获取单个接口")
     @RequestMapping(value = "/get", method = RequestMethod.POST)
-    @ApiResponses({@ApiResponse(code = 200, message = "派单信息表信息", response = TBCrm.class)})
+    @ApiResponses({ @ApiResponse(code = 200, message = "派单信息表信息", response = TBCrm.class) })
     public Result get(@ApiParam(value = "派单信息表id", required = true) @RequestParam long id) {
         return ResultUtil.ok(tBCrmService.getById(id));
     }
@@ -101,40 +99,42 @@ public class TBCrmController {
     //批量禁用
     @ApiOperation(value = "批量禁用接口")
     @RequestMapping(value = "/batchDisable", method = RequestMethod.POST)
-    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
+    @ApiResponses({ @ApiResponse(code = 200, message = "返回信息", response = Object.class) })
     @OperationLog(logType = LogTypeEnum.UPDATE)
-    public Result batchDisable(@ApiParam(value = "派单信息表id", required = true) @RequestParam(required = true) long[] crmIds) {
+    public Result batchDisable(@ApiParam(value = "派单信息表id", required = true) @RequestParam long[] crmIds) {
         return ResultUtil.ok(tBCrmService.batchDisable(crmIds));
     }
 
     //统计待划定数量
-
     @ApiOperation(value = "统计待划定数量接口")
     @RequestMapping(value = "/countWaitZone", method = RequestMethod.POST)
-    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
-    public Result countWaitZone(@ApiParam(value = "服务单元") @RequestParam(required = false) Long serviceId,
-                                @ApiParam(value = "是否绑定") @RequestParam(required = false) Boolean isBind,
-                                @ApiParam(value = "大区经理") @RequestParam(required = false) Long leadId,
-                                @ApiParam(value = "派单人") @RequestParam(required = false) Long crmUserId,
-                                @ApiParam(value = "客户类型") @RequestParam(required = false) ProductTypeEnum type,
-                                @ApiParam(value = "客户名称(模糊查询)") @RequestParam(required = false) String custom,
-                                @ApiParam(value = "项目单号(模糊查询) ") @RequestParam(required = false) String crmNo,
-                                @ApiParam(value = "派单时间开始") @RequestParam(required = false) Long startTime,
-                                @ApiParam(value = "派单时间结束") @RequestParam(required = false) Long endTime) {
+    @ApiResponses({ @ApiResponse(code = 200, message = "返回信息", response = Object.class) })
+    public Result countWaitZone(@ApiParam(value = "服务单元") @RequestParam(required = false) Long serviceId, @ApiParam(value = "是否绑定") @RequestParam(required = false) Boolean isBind,
+            @ApiParam(value = "大区经理") @RequestParam(required = false) Long leadId, @ApiParam(value = "派单人") @RequestParam(required = false) Long crmUserId,
+            @ApiParam(value = "客户类型") @RequestParam(required = false) ProductTypeEnum type, @ApiParam(value = "客户名称(模糊查询)") @RequestParam(required = false) String custom,
+            @ApiParam(value = "项目单号(模糊查询) ") @RequestParam(required = false) String crmNo, @ApiParam(value = "派单时间开始") @RequestParam(required = false) Long startTime,
+            @ApiParam(value = "派单时间结束") @RequestParam(required = false) Long endTime) {
         return ResultUtil.ok(tBCrmService.count(serviceId, isBind, leadId, crmUserId, type, custom, crmNo, startTime, endTime));
     }
 
     @ApiOperation(value = "查询项目派单信息")
     @RequestMapping(value = "/crm_project_info", method = RequestMethod.POST)
-    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = DingElementResult.class)})
-    public Result findDingInfo(@ApiParam(value = "sop单号") @RequestParam(required = false) String sopNo,
-                               @ApiParam(value = "crm单号") @RequestParam(required = false) String crmNo) {
+    @ApiResponses({ @ApiResponse(code = 200, message = "查询成功", response = CrmProjectResult.class) })
+    public Result findDingInfo(@ApiParam(value = "sop单号") @RequestParam(required = false) String sopNo, @ApiParam(value = "crm单号") @RequestParam(required = false) String crmNo) {
         return ResultUtil.ok(tBCrmService.findCrmProjectInfo(sopNo, null, crmNo));
     }
 
+    @ApiOperation(value = "查询同服务单元客户绑定数据")
+    @RequestMapping(value = "/crm_bind_info", method = RequestMethod.POST)
+    @ApiResponses({ @ApiResponse(code = 200, message = "查询成功", response = CrmBindResult.class) })
+    public Result findCrmBindInfo(@ApiParam(value = "服务单元id", required = true) @RequestParam String serviceUnitId,
+            @ApiParam(value = "客户id", required = true) @RequestParam String customId) {
+        return ResultUtil.ok(tBCrmService.findCrmBindInfo(SystemConstant.convertIdToLong(serviceUnitId), SystemConstant.convertIdToLong(customId)));
+    }
+
     @ApiOperation(value = "派单数据刷新(测试用)")
     @RequestMapping(value = "/crm_update", method = RequestMethod.POST)
-    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
+    @ApiResponses({ @ApiResponse(code = 200, message = "查询成功", response = Result.class) })
     @Aac(auth = false)
     public void updateData() {
         List<TBCrm> tbCrmList = tBCrmService.list();

+ 4 - 4
sop-api/src/main/java/com/qmth/sop/server/api/TBServiceScopeController.java

@@ -63,15 +63,15 @@ public class TBServiceScopeController {
         return ResultUtil.ok(tbCrmService.findUnbindOrderPage(SystemConstant.convertIdToLong(crmUserId), productType, customName, crmNo, startTime, endTime, pageNumber, pageSize));
     }
 
-    @ApiOperation(value = "服务范围管理-批量划定派单和服务单元关系")
-    @RequestMapping(value = "/bind_batch", method = RequestMethod.POST)
+    @ApiOperation(value = "服务范围管理-划定派单和服务单元关系")
+    @RequestMapping(value = "/bind", method = RequestMethod.POST)
     @ApiResponses({@ApiResponse(code = 200, message = "操作成功", response = Result.class)})
     @OperationLog(logType = LogTypeEnum.UPDATE)
-    public Result bindCrmWithServiceBatch(@Valid @RequestBody ServiceScopeParam serviceScopeParam, BindingResult bindingResult) {
+    public Result bind(@Valid @RequestBody ServiceScopeParam serviceScopeParam, BindingResult bindingResult) {
         if (bindingResult.hasErrors()) {
             return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
         }
-        tbCrmService.bindCrmWithServiceBatch(serviceScopeParam);
+        tbCrmService.bindCrmWithService(serviceScopeParam);
         return ResultUtil.ok();
     }
 

+ 34 - 9
sop-business/src/main/java/com/qmth/sop/business/bean/params/ServiceScopeParam.java

@@ -2,12 +2,11 @@ package com.qmth.sop.business.bean.params;
 
 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
 import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
+import com.qmth.sop.common.enums.RegionManagerSelectedTypeEnum;
 import io.swagger.annotations.ApiModelProperty;
 import org.hibernate.validator.constraints.Range;
 
-import javax.validation.constraints.NotEmpty;
 import javax.validation.constraints.NotNull;
-import java.util.List;
 
 /**
  * @Description: 服务单元批量划定参数
@@ -15,16 +14,26 @@ import java.util.List;
  * @Date: 2023-08-11
  */
 public class ServiceScopeParam {
+
     @ApiModelProperty("服务单元id")
     @JsonSerialize(using = ToStringSerializer.class)
     @NotNull(message = "请提供划定服务单元")
     @Range(min = 1L, message = "请提供划定服务单元")
     private Long serviceUnitId;
 
-    @ApiModelProperty("派单id集合")
+    @ApiModelProperty("派单id")
+    @JsonSerialize(using = ToStringSerializer.class)
+    @NotNull(message = "请提供划定派单")
+    @Range(min = 1L, message = "请提供划定派单")
+    private Long crmId;
+
+    @ApiModelProperty("大区经理指定方式")
+    @NotNull(message = "请提供大区经理指定方式")
+    private RegionManagerSelectedTypeEnum leadType;
+
+    @ApiModelProperty("大区经理id")
     @JsonSerialize(using = ToStringSerializer.class)
-    @NotEmpty(message = "请提供划定派单集合")
-    private List<Long> crmIdList;
+    private Long leadId;
 
     public Long getServiceUnitId() {
         return serviceUnitId;
@@ -34,11 +43,27 @@ public class ServiceScopeParam {
         this.serviceUnitId = serviceUnitId;
     }
 
-    public List<Long> getCrmIdList() {
-        return crmIdList;
+    public Long getCrmId() {
+        return crmId;
+    }
+
+    public void setCrmId(Long crmId) {
+        this.crmId = crmId;
+    }
+
+    public RegionManagerSelectedTypeEnum getLeadType() {
+        return leadType;
+    }
+
+    public void setLeadType(RegionManagerSelectedTypeEnum leadType) {
+        this.leadType = leadType;
+    }
+
+    public Long getLeadId() {
+        return leadId;
     }
 
-    public void setCrmIdList(List<Long> crmIdList) {
-        this.crmIdList = crmIdList;
+    public void setLeadId(Long leadId) {
+        this.leadId = leadId;
     }
 }

+ 80 - 0
sop-business/src/main/java/com/qmth/sop/business/bean/result/CrmBindResult.java

@@ -0,0 +1,80 @@
+package com.qmth.sop.business.bean.result;
+
+import com.qmth.sop.common.enums.RegionManagerSelectedTypeEnum;
+import io.swagger.annotations.ApiModelProperty;
+
+import java.util.List;
+
+/**
+ * @Description: 派单绑定信息
+ * @Author: CaoZixuan
+ * @Date: 2024-05-17
+ */
+public class CrmBindResult {
+
+    @ApiModelProperty("大区经理指定方式")
+    private RegionManagerSelectedTypeEnum leadType;
+
+    @ApiModelProperty("大区经理")
+    private Long leadId;
+
+    @ApiModelProperty("大区经理名称")
+    private String leadName;
+
+    @ApiModelProperty("是否可以编辑")
+    private boolean canEdit;
+
+    @ApiModelProperty("同客户同服务单元的派单id集合")
+    private List<Long> similarCrmIdList;
+
+    @ApiModelProperty("客户名称")
+    private String customName;
+
+    public RegionManagerSelectedTypeEnum getLeadType() {
+        return leadType;
+    }
+
+    public void setLeadType(RegionManagerSelectedTypeEnum leadType) {
+        this.leadType = leadType;
+    }
+
+    public Long getLeadId() {
+        return leadId;
+    }
+
+    public void setLeadId(Long leadId) {
+        this.leadId = leadId;
+    }
+
+    public String getLeadName() {
+        return leadName;
+    }
+
+    public void setLeadName(String leadName) {
+        this.leadName = leadName;
+    }
+
+    public boolean isCanEdit() {
+        return canEdit;
+    }
+
+    public void setCanEdit(boolean canEdit) {
+        this.canEdit = canEdit;
+    }
+
+    public List<Long> getSimilarCrmIdList() {
+        return similarCrmIdList;
+    }
+
+    public void setSimilarCrmIdList(List<Long> similarCrmIdList) {
+        this.similarCrmIdList = similarCrmIdList;
+    }
+
+    public String getCustomName() {
+        return customName;
+    }
+
+    public void setCustomName(String customName) {
+        this.customName = customName;
+    }
+}

+ 11 - 5
sop-business/src/main/java/com/qmth/sop/business/service/TBCrmService.java

@@ -5,10 +5,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
 import com.qmth.sop.business.bean.dto.SopCrmInfo;
 import com.qmth.sop.business.bean.params.ServiceScopeParam;
 import com.qmth.sop.business.bean.params.TBCrmParam;
-import com.qmth.sop.business.bean.result.CrmProjectResult;
-import com.qmth.sop.business.bean.result.CrmServiceResult;
-import com.qmth.sop.business.bean.result.CrmSubTotalResult;
-import com.qmth.sop.business.bean.result.TBCrmResult;
+import com.qmth.sop.business.bean.result.*;
 import com.qmth.sop.business.entity.TBCrm;
 import com.qmth.sop.common.enums.CrmStatusEnum;
 import com.qmth.sop.common.enums.ProductTypeEnum;
@@ -68,7 +65,7 @@ public interface TBCrmService extends IService<TBCrm> {
      *
      * @param serviceScopeParam 服务范围划定参数
      */
-    void bindCrmWithServiceBatch(ServiceScopeParam serviceScopeParam);
+    void bindCrmWithService(ServiceScopeParam serviceScopeParam);
 
     /**
      * 服务范围管理 - 移除(解绑派单和服务单元)
@@ -193,4 +190,13 @@ public interface TBCrmService extends IService<TBCrm> {
      * 从纷享销客同步派单信息
      */
     Map<String, Object> syncCrmFromFxxk(Map<String, Object> map);
+
+    /**
+     * 根据服务单元id和客户id查询派单绑定信息
+     *
+     * @param serviceUnitId 服务单元id
+     * @param customId      客户id
+     * @return 派单绑定信息
+     */
+    CrmBindResult findCrmBindInfo(Long serviceUnitId, Long customId);
 }

+ 138 - 20
sop-business/src/main/java/com/qmth/sop/business/service/impl/TBCrmServiceImpl.java

@@ -123,10 +123,17 @@ public class TBCrmServiceImpl extends ServiceImpl<TBCrmMapper, TBCrm> implements
 
     @Transactional
     @Override
-    public void bindCrmWithServiceBatch(ServiceScopeParam serviceScopeParam) {
-        List<Long> crmIdList = serviceScopeParam.getCrmIdList();
+    public void bindCrmWithService(ServiceScopeParam serviceScopeParam) {
+        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
+        Long crmId = serviceScopeParam.getCrmId();
         Long serviceUnitId = serviceScopeParam.getServiceUnitId();
-        if (CollectionUtils.isEmpty(crmIdList)) {
+        RegionManagerSelectedTypeEnum leadType = serviceScopeParam.getLeadType();
+        Long leadId = serviceScopeParam.getLeadId();
+        if (RegionManagerSelectedTypeEnum.BY_PERSON.equals(leadType) && (leadId == null || leadId == 0)) {
+            throw ExceptionResultEnum.ERROR.exception("指定大区经理模式必须选择大区经理");
+        }
+
+        if (crmId == null || crmId == 0) {
             throw ExceptionResultEnum.ERROR.exception("未选择要划定的派单");
         }
 
@@ -139,21 +146,83 @@ public class TBCrmServiceImpl extends ServiceImpl<TBCrmMapper, TBCrm> implements
                 throw ExceptionResultEnum.ERROR.exception("不能划定进已完结或已作废的服务单元");
             }
         }
-        List<TBCrm> tbCrmList = this.listByIds(crmIdList);
-        // 派单调配资源变换
-        for (TBCrm dbCrm : tbCrmList) {
-            this.crmReBindServiceUnit(dbCrm.getCrmNo(), dbCrm.getServiceId(), serviceUnitId);
+        TBCrm tbCrm = this.getById(crmId);
+        if (Objects.isNull(tbCrm)) {
+            throw ExceptionResultEnum.ERROR.exception("未找到派单");
+        }
+        Long customId = tbCrm.getCustomId();
+        CrmBindResult crmBindResult = this.findCrmBindInfo(serviceUnitId, customId);
+        RegionManagerSelectedTypeEnum defaultLeadType = crmBindResult.getLeadType();
+        Long defaultLeadId = crmBindResult.getLeadId();
+        boolean canEdit = crmBindResult.isCanEdit();
+        String customName = crmBindResult.getCustomName();
+        List<Long> similarCrmIdList = crmBindResult.getSimilarCrmIdList();
+
+        if (canEdit) {
+            // 可以更改
+            switch (leadType) {
+            case BY_AREA:
+                // 按地区匹配
+                ServiceRegionDetailResult serviceRegionDetailResult = tbServiceRegionDetailService.findRegionDetail(
+                        serviceUnitId, tbCrm.getCrmNo());
+                UpdateWrapper<TBCrm> areaUpdateWrapper = new UpdateWrapper<>();
+                areaUpdateWrapper.lambda().set(TBCrm::getServiceId, serviceUnitId).set(TBCrm::getLeadType, leadType)
+                        .set(TBCrm::getUpdateId, requestUser.getId())
+                        .set(TBCrm::getUpdateTime, System.currentTimeMillis());
+                if (Objects.nonNull(serviceRegionDetailResult)) {
+                    areaUpdateWrapper.lambda().set(TBCrm::getLeadId, serviceRegionDetailResult.getLeadId())
+                            .set(TBCrm::getRegionId, serviceRegionDetailResult.getServiceRegionId())
+                            .set(TBCrm::getRegionDetailId, serviceRegionDetailResult.getServiceRegionDetailId());
+                }
+                if (leadType.equals(defaultLeadType)) {
+                    // 更新自己信息
+                    areaUpdateWrapper.lambda().eq(TBCrm::getId, crmId);
+                } else {
+                    // 更新同类派单信息
+                    areaUpdateWrapper.lambda().in(TBCrm::getId, similarCrmIdList);
+                }
+                this.update(areaUpdateWrapper);
+
+                break;
+            case BY_PERSON:
+                // 直接指定大区经理
+                UpdateWrapper<TBCrm> personUpdateWrapper = new UpdateWrapper<>();
+                personUpdateWrapper.lambda().set(TBCrm::getServiceId, serviceUnitId).set(TBCrm::getLeadType, leadType)
+                        .set(TBCrm::getLeadId, leadId).set(TBCrm::getUpdateId, requestUser.getId())
+                        .set(TBCrm::getUpdateTime, System.currentTimeMillis());
+                if (leadType.equals(defaultLeadType) && leadId.equals(defaultLeadId)) {
+                    // 更新自己信息
+                    personUpdateWrapper.lambda().eq(TBCrm::getId, crmId);
+                } else {
+                    // 更新同类派单信息
+                    personUpdateWrapper.lambda().in(TBCrm::getId, similarCrmIdList);
+                }
+                break;
+            }
+        } else {
+            // 不可更改
+            if (!Objects.equals(leadType, defaultLeadType) || !Objects.equals(leadId, defaultLeadId)) {
+                throw ExceptionResultEnum.ERROR.exception(String.format("客户[%s]在该服务单元下已经有发布的sop,无法更改绑定信息", customName));
+            }
+            tbCrm.setServiceId(serviceUnitId);
+            tbCrm.setLeadType(defaultLeadType);
+            tbCrm.setLeadId(defaultLeadId);
         }
     }
 
     @Transactional
     @Override
     public void unbindCrmWithService(Long crmId) {
+        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
         TBCrm tbCrm = this.getById(crmId);
         if (Objects.isNull(tbCrm)) {
             throw ExceptionResultEnum.ERROR.exception("未找到派单信息");
         }
+        if (CrmStatusEnum.PUBLISH.equals(tbCrm.getStatus())) {
+            throw ExceptionResultEnum.ERROR.exception(String.format("已发布的派单[%s]不能移出,请先撤销发布", tbCrm.getCrmNo()));
+        }
         Long serviceUnitId = tbCrm.getServiceId();
+
         TBService tbService = tbServiceService.getById(serviceUnitId);
         if (Objects.nonNull(tbService)) {
             ServiceStatusEnum status = tbService.getStatus();
@@ -161,7 +230,10 @@ public class TBCrmServiceImpl extends ServiceImpl<TBCrmMapper, TBCrm> implements
                 throw ExceptionResultEnum.ERROR.exception("服务单元的当前状态更新为" + ServiceStatusEnum.FINISH.name() + "不允许移除");
             }
         }
-        this.crmReBindServiceUnit(tbCrm.getCrmNo(), serviceUnitId, null);
+        UpdateWrapper<TBCrm> updateWrapper = new UpdateWrapper<>();
+        updateWrapper.lambda().eq(TBCrm::getId, crmId).set(TBCrm::getServiceId, null).set(TBCrm::getLeadType, null)
+                .set(TBCrm::getLeadId, null).set(TBCrm::getRegionId, null).set(TBCrm::getRegionDetailId, null).set(TBCrm::getUpdateId, requestUser.getId()).set(TBCrm::getUpdateTime, System.currentTimeMillis());
+        this.update(updateWrapper);
     }
 
     @Override
@@ -388,7 +460,7 @@ public class TBCrmServiceImpl extends ServiceImpl<TBCrmMapper, TBCrm> implements
     }
 
     @Override
-    public CrmProjectResult findCrmProjectInfo(String sopNo, Long crmDetailId,String crmNo) {
+    public CrmProjectResult findCrmProjectInfo(String sopNo, Long crmDetailId, String crmNo) {
         CrmProjectResult result = new CrmProjectResult();
         TBCrmDetail tbCrmDetail = null;
         if (crmDetailId != null && crmDetailId > 0) {
@@ -482,6 +554,7 @@ public class TBCrmServiceImpl extends ServiceImpl<TBCrmMapper, TBCrm> implements
         return this.baseMapper.findByTBCrmListByServiceUnitIdAndAddress(serviceUnitId, province, city);
     }
 
+    @Deprecated
     @Transactional
     @Override
     public void crmReBindServiceUnit(String crmNo, Long oldServiceUnitId, Long newServiceUnitId) {
@@ -731,21 +804,66 @@ public class TBCrmServiceImpl extends ServiceImpl<TBCrmMapper, TBCrm> implements
         }
     }
 
+    @Override
+    public CrmBindResult findCrmBindInfo(Long serviceUnitId, Long customId) {
+        RegionManagerSelectedTypeEnum leadType = null;
+        Long leadId = null;
+        boolean canEdit = true;
+        SysCustom sysCustom = sysCustomService.getById(customId);
+        if (Objects.isNull(sysCustom)) {
+            throw ExceptionResultEnum.ERROR.exception("未找到客户");
+        }
+
+        List<TBCrm> sameCrmList = this.list(new QueryWrapper<TBCrm>().lambda().eq(TBCrm::getCustomId, customId)
+                .eq(TBCrm::getServiceId, serviceUnitId));
+        if (CollectionUtils.isNotEmpty(sameCrmList)) {
+            // 对于同一服务单元同客户下有发布sop的,不允许更换大区经理,上述两图中内容置灰,不可更换大区经理。一旦更换大区经理,该客户的所有派单均更换大区经理
+            List<RegionManagerSelectedTypeEnum> leadTypeList = sameCrmList.stream().map(TBCrm::getLeadType).collect(Collectors.toList());
+            if (leadTypeList.size() != 1) {
+                throw ExceptionResultEnum.ERROR.exception("客户在服务单元下存在不同的大区经理指定方式");
+            }
+            leadType = leadTypeList.get(0);
+            List<Long> leadIdList = sameCrmList.stream().map(TBCrm::getLeadId).collect(Collectors.toList());
+            if (leadIdList.size() != 1) {
+                throw ExceptionResultEnum.ERROR.exception("客户在服务单元下存在不同的大区经理");
+            }
+            leadId = leadIdList.get(0);
+            if (sameCrmList.stream().anyMatch(e -> CrmStatusEnum.PUBLISH.equals(e.getStatus()))) {
+                canEdit = false;
+            }
+        }
+        CrmBindResult crmBindResult = new CrmBindResult();
+        crmBindResult.setSimilarCrmIdList(sameCrmList.stream().map(TBCrm::getId).distinct().collect(Collectors.toList()));
+        crmBindResult.setCanEdit(canEdit);
+        if (leadId != null) {
+            SysUser leader = sysUserService.getById(leadId);
+            if (Objects.isNull(leader)) {
+                throw ExceptionResultEnum.ERROR.exception("未找到大区经理用户");
+            }
+            crmBindResult.setLeadId(leadId);
+            crmBindResult.setLeadName(leader.getRealName());
+        }
+        crmBindResult.setLeadType(leadType);
+        crmBindResult.setCustomName(sysCustom.getName());
+        return crmBindResult;
+    }
+
     /**
      * 根据sop用户身份类型构建用户档案信息
-     * @param datasource 数据源
+     *
+     * @param datasource      数据源
      * @param sopRoleTypeEnum 身份
      * @return 用户档案信息
      */
-    private List<UserArchivesDto> buildUserArchivesDtoList(List<TBUserArchivesAllocation> datasource, SopRoleTypeEnum sopRoleTypeEnum){
-        return datasource.stream()
-                .filter(e -> sopRoleTypeEnum.equals(e.getSopRoleType())).flatMap(e -> {
-                    UserArchivesDto dto = new UserArchivesDto();
-                    Long userArchivesId = e.getArchivesId();
-                    dto.setUserArchivesId(userArchivesId);
-                    dto.setUserId(e.getUserId());
-                    dto.setName(tbUserArchivesService.getById(userArchivesId).getName());
-                    return Stream.of(dto);
-                }).collect(Collectors.toList());
+    private List<UserArchivesDto> buildUserArchivesDtoList(List<TBUserArchivesAllocation> datasource,
+            SopRoleTypeEnum sopRoleTypeEnum) {
+        return datasource.stream().filter(e -> sopRoleTypeEnum.equals(e.getSopRoleType())).flatMap(e -> {
+            UserArchivesDto dto = new UserArchivesDto();
+            Long userArchivesId = e.getArchivesId();
+            dto.setUserArchivesId(userArchivesId);
+            dto.setUserId(e.getUserId());
+            dto.setName(tbUserArchivesService.getById(userArchivesId).getName());
+            return Stream.of(dto);
+        }).collect(Collectors.toList());
     }
 }

+ 4 - 1
sop-business/src/main/resources/db/log/caozixuan_update_log.sql

@@ -48,4 +48,7 @@ update sys_ding_obj set sop_role_type = 'ENGINEER' where sop_role_type = '';
 INSERT INTO sequence (name, current_value, increment) VALUES ('sysCustomCode', '1', '1');
 
 -- 2024-05-16
-INSERT INTO sys_config (id, config_key, config_name, config_value, enable, sort, create_id) VALUES ('39', 'sms.custom.info.missing.remind.code', '客户信息缺失通知', 'SMS_467015006', '1', '1', '1');
+INSERT INTO sys_config (id, config_key, config_name, config_value, enable, sort, create_id) VALUES ('39', 'sms.custom.info.missing.remind.code', '客户信息缺失通知', 'SMS_467015006', '1', '1', '1');
+
+-- 2024-05-17
+UPDATE sys_privilege SET name = '服务范围管理-划定派单和服务单元关系', url = '/api/service/service/scope/bind' WHERE (id = '2009');