liujie
2025-09-06 53c292bfd62bb2f76b45860d5961d381d0fcd539
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
package com.ruoyi.system.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.WarehousingGoodsDto;
import com.ruoyi.system.dto.WarehousingGoodsNextDto;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.model.*;
import com.ruoyi.system.query.TErpGoodsQuery;
import com.ruoyi.system.query.TErpGoodsWarehouseQuery;
import com.ruoyi.system.service.TErpSupplierWarehousingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.vo.TErpGoodsVO;
import com.ruoyi.system.vo.TErpGoodsWarehouseLastVO;
import com.ruoyi.system.vo.TErpGoodsWarehouseRecordLastVO;
import com.ruoyi.system.vo.TErpGoodsWarehouseVO;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
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 TErpSupplierWarehousingServiceImpl extends ServiceImpl<TErpSupplierWarehousingMapper, TErpSupplierWarehousing> implements TErpSupplierWarehousingService {
 
    @Resource
    private TErpGoodsTypeMapper erpGoodsTypeMapper;
 
    @Resource
    private TErpSupplierWarehousingMapper erpSupplierWarehousingMapper;
 
    @Resource
    private TErpGoodsMapper erpGoodsMapper;
 
    @Resource
    private TErpSupplierOutboundGoodsMapper erpSupplierOutboundGoodsMapper;
 
    @Resource
    private TErpSupplierWarehousingBatchMapper erpSupplierWarehousingBatchMapper;
 
    @Resource
    private TCrmWarehouseMapper crmWarehouseMapper;
 
 
    @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 List<TErpGoodsWarehouseLastVO> detail(String id, String warehouseName, SysUser user) {
 
 
        TErpGoods goods = erpGoodsMapper.selectById(id);
        List<TCrmWarehouse> tCrmWarehouses = crmWarehouseMapper.selectList(new LambdaQueryWrapper<TCrmWarehouse>().eq(warehouseName != null && !warehouseName.isEmpty(), TCrmWarehouse::getWarehouseName, warehouseName));
        if(tCrmWarehouses.isEmpty()){
            return Collections.emptyList();
        }
        List<TErpSupplierWarehousing> tErpSupplierWarehousings = erpSupplierWarehousingMapper.selectList(new LambdaQueryWrapper<TErpSupplierWarehousing>().in(TErpSupplierWarehousing::getWarehouseId, tCrmWarehouses.stream().map(TCrmWarehouse::getId).collect(Collectors.toList())).eq(TErpSupplierWarehousing::getSupplierId, user.getUserId()).eq(TErpSupplierWarehousing::getGoodsId, id));
        if(tErpSupplierWarehousings.isEmpty()){
            return Collections.emptyList();
        }
 
        // 出库详情
        List<TErpSupplierOutboundGoods> tErpSupplierOutboundGoods = erpSupplierOutboundGoodsMapper.selectList(new LambdaQueryWrapper<TErpSupplierOutboundGoods>().in(TErpSupplierOutboundGoods::getWarehousingId, tErpSupplierWarehousings.stream().map(TErpSupplierWarehousing::getId).collect(Collectors.toList())));
 
        // 入库批次
        List<TErpSupplierWarehousingBatch> tErpSupplierWarehousingBatches = erpSupplierWarehousingBatchMapper.selectList(new LambdaQueryWrapper<TErpSupplierWarehousingBatch>().in(!tErpSupplierWarehousings.isEmpty(), TErpSupplierWarehousingBatch::getWarehousingId, tErpSupplierWarehousings.stream().map(TErpSupplierWarehousing::getId).collect(Collectors.toList())));
 
        ArrayList<TErpGoodsWarehouseLastVO> tErpGoodsWarehouseLastVOS = new ArrayList<>();
 
        // 通过WarehouseId分组
 
        Map<String, List<TErpSupplierWarehousing>> collect = tErpSupplierWarehousings.stream().collect(Collectors.groupingBy(TErpSupplierWarehousing::getWarehouseId));
        for (Map.Entry<String, List<TErpSupplierWarehousing>> entry : collect.entrySet()) {
            TErpGoodsWarehouseLastVO tErpGoodsWarehouseLastVO = new TErpGoodsWarehouseLastVO();
            // 获取key
            String houseId = entry.getKey();
            List<TErpSupplierWarehousing> list = entry.getValue();
            tErpGoodsWarehouseLastVO.setHouseId( houseId);
            tCrmWarehouses.stream().filter(t -> t.getId().equals(houseId)).findFirst().ifPresent(t -> tErpGoodsWarehouseLastVO.setHouseName(t.getWarehouseName()));
            List<String> warehousingIds = list.stream().map(TErpSupplierWarehousing::getId).collect(Collectors.toList());
 
            // 当前仓库的入库批次信息
            List<TErpSupplierWarehousingBatch> warehousingBatchesList = tErpSupplierWarehousingBatches.stream().filter(t -> warehousingIds.contains(t.getWarehousingId())).collect(Collectors.toList());
            ArrayList<TErpGoodsWarehouseVO> tErpGoodsWarehouseVOS = new ArrayList<>();
            for (TErpSupplierWarehousingBatch tErpSupplierWarehousingBatch : warehousingBatchesList) {
                TErpSupplierWarehousing tErpSupplierWarehousing = list.stream().filter(e -> e.getId().equals(tErpSupplierWarehousingBatch.getWarehousingId())).findFirst().get();
                TErpGoodsWarehouseVO tErpGoodsWarehouseVO = new TErpGoodsWarehouseVO();
                tErpGoodsWarehouseVO.setBatchNumber(tErpSupplierWarehousingBatch.getBatchNumber());
                tErpGoodsWarehouseVO.setProductionDate(tErpSupplierWarehousingBatch.getProductionDate());
                tErpGoodsWarehouseVO.setExpiryDate(tErpSupplierWarehousingBatch.getExpiryDate());
                TErpSupplierOutboundGoods tErpSupplierOutboundGoods1 = tErpSupplierOutboundGoods.stream().filter(t -> t.getWarehousingBatchId().equals(tErpSupplierWarehousingBatch.getId())).findFirst().orElse(null);
                if(tErpSupplierOutboundGoods1!=null){
                    tErpGoodsWarehouseVO.setNowNum(tErpSupplierWarehousingBatch.getWarehousingNumber()-tErpSupplierOutboundGoods1.getOutboundCount());
                }else {
                    tErpGoodsWarehouseVO.setNowNum(tErpSupplierWarehousingBatch.getWarehousingNumber());
                }
 
                tErpGoodsWarehouseVO.setUnitAmount(tErpSupplierWarehousing.getUnitAmount().multiply(new BigDecimal(tErpGoodsWarehouseVO.getNowNum())).setScale(2, RoundingMode.HALF_UP).doubleValue());
                tErpGoodsWarehouseVOS.add(tErpGoodsWarehouseVO);
            }
            tErpGoodsWarehouseLastVO.setAllNum(tErpGoodsWarehouseVOS.stream().mapToInt(TErpGoodsWarehouseVO::getNowNum).sum());
            tErpGoodsWarehouseLastVO.setAllTotalPrice(tErpGoodsWarehouseVOS.stream().mapToDouble(TErpGoodsWarehouseVO::getUnitAmount).sum());
            tErpGoodsWarehouseLastVO.setList(tErpGoodsWarehouseVOS);
            tErpGoodsWarehouseLastVO.setWarningInventory(goods.getWarningInventory());
            tErpGoodsWarehouseLastVOS.add(tErpGoodsWarehouseLastVO);
 
        }
        return tErpGoodsWarehouseLastVOS;
    }
 
    @Override
    public PageInfo<TErpGoodsWarehouseRecordLastVO> pageWarehouseList(TErpGoodsWarehouseQuery query, SysUser user) {
        TErpGoods goods = erpGoodsMapper.selectById(query.getGoodsId());
 
        PageInfo<TErpGoodsWarehouseRecordLastVO> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        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";
        }
        List<TErpGoodsWarehouseRecordLastVO> list = this.baseMapper.pageWarehouseList(query,pageInfo,user,sTime,eTime);
        for (TErpGoodsWarehouseRecordLastVO tErpGoodsWarehouseRecordLastVO : list) {
            ArrayList<TErpGoodsWarehouseVO> tErpGoodsWarehouseVOS = new ArrayList<>();
            if(tErpGoodsWarehouseRecordLastVO.getCategory()==1){
                List<TErpSupplierWarehousingBatch> tErpSupplierWarehousingBatches = erpSupplierWarehousingBatchMapper.selectList(new LambdaQueryWrapper<TErpSupplierWarehousingBatch>().eq(TErpSupplierWarehousingBatch::getWarehousingId, tErpGoodsWarehouseRecordLastVO.getId()));
                for (TErpSupplierWarehousingBatch tErpSupplierWarehousingBatch : tErpSupplierWarehousingBatches) {
                    TErpGoodsWarehouseVO tErpGoodsWarehouseVO = new TErpGoodsWarehouseVO();
                    tErpGoodsWarehouseVO.setBatchNumber(tErpSupplierWarehousingBatch.getBatchNumber());
                    tErpGoodsWarehouseVO.setProductionDate(tErpSupplierWarehousingBatch.getProductionDate());
                    tErpGoodsWarehouseVO.setExpiryDate(tErpSupplierWarehousingBatch.getExpiryDate());
                    tErpGoodsWarehouseVO.setNowNum(tErpSupplierWarehousingBatch.getWarehousingNumber());
                    tErpGoodsWarehouseVOS.add(tErpGoodsWarehouseVO);
                }
            }else {
                List<TErpSupplierOutboundGoods> tErpSupplierOutboundGoods = erpSupplierOutboundGoodsMapper.selectList(new LambdaQueryWrapper<TErpSupplierOutboundGoods>().eq(TErpSupplierOutboundGoods::getOutboundId, tErpGoodsWarehouseRecordLastVO.getId()));
                for (TErpSupplierOutboundGoods tErpSupplierOutboundGoods1 : tErpSupplierOutboundGoods) {
                    TErpGoodsWarehouseVO tErpGoodsWarehouseVO = new TErpGoodsWarehouseVO();
                    String warehousingBatchId = tErpSupplierOutboundGoods1.getWarehousingBatchId();
                    TErpSupplierWarehousingBatch tErpSupplierWarehousingBatch = erpSupplierWarehousingBatchMapper.selectById(warehousingBatchId);
                    tErpGoodsWarehouseVO.setBatchNumber(tErpSupplierWarehousingBatch.getBatchNumber());
                    tErpGoodsWarehouseVO.setExpiryDate(tErpSupplierWarehousingBatch.getExpiryDate());
                    tErpGoodsWarehouseVO.setProductionDate(tErpSupplierWarehousingBatch.getProductionDate());
                    tErpGoodsWarehouseVO.setNowNum(tErpSupplierOutboundGoods1.getOutboundCount());
                    tErpGoodsWarehouseVOS.add(tErpGoodsWarehouseVO);
                }
            }
            tErpGoodsWarehouseRecordLastVO.setAllNum(tErpGoodsWarehouseVOS.stream().mapToInt(TErpGoodsWarehouseVO::getNowNum).sum());
            tErpGoodsWarehouseRecordLastVO.setWarningInventory(goods.getWarningInventory());
            tErpGoodsWarehouseRecordLastVO.setList(tErpGoodsWarehouseVOS);
 
        }
        pageInfo.setRecords( list);
        return pageInfo;
    }
 
    @Override
    public void warehousingGoods(List<WarehousingGoodsDto> dtos, SysUser user) {
        for (WarehousingGoodsDto dto : dtos) {
            TErpSupplierWarehousing tErpSupplierWarehousing = new TErpSupplierWarehousing();
            // 当前年月日时分秒
            String time = DateUtils.dateTimeNow();
            tErpSupplierWarehousing.setWarehousingNo("G"+time);
            tErpSupplierWarehousing.setSupplierId(user.getUserId().toString());
            tErpSupplierWarehousing.setWarehouseId(dto.getWarehouseId());
            tErpSupplierWarehousing.setGoodsId(dto.getGoodsId());
            TErpGoods goods = erpGoodsMapper.selectById(dto.getGoodsId());
            tErpSupplierWarehousing.setGoodsName(goods.getGoodsName());
            tErpSupplierWarehousing.setGoodsCount(tErpSupplierWarehousing.getGoodsCount());
            tErpSupplierWarehousing.setUnitAmount(tErpSupplierWarehousing.getUnitAmount());
            tErpSupplierWarehousing.setTotalPrice(tErpSupplierWarehousing.getTotalPrice());
            erpSupplierWarehousingMapper.insert(tErpSupplierWarehousing);
            List<WarehousingGoodsNextDto> warehousingGoodsNextDtos = dto.getWarehousingGoodsNextDtos();
            for (WarehousingGoodsNextDto warehousingGoodsNextDto : warehousingGoodsNextDtos) {
                TErpSupplierWarehousingBatch tErpSupplierWarehousingBatch = new TErpSupplierWarehousingBatch();
                tErpSupplierWarehousingBatch.setWarehousingId(tErpSupplierWarehousing.getId());
                tErpSupplierWarehousingBatch.setWarehousingNumber(warehousingGoodsNextDto.getWarehousingNum());
                tErpSupplierWarehousingBatch.setBatchNumber(warehousingGoodsNextDto.getBatchNumber());
                tErpSupplierWarehousingBatch.setProductionDate(warehousingGoodsNextDto.getProductionDate().atStartOfDay());
                tErpSupplierWarehousingBatch.setExpiryDate(warehousingGoodsNextDto.getExpiryDate().atStartOfDay());
                erpSupplierWarehousingBatchMapper.insert(tErpSupplierWarehousingBatch);
            }
        }
 
    }
}