liujie
昨天 f41b11c958ef7d0416d194278d1fe7cd1a1e8219
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
package com.ruoyi.system.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.dto.InventoryDto;
import com.ruoyi.system.dto.InventoryGoodsDto;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.model.*;
import com.ruoyi.system.query.TErpGoodsInventoryQuery;
import com.ruoyi.system.query.TErpGoodsQuery;
import com.ruoyi.system.service.TErpGoodsService;
import com.ruoyi.system.service.TErpSupplierInventoryGoodsService;
import com.ruoyi.system.vo.TErpGoodsInventoryVO;
import com.ruoyi.system.vo.TErpGoodsVO;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * <p>
 * erp商品 服务实现类
 * </p>
 *
 * @author xiaochen
 * @since 2025-08-20
 */
@Service
public class TErpGoodsServiceImpl extends ServiceImpl<TErpGoodsMapper, TErpGoods> implements TErpGoodsService {
 
    @Resource
    private TErpGoodsTypeMapper erpGoodsTypeMapper;
 
    @Resource
    private TErpGoodsUnitMapper erpGoodsUnitMapper;
 
    @Resource
    private TErpSupplierInventoryMapper erpSupplierInventoryMapper;
    @Resource
    private TErpClinicInventoryMapper erpClinicInventoryMapper;
 
    @Resource
    private TErpSupplierInventoryGoodsMapper erpSupplierInventoryGoodsMapper;
    @Resource
    private TErpClinicInventoryGoodsMapper erpClinicInventoryGoodsMapper;
 
    @Resource
    private TErpSupplierWarehousingMapper erpSupplierWarehousingMapper;
    @Resource
    private TErpClinicWarehousingMapper erpClinicWarehousingMapper;
 
    @Resource
    private TErpSupplierWarehousingBatchMapper erpSupplierWarehousingBatchMapper;
    @Resource
    private TErpClinicWarehousingBatchMapper erpClinicWarehousingBatchMapper;
 
    @Resource
    private TErpSupplierOutboundMapper erpSupplierOutboundMapper;
    @Resource
    private TErpClinicOutboundMapper erpClinicOutboundMapper;
 
    @Resource
    private TErpSupplierOutboundGoodsMapper erpSupplierOutboundGoodsMapper;
    @Resource
    private TErpClinicOutboundGoodsMapper erpClinicOutboundGoodsMapper;
 
    @Resource
    private  TCrmClinicMapper crmClinicMapper;
 
    @Resource
    private  TCrmSupplierMapper crmSupplierMapper;
 
 
    @Override
    public PageInfo<TErpGoodsVO> pageList(TErpGoodsQuery query, SysUser user) {
        PageInfo<TErpGoodsVO> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        List<TErpGoodsVO> list = this.baseMapper.pageList(query, pageInfo, user);
        if (list.isEmpty()) {
            return pageInfo;
        }
        List<String> typeIds = list.stream().map(TErpGoods::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()));
            }
        }
 
        pageInfo.setRecords(list);
        return pageInfo;
    }
 
    @Override
    public List<TErpGoodsVO> listExport(TErpGoodsQuery query, SysUser user) {
        List<TErpGoodsVO> list = this.baseMapper.listExport(query, user);
        if (list.isEmpty()) {
            return list;
        }
        List<String> typeIds = list.stream().map(TErpGoods::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()));
                tErpGoodsVO.setTypeName(tErpGoodsVO.getTypeName());
            }
        }
        List<String> lowUnitIds = list.stream().map(TErpGoods::getLowUnitId).collect(Collectors.toList());
        if (!lowUnitIds.isEmpty()) {
            List<TErpGoodsUnit> tErpGoodsUnits = erpGoodsUnitMapper.selectBatchIds(lowUnitIds);
            for (TErpGoodsVO tErpGoodsVO : list) {
                tErpGoodsUnits.stream().filter(t -> t.getId().equals(tErpGoodsVO.getLowUnitId())).findFirst().ifPresent(t -> tErpGoodsVO.setLowUnitName(t.getUnitName()));
            }
        }
        List<String> packingUnitId = list.stream().map(TErpGoods::getPackingUnitId).collect(Collectors.toList());
        if (!packingUnitId.isEmpty()) {
            List<TErpGoodsUnit> tErpGoodsUnits = erpGoodsUnitMapper.selectBatchIds(packingUnitId);
            for (TErpGoodsVO tErpGoodsVO : list) {
                tErpGoodsUnits.stream().filter(t -> t.getId().equals(tErpGoodsVO.getPackingUnitId())).findFirst().ifPresent(t -> tErpGoodsVO.setPackingUnitName(t.getUnitName()));
            }
        }
        return list;
    }
 
    @Override
    public boolean isExit(String goodsIdCode, String quasiNumber) {
        Long size = this.baseMapper.selectCount(new LambdaQueryWrapper<>(TErpGoods.class).eq(TErpGoods::getGoodsIdCode, goodsIdCode).or().eq(TErpGoods::getQuasiNumber, quasiNumber));
        if (size > 0) {
            return true;
        }
 
        return false;
    }
 
    @Override
    public PageInfo<TErpGoodsInventoryVO> pageInventoryGoodsPageList(TErpGoodsInventoryQuery 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();
        }
 
        // 当前时间一个月后
        Date endDate = DateUtils.addMonths(DateUtils.getNowDate(), 1);
 
        PageInfo<TErpGoodsInventoryVO> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        List<TErpGoodsInventoryVO> list = this.baseMapper.pageInventoryGoodsPageList(query, pageInfo, user,supplierClinicId,endDate);
        if (list.isEmpty()) {
            return pageInfo;
        }
        List<String> typeIds = list.stream().map(TErpGoodsInventoryVO::getTypeId).collect(Collectors.toList());
        if (!typeIds.isEmpty()) {
            List<TErpGoodsType> typeList = erpGoodsTypeMapper.selectBatchIds(typeIds);
            for (TErpGoodsInventoryVO tErpGoodsVO : list) {
                typeList.stream().filter(t -> t.getId().equals(tErpGoodsVO.getTypeId())).findFirst().ifPresent(t -> tErpGoodsVO.setTypeName(t.getTypeName()));
                tErpGoodsVO.setTypeName(tErpGoodsVO.getTypeName());
            }
        }
        List<String> packingUnitId = list.stream().map(TErpGoodsInventoryVO::getPackingUnitId).collect(Collectors.toList());
        if (!packingUnitId.isEmpty()) {
            List<TErpGoodsUnit> tErpGoodsUnits = erpGoodsUnitMapper.selectBatchIds(packingUnitId);
            for (TErpGoodsInventoryVO tErpGoodsVO : list) {
                tErpGoodsUnits.stream().filter(t -> t.getId().equals(tErpGoodsVO.getPackingUnitId())).findFirst().ifPresent(t -> tErpGoodsVO.setPackingUnitName(t.getUnitName()));
            }
        }
        pageInfo.setRecords(list);
        return pageInfo;
    }
    @Override
    public PageInfo<TErpGoodsInventoryVO> pageInventoryGoodsPageList1(TErpGoodsInventoryQuery 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();
        }
 
        // 当前时间一个月后
        Date endDate = DateUtils.addMonths(DateUtils.getNowDate(), 1);
 
        PageInfo<TErpGoodsInventoryVO> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        List<TErpGoodsInventoryVO> list = this.baseMapper.pageInventoryGoodsPageList1(query, pageInfo, user,supplierClinicId,endDate);
        if (list.isEmpty()) {
            return pageInfo;
        }
        List<String> typeIds = list.stream().map(TErpGoodsInventoryVO::getTypeId).collect(Collectors.toList());
        if (!typeIds.isEmpty()) {
            List<TErpGoodsType> typeList = erpGoodsTypeMapper.selectBatchIds(typeIds);
            for (TErpGoodsInventoryVO tErpGoodsVO : list) {
                typeList.stream().filter(t -> t.getId().equals(tErpGoodsVO.getTypeId())).findFirst().ifPresent(t -> tErpGoodsVO.setTypeName(t.getTypeName()));
                tErpGoodsVO.setTypeName(tErpGoodsVO.getTypeName());
            }
        }
        List<String> packingUnitId = list.stream().map(TErpGoodsInventoryVO::getPackingUnitId).collect(Collectors.toList());
        if (!packingUnitId.isEmpty()) {
            List<TErpGoodsUnit> tErpGoodsUnits = erpGoodsUnitMapper.selectBatchIds(packingUnitId);
            for (TErpGoodsInventoryVO tErpGoodsVO : list) {
                tErpGoodsUnits.stream().filter(t -> t.getId().equals(tErpGoodsVO.getPackingUnitId())).findFirst().ifPresent(t -> tErpGoodsVO.setPackingUnitName(t.getUnitName()));
            }
        }
        pageInfo.setRecords(list);
        return pageInfo;
    }
    @Override
    public List<TErpGoodsInventoryVO> pageInventoryGoodsList(String warehouseId, 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();
        }
 
        // 当前时间一个月后
        Date endDate = DateUtils.addMonths(DateUtils.getNowDate(), 1);
 
        List<TErpGoodsInventoryVO> list = this.baseMapper.pageInventoryGoodsList(warehouseId, user,supplierClinicId,endDate);
        if (list.isEmpty()) {
            return list;
        }
        List<String> typeIds = list.stream().map(TErpGoodsInventoryVO::getTypeId).collect(Collectors.toList());
        if (!typeIds.isEmpty()) {
            List<TErpGoodsType> typeList = erpGoodsTypeMapper.selectBatchIds(typeIds);
            for (TErpGoodsInventoryVO tErpGoodsVO : list) {
                typeList.stream().filter(t -> t.getId().equals(tErpGoodsVO.getTypeId())).findFirst().ifPresent(t -> tErpGoodsVO.setTypeName(t.getTypeName()));
                tErpGoodsVO.setTypeName(tErpGoodsVO.getTypeName());
            }
        }
        List<String> packingUnitId = list.stream().map(TErpGoodsInventoryVO::getPackingUnitId).collect(Collectors.toList());
        if (!packingUnitId.isEmpty()) {
            List<TErpGoodsUnit> tErpGoodsUnits = erpGoodsUnitMapper.selectBatchIds(packingUnitId);
            for (TErpGoodsInventoryVO tErpGoodsVO : list) {
                tErpGoodsUnits.stream().filter(t -> t.getId().equals(tErpGoodsVO.getPackingUnitId())).findFirst().ifPresent(t -> tErpGoodsVO.setPackingUnitName(t.getUnitName()));
            }
        }
        return list;
    }
    @Override
    public List<TErpGoodsInventoryVO> pageInventoryGoodsList1(String warehouseId, 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();
        }
 
        // 当前时间一个月后
        Date endDate = DateUtils.addMonths(DateUtils.getNowDate(), 1);
 
        List<TErpGoodsInventoryVO> list = this.baseMapper.pageInventoryGoodsList1(warehouseId, user,supplierClinicId,endDate);
        if (list.isEmpty()) {
            return list;
        }
        List<String> typeIds = list.stream().map(TErpGoodsInventoryVO::getTypeId).collect(Collectors.toList());
        if (!typeIds.isEmpty()) {
            List<TErpGoodsType> typeList = erpGoodsTypeMapper.selectBatchIds(typeIds);
            for (TErpGoodsInventoryVO tErpGoodsVO : list) {
                typeList.stream().filter(t -> t.getId().equals(tErpGoodsVO.getTypeId())).findFirst().ifPresent(t -> tErpGoodsVO.setTypeName(t.getTypeName()));
                tErpGoodsVO.setTypeName(tErpGoodsVO.getTypeName());
            }
        }
        List<String> packingUnitId = list.stream().map(TErpGoodsInventoryVO::getPackingUnitId).collect(Collectors.toList());
        if (!packingUnitId.isEmpty()) {
            List<TErpGoodsUnit> tErpGoodsUnits = erpGoodsUnitMapper.selectBatchIds(packingUnitId);
            for (TErpGoodsInventoryVO tErpGoodsVO : list) {
                tErpGoodsUnits.stream().filter(t -> t.getId().equals(tErpGoodsVO.getPackingUnitId())).findFirst().ifPresent(t -> tErpGoodsVO.setPackingUnitName(t.getUnitName()));
            }
        }
        return list;
    }
 
    @Override
    public void inventoryGoods(InventoryDto dto, 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();
        }
        TErpSupplierInventory tErpSupplierInventory = new TErpSupplierInventory();
        String s = DateUtils.dateTimeNow();
        tErpSupplierInventory.setInventoryNumber("P" + s);
        tErpSupplierInventory.setWarehouseId(dto.getWarehouseId());
        tErpSupplierInventory.setSupplierId(supplierClinicId);
        erpSupplierInventoryMapper.insert(tErpSupplierInventory);
 
        List<InventoryGoodsDto> dtos = dto.getDtos();
        for (InventoryGoodsDto inventoryGoodsDto : dtos) {
            // 根据入库批次id查询入库信息
            TErpSupplierWarehousing tErpSupplierWarehousing = new TErpSupplierWarehousing();
            TErpSupplierWarehousingBatch tErpSupplierWarehousingBatch = erpSupplierWarehousingBatchMapper.selectById(inventoryGoodsDto.getId());
            String warehousingId = tErpSupplierWarehousingBatch.getWarehousingId();
            TErpSupplierWarehousing tErpSupplierWarehousing1 = erpSupplierWarehousingMapper.selectById(warehousingId);
            TErpGoods erpGoods = this.getById(tErpSupplierWarehousing1.getGoodsId());
 
            // 盘点是盘亏盘盈   赢
            if (inventoryGoodsDto.getNum() < inventoryGoodsDto.getInventoryCount()) {
                // 添加入库信息
                tErpSupplierWarehousing.setWarehouseId(dto.getWarehouseId());
                tErpSupplierWarehousing.setSupplierId(supplierClinicId);
                tErpSupplierWarehousing.setGoodsId(tErpSupplierWarehousing1.getGoodsId());
                tErpSupplierWarehousing.setGoodsName(erpGoods.getGoodsName());
                tErpSupplierWarehousing.setGoodsCount(inventoryGoodsDto.getInventoryCount() - inventoryGoodsDto.getNum());
                tErpSupplierWarehousing.setUnitAmount(erpGoods.getSalesAmount());
                tErpSupplierWarehousing.setTotalPrice(erpGoods.getSalesAmount().multiply(new BigDecimal(tErpSupplierWarehousing.getGoodsCount())));
                // 当前年月日时分秒
                String time = DateUtils.dateTimeNow();
                tErpSupplierWarehousing.setWarehouseNo("G" + time);
                tErpSupplierWarehousing.setType(2);
                erpSupplierWarehousingMapper.insert(tErpSupplierWarehousing);
 
                // 添加批次信息
                TErpSupplierWarehousingBatch tErpSupplierWarehousingBatch1 = new TErpSupplierWarehousingBatch();
                tErpSupplierWarehousingBatch1.setWarehousingId(tErpSupplierWarehousing.getId());
                tErpSupplierWarehousingBatch1.setWarehousingNumber(tErpSupplierWarehousing.getGoodsCount());
                tErpSupplierWarehousingBatch1.setBatchNumber(tErpSupplierWarehousingBatch.getBatchNumber());
                tErpSupplierWarehousingBatch1.setProductionDate(tErpSupplierWarehousingBatch.getProductionDate());
                tErpSupplierWarehousingBatch1.setExpiryDate(tErpSupplierWarehousingBatch.getExpiryDate());
                erpSupplierWarehousingBatchMapper.insert(tErpSupplierWarehousingBatch1);
 
 
                // 添加盘点信息
                TErpSupplierInventoryGoods tErpSupplierInventoryGoods = new TErpSupplierInventoryGoods();
                tErpSupplierInventoryGoods.setInventoryId(tErpSupplierInventory.getId());
                tErpSupplierInventoryGoods.setWarehousingId(tErpSupplierWarehousing.getId());
                tErpSupplierInventoryGoods.setWarehousingBatchId(tErpSupplierWarehousingBatch1.getId());
                tErpSupplierInventoryGoods.setInventoryCount(inventoryGoodsDto.getInventoryCount());
                tErpSupplierInventoryGoods.setDamagedCount(inventoryGoodsDto.getDamagedCount());
                tErpSupplierInventoryGoods.setInventoryType(1);
                tErpSupplierInventoryGoods.setGoodsId(tErpSupplierWarehousing1.getGoodsId());
                erpSupplierInventoryGoodsMapper.insert(tErpSupplierInventoryGoods);
 
            } else {
 
 
                TErpSupplierOutbound tErpSupplierOutbound = new TErpSupplierOutbound();
                tErpSupplierOutbound.setWarehouseId(dto.getWarehouseId());
                tErpSupplierOutbound.setSupplierId(supplierClinicId);
                tErpSupplierOutbound.setOutboundType(6);
                tErpSupplierOutbound.setOutboundNumber("G" + s);
                int count = inventoryGoodsDto.getNum() - inventoryGoodsDto.getInventoryCount();
                tErpSupplierOutbound.setTotalMoney(erpGoods.getSalesAmount().multiply(new BigDecimal(count)));
                erpSupplierOutboundMapper.insert(tErpSupplierOutbound);
 
 
                TErpSupplierOutboundGoods tErpSupplierOutboundGoods = new TErpSupplierOutboundGoods();
                tErpSupplierOutboundGoods.setOutboundId(tErpSupplierOutbound.getId());
                tErpSupplierOutboundGoods.setWarehousingId(tErpSupplierWarehousing1.getId());
                tErpSupplierOutboundGoods.setWarehousingBatchId(tErpSupplierWarehousingBatch.getId());
                tErpSupplierOutboundGoods.setOutboundCount(count);
                tErpSupplierOutboundGoods.setTotalPrice(erpGoods.getSalesAmount().multiply(new BigDecimal(count)));
                erpSupplierOutboundGoodsMapper.insert(tErpSupplierOutboundGoods);
 
 
                // 添加盘点信息
                TErpSupplierInventoryGoods tErpSupplierInventoryGoods = new TErpSupplierInventoryGoods();
                tErpSupplierInventoryGoods.setInventoryId(tErpSupplierInventory.getId());
                tErpSupplierInventoryGoods.setWarehousingId(tErpSupplierOutbound.getId());
                tErpSupplierInventoryGoods.setWarehousingBatchId(tErpSupplierOutboundGoods.getId());
                tErpSupplierInventoryGoods.setInventoryCount(inventoryGoodsDto.getInventoryCount());
                tErpSupplierInventoryGoods.setDamagedCount(inventoryGoodsDto.getDamagedCount());
                tErpSupplierInventoryGoods.setInventoryType(2);
                tErpSupplierInventoryGoods.setGoodsId(tErpSupplierWarehousing1.getGoodsId());
                erpSupplierInventoryGoodsMapper.insert(tErpSupplierInventoryGoods);
            }
        }
 
 
    }
    @Override
    public void inventoryGoods1(InventoryDto dto, 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();
        }
        TErpClinicInventory tErpSupplierInventory = new TErpClinicInventory();
        String s = DateUtils.dateTimeNow();
        tErpSupplierInventory.setInventoryNumber("P" + s);
        tErpSupplierInventory.setClinicId(supplierClinicId);
        erpClinicInventoryMapper.insert(tErpSupplierInventory);
 
        List<InventoryGoodsDto> dtos = dto.getDtos();
        for (InventoryGoodsDto inventoryGoodsDto : dtos) {
            // 根据入库批次id查询入库信息
            TErpClinicWarehousing tErpSupplierWarehousing = new TErpClinicWarehousing();
            TErpClinicWarehousingBatch tErpSupplierWarehousingBatch = erpClinicWarehousingBatchMapper.selectById(inventoryGoodsDto.getId());
            String warehousingId = tErpSupplierWarehousingBatch.getWarehousingId();
            TErpClinicWarehousing tErpSupplierWarehousing1 = erpClinicWarehousingMapper.selectById(warehousingId);
            TErpGoods erpGoods = this.getById(tErpSupplierWarehousing1.getGoodsId());
 
            // 盘点是盘亏盘盈   赢
            if (inventoryGoodsDto.getNum() < inventoryGoodsDto.getInventoryCount()) {
                // 添加入库信息
                tErpSupplierWarehousing.setClinicId(supplierClinicId);
                tErpSupplierWarehousing.setGoodsId(tErpSupplierWarehousing1.getGoodsId());
                tErpSupplierWarehousing.setGoodsName(erpGoods.getGoodsName());
                tErpSupplierWarehousing.setPurchaseCount(inventoryGoodsDto.getInventoryCount() - inventoryGoodsDto.getNum());
                tErpSupplierWarehousing.setUnitAmount(erpGoods.getSalesAmount());
                tErpSupplierWarehousing.setTotalPrice(erpGoods.getSalesAmount().multiply(new BigDecimal(tErpSupplierWarehousing.getPurchaseCount())));
                // 当前年月日时分秒
                String time = DateUtils.dateTimeNow();
                tErpSupplierWarehousing.setWarehouseNo("G" + time);
                tErpSupplierWarehousing.setType(2);
                erpClinicWarehousingMapper.insert(tErpSupplierWarehousing);
 
                // 添加批次信息
                TErpSupplierWarehousingBatch tErpSupplierWarehousingBatch1 = new TErpSupplierWarehousingBatch();
                tErpSupplierWarehousingBatch1.setWarehousingId(tErpSupplierWarehousing.getId());
                tErpSupplierWarehousingBatch1.setWarehousingNumber(tErpSupplierWarehousing.getPurchaseCount());
                tErpSupplierWarehousingBatch1.setBatchNumber(tErpSupplierWarehousingBatch.getBatchNumber());
                tErpSupplierWarehousingBatch1.setProductionDate(tErpSupplierWarehousingBatch.getProductionDate());
                tErpSupplierWarehousingBatch1.setExpiryDate(tErpSupplierWarehousingBatch.getExpiryDate());
                erpSupplierWarehousingBatchMapper.insert(tErpSupplierWarehousingBatch1);
 
 
                // 添加盘点信息
                TErpSupplierInventoryGoods tErpSupplierInventoryGoods = new TErpSupplierInventoryGoods();
                tErpSupplierInventoryGoods.setInventoryId(tErpSupplierInventory.getId());
                tErpSupplierInventoryGoods.setWarehousingId(tErpSupplierWarehousing.getId());
                tErpSupplierInventoryGoods.setWarehousingBatchId(tErpSupplierWarehousingBatch1.getId());
                tErpSupplierInventoryGoods.setInventoryCount(inventoryGoodsDto.getInventoryCount());
                tErpSupplierInventoryGoods.setDamagedCount(inventoryGoodsDto.getDamagedCount());
                tErpSupplierInventoryGoods.setInventoryType(1);
                tErpSupplierInventoryGoods.setGoodsId(tErpSupplierWarehousing1.getGoodsId());
                erpSupplierInventoryGoodsMapper.insert(tErpSupplierInventoryGoods);
 
            } else {
 
 
                TErpSupplierOutbound tErpSupplierOutbound = new TErpSupplierOutbound();
                tErpSupplierOutbound.setWarehouseId(dto.getWarehouseId());
                tErpSupplierOutbound.setSupplierId(supplierClinicId);
                tErpSupplierOutbound.setOutboundType(6);
                tErpSupplierOutbound.setOutboundNumber("G" + s);
                int count = inventoryGoodsDto.getNum() - inventoryGoodsDto.getInventoryCount();
                tErpSupplierOutbound.setTotalMoney(erpGoods.getSalesAmount().multiply(new BigDecimal(count)));
                erpSupplierOutboundMapper.insert(tErpSupplierOutbound);
 
 
                TErpSupplierOutboundGoods tErpSupplierOutboundGoods = new TErpSupplierOutboundGoods();
                tErpSupplierOutboundGoods.setOutboundId(tErpSupplierOutbound.getId());
                tErpSupplierOutboundGoods.setWarehousingId(tErpSupplierWarehousing1.getId());
                tErpSupplierOutboundGoods.setWarehousingBatchId(tErpSupplierWarehousingBatch.getId());
                tErpSupplierOutboundGoods.setOutboundCount(count);
                tErpSupplierOutboundGoods.setTotalPrice(erpGoods.getSalesAmount().multiply(new BigDecimal(count)));
                erpSupplierOutboundGoodsMapper.insert(tErpSupplierOutboundGoods);
 
 
                // 添加盘点信息
                TErpSupplierInventoryGoods tErpSupplierInventoryGoods = new TErpSupplierInventoryGoods();
                tErpSupplierInventoryGoods.setInventoryId(tErpSupplierInventory.getId());
                tErpSupplierInventoryGoods.setWarehousingId(tErpSupplierOutbound.getId());
                tErpSupplierInventoryGoods.setWarehousingBatchId(tErpSupplierOutboundGoods.getId());
                tErpSupplierInventoryGoods.setInventoryCount(inventoryGoodsDto.getInventoryCount());
                tErpSupplierInventoryGoods.setDamagedCount(inventoryGoodsDto.getDamagedCount());
                tErpSupplierInventoryGoods.setInventoryType(2);
                tErpSupplierInventoryGoods.setGoodsId(tErpSupplierWarehousing1.getGoodsId());
                erpSupplierInventoryGoodsMapper.insert(tErpSupplierInventoryGoods);
            }
        }
 
 
    }
}