| | |
| | | package cn.iocoder.yudao.module.mes.service.wm.salesnotice; |
| | | |
| | | import cn.iocoder.yudao.module.mes.api.mps.MesProMpsApi; |
| | | |
| | | import cn.iocoder.yudao.module.erp.api.sale.enums.ErpSaleOrderAuditStatus; |
| | | |
| | | 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.object.BeanUtils; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.wm.salesnotice.vo.MesWmSalesNoticeImportExcelVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.wm.salesnotice.vo.MesWmSalesNoticeImportRespVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.wm.salesnotice.vo.MesWmSalesNoticePageReqVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.wm.salesnotice.vo.MesWmSalesNoticeSaveReqVO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.wm.productsales.MesWmProductSalesDO; |
| | |
| | | import cn.iocoder.yudao.module.mes.dal.mysql.wm.salesnotice.MesWmSalesNoticeLineMapper; |
| | | import cn.iocoder.yudao.module.mes.enums.wm.MesWmOutStatusEnum; |
| | | import cn.iocoder.yudao.module.mes.enums.wm.MesWmProductSalesStatusEnum; |
| | | import cn.iocoder.yudao.module.mes.enums.md.autocode.MesMdAutoCodeRuleCodeEnum; |
| | | import cn.iocoder.yudao.module.mes.service.md.autocode.MesMdAutoCodeRecordService; |
| | | import cn.iocoder.yudao.module.mes.service.md.client.MesMdClientService; |
| | | import cn.iocoder.yudao.module.crm.api.customer.CrmCustomerApi; |
| | | import cn.iocoder.yudao.module.erp.api.sale.ErpSaleOrderApi; |
| | | import cn.iocoder.yudao.module.erp.api.sale.dto.ErpSaleOrderItemRespDTO; |
| | | import cn.iocoder.yudao.module.erp.api.sale.dto.ErpSaleOrderRespDTO; |
| | |
| | | import org.springframework.validation.annotation.Validated; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.Collections; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.concurrent.atomic.AtomicInteger; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*; |
| | |
| | | @Resource |
| | | private MesWmSalesNoticeLineService salesNoticeLineService; |
| | | @Resource |
| | | private MesMdClientService clientService; |
| | | private CrmCustomerApi customerApi; |
| | | |
| | | @Resource |
| | | private ErpSaleOrderApi saleOrderApi; |
| | | @Resource |
| | | private MesProMpsApi mpsApi; |
| | | @Resource |
| | | private MesMdAutoCodeRecordService autoCodeRecordService; |
| | | |
| | |
| | | |
| | | @Override |
| | | public MesWmSalesNoticeDO getSalesNotice(Long id) { |
| | | return salesNoticeMapper.selectById(id); |
| | | MesWmSalesNoticeDO notice = salesNoticeMapper.selectById(id); |
| | | // 校验客户权限 |
| | | if (notice != null && notice.getClientId() != null) { |
| | | List<Long> permittedIds = customerApi.getPermittedCustomerIds(); |
| | | if (permittedIds != null && !permittedIds.contains(notice.getClientId())) { |
| | | return null; // 无权限则返回 null |
| | | } |
| | | } |
| | | return notice; |
| | | } |
| | | |
| | | @Override |
| | | public PageResult<MesWmSalesNoticeDO> getSalesNoticePage(MesWmSalesNoticePageReqVO pageReqVO) { |
| | | return salesNoticeMapper.selectPage(pageReqVO); |
| | | // 获取用户有权限的客户 ID 列表 |
| | | List<Long> permittedClientIds = customerApi.getPermittedCustomerIds(); |
| | | return salesNoticeMapper.selectPage(pageReqVO, permittedClientIds); |
| | | } |
| | | |
| | | @Override |
| | |
| | | validateNoticeCodeUnique(saveReqVO.getId(), saveReqVO.getCode()); |
| | | // 校验客户存在 |
| | | if (saveReqVO.getClientId() != null) { |
| | | clientService.validateClientExistsAndEnable(saveReqVO.getClientId()); |
| | | customerApi.validateCustomer(saveReqVO.getClientId()); |
| | | } |
| | | } |
| | | |
| | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Long generateSalesNoticeFromSaleOrder(Long saleOrderId) { |
| | | // 1. 获取销售订单信息 |
| | | // 1. 校验是否已生成过发货通知单(防止重复生成) |
| | | MesWmSalesNoticeDO existNotice = salesNoticeMapper.selectBySaleOrderId(saleOrderId); |
| | | if (existNotice != null) { |
| | | throw exception(WM_SALES_NOTICE_ALREADY_EXISTS); |
| | | } |
| | | |
| | | // 2. 获取销售订单信息 |
| | | ErpSaleOrderRespDTO saleOrder = saleOrderApi.getSaleOrder(saleOrderId).getCheckedData(); |
| | | if (saleOrder == null) { |
| | | throw exception(WM_SALES_NOTICE_SALE_ORDER_NOT_EXISTS); |
| | | } |
| | | |
| | | // 2. 获取销售订单明细 |
| | | if (!Integer.valueOf(ErpSaleOrderAuditStatus.APPROVED).equals(saleOrder.getStatus())) { |
| | | throw exception(WM_SALES_NOTICE_SALE_ORDER_NOT_APPROVED); |
| | | } |
| | | |
| | | // 3. 获取销售订单明细 |
| | | List<ErpSaleOrderItemRespDTO> saleOrderItems = saleOrderApi.getSaleOrderItemList(saleOrderId).getCheckedData(); |
| | | if (CollUtil.isEmpty(saleOrderItems)) { |
| | | throw exception(WM_SALES_NOTICE_SALE_ORDER_ITEM_EMPTY); |
| | | } |
| | | |
| | | // 3. 生成发货通知单编码 |
| | | String code = autoCodeRecordService.generateAutoCode("SALESNOTICE_CODE", null); |
| | | List<Long> productionItemIds = saleOrderItems.stream() |
| | | .filter(item -> Integer.valueOf(1).equals(item.getNeedProduction())) |
| | | .map(ErpSaleOrderItemRespDTO::getId).toList(); |
| | | if (!Boolean.TRUE.equals(mpsApi.checkCanCreateSalesNotice(productionItemIds))) { |
| | | throw exception(WM_SALES_NOTICE_PRODUCTION_NOT_READY); |
| | | } |
| | | // 4. 生成发货通知单编码 |
| | | String code = autoCodeRecordService.generateAutoCode(MesMdAutoCodeRuleCodeEnum.WM_SALES_NOTICE_CODE.getCode(), null); |
| | | |
| | | // 4. 创建发货通知单主表 |
| | | // 5. 创建发货通知单主表 |
| | | MesWmSalesNoticeDO notice = new MesWmSalesNoticeDO(); |
| | | notice.setCode(code); |
| | | notice.setName("销售订单[" + saleOrder.getNo() + "]发货通知"); |
| | |
| | | notice.setStatus(PREPARE.getStatus()); |
| | | salesNoticeMapper.insert(notice); |
| | | |
| | | // 5. 遍历明细创建发货通知单行 |
| | | // 6. 遍历明细创建发货通知单行 |
| | | for (ErpSaleOrderItemRespDTO saleOrderItem : saleOrderItems) { |
| | | MesWmSalesNoticeLineDO line = new MesWmSalesNoticeLineDO(); |
| | | line.setNoticeId(notice.getId()); |
| | |
| | | throw exception(WM_SALES_NOTICE_STATUS_NOT_APPROVED); |
| | | } |
| | | |
| | | // 2. 获取发货通知单行 |
| | | // 2. 校验是否已生成过销售出库单(防止重复生成) |
| | | if (productSalesMapper.selectByNoticeId(noticeId) != null) { |
| | | throw exception(WM_PRODUCT_SALES_ALREADY_EXISTS); |
| | | } |
| | | |
| | | // 3. 获取发货通知单行 |
| | | List<MesWmSalesNoticeLineDO> noticeLines = salesNoticeLineService.getSalesNoticeLineListByNoticeId(noticeId); |
| | | if (CollUtil.isEmpty(noticeLines)) { |
| | | throw exception(WM_SALES_NOTICE_LINE_EMPTY); |
| | | } |
| | | |
| | | // 3. 生成销售出库单编码 |
| | | String code = autoCodeRecordService.generateAutoCode("WM_PRODUCT_SALES_CODE", null); |
| | | // 4. 生成销售出库单编码 |
| | | String code = autoCodeRecordService.generateAutoCode(MesMdAutoCodeRuleCodeEnum.WM_PRODUCT_SALES_CODE.getCode(), null); |
| | | |
| | | // 4. 创建销售出库单主表 |
| | | // 5. 创建销售出库单主表 |
| | | MesWmProductSalesDO productSales = new MesWmProductSalesDO(); |
| | | productSales.setCode(code); |
| | | productSales.setName("发货通知单[" + notice.getCode() + "]出库"); |
| | |
| | | productSales.setStatus(MesWmProductSalesStatusEnum.PREPARE.getStatus()); |
| | | productSalesMapper.insert(productSales); |
| | | |
| | | // 5. 遍历发货通知单行创建销售出库单行 |
| | | // 6. 遍历发货通知单行创建销售出库单行 |
| | | for (MesWmSalesNoticeLineDO noticeLine : noticeLines) { |
| | | MesWmProductSalesLineDO salesLine = new MesWmProductSalesLineDO(); |
| | | salesLine.setSalesId(productSales.getId()); |
| | |
| | | return productSales.getId(); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public MesWmSalesNoticeImportRespVO importSalesNoticeList(List<MesWmSalesNoticeImportExcelVO> importList, boolean updateSupport) { |
| | | if (CollUtil.isEmpty(importList)) { |
| | | throw exception(WM_SALES_NOTICE_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | MesWmSalesNoticeImportRespVO respVO = MesWmSalesNoticeImportRespVO.builder() |
| | | .createCodes(new ArrayList<>()).updateCodes(new ArrayList<>()) |
| | | .failureCodes(new LinkedHashMap<>()).build(); |
| | | AtomicInteger index = new AtomicInteger(1); |
| | | importList.forEach(importItem -> { |
| | | int currentIndex = index.getAndIncrement(); |
| | | String code = importItem.getCode(); |
| | | String key = StrUtil.blankToDefault(code, "第 " + currentIndex + " 行"); |
| | | if (StrUtil.isBlank(code)) { |
| | | respVO.getFailureCodes().put(key, "单号不能为空"); |
| | | return; |
| | | } |
| | | if (StrUtil.isBlank(importItem.getName())) { |
| | | respVO.getFailureCodes().put(key, "名称不能为空"); |
| | | return; |
| | | } |
| | | MesWmSalesNoticeDO existing = salesNoticeMapper.selectByCode(code); |
| | | if (existing != null && updateSupport) { |
| | | if (!PREPARE.getStatus().equals(existing.getStatus())) { |
| | | respVO.getFailureCodes().put(key, "单据非草稿状态,不允许覆盖更新"); |
| | | return; |
| | | } |
| | | MesWmSalesNoticeDO updateObj = BeanUtils.toBean(importItem, MesWmSalesNoticeDO.class); |
| | | updateObj.setId(existing.getId()); |
| | | updateObj.setStatus(existing.getStatus()); |
| | | salesNoticeMapper.updateById(updateObj); |
| | | respVO.getUpdateCodes().add(code); |
| | | } else if (existing != null) { |
| | | respVO.getFailureCodes().put(key, "单号已存在:" + code); |
| | | } else { |
| | | MesWmSalesNoticeDO createObj = BeanUtils.toBean(importItem, MesWmSalesNoticeDO.class); |
| | | createObj.setId(null); |
| | | createObj.setStatus(PREPARE.getStatus()); |
| | | salesNoticeMapper.insert(createObj); |
| | | respVO.getCreateCodes().add(code); |
| | | } |
| | | }); |
| | | return respVO; |
| | | } |
| | | |
| | | } |