liding
2 天以前 f6f57ba70679a0b050031f3cdf81b5bf5d4cbd60
main-business/src/main/java/com/ruoyi/business/service/impl/ProductionMasterServiceImpl.java
@@ -12,12 +12,16 @@
import com.ruoyi.business.mapper.*;
import com.ruoyi.business.service.ProductionMasterService;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
@@ -49,6 +53,32 @@
    public IPage<ProductionMasterDto> selectPMList(Page page, ProductionMasterDto productionMasterDto) {
        // 1. 构建主表查询条件
        LambdaQueryWrapper<ProductionMaster> masterQueryWrapper = new LambdaQueryWrapper<>();
        String keyword = productionMasterDto.getSearchAll();
        if (StringUtils.isNotBlank(keyword)) {
            // 查询煤种名称中模糊匹配的coalId列表
            List<Long> matchedCoalIds = coalInfoMapper.selectList(
                            new LambdaQueryWrapper<CoalInfo>().like(CoalInfo::getCoal, keyword)
                    ).stream()
                    .map(CoalInfo::getId)
                    .toList();
            // 组装查询条件:煤种ID在匹配的列表中
            // 如果 matchedCoalIds 为空,直接返回 0 条数据(构造一个不可能成立的条件)
            if (matchedCoalIds.isEmpty()) {
                masterQueryWrapper.apply("1 = 0"); // 强制返回空结果
            }
            // 如果有匹配的 coalId,则按 coalId 查询
            else {
                String ids = matchedCoalIds.stream()
                        .map(String::valueOf)
                        .collect(Collectors.joining(","));
                masterQueryWrapper.apply(
                        "{0} = ANY(string_to_array(coal_id, ','))",
                        ids
                );
            }
        }
        // 2. 执行主表分页查询
        IPage<ProductionMaster> entityPage = productionMasterMapper.selectPage(page, masterQueryWrapper);
@@ -77,7 +107,6 @@
        IPage<ProductionMasterDto> dtoPage = new Page<>();
        BeanUtils.copyProperties(entityPage, dtoPage, "records");
        dtoPage.setRecords(dtoList);
        return dtoPage;
    }
@@ -212,8 +241,6 @@
            coalIds.add(p.getCoalId());
        }
        List<CoalInfo> coalInfos = coalInfoMapper.selectList(new LambdaQueryWrapper<CoalInfo>().in(CoalInfo::getId, coalIds));
        ProductionMaster master = new ProductionMaster();
        master.setProductionQuantity(totalQuantity);
        master.setTotalCost(totalCost);
@@ -257,14 +284,46 @@
     * 将加工产生的产品记录到待入库表
     */
    private void insertPendingInventory(List<Production> list) {
        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formattedDate = currentDate.format(formatter);
        // 常量定义:保留2位小数,四舍五入模式
        final int SCALE = 2;
        final RoundingMode ROUNDING_MODE = RoundingMode.HALF_UP;
        // 税率13%,用字符串构造BigDecimal避免精度误差
        final BigDecimal TAX_RATE = new BigDecimal("1.13");
        for (Production p : list) {
            PendingInventory pending = new PendingInventory();
            pending.setCoalId(p.getCoalId());
            pending.setInventoryQuantity(p.getProductionQuantity());
            pending.setSupplierName("生产加工入库");
            pending.setTotalPriceIncludingTax(p.getTotalCost());
            pending.setPriceIncludingTax(p.getPurchasePrice());
            pending.setPriceIncludingTax(p.getPurchasePrice());
            pending.setUnit("t");
            pending.setSupplierName(formattedDate + " - " + "生产加工入库");
            // 1. 非空处理:避免null导致的运算异常
            BigDecimal totalCost = p.getTotalCost() == null ? BigDecimal.ZERO : p.getTotalCost();
            BigDecimal productionQuantity = p.getProductionQuantity() == null ? BigDecimal.ZERO : p.getProductionQuantity();
            // 2. 含税总价 = 含税单价 * 产量 → 保留2位小数
            BigDecimal totalPriceIncludingTax = totalCost.multiply(productionQuantity)
                    .setScale(SCALE, ROUNDING_MODE);
            pending.setTotalPriceIncludingTax(totalPriceIncludingTax);
            // 3. 含税单价 → 直接保留2位小数
            pending.setPriceIncludingTax(totalCost.setScale(SCALE, ROUNDING_MODE));
            // 4. 不含税单价 = 含税单价 / 1.13 → 先高精度计算,再保留2位
            BigDecimal priceExcludingTax = totalCost.divide(TAX_RATE, 10, ROUNDING_MODE) // 中间保留10位防误差
                    .setScale(SCALE, ROUNDING_MODE);   // 最终保留2位
            pending.setPriceExcludingTax(priceExcludingTax);
            // 5. 不含税总价 = 不含税单价 * 产量 → 保留2位小数
            BigDecimal totalPriceExcludingTax = priceExcludingTax.multiply(productionQuantity)
                    .setScale(SCALE, ROUNDING_MODE);
            pending.setTotalPriceExcludingTax(totalPriceExcludingTax);
            pending.setRegistrantId(p.getProducerId());
            pending.setRegistrationDate(LocalDate.now());
            pendingInventoryMapper.insert(pending);
        }
    }