lin
2024-04-08 18118188e370e45f6e1bd43e5b33fd4c4a156655
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
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.pay.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import org.springblade.modules.pay.entity.OrderInfoEntity;
import org.springblade.modules.pay.entity.RefundInfoEntity;
import org.springblade.modules.pay.mapper.RefundInfoMapper;
import org.springblade.modules.pay.service.IOrderInfoService;
import org.springblade.modules.pay.service.IRefundInfoService;
import org.springblade.modules.pay.vo.RefundInfoVO;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
 
/**
 * 退款信息 服务实现类
 *
 * @author BladeX
 * @since 2024-04-07
 */
@Service
public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfoEntity> implements IRefundInfoService {
 
 
    @Resource
    private IOrderInfoService orderInfoService;
    @Override
    public IPage<RefundInfoVO> selectRefundInfoPage(IPage<RefundInfoVO> page, RefundInfoVO refundInfo) {
        return page.setRecords(baseMapper.selectRefundInfoPage(page, refundInfo));
    }
 
    @Override
    public RefundInfoEntity createRefundByOrderNo(String orderNo, String reason) {
 
        //根据订单号获取订单信息
        OrderInfoEntity orderInfo = orderInfoService.getOne(Wrappers.<OrderInfoEntity>lambdaQuery()
            .eq(OrderInfoEntity::getOrderNo, orderNo));
 
        //根据订单号生成退款订单
        RefundInfoEntity refundInfo = new RefundInfoEntity();
        refundInfo.setOrderNo(orderNo);//订单编号
        refundInfo.setRefundNo(getOrderNoStr());//退款单编号
        refundInfo.setTotalFee(orderInfo.getTotalFee());//原订单金额(分)
        refundInfo.setRefund(orderInfo.getTotalFee());//退款金额(分)
        refundInfo.setReason(reason);//退款原因
 
        //保存退款订单
        baseMapper.insert(refundInfo);
 
        return refundInfo;
    }
 
 
    @Override
    public void updateRefund(String content) {
        //将json字符串转换成Map
        Gson gson = new Gson();
        Map<String, String> resultMap = gson.fromJson(content, HashMap.class);
 
        //根据退款单编号修改退款单
        QueryWrapper<RefundInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("refund_no", resultMap.get("out_refund_no"));
 
        //设置要修改的字段
        RefundInfoEntity refundInfo = new RefundInfoEntity();
 
        refundInfo.setRefundId(resultMap.get("refund_id"));//微信支付退款单号
 
        //查询退款和申请退款中的返回参数
        if(resultMap.get("status") != null){
            refundInfo.setRefundStatus(resultMap.get("status"));//退款状态
            refundInfo.setContentReturn(content);//将全部响应结果存入数据库的content字段
        }
        //退款回调中的回调参数
        if(resultMap.get("refund_status") != null){
            refundInfo.setRefundStatus(resultMap.get("refund_status"));//退款状态
            refundInfo.setContentNotify(content);//将全部响应结果存入数据库的content字段
        }
 
        //更新退款单
        baseMapper.update(refundInfo, queryWrapper);
    }
 
 
    //生成订单号
    public static String getOrderNoStr() {
        String order = "refund" + new SimpleDateFormat("yyyyMMdd").format(new Date());
        Random r = new Random();
        for (int i = 0; i < 10; i++) {
            order += r.nextInt(9);
        }
        return order;
    }
}