liujie
2025-10-13 1ae3b6300eb40a8b0683add19dd49590de4bd4d9
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
package com.ruoyi.system.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.core.domain.BaseModel;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.dto.AddProcurementDto;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.model.*;
import com.ruoyi.system.query.TErpGoodsQuery;
import com.ruoyi.system.query.TErpProcurementQuery;
import com.ruoyi.system.service.TErpGoodsService;
import com.ruoyi.system.service.TErpProcurementGoodsService;
import com.ruoyi.system.service.TErpProcurementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.vo.*;
import lombok.SneakyThrows;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * <p>
 * erp采购 服务实现类
 * </p>
 *
 * @author xiaochen
 * @since 2025-08-20
 */
@Service
public class TErpProcurementServiceImpl extends ServiceImpl<TErpProcurementMapper, TErpProcurement> implements TErpProcurementService {
 
    @Resource
    private TErpProcurementGoodsMapper erpProcurementGoodsMapper;
 
    @Resource
    private TSysCommissionMapper sysCommissionMapper;
 
    @Resource
    private TCrmClinicMapper crmClinicMapper;
 
    @Resource
    private TCrmSupplierMapper crmSupplierMapper;
 
    @Resource
    private SysUserMapper sysUserMapper;
 
    @Resource
    private TErpGoodsMapper erpGoodsMapper;
 
    @Resource
    private TErpGoodsTypeMapper erpGoodsTypeMapper;
 
    @Resource
    private TErpGoodsUnitMapper erpGoodsUnitMapper;
 
    @Autowired
    private TErpProcurementGoodsService erpProcurementGoodsService;
 
    @Resource
    private TErpSupplierWarehousingMapper erpSupplierWarehousingMapper;
 
    @Resource
    private TErpSupplierWarehousingBatchMapper erpSupplierWarehousingBatchMapper;
 
    @Resource
    private TErpSupplierOutboundMapper erpSupplierOutboundMapper;
 
    @Resource
    private TErpSupplierOutboundGoodsMapper erpSupplierOutboundGoodsMapper;
 
 
    @Override
    public PageInfo<TErpProcurementVo> pageList(TErpProcurementQuery query, SysUser user) {
 
 
        Integer roleType = user.getRoleType();
        String supplierClinicId = null;
        if (roleType == 4) {
            // 供应商
            TCrmSupplier crmSupplier = crmSupplierMapper.selectOne(Wrappers.lambdaQuery(TCrmSupplier.class)
                    .eq(TCrmSupplier::getUserId, user.getUserId())
                    .last("LIMIT 1"));
            supplierClinicId = crmSupplier.getId();
        }
        if (roleType == 5) {
            // 诊所
            TCrmClinic crmClinic = crmClinicMapper.selectOne(Wrappers.lambdaQuery(TCrmClinic.class)
                    .eq(TCrmClinic::getUserId, user.getUserId())
                    .last("LIMIT 1"));
            supplierClinicId = crmClinic.getId();
        }
 
        String sTime = null;
        String eTime = null;
        if (query.getTime() != null && !query.getTime().isEmpty()) {
            String[] split = query.getTime().split(" - ");
            sTime = split[0] + " 00:00:00";
            eTime = split[1] + " 23:59:59";
        }
        PageInfo<TErpProcurementVo> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        List<TErpProcurementVo> list = this.baseMapper.pageList(query, pageInfo, user, sTime, eTime, supplierClinicId);
        if (list.isEmpty()) {
            return pageInfo;
        }
        List<String> ids = list.stream().map(TErpProcurementVo::getId).collect(Collectors.toList());
        List<TErpProcurementGoods> tErpProcurementGoods = erpProcurementGoodsMapper.selectList
                (new LambdaQueryWrapper<TErpProcurementGoods>().in(TErpProcurementGoods::getProcurementId, ids));
        List<TSysCommission> tSysCommissions = new ArrayList<>();
        if (!tErpProcurementGoods.isEmpty()) {
            List<String> collect = tErpProcurementGoods.stream().map(TErpProcurementGoods::getId).collect(Collectors.toList());
            tSysCommissions = sysCommissionMapper.selectList(new LambdaQueryWrapper<TSysCommission>().in(TSysCommission::getTErpProcurementGoodsId, collect));
        }
        for (TErpProcurementVo tErpProcurementVo : list) {
            List<TErpProcurementGoods> collect = tErpProcurementGoods.stream().filter(e -> e.getProcurementId().equals(tErpProcurementVo.getId())).collect(Collectors.toList());
            int size = collect.stream().map(TErpProcurementGoods::getGoodsId).collect(Collectors.toSet()).size();
            tErpProcurementVo.setTypeNum(size);
            BigDecimal reduce = collect.stream().map(TErpProcurementGoods::getTotalPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
            tErpProcurementVo.setTotalPrice(reduce);
            if (!collect.isEmpty()) {
                List<String> collect1 = collect.stream().map(TErpProcurementGoods::getId).collect(Collectors.toList());
                List<TSysCommission> collect2 = tSysCommissions.stream().filter(e -> collect1.contains(e.getTErpProcurementGoodsId())).collect(Collectors.toList());
                BigDecimal reduce1 = collect2.stream().filter(e -> e.getType() == 1).map(TSysCommission::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal reduce2 = collect2.stream().filter(e -> e.getType() == 2).map(TSysCommission::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
                tErpProcurementVo.setPlatformMoney(reduce1);
                tErpProcurementVo.setSupplierMoney(reduce2);
            }
 
        }
 
 
        pageInfo.setRecords(list);
        return pageInfo;
    }
 
    @Override
    public PageInfo<TErpProcurementVo> pageListWarehouse(TErpProcurementQuery query, SysUser user) {
 
 
        Integer roleType = user.getRoleType();
        String supplierClinicId = null;
        if (roleType == 6) {
            // 供应商
            TCrmSupplier crmSupplier = crmSupplierMapper.selectOne(Wrappers.lambdaQuery(TCrmSupplier.class)
                    .eq(TCrmSupplier::getUserId, user.getUserId())
                    .last("LIMIT 1"));
            supplierClinicId = crmSupplier.getId();
 
 
            String sTime = null;
            String eTime = null;
            if (query.getTime() != null && !query.getTime().isEmpty()) {
                String[] split = query.getTime().split(" - ");
                sTime = split[0] + " 00:00:00";
                eTime = split[1] + " 23:59:59";
            }
            PageInfo<TErpProcurementVo> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
            List<TErpProcurementVo> list = this.baseMapper.pageListWarehouse(query, pageInfo, user, sTime, eTime, supplierClinicId);
//            if (list.isEmpty()) {
//                return pageInfo;
//            }
//            List<String> ids = list.stream().map(TErpProcurementVo::getId).collect(Collectors.toList());
//            List<TErpProcurementGoods> tErpProcurementGoods = erpProcurementGoodsMapper.selectList
//                    (new LambdaQueryWrapper<TErpProcurementGoods>().in(TErpProcurementGoods::getProcurementId, ids));
//            List<TSysCommission> tSysCommissions = new ArrayList<>();
//            if (!tErpProcurementGoods.isEmpty()) {
//                List<String> collect = tErpProcurementGoods.stream().map(TErpProcurementGoods::getId).collect(Collectors.toList());
//                tSysCommissions = sysCommissionMapper.selectList(new LambdaQueryWrapper<TSysCommission>().in(TSysCommission::getTErpProcurementGoodsId, collect));
//            }
//            for (TErpProcurementVo tErpProcurementVo : list) {
//                List<TErpProcurementGoods> collect = tErpProcurementGoods.stream().filter(e -> e.getProcurementId().equals(tErpProcurementVo.getId())).collect(Collectors.toList());
//                int size = collect.stream().map(TErpProcurementGoods::getGoodsId).collect(Collectors.toSet()).size();
//                tErpProcurementVo.setTypeNum(size);
//                BigDecimal reduce = collect.stream().map(TErpProcurementGoods::getTotalPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
//                tErpProcurementVo.setTotalPrice(reduce);
//                if (!collect.isEmpty()) {
//                    List<String> collect1 = collect.stream().map(TErpProcurementGoods::getId).collect(Collectors.toList());
//                    List<TSysCommission> collect2 = tSysCommissions.stream().filter(e -> collect1.contains(e.getTErpProcurementGoodsId())).collect(Collectors.toList());
//                    BigDecimal reduce1 = collect2.stream().filter(e -> e.getType() == 1).map(TSysCommission::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
//                    BigDecimal reduce2 = collect2.stream().filter(e -> e.getType() == 2).map(TSysCommission::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
//                    tErpProcurementVo.setPlatformMoney(reduce1);
//                    tErpProcurementVo.setSupplierMoney(reduce2);
//                }
//
//            }
 
 
            pageInfo.setRecords(list);
            return pageInfo;
        }
        return null;
    }
 
    @Override
    public TErpProcurementDetailVo detail(String id, SysUser user, String goodsName) {
 
        Integer roleType = user.getRoleType();
        String supplierClinicId = null;
        if (roleType == 4) {
            // 供应商
            TCrmSupplier crmSupplier = crmSupplierMapper.selectOne(Wrappers.lambdaQuery(TCrmSupplier.class)
                    .eq(TCrmSupplier::getUserId, user.getUserId())
                    .last("LIMIT 1"));
            supplierClinicId = crmSupplier.getId();
        }
        if (roleType == 5) {
            // 诊所
            TCrmClinic crmClinic = crmClinicMapper.selectOne(Wrappers.lambdaQuery(TCrmClinic.class)
                    .eq(TCrmClinic::getUserId, user.getUserId())
                    .last("LIMIT 1"));
            supplierClinicId = crmClinic.getId();
        }
 
        TErpProcurementDetailVo tErpProcurementVo = new TErpProcurementDetailVo();
 
        TErpProcurement tErpProcurement = this.getById(id);
        tErpProcurementVo.setId(id);
        tErpProcurementVo.setProcurementCode(tErpProcurement.getProcurementCode());
        tErpProcurementVo.setClinicId(tErpProcurement.getClinicId());
 
        TCrmClinic tCrmClinic = crmClinicMapper.selectById(tErpProcurement.getClinicId());
        tErpProcurementVo.setClinicName(tCrmClinic.getClinicName());
        tErpProcurementVo.setUserName(tCrmClinic.getPersonChargeName());
        SysUser sysUser = sysUserMapper.selectUserById(Long.valueOf(tErpProcurement.getCreateId()));
 
        tErpProcurementVo.setUserName(sysUser.getNickName());
        tErpProcurementVo.setCreateTime(tErpProcurement.getCreateTime());
        tErpProcurementVo.setUpdateTime(tErpProcurement.getUpdateTime());
        if (tErpProcurement.getWarehousingUserId() != null) {
            SysUser sysUser1 = sysUserMapper.selectUserById(Long.valueOf(tErpProcurement.getWarehousingUserId()));
            tErpProcurementVo.setOperName(sysUser1.getNickName());
        }
        tErpProcurementVo.setStatus(tErpProcurement.getStatus());
        tErpProcurementVo.setSendTime(tErpProcurement.getSendTime());
 
        tErpProcurementVo.setMoney(tErpProcurement.getMoney());
        tErpProcurementVo.setTotalPrice(tErpProcurement.getPayMoney());
 
        List<TErpProcurementGoods> tErpProcurementGoods = erpProcurementGoodsMapper.selectList
                (new LambdaQueryWrapper<TErpProcurementGoods>().eq(TErpProcurementGoods::getProcurementId, id)
                        .like(goodsName != null && !goodsName.isEmpty(), TErpProcurementGoods::getGoodsName, goodsName)
                        .eq(TErpProcurementGoods::getSupplierId, supplierClinicId));
        if (tErpProcurementGoods.isEmpty()) {
            return tErpProcurementVo;
        }
 
        ArrayList<TErpProcurementDetailNextVo> tErpProcurementDetailNextVos = new ArrayList<>();
        for (TErpProcurementGoods tErpProcurementGood : tErpProcurementGoods) {
            TErpProcurementDetailNextVo tErpProcurementDetailNextVo = new TErpProcurementDetailNextVo();
            tErpProcurementDetailNextVo.setGoodsName(tErpProcurementGood.getGoodsName());
            tErpProcurementDetailNextVo.setUnitName(tErpProcurementGood.getUnitName());
            tErpProcurementDetailNextVo.setNum(tErpProcurementGood.getPurchaseCount());
            tErpProcurementDetailNextVo.setTotalPrice(tErpProcurementGood.getTotalPrice());
            tErpProcurementDetailNextVo.setQuasiNumber(tErpProcurementGood.getQuasiNumber());
            tErpProcurementDetailNextVo.setPurchasePrice(tErpProcurementGood.getSalesAmount());
 
 
            List<TSysCommission> tSysCommissions = sysCommissionMapper.selectList(new LambdaQueryWrapper<TSysCommission>().eq(TSysCommission::getTErpProcurementGoodsId, tErpProcurementGood.getId()));
            if (!tSysCommissions.isEmpty()) {
                BigDecimal reduce = tSysCommissions.stream().filter(e -> e.getType() == 1).map(TSysCommission::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
                tErpProcurementDetailNextVo.setPlatformMoney(reduce);
                BigDecimal reduce1 = tSysCommissions.stream().filter(e -> e.getType() == 2).map(TSysCommission::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
                tErpProcurementDetailNextVo.setSupplierMoney(reduce1);
            }
            tErpProcurementDetailNextVos.add(tErpProcurementDetailNextVo);
 
        }
        tErpProcurementVo.setList(tErpProcurementDetailNextVos);
        return tErpProcurementVo;
    }
 
    @Override
    public PageInfo<TErpGoods> pageGoodsPageList(TErpGoodsQuery query, SysUser user) {
        PageInfo<TErpGoods> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        PageInfo<TErpGoods> p = erpGoodsMapper.selectPage(pageInfo, new LambdaQueryWrapper<TErpGoods>().eq(TErpGoods::getGoodsSource, 1).isNotNull(TErpGoods::getClinicPurchasePrice).orderByDesc(BaseModel::getCreateTime));
        List<TErpGoods> list = p.getRecords();
 
        List<String> typeIds = list.stream().map(TErpGoods::getTypeId).collect(Collectors.toList());
        if (!typeIds.isEmpty()) {
            List<TErpGoodsType> typeList = erpGoodsTypeMapper.selectBatchIds(typeIds);
            for (TErpGoods tErpGoodsVO : list) {
                typeList.stream().filter(t -> t.getId().equals(tErpGoodsVO.getTypeId())).findFirst().ifPresent(t -> tErpGoodsVO.setTypeName(t.getTypeName()));
                tErpGoodsVO.setTypeName(tErpGoodsVO.getTypeName());
            }
        }
        List<String> supplierClinicId = list.stream().filter(e -> e.getGoodsSource() == 1).map(TErpGoods::getSupplierClinicId).collect(Collectors.toList());
        if (!supplierClinicId.isEmpty()) {
            List<TCrmSupplier> crmClinics = crmSupplierMapper.selectBatchIds(supplierClinicId);
            for (TErpGoods tErpGoodsVO : list) {
                crmClinics.stream().filter(t -> t.getId().equals(tErpGoodsVO.getSupplierClinicId())).findFirst().ifPresent(t -> tErpGoodsVO.setSupplierName(t.getSupplierName()));
            }
        }
        pageInfo.setRecords(list);
        return pageInfo;
    }
 
    @Override
    public List<TErpGoodsVO> inventoryNotEnoughList(SysUser user) {
        List<TErpGoodsVO> list = this.baseMapper.inventoryNotEnoughList(user);
        List<String> typeIds = list.stream().map(TErpGoodsVO::getTypeId).collect(Collectors.toList());
        if (!typeIds.isEmpty()) {
            List<TErpGoodsType> typeList = erpGoodsTypeMapper.selectBatchIds(typeIds);
            for (TErpGoodsVO tErpGoodsVO : list) {
                typeList.stream().filter(t -> t.getId().equals(tErpGoodsVO.getTypeId())).findFirst().ifPresent(t -> tErpGoodsVO.setTypeName(t.getTypeName()));
            }
        }
        List<String> supplierClinicId = list.stream().filter(e -> e.getGoodsSource() == 1).map(TErpGoodsVO::getSupplierClinicId).collect(Collectors.toList());
        if (!supplierClinicId.isEmpty()) {
            List<TCrmSupplier> crmSuppliers = crmSupplierMapper.selectBatchIds(supplierClinicId);
            for (TErpGoodsVO tErpGoodsVO : list) {
                crmSuppliers.stream().filter(t -> t.getId().equals(tErpGoodsVO.getSupplierClinicId())).findFirst().ifPresent(t -> tErpGoodsVO.setSupplierName(t.getSupplierName()));
            }
        }
 
        return list;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void addProcurement(String clinicSupplierId, SysUser user, List<AddProcurementDto> dtos) {
        List<String> goodsIds = dtos.stream().map(AddProcurementDto::getGoodsId).collect(Collectors.toList());
        List<TErpGoods> tErpGoods = erpGoodsMapper.selectBatchIds(goodsIds);
        for (AddProcurementDto dto : dtos) {
            TErpGoods tErpGoodsVO = tErpGoods.stream().filter(t -> t.getId().equals(dto.getGoodsId())).findFirst().get();
            dto.setPlatformCommissionPrice(tErpGoodsVO.getPlatformCommissionPrice());
            dto.setSupplierId(tErpGoodsVO.getSupplierClinicId());
            dto.setGoodsSalesAmount(tErpGoodsVO.getClinicPurchasePrice());
            if (dto.getSalesAmount() == null) {
                dto.setSalesAmount(tErpGoodsVO.getClinicPurchasePrice());
            }
        }
        // 供应商分组
        Map<String, List<AddProcurementDto>> supplierClinicIdListMap = dtos.stream().collect(Collectors.groupingBy(AddProcurementDto::getSupplierId));
        for (Map.Entry<String, List<AddProcurementDto>> entry : supplierClinicIdListMap.entrySet()) {
            String supplierClinicId = entry.getKey();
            TCrmSupplier supplier = crmSupplierMapper.selectById(supplierClinicId);
            List<AddProcurementDto> value = entry.getValue();
            TErpProcurement tErpProcurement = new TErpProcurement();
            tErpProcurement.setClinicId(clinicSupplierId);
            tErpProcurement.setProcurementCode("C" + DateUtils.dateTimeNow());
            tErpProcurement.setSupplierId(supplierClinicId);
 
            tErpProcurement.setTermNo(supplier.getTermNo());
            tErpProcurement.setMerchantNo(supplier.getRecvMerchantNo());
            tErpProcurement.setCreateId(user.getUserId().toString());
 
            BigDecimal all = BigDecimal.ZERO;
 
            for (AddProcurementDto addProcurementDto : value) {
                BigDecimal multiply = addProcurementDto.getSalesAmount().multiply(BigDecimal.valueOf(addProcurementDto.getPurchaseCount()));
                all = all.add(multiply);
            }
            tErpProcurement.setPayMoney(all);
            AddProcurementDto addProcurementDto = value.get(0);
            if (addProcurementDto.getState() != null && addProcurementDto.getState() == 1) {
                tErpProcurement.setStatus(1);
            } else {
                tErpProcurement.setStatus(2);
            }
            ArrayList<TErpProcurementGoods> tErpProcurementGoods1 = new ArrayList<>();
 
            BigDecimal add = BigDecimal.ZERO;
 
 
            BigDecimal supplierCommissionPrice = BigDecimal.ZERO;
 
            for (AddProcurementDto dto : value) {
                TErpProcurementGoods tErpProcurementGoods = new TErpProcurementGoods();
                tErpProcurementGoods.setProcurementId(tErpProcurement.getId());
                TErpGoods goods = tErpGoods.stream().filter(e -> e.getId().equals(dto.getGoodsId())).findFirst().get();
                tErpProcurementGoods.setGoodsId(dto.getGoodsId());
                tErpProcurementGoods.setGoodsName(goods.getGoodsName());
                tErpProcurementGoods.setSupplierName(supplier.getSupplierName());
                tErpProcurementGoods.setQuasiNumber(goods.getQuasiNumber());
 
                TErpGoodsUnit tErpGoodsUnit = erpGoodsUnitMapper.selectById(goods.getPackingUnitId());
                tErpProcurementGoods.setUnitName(tErpGoodsUnit.getUnitName());
                tErpProcurementGoods.setPurchasePrice(dto.getGoodsSalesAmount());
                tErpProcurementGoods.setSalesAmount(dto.getSalesAmount());
                tErpProcurementGoods.setTotalPrice(dto.getGoodsSalesAmount().multiply(new BigDecimal(dto.getPurchaseCount())));
 
                tErpProcurementGoods.setPurchaseCount(dto.getPurchaseCount());
                tErpProcurementGoods.setSupplierId(supplierClinicId);
                tErpProcurementGoods.setRecvMerchantNo(supplier.getRecvMerchantNo());
                tErpProcurementGoods.setSupplierMoney(dto.getSalesAmount().subtract(dto.getPlatformCommissionPrice()));
                supplierCommissionPrice = supplierCommissionPrice.add(tErpProcurementGoods.getSupplierMoney().multiply(BigDecimal.valueOf(dto.getPurchaseCount())));
                if (tErpProcurementGoods.getSupplierMoney().doubleValue() < 0) {
                    throw new RuntimeException("平台抽成不能大于售卖价格");
                }
                add = add.add(dto.getPlatformCommissionPrice().multiply(BigDecimal.valueOf(dto.getPurchaseCount())));
                tErpProcurementGoods1.add(tErpProcurementGoods);
            }
            BigDecimal bigDecimal = tErpProcurement.getPayMoney().multiply(BigDecimal.valueOf(0.0038)).setScale(2, RoundingMode.HALF_UP);
 
            tErpProcurement.setMoney(add.subtract(bigDecimal));
            if (tErpProcurement.getMoney().doubleValue() < 0) {
                throw new RuntimeException("金额设置错误,请联系平台管理员");
            }
            tErpProcurement.sets
            this.save(tErpProcurement);
            tErpProcurementGoods1.forEach(e -> e.setProcurementId(tErpProcurement.getId()));
            erpProcurementGoodsService.saveBatch(tErpProcurementGoods1);
        }
 
    }
 
    @Override
    public DetailProcurementVO detailProcurement(String clinicSupplierId, SysUser user, String id) {
        DetailProcurementVO detailProcurementVO = new DetailProcurementVO();
        TErpProcurement tErpProcurement = this.getById(id);
        detailProcurementVO.setStatus(tErpProcurement.getStatus());
        detailProcurementVO.setCreateTime(tErpProcurement.getCreateTime());
        detailProcurementVO.setSendTime(tErpProcurement.getSendTime());
        TCrmSupplier supplier = crmSupplierMapper.selectById(tErpProcurement.getSupplierId());
        detailProcurementVO.setSupplierName(supplier.getSupplierName());
        detailProcurementVO.setUserName(sysUserMapper.selectUserById(Long.valueOf(tErpProcurement.getCreateId())).getUserName());
        TCrmClinic tCrmClinic = crmClinicMapper.selectById(tErpProcurement.getClinicId());
        detailProcurementVO.setClinicName(tCrmClinic.getClinicName());
        detailProcurementVO.setClinicAddress(tCrmClinic.getClinicAddress());
        detailProcurementVO.setLogisticsNumber(tErpProcurement.getLogisticsNumber());
        List<TErpProcurementGoods> list = erpProcurementGoodsService.list(new QueryWrapper<TErpProcurementGoods>().eq("procurement_id", id));
        ArrayList<DetailProcurementNextVO> detailProcurementNextVOS = new ArrayList<>();
        for (TErpProcurementGoods tErpProcurementGoods : list) {
            DetailProcurementNextVO detailProcurementNextVO = new DetailProcurementNextVO();
            detailProcurementNextVO.setGoodsName(tErpProcurementGoods.getGoodsName());
            detailProcurementNextVO.setQuasiNumber(tErpProcurementGoods.getQuasiNumber());
            detailProcurementNextVO.setPackingUnitName(tErpProcurementGoods.getUnitName());
            detailProcurementNextVO.setSalesAmount(tErpProcurementGoods.getSalesAmount());
            detailProcurementNextVO.setNum(tErpProcurementGoods.getPurchaseCount());
            detailProcurementNextVO.setTotalPrice(tErpProcurement.getPayMoney());
            detailProcurementNextVO.setId(tErpProcurementGoods.getId());
            detailProcurementNextVO.setPurchasePrice(tErpProcurementGoods.getPurchasePrice());
            detailProcurementNextVOS.add(detailProcurementNextVO);
        }
        detailProcurementVO.setList(detailProcurementNextVOS);
        return detailProcurementVO;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void detailProcurementSend(String clinicSupplierId, SysUser user, String id) {
        // 查出这个采购的下级商品
        List<TErpProcurementGoods> list = erpProcurementGoodsService.list(new QueryWrapper<TErpProcurementGoods>().eq("procurement_id", id));
        List<TErpSupplierWarehousing> tErpSupplierWarehousings = erpSupplierWarehousingMapper.selectList(new LambdaQueryWrapper<TErpSupplierWarehousing>()
                .eq(TErpSupplierWarehousing::getSupplierId, clinicSupplierId).in(TErpSupplierWarehousing::getGoodsId, list.stream().map(TErpProcurementGoods::getGoodsId).collect(Collectors.toList()))
                .orderByAsc(BaseModel::getCreateTime));
 
        if(tErpSupplierWarehousings.isEmpty()){
            throw new RuntimeException("商品供应商未入库,请先入库");
        }
 
        List<TErpSupplierWarehousingBatch> tErpSupplierWarehousingBatches = erpSupplierWarehousingBatchMapper.selectList(new LambdaQueryWrapper<TErpSupplierWarehousingBatch>().in(TErpSupplierWarehousingBatch::getWarehousingId, tErpSupplierWarehousings.stream().map(TErpSupplierWarehousing::getId).collect(Collectors.toList())));
        // 获取所有的出库记录
        List<TErpSupplierOutboundGoods> tErpSupplierOutboundGoodsLists = erpSupplierOutboundGoodsMapper.selectList(new LambdaQueryWrapper<TErpSupplierOutboundGoods>().in(TErpSupplierOutboundGoods::getWarehousingBatchId, tErpSupplierWarehousingBatches.stream().map(TErpSupplierWarehousingBatch::getId).collect(Collectors.toList())));
 
 
        outer:for (TErpProcurementGoods tErpProcurementGoods : list) {
            // 分别入库
            String goodsId = tErpProcurementGoods.getGoodsId();
            // 查询这个供应商的 该商品是否入过库 未入库提示  出库判断库存 先找先入库的
            List<TErpSupplierWarehousing> collect = tErpSupplierWarehousings.stream().filter(e -> e.getGoodsId().equals(goodsId)).collect(Collectors.toList());
            if(collect.isEmpty()){
                throw new RuntimeException("商品供应商未入库,请先入库");
            }
            // 需要出库的数量
            Integer purchaseCount = tErpProcurementGoods.getPurchaseCount();
 
 
            if(purchaseCount==0){
                continue;
            }
 
            for (TErpSupplierWarehousing tErpSupplierWarehousing : collect) {
                List<TErpSupplierWarehousingBatch> warehousingBatches = tErpSupplierWarehousingBatches.stream().filter(e -> e.getWarehousingId().equals(tErpSupplierWarehousing.getId())).collect(Collectors.toList());
                for (TErpSupplierWarehousingBatch warehousingBatch : warehousingBatches) {
                    // 已出库数量
                    int sum = tErpSupplierOutboundGoodsLists.stream().filter(e -> e.getWarehousingBatchId().equals(warehousingBatch.getId())).mapToInt(TErpSupplierOutboundGoods::getOutboundCount).sum();
                    // 该批次剩余的数量
                    int count = warehousingBatch.getWarehousingNumber() - sum;
 
                    // 该批次没有数量 找下个批次
                    if(count==0 ){
                        continue;
                    }
                    // 如果大于这个数量 则使用这个批次
                    if(count>=purchaseCount){
                        // 添加出库记录
                        TErpSupplierOutbound tErpSupplierOutbound= new TErpSupplierOutbound();
                        tErpSupplierOutbound.setSupplierId(clinicSupplierId);
                        tErpSupplierOutbound.setWarehouseId(tErpSupplierWarehousing.getWarehouseId());
                        tErpSupplierOutbound.setOutboundType(7);
                        //TODO 确定这个是采购价还是销售价格
                        tErpSupplierOutbound.setTotalMoney(tErpProcurementGoods.getPurchasePrice().multiply(new BigDecimal(purchaseCount)));
                        tErpSupplierOutbound.setOutboundNumber("G" + DateUtils.dateTimeNow());
                        erpSupplierOutboundMapper.insert(tErpSupplierOutbound);
 
                        TErpSupplierOutboundGoods tErpSupplierOutboundGoods = new TErpSupplierOutboundGoods();
                        tErpSupplierOutboundGoods.setOutboundId(tErpSupplierOutbound.getId());
                        tErpSupplierOutboundGoods.setWarehousingId(tErpSupplierWarehousing.getId());
                        tErpSupplierOutboundGoods.setWarehousingBatchId(warehousingBatch.getId());
                        tErpSupplierOutboundGoods.setOutboundCount(purchaseCount);
                        tErpSupplierOutboundGoods.setTotalPrice(tErpProcurementGoods.getPurchasePrice().multiply(new BigDecimal(purchaseCount)));
                        erpSupplierOutboundGoodsMapper.insert(tErpSupplierOutboundGoods);
                        continue outer;
                    }
 
                    if(count<purchaseCount){
                        purchaseCount = purchaseCount - count;
                        // 添加出库记录
                        TErpSupplierOutbound tErpSupplierOutbound= new TErpSupplierOutbound();
                        tErpSupplierOutbound.setSupplierId(clinicSupplierId);
                        tErpSupplierOutbound.setWarehouseId(tErpSupplierWarehousing.getWarehouseId());
                        tErpSupplierOutbound.setOutboundType(7);
                        //TODO 确定这个是采购价还是销售价格
                        tErpSupplierOutbound.setTotalMoney(tErpProcurementGoods.getPurchasePrice().multiply(new BigDecimal(purchaseCount)));
                        tErpSupplierOutbound.setOutboundNumber("G" + DateUtils.dateTimeNow());
                        erpSupplierOutboundMapper.insert(tErpSupplierOutbound);
 
                        TErpSupplierOutboundGoods tErpSupplierOutboundGoods = new TErpSupplierOutboundGoods();
                        tErpSupplierOutboundGoods.setOutboundId(tErpSupplierOutbound.getId());
                        tErpSupplierOutboundGoods.setWarehousingId(tErpSupplierWarehousing.getId());
                        tErpSupplierOutboundGoods.setWarehousingBatchId(warehousingBatch.getId());
                        tErpSupplierOutboundGoods.setOutboundCount(purchaseCount);
                        tErpSupplierOutboundGoods.setTotalPrice(tErpProcurementGoods.getPurchasePrice().multiply(new BigDecimal(purchaseCount)));
                        erpSupplierOutboundGoodsMapper.insert(tErpSupplierOutboundGoods);
                    }
 
                }
 
            }
            if(purchaseCount>0){
                throw new RuntimeException("商品供应商库存不足,请先入库");
            }
 
 
        }
 
    }
 
}