| | |
| | | package cn.iocoder.yudao.module.crm.service.quotation; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ObjUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| | | import cn.iocoder.yudao.framework.common.util.number.MoneyUtils; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | 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.enums.task.BpmProcessInstanceStatusEnum; |
| | | import cn.iocoder.yudao.module.bpm.service.definition.BpmCategoryService; |
| | | import cn.iocoder.yudao.module.bpm.service.definition.BpmProcessDefinitionService; |
| | | import cn.iocoder.yudao.module.crm.controller.admin.contract.vo.contract.CrmContractSaveReqVO; |
| | | import cn.iocoder.yudao.module.crm.controller.admin.quotation.vo.CrmSaleQuotationPageReqVO; |
| | | import cn.iocoder.yudao.module.crm.controller.admin.quotation.vo.CrmSaleQuotationSaveReqVO; |
| | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | 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; |
| | |
| | | @Validated |
| | | @Slf4j |
| | | public class CrmSaleQuotationServiceImpl implements CrmSaleQuotationService { |
| | | |
| | | /** |
| | | * BPM 销售报价审批分类编码 |
| | | */ |
| | | private static final String SALE_QUOTATION_APPROVE_CATEGORY_CODE = "sale_quotation_approve"; |
| | | |
| | | @Resource |
| | | private CrmSaleQuotationMapper saleQuotationMapper; |
| | |
| | | private StorageAttachmentApi storageAttachmentApi; |
| | | @Resource |
| | | private CrmContractService contractService; |
| | | |
| | | @Resource |
| | | private BpmProcessInstanceApi bpmProcessInstanceApi; |
| | | @Resource |
| | | private BpmCategoryService bpmCategoryService; |
| | | @Resource |
| | | private BpmProcessDefinitionService bpmProcessDefinitionService; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | |
| | | // 2.3 删除数据权限 |
| | | permissionService.deletePermission(CrmBizTypeEnum.CRM_SALE_QUOTATION.getType(), saleQuotation.getId()); |
| | | }); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void submitSaleQuotation(Long id, String processDefinitionKey, Long userId) { |
| | | // 1. 校验报价单存在且为草稿状态 |
| | | CrmSaleQuotationDO saleQuotation = validateSaleQuotationExists(id); |
| | | if (ObjUtil.notEqual(saleQuotation.getStatus(), CrmSaleQuotationStatusEnum.DRAFT.getStatus())) { |
| | | throw exception(SALE_QUOTATION_SUBMIT_FAIL_NOT_DRAFT, saleQuotation.getNo()); |
| | | } |
| | | |
| | | // 2. 全局免审批:直接置为审批通过,无需流程模型 |
| | | if (bpmProcessInstanceApi.isGlobalAutoApprove()) { |
| | | saleQuotationMapper.updateById(new CrmSaleQuotationDO().setId(id).setProcessInstanceId("") |
| | | .setStatus(CrmSaleQuotationStatusEnum.PROCESS.getStatus())); |
| | | updateSaleQuotationAuditStatus(id, BpmProcessInstanceStatusEnum.APPROVE.getStatus()); |
| | | return; |
| | | } |
| | | |
| | | // 3. 校验审批分类和流程定义 |
| | | validateSaleQuotationApproveCategoryAndProcessDefinition(); |
| | | ProcessDefinition processDefinition = bpmProcessDefinitionService.getActiveProcessDefinition(processDefinitionKey); |
| | | if (processDefinition == null) { |
| | | throw exception(SALE_QUOTATION_BPM_PROCESS_DEFINITION_NOT_EXISTS); |
| | | } |
| | | |
| | | // 4. 创建审批流程实例 |
| | | String processInstanceId = bpmProcessInstanceApi.createProcessInstance(userId, new BpmProcessInstanceCreateReqDTO() |
| | | .setProcessDefinitionKey(processDefinitionKey).setBusinessKey(String.valueOf(id))); |
| | | |
| | | // 5. 更新流程实例编号和状态 |
| | | saleQuotationMapper.updateById(new CrmSaleQuotationDO().setId(id).setProcessInstanceId(processInstanceId) |
| | | .setStatus(CrmSaleQuotationStatusEnum.PROCESS.getStatus())); |
| | | } |
| | | |
| | | @Override |
| | | public void updateSaleQuotationAuditStatus(Long id, Integer bpmResult) { |
| | | // 1.1 校验报价单是否存在 |
| | | CrmSaleQuotationDO saleQuotation = validateSaleQuotationExists(id); |
| | | // 1.2 只有审批中,才可以更新审批结果 |
| | | if (ObjUtil.notEqual(saleQuotation.getStatus(), CrmSaleQuotationStatusEnum.PROCESS.getStatus())) { |
| | | log.error("[updateSaleQuotationAuditStatus][报价单({}) 不处于审批中,无法更新审批结果({})]", |
| | | saleQuotation.getNo(), bpmResult); |
| | | throw exception(SALE_QUOTATION_UPDATE_AUDIT_STATUS_FAIL_NOT_PROCESS, saleQuotation.getNo()); |
| | | } |
| | | |
| | | // 2. 转换 BPM 审批结果并更新 |
| | | Integer status; |
| | | if (ObjUtil.equal(bpmResult, BpmProcessInstanceStatusEnum.APPROVE.getStatus())) { |
| | | status = CrmSaleQuotationStatusEnum.APPROVE.getStatus(); |
| | | } else if (ObjUtil.equal(bpmResult, BpmProcessInstanceStatusEnum.REJECT.getStatus())) { |
| | | status = CrmSaleQuotationStatusEnum.REJECT.getStatus(); |
| | | } else if (ObjUtil.equal(bpmResult, BpmProcessInstanceStatusEnum.CANCEL.getStatus())) { |
| | | status = CrmSaleQuotationStatusEnum.CANCEL.getStatus(); |
| | | } else { |
| | | log.warn("[updateSaleQuotationAuditStatus][报价单({}) 收到未知审批结果({}),忽略]", id, bpmResult); |
| | | return; |
| | | } |
| | | saleQuotationMapper.updateById(new CrmSaleQuotationDO().setId(id).setStatus(status)); |
| | | } |
| | | |
| | | @Override |
| | | public List<Map<String, Object>> getSaleQuotationApproveProcessDefinitionList() { |
| | | validateSaleQuotationApproveCategoryAndProcessDefinition(); |
| | | List<BpmProcessDefinitionInfoDO> definitionInfoList = bpmProcessDefinitionService |
| | | .getProcessDefinitionInfoListByCategory(SALE_QUOTATION_APPROVE_CATEGORY_CODE); |
| | | Set<String> processDefinitionIds = convertSet(definitionInfoList, BpmProcessDefinitionInfoDO::getProcessDefinitionId); |
| | | List<ProcessDefinition> processDefinitions = bpmProcessDefinitionService.getProcessDefinitionList(processDefinitionIds); |
| | | // 只保留激活状态,且相同 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); |
| | | } |
| | | } |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | | * 校验 BPM 销售报价审批分类和流程定义是否存在 |
| | | */ |
| | | private void validateSaleQuotationApproveCategoryAndProcessDefinition() { |
| | | List<String> codes = Collections.singletonList(SALE_QUOTATION_APPROVE_CATEGORY_CODE); |
| | | Map<String, ?> categoryMap = bpmCategoryService.getCategoryMap(codes); |
| | | if (!categoryMap.containsKey(SALE_QUOTATION_APPROVE_CATEGORY_CODE)) { |
| | | throw exception(SALE_QUOTATION_BPM_PROCESS_DEFINITION_NOT_EXISTS); |
| | | } |
| | | List<BpmProcessDefinitionInfoDO> definitionInfoList = bpmProcessDefinitionService |
| | | .getProcessDefinitionInfoListByCategory(SALE_QUOTATION_APPROVE_CATEGORY_CODE); |
| | | if (CollUtil.isEmpty(definitionInfoList)) { |
| | | throw exception(SALE_QUOTATION_BPM_PROCESS_DEFINITION_NOT_EXISTS); |
| | | } |
| | | 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(SALE_QUOTATION_BPM_PROCESS_DEFINITION_NOT_EXISTS); |
| | | } |
| | | } |
| | | |
| | | private CrmSaleQuotationDO validateSaleQuotationExists(Long id) { |
| | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Long convertToContract(Long id, Long userId) { |
| | | // 1. 校验报价单存在且为草稿状态 |
| | | // 1. 校验报价单存在,且为草稿或审批通过状态 |
| | | CrmSaleQuotationDO saleQuotation = validateSaleQuotationExists(id); |
| | | if (!CrmSaleQuotationStatusEnum.DRAFT.getStatus().equals(saleQuotation.getStatus())) { |
| | | throw exception(SALE_QUOTATION_CONVERT_FAIL_NOT_DRAFT, saleQuotation.getNo()); |
| | | if (!CrmSaleQuotationStatusEnum.DRAFT.getStatus().equals(saleQuotation.getStatus()) |
| | | && !CrmSaleQuotationStatusEnum.APPROVE.getStatus().equals(saleQuotation.getStatus())) { |
| | | CrmSaleQuotationStatusEnum current = CrmSaleQuotationStatusEnum.getByStatus(saleQuotation.getStatus()); |
| | | throw exception(SALE_QUOTATION_CONVERT_FAIL_NOT_DRAFT, saleQuotation.getNo(), |
| | | current != null ? current.getName() : String.valueOf(saleQuotation.getStatus())); |
| | | } |
| | | // 校验是否已转合同 |
| | | if (saleQuotation.getContractId() != null) { |