liujie
4 天以前 5ba50d43ea3384a606ab16501633797297a78de3
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
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.util.Collections;
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 TErpSupplierInventoryGoodsMapper erpSupplierInventoryGoodsMapper;
 
    @Resource
    private TErpSupplierWarehousingMapper erpSupplierWarehousingMapper;
 
    @Resource
    private TErpSupplierWarehousingBatchMapper erpSupplierWarehousingBatchMapper;
 
    @Resource
    private TErpSupplierOutboundMapper erpSupplierOutboundMapper;
 
    @Resource
    private TErpSupplierOutboundGoodsMapper erpSupplierOutboundGoodsMapper;
 
    @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();
        }
        PageInfo<TErpGoodsInventoryVO> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        List<TErpGoodsInventoryVO> list = this.baseMapper.pageInventoryGoodsPageList(query, pageInfo, user,supplierClinicId);
        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 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.setGoodsId(tErpSupplierWarehousing1.getGoodsId());
                tErpSupplierOutbound.setOutboundType(6);
                tErpSupplierOutbound.setOutboundNumber("G" + s);
                int count = inventoryGoodsDto.getNum() - inventoryGoodsDto.getInventoryCount();
                tErpSupplierOutbound.setTotalMoney(erpGoods.getSalesAmount().multiply(new BigDecimal(count)));
                tErpSupplierOutbound.setGoodsId(erpGoods.getId());
                erpSupplierOutboundMapper.insert(tErpSupplierOutbound);
 
 
                TErpSupplierOutboundGoods tErpSupplierOutboundGoods = new TErpSupplierOutboundGoods();
                tErpSupplierOutboundGoods.setOutboundId(tErpSupplierOutbound.getId());
                tErpSupplierOutboundGoods.setWarehousingId(tErpSupplierWarehousing.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);
            }
        }
 
 
    }
}