liding
2 天以前 3163a87aeb728317ebdf565f25362f0a485205f3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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);
                }
            }
        }
    }
}