gongchunyi
10 小时以前 6b4cfc6f9d660b92be99ba4e3411a3267bc57155
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package com.ruoyi.procurementrecord.utils;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.procurementrecord.mapper.ProcurementRecordMapper;
import com.ruoyi.procurementrecord.mapper.ProcurementRecordOutMapper;
import com.ruoyi.stock.dto.StockInRecordDto;
import com.ruoyi.stock.dto.StockInventoryDto;
import com.ruoyi.stock.dto.StockUninventoryDto;
import com.ruoyi.stock.pojo.StockInRecord;
import com.ruoyi.stock.pojo.StockInventory;
import com.ruoyi.stock.pojo.StockOutRecord;
import com.ruoyi.stock.pojo.StockUninventory;
import com.ruoyi.stock.service.StockInRecordService;
import com.ruoyi.stock.service.StockInventoryService;
import com.ruoyi.stock.service.StockOutRecordService;
import com.ruoyi.stock.service.StockUninventoryService;
import com.ruoyi.stock.service.impl.StockInRecordServiceImpl;
import com.ruoyi.stock.service.impl.StockOutRecordServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
 
import java.math.BigDecimal;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
 
@Component
@RequiredArgsConstructor
public class StockUtils {
    private final ProcurementRecordOutMapper procurementRecordOutMapper;
    private final ProcurementRecordMapper procurementRecordMapper;
    private final StockUninventoryService stockUninventoryService;
    private final StockInventoryService stockInventoryService;
    private final StockInRecordService stockInRecordService;
    private final StockOutRecordService stockOutRecordService;
 
    /**
     * 合格库存出库前校验:按 stock_inventory 可用数量(数量 − 冻结)
     */
    public void assertQualifiedAvailable(Long productModelId, BigDecimal outboundQty) {
        if (productModelId == null) {
            throw new ServiceException("出库失败,产品规格未维护");
        }
        if (outboundQty == null || outboundQty.compareTo(BigDecimal.ZERO) <= 0) {
            return;
        }
        StockInventory inv = stockInventoryService.getOne(
                Wrappers.<StockInventory>lambdaQuery().eq(StockInventory::getProductModelId, productModelId));
        if (inv == null) {
            throw new ServiceException("出库失败,合格库中无该产品库存");
        }
        BigDecimal locked = inv.getLockedQuantity() == null ? BigDecimal.ZERO : inv.getLockedQuantity();
        BigDecimal qty = inv.getQualitity() == null ? BigDecimal.ZERO : inv.getQualitity();
        BigDecimal available = qty.subtract(locked);
        if (outboundQty.compareTo(available) > 0) {
            throw new ServiceException("出库失败,出库数量不能大于当前合格库存可用数量");
        }
    }
 
    /**
     * 不合格库存出库前校验:按 stock_uninventory 可用数量(数量 − 冻结)
     */
    public void assertUnqualifiedAvailable(Long productModelId, BigDecimal outboundQty) {
        if (productModelId == null) {
            throw new ServiceException("出库失败,产品规格未维护");
        }
        if (outboundQty == null || outboundQty.compareTo(BigDecimal.ZERO) <= 0) {
            return;
        }
        StockUninventory inv = stockUninventoryService.getOne(
                Wrappers.<StockUninventory>lambdaQuery().eq(StockUninventory::getProductModelId, productModelId));
        if (inv == null) {
            throw new ServiceException("出库失败,不合格库中无该产品库存");
        }
        BigDecimal locked = inv.getLockedQuantity() == null ? BigDecimal.ZERO : inv.getLockedQuantity();
        BigDecimal qty = inv.getQualitity() == null ? BigDecimal.ZERO : inv.getQualitity();
        BigDecimal available = qty.subtract(locked);
        if (outboundQty.compareTo(available) > 0) {
            throw new ServiceException("出库失败,出库数量不能大于当前不合格库存可用数量");
        }
    }
 
    /**
     * 不合格入库
     *
     * @param productModelId
     * @param quantity
     * @param recordType
     * @param recordId
     */
    public void addUnStock(Long productModelId, BigDecimal quantity, String recordType, Long recordId) {
        addUnStock(null, null, productModelId, quantity, recordType, recordId);
    }
 
    /**
     * 不合格入库(带销售订单关联,写入入库记录)
     */
    public void addUnStock(Long salesLedgerId, Long salesLedgerProductId, Long productModelId, BigDecimal quantity, String recordType, Long recordId) {
        StockUninventoryDto stockUninventoryDto = new StockUninventoryDto();
        stockUninventoryDto.setRecordId(recordId);
        stockUninventoryDto.setRecordType(String.valueOf(recordType));
        stockUninventoryDto.setQualitity(quantity);
        stockUninventoryDto.setProductModelId(productModelId);
        stockUninventoryDto.setSalesLedgerId(salesLedgerId);
        stockUninventoryDto.setSalesLedgerProductId(salesLedgerProductId);
        stockUninventoryService.addStockUninventory(stockUninventoryDto);
    }
 
    /**
     * 不合格出库
     */
    public void subtractUnStock(Long productModelId, BigDecimal quantity, String recordType, Long recordId) {
        subtractUnStock(null, null, productModelId, quantity, recordType, recordId);
    }
 
    /**
     * 不合格出库(可带销售订单关联写入出库记录)
     */
    public void subtractUnStock(Long salesLedgerId, Long salesLedgerProductId, Long productModelId, BigDecimal quantity, String recordType, Long recordId) {
        StockUninventoryDto stockUninventoryDto = new StockUninventoryDto();
        stockUninventoryDto.setRecordId(recordId);
        stockUninventoryDto.setRecordType(recordType);
        stockUninventoryDto.setQualitity(quantity);
        stockUninventoryDto.setProductModelId(productModelId);
        stockUninventoryDto.setSalesLedgerId(salesLedgerId);
        stockUninventoryDto.setSalesLedgerProductId(salesLedgerProductId);
        stockUninventoryService.subtractStockUninventory(stockUninventoryDto);
    }
 
    /**
     * 合格入库
     *
     * @param productModelId
     * @param quantity
     * @param recordType
     * @param recordId
     */
    public void addStock(Long productModelId, BigDecimal quantity, String recordType, Long recordId) {
        addStock(null, null, productModelId, quantity, recordType, recordId);
    }
 
    /**
     * 合格入库
     *
     * @param productModelId
     * @param quantity
     * @param recordType
     * @param recordId
     */
    public void addStock(Long salesLedgerId, Long salesLedgerProductId, Long productModelId, BigDecimal quantity, String recordType, Long recordId) {
        StockInventoryDto stockInventoryDto = new StockInventoryDto();
        stockInventoryDto.setRecordId(recordId);
        stockInventoryDto.setRecordType(String.valueOf(recordType));
        stockInventoryDto.setQualitity(quantity);
        stockInventoryDto.setProductModelId(productModelId);
        stockInventoryDto.setSalesLedgerId(salesLedgerId);
        stockInventoryDto.setSalesLedgerProductId(salesLedgerProductId);
        stockInventoryService.addstockInventory(stockInventoryDto);
    }
 
 
    /**
     * 合格出库
     *
     * @param productModelId
     * @param quantity
     * @param recordType
     * @param recordId
     */
    public void substractStock(Long productModelId, BigDecimal quantity, String recordType, Long recordId) {
        StockInventoryDto stockInventoryDto = new StockInventoryDto();
        stockInventoryDto.setRecordId(recordId);
        stockInventoryDto.setRecordType(String.valueOf(recordType));
        stockInventoryDto.setQualitity(quantity);
        stockInventoryDto.setProductModelId(productModelId);
        stockInventoryService.subtractStockInventory(stockInventoryDto);
    }
 
    /**
     * 合格出库
     *
     * @param productModelId
     * @param quantity
     * @param recordType
     * @param recordId
     */
    public void substractStock(Long salesId, Long salseProductId, Long productModelId, BigDecimal quantity, String recordType, Long recordId) {
        StockInventoryDto stockInventoryDto = new StockInventoryDto();
        stockInventoryDto.setRecordId(recordId);
        stockInventoryDto.setRecordType(String.valueOf(recordType));
        stockInventoryDto.setQualitity(quantity);
        stockInventoryDto.setProductModelId(productModelId);
        stockInventoryDto.setSalesLedgerId(salesId);
        stockInventoryDto.setSalesLedgerProductId(salseProductId);
        stockInventoryService.subtractStockInventory(stockInventoryDto);
    }
 
    //不合格库存删除
    public void deleteStockInRecord(Long recordId, String recordType) {
        StockInRecord one = stockInRecordService.getOne(new QueryWrapper<StockInRecord>()
                .lambda().eq(StockInRecord::getRecordId, recordId)
                .eq(StockInRecord::getRecordType, recordType));
        if (ObjectUtils.isNotEmpty(one)) {
            stockInRecordService.batchDelete(Collections.singletonList(one.getId()));
        }
    }
 
    public void deleteStockOutRecord(Long recordId, String recordType) {
        StockOutRecord one = stockOutRecordService.getOne(new QueryWrapper<StockOutRecord>()
                .lambda().eq(StockOutRecord::getRecordId, recordId)
                .eq(StockOutRecord::getRecordType, recordType));
        if (ObjectUtils.isNotEmpty(one)) {
            stockOutRecordService.batchDelete(Collections.singletonList(one.getId()));
        }
    }
}