gongchunyi
4 天以前 c885344040aa322a1476a8720ca5751b9288c263
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package com.ruoyi.basic.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.basic.dto.SupplierManageDto;
import com.ruoyi.basic.excel.SupplierManageExcelDto;
import com.ruoyi.basic.mapper.SupplierManageMapper;
import com.ruoyi.basic.pojo.SupplierManage;
import com.ruoyi.basic.service.ISupplierService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.purchase.mapper.PurchaseLedgerMapper;
import com.ruoyi.purchase.pojo.PurchaseLedger;
import com.ruoyi.purchase.vo.SupplierTransactionsDetailsVo;
import com.ruoyi.purchase.vo.SupplierTransactionsVo;
import com.ruoyi.quality.mapper.QualityInspectMapper;
import com.ruoyi.quality.mapper.QualityUnqualifiedMapper;
import com.ruoyi.quality.pojo.QualityInspect;
import com.ruoyi.quality.pojo.QualityUnqualified;
import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
import com.ruoyi.sales.pojo.SalesLedgerProduct;
import com.ruoyi.stock.mapper.StockInRecordMapper;
import com.ruoyi.stock.pojo.StockInRecord;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
 
@Service
@RequiredArgsConstructor
public class SupplierServiceImpl extends ServiceImpl<SupplierManageMapper, SupplierManage> implements ISupplierService {
 
    private final SupplierManageMapper supplierMapper;
    private final PurchaseLedgerMapper purchaseLedgerMapper;
    private final SalesLedgerProductMapper salesLedgerProductMapper;
    private final StockInRecordMapper stockInRecordMapper;
    private final QualityInspectMapper qualityInspectMapper;
    private final QualityUnqualifiedMapper qualityUnqualifiedMapper;
 
    /**
     * 供应商新增
     *
     * @param supplierManage
     * @return
     */
    @Override
    public void saveSupplier(SupplierManage supplierManage) {
        LambdaQueryWrapper<SupplierManage> supplierManageLambdaQueryWrapper = new LambdaQueryWrapper<>();
        supplierManageLambdaQueryWrapper.eq(SupplierManage::getSupplierName, supplierManage.getSupplierName());
        if (supplierMapper.selectCount(supplierManageLambdaQueryWrapper) > 0) {
            throw new RuntimeException("供应商已存在");
        }
        supplierMapper.insert(supplierManage);
    }
 
    /**
     * 供应商删除
     *
     * @param ids
     * @return
     */
    @Override
    public int delSupplier(List<Integer> ids) {
        List<PurchaseLedger> purchaseLedgers = purchaseLedgerMapper.selectList(new QueryWrapper<PurchaseLedger>().lambda().in(PurchaseLedger::getSupplierId, ids));
        if (purchaseLedgers.size() > 0) {
            throw new RuntimeException("供应商已存在采购合同");
        }
        LambdaQueryWrapper<SupplierManage> delWrapper = new LambdaQueryWrapper<>();
        delWrapper.in(SupplierManage::getId, ids);
 
        return supplierMapper.delete(delWrapper);
    }
 
    /**
     * 供应商详情
     *
     * @param id
     * @return
     */
    @Override
    public SupplierManage supplierDetail(Integer id) {
        return supplierMapper.selectById(id);
    }
 
    /**
     * 供应商修改
     *
     * @param supplierManage
     * @return
     */
    @Override
    public int supplierUpdate(SupplierManage supplierManage) {
        return supplierMapper.updateById(supplierManage);
    }
 
    /**
     * 供应商分页查询
     *
     * @param page
     * @param supplierManageDto
     * @return
     */
    @Override
    public IPage<SupplierManage> supplierListPage(Page page, SupplierManageDto supplierManageDto) {
        return supplierMapper.supplierListPage(page, supplierManageDto);
    }
 
    /**
     * 供应商导出
     *
     * @param response
     * @param supplierManageDto
     */
    @Override
    public void supplierExport(HttpServletResponse response, SupplierManageDto supplierManageDto) {
        List<SupplierManageExcelDto> supplierManageList = supplierMapper.supplierExportList(supplierManageDto);
        ExcelUtil<SupplierManageExcelDto> util = new ExcelUtil<SupplierManageExcelDto>(SupplierManageExcelDto.class);
        util.exportExcel(response, supplierManageList, "供应商导出");
    }
 
    @Override
    public Boolean importData(MultipartFile file) {
 
        try {
            ExcelUtil<SupplierManageExcelDto> util = new ExcelUtil<SupplierManageExcelDto>(SupplierManageExcelDto.class);
            List<SupplierManageExcelDto> list = util.importExcel(file.getInputStream());
            ArrayList<SupplierManage> supplierManages = new ArrayList<>();
            list.forEach(dto -> {
                SupplierManage supplierManage = new SupplierManage();
                BeanUtils.copyProperties(dto, supplierManage);
                supplierManage.setMaintainTime(LocalDate.now());
                Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
 
                supplierManage.setMaintainUserId(Integer.parseInt(userId + ""));
                supplierManages.add(supplierManage);
            });
 
            this.saveOrUpdateBatch(supplierManages);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
 
    @Override
    public IPage<SupplierTransactionsVo> supplierTransactions(Page page, String supplierName) {
        IPage<SupplierTransactionsVo> plPage = supplierMapper.supplierTransactions(page, supplierName);
        List<SupplierTransactionsVo> resultList = plPage.getRecords();
        if (resultList.isEmpty()) {
            return plPage;
        }
 
        List<Long> supplierIds = resultList.stream().map(SupplierTransactionsVo::getSupplierId).collect(Collectors.toList());
 
        List<PurchaseLedger> purchaseLedgers = purchaseLedgerMapper.selectList(new LambdaQueryWrapper<PurchaseLedger>()
                .in(PurchaseLedger::getSupplierId, supplierIds));
 
        if (purchaseLedgers.isEmpty()) {
            for (SupplierTransactionsVo vo : resultList) {
                vo.setShippedAmount(BigDecimal.ZERO);
                BigDecimal contractAmount = vo.getContractAmounts() == null ? BigDecimal.ZERO : vo.getContractAmounts();
                vo.setUnshippedAmount(contractAmount);
            }
            return plPage;
        }
 
        List<Long> ledgerIds = purchaseLedgers.stream().map(PurchaseLedger::getId).collect(Collectors.toList());
 
        List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectList(new LambdaQueryWrapper<SalesLedgerProduct>()
                .in(SalesLedgerProduct::getSalesLedgerId, ledgerIds)
                .eq(SalesLedgerProduct::getType, 2));
 
        //  不质检直接入库的 - 7
        List<StockInRecord> directInboundRecords = stockInRecordMapper.selectList(new LambdaQueryWrapper<StockInRecord>()
                .eq(StockInRecord::getRecordType, "7")
                .eq(StockInRecord::getApprovalStatus, 1)
                .in(StockInRecord::getRecordId, ledgerIds));
 
        //  通过质检入库的
        List<QualityInspect> qualityInspects = qualityInspectMapper.selectList(new LambdaQueryWrapper<QualityInspect>()
                .in(QualityInspect::getPurchaseLedgerId, ledgerIds));
        List<Long> inspectIds = qualityInspects.stream().map(QualityInspect::getId).collect(Collectors.toList());
 
        //  质检合格入库 - 10
        List<StockInRecord> qualifiedInboundRecords = new ArrayList<>();
        List<QualityUnqualified> qualityUnqualifieds = new ArrayList<>();
        if (!inspectIds.isEmpty()) {
            qualifiedInboundRecords = stockInRecordMapper.selectList(new LambdaQueryWrapper<StockInRecord>()
                    .eq(StockInRecord::getRecordType, "10")
                    .eq(StockInRecord::getApprovalStatus, 1)
                    .in(StockInRecord::getRecordId, inspectIds));
 
            qualityUnqualifieds = qualityUnqualifiedMapper.selectList(new LambdaQueryWrapper<QualityUnqualified>()
                    .in(QualityUnqualified::getInspectId, inspectIds));
        }
        List<Long> unqualifiedIds = qualityUnqualifieds.stream().map(QualityUnqualified::getId).collect(Collectors.toList());
 
        //  质检不合格入库 - 11
        List<StockInRecord> unqualifiedInboundRecords = new ArrayList<>();
        if (!unqualifiedIds.isEmpty()) {
            unqualifiedInboundRecords = stockInRecordMapper.selectList(new LambdaQueryWrapper<StockInRecord>()
                    .eq(StockInRecord::getRecordType, "11")
                    .eq(StockInRecord::getApprovalStatus, 1)
                    .in(StockInRecord::getRecordId, unqualifiedIds));
        }
 
        for (SupplierTransactionsVo vo : resultList) {
            BigDecimal shippedAmount = BigDecimal.ZERO;
 
            List<PurchaseLedger> currentLedgers = purchaseLedgers.stream().filter(pl -> pl.getSupplierId().equals(vo.getSupplierId())).toList();
            for (PurchaseLedger ledger : currentLedgers) {
                Long ledgerId = ledger.getId();
 
                List<StockInRecord> currentDirectRecords = directInboundRecords.stream().filter(s -> s.getRecordId().equals(ledgerId)).toList();
                for (StockInRecord sir : currentDirectRecords) {
                    List<SalesLedgerProduct> currentProducts = salesLedgerProducts.stream().filter(s -> s.getSalesLedgerId().equals(ledgerId)).toList();
                    for (SalesLedgerProduct slp : currentProducts) {
                        if (sir.getStockInNum() != null && slp.getTaxInclusiveUnitPrice() != null) {
                            shippedAmount = shippedAmount.add(sir.getStockInNum().multiply(slp.getTaxInclusiveUnitPrice()));
                        }
                    }
                }
 
                List<QualityInspect> currentInspects = qualityInspects.stream().filter(q -> q.getPurchaseLedgerId().equals(ledgerId)).toList();
                for (QualityInspect inspect : currentInspects) {
                    List<StockInRecord> currentQualifiedRecords = qualifiedInboundRecords.stream().filter(s -> s.getRecordId().equals(inspect.getId())).toList();
                    for (StockInRecord sir : currentQualifiedRecords) {
                        BigDecimal minPrice = getMinTaxInclusiveUnitPrice(salesLedgerProducts, ledgerId, sir.getProductModelId());
                        if (sir.getStockInNum() != null && minPrice != null) {
                            shippedAmount = shippedAmount.add(sir.getStockInNum().multiply(minPrice));
                        }
                    }
 
                    List<QualityUnqualified> currentUnqualifieds = qualityUnqualifieds.stream().filter(q -> q.getInspectId().equals(inspect.getId())).toList();
                    for (QualityUnqualified unqualified : currentUnqualifieds) {
                        List<StockInRecord> currentUnqualifiedRecords = unqualifiedInboundRecords.stream().filter(s -> s.getRecordId().equals(unqualified.getId())).toList();
                        for (StockInRecord sir : currentUnqualifiedRecords) {
                            BigDecimal minPrice = getMinTaxInclusiveUnitPrice(salesLedgerProducts, ledgerId, sir.getProductModelId());
                            if (sir.getStockInNum() != null && minPrice != null) {
                                shippedAmount = shippedAmount.add(sir.getStockInNum().multiply(minPrice));
                            }
                        }
                    }
                }
            }
 
            BigDecimal contractAmount = vo.getContractAmounts() == null ? BigDecimal.ZERO : vo.getContractAmounts();
            if (shippedAmount.subtract(contractAmount).compareTo(BigDecimal.ZERO) > 0) {
                shippedAmount = contractAmount;
            }
            vo.setShippedAmount(shippedAmount);
            vo.setUnshippedAmount(contractAmount.subtract(shippedAmount));
        }
 
        return plPage;
    }
 
    @Override
    public IPage<SupplierTransactionsDetailsVo> supplierTransactionsDetails(Page page, Long supplierId) {
        IPage<SupplierTransactionsDetailsVo> plPage = supplierMapper.supplierTransactionsDetails(page, supplierId);
 
        List<SupplierTransactionsDetailsVo> resultList = plPage.getRecords();
        if (resultList.isEmpty()) {
            return plPage;
        }
 
        List<Long> ledgerIds = resultList.stream().map(SupplierTransactionsDetailsVo::getPurchaseLedgerId).collect(Collectors.toList());
 
        List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectList(new LambdaQueryWrapper<SalesLedgerProduct>()
                .in(SalesLedgerProduct::getSalesLedgerId, ledgerIds)
                .eq(SalesLedgerProduct::getType, 2));
 
        //  不质检直接入库的 - 7
        List<StockInRecord> directInboundRecords = stockInRecordMapper.selectList(new LambdaQueryWrapper<StockInRecord>()
                .eq(StockInRecord::getRecordType, "7")
                .eq(StockInRecord::getApprovalStatus, 1)
                .in(StockInRecord::getRecordId, ledgerIds));
 
        //  通过质检入库的
        List<QualityInspect> qualityInspects = qualityInspectMapper.selectList(new LambdaQueryWrapper<QualityInspect>()
                .in(QualityInspect::getPurchaseLedgerId, ledgerIds));
        List<Long> inspectIds = qualityInspects.stream().map(QualityInspect::getId).collect(Collectors.toList());
 
        //  质检合格入库 - 10
        List<StockInRecord> qualifiedInboundRecords = new ArrayList<>();
        List<QualityUnqualified> qualityUnqualifieds = new ArrayList<>();
        if (!inspectIds.isEmpty()) {
            qualifiedInboundRecords = stockInRecordMapper.selectList(new LambdaQueryWrapper<StockInRecord>()
                    .eq(StockInRecord::getRecordType, "10")
                    .eq(StockInRecord::getApprovalStatus, 1)
                    .in(StockInRecord::getRecordId, inspectIds));
 
            qualityUnqualifieds = qualityUnqualifiedMapper.selectList(new LambdaQueryWrapper<QualityUnqualified>()
                    .in(QualityUnqualified::getInspectId, inspectIds));
        }
        List<Long> unqualifiedIds = qualityUnqualifieds.stream().map(QualityUnqualified::getId).collect(Collectors.toList());
 
        //  质检不合格入库 - 11
        List<StockInRecord> unqualifiedInboundRecords = new ArrayList<>();
        if (!unqualifiedIds.isEmpty()) {
            unqualifiedInboundRecords = stockInRecordMapper.selectList(new LambdaQueryWrapper<StockInRecord>()
                    .eq(StockInRecord::getRecordType, "11")
                    .eq(StockInRecord::getApprovalStatus, 1)
                    .in(StockInRecord::getRecordId, unqualifiedIds));
        }
 
        for (SupplierTransactionsDetailsVo vo : resultList) {
            BigDecimal shippedAmount = BigDecimal.ZERO;
 
            List<StockInRecord> currentDirectRecords = directInboundRecords.stream().filter(s -> s.getRecordId().equals(vo.getPurchaseLedgerId())).toList();
            for (StockInRecord sir : currentDirectRecords) {
                List<SalesLedgerProduct> currentProducts = salesLedgerProducts.stream().filter(s -> s.getSalesLedgerId().equals(vo.getPurchaseLedgerId())).toList();
                for (SalesLedgerProduct slp : currentProducts) {
                    if (sir.getStockInNum() != null && slp.getTaxInclusiveUnitPrice() != null) {
                        shippedAmount = shippedAmount.add(sir.getStockInNum().multiply(slp.getTaxInclusiveUnitPrice()));
                    }
                }
            }
 
            List<QualityInspect> currentInspects = qualityInspects.stream().filter(q -> q.getPurchaseLedgerId().equals(vo.getPurchaseLedgerId())).toList();
            for (QualityInspect inspect : currentInspects) {
                List<StockInRecord> currentQualifiedRecords = qualifiedInboundRecords.stream().filter(s -> s.getRecordId().equals(inspect.getId())).toList();
                for (StockInRecord sir : currentQualifiedRecords) {
                    BigDecimal minPrice = getMinTaxInclusiveUnitPrice(salesLedgerProducts, vo.getPurchaseLedgerId(), sir.getProductModelId());
                    if (sir.getStockInNum() != null && minPrice != null) {
                        shippedAmount = shippedAmount.add(sir.getStockInNum().multiply(minPrice));
                    }
                }
 
                List<QualityUnqualified> currentUnqualifieds = qualityUnqualifieds.stream().filter(q -> q.getInspectId().equals(inspect.getId())).toList();
                for (QualityUnqualified unqualified : currentUnqualifieds) {
                    List<StockInRecord> currentUnqualifiedRecords = unqualifiedInboundRecords.stream().filter(s -> s.getRecordId().equals(unqualified.getId())).toList();
                    for (StockInRecord sir : currentUnqualifiedRecords) {
                        BigDecimal minPrice = getMinTaxInclusiveUnitPrice(salesLedgerProducts, vo.getPurchaseLedgerId(), sir.getProductModelId());
                        if (sir.getStockInNum() != null && minPrice != null) {
                            shippedAmount = shippedAmount.add(sir.getStockInNum().multiply(minPrice));
                        }
                    }
                }
            }
 
            BigDecimal contractAmount = vo.getContractAmount() == null ? BigDecimal.ZERO : vo.getContractAmount();
            if (shippedAmount.subtract(contractAmount).compareTo(BigDecimal.ZERO) > 0) {
                shippedAmount = contractAmount;
            }
            vo.setShippedAmount(shippedAmount);
            vo.setUnshippedAmount(contractAmount.subtract(shippedAmount));
        }
 
        return plPage;
    }
 
    private BigDecimal getMinTaxInclusiveUnitPrice(List<SalesLedgerProduct> allSlp, Long salesLedgerId, Long productModelId) {
        return allSlp.stream()
                .filter(s -> s.getSalesLedgerId().equals(salesLedgerId) && s.getProductModelId() != null && s.getProductModelId().equals(productModelId))
                .map(SalesLedgerProduct::getTaxInclusiveUnitPrice)
                .filter(java.util.Objects::nonNull)
                .min(BigDecimal::compareTo)
                .orElse(null);
    }
}