From 79c416ea8a72aa9469deb14272135cca8dc01aac Mon Sep 17 00:00:00 2001
From: liu <2021943741@qq.com>
Date: 星期三, 16 九月 2026 16:48:01 +0800
Subject: [PATCH] feat(crm): 退费单/销户申请审核通过后禁止修改和删除

---
 yudao-module-crm/src/main/java/cn/iocoder/yudao/module/crm/service/cancellation/CrmCancellationServiceImpl.java |  210 ++++++++++++++++++----------------------------------
 1 files changed, 72 insertions(+), 138 deletions(-)

diff --git a/yudao-module-crm/src/main/java/cn/iocoder/yudao/module/crm/service/cancellation/CrmCancellationServiceImpl.java b/yudao-module-crm/src/main/java/cn/iocoder/yudao/module/crm/service/cancellation/CrmCancellationServiceImpl.java
index 5ab0831..0a3e14c 100644
--- a/yudao-module-crm/src/main/java/cn/iocoder/yudao/module/crm/service/cancellation/CrmCancellationServiceImpl.java
+++ b/yudao-module-crm/src/main/java/cn/iocoder/yudao/module/crm/service/cancellation/CrmCancellationServiceImpl.java
@@ -1,12 +1,10 @@
 package cn.iocoder.yudao.module.crm.service.cancellation;
 
-import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.util.ObjUtil;
 import cn.hutool.core.util.RandomUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.iocoder.yudao.framework.common.pojo.PageResult;
 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
-import cn.iocoder.yudao.module.crm.controller.admin.cancellation.vo.CrmCancellationAuditReqVO;
 import cn.iocoder.yudao.module.crm.controller.admin.cancellation.vo.CrmCancellationPageReqVO;
 import cn.iocoder.yudao.module.crm.controller.admin.cancellation.vo.CrmCancellationRespVO;
 import cn.iocoder.yudao.module.crm.controller.admin.cancellation.vo.CrmCancellationSaveReqVO;
@@ -14,55 +12,45 @@
 import cn.iocoder.yudao.module.crm.dal.dataobject.customer.CrmCustomerDO;
 import cn.iocoder.yudao.module.crm.dal.mysql.cancellation.CrmCancellationMapper;
 import cn.iocoder.yudao.module.crm.dal.mysql.customer.CrmCustomerMapper;
-import cn.iocoder.yudao.module.bpm.api.task.BpmProcessInstanceApi;
-import cn.iocoder.yudao.module.bpm.api.task.dto.BpmProcessInstanceCreateReqDTO;
-import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmProcessDefinitionInfoDO;
-import cn.iocoder.yudao.module.bpm.service.definition.BpmCategoryService;
-import cn.iocoder.yudao.module.bpm.service.definition.BpmProcessDefinitionService;
 import cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants;
 import cn.iocoder.yudao.module.crm.enums.common.CrmAuditStatusEnum;
+import cn.iocoder.yudao.module.system.api.approval.ApprovalConfigApi;
+import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
+import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
 import jakarta.annotation.Resource;
 import lombok.extern.slf4j.Slf4j;
-import org.flowable.engine.repository.ProcessDefinition;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.validation.annotation.Validated;
 
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 import java.util.Set;
 
 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
-import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
-import static cn.iocoder.yudao.module.crm.util.CrmAuditStatusUtils.convertBpmResultToAuditStatus;
+import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
 
 @Service
 @Validated
 @Slf4j
 public class CrmCancellationServiceImpl implements CrmCancellationService {
 
-    private static final Integer AUDIT_DRAFT = 0;
-
     /**
-     * BPM 閿�鎴峰鎵瑰垎绫荤紪鐮�
+     * CRM 閿�鎴风敵璇峰鎵圭殑涓氬姟绫诲瀷缂栫爜
+     *
+     * 涓� system_approval_config.biz_type 瀵瑰簲锛屽鎵逛汉鍦ㄣ�岀郴缁熺鐞� - 瀹℃壒閰嶇疆銆嶄腑缁存姢銆�
      */
-    private static final String CANCELLATION_APPROVE_CATEGORY_CODE = "cancellation_approve";
+    private static final String CANCELLATION_APPROVE_BIZ_TYPE = "cancellation_approve";
 
     @Resource
     private CrmCancellationMapper cancellationMapper;
     @Resource
     private CrmCustomerMapper customerMapper;
     @Resource
-    private BpmProcessInstanceApi bpmProcessInstanceApi;
+    private AdminUserApi adminUserApi;
     @Resource
-    private BpmCategoryService bpmCategoryService;
-    @Resource
-    private BpmProcessDefinitionService bpmProcessDefinitionService;
+    private ApprovalConfigApi approvalConfigApi;
 
     @Override
     @Transactional(rollbackFor = Exception.class)
@@ -82,9 +70,7 @@
         if (createReqVO.getApplyTime() == null) {
             createReqVO.setApplyTime(LocalDateTime.now());
         }
-        if (createReqVO.getAuditStatus() == null) {
-            createReqVO.setAuditStatus(AUDIT_DRAFT);
-        }
+        createReqVO.setAuditStatus(CrmAuditStatusEnum.DRAFT.getStatus()); // 榛樿涓烘湭鎻愪氦鐘舵��
         CrmCancellationDO entity = BeanUtils.toBean(createReqVO, CrmCancellationDO.class);
         cancellationMapper.insert(entity);
         return entity.getId();
@@ -92,19 +78,23 @@
 
     @Override
     public void updateCancellation(CrmCancellationSaveReqVO updateReqVO) {
-        CrmCancellationDO exists = cancellationMapper.selectById(updateReqVO.getId());
-        if (exists == null) {
-            throw exception(ErrorCodeConstants.CRM_CANCELLATION_NOT_EXISTS);
+        // 1. 鏍¢獙瀛樺湪 + 瀹℃牳閫氳繃鐨勯攢鎴风敵璇蜂笉鍏佽淇敼
+        CrmCancellationDO cancellation = validateCancellationExists(updateReqVO.getId());
+        if (ObjUtil.equal(cancellation.getAuditStatus(), CrmAuditStatusEnum.APPROVE.getStatus())) {
+            throw exception(ErrorCodeConstants.CRM_CANCELLATION_UPDATE_FAIL_APPROVE);
         }
+        // 2. 鏇存柊閿�鎴风敵璇�
         CrmCancellationDO entity = BeanUtils.toBean(updateReqVO, CrmCancellationDO.class);
+        entity.setAuditStatus(null); // 瀹℃壒鐘舵�佸彧鑳界敱鎻愪氦/瀹℃牳鍙樻洿
         cancellationMapper.updateById(entity);
     }
 
     @Override
     public void deleteCancellation(Long id) {
-        CrmCancellationDO exists = cancellationMapper.selectById(id);
-        if (exists == null) {
-            throw exception(ErrorCodeConstants.CRM_CANCELLATION_NOT_EXISTS);
+        // 鏍¢獙瀛樺湪 + 瀹℃牳閫氳繃鐨勯攢鎴风敵璇蜂笉鍏佽鍒犻櫎
+        CrmCancellationDO cancellation = validateCancellationExists(id);
+        if (ObjUtil.equal(cancellation.getAuditStatus(), CrmAuditStatusEnum.APPROVE.getStatus())) {
+            throw exception(ErrorCodeConstants.CRM_CANCELLATION_DELETE_FAIL_APPROVE);
         }
         cancellationMapper.deleteById(id);
     }
@@ -126,129 +116,73 @@
     }
 
     @Override
-    public void auditCancellation(CrmCancellationAuditReqVO auditReqVO) {
-        CrmCancellationDO exists = cancellationMapper.selectById(auditReqVO.getId());
-        if (exists == null) {
-            throw exception(ErrorCodeConstants.CRM_CANCELLATION_NOT_EXISTS);
+    @Transactional(rollbackFor = Exception.class)
+    public void submitCancellation(Long id) {
+        // 1. 鏍¢獙瀛樺湪 + 鍙彁浜ょ姸鎬侊紙鏈彁浜� / 瀹℃牳涓嶉�氳繃锛�
+        CrmCancellationDO cancellation = validateCancellationExists(id);
+        if (ObjUtil.notEqual(cancellation.getAuditStatus(), CrmAuditStatusEnum.DRAFT.getStatus())
+                && ObjUtil.notEqual(cancellation.getAuditStatus(), CrmAuditStatusEnum.REJECT.getStatus())) {
+            throw exception(ErrorCodeConstants.CRM_CANCELLATION_SUBMIT_FAIL_NOT_EDITABLE);
         }
-        CrmCancellationDO update = new CrmCancellationDO();
-        update.setId(exists.getId());
-        update.setAuditStatus(auditReqVO.getAuditStatus());
-        if (StrUtil.isNotBlank(auditReqVO.getRemark())) {
-            update.setRemark(auditReqVO.getRemark());
-        }
-        cancellationMapper.updateById(update);
+
+        // 2. 鏍¢獙瀹℃壒閰嶇疆宸插惎鐢ㄤ笖閰嶇疆浜嗘湁鏁堝鎵逛汉锛堟湭閰嶇疆鏃舵姏鍑哄甫鏄庣‘鎻愮ず鐨勪笟鍔″紓甯革級
+        approvalConfigApi.validateApprovalEnabledAndGetApprovers(CANCELLATION_APPROVE_BIZ_TYPE);
+
+        // 3. 鐘舵�佺疆涓哄鎵逛腑锛屽苟娓呯┖涓婁竴杞鏍哥粨鏋滐紝閬垮厤娈嬬暀銆屽鏍镐笉閫氳繃銆嶇殑鍘熷洜
+        cancellationMapper.submitAndResetAuditInfo(id, CrmAuditStatusEnum.PROCESS.getStatus());
     }
 
     @Override
-    public void submitCancellation(Long id, String processDefinitionKey, Long userId) {
-        // 1. 鏍¢獙閿�鎴风敵璇锋槸鍚﹀湪鑽夌鐘舵��
-        CrmCancellationDO cancellation = cancellationMapper.selectById(id);
-        if (cancellation == null) {
-            throw exception(ErrorCodeConstants.CRM_CANCELLATION_NOT_EXISTS);
-        }
-        if (ObjUtil.notEqual(cancellation.getAuditStatus(), CrmAuditStatusEnum.DRAFT.getStatus())) {
-            throw exception(ErrorCodeConstants.CRM_CANCELLATION_SUBMIT_FAIL_NOT_DRAFT);
-        }
-
-        // 2. 鏍¢獙閿�鎴峰鎵瑰垎绫诲拰娴佺▼瀹氫箟鏄惁瀛樺湪锛屼笖浼犲叆鐨勬祦绋嬪畾涔夊浜庢縺娲荤姸鎬�
-        validateCancellationApproveCategoryAndProcessDefinition();
-        ProcessDefinition processDefinition = bpmProcessDefinitionService.getActiveProcessDefinition(processDefinitionKey);
-        if (processDefinition == null) {
-            throw exception(ErrorCodeConstants.CRM_CANCELLATION_BPM_PROCESS_DEFINITION_NOT_EXISTS);
-        }
-
-        // 3. 鍒涘缓閿�鎴峰鎵规祦绋嬪疄渚�
-        String processInstanceId = bpmProcessInstanceApi.createProcessInstance(userId,
-                new BpmProcessInstanceCreateReqDTO().setProcessDefinitionKey(processDefinitionKey)
-                        .setBusinessKey(String.valueOf(id)));
-
-        // 4. 鏇存柊閿�鎴风敵璇锋祦绋嬬紪鍙蜂笌瀹℃壒鐘舵��
-        cancellationMapper.updateById(new CrmCancellationDO().setId(id).setProcessInstanceId(processInstanceId)
-                .setAuditStatus(CrmAuditStatusEnum.PROCESS.getStatus()));
+    public Set<Long> getCancellationApproverUserIds() {
+        return approvalConfigApi.getApproverUserIds(CANCELLATION_APPROVE_BIZ_TYPE);
     }
 
     @Override
-    public void updateCancellationAuditStatus(Long id, Integer bpmResult) {
-        // 1.1 鏍¢獙閿�鎴风敵璇锋槸鍚﹀瓨鍦�
-        CrmCancellationDO cancellation = cancellationMapper.selectById(id);
-        if (cancellation == null) {
-            throw exception(ErrorCodeConstants.CRM_CANCELLATION_NOT_EXISTS);
-        }
-        // 1.2 鍙湁瀹℃壒涓紝鍙互鏇存柊瀹℃壒缁撴灉
+    @Transactional(rollbackFor = Exception.class)
+    public void auditCancellation(Long id, Boolean pass, String reviewRemark) {
+        // 1. 鏍¢獙瀛樺湪
+        CrmCancellationDO cancellation = validateCancellationExists(id);
+        // 2. 鏍¢獙澶勪簬瀹℃壒涓姸鎬�
         if (ObjUtil.notEqual(cancellation.getAuditStatus(), CrmAuditStatusEnum.PROCESS.getStatus())) {
-            log.error("[updateCancellationAuditStatus][cancellation({}) 涓嶅浜庡鎵逛腑锛屾棤娉曟洿鏂板鎵圭粨鏋�({})]",
-                    cancellation.getId(), bpmResult);
-            throw exception(ErrorCodeConstants.CRM_CANCELLATION_UPDATE_AUDIT_STATUS_FAIL_NOT_PROCESS);
+            throw exception(ErrorCodeConstants.CRM_CANCELLATION_AUDIT_FAIL_NOT_PROCESS);
         }
-
-        // 2. 鏇存柊閿�鎴风敵璇峰鎵圭粨鏋�
-        Integer auditStatus = convertBpmResultToAuditStatus(bpmResult);
-        cancellationMapper.updateById(new CrmCancellationDO().setId(id).setAuditStatus(auditStatus));
+        // 3. 鏍¢獙褰撳墠鐧诲綍鐢ㄦ埛鏄涓氬姟绫诲瀷鐨勫鎵逛汉锛堟垨绛撅細浠讳竴浜哄潎鍙鏍革級
+        Long userId = getLoginUserId();
+        approvalConfigApi.validateApprover(CANCELLATION_APPROVE_BIZ_TYPE, userId);
+        // 4. 瀹℃牳涓嶉�氳繃锛氬繀椤诲~鍐欏師鍥狅紝鐘舵�佺疆涓哄鏍镐笉閫氳繃锛岄��鍥炴彁浜や汉淇敼鍚庡彲閲嶆柊鎻愪氦
+        if (!Boolean.TRUE.equals(pass)) {
+            if (StrUtil.isBlank(reviewRemark)) {
+                throw exception(ErrorCodeConstants.CRM_CANCELLATION_AUDIT_REJECT_REASON_REQUIRED);
+            }
+            cancellationMapper.auditCancellation(id, CrmAuditStatusEnum.REJECT.getStatus(),
+                    userId, getUserNickname(userId), reviewRemark);
+            return;
+        }
+        // 5. 瀹℃牳閫氳繃锛氳褰曞鏍镐汉涓庡鏍告剰瑙侊紝鐘舵�佺疆涓哄鏍搁�氳繃
+        cancellationMapper.auditCancellation(id, CrmAuditStatusEnum.APPROVE.getStatus(),
+                userId, getUserNickname(userId), reviewRemark);
     }
 
-    @Override
-    public List<Map<String, Object>> getCancellationApproveProcessDefinitionList() {
-        // 1. 鏍¢獙閿�鎴峰鎵瑰垎绫诲拰娴佺▼瀹氫箟鏄惁瀛樺湪
-        validateCancellationApproveCategoryAndProcessDefinition();
-
-        // 2. 鑾峰彇鍒嗙被涓嬬殑娴佺▼瀹氫箟淇℃伅
-        List<BpmProcessDefinitionInfoDO> definitionInfoList = bpmProcessDefinitionService
-                .getProcessDefinitionInfoListByCategory(CANCELLATION_APPROVE_CATEGORY_CODE);
-
-        // 3. 鑾峰彇娴佺▼瀹氫箟璇︽儏
-        Set<String> processDefinitionIds = convertSet(definitionInfoList, BpmProcessDefinitionInfoDO::getProcessDefinitionId);
-        List<ProcessDefinition> processDefinitions = bpmProcessDefinitionService.getProcessDefinitionList(processDefinitionIds);
-
-        // 4. 杩囨护锛氬彧淇濈暀婵�娲荤姸鎬侊紝涓旂浉鍚� key 鍙繚鐣欐渶鏂扮増鏈�
-        Map<String, ProcessDefinition> latestVersionMap = new HashMap<>();
-        for (ProcessDefinition pd : processDefinitions) {
-            if (pd.isSuspended()) {
-                continue;
-            }
-            ProcessDefinition existing = latestVersionMap.get(pd.getKey());
-            if (existing == null || pd.getVersion() > existing.getVersion()) {
-                latestVersionMap.put(pd.getKey(), pd);
-            }
+    private CrmCancellationDO validateCancellationExists(Long id) {
+        CrmCancellationDO cancellation = cancellationMapper.selectById(id);
+        if (cancellation == null) {
+            throw exception(ErrorCodeConstants.CRM_CANCELLATION_NOT_EXISTS);
         }
-
-        // 5. 杩斿洖娴佺▼瀹氫箟鍒楄〃
-        List<Map<String, Object>> result = new ArrayList<>();
-        for (ProcessDefinition pd : latestVersionMap.values()) {
-            Map<String, Object> item = new HashMap<>();
-            item.put("id", pd.getId());
-            item.put("key", pd.getKey());
-            item.put("name", pd.getName());
-            item.put("version", pd.getVersion());
-            result.add(item);
-        }
-        return result;
+        return cancellation;
     }
 
     /**
-     * 鏍¢獙 BPM 閿�鎴峰鎵瑰垎绫诲拰娴佺▼瀹氫箟鏄惁瀛樺湪
+     * 鑾峰彇鐢ㄦ埛鏄电О
+     *
+     * @param userId 鐢ㄦ埛缂栧彿
+     * @return 鏄电О锛岀敤鎴蜂笉瀛樺湪鏃惰繑鍥� null
      */
-    private void validateCancellationApproveCategoryAndProcessDefinition() {
-        // 1. 鏍¢獙鍒嗙被鏄惁瀛樺湪
-        Map<String, ?> categoryMap = bpmCategoryService.getCategoryMap(Collections.singletonList(CANCELLATION_APPROVE_CATEGORY_CODE));
-        if (!categoryMap.containsKey(CANCELLATION_APPROVE_CATEGORY_CODE)) {
-            throw exception(ErrorCodeConstants.CRM_CANCELLATION_BPM_PROCESS_DEFINITION_NOT_EXISTS);
+    private String getUserNickname(Long userId) {
+        if (userId == null) {
+            return null;
         }
-
-        // 2. 鏍¢獙鍒嗙被涓嬫槸鍚︽湁鍙敤鐨勬祦绋嬪畾涔�
-        List<BpmProcessDefinitionInfoDO> definitionInfoList = bpmProcessDefinitionService
-                .getProcessDefinitionInfoListByCategory(CANCELLATION_APPROVE_CATEGORY_CODE);
-        if (CollUtil.isEmpty(definitionInfoList)) {
-            throw exception(ErrorCodeConstants.CRM_CANCELLATION_BPM_PROCESS_DEFINITION_NOT_EXISTS);
-        }
-
-        // 3. 鏍¢獙鏄惁鏈夋縺娲荤姸鎬佺殑娴佺▼瀹氫箟
-        Set<String> processDefinitionIds = convertSet(definitionInfoList, BpmProcessDefinitionInfoDO::getProcessDefinitionId);
-        List<ProcessDefinition> processDefinitions = bpmProcessDefinitionService.getProcessDefinitionList(processDefinitionIds);
-        boolean hasActiveDefinition = processDefinitions.stream().anyMatch(pd -> !pd.isSuspended());
-        if (!hasActiveDefinition) {
-            throw exception(ErrorCodeConstants.CRM_CANCELLATION_BPM_PROCESS_DEFINITION_NOT_EXISTS);
-        }
+        AdminUserRespDTO user = adminUserApi.getUser(userId);
+        return user == null ? null : user.getNickname();
     }
 
     private String generateCancellationNo() {
@@ -256,4 +190,4 @@
                 + RandomUtil.randomNumbers(4);
     }
 
-}
\ No newline at end of file
+}

--
Gitblit v1.9.3