chenhj
2026-04-24 ffe4413db421aabb7446007ff76d4e9943c6fc84
src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java
@@ -6,20 +6,14 @@
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.enums.StockInUnQualifiedRecordTypeEnum;
import com.ruoyi.common.enums.StockOutQualifiedRecordTypeEnum;
import com.ruoyi.framework.web.domain.R;
import com.ruoyi.procurementrecord.utils.StockUtils;
import com.ruoyi.production.mapper.*;
import com.ruoyi.production.pojo.ProductionAccount;
import com.ruoyi.production.pojo.ProductionOperationTask;
import com.ruoyi.production.pojo.ProductionPlan;
import com.ruoyi.production.pojo.ProductionProductMain;
import com.ruoyi.production.service.ProductionOrderService;
import com.ruoyi.purchase.mapper.PurchaseLedgerMapper;
import com.ruoyi.purchase.pojo.PurchaseLedger;
import com.ruoyi.quality.mapper.QualityInspectMapper;
import com.ruoyi.quality.pojo.QualityInspect;
import com.ruoyi.sales.dto.InvoiceRegistrationProductDto;
import com.ruoyi.sales.dto.SalesLedgerProductDto;
import com.ruoyi.sales.mapper.InvoiceRegistrationProductMapper;
@@ -44,7 +38,9 @@
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
@@ -68,6 +64,7 @@
    private final PurchaseLedgerMapper purchaseLedgerMapper;
    private final ProductionPlanMapper productionPlanMapper;
    private final ProductionOperationTaskMapper productionOperationTaskMapper;
    private final ProductionOrderService productionOrderService;
    private final TechnologyRoutingMapper technologyRoutingMapper;
    private final TechnologyBomStructureMapper technologyBomStructureMapper;
    private final InvoiceRegistrationProductMapper invoiceRegistrationProductMapper;
@@ -253,49 +250,38 @@
     * 新增生产数据
     */
    public void addProductionData(SalesLedgerProduct salesLedgerProduct) {
        if (!Integer.valueOf(1).equals(salesLedgerProduct.getType())) {
        //先判断该产品是否需要生产
        if (!salesLedgerProduct.getIsProduction()) {
            return;
        }
        SalesLedger salesLedger = salesLedgerMapper.selectById(salesLedgerProduct.getSalesLedgerId());
        ProductionPlan productionPlan = new ProductionPlan();
        productionPlan.setApplyNo(buildSalesPlanApplyNo(salesLedgerProduct.getId()));
        productionPlan.setSource("sales");
        productionPlan.setSalesLedgerId(salesLedgerProduct.getSalesLedgerId());
        productionPlan.setSalesLedgerProductId(salesLedgerProduct.getId());
        productionPlan.setMpsNo(generateNextPlanNo(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))));
        productionPlan.setProductModelId(salesLedgerProduct.getProductModelId());
        productionPlan.setQtyRequired(salesLedgerProduct.getQuantity());
        productionPlan.setRemark("销售台账自动生成");
        productionPlan.setIssued(Boolean.FALSE);
        productionPlan.setSource("销售");
        productionPlan.setStatus(0);
        if (salesLedger != null) {
            productionPlan.setMpsNo(salesLedger.getSalesContractNo());
            if (salesLedger.getDeliveryDate() != null) {
                productionPlan.setRequiredDate(salesLedger.getDeliveryDate().atStartOfDay());
                productionPlan.setPromisedDeliveryDate(salesLedger.getDeliveryDate().atStartOfDay());
            }
        }
        productionPlanMapper.insert(productionPlan);
    }
    /**
     * 删除生产数据
     * 删除生产计划
     */
    public void deleteProductionData(List<Long> productIds) {
        List<String> applyNos = productIds.stream()
                .filter(Objects::nonNull)
                .map(this::buildSalesPlanApplyNo)
                .collect(Collectors.toList());
        if (CollectionUtils.isEmpty(applyNos)) {
            return;
        }
        List<ProductionPlan> productionPlans = productionPlanMapper.selectList(
                new LambdaQueryWrapper<ProductionPlan>().in(ProductionPlan::getApplyNo, applyNos));
        if (CollectionUtils.isEmpty(productionPlans)) {
                new LambdaQueryWrapper<ProductionPlan>()
                        .in(ProductionPlan::getSalesLedgerProductId, productIds.stream().map(Long::intValue).collect(Collectors.toList())));
        if (org.springframework.util.CollectionUtils.isEmpty(productionPlans)) {
            return;
        }
        boolean hasIssued = productionPlans.stream().anyMatch(item -> Boolean.TRUE.equals(item.getIssued()) || (item.getStatus() != null && item.getStatus() > 0));
        if (hasIssued) {
            throw new ServiceException("对应生产计划已下发生成生产订单,请先处理生产计划/生产订单后再修改销售台账");
        //如果生产计划已下发则不能删除
        if (productionPlans.stream().anyMatch(productionPlan -> productionPlan.getStatus() != 0)) {
            throw new RuntimeException("生产计划已下发,不能删除该销售产品");
        }
        productionPlanMapper.deleteByIds(productionPlans.stream().map(ProductionPlan::getId).collect(Collectors.toList()));
        List<Long> ids = productionPlans.stream().map(ProductionPlan::getId).collect(Collectors.toList());
        productionPlanMapper.deleteByIds(ids);
    }
    @Override
@@ -408,7 +394,21 @@
        return R.ok();
    }
    private String buildSalesPlanApplyNo(Long salesLedgerProductId) {
        return "SALE_PRODUCT_" + salesLedgerProductId;
    private String generateNextPlanNo(String datePrefix) {
        QueryWrapper<ProductionPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("mps_no", "JH" + datePrefix);
        queryWrapper.orderByDesc("mps_no");
        queryWrapper.last("LIMIT 1");
        ProductionPlan latestPlan = productionPlanMapper.selectOne(queryWrapper);
        int sequence = 1;
        if (latestPlan != null && latestPlan.getMpsNo() != null && !latestPlan.getMpsNo().isEmpty()) {
            String sequenceStr = latestPlan.getMpsNo().substring(("JH" + datePrefix).length());
            try {
                sequence = Integer.parseInt(sequenceStr) + 1;
            } catch (NumberFormatException e) {
                sequence = 1;
            }
        }
        return "JH" + datePrefix + String.format("%04d", sequence);
    }
}