xuhy
昨天 092a98a583c4fe822a3eb4c4dac086f84b16189c
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
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.plugins.pagination.Page;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.dto.ClinicOutboundGoodsDto;
import com.ruoyi.system.dto.OutboundGoodsNextDto;
import com.ruoyi.system.dto.clinicWarehouseGoodsDto;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.model.*;
import com.ruoyi.system.query.*;
import com.ruoyi.system.service.TErpClinicWarehousingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.vo.*;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
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 TErpClinicWarehousingServiceImpl extends ServiceImpl<TErpClinicWarehousingMapper, TErpClinicWarehousing> implements TErpClinicWarehousingService {
 
 
    @Resource
    private TCrmClinicMapper crmClinicMapper;
 
    @Resource
    private TErpClinicInventoryGoodsMapper erpClinicInventoryGoodsMapper;
 
    @Resource
    private TErpGoodsMapper erpGoodsMapper;
 
    @Resource
    private TErpClinicWarehousingMapper erpClinicWarehousingMapper;
 
    @Resource
    private TErpClinicWarehousingBatchMapper erpClinicWarehousingBatchMapper;
 
    @Resource
    private TErpClinicOutboundGoodsMapper erpClinicOutboundGoodsMapper;
 
 
    @Resource
    private TErpClinicOutboundMapper erpClinicOutboundMapper;
 
    @Resource
    private TErpGoodsUnitMapper erpGoodsUnitMapper;
 
 
    @Resource
    private TErpGoodsTypeMapper erpGoodsTypeMapper;
 
 
 
    @Override
    public PageInfo<PageInventoryListVo> pageInventoryList(TErpInventoryQuery query, SysUser user) {
        Integer roleType = user.getRoleType();
        String supplierClinicId = null;
        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<PageInventoryListVo> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        List<PageInventoryListVo> list = this.baseMapper.pageInventoryList(pageInfo,query,user,sTime,eTime,supplierClinicId);
        if(list.isEmpty()){
            return pageInfo;
        }
 
 
 
        List<String> collect = list.stream().map(PageInventoryListVo::getId).collect(Collectors.toList());
        List<TErpClinicInventoryGoods> tErpSupplierInventoryGoods = erpClinicInventoryGoodsMapper.selectList(new LambdaQueryWrapper<TErpClinicInventoryGoods>().in(TErpClinicInventoryGoods::getInventoryId,collect));
        for (PageInventoryListVo pageInventoryListVo : list) {
            // 盘盈
            BigDecimal add = BigDecimal.ZERO;
 
            // 盘亏
            BigDecimal subtract = BigDecimal.ZERO;
            List<TErpClinicInventoryGoods> tErpSupplierInventoryGoodsList = tErpSupplierInventoryGoods.stream().filter(t -> t.getInventoryId().equals(pageInventoryListVo.getId())).collect(Collectors.toList());
            for (TErpClinicInventoryGoods tErpSupplierInventoryGoods1 : tErpSupplierInventoryGoodsList) {
 
 
                if(tErpSupplierInventoryGoods1!=null){
                    // 盘盈 查入库
                    if(tErpSupplierInventoryGoods1.getInventoryType()==1){
                        TErpClinicWarehousing tErpSupplierWarehousing = erpClinicWarehousingMapper.selectById(tErpSupplierInventoryGoods1.getWarehousingId());
//                        List<TErpSupplierWarehousingBatch> tErpSupplierWarehousingBatches = erpSupplierWarehousingBatchMapper.selectList(new LambdaQueryWrapper<TErpSupplierWarehousingBatch>().eq(TErpSupplierWarehousingBatch::getWarehousingId, tErpSupplierWarehousing.getId()));
//                        int sum = tErpSupplierWarehousingBatches.stream().mapToInt(TErpSupplierWarehousingBatch::getWarehousingNumber).sum();
                        String goodsId = tErpSupplierInventoryGoods1.getGoodsId();
                        TErpGoods goods = erpGoodsMapper.selectById(goodsId);
                        int count = tErpSupplierInventoryGoods1.getInventoryCount() - tErpSupplierInventoryGoods1.getDamagedCount();
                        BigDecimal multiply = goods.getSalesAmount().multiply(new BigDecimal(count));
                        add = add.add(multiply);
                    }else {
                        // 盘亏 查出库
                        TErpClinicOutbound tErpSupplierOutbound = erpClinicOutboundMapper.selectById(tErpSupplierInventoryGoods1.getWarehousingId());
 
                        List<TErpClinicOutboundGoods> tErpSupplierOutboundGoods = erpClinicOutboundGoodsMapper.selectList(new LambdaQueryWrapper<TErpClinicOutboundGoods>().eq(TErpClinicOutboundGoods::getOutboundId, tErpSupplierOutbound.getId()));
                        for (TErpClinicOutboundGoods tErpSupplierOutboundGood : tErpSupplierOutboundGoods) {
                            TErpClinicWarehousing tErpSupplierWarehousing = erpClinicWarehousingMapper.selectById(tErpSupplierOutboundGood.getWarehousingId());
                            String goodsId = tErpSupplierInventoryGoods1.getGoodsId();
                            TErpGoods goods = erpGoodsMapper.selectById(goodsId);
                            int count = tErpSupplierInventoryGoods1.getInventoryCount() - tErpSupplierInventoryGoods1.getDamagedCount();
                            BigDecimal multiply = goods.getSalesAmount().multiply(new BigDecimal(count));
                            add = add.add(multiply);
                        }
//                        int sum = tErpSupplierOutboundGoods.stream().mapToInt(TErpSupplierOutboundGoods::getOutboundCount).sum();
//                        String goodsId = tErpSupplierOutbound.getGoodsId();
//                        TErpGoods goods = erpGoodsMapper.selectById(goodsId);
//                        BigDecimal multiply = goods.getSalesAmount().multiply(new BigDecimal(sum));
//                        subtract = subtract.add(multiply);
                    }
                }
            }
            int size = tErpSupplierInventoryGoodsList.stream().map(TErpClinicInventoryGoods::getGoodsId).collect(Collectors.toSet()).size();
            pageInventoryListVo.setNum(size);
            pageInventoryListVo.setTotalPrice(add);
 
        }
 
        pageInfo.setRecords(list);
        return pageInfo;
    }
 
    @Override
    public List<InventoryDetailVo> detailInventory(String id, SysUser user) {
        ArrayList<InventoryDetailVo> inventoryDetailVos = new ArrayList<>();
 
        List<TErpClinicInventoryGoods> tErpSupplierInventoryGoods = erpClinicInventoryGoodsMapper.selectList(new LambdaQueryWrapper<TErpClinicInventoryGoods>().eq(TErpClinicInventoryGoods::getInventoryId, id));
        if(tErpSupplierInventoryGoods!=null && !tErpSupplierInventoryGoods.isEmpty()){
            for (TErpClinicInventoryGoods tErpSupplierInventoryGood : tErpSupplierInventoryGoods) {
                InventoryDetailVo inventoryDetailVo = new InventoryDetailVo();
                if(tErpSupplierInventoryGood.getInventoryType()==1){
                    TErpClinicWarehousing tErpSupplierWarehousing = erpClinicWarehousingMapper.selectById(tErpSupplierInventoryGood.getWarehousingId());
                    String goodsId = tErpSupplierInventoryGood.getGoodsId();
                    TErpGoods goods = erpGoodsMapper.selectById(goodsId);
                    inventoryDetailVo.setGoodsName(goods.getGoodsName());
                    TErpGoodsUnit tErpGoodsUnit = erpGoodsUnitMapper.selectById(goods.getPackingUnitId());
                    inventoryDetailVo.setUnitName(tErpGoodsUnit.getUnitName());
                    inventoryDetailVo.setWarehouseNo(tErpSupplierWarehousing.getWarehouseNo());
                    TErpClinicWarehousingBatch tErpSupplierWarehousingBatch = erpClinicWarehousingBatchMapper.selectById(tErpSupplierInventoryGood.getWarehousingBatchId());
                    inventoryDetailVo.setBatchNumber(tErpSupplierWarehousingBatch.getBatchNumber());
 
                    List<TErpClinicWarehousingBatch> tErpSupplierWarehousingBatches = erpClinicWarehousingBatchMapper.selectList(new LambdaQueryWrapper<TErpClinicWarehousingBatch>().eq(TErpClinicWarehousingBatch::getBatchNumber, tErpSupplierWarehousingBatch.getBatchNumber()));
                    int sum = tErpSupplierWarehousingBatches.stream().mapToInt(TErpClinicWarehousingBatch::getWarehousingNumber).sum();
                    List<String> collect = tErpSupplierWarehousingBatches.stream().map(TErpClinicWarehousingBatch::getId).collect(Collectors.toList());
                    if(!collect.isEmpty()){
                        List<TErpClinicOutboundGoods> tErpSupplierOutboundGoods = erpClinicOutboundGoodsMapper.selectList(new LambdaQueryWrapper<TErpClinicOutboundGoods>().eq(TErpClinicOutboundGoods::getWarehousingBatchId, collect));
                        int sum1 = tErpSupplierOutboundGoods.stream().mapToInt(TErpClinicOutboundGoods::getOutboundCount).sum();
                        sum = sum-sum1;
                    }
 
                    // sum = 剩余库存
                    inventoryDetailVo.setNowCount(sum);
                    inventoryDetailVo.setInventoryCount(tErpSupplierInventoryGood.getInventoryCount());
                    inventoryDetailVo.setInventoryType(tErpSupplierInventoryGood.getInventoryType());
                    inventoryDetailVo.setDamagedCount(tErpSupplierInventoryGood.getDamagedCount());
                }
                inventoryDetailVos.add(inventoryDetailVo);
 
            }
 
            return inventoryDetailVos;
 
        }
 
 
        return Collections.emptyList();
    }
 
 
 
    @Override
    public PageInfo<ValidityPeriodWarningVo> validityPeriodWarning(ValidityPeriodWarningQuery query, SysUser user) {
        Integer roleType = user.getRoleType();
        String supplierClinicId = null;
        if(roleType == 5){
            // 诊所
            TCrmClinic crmClinic = crmClinicMapper.selectOne(Wrappers.lambdaQuery(TCrmClinic.class)
                    .eq(TCrmClinic::getUserId, user.getUserId())
                    .last("LIMIT 1"));
            supplierClinicId =crmClinic.getId();
        }
        // 获取当前时间后9个月的时间
        LocalDateTime nineMonthLater = LocalDateTime.now().plusMonths(9);
 
        PageInfo<ValidityPeriodWarningVo> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
 
 
 
        return this.baseMapper.validityPeriodWarning(pageInfo,query,user,nineMonthLater,supplierClinicId);
    }
 
    @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()));
                tErpGoodsVO.setTypeName(tErpGoodsVO.getTypeName());
            }
        }
        pageInfo.setRecords(list);
        return pageInfo;
    }
 
    @Override
    public PageInfo<TErpClinicWarehousePageListVO> warehousePageList(TClinicWarehouseQuery query, SysUser user, String supplierClinicId) {
        String time = query.getTime();
        String sTime =null;
        String eTime = null;
        if(time!=null && !time.isEmpty()){
            String[] split = time.split(" - ");
            sTime = split[0] + " 00:00:00";
            eTime = split[1] + " 23:59:59";
        }
        PageInfo<TErpClinicWarehousePageListVO> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        List<TErpClinicWarehousePageListVO> list = this.baseMapper.warehousePageList(query,pageInfo,user,supplierClinicId,sTime,eTime);
        if(list.isEmpty()){
            return pageInfo;
        }
        List<String> collect = list.stream().map(TErpClinicWarehousePageListVO::getId).collect(Collectors.toList());
        List<TErpClinicWarehousingBatch> tErpClinicWarehousingBatches = erpClinicWarehousingBatchMapper.selectList(new LambdaQueryWrapper<TErpClinicWarehousingBatch>().in(TErpClinicWarehousingBatch::getWarehousingId, collect));
 
        for (TErpClinicWarehousePageListVO tErpClinicWarehousePageListVO : list) {
            int size = tErpClinicWarehousingBatches.stream().filter(e -> e.getWarehousingId().equals(tErpClinicWarehousePageListVO.getId())).map(TErpClinicWarehousingBatch::getGoodsId).collect(Collectors.toSet()).size();
            tErpClinicWarehousePageListVO.setTypeNum(size);
        }
        pageInfo.setRecords(list);
        return pageInfo;
    }
 
    @Override
    public PageInfo<TErpClinicOutboundPageListVO> outboundPageList(TClinicOutboundQuery query, SysUser user, String supplierClinicId) {
        String time = query.getTime();
        String sTime =null;
        String eTime = null;
        if(time!=null && !time.isEmpty()){
            String[] split = time.split(" - ");
            sTime = split[0] + " 00:00:00";
            eTime = split[1] + " 23:59:59";
        }
        PageInfo<TErpClinicOutboundPageListVO> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        List<TErpClinicOutboundPageListVO> list = this.baseMapper.outboundPageList(query,pageInfo,user,supplierClinicId,sTime,eTime);
        pageInfo.setRecords(list);
        return pageInfo;
    }
 
    @Override
    public void warehouseGoods(List<clinicWarehouseGoodsDto> dtos, SysUser user, String supplierClinicId) {
 
        // dtos根据供应商分组
        Map<String, List<clinicWarehouseGoodsDto>> collect = dtos.stream().collect(Collectors.groupingBy(clinicWarehouseGoodsDto::getSupplierId));
 
        for (Map.Entry<String, List<clinicWarehouseGoodsDto>> stringListEntry : collect.entrySet()) {
            String key = stringListEntry.getKey();
            List<clinicWarehouseGoodsDto> value = stringListEntry.getValue();
            TErpClinicWarehousing tErpClinicWarehousing = new TErpClinicWarehousing();
            tErpClinicWarehousing.setClinicId(supplierClinicId);
            tErpClinicWarehousing.setCreateId(user.getUserId().toString());
            tErpClinicWarehousing.setWarehouseNo("G"+ DateUtils.dateTimeNow());
            tErpClinicWarehousing.setCreateTime(LocalDateTime.now());
            tErpClinicWarehousing.setSupplierId(key);
            tErpClinicWarehousing.setType(3);
            this.save(tErpClinicWarehousing);
 
            BigDecimal totalPrice = new BigDecimal(0);
            for (clinicWarehouseGoodsDto dto : value) {
                TErpClinicWarehousingBatch tErpClinicWarehousingBatch = new TErpClinicWarehousingBatch();
                tErpClinicWarehousingBatch.setWarehousingId(tErpClinicWarehousing.getId());
                tErpClinicWarehousingBatch.setWarehousingNumber(dto.getPurchaseCount());
                tErpClinicWarehousingBatch.setBatchNumber(dto.getBatchNumber());
                tErpClinicWarehousingBatch.setProductionDate(dto.getProductionDate());
                tErpClinicWarehousingBatch.setExpiryDate(dto.getExpiryDate());
                tErpClinicWarehousingBatch.setGoodsId(dto.getId());
                TErpGoods goods = erpGoodsMapper.selectById(dto.getId());
                tErpClinicWarehousingBatch.setGoodsName(goods.getGoodsName());
                tErpClinicWarehousingBatch.setQuasiNumber(goods.getQuasiNumber());
                tErpClinicWarehousingBatch.setUnitAmount(dto.getUnitAmount());
                tErpClinicWarehousingBatch.setSalesAmount(dto.getSalesAmount());
                if(dto.getSalesAmount()==null){
                    tErpClinicWarehousingBatch.setSalesAmount(goods.getClinicPurchasePrice());
                }
                tErpClinicWarehousingBatch.setPurchaseCount(dto.getPurchaseCount());
                erpClinicWarehousingBatchMapper.insert(tErpClinicWarehousingBatch);
 
                totalPrice = totalPrice.add(dto.getSalesAmount().multiply(new BigDecimal(dto.getPurchaseCount())));
            }
            tErpClinicWarehousing.setTotalPrice(totalPrice);
            this.updateById(tErpClinicWarehousing);
 
        }
 
    }
 
    @Override
    public void outboundGoods(ClinicOutboundGoodsDto dto, SysUser user, String supplierClinicId) {
        TErpClinicOutbound tErpClinicOutbound = new TErpClinicOutbound();
        tErpClinicOutbound.setClinicId(supplierClinicId);
        tErpClinicOutbound.setCreateId(user.getUserId().toString());
        tErpClinicOutbound.setOutboundNumber("G"+ DateUtils.dateTimeNow());
        tErpClinicOutbound.setCreateTime(LocalDateTime.now());
        tErpClinicOutbound.setOutboundType(dto.getOutboundType());
        tErpClinicOutbound.setTotalMoney(dto.getTotalPrice());
        tErpClinicOutbound.setOutboundReason(dto.getOutboundReason());
        tErpClinicOutbound.setTypeNum(dto.getTypeNum());
        erpClinicOutboundMapper.insert(tErpClinicOutbound);
 
        List<TErpClinicOutboundGoods> tErpClinicOutboundGoods = new ArrayList<>();
        for (OutboundGoodsNextDto outboundGoodsNextDto : dto.getOutboundGoodsNextDtos()) {
            TErpClinicOutboundGoods tErpClinicOutboundGoods1 = new TErpClinicOutboundGoods();
            tErpClinicOutboundGoods1.setOutboundId(tErpClinicOutbound.getId());
            String batchId = outboundGoodsNextDto.getBatchId();
            TErpClinicWarehousingBatch tErpClinicWarehousingBatch = erpClinicWarehousingBatchMapper.selectById(batchId);
            tErpClinicOutboundGoods1.setWarehousingId(tErpClinicWarehousingBatch.getWarehousingId());
            tErpClinicOutboundGoods1.setWarehousingBatchId(batchId);
            tErpClinicOutboundGoods1.setOutboundCount(outboundGoodsNextDto.getNum());
            tErpClinicOutboundGoods1.setTotalPrice(tErpClinicWarehousingBatch.getUnitAmount().multiply(BigDecimal.valueOf(outboundGoodsNextDto.getNum())));
            tErpClinicOutboundGoods1.setGoodsId(tErpClinicWarehousingBatch.getGoodsId());
            tErpClinicOutboundGoods.add(tErpClinicOutboundGoods1);
            erpClinicOutboundGoodsMapper.insert(tErpClinicOutboundGoods1);
        }
 
    }
}