gongchunyi
10 小时以前 467615249613c312a984097d3ebb673ebef3e10d
src/main/java/com/ruoyi/production/service/impl/ProductOrderServiceImpl.java
@@ -2,21 +2,34 @@
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.exception.ServiceException;
import com.ruoyi.procurementrecord.utils.StockUtils;
import com.ruoyi.production.dto.ProductOrderDto;
import com.ruoyi.production.dto.ProductOrderSourceDto;
import com.ruoyi.production.dto.ProductStructureDto;
import com.ruoyi.production.enums.ProductOrderStatusEnum;
import com.ruoyi.production.mapper.*;
import com.ruoyi.production.pojo.*;
import com.ruoyi.production.service.ProcessRouteService;
import com.ruoyi.production.service.IProductionOrderAppendixService;
import com.ruoyi.production.service.ProductOrderService;
import com.ruoyi.productionPlan.mapper.ProductOrderPlanMapper;
import com.ruoyi.productionPlan.mapper.ProductionPlanMapper;
import com.ruoyi.productionPlan.pojo.ProductOrderPlan;
import com.ruoyi.productionPlan.pojo.ProductionPlan;
import com.ruoyi.quality.mapper.QualityInspectMapper;
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.stream.Collectors;
@Service
public class ProductOrderServiceImpl extends ServiceImpl<ProductOrderMapper, ProductOrder> implements ProductOrderService {
@@ -39,9 +52,35 @@
    @Autowired
    private ProductWorkOrderMapper productWorkOrderMapper;
    @Autowired
    private ProductionProductMainMapper productionProductMainMapper;
    @Autowired
    private ProductOrderPlanMapper productOrderPlanMapper;
    @Autowired
    private ProductionPlanMapper productionPlanMapper;
    @Autowired
    private ProductionProductOutputMapper productionProductOutputMapper;
    @Autowired
    private ProductionProductInputMapper productionProductInputMapper;
    @Autowired
    private QualityInspectMapper qualityInspectMapper;
    @Autowired
    private SalesLedgerProductionAccountingMapper salesLedgerProductionAccountingMapper;
    @Autowired
    private StockUtils stockUtils;
    @Autowired
    private IProductionOrderAppendixService productionOrderAppendixService;
    @Override
    public IPage<ProductOrderDto> pageProductOrder(Page page, ProductOrderDto productOrder) {
    public IPage<ProductOrderDto> pageProductOrder(Page<ProductOrder> page, ProductOrderDto productOrder) {
        return productOrderMapper.pageProductOrder(page, productOrder);
    }
@@ -69,11 +108,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 +122,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());
@@ -102,6 +137,52 @@
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean revoke(List<Long> ids) {
        List<ProductOrder> orders = productOrderMapper.selectBatchIds(ids);
        if (orders.isEmpty()) {
            throw new RuntimeException("生产订单不存在");
        }
        for (ProductOrder order : orders) {
            if (!ProductOrderStatusEnum.canRevoke(order.getStatus())) {
                throw new RuntimeException("只有【待开始】状态的订单才可以撤回");
            }
        }
        // 回退生产计划
        List<ProductOrderPlan> productOrderPlans = productOrderPlanMapper.selectList(Wrappers.<ProductOrderPlan>lambdaQuery().in(ProductOrderPlan::getProductOrderId, ids));
        for (ProductOrderPlan productOrderPlan : productOrderPlans) {
            ProductionPlan productionPlan = productionPlanMapper.selectById(productOrderPlan.getProductionPlanId());
            if (productionPlan != null) {
                BigDecimal newAssigned = productionPlan.getAssignedQuantity().subtract(productOrderPlan.getAssignedQuantity());
                if (newAssigned.compareTo(BigDecimal.ZERO) < 0) {
                    newAssigned = BigDecimal.ZERO;
                }
                productionPlan.setAssignedQuantity(newAssigned);
                BigDecimal volume = productionPlan.getVolume() == null ? BigDecimal.ZERO : productionPlan.getVolume();
                int status;
                if (newAssigned.compareTo(BigDecimal.ZERO) == 0) {
                    status = 0; // 未下发
                } else if (newAssigned.compareTo(volume) < 0) {
                    status = 1; // 部分下发
                } else {
                    status = 2; // 已下发
                }
                productionPlan.setStatus(status);
                productionPlanMapper.updateById(productionPlan);
            }
        }
        // 将订单状态改为已取消
        for (ProductOrder order : orders) {
            order.setStatus(ProductOrderStatusEnum.CANCEL.getCode());
        }
        updateBatchById(orders);
        return true;
    }
    @Override
    public List<ProcessRoute> listProcessRoute(Long productModelId) {
        return productOrderMapper.listProcessRoute(productModelId);
    }
@@ -110,4 +191,142 @@
    public List<ProductStructureDto> listProcessBom(Long orderId) {
        return productOrderMapper.listProcessBom(orderId);
    }
    @Override
    public Boolean addProductOrder(ProductOrder productOrder) {
        fillAndSaveProductOrder(productOrder);
        return true;
    }
    @Override
    public Long insertProductOrder(ProductOrder productOrder) {
        fillAndSaveProductOrder(productOrder);
        return productOrder.getId();
    }
    private void fillAndSaveProductOrder(ProductOrder productOrder) {
        String orderNo = generateNextOrderNo(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")));
        productOrder.setNpsNo(orderNo);
        productOrder.setCompleteQuantity(BigDecimal.ZERO);
        this.save(productOrder);
        if (ObjectUtils.isNotEmpty(productOrder.getRouteId())) {
            this.bindingRoute(productOrder);
        }
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(Long id) {
        ProductOrder order = productOrderMapper.selectById(id);
        if (order == null) {
            throw new RuntimeException("生产订单不存在");
        }
        if (!ProductOrderStatusEnum.canDelete(order.getStatus())) {
            throw new RuntimeException("只有【待开始、已取消】状态的订单才可以删除");
        }
        //  是否已生产
        List<ProductWorkOrder> productWorkOrders = productWorkOrderMapper.selectList(Wrappers.<ProductWorkOrder>lambdaQuery().eq(ProductWorkOrder::getProductOrderId, id));
        if (!productWorkOrders.isEmpty()) {
            List<Long> workOrderIds = productWorkOrders.stream()
                    .map(ProductWorkOrder::getId)
                    .collect(Collectors.toList());
            List<ProductionProductMain> productionProductMains = productionProductMainMapper.selectList(Wrappers.<ProductionProductMain>lambdaQuery().in(ProductionProductMain::getWorkOrderId, workOrderIds));
            if (!productionProductMains.isEmpty()) {
                throw new RuntimeException("生产订单已经开始生产,不能删除");
            }
            //  删除工单
            productWorkOrderMapper.delete(Wrappers.<ProductWorkOrder>lambdaQuery().eq(ProductWorkOrder::getProductOrderId, id));
        }
        //  回退生产计划
        List<ProductOrderPlan> productOrderPlans = productOrderPlanMapper.selectList(Wrappers.<ProductOrderPlan>lambdaQuery().eq(ProductOrderPlan::getProductOrderId, id));
        for (ProductOrderPlan productOrderPlan : productOrderPlans) {
            ProductionPlan productionPlan = productionPlanMapper.selectById(productOrderPlan.getProductionPlanId());
            if (productionPlan != null) {
                //  回退数量
                BigDecimal newAssigned = productionPlan.getAssignedQuantity()
                        .subtract(productOrderPlan.getAssignedQuantity());
                if (newAssigned.compareTo(BigDecimal.ZERO) < 0) {
                    newAssigned = BigDecimal.ZERO;
                }
                productionPlan.setAssignedQuantity(newAssigned);
                BigDecimal volume = productionPlan.getVolume() == null ? BigDecimal.ZERO : productionPlan.getVolume();
                int status;
                if (newAssigned.compareTo(BigDecimal.ZERO) == 0) {
                    status = 0; // 未下发
                } else if (newAssigned.compareTo(volume) < 0) {
                    status = 1; // 部分下发
                } else {
                    status = 2; // 已下发
                }
                productionPlan.setStatus(status);
                productionPlanMapper.updateById(productionPlan);
            }
        }
        // 删除中间表
        productOrderPlanMapper.delete(Wrappers.<ProductOrderPlan>lambdaQuery().eq(ProductOrderPlan::getProductOrderId, id));
        //  删除附表的工艺路线与BOM
        productionOrderAppendixService.deleteData(order.getId(), order.getRouteId());
        //  删除订单
        productOrderMapper.deleteById(id);
        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 List<ProductOrderSourceDto> productOrderSource(Long orderId) {
        if (orderId == null) {
            throw new ServiceException("查询订单数据不能为空");
        }
        ProductOrder productOrder = getById(orderId);
        if (productOrder == null) {
            throw new ServiceException("查询失败,生产订单不存在");
        }
        List<ProductOrderSourceDto> list = baseMapper.productOrderSource(orderId);
        return list;
    }
}