package com.ruoyi.production.service.impl; import cn.hutool.core.util.BooleanUtil; import com.alibaba.fastjson2.JSON; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ruoyi.common.enums.StockOutQualifiedRecordTypeEnum; import com.ruoyi.common.exception.base.BaseException; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.procurementrecord.utils.StockUtils; import com.ruoyi.production.dto.DrawMaterialDto; import com.ruoyi.production.dto.ProductOrderDto; import com.ruoyi.production.dto.ProductStructureDto; import com.ruoyi.production.mapper.*; import com.ruoyi.production.pojo.*; import com.ruoyi.production.service.ProductOrderService; import com.ruoyi.quality.mapper.QualityInspectMapper; import com.ruoyi.stock.dto.StockInventoryDto; import com.ruoyi.stock.mapper.StockInventoryMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.*; import java.util.stream.Collectors; @Service public class ProductOrderServiceImpl extends ServiceImpl implements ProductOrderService { @Autowired private ProductOrderMapper productOrderMapper; @Autowired private ProcessRouteMapper processRouteMapper; @Autowired private ProductProcessRouteMapper productProcessRouteMapper; @Autowired private ProcessRouteItemMapper processRouteItemMapper; @Autowired private ProductProcessRouteItemMapper productProcessRouteItemMapper; @Autowired private ProductWorkOrderMapper productWorkOrderMapper; @Autowired private ProductionProductMainMapper productionProductMainMapper; @Autowired private ProductionProductOutputMapper productionProductOutputMapper; @Autowired private ProductionProductInputMapper productionProductInputMapper; @Autowired private QualityInspectMapper qualityInspectMapper; @Autowired private SalesLedgerProductionAccountingMapper salesLedgerProductionAccountingMapper; @Autowired private StockUtils stockUtils; @Autowired private ProductStructureMapper productStructureMapper; @Autowired private StockInventoryMapper stockInventoryMapper; @Override public IPage pageProductOrder(Page page, ProductOrderDto productOrder) { IPage productOrderDtoIPage = productOrderMapper.pageProductOrder(page, productOrder); List productOrderDtos = productOrderDtoIPage.getRecords(); for (int i = 0; i < productOrderDtos.size(); i++) { ProductOrderDto productOrderDto = productOrderDtos.get(i); if (BooleanUtil.isTrue(productOrderDto.getIsEnd())) { // 如果生产订单被结束,则将完成进度设置为100% productOrderDto.setCompletionStatus(BigDecimal.valueOf(100)); } } return productOrderDtoIPage; } @Override public int bindingRoute(ProductOrder productOrder) { //新增生产订单下的工艺路线主表 ProcessRoute processRoute = processRouteMapper.selectById(productOrder.getRouteId()); ProductProcessRoute productProcessRoute = new ProductProcessRoute(); productProcessRoute.setProductModelId(processRoute.getProductModelId()); productProcessRoute.setProcessRouteCode(processRoute.getProcessRouteCode()); productProcessRoute.setProductOrderId(productOrder.getId()); productProcessRoute.setBomId(processRoute.getBomId()); productProcessRouteMapper.insert(productProcessRoute); //新增生产订单下的工艺路线子表 List processRouteItems = processRouteItemMapper.selectList(new QueryWrapper().lambda().eq(ProcessRouteItem::getRouteId, processRoute.getId())); // 生成当前日期的前缀:年月日 String datePrefix = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); for (ProcessRouteItem processRouteItem : processRouteItems) { ProductProcessRouteItem productProcessRouteItem = new ProductProcessRouteItem(); productProcessRouteItem.setProductModelId(processRouteItem.getProductModelId()); productProcessRouteItem.setProcessId(processRouteItem.getProcessId()); productProcessRouteItem.setProductOrderId(productOrder.getId()); productProcessRouteItem.setProductRouteId(productProcessRoute.getId()); productProcessRouteItem.setDragSort(processRouteItem.getDragSort()); int insert = productProcessRouteItemMapper.insert(productProcessRouteItem); if (insert > 0) { // 查询今日已存在的最大工单号 ProductWorkOrder lastWorkOrder = productWorkOrderMapper.selectMax(datePrefix); int sequenceNumber = 1; // 默认序号 if (lastWorkOrder != null && lastWorkOrder.getWorkOrderNo() != null) { String lastNo = lastWorkOrder.getWorkOrderNo().toString(); if (lastNo.startsWith(datePrefix)) { String seqStr = lastNo.substring(datePrefix.length()); try { sequenceNumber = Integer.parseInt(seqStr) + 1; } catch (NumberFormatException e) { sequenceNumber = 1; } } } // 生成完整的工单号 String workOrderNoStr = "GD" + String.format("%s%03d", datePrefix, sequenceNumber); ProductWorkOrder productWorkOrder = new ProductWorkOrder(); productWorkOrder.setProductProcessRouteItemId(productProcessRouteItem.getId()); productWorkOrder.setProductOrderId(productOrder.getId()); ProductOrder order = productOrderMapper.selectById(productOrder.getId()); productWorkOrder.setPlanQuantity(order.getQuantity()); productWorkOrder.setWorkOrderNo(workOrderNoStr); productWorkOrder.setStatus(1); productWorkOrderMapper.insert(productWorkOrder); } } return productOrderMapper.updateById(productOrder); } @Override public List listProcessRoute(Long productModelId) { return productOrderMapper.listProcessRoute(productModelId); } @Override public List listProcessBom(Long orderId) { return productOrderMapper.listProcessBom(orderId); } @Override public Boolean addProductOrder(ProductOrder productOrder) { String string = generateNextOrderNo(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))); productOrder.setNpsNo(string); productOrder.setCompleteQuantity(BigDecimal.ZERO); this.save(productOrder); if (ObjectUtils.isNotEmpty(productOrder.getRouteId())) { this.bindingRoute(productOrder); } return true; } @Override public Boolean delete(Long[] ids) { //如果已经开始生产,不能删除 //查询生产订单下的工单 List productWorkOrders = productWorkOrderMapper.selectList(Wrappers.lambdaQuery().in(ProductWorkOrder::getProductOrderId, ids)); if (productWorkOrders.size() > 0) { //判断是否有报工数据 List productionProductMains = productionProductMainMapper.selectList(Wrappers.lambdaQuery() .in(ProductionProductMain::getWorkOrderId, productWorkOrders.stream().map(ProductWorkOrder::getId).collect(Collectors.toList()))); if (productionProductMains.size() > 0) { throw new RuntimeException("生产订单已经开始生产,不能删除"); } //删除工单 productWorkOrderMapper.delete(Wrappers.lambdaQuery().in(ProductWorkOrder::getProductOrderId, ids)); } //删除工艺路线 productProcessRouteItemMapper.delete(new LambdaQueryWrapper() .in(ProductProcessRouteItem::getProductOrderId, ids)); productProcessRouteMapper.delete(new LambdaQueryWrapper() .in(ProductProcessRoute::getProductOrderId, ids)); //删除生产订单 productOrderMapper.delete(new LambdaQueryWrapper() .in(ProductOrder::getId, ids)); return true; } //获取当前生产订单号 public String getMaxOrderNoByDate(String datePrefix) { QueryWrapper queryWrapper = new QueryWrapper<>(); // 匹配以 SC + 日期开头的订单号 queryWrapper.likeRight("nps_no", "SC" + datePrefix); // 按订单号倒序排列 queryWrapper.orderByDesc("nps_no"); queryWrapper.last("LIMIT 1"); ProductOrder latestOrder = this.getOne(queryWrapper); return latestOrder != null ? latestOrder.getNpsNo() : null; } public String generateNextOrderNo(String datePrefix) { String maxOrderNo = getMaxOrderNoByDate(datePrefix); int sequence = 1; // 默认起始序号 if (maxOrderNo != null && !maxOrderNo.isEmpty()) { // 提取流水号部分(假设格式为 SC + 日期 + 流水号) String sequenceStr = maxOrderNo.substring(("SC" + datePrefix).length()); try { sequence = Integer.parseInt(sequenceStr) + 1; } catch (NumberFormatException e) { // 异常情况下重置为1 sequence = 1; } } // 生成新订单号 return "SC" + datePrefix + String.format("%04d", sequence); } @Override public int finishOrder(Long orderId) { ProductOrder productOrder = new ProductOrder(); productOrder.setId(orderId); productOrder.setIsEnd(true); return productOrderMapper.updateById(productOrder); } @Override public int cleanRecord(Long id, Map cleanRecord) { ProductOrder productOrder = productOrderMapper.selectById(id); if (productOrder == null) { throw new BaseException("订单不存在"); } productOrder.setCleanRecord(JSON.toJSONString(cleanRecord)); return productOrderMapper.updateById(productOrder); } @Override public List getByBomId(Long bomId) { List structureList = productStructureMapper.listBybomId(bomId); if (CollectionUtils.isEmpty(structureList)) { return Collections.emptyList(); } Set allNodeIds = structureList.stream() .map(ProductStructureDto::getId) .collect(Collectors.toSet()); Set parentIds = structureList.stream() .filter(node -> node.getParentId() != null && node.getParentId() != 0) .map(ProductStructureDto::getParentId) .collect(Collectors.toSet()); Set leafNodeIds = new HashSet<>(allNodeIds); leafNodeIds.removeAll(parentIds); // 获取叶子节点的 productModelId List productModelIds = structureList.stream() .filter(node -> leafNodeIds.contains(node.getId())) .map(ProductStructureDto::getProductModelId) .filter(Objects::nonNull) .distinct() .collect(Collectors.toList()); if (productModelIds.isEmpty()) { return Collections.emptyList(); } return stockInventoryMapper.getStockInventory(productModelIds); } @Override @Transactional(rollbackFor = Exception.class) public int drawMaterials(ProductOrderDto productOrderDto) { if (productOrderDto == null || productOrderDto.getId() == null) { throw new RuntimeException("参数错误"); } String jsonString = productOrderDto.getDrawMaterials(); if (StringUtils.isEmpty(jsonString)) { throw new RuntimeException("领料明细不能为空"); } List drawMaterialsList; try { drawMaterialsList = JSON.parseArray(jsonString, DrawMaterialDto.class); } catch (Exception e) { throw new RuntimeException("领料明细格式错误"); } if (CollectionUtils.isEmpty(drawMaterialsList)) { throw new RuntimeException("领料明细不能为空"); } // 处理领料(扣减库存) try { for (DrawMaterialDto drawMaterialDto : drawMaterialsList) { if (drawMaterialDto.getProductModelId() == null) { throw new RuntimeException("产品型号ID不能为空"); } stockUtils.substractStock(drawMaterialDto.getProductModelId(), drawMaterialDto.getRequisitionQty(), StockOutQualifiedRecordTypeEnum.DRAW_MATERIALS_STOCK_OUT.getCode(), productOrderDto.getId()); } } catch (Exception e) { throw new RuntimeException("领料失败:" + e.getMessage()); } // JSON字符串存储 String newJsonString = JSON.toJSONString(drawMaterialsList); int update = productOrderMapper.update(null, new LambdaUpdateWrapper() .eq(ProductOrder::getId, productOrderDto.getId()) .set(ProductOrder::getDrawMaterials, newJsonString)); // 校验更新结果 if (update == 0) { throw new RuntimeException("更新订单失败"); } return update; } }