chenhj
2 天以前 9dcd90dc8e329900e6058ac0a3aa44a9e7e04599
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.ruoyi.business.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.dto.OfficialInventoryDto;
import com.ruoyi.business.dto.PendingInventoryDto;
import com.ruoyi.business.entity.*;
import com.ruoyi.business.mapper.InventorySummaryMapper;
import com.ruoyi.business.mapper.OfficialInventoryMapper;
import com.ruoyi.business.mapper.PendingInventoryMapper;
import com.ruoyi.business.service.InputInventoryRecordService;
import com.ruoyi.business.service.InventorySummaryService;
import com.ruoyi.business.service.OutputInventoryRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.util.List;
 
import static com.ruoyi.business.constant.InventoryRecordConstant.OFFICIAL_INVENTORY;
import static com.ruoyi.business.constant.InventoryRecordConstant.PENDING_INVENTORY;
 
/**
 * <p>
 * 库存汇总表 服务实现类
 * </p>
 *
 * @author chenhj
 * @since 2025-06-14
 */
@Service
@RequiredArgsConstructor
public class InventorySummaryServiceImpl extends ServiceImpl<InventorySummaryMapper, InventorySummary> implements InventorySummaryService {
    @Autowired
    private InventorySummaryMapper inventorySummaryMapper;
    @Autowired
    private InputInventoryRecordService inputInventoryRecordService;
    @Autowired
    private OutputInventoryRecordService outputInventoryRecordService;
    @Autowired
    private PendingInventoryMapper pendingInventoryMapper;
    @Autowired
    private OfficialInventoryMapper officialInventoryMapper;
 
    @Override
    public int updateInventory(PendingInventoryDto pendingInventoryDto, OfficialInventoryDto officialInventoryDto) {
        if ((pendingInventoryDto != null && officialInventoryDto != null) || (pendingInventoryDto == null && officialInventoryDto == null)) {
            throw new RuntimeException("库存记录异常");
        }
 
        String inventoryType = pendingInventoryDto != null ? PENDING_INVENTORY : OFFICIAL_INVENTORY;
        Long inventoryId = pendingInventoryDto != null ? pendingInventoryDto.getId() : officialInventoryDto.getId();
        Long inputEndRecordId = 0L;
        Long outputEndRecordId = 0L;
 
        InventorySummary inventorySummary = inventorySummaryMapper.selectOne(new LambdaQueryWrapper<InventorySummary>()
                .eq(InventorySummary::getInventoryId, inventoryId)
                .eq(InventorySummary::getInventoryType, inventoryType));
        if (inventorySummary != null) {
            inputEndRecordId = inventorySummary.getInputEndRecordId();
            outputEndRecordId  = inventorySummary.getOutputEndRecordId();
        }
        // 查询节点以后所有入库记录
        List<InputInventoryRecord> inputInventoryRecords = inputInventoryRecordService.list(new LambdaQueryWrapper<InputInventoryRecord>()
                .eq(InputInventoryRecord::getInventoryId, inventoryId)
                .eq(InputInventoryRecord::getInventoryType, inventoryType)
                .gt(InputInventoryRecord::getId, inputEndRecordId));
        // 查询节点以后所有出库记录
        List<OutputInventoryRecord> outputInventoryRecords = outputInventoryRecordService.list(new LambdaQueryWrapper<OutputInventoryRecord>()
                .eq(OutputInventoryRecord::getInventoryId, inventoryId)
                .eq(OutputInventoryRecord::getInventoryType, inventoryType)
                .gt(OutputInventoryRecord::getId, outputEndRecordId));
 
        // 如果入库数量大于出库数量,则库存数量为入库数量减去出库数量,反之则为0
        BigDecimal quantity = BigDecimal.ZERO;
        BigDecimal totalInputQuantity = inputInventoryRecords.stream().map(InputInventoryRecord::getQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal totalOutputQuantity = outputInventoryRecords.stream().map(OutputInventoryRecord::getQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);
        if (totalInputQuantity.compareTo(totalOutputQuantity) > 0) {
            quantity = totalInputQuantity.subtract(totalOutputQuantity);
        }
        if (inventoryType.equals(PENDING_INVENTORY)) {
            // 变更待入库值
            PendingInventory pendingInventory = pendingInventoryMapper.getPendingInventoryForUpdateById(pendingInventoryDto.getId());
            if (pendingInventory == null) {
                throw new RuntimeException("库存记录不存在");
            }
            pendingInventory.setInventoryQuantity(quantity);
           return pendingInventoryMapper.updateById(pendingInventory);
        } else {
            OfficialInventory officialInventory = officialInventoryMapper.getOfficialInventoryForUpdateById(officialInventoryDto.getId());
            if (officialInventory == null) {
                throw new RuntimeException("库存记录不存在");
            }
            officialInventory.setInventoryQuantity(quantity);
            return officialInventoryMapper.updateById(officialInventory);
        }
    }
}