| | |
| | | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| | | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; |
| | | import cn.iocoder.yudao.module.bpm.api.task.BpmProcessInstanceApi; |
| | | import cn.iocoder.yudao.module.bpm.api.task.dto.BpmProcessInstanceCreateReqDTO; |
| | | import cn.iocoder.yudao.module.crm.controller.admin.receivable.vo.receivable.CrmReceivablePageReqVO; |
| | | import cn.iocoder.yudao.module.crm.controller.admin.receivable.vo.receivable.CrmReceivableSaveReqVO; |
| | | import cn.iocoder.yudao.module.crm.dal.dataobject.contract.CrmContractDO; |
| | |
| | | import com.mzt.logapi.service.impl.DiffParseFunction; |
| | | import com.mzt.logapi.starter.annotation.LogRecord; |
| | | import jakarta.annotation.Resource; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.context.annotation.Lazy; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.*; |
| | | import static cn.iocoder.yudao.module.crm.enums.LogRecordConstants.*; |
| | | import static cn.iocoder.yudao.module.crm.util.CrmAuditStatusUtils.convertBpmResultToAuditStatus; |
| | | |
| | | /** |
| | | * CRM 回款 Service 实现类 |
| | |
| | | */ |
| | | @Service |
| | | @Validated |
| | | @Slf4j |
| | | public class CrmReceivableServiceImpl implements CrmReceivableService { |
| | | |
| | | /** |
| | | * BPM 合同审批流程标识 |
| | | */ |
| | | public static final String BPM_PROCESS_DEFINITION_KEY = "crm-receivable-audit"; |
| | | |
| | | @Resource |
| | | private CrmReceivableMapper receivableMapper; |
| | |
| | | |
| | | @Resource |
| | | private AdminUserApi adminUserApi; |
| | | @Resource |
| | | private BpmProcessInstanceApi bpmProcessInstanceApi; |
| | | @Resource |
| | | private StorageAttachmentApi storageAttachmentApi; |
| | | |
| | |
| | | throw exception(RECEIVABLE_NO_EXISTS); |
| | | } |
| | | |
| | | // 2.1 插入回款 |
| | | // 2.1 插入回款(创建后即为未审核状态) |
| | | CrmReceivableDO receivable = BeanUtils.toBean(createReqVO, CrmReceivableDO.class) |
| | | .setNo(no).setAuditStatus(CrmAuditStatusEnum.DRAFT.getStatus()); |
| | | .setNo(no).setAuditStatus(CrmAuditStatusEnum.PROCESS.getStatus()); |
| | | receivableMapper.insert(receivable); |
| | | // 2.2 |
| | | |
| | |
| | | // 1.5 校验可回款金额不超过该开票剩余可回款金额 |
| | | validateReceivablePriceExceedsInvoiceRemaining(updateReqVO); |
| | | |
| | | // 1.6 只有草稿、审批中,可以编辑; |
| | | if (!ObjectUtils.equalsAny(oldReceivable.getAuditStatus(), CrmAuditStatusEnum.DRAFT.getStatus(), |
| | | CrmAuditStatusEnum.PROCESS.getStatus())) { |
| | | // 1.6 只有未审核,可以编辑; |
| | | if (ObjectUtil.notEqual(oldReceivable.getAuditStatus(), CrmAuditStatusEnum.PROCESS.getStatus())) { |
| | | throw exception(RECEIVABLE_UPDATE_FAIL_EDITING_PROHIBITED); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | @Override |
| | | public void updateReceivableAuditStatus(Long id, Integer bpmResult) { |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updateReceivableStatus(Long id, Integer status) { |
| | | boolean approve = CrmAuditStatusEnum.APPROVE.getStatus().equals(status); |
| | | // 1.1 校验存在 |
| | | CrmReceivableDO receivable = validateReceivableExists(id); |
| | | // 1.2 只有审批中,可以更新审批结果 |
| | | if (ObjUtil.notEqual(receivable.getAuditStatus(), CrmAuditStatusEnum.PROCESS.getStatus())) { |
| | | log.error("[updateReceivableAuditStatus][receivable({}) 不处于审批中,无法更新审批结果({})]", |
| | | receivable.getId(), bpmResult); |
| | | throw exception(RECEIVABLE_UPDATE_AUDIT_STATUS_FAIL_NOT_PROCESS); |
| | | // 1.2 校验状态 |
| | | if (receivable.getAuditStatus().equals(status)) { |
| | | throw exception(approve ? RECEIVABLE_APPROVE_FAIL : RECEIVABLE_PROCESS_FAIL); |
| | | } |
| | | |
| | | // 2. 更新回款审批状态 |
| | | Integer auditStatus = convertBpmResultToAuditStatus(bpmResult); |
| | | receivableMapper.updateById(new CrmReceivableDO().setId(id).setAuditStatus(auditStatus)); |
| | | // 2. 更新状态 |
| | | int updateCount = receivableMapper.updateByIdAndStatus(id, receivable.getAuditStatus(), |
| | | new CrmReceivableDO().setAuditStatus(status)); |
| | | if (updateCount == 0) { |
| | | throw exception(approve ? RECEIVABLE_APPROVE_FAIL : RECEIVABLE_PROCESS_FAIL); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | |
| | | // 3. 记录操作日志上下文 |
| | | LogRecordContext.putVariable("receivable", receivable); |
| | | LogRecordContext.putVariable("period", getReceivablePeriod(receivable.getPlanId())); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @LogRecord(type = CRM_RECEIVABLE_TYPE, subType = CRM_RECEIVABLE_SUBMIT_SUB_TYPE, bizNo = "{{#id}}", |
| | | success = CRM_RECEIVABLE_SUBMIT_SUCCESS) |
| | | public void submitReceivable(Long id, Long userId) { |
| | | // 1. 校验回款是否在审批 |
| | | CrmReceivableDO receivable = validateReceivableExists(id); |
| | | if (ObjUtil.notEqual(receivable.getAuditStatus(), CrmAuditStatusEnum.DRAFT.getStatus())) { |
| | | throw exception(RECEIVABLE_SUBMIT_FAIL_NOT_DRAFT); |
| | | } |
| | | |
| | | // 2. 创建回款审批流程实例 |
| | | String processInstanceId = bpmProcessInstanceApi.createProcessInstance(userId, new BpmProcessInstanceCreateReqDTO() |
| | | .setProcessDefinitionKey(BPM_PROCESS_DEFINITION_KEY).setBusinessKey(String.valueOf(id))); |
| | | |
| | | // 3. 更新回款工作流编号 |
| | | receivableMapper.updateById(new CrmReceivableDO().setId(id).setProcessInstanceId(processInstanceId) |
| | | .setAuditStatus(CrmAuditStatusEnum.PROCESS.getStatus())); |
| | | |
| | | // 4. 记录日志 |
| | | LogRecordContext.putVariable("receivableNo", receivable.getNo()); |
| | | } |
| | | |
| | | private CrmReceivableDO validateReceivableExists(Long id) { |