maven
11 小时以前 5eec9b5a9d8bf9e49663d5a51cab7490fef5b204
main-business/src/main/java/com/ruoyi/business/service/impl/ProductionMasterServiceImpl.java
@@ -5,19 +5,23 @@
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.basic.entity.CoalInfo;
import com.ruoyi.basic.mapper.CoalInfoMapper;
import com.ruoyi.business.dto.ProductionMasterDto;
import com.ruoyi.business.entity.*;
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);
@@ -143,7 +173,7 @@
        batchInsertInventories(masterId, dto.getProductionInventoryList());
        // 插入待入库数据
        insertPendingInventory(dto.getProductionList());
//        insertPendingInventory(dto.getProductionList());
        return 1;
    }
@@ -233,6 +263,7 @@
            BeanUtils.copyProperties(p, copy);
            copy.setId(null);
            copy.setProductionMasterId(masterId);
            copy.setStatus(1);
            productionMapper.insert(copy);
        }
    }
@@ -253,14 +284,42 @@
    /**
     * 将加工产生的产品记录到待入库表
     */
    private void insertPendingInventory(List<Production> list) {
    public void insertPendingInventory(List<Production> list) {
        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formattedDate = currentDate.format(formatter);
        final int SCALE = 2;
        final RoundingMode ROUNDING_MODE = RoundingMode.HALF_UP;
        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.setUnit("吨");
            pending.setSupplierName(formattedDate + " - " + "生产加工入库");
            // 非空处理
            BigDecimal totalCost = p.getTotalCost() == null ? BigDecimal.ZERO : p.getTotalCost();
            BigDecimal productionQuantity = p.getProductionQuantity() == null ? BigDecimal.ZERO : p.getProductionQuantity();
            // 含税总价
            BigDecimal totalPriceIncludingTax = totalCost.multiply(productionQuantity)
                    .setScale(SCALE, ROUNDING_MODE);
            pending.setTotalPriceIncludingTax(totalPriceIncludingTax);
            // 含税单价
            pending.setPriceIncludingTax(totalCost.setScale(SCALE, ROUNDING_MODE));
            // 不含税单价(直接保留2位小数)
            BigDecimal priceExcludingTax = totalCost.divide(TAX_RATE, SCALE, ROUNDING_MODE);
            pending.setPriceExcludingTax(priceExcludingTax);
            // 不含税总价
            BigDecimal totalPriceExcludingTax = priceExcludingTax.multiply(productionQuantity)
                    .setScale(SCALE, ROUNDING_MODE);
            pending.setTotalPriceExcludingTax(totalPriceExcludingTax);
            pending.setRegistrantId(p.getProducerId());
            pending.setRegistrationDate(LocalDate.now());
            pendingInventoryMapper.insert(pending);
@@ -344,20 +403,20 @@
        }
        // 批量更新官方库存
        for (Map.Entry<Long, BigDecimal> entry : inventoryAdjustMap.entrySet()) {
            OfficialInventory official = officialInventoryMapper.selectById(entry.getKey());
            if (official == null) {
                throw new BaseException("官方库存不存在,ID: " + entry.getKey());
            }
            // 使用线程安全的BigDecimal操作
            official.setInventoryQuantity(
                    Optional.ofNullable(official.getInventoryQuantity())
                            .orElse(BigDecimal.ZERO)
                            .add(entry.getValue())
            );
            officialInventoryMapper.updateById(official);
        }
//        for (Map.Entry<Long, BigDecimal> entry : inventoryAdjustMap.entrySet()) {
//            OfficialInventory official = officialInventoryMapper.selectById(entry.getKey());
//            if (official == null) {
//                throw new BaseException("官方库存不存在,ID: " + entry.getKey());
//            }
//
//            // 使用线程安全的BigDecimal操作
//            official.setInventoryQuantity(
//                    Optional.ofNullable(official.getInventoryQuantity())
//                            .orElse(BigDecimal.ZERO)
//                            .add(entry.getValue())
//            );
//            officialInventoryMapper.updateById(official);
//        }
        // 批量删除生产库存
        if (!productionIdsToDelete.isEmpty()) {