chenhj
3 天以前 8097a2a4383a3785e11990170d61c59ae6b48888
库存明细数据处理
已修改3个文件
已添加1个文件
175 ■■■■ 文件已修改
main-business/src/main/java/com/ruoyi/business/service/InventorySummaryService.java 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
main-business/src/main/java/com/ruoyi/business/service/impl/InputInventoryRecordServiceImpl.java 66 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
main-business/src/main/java/com/ruoyi/business/service/impl/InventorySummaryServiceImpl.java 25 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
main-business/src/main/java/com/ruoyi/business/utils/InventoryUtils.java 73 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
main-business/src/main/java/com/ruoyi/business/service/InventorySummaryService.java
@@ -1,9 +1,11 @@
package com.ruoyi.business.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.ruoyi.business.dto.OfficialInventoryDto;
import com.ruoyi.business.dto.PendingInventoryDto;
import com.ruoyi.business.entity.InventorySummary;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
/**
 * <p>
@@ -21,4 +23,11 @@
     * @param officialInventoryDto æ­£å¼åº“å­˜
     */
    int updateInventory(PendingInventoryDto pendingInventoryDto, OfficialInventoryDto officialInventoryDto);
    /**
     * æ‰¹é‡æ›´æ–°åº“å­˜
     * @param ids åº“å­˜id
     * @return
     */
    int updateInventorySummary(List<Long> ids);
}
main-business/src/main/java/com/ruoyi/business/service/impl/InputInventoryRecordServiceImpl.java
@@ -11,6 +11,7 @@
import com.ruoyi.business.mapper.InventorySummaryMapper;
import com.ruoyi.business.service.InputInventoryRecordService;
import com.ruoyi.business.service.InventorySummaryService;
import com.ruoyi.business.utils.InventoryUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@@ -19,6 +20,7 @@
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import static com.ruoyi.business.constant.InventoryRecordConstant.OFFICIAL_INVENTORY;
import static com.ruoyi.business.constant.InventoryRecordConstant.PENDING_INVENTORY;
@@ -37,6 +39,7 @@
    private final InputInventoryRecordMapper inputInventoryRecordMapper;
    private final InventorySummaryService inventorySummaryService;
    private final InventorySummaryMapper inventorySummaryMapper;
    private final InventoryUtils inventoryUtils;
    @Override
    public int insertInputInventoryRecord(PendingInventoryDto pendingInventoryDto, OfficialInventoryDto officialInventoryDto, BigDecimal quantity) {
@@ -62,36 +65,41 @@
    @Override
    public int deleteInputInventoryRecord(List<Long> ids) {
        if (CollectionUtils.isNotEmpty(ids)) {
            List<InputInventoryRecord> inputInventoryRecords = inputInventoryRecordMapper.selectBatchIds(ids);
            // æ ¹æ®id进行降序排序
            List<InputInventoryRecord> inputInventoryRecordList = inputInventoryRecords.stream()
                    .sorted(Comparator.comparing(InputInventoryRecord::getId).reversed())
                    .toList();
            String InventoryType = inputInventoryRecords.get(0).getInventoryType();
            List<Long> inventoryIds = inputInventoryRecords.stream().map(InputInventoryRecord::getInventoryId).toList();
            List<InventorySummary> inventorySummaries = inventorySummaryMapper.selectList(new LambdaQueryWrapper<InventorySummary>()
                    .eq(InventorySummary::getInventoryType, InventoryType)
                    .in(InventorySummary::getInventoryId, inventoryIds));
            if (CollectionUtils.isNotEmpty(inventorySummaries)) {
                List<InventorySummary> updates = new ArrayList<>();
                for (InventorySummary inventorySummary : inventorySummaries) {
                    for (InputInventoryRecord inputInventoryRecord : inputInventoryRecords) {
                        // å¦‚果节点上的入库记录id大于变更的id,说明此id前所有入库记录都要重新计算
                        if (Objects.equals(inventorySummary.getInventoryId(), inputInventoryRecord.getInventoryId()) && inventorySummary.getInputEndRecordId() > inputInventoryRecord.getId()) {
                            inventorySummary.setInputEndRecordId(inputInventoryRecord.getId());
                            updates.add(inventorySummary);
                        }
                    }
                }
                // é‡ç½®ä»–们的节点最终id
                inventorySummaryMapper.updateById(updates);
            }
        if (CollectionUtils.isEmpty(ids)) {
            throw new RuntimeException("请传入要删除的id记录");
        }
        // todo é‡æ–°è®¡ç®—节点库存
        // todo æ›´æ–°åº“存实施数据
        List<InputInventoryRecord> inputInventoryRecords = inputInventoryRecordMapper.selectBatchIds(ids);
        // æ ¹æ®id进行降序排序
        List<InputInventoryRecord> inputInventoryRecordList = inputInventoryRecords.stream()
                .sorted(Comparator.comparing(InputInventoryRecord::getId).reversed())
                .toList();
        String InventoryType = inputInventoryRecords.get(0).getInventoryType();
        List<Long> inventoryIds = inputInventoryRecords.stream().map(InputInventoryRecord::getInventoryId).toList().stream().distinct().collect(Collectors.toList());
        List<InventorySummary> inventorySummaries = inventorySummaryMapper.selectList(new LambdaQueryWrapper<InventorySummary>()
                .eq(InventorySummary::getInventoryType, InventoryType)
                .in(InventorySummary::getInventoryId, inventoryIds));
        if (CollectionUtils.isNotEmpty(inventorySummaries)) {
            List<InventorySummary> updates = new ArrayList<>();
            for (InventorySummary inventorySummary : inventorySummaries) {
                for (InputInventoryRecord inputInventoryRecord : inputInventoryRecordList) {
                    // å¦‚果节点上的入库记录id大于变更的id,说明此id前所有入库记录都要重新计算
                    if (Objects.equals(inventorySummary.getInventoryId(), inputInventoryRecord.getInventoryId()) && inventorySummary.getInputEndRecordId() > inputInventoryRecord.getId()) {
                        inventorySummary.setInputEndRecordId(inputInventoryRecord.getId());
                        updates.add(inventorySummary);
                    }
                }
            }
            // é‡ç½®èŠ‚ç‚¹æœ€ç»ˆid
            inventorySummaryMapper.updateById(updates);
        }
        // é‡æ–°è®¡ç®—节点库存
        inventorySummaryService.updateInventorySummary(ids);
        // æ›´æ–°åº“存实时数据
        inventoryUtils.updateInventoryByIds(inventoryIds, inputInventoryRecords.get(0).getInventoryType());
        // åˆ é™¤å…¥åº“记录
main-business/src/main/java/com/ruoyi/business/service/impl/InventorySummaryServiceImpl.java
@@ -87,4 +87,29 @@
            return officialInventoryMapper.updateById(officialInventory);
        }
    }
    @Override
    public int updateInventorySummary(List<Long> ids) {
        List<InventorySummary> inventorySummaries = inventorySummaryMapper.selectBatchIds(ids);
        for (InventorySummary inventorySummary : inventorySummaries) {
            // æŸ¥è¯¢èŠ‚ç‚¹ä¹‹å‰æ‰€æœ‰å…¥åº“è®°å½•
            List<InputInventoryRecord> inputInventoryRecords = inputInventoryRecordMapper.selectList(new LambdaQueryWrapper<InputInventoryRecord>()
                    .eq(InputInventoryRecord::getInventoryId, inventorySummary.getInventoryId())
                    .eq(InputInventoryRecord::getInventoryType, inventorySummary.getInventoryType())
                    .lt(InputInventoryRecord::getId, inventorySummary.getInputEndRecordId()));
            // æŸ¥è¯¢èŠ‚ç‚¹ä¹‹å‰æ‰€æœ‰å‡ºåº“è®°å½•
            List<OutputInventoryRecord> outputInventoryRecords = outputInventoryRecordMapper.selectList(new LambdaQueryWrapper<OutputInventoryRecord>()
                    .eq(OutputInventoryRecord::getInventoryId, inventorySummary.getInventoryId())
                    .eq(OutputInventoryRecord::getInventoryType, inventorySummary.getInventoryType())
                    .lt(OutputInventoryRecord::getId, inventorySummary.getOutputEndRecordId()));
            // è®¡ç®—库存数量
            BigDecimal inputQuantity = inputInventoryRecords.stream().map(InputInventoryRecord::getQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal outputQuantity = outputInventoryRecords.stream().map(OutputInventoryRecord::getQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);
            inventorySummary.setInventoryQuantity(inputQuantity.subtract(outputQuantity));
        }
        return inventorySummaryMapper.updateById(inventorySummaries).size();
    }
}
main-business/src/main/java/com/ruoyi/business/utils/InventoryUtils.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,73 @@
package com.ruoyi.business.utils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.business.constant.InventoryRecordConstant;
import com.ruoyi.business.entity.*;
import com.ruoyi.business.mapper.*;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.util.List;
@Component
public class InventoryUtils {
    @Autowired
    private InventorySummaryMapper inventorySummaryMapper;
    @Autowired
    private InputInventoryRecordMapper inputInventoryRecordMapper;
    @Autowired
    private OutputInventoryRecordMapper outputInventoryRecordMapper;
    @Autowired
    private OfficialInventoryMapper officialInventoryMapper;
    @Autowired
    private PendingInventoryMapper pendingInventoryMapper;
    /**
     * æ›´æ–°åº“å­˜
     */
    public void updateInventoryByIds(List<Long> ids, String type) {
        if (StringUtils.isEmpty(type)) {
            throw new RuntimeException("请指定库存类型");
        }
        for (Long id : ids) {
            // èŽ·å–åº“å­˜èŠ‚ç‚¹
            InventorySummary inventorySummary = inventorySummaryMapper.selectOne(new LambdaQueryWrapper<InventorySummary>()
                    .eq(InventorySummary::getInventoryId, id)
                    .eq(InventorySummary::getInventoryType, type));
            Long inputEndRecordId = inventorySummary != null ? inventorySummary.getInputEndRecordId() : 0L;
            Long outputEndRecordId = inventorySummary != null ? inventorySummary.getOutputEndRecordId() : 0L;
            List<InputInventoryRecord> inputInventoryRecords = inputInventoryRecordMapper.selectList(new LambdaQueryWrapper<InputInventoryRecord>()
                    .eq(InputInventoryRecord::getInventoryId, id)
                    .eq(InputInventoryRecord::getInventoryType, type)
                    .gt(InputInventoryRecord::getId, inputEndRecordId));
            List<OutputInventoryRecord> outputInventoryRecords = outputInventoryRecordMapper.selectList(new LambdaQueryWrapper<OutputInventoryRecord>()
                    .eq(OutputInventoryRecord::getInventoryId, id)
                    .eq(OutputInventoryRecord::getInventoryType, type)
                    .gt(OutputInventoryRecord::getId, outputEndRecordId));
            // èŽ·å–åº“å­˜æ•°æ®
            BigDecimal inputQuantity = inputInventoryRecords.stream().map(InputInventoryRecord::getQuantity).reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
            BigDecimal outputQuantity = outputInventoryRecords.stream().map(OutputInventoryRecord::getQuantity).reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
            // è®¡ç®—这次库存数据
            BigDecimal quantity = inventorySummary != null ? inventorySummary.getInventoryQuantity().add(inputQuantity.subtract(outputQuantity)) : inputQuantity.subtract(outputQuantity);
            if (inventorySummary != null) {
                if (type.equals(InventoryRecordConstant.OFFICIAL_INVENTORY)) {
                    OfficialInventory update = new OfficialInventory();
                    update.setInventoryQuantity(quantity);
                    update.setId(id);
                    officialInventoryMapper.updateById(update);
                } else {
                    PendingInventory update = new PendingInventory();
                    update.setInventoryQuantity(quantity);
                    update.setId(id);
                    pendingInventoryMapper.updateById(update);
                }
            }
        }
    }
}