Pu Zhibing
2025-02-08 d94776ba25ffcd19cdd7970048ed88d9212bd394
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package com.ruoyi.order.service.impl;
 
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kuaidi100.sdk.contant.CompanyConstant;
import com.ruoyi.account.api.feignClient.AppUserClient;
import com.ruoyi.account.api.model.AppUser;
import com.ruoyi.account.api.model.UserAddress;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.order.mapper.RefundPassMapper;
import com.ruoyi.order.model.Order;
import com.ruoyi.order.model.RefundPass;
import com.ruoyi.order.service.OrderService;
import com.ruoyi.order.service.RefundPassService;
import com.ruoyi.order.util.ExpressDeliveryUtil;
import com.ruoyi.order.util.vo.MapTrackKD100Vo;
import com.ruoyi.order.vo.*;
import com.ruoyi.other.api.domain.BaseSetting;
import com.ruoyi.other.api.domain.SystemConfig;
import com.ruoyi.other.api.feignClient.BaseSettingClient;
import com.ruoyi.other.api.feignClient.ShopClient;
import com.ruoyi.other.api.feignClient.SystemConfigClient;
import com.ruoyi.other.api.feignClient.TechnicianClient;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.feignClient.SysUserClient;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author luodangjia
 * @since 2024-11-21
 */
@Service
public class RefundPassServiceImpl extends ServiceImpl<RefundPassMapper, RefundPass> implements RefundPassService {
    
    @Resource
    private OrderService orderService;
    
    @Resource
    private BaseSettingClient baseSettingClient;
    @Resource
    private AppUserClient appUserClient;
    @Resource
    private TokenService tokenService;
    
    @Resource
    private SysUserClient sysUserClient;
    
    @Resource
    private RedisTemplate redisTemplate;
    
    @Resource
    private SystemConfigClient systemConfigClient;
    
    
    
    
    
    /**
     * 申请售后
     * @param applyRefundPass
     * @return
     */
    @Override
    public R applyRefundPass(ApplyRefundPass applyRefundPass) {
        RefundPass one = this.getOne(new LambdaQueryWrapper<RefundPass>().eq(RefundPass::getOrderId, applyRefundPass.getId()).eq(RefundPass::getDelFlag, 0)
                .ne(RefundPass::getStatus, 3));
        if(null != one){
            return R.fail("不能重复提交售后");
        }
        Order order = orderService.getById(applyRefundPass.getId());
        //判断是都已经超过售后时间
        LocalDateTime afterSaleTime = order.getAfterSaleTime();
        if(null != afterSaleTime && LocalDateTime.now().isAfter(afterSaleTime)){
            return R.fail("已超过售后期间");
        }
        //构建售后申请数据
        RefundPass refundPass = new RefundPass();
        refundPass.setOrderId(applyRefundPass.getId());
        refundPass.setStatus(1);
        refundPass.setRefundMethod(applyRefundPass.getRefundMethod());
        refundPass.setRefundReason(applyRefundPass.getRefundReason());
        refundPass.setUserRemark(applyRefundPass.getUserRemark());
        refundPass.setPics(applyRefundPass.getPics());
        refundPass.setPassStatus(1);
        refundPass.setDelFlag(0);
        refundPass.setCreateTime(LocalDateTime.now());
        BaseSetting baseSetting = baseSettingClient.getBaseSetting(5).getData();
        JSONObject jsonObject = JSON.parseObject(baseSetting.getContent());
        refundPass.setName(jsonObject.getString("name"));
        refundPass.setPhone(jsonObject.getString("phone"));
        refundPass.setAddress(jsonObject.getString("address"));
        this.save(refundPass);
        order.setOldOrderStatus(order.getOrderStatus());
        order.setOrderStatus(7);
        orderService.updateById(order);
        return R.ok();
    }
    
    
    /**
     * 管理后台获取售后管理列表数据
     * @return
     */
    @Override
    public PageInfo<OrderRefundPassList> getOrderRefundPassList(OrderRefundPassListVo refundPassListVo) {
        Long userid = tokenService.getLoginUser().getUserid();
        SysUser sysUser = sysUserClient.getSysUser(userid).getData();
        Integer shopId = null;
        if(2 == sysUser.getRoleType()){
            shopId = sysUser.getObjectId();
        }
        //搜索条件,用户姓名和电话
        if(StringUtils.isNotEmpty(refundPassListVo.getUserName()) || StringUtils.isNotEmpty(refundPassListVo.getPhone())){
        }
 
        //搜索条件,用户姓名
        if(StringUtils.isNotEmpty(refundPassListVo.getName())){
            List<AppUser> data = appUserClient.getAppUserByNameNoFilter(refundPassListVo.getName()).getData();
            List<Long> collect = data.stream().map(AppUser::getId).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(collect)){
                return new PageInfo<>();
            }
            if(null != refundPassListVo.getAppUserIds()){
                List<Long> appUserIds = refundPassListVo.getAppUserIds();
                appUserIds.addAll(collect);
                refundPassListVo.setAppUserIds(appUserIds);
            }else{
                refundPassListVo.setAppUserIds(collect);
            }
        }
        //搜索条件,用户电话
        if(StringUtils.isNotEmpty(refundPassListVo.getPhone())){
            List<AppUser> data = appUserClient.getAppUserByPhoneNoFilter(refundPassListVo.getPhone()).getData();
            List<Long> collect = data.stream().map(AppUser::getId).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(collect)){
                return new PageInfo<>();
            }
 
            if(null != refundPassListVo.getAppUserIds()){
                List<Long> appUserIds = refundPassListVo.getAppUserIds();
                if (!containsAny(appUserIds,collect)) {
                    return new PageInfo<>();
                }
                appUserIds.addAll(collect);
                refundPassListVo.setAppUserIds(appUserIds);
            }else{
                refundPassListVo.setAppUserIds(collect);
            }
        }
        if (null != refundPassListVo.getAppUserIds()){
            refundPassListVo.setAppUserIds(refundPassListVo.getAppUserIds().stream().distinct().collect(Collectors.toList()));
        }
 
 
 
        PageInfo<OrderRefundPassList> pageInfo = new PageInfo(refundPassListVo.getPageCurr(), refundPassListVo.getPageSize());
        List<OrderRefundPassList> orderRefundPassList = this.baseMapper.getOrderRefundPassList(pageInfo, refundPassListVo.getCode(), refundPassListVo.getAppUserIds(), shopId, refundPassListVo.getRefundMethod(), refundPassListVo.getStatus());
        for (OrderRefundPassList refundPassList : orderRefundPassList) {
            AppUser appUser = appUserClient.getAppUserById(refundPassList.getAppUserId());
            if(null != appUser){
                refundPassList.setUserName(appUser.getName());
                refundPassList.setPhone(appUser.getPhone());
            }
        }
        return pageInfo.setRecords(orderRefundPassList);
    }
 
    /**
     * 判断 list1 是否包含 list2 中的至少一个元素
     *
     * @param list1 第一个列表
     * @param list2 第二个列表
     * @return 如果 list1 包含 list2 中的至少一个元素,返回 true;否则返回 false
     */
    private boolean containsAny(List<Long> list1, List<Long> list2) {
        // 将 list1 转换为 HashSet 以提高查询效率
        Set<Long> set1 = new HashSet<>(list1);
 
        // 遍历 list2,检查是否有元素存在于 set1 中
        for (Long element : list2) {
            if (set1.contains(element)) {
                return true;
            }
        }
 
        // 如果没有找到共同元素,返回 false
        return false;
    }
 
//    public void processAppUserIds(List<Long> appUserIds , String searchKey, Function<String, R<List<AppUser>>> userSearchFunction){
//        if (StringUtils.isNotEmpty(searchKey)) {
//            List<Long> userIds = Optional.ofNullable(userSearchFunction.apply(searchKey).getData())
//                    .orElse(Collections.emptyList())
//                    .stream()
//                    .map(AppUser::getId)
//                    .collect(Collectors.toList());
//
//            if (CollectionUtils.isEmpty(userIds)) {
//                return;
//            }
//
//            List<Long> existingUserIds = orderPageList.getAppUserIds();
//            if (existingUserIds != null) {
//                if (!containsAny(existingUserIds, userIds)) {
//                    return;
//                }
//                existingUserIds.addAll(userIds);
//            } else {
//                orderPageList.setAppUserIds(userIds);
//            }
//        }
//    }
    
    
    /**
     * 售后订单审核操作
     * @param id
     * @param status
     * @param passRemark
     * @return
     */
    @Override
    public R authPassStatus(Long id, Integer status, String passRemark) {
        RefundPass refundPass = this.getById(id);
        if(refundPass.getPassStatus() != 1){
            return R.fail("不能重复操作");
        }
        refundPass.setPassStatus(status);
        refundPass.setAuthTime(LocalDateTime.now());
        //退货退款
        if(refundPass.getRefundMethod() == 1 && 2 == status){
            refundPass.setStatus(4);
        }
        if(refundPass.getRefundMethod() == 2 && 2 == status){
            refundPass.setStatus(2);
        }
        if(3 == status){
            refundPass.setStatus(3);
        }
        refundPass.setPassRemark(passRemark);
        //仅退款的售后需要将支付金额原路返回,然后再扣减支付获得的积分
        if(refundPass.getRefundMethod() == 2 && 2 == status){
            Order order = orderService.getById(refundPass.getOrderId());
            order.setOrderStatus(6);
            //返回订单支付金额和回退积分和会员等级
            R r = orderService.refundPayMoney(order);
            if(200 != r.getCode()){
                return r;
            }
            orderService.updateById(order);
        }
        this.updateById(refundPass);
        return R.ok();
    }
    
    
    /**
     * 售后确认收货操作
     * @param id
     * @return
     */
    @Override
    public R refundPassReceive(Long id) {
        RefundPass refundPass = this.getById(id);
        if(refundPass.getPassStatus() != 2){
            return R.fail("操作失败");
        }
        if(refundPass.getStatus() != 5){
            return R.fail("操作失败");
        }
        refundPass.setStatus(2);
        refundPass.setReceiveTime(LocalDateTime.now());
        //仅退款的售后需要将支付金额原路返回,然后再扣减支付获得的积分
        Order order = orderService.getById(refundPass.getOrderId());
        order.setOrderStatus(6);
        //返回订单支付金额和回退积分和会员等级
        R r = orderService.refundPayMoney(order);
        if(200 != r.getCode()){
            return r;
        }
        this.updateById(refundPass);
        orderService.updateById(order);
        return R.ok();
    }
    
    
    /**
     * 获取售后详情
     * @param id
     * @return
     */
    @Override
    public RefundPassInfo getRefundPassInfo(Long id) {
        RefundPass refundPass = this.getById(id);
        OrderInfoVo orderInfo = orderService.getOrderInfo(refundPass.getOrderId());
        RefundPassInfo refundPassInfo = new RefundPassInfo();
        BeanUtils.copyBeanProp(refundPassInfo, orderInfo);
        refundPassInfo.setId(id.toString());
        refundPassInfo.setPassStatus(refundPassInfo.getPassStatus());
        refundPassInfo.setPassRemark(refundPassInfo.getPassRemark());
        refundPassInfo.setPassCreateTime(refundPass.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        refundPassInfo.setRefundMethod(refundPass.getRefundMethod());
        refundPassInfo.setRefundReason(refundPass.getRefundReason());
        refundPassInfo.setUserRemark(refundPass.getUserRemark());
        refundPassInfo.setPics(refundPass.getPics());
        refundPassInfo.setPassStatus(refundPass.getPassStatus());
        refundPassInfo.setPassRemark(refundPass.getPassRemark());
        refundPassInfo.setStatus(refundPass.getStatus());
        String code = refundPass.getCode();
        if(StringUtils.isNotEmpty(code)){
            JSONObject jsonObject = JSON.parseObject(code);
            refundPassInfo.setExpressCode(jsonObject.getString("num"));
            String com = jsonObject.getString("com");
            refundPassInfo.setExpressName(com);
        }
        return refundPassInfo;
    }
    
    
    /**
     * 定时查询快递信息
     */
    @Override
    public void taskExpress() {
        Set<Long> order_express = redisTemplate.opsForZSet().rangeByScore("order_refund_express", 0, LocalDateTime.now().toEpochSecond(ZoneOffset.UTC));
        if(order_express.size() > 0){
            for (Long id : order_express) {
                RefundPass refundPass = this.getById(id);
                Order order = orderService.getById(refundPass.getOrderId());
                if(refundPass.getPassStatus() != 5){
                    redisTemplate.opsForZSet().remove("order_refund_express", id);
                    continue;
                }
                String expressJson = refundPass.getCode();
                if(StringUtils.isEmpty(expressJson)){
                    redisTemplate.opsForZSet().remove("order_refund_express", id);
                    continue;
                }
                //{"com":"jd","num":"JDV016336234367"}
                JSONObject jsonObject1 = JSON.parseObject(refundPass.getCode());
                String com = jsonObject1.getString("com");
                String num = jsonObject1.getString("num");
                UserAddress userAddress = JSON.parseObject(order.getAddressJson(), UserAddress.class);
                MapTrackKD100Vo mapTrackKD100Vo = ExpressDeliveryUtil.kd100MapTrack(com, num, userAddress.getProvince() + userAddress.getCity(), order.getDeliverProvince() + order.getDeliverCity());
                refundPass.setExpressResult(JSON.toJSONString(mapTrackKD100Vo));
                this.updateById(refundPass);
                //延长时间x小时
                SystemConfig systemConfig = systemConfigClient.getSystemConfig(3).getData();
                JSONObject jsonObject2 = JSON.parseObject(systemConfig.getContent());
                Integer waitTime = jsonObject2.getInteger("waitTime");
                redisTemplate.opsForZSet().add("order_refund_express", refundPass.getId(), LocalDateTime.now().plusHours(waitTime).toEpochSecond(ZoneOffset.UTC));
            }
        }
    }
}