| | |
| | | |
| | | 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.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.mes.controller.admin.wm.productsales.vo.MesWmProductSalesPageReqVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.wm.productsales.vo.MesWmProductSalesImportExcelVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.wm.productsales.vo.MesWmProductSalesSaveReqVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.wm.productsales.vo.line.MesWmProductSalesLineSaveReqVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.wm.vo.MesWmDocImportRespVO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.md.item.MesMdItemDO; |
| | | import cn.iocoder.yudao.module.mes.service.md.item.MesMdItemService; |
| | | import cn.iocoder.yudao.module.crm.api.customer.dto.CrmCustomerRespDTO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.wm.productsales.vo.MesWmProductSalesShippingReqVO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.wm.salesnotice.MesWmSalesNoticeDO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.wm.productsales.MesWmProductSalesDO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.wm.productsales.MesWmProductSalesDetailDO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.wm.productsales.MesWmProductSalesLineDO; |
| | | import cn.iocoder.yudao.module.mes.dal.mysql.wm.productsales.MesWmProductSalesMapper; |
| | | import cn.iocoder.yudao.module.mes.dal.mysql.wm.productsales.MesWmProductSalesLineMapper; |
| | | import cn.iocoder.yudao.module.mes.enums.MesBizTypeConstants; |
| | | import cn.iocoder.yudao.module.mes.enums.wm.MesWmProductSalesStatusEnum; |
| | | import cn.iocoder.yudao.module.mes.enums.wm.MesWmQualityStatusEnum; |
| | | import cn.iocoder.yudao.module.mes.enums.wm.MesWmSalesNoticeStatusEnum; |
| | | import cn.iocoder.yudao.module.mes.enums.wm.MesWmStockReserveBizTypeEnum; |
| | | import cn.iocoder.yudao.module.mes.enums.wm.MesWmTransactionTypeEnum; |
| | | import cn.iocoder.yudao.module.mes.service.md.client.MesMdClientService; |
| | | import cn.iocoder.yudao.module.crm.api.customer.CrmCustomerApi; |
| | | import cn.iocoder.yudao.module.mes.service.wm.materialstock.MesWmMaterialStockService; |
| | | import cn.iocoder.yudao.module.mes.service.wm.salesnotice.MesWmSalesNoticeService; |
| | | import cn.iocoder.yudao.module.mes.service.wm.stockreserve.MesWmStockReserveService; |
| | | import cn.iocoder.yudao.module.mes.service.wm.transaction.MesWmTransactionService; |
| | | import cn.iocoder.yudao.module.mes.service.wm.transaction.dto.MesWmTransactionSaveReqDTO; |
| | | import cn.iocoder.yudao.module.mes.service.qc.oqc.MesQcOqcService; |
| | | import cn.iocoder.yudao.module.erp.api.sale.ErpSaleOrderApi; |
| | | import cn.iocoder.yudao.module.erp.api.sale.enums.ErpSaleOrderOutStatusEnum; |
| | | 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 org.springframework.validation.annotation.Validated; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; |
| | |
| | | /** |
| | | * MES 销售出库单 Service 实现类 |
| | | * |
| | | * @author 芋道源码 |
| | | * @author 超级管理员 |
| | | */ |
| | | @Service |
| | | @Validated |
| | | @Slf4j |
| | | public class MesWmProductSalesServiceImpl implements MesWmProductSalesService { |
| | | |
| | | @Resource |
| | | private MesWmProductSalesMapper productSalesMapper; |
| | | @Resource |
| | | private MesWmProductSalesLineMapper productSalesLineMapper; |
| | | |
| | | @Resource |
| | | private MesWmProductSalesLineService productSalesLineService; |
| | | @Resource |
| | | private MesWmProductSalesDetailService productSalesDetailService; |
| | | @Resource |
| | | private MesMdClientService clientService; |
| | | private CrmCustomerApi customerApi; |
| | | @Resource |
| | | private MesWmSalesNoticeService salesNoticeService; |
| | | @Resource |
| | |
| | | private MesWmMaterialStockService materialStockService; |
| | | @Resource |
| | | private MesWmStockReserveService stockReserveService; |
| | | @Resource |
| | | @Lazy |
| | | private MesQcOqcService oqcService; |
| | | |
| | | @Resource |
| | | private ErpSaleOrderApi saleOrderApi; |
| | | |
| | | @Resource |
| | | @Lazy |
| | | private MesMdItemService mdItemService; |
| | | |
| | | @Override |
| | | public Long createProductSales(MesWmProductSalesSaveReqVO createReqVO) { |
| | |
| | | |
| | | @Override |
| | | public PageResult<MesWmProductSalesDO> getProductSalesPage(MesWmProductSalesPageReqVO pageReqVO) { |
| | | return productSalesMapper.selectPage(pageReqVO); |
| | | return productSalesMapper.selectPage(pageReqVO, (List<Long>) null); |
| | | } |
| | | |
| | | @Override |
| | |
| | | } |
| | | } |
| | | |
| | | // 获取出库单信息 |
| | | MesWmProductSalesDO sales = productSalesMapper.selectById(id); |
| | | |
| | | // 检查所有行的 oqcCheckFlag:如果有需要 OQC 检验的行,进入待检测状态 |
| | | boolean needOqc = CollectionUtils.anyMatch(lines, |
| | | line -> Boolean.TRUE.equals(line.getOqcCheckFlag())); |
| | | if (needOqc) { |
| | | // 为需要 OQC 检验且尚未创建检验单的明细创建 OQC 检验单 |
| | | List<MesWmProductSalesLineDO> oqcLines = CollectionUtils.filterList(lines, |
| | | line -> Boolean.TRUE.equals(line.getOqcCheckFlag()) && line.getOqcId() == null); |
| | | for (MesWmProductSalesLineDO line : oqcLines) { |
| | | // 直接调用,让异常抛出,保证数据一致性 |
| | | Long oqcId = oqcService.createOqcFromProductSales( |
| | | sales.getId(), line, sales.getCode(), sales.getClientId()); |
| | | // 更新出库明细的 oqcId |
| | | productSalesLineMapper.updateById(new MesWmProductSalesLineDO() |
| | | .setId(line.getId()) |
| | | .setOqcId(oqcId)); |
| | | } |
| | | |
| | | // 批量初始化 OQC 行的质量状态为"待检验" |
| | | List<Long> oqcLineIds = convertList( |
| | | CollectionUtils.filterList(lines, line -> Boolean.TRUE.equals(line.getOqcCheckFlag())), |
| | |
| | | // 执行拣货(待拣货 → 待填写运单) |
| | | productSalesMapper.updateById(new MesWmProductSalesDO() |
| | | .setId(id).setStatus(MesWmProductSalesStatusEnum.SHIPPING.getStatus())); |
| | | |
| | | // 回写 ERP 销售订单出库状态为"部分出库"(表示已拣货/占用) |
| | | if (sales.getSaleOrderId() != null) { |
| | | saleOrderApi.updateOrderOutStatusTo(sales.getSaleOrderId(), |
| | | ErpSaleOrderOutStatusEnum.PART_OUT.getStatus()); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | |
| | | productSalesMapper.updateById(new MesWmProductSalesDO() |
| | | .setId(id).setStatus(MesWmProductSalesStatusEnum.FINISHED.getStatus())); |
| | | |
| | | // 5. 更新关联的发货通知单状态 |
| | | // 5. 更新关联的发货通知单出库数量和状态 |
| | | if (sales.getNoticeId() != null) { |
| | | salesNoticeService.finishSalesNotice(sales.getNoticeId()); |
| | | updateSalesNoticeOutCount(sales); |
| | | } |
| | | |
| | | // 6. 回写 ERP 销售订单的出库数量和状态 |
| | | updateErpSaleOrderOutCount(sales); |
| | | } |
| | | |
| | | /** |
| | | * 更新发货通知单的出库数量和状态 |
| | | */ |
| | | private void updateSalesNoticeOutCount(MesWmProductSalesDO sales) { |
| | | // 获取出库单行 |
| | | List<MesWmProductSalesLineDO> lines = productSalesLineService.getProductSalesLineListBySalesId(sales.getId()); |
| | | if (CollUtil.isEmpty(lines)) { |
| | | return; |
| | | } |
| | | // 遍历行,按 noticeLineId 更新出库数量 |
| | | for (MesWmProductSalesLineDO line : lines) { |
| | | if (line.getNoticeLineId() != null && line.getQuantity() != null) { |
| | | salesNoticeService.updateSalesNoticeLineOutCount(line.getNoticeLineId(), line.getQuantity()); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | .setBatchId(detail.getBatchId()).setBatchCode(detail.getBatchCode()) |
| | | .setWarehouseId(detail.getWarehouseId()).setLocationId(detail.getLocationId()).setAreaId(detail.getAreaId()) |
| | | .setBizType(MesBizTypeConstants.WM_PRODUCT_SALES).setBizId(sales.getId()) |
| | | .setBizCode(sales.getCode()).setBizLineId(detail.getLineId()))); |
| | | .setBizCode(sales.getCode()).setBizLineId(detail.getLineId()) |
| | | .setMaterialStockId(detail.getMaterialStockId()))); // 指定库存记录ID,避免重新匹配 |
| | | } |
| | | |
| | | @Override |
| | |
| | | // 校验编码唯一 |
| | | validateCodeUnique(id, reqVO.getCode()); |
| | | // 校验客户存在 |
| | | clientService.validateClientExistsAndEnable(reqVO.getClientId()); |
| | | customerApi.validateCustomer(reqVO.getClientId()); |
| | | // 校验发货通知单 |
| | | if (reqVO.getNoticeId() != null) { |
| | | MesWmSalesNoticeDO notice = salesNoticeService.validateSalesNoticeExists(reqVO.getNoticeId()); |
| | |
| | | productSalesMapper.updateById(new MesWmProductSalesDO().setId(id).setStatus(status)); |
| | | } |
| | | |
| | | // ==================== 批量导入 ==================== |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public MesWmDocImportRespVO importProductSalesList(List<MesWmProductSalesImportExcelVO> importList) { |
| | | MesWmDocImportRespVO respVO = MesWmDocImportRespVO.empty(); |
| | | if (CollUtil.isEmpty(importList)) { |
| | | return respVO; |
| | | } |
| | | // 1. 按「出库单号」归并成单据 + 明细行 |
| | | Map<String, List<MesWmProductSalesImportExcelVO>> grouped = new LinkedHashMap<>(); |
| | | int index = 0; |
| | | for (MesWmProductSalesImportExcelVO row : importList) { |
| | | index++; |
| | | String code = StrUtil.trim(row.getSalesCode()); |
| | | if (StrUtil.isBlank(code)) { |
| | | respVO.getFailureCodes().put("第 " + index + " 行", "出库单号不能为空"); |
| | | continue; |
| | | } |
| | | grouped.computeIfAbsent(code, k -> new ArrayList<>()).add(row); |
| | | } |
| | | |
| | | // 2. 预加载客户,按名称建索引 |
| | | Map<String, Long> clientMap = new HashMap<>(); |
| | | try { |
| | | List<CrmCustomerRespDTO> customers = customerApi.getCustomerSimpleList(); |
| | | if (CollUtil.isNotEmpty(customers)) { |
| | | customers.forEach(c -> { |
| | | if (StrUtil.isNotBlank(c.getName())) { |
| | | clientMap.put(c.getName().trim(), c.getId()); |
| | | } |
| | | }); |
| | | } |
| | | } catch (Exception e) { |
| | | log.warn("[importProductSalesList][加载客户失败]", e); |
| | | } |
| | | |
| | | // 3. 逐张单据处理 |
| | | for (Map.Entry<String, List<MesWmProductSalesImportExcelVO>> entry : grouped.entrySet()) { |
| | | String code = entry.getKey(); |
| | | List<MesWmProductSalesImportExcelVO> rows = entry.getValue(); |
| | | MesWmProductSalesImportExcelVO head = rows.get(0); |
| | | try { |
| | | // 3.1 校验客户 |
| | | String clientName = StrUtil.trim(head.getClientName()); |
| | | Long clientId = clientName == null ? null : clientMap.get(clientName); |
| | | if (clientId == null) { |
| | | respVO.getFailureCodes().put(code, "客户[" + clientName + "]不存在"); |
| | | continue; |
| | | } |
| | | // 3.2 校验日期与编码 |
| | | LocalDateTime salesDate = parseImportDateTime(head.getSalesDate()); |
| | | if (salesDate == null) { |
| | | respVO.getFailureCodes().put(code, "出库日期不能为空或格式不正确(支持 yyyy-MM-dd / yyyy-MM-dd HH:mm:ss)"); |
| | | continue; |
| | | } |
| | | if (productSalesMapper.selectByCode(code) != null) { |
| | | respVO.getFailureCodes().put(code, "出库单号已存在"); |
| | | continue; |
| | | } |
| | | // 3.3 创建单据头 |
| | | MesWmProductSalesSaveReqVO saveReqVO = new MesWmProductSalesSaveReqVO(); |
| | | saveReqVO.setCode(code); |
| | | saveReqVO.setName(StrUtil.blankToDefault(StrUtil.trim(head.getSalesName()), code)); |
| | | saveReqVO.setClientId(clientId); |
| | | saveReqVO.setSalesDate(salesDate); |
| | | saveReqVO.setSalesOrderCode(StrUtil.trim(head.getSalesOrderCode())); |
| | | saveReqVO.setContactName(StrUtil.trim(head.getContactName())); |
| | | saveReqVO.setContactTelephone(StrUtil.trim(head.getContactTelephone())); |
| | | saveReqVO.setContactAddress(StrUtil.trim(head.getContactAddress())); |
| | | saveReqVO.setCarrier(StrUtil.trim(head.getCarrier())); |
| | | saveReqVO.setShippingNumber(StrUtil.trim(head.getShippingNumber())); |
| | | saveReqVO.setRemark(StrUtil.trim(head.getRemark())); |
| | | Long salesId = createProductSales(saveReqVO); |
| | | // 3.4 逐行创建明细 |
| | | for (MesWmProductSalesImportExcelVO row : rows) { |
| | | createProductSalesLineByImport(salesId, row, respVO, code); |
| | | } |
| | | respVO.getCreateCodes().add(code); |
| | | } catch (Exception e) { |
| | | respVO.getFailureCodes().put(code, e.getMessage() == null ? "导入失败" : e.getMessage()); |
| | | } |
| | | } |
| | | return respVO; |
| | | } |
| | | |
| | | /** |
| | | * 导入时创建单行明细;失败只记录不抛出,避免影响整张单据的其它行 |
| | | */ |
| | | private void createProductSalesLineByImport(Long salesId, MesWmProductSalesImportExcelVO row, |
| | | MesWmDocImportRespVO respVO, String salesCode) { |
| | | String itemCode = StrUtil.trim(row.getItemCode()); |
| | | MesMdItemDO item = itemCode == null ? null : mdItemService.getItemByCode(itemCode); |
| | | if (item == null) { |
| | | respVO.getFailureCodes().put(salesCode, "物料编码[" + itemCode + "]不存在,该行已跳过"); |
| | | return; |
| | | } |
| | | if (row.getQuantity() == null || row.getQuantity().signum() <= 0) { |
| | | respVO.getFailureCodes().put(salesCode, "物料[" + itemCode + "]出库数量必须大于 0,该行已跳过"); |
| | | return; |
| | | } |
| | | try { |
| | | MesWmProductSalesLineSaveReqVO lineReqVO = new MesWmProductSalesLineSaveReqVO(); |
| | | lineReqVO.setSalesId(salesId); |
| | | lineReqVO.setItemId(item.getId()); |
| | | lineReqVO.setQuantity(row.getQuantity()); |
| | | lineReqVO.setBatchCode(StrUtil.trim(row.getBatchCode())); |
| | | lineReqVO.setRemark(StrUtil.trim(row.getRemark())); |
| | | productSalesLineService.createProductSalesLine(lineReqVO); |
| | | } catch (Exception e) { |
| | | respVO.getFailureCodes().put(salesCode, "物料[" + itemCode + "]明细创建失败:" + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 解析导入日期,支持 yyyy-MM-dd 与 yyyy-MM-dd HH:mm:ss,失败返回 null |
| | | */ |
| | | private LocalDateTime parseImportDateTime(String text) { |
| | | if (StrUtil.isBlank(text)) { |
| | | return null; |
| | | } |
| | | String value = text.trim().replace('/', '-'); |
| | | try { |
| | | if (value.length() <= 10) { |
| | | return LocalDate.parse(value).atStartOfDay(); |
| | | } |
| | | return LocalDateTime.parse(value.replace(' ', 'T')); |
| | | } catch (Exception e) { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | } |
| | | |