| | |
| | | import cn.iocoder.yudao.module.mes.api.mps.MesProMpsApi; |
| | | import cn.iocoder.yudao.module.mes.api.mps.dto.MesProMpsCreateReqDTO; |
| | | import cn.iocoder.yudao.module.mes.api.mps.dto.MesProMpsRespDTO; |
| | | import cn.iocoder.yudao.module.mes.api.salesnotice.MesWmSalesNoticeApi; |
| | | import cn.iocoder.yudao.module.system.api.storage.StorageAttachmentApi; |
| | | import cn.iocoder.yudao.module.system.api.user.AdminUserApi; |
| | | import com.mzt.logapi.starter.annotation.LogRecord; |
| | |
| | | private MdmItemApi mdmItemApi; |
| | | @Resource |
| | | private MesProMpsApi mpsApi; |
| | | @Resource |
| | | private MesWmSalesNoticeApi salesNoticeApi; |
| | | |
| | | @Resource |
| | | private StorageAttachmentApi storageAttachmentApi; |
| | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void cancelSaleOrder(Long id) { |
| | | // 1.1 校验存在 |
| | | ErpSaleOrderDO saleOrder = validateSaleOrderExists(id); |
| | | ErpSaleOrderDO saleOrder = saleOrderMapper.selectByIdForUpdate(id); |
| | | if (saleOrder == null) throw exception(SALE_ORDER_NOT_EXISTS); |
| | | // 1.2 校验状态:只有已审核的订单才能作废 |
| | | if (!ErpAuditStatus.APPROVE.getStatus().equals(saleOrder.getStatus())) { |
| | | throw exception(SALE_ORDER_CANCEL_FAIL_NOT_APPROVE, saleOrder.getNo()); |
| | |
| | | throw exception(SALE_ORDER_CANCEL_FAIL_EXISTS_OUT); |
| | | } |
| | | |
| | | // 销售订单审核后可能已生成 MPS。未取消的计划不能随着订单作废继续执行。 |
| | | List<ErpSaleOrderItemDO> orderItems = saleOrderItemMapper.selectListByOrderId(id); |
| | | if (CollUtil.isNotEmpty(orderItems)) { |
| | | List<Long> itemIds = convertList(orderItems, ErpSaleOrderItemDO::getId); |
| | | List<MesProMpsRespDTO> plans = mpsApi.getMpsListBySaleOrderItemIds(itemIds); |
| | | if (plans == null) throw exception(SALE_ORDER_CANCEL_FAIL_MPS_UNKNOWN); |
| | | if (plans.stream().anyMatch(plan -> plan == null || ObjectUtil.notEqual(plan.getStatus(), MPS_STATUS_CANCELED))) { |
| | | throw exception(SALE_ORDER_CANCEL_FAIL_ACTIVE_MPS); |
| | | } |
| | | } |
| | | Map<Long, Boolean> noticeStatus = salesNoticeApi.checkSalesNoticeBySaleOrderIds(List.of(id)); |
| | | if (noticeStatus == null || noticeStatus.get(id) == null) throw exception(SALE_ORDER_CANCEL_FAIL_NOTICE_UNKNOWN); |
| | | if (Boolean.TRUE.equals(noticeStatus.get(id))) throw exception(SALE_ORDER_CANCEL_FAIL_NOTICE_EXISTS); |
| | | |
| | | // 2. 更新状态为已作废 |
| | | int updateCount = saleOrderMapper.updateByIdAndStatus(id, saleOrder.getStatus(), |
| | | new ErpSaleOrderDO().setStatus(ErpAuditStatus.CANCEL.getStatus())); |