3 天以前 f4d4d29368ccacb807f93e2033cd4a643a3ddade
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
package com.ruoyi.sales.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.approve.pojo.ApproveProcess;
import com.ruoyi.approve.service.impl.ApproveProcessServiceImpl;
import com.ruoyi.common.enums.FileNameType;
import com.ruoyi.common.enums.StockOutQualifiedRecordTypeEnum;
import com.ruoyi.other.service.impl.TempFileServiceImpl;
import com.ruoyi.procurementrecord.mapper.ReturnManagementMapper;
import com.ruoyi.procurementrecord.mapper.ReturnSaleProductMapper;
import com.ruoyi.procurementrecord.pojo.ReturnManagement;
import com.ruoyi.procurementrecord.pojo.ReturnSaleProduct;
import com.ruoyi.procurementrecord.service.impl.ReturnManagementServiceImpl;
import com.ruoyi.procurementrecord.utils.StockUtils;
import com.ruoyi.sales.dto.SalesLedgerProductDto;
import com.ruoyi.sales.dto.ShippingInfoDto;
import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
import com.ruoyi.sales.mapper.ShippingInfoDetailMapper;
import com.ruoyi.sales.mapper.ShippingInfoMapper;
import com.ruoyi.sales.pojo.SalesLedgerProduct;
import com.ruoyi.sales.pojo.ShippingInfo;
import com.ruoyi.sales.pojo.ShippingInfoDetail;
import com.ruoyi.sales.service.ShippingInfoDetailService;
import com.ruoyi.sales.service.ShippingInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @author :yys
 * @date : 2025/10/22 9:33
 */
@Service
@Slf4j
public class ShippingInfoServiceImpl extends ServiceImpl<ShippingInfoMapper, ShippingInfo> implements ShippingInfoService {
 
    @Autowired
    private ShippingInfoMapper shippingInfoMapper;
 
    @Autowired
    private SalesLedgerProductMapper salesLedgerProductMapper;
    @Autowired
    private StockUtils stockUtils;
    @Autowired
    private CommonFileServiceImpl commonFileService;
 
    @Autowired
    private ApproveProcessServiceImpl approveProcessService;
 
    @Autowired
    private ShippingInfoDetailMapper shippingInfoDetailMapper;
 
    @Autowired
    private ShippingInfoDetailService shippingInfoDetailService;
 
    @Autowired
    private ReturnManagementMapper returnManagementMapper;
 
    @Autowired
    private ReturnSaleProductMapper returnSaleProductMapper;
 
    @Autowired
    private ReturnManagementServiceImpl returnManagementService;
 
    @Override
    public List<SalesLedgerProductDto> getReturnManagementDtoById(Long shippingId) {
        return shippingInfoMapper.getReturnManagementDtoById(shippingId );
 
    }
 
    /**
     * 获取销售产品单的已发货数量
     */
    @Override
    public Map<String, BigDecimal> getNumberOfSalesLedgerProduct(Long salesLedgerProductId) {
        Map<String, BigDecimal> map = new HashMap<>();
        map.put("shippingNum",BigDecimal.ZERO); // 发货数量
        map.put("returnNum",BigDecimal.ZERO); // 退货数量
        List<ShippingInfo> shippingInfos = shippingInfoMapper.selectList(new LambdaQueryWrapper<ShippingInfo>()
                .eq(ShippingInfo::getSalesLedgerProductId, salesLedgerProductId)
                // 只筛选“发货中”和“已发货”两种状态
                .in(ShippingInfo::getStatus, "发货中", "已发货"));
        if(CollectionUtils.isEmpty(shippingInfos)){
            return map;
        }
        List<ShippingInfoDetail> shippingInfoDetails = shippingInfoDetailMapper.selectList(new LambdaQueryWrapper<ShippingInfoDetail>()
                .in(ShippingInfoDetail::getShippingInfoId, shippingInfos.stream().map(ShippingInfo::getId).collect(Collectors.toList())));
        if(CollectionUtils.isEmpty(shippingInfoDetails)){
            return map;
        }
        // 获取发货数量
        map.put("shippingNum",shippingInfoDetails.stream().map(ShippingInfoDetail::getShippingNum).reduce(BigDecimal.ZERO, BigDecimal::add));
        // 获取退货数量
        List<ReturnSaleProduct> returnSaleProducts = returnSaleProductMapper.selectList(new LambdaQueryWrapper<ReturnSaleProduct>()
                .eq(ReturnSaleProduct::getReturnSaleLedgerProductId, salesLedgerProductId)
                .eq(ReturnSaleProduct::getStatus,1));
        if(CollectionUtils.isEmpty(returnSaleProducts)){
            return map;
        }
        map.put("returnNum",returnSaleProducts.stream().map(ReturnSaleProduct::getNum).reduce(BigDecimal.ZERO, BigDecimal::add));
        return map;
    }
 
    @Override
    public ShippingInfoDto getDateil(Long id) {
        ShippingInfoDto shippingInfoDto =  shippingInfoMapper.getDateil(id);
        if(shippingInfoDto != null){
            assembleDetails(shippingInfoDto);
        }
        return shippingInfoDto;
    }
 
    @Override
    public List<ShippingInfo> getShippingInfoByCustomerName(String customerName) {
        return shippingInfoMapper.getShippingInfoByCustomerName(customerName);
    }
 
    @Override
    public IPage<ShippingInfoDto> listPage(Page page, ShippingInfo req) {
        IPage<ShippingInfoDto> listPage = shippingInfoMapper.listPage(page, req);
        listPage.getRecords().forEach(this::assembleDetails);
        return listPage;
    }
 
 
    /**
     * 封装详情
     * @param item
     */
    public void assembleDetails(ShippingInfoDto item) {
        item.setCommonFileList(commonFileService.getFileListByBusinessId(item.getId(), FileNameType.SHIP.getValue()));
        List<ShippingInfoDetail> shippingInfoDetails = shippingInfoDetailMapper.selectList(new LambdaQueryWrapper<ShippingInfoDetail>()
                .eq(ShippingInfoDetail::getShippingInfoId, item.getId()));
        // 核心优化:层层防护空指针 + 处理空集合场景
        item.setShippingSuccessTotal(Optional.ofNullable(shippingInfoDetails)
                .orElse(Collections.emptyList())
                .stream()
                .filter(Objects::nonNull)
                .map(ShippingInfoDetail::getShippingNum)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add)); // 用Lambda替代方法引用
        item.setWaitShippingTotal(item.getShippingTotal().subtract(item.getShippingSuccessTotal()));
        // 查询退货数量
        if(CollectionUtils.isNotEmpty(shippingInfoDetails)){
            item.setReturnTotal(returnManagementService.getReturnSaleProductCountByShippingId(shippingInfoDetails
                    .stream()
                    .map(ShippingInfoDetail::getId)
                    .collect(Collectors.toList())));
        }
    }
 
    @Override
    public boolean deductStock(ShippingInfoDto req) throws IOException {
        ShippingInfo byId = this.getById(req.getId());
        if (byId == null) {
            throw new RuntimeException("发货信息不存在");
        }
        //扣减库存
        SalesLedgerProduct salesLedgerProduct = salesLedgerProductMapper.selectById(byId.getSalesLedgerProductId());
        stockUtils.substractStock(salesLedgerProduct.getProductModelId(), req.getShippingTotal(), StockOutQualifiedRecordTypeEnum.SALE_SHIP_STOCK_OUT.getCode(), req.getId());
        byId.setExpressNumber(req.getExpressNumber());
        byId.setExpressCompany(req.getExpressCompany());
        byId.setStatus(req.getStatus());
        byId.setShippingCarNumber(req.getShippingCarNumber());
        return this.updateById(byId);
    }
 
    @Override
    public boolean delete(List<Long> ids) {
        List<ShippingInfo> shippingInfos = shippingInfoMapper.selectList(new LambdaQueryWrapper<ShippingInfo>()
                .in(ShippingInfo::getId, ids));
        if(CollectionUtils.isEmpty(shippingInfos)) return false;
        // 删除子表
        List<ShippingInfoDetail> shippingInfoDetails = shippingInfoDetailMapper.selectList(new LambdaQueryWrapper<ShippingInfoDetail>()
                .in(ShippingInfoDetail::getShippingInfoId, ids));
        if(CollectionUtils.isNotEmpty(shippingInfoDetails)){
            shippingInfoDetailService.delete(shippingInfoDetails.stream().map(ShippingInfoDetail::getId).collect(Collectors.toList()));
        }
        // 扣已发货库存
        for (ShippingInfo shippingInfo : shippingInfos) {
            if("已发货".equals(shippingInfo.getStatus())) {
                stockUtils.deleteStockOutRecord(shippingInfo.getId(), StockOutQualifiedRecordTypeEnum.SALE_SHIP_STOCK_OUT.getCode());
            }
        }
        // 删除发货审批
        if(CollectionUtils.isNotEmpty(shippingInfos)){
            for (ShippingInfo shippingInfo : shippingInfos){
                ApproveProcess one = approveProcessService.getOne(new LambdaQueryWrapper<ApproveProcess>()
                        .like(ApproveProcess::getApproveReason, shippingInfo.getShippingNo()));
                if(one != null){
                    approveProcessService.delByIds(Collections.singletonList(one.getId()));
                }
            }
        }
 
        return this.removeBatchByIds(ids);
    }
}