chenhj
23 小时以前 d9b764868dbfa79aa79d79f676f60a28c4055b06
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
100
package com.ruoyi.business.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
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.InputInventoryRecord;
import com.ruoyi.business.entity.InventorySummary;
import com.ruoyi.business.mapper.InputInventoryRecordMapper;
import com.ruoyi.business.mapper.InventorySummaryMapper;
import com.ruoyi.business.service.InputInventoryRecordService;
import com.ruoyi.business.service.InventorySummaryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
 
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 InputInventoryRecordServiceImpl extends ServiceImpl<InputInventoryRecordMapper, InputInventoryRecord> implements InputInventoryRecordService {
    private final InputInventoryRecordMapper inputInventoryRecordMapper;
    private final InventorySummaryService inventorySummaryService;
    private final InventorySummaryMapper inventorySummaryMapper;
 
    @Override
    public int insertInputInventoryRecord(PendingInventoryDto pendingInventoryDto, OfficialInventoryDto officialInventoryDto, BigDecimal quantity) {
        if ((pendingInventoryDto != null && officialInventoryDto != null) || (pendingInventoryDto == null && officialInventoryDto == null)) {
            throw new RuntimeException("库存记录异常");
        }
 
        InputInventoryRecord inputInventoryRecord = new InputInventoryRecord();
        if (officialInventoryDto != null) {
            inputInventoryRecord.setInventoryType(OFFICIAL_INVENTORY);
            inputInventoryRecord.setId(officialInventoryDto.getId());
        } else {
            inputInventoryRecord.setInventoryType(PENDING_INVENTORY);
            inputInventoryRecord.setId(pendingInventoryDto.getId());
        }
        inputInventoryRecord.setQuantity(quantity);
 
        inputInventoryRecordMapper.insert(inputInventoryRecord);
 
        // 更新库存
        return inventorySummaryService.updateInventory(pendingInventoryDto, officialInventoryDto);
    }
 
    @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);
            }
        }
        // todo 重新计算节点库存
        // todo 更新库存实施数据
 
 
        // 删除入库记录
        return inputInventoryRecordMapper.deleteByIds(ids);
    }
}