xin
2025-08-21 227b029030818925089cbe91dc1288594a5d4aa9
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
package com.oying.modules.sh.service.impl;
 
import com.oying.exception.BadRequestException;
import com.oying.modules.hwc.service.SwiftPassService;
import com.oying.modules.sh.domain.Order;
import com.oying.modules.sh.domain.OrderProductSnapshot;
import com.oying.modules.sh.domain.OrderReturn;
import com.oying.modules.sh.domain.OrderReturnProductSnapshot;
import com.oying.modules.sh.domain.request.AuditOrderReturn;
import com.oying.modules.sh.domain.request.ProductOrder;
import com.oying.modules.sh.domain.request.ReturnOrder;
import com.oying.modules.sh.domain.vo.OrderResponse;
import com.oying.modules.sh.domain.vo.OrderReturnResponse;
import com.oying.modules.sh.service.*;
import com.oying.utils.*;
import com.oying.utils.enums.GenerateEnum;
import com.oying.utils.enums.OrderStatusEnum;
import com.oying.utils.enums.PayTypeEnum;
import com.oying.utils.enums.ReturnAuditEnum;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oying.modules.sh.domain.dto.OrderReturnQueryCriteria;
import com.oying.modules.sh.mapper.OrderReturnMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.sql.Timestamp;
import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
 
/**
 * @author lixin
 * @description 服务实现
 * @date 2025-06-11
 **/
@Service
@RequiredArgsConstructor
public class OrderReturnServiceImpl extends ServiceImpl<OrderReturnMapper, OrderReturn> implements OrderReturnService {
 
    private final OrderReturnMapper orderReturnMapper;
    private final OrderOperationLogService operationLogService;
    private final OrderService orderService;
    private final OrderReturnProductSnapshotService productSnapshotService;
    private final RedisUtils redisUtils;
    private final SwiftPassService swiftPassService;
    private final OrderAddressSnapshotService addressSnapshotService;
    private static final String ORDER_RETURN_KEY = "oying:order:refund";
    private static final String ORDER_CODE = "REFUND";
    public static final Integer DAY = 30;
 
    @Override
    public PageResult<OrderReturn> queryAll(OrderReturnQueryCriteria criteria, Page<Object> page) {
        criteria.setOffset(page.offset());
        List<OrderReturn> list = orderReturnMapper.findAll(criteria, criteria.getBlurry());
        Long total = orderReturnMapper.countAll(criteria, criteria.getBlurry());
        return PageUtil.toPage(list, total);
    }
 
    @Override
    public List<OrderReturn> queryAll(OrderReturnQueryCriteria criteria) {
        return orderReturnMapper.findAll(criteria, criteria.getBlurry());
    }
 
    @Override
    public OrderReturnResponse getByReturnNum(String returnNum) {
        OrderReturn orderReturn = orderReturnMapper.getByReturnNum(returnNum);
        return new OrderReturnResponse(orderReturn,
                addressSnapshotService.queryByOrderNum(orderReturn.getOrderNum()),
                operationLogService.getByOrderNum(orderReturn.getOrderNum(), ORDER_CODE));
    }
 
    @Override
    public void updatePayStatus(String returnNum, String refundStatus, String successTime) {
        orderReturnMapper.updatePayStatus(returnNum, refundStatus, successTime);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized void create(ReturnOrder resources) {
        Order order = orderService.queryByOrderNum(resources.getOrderNum());
        if (order == null) {
            throw new BadRequestException("订单不存在!");
        }
        if (Objects.equals(order.getOrderStatus(), OrderStatusEnum.ZERO.getKey())) {
            throw new BadRequestException("订单未支付");
        }
        OrderReturn returnOrder1 = orderReturnMapper.getByOrderNum(resources.getOrderNum(), ReturnAuditEnum.ZERO.getKey());
        if (returnOrder1 != null) {
            throw new BadRequestException("退款订单已提交");
        }
        OrderReturn returnOrder2 = orderReturnMapper.getByOrderNum(resources.getOrderNum(), ReturnAuditEnum.TWO.getKey());
        if (returnOrder2 != null) {
            throw new BadRequestException("退款订单已处理");
        }
        if (!DateUtil.isBefore(order.getPayTime(), DAY)) {
            throw new BadRequestException("订单已超过售后有效期");
        }
        // 退款订单
        OrderReturn returnOrder = new OrderReturn();
        // 退款订单号
        String returnNum = redisUtils.generateSn(ORDER_RETURN_KEY, GenerateEnum.ORDER_RETURN.getKey());
        returnOrder.setReturnNum(returnNum);
        returnOrder.setReturnStatus(ReturnAuditEnum.ZERO.getKey());
        returnOrder.setReturnStatusDescribe(ReturnAuditEnum.ZERO.getValue());
        returnOrder.setOrderNum(resources.getOrderNum());
        returnOrder.setOrderTime(order.getOrderTime());
        returnOrder.setOrderStoreNum(order.getOrderStoreNum());
        returnOrder.setPackagingPrice(order.getPackagingPrice());
        returnOrder.setSendPrice(order.getSendPrice());
        returnOrder.setSendType(order.getSendType() != null ? order.getSendType() : null);
        returnOrder.setRiderId(order.getRiderId() != null ? order.getRiderId() : null);
        returnOrder.setRiderPhone(order.getRiderPhone() != null ? order.getRiderPhone() : null);
        returnOrder.setRiderName(order.getRiderName() != null ? order.getRiderName() : null);
        returnOrder.setPayType(order.getPayType());
        returnOrder.setUserId(order.getUserId());
        returnOrder.setUsername(order.getUsername());
        returnOrder.setStoreId(order.getStoreId());
        returnOrder.setStoreName(order.getStoreName());
        returnOrder.setStoreLogo(order.getStoreLogo());
        returnOrder.setOriginalPrice(order.getOriginalPrice());
        returnOrder.setPaidPrice(order.getPaidPrice());
        returnOrder.setActuallyPayPrice(order.getActuallyPayPrice());
        returnOrder.setReason(resources.getReason());
        returnOrder.setRemark(resources.getRemark());
        returnOrder.setPhotos(resources.getPhotos());
        returnOrder.setAuditStatus(ReturnAuditEnum.ZERO.getKey());
        orderReturnMapper.insert(returnOrder);
        List<OrderReturnProductSnapshot> productSnapshots = new ArrayList<>();
        for (ProductOrder productOrder : resources.getProductOrders()) {
            for (OrderProductSnapshot snapshot : order.getProductSnapshots()) {
                if (productOrder.getProductId().equals(snapshot.getProductId())) {
                    OrderReturnProductSnapshot productSnapshot = getSnapshot(snapshot, returnNum);
                    productSnapshots.add(productSnapshot);
                }
            }
        }
        productSnapshotService.saveBatch(productSnapshots);
    }
 
    private static OrderReturnProductSnapshot getSnapshot(OrderProductSnapshot snapshot, String returnNum) {
        OrderReturnProductSnapshot productSnapshot = new OrderReturnProductSnapshot();
        productSnapshot.setReturnNum(returnNum);
        productSnapshot.setStoreId(snapshot.getStoreId());
        productSnapshot.setProductId(snapshot.getProductId());
        productSnapshot.setProductCode(snapshot.getProductCode() != null ? snapshot.getProductCode() : null);
        productSnapshot.setProductBarcode(snapshot.getProductBarcode());
        productSnapshot.setProductName(snapshot.getProductName());
        productSnapshot.setProductTitle(snapshot.getProductTitle());
        productSnapshot.setProductMainImage(snapshot.getProductMainImage());
        productSnapshot.setProductDescription(snapshot.getProductDescription());
        productSnapshot.setParamData(snapshot.getParamData() != null ? snapshot.getParamData() : null);
        productSnapshot.setUnitPrice(snapshot.getUnitPrice());
        productSnapshot.setDetailCount(snapshot.getDetailCount());
        productSnapshot.setOriginalPrice(snapshot.getOriginalPrice());
        productSnapshot.setPaidPrice(snapshot.getPaidPrice());
        productSnapshot.setActuallyPayPrice(snapshot.getActuallyPayPrice());
        return productSnapshot;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cancel(String returnNum) {
        OrderReturn orderReturn = orderReturnMapper.getByReturnNum(returnNum);
        if (orderReturn == null) {
            throw new BadRequestException("订单不存在!");
        }
        if (!orderReturn.getUserId().equals(SecurityUtils.getCurrentUserId())) {
            throw new BadRequestException("不能修改他人订单");
        }
        orderReturnMapper.updateStatus(returnNum, OrderStatusEnum.FIFTEEN.getKey(), OrderStatusEnum.FIFTEEN.getValue(),
                ReturnAuditEnum.THREE.getKey());
        OrderResponse response = orderService.getByOrderNum(orderReturn.getOrderNum());
        operationLogService.create(response, OrderStatusEnum.FIFTEEN, null);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(OrderReturn resources) {
        OrderReturn orderReturn = getById(resources.getReturnId());
        orderReturn.copy(resources);
        orderReturnMapper.updateById(orderReturn);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized void audit(AuditOrderReturn resources) {
        OrderReturn orderReturn = orderReturnMapper.getByReturnNum(resources.getReturnNum());
        if (orderReturn == null) {
            throw new BadRequestException("订单不存在!");
        }
        if (!Objects.equals(orderReturn.getReturnStatus(), ReturnAuditEnum.ZERO.getKey())) {
            throw new BadRequestException("订单已处理或取消");
        }
        if (resources.getAmount().compareTo(orderReturn.getActuallyPayPrice()) > 0) {
            throw new BadRequestException("退款金额超过订单金额");
        }
        switch (resources.getAuditStatus()) {
            case ONE:
                break;
            case TWO:
                long total = BigDecimalUtils.yuanToCents(orderReturn.getActuallyPayPrice());
                long refund = BigDecimalUtils.yuanToCents(resources.getAmount());
                orderReturn.setRefundPrice(resources.getAmount());
                PayTypeEnum status = PayTypeEnum.find(orderReturn.getPayType());
                switch (status) {
                    case HWC:
                    case HWC2:
                        if (refund > 0) {
                            Map<String, String> re = swiftPassService.refund(orderReturn.getReturnNum(), orderReturn.getOrderNum(),
                                    resources.getMessage(), refund, total, status);
                            orderReturn.setChannel(re.get("refund_channel"));
                        }
                        break;
                    default:
                        throw new BadRequestException("暂未开通其余支付模式");
                }
                break;
            default:
                throw new BadRequestException("审核状态错误");
        }
        // 审核状态
        orderReturn.setAuditStatus(resources.getAuditStatus().getKey());
        // 审核人
        orderReturn.setAuditUser(SecurityUtils.getCurrentUsername());
        // 审核时间
        orderReturn.setAuditTime(new Timestamp(System.currentTimeMillis()));
        // 审核信息
        orderReturn.setAuditMessage(resources.getMessage());
        orderReturnMapper.updateById(orderReturn);
    }
 
    @Override
    public void download(List<OrderReturn> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (OrderReturn orderReturn : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("退单号", orderReturn.getReturnNum());
            map.put("订单号", orderReturn.getOrderNum());
            map.put("用户", orderReturn.getUserId());
            map.put("门店ID", orderReturn.getStoreId());
            map.put("原金额", orderReturn.getOriginalPrice());
            map.put("折扣价", orderReturn.getPaidPrice());
            map.put("实付金额", orderReturn.getActuallyPayPrice());
            map.put("退款价格", orderReturn.getRefundPrice());
            map.put("退款状态", orderReturn.getRefundStatus());
            map.put("退款成功时间", orderReturn.getSuccessTime());
            map.put("退款渠道", orderReturn.getChannel());
            map.put("退货类型", orderReturn.getReason());
            map.put("备注", orderReturn.getRemark());
            map.put("图片", orderReturn.getPhotos());
            map.put("审核状态", ReturnAuditEnum.getValue(orderReturn.getAuditStatus()));
            map.put("审核人", orderReturn.getAuditUser());
            map.put("审核时间", orderReturn.getAuditTime());
            map.put("审核信息", orderReturn.getAuditMessage());
            map.put("创建人", orderReturn.getCreateBy());
            map.put("创建时间", orderReturn.getCreateTime());
            map.put("修改者", orderReturn.getUpdateBy());
            map.put("修改时间", orderReturn.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}