package cn.iocoder.yudao.module.crm.service.contract; 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.contract.vo.command.CrmContractCommandPageReqVO; import cn.iocoder.yudao.module.crm.controller.admin.contract.vo.command.CrmContractCommandRespVO; import cn.iocoder.yudao.module.crm.controller.admin.contract.vo.command.CrmContractCommandSaveReqVO; import cn.iocoder.yudao.module.crm.dal.dataobject.contract.CrmContractCommandDO; import cn.iocoder.yudao.module.crm.dal.dataobject.contract.CrmContractDO; import cn.iocoder.yudao.module.crm.dal.mysql.contract.CrmContractCommandMapper; import cn.iocoder.yudao.module.crm.dal.mysql.contract.CrmContractMapper; import cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants; import cn.iocoder.yudao.module.mes.api.workorder.MesProWorkOrderApi; import jakarta.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; import java.time.LocalDateTime; import java.util.List; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; @Service @Validated public class CrmContractCommandServiceImpl implements CrmContractCommandService { private static final Integer STATUS_WAIT = 0; private static final Integer STATUS_DISPATCHED = 10; private static final Integer STATUS_DONE = 20; private static final Integer STATUS_CANCELED = 30; /** 目标模块:MES 生产工单(有真实落地单据,执行时确认工单) */ private static final String TARGET_MODULE_MES_WORK_ORDER = "mes-work-order"; @Resource private CrmContractCommandMapper commandMapper; @Resource private CrmContractMapper contractMapper; @Resource private MesProWorkOrderApi workOrderApi; @Override @Transactional(rollbackFor = Exception.class) public Long createCommand(CrmContractCommandSaveReqVO createReqVO) { CrmContractDO contract = contractMapper.selectById(createReqVO.getContractId()); if (contract == null) { throw exception(ErrorCodeConstants.CONTRACT_NOT_EXISTS); } if (StrUtil.isBlank(createReqVO.getContractNo())) { createReqVO.setContractNo(contract.getNo()); } if (createReqVO.getOperateTime() == null) { createReqVO.setOperateTime(LocalDateTime.now()); } if (createReqVO.getStatus() == null) { createReqVO.setStatus(STATUS_WAIT); } CrmContractCommandDO entity = BeanUtils.toBean(createReqVO, CrmContractCommandDO.class); commandMapper.insert(entity); return entity.getId(); } @Override public void updateCommand(CrmContractCommandSaveReqVO updateReqVO) { CrmContractCommandDO exists = commandMapper.selectById(updateReqVO.getId()); if (exists == null) { throw exception(ErrorCodeConstants.CRM_CONTRACT_COMMAND_NOT_EXISTS); } CrmContractCommandDO entity = BeanUtils.toBean(updateReqVO, CrmContractCommandDO.class); commandMapper.updateById(entity); } @Override public void deleteCommand(Long id) { CrmContractCommandDO exists = commandMapper.selectById(id); if (exists == null) { throw exception(ErrorCodeConstants.CRM_CONTRACT_COMMAND_NOT_EXISTS); } commandMapper.deleteById(id); } @Override public CrmContractCommandRespVO getCommand(Long id) { CrmContractCommandDO entity = commandMapper.selectById(id); return entity == null ? null : BeanUtils.toBean(entity, CrmContractCommandRespVO.class); } @Override public PageResult getCommandPage(CrmContractCommandPageReqVO pageReqVO) { return BeanUtils.toBean(commandMapper.selectPage(pageReqVO), CrmContractCommandRespVO.class); } @Override @Transactional(rollbackFor = Exception.class) public void executeCommand(Long id) { CrmContractCommandDO exists = commandMapper.selectById(id); if (exists == null) { throw exception(ErrorCodeConstants.CRM_CONTRACT_COMMAND_NOT_EXISTS); } if (!STATUS_WAIT.equals(exists.getStatus()) && !STATUS_DISPATCHED.equals(exists.getStatus())) { throw exception(ErrorCodeConstants.CRM_COMMAND_EXECUTE_FAIL_STATUS); } // 跨模块真实下发:mes-work-order 目标模块,确认指令关联的工单 if (TARGET_MODULE_MES_WORK_ORDER.equals(exists.getTargetModule())) { if (exists.getTargetId() == null) { throw exception(ErrorCodeConstants.CRM_COMMAND_DISPATCH_TARGET_MISSING); } workOrderApi.confirmWorkOrder(exists.getTargetId()); } // 其余目标模块(mes-metering/srm-service)暂无真实落地单据,仅记录状态流转 CrmContractCommandDO update = new CrmContractCommandDO(); update.setId(id); update.setStatus(STATUS_DONE); commandMapper.updateById(update); } @Override @Transactional(rollbackFor = Exception.class) public void cancelCommand(Long id) { CrmContractCommandDO exists = commandMapper.selectById(id); if (exists == null) { throw exception(ErrorCodeConstants.CRM_CONTRACT_COMMAND_NOT_EXISTS); } if (!STATUS_WAIT.equals(exists.getStatus()) && !STATUS_DISPATCHED.equals(exists.getStatus())) { throw exception(ErrorCodeConstants.CRM_COMMAND_EXECUTE_FAIL_STATUS); } // 跨模块联动取消:已下发的 MES 工单同步取消 if (TARGET_MODULE_MES_WORK_ORDER.equals(exists.getTargetModule()) && exists.getTargetId() != null) { workOrderApi.cancelWorkOrder(exists.getTargetId()); } CrmContractCommandDO update = new CrmContractCommandDO(); update.setId(id); update.setStatus(STATUS_CANCELED); commandMapper.updateById(update); } @Override public Long countByContractId(Long contractId) { List list = commandMapper.selectListByContractId(contractId); return (long) list.size(); } }