liding
14 小时以前 cf984315319d060fa55b1f35cd95403690a30762
src/main/java/com/ruoyi/production/service/impl/ProductOrderServiceImpl.java
@@ -1,22 +1,38 @@
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.production.dto.ProductBomDto;
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.ProcessRouteService;
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.List;
import java.util.*;
import java.util.stream.Collectors;
@Service
public class ProductOrderServiceImpl extends ServiceImpl<ProductOrderMapper, ProductOrder> implements ProductOrderService {
@@ -39,10 +55,44 @@
    @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<ProductOrderDto> pageProductOrder(Page page, ProductOrderDto productOrder) {
        return productOrderMapper.pageProductOrder(page, productOrder);
        IPage<ProductOrderDto> productOrderDtoIPage = productOrderMapper.pageProductOrder(page, productOrder);
        List<ProductOrderDto> 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
@@ -69,11 +119,7 @@
            int insert = productProcessRouteItemMapper.insert(productProcessRouteItem);
            if (insert > 0) {
                // 查询今日已存在的最大工单号
                QueryWrapper<ProductWorkOrder> queryWrapper = new QueryWrapper<>();
                queryWrapper.likeRight("work_order_no", datePrefix)
                        .orderByDesc("work_order_no")
                        .last("LIMIT 1");
                ProductWorkOrder lastWorkOrder = productWorkOrderMapper.selectOne(queryWrapper);
                ProductWorkOrder lastWorkOrder = productWorkOrderMapper.selectMax(datePrefix);
                int sequenceNumber = 1; // 默认序号
                if (lastWorkOrder != null && lastWorkOrder.getWorkOrderNo() != null) {
                    String lastNo = lastWorkOrder.getWorkOrderNo().toString();
@@ -87,7 +133,7 @@
                    }
                }
                // 生成完整的工单号
                String workOrderNoStr = String.format("%s%03d", datePrefix, sequenceNumber);
                String workOrderNoStr = "GD" + String.format("%s%03d", datePrefix, sequenceNumber);
                ProductWorkOrder productWorkOrder = new ProductWorkOrder();
                productWorkOrder.setProductProcessRouteItemId(productProcessRouteItem.getId());
                productWorkOrder.setProductOrderId(productOrder.getId());
@@ -110,4 +156,176 @@
    public List<ProductStructureDto> 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<ProductWorkOrder> productWorkOrders = productWorkOrderMapper.selectList(Wrappers.<ProductWorkOrder>lambdaQuery().in(ProductWorkOrder::getProductOrderId, ids));
        if (productWorkOrders.size() > 0) {
            //判断是否有报工数据
            List<ProductionProductMain> productionProductMains = productionProductMainMapper.selectList(Wrappers.<ProductionProductMain>lambdaQuery()
                    .in(ProductionProductMain::getWorkOrderId, productWorkOrders.stream().map(ProductWorkOrder::getId).collect(Collectors.toList())));
            if (productionProductMains.size() > 0) {
                throw new RuntimeException("生产订单已经开始生产,不能删除");
            }
            //删除工单
            productWorkOrderMapper.delete(Wrappers.<ProductWorkOrder>lambdaQuery().in(ProductWorkOrder::getProductOrderId, ids));
        }
        //删除工艺路线
        productProcessRouteItemMapper.delete(new LambdaQueryWrapper<ProductProcessRouteItem>()
                .in(ProductProcessRouteItem::getProductOrderId, ids));
        productProcessRouteMapper.delete(new LambdaQueryWrapper<ProductProcessRoute>()
                .in(ProductProcessRoute::getProductOrderId, ids));
        //删除生产订单
        productOrderMapper.delete(new LambdaQueryWrapper<ProductOrder>()
                .in(ProductOrder::getId, ids));
        return true;
    }
    //获取当前生产订单号
    public String getMaxOrderNoByDate(String datePrefix) {
        QueryWrapper<ProductOrder> 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<String, Object> cleanRecord) {
        ProductOrder productOrder = productOrderMapper.selectById(id);
        if (productOrder == null) {
            throw new BaseException("订单不存在");
        }
        productOrder.setCleanRecord(JSON.toJSONString(cleanRecord));
        return productOrderMapper.updateById(productOrder);
    }
    @Override
    public List<StockInventoryDto> getByBomId(Long bomId) {
        List<ProductStructureDto> structureList = productStructureMapper.listBybomId(bomId);
        if (CollectionUtils.isEmpty(structureList)) {
            return Collections.emptyList();
        }
        Set<Long> allNodeIds = structureList.stream()
                .map(ProductStructureDto::getId)
                .collect(Collectors.toSet());
        Set<Long> parentIds = structureList.stream()
                .filter(node -> node.getParentId() != null && node.getParentId() != 0)
                .map(ProductStructureDto::getParentId)
                .collect(Collectors.toSet());
        Set<Long> leafNodeIds = new HashSet<>(allNodeIds);
        leafNodeIds.removeAll(parentIds);
        //  获取叶子节点的 productModelId
        List<Long> 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<DrawMaterialDto> 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<ProductOrder>()
                        .eq(ProductOrder::getId, productOrderDto.getId())
                        .set(ProductOrder::getDrawMaterials, newJsonString));
        // 校验更新结果
        if (update == 0) {
            throw new RuntimeException("更新订单失败");
        }
        return update;
    }
}