liujie
2026-02-27 128b41b50a0524304f4a586d64ec105a050a95fc
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
package com.ruoyi.web.controller.api;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.BaseModel;
import com.ruoyi.common.core.domain.BasePage;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.OrderNos;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.dto.BuyInsuranceDto;
import com.ruoyi.system.model.*;
import com.ruoyi.system.query.GetCourseListQuery;
import com.ruoyi.system.service.*;
import com.ruoyi.system.vo.UserInsuranceVo;
import com.ruoyi.system.wxPay.utils.WxRefundUtil;
import com.ruoyi.system.wxPay.utils.WxV3Pay;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import javax.validation.Valid;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import java.util.Objects;
 
@Slf4j
@RestController
@RequestMapping("/insurance")
@Api(tags = "保险模块")
public class InsuranceController {
 
    private final TInsuranceOrderService tInsuranceOrderService;
    private final TokenService tokenService;
    private final TInsuranceService tInsuranceService;
    private final WxV3Pay wxV3Pay;
    private final TAppUserService  tAppUserService;
    private final TCourseOrderService tCourseOrderService;
 
 
    public InsuranceController(TInsuranceOrderService tInsuranceOrderService, TokenService tokenService, TInsuranceService tInsuranceService, WxV3Pay wxV3Pay, TAppUserService tAppUserService, TCourseOrderService tCourseOrderService) {
        this.tInsuranceOrderService = tInsuranceOrderService;
        this.tokenService = tokenService;
        this.tInsuranceService = tInsuranceService;
        this.wxV3Pay = wxV3Pay;
        this.tAppUserService = tAppUserService;
        this.tCourseOrderService = tCourseOrderService;
    }
 
 
    /**
     * 获取是否支付了保险费 是否到期
     */
    @GetMapping("/getInsurance")
    @ApiOperation(value = "保险模块--获取用户是否有保险",response = TBanner.class)
    public R<UserInsuranceVo> getCourseList(int type) {
        String userId = tokenService.getLoginUserApplet().getUserId();
        UserInsuranceVo insuranceVo = new UserInsuranceVo();
        TInsuranceOrder one = tInsuranceOrderService.getOne(Wrappers.<TInsuranceOrder>lambdaQuery().eq(TInsuranceOrder::getType,type).eq(TInsuranceOrder::getAppUserId, userId).eq(TInsuranceOrder::getStatus, 2).orderByDesc(BaseModel::getCreateTime).last("LIMIT 1"));
        if(one!=null){
            insuranceVo.setUserId(userId);
            insuranceVo.setPayTime(one.getPayTime());
            insuranceVo.setEndTime(one.getEndTime());
        }
        return R.ok(insuranceVo);
    }
 
 
 
    @PostMapping("/buyInsurance")
    @ApiOperation(value = "保险模块--购买保险")
    public R<?> buyInsurance(@RequestBody @Valid BuyInsuranceDto dto) throws ParseException {
        String userId = tokenService.getLoginUserApplet().getUserId();
        TAppUser appUser = tAppUserService.getById(userId);
 
        // 查询保险订单
        TInsuranceOrder one = tInsuranceOrderService.getOne(Wrappers.<TInsuranceOrder>lambdaQuery().eq(TInsuranceOrder::getType,dto.getCourseType()).eq(TInsuranceOrder::getAppUserId, userId).eq(TInsuranceOrder::getStatus, 2).orderByDesc(BaseModel::getCreateTime).last("LIMIT 1"));
        if(one!=null){
            if(one.getEndTime().after(new Date())){
                return R.fail("您已购买保险");
            }
        }
        TInsuranceOrder insuranceOrder = new TInsuranceOrder();
        insuranceOrder.setAppUserId(userId);
        insuranceOrder.setName(dto.getName());
        insuranceOrder.setPhone(dto.getPhone());
        insuranceOrder.setIdCard(dto.getIdCard());
        insuranceOrder.setImg(dto.getImg());
        insuranceOrder.setPayType(dto.getType());
        insuranceOrder.setType(dto.getCourseType());
        TInsurance insurance = tInsuranceService.getOne(new LambdaQueryWrapper<TInsurance>().eq(TInsurance::getInsuranceType, dto.getCourseType()).eq(TInsurance::getStatus, 1));
        if(insurance==null){
            return R.fail("保险已关闭");
        }
        insuranceOrder.setInsuranceId(insurance.getId());
        insuranceOrder.setPayMoney(insurance.getInsuranceMoney().setScale(2, RoundingMode.HALF_UP));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse(insurance.getValidityTime());
        insuranceOrder.setEndTime(date);
 
        String payNo = OrderNos.getDid();
        insuranceOrder.setPayNo(payNo);
        boolean save = tInsuranceOrderService.save(insuranceOrder);
        if(dto.getType()==2){
            return R.ok();
        }
        Map<String, Object> result = wxV3Pay.jsApiInsurance(payNo, insuranceOrder.getPayMoney().multiply(BigDecimal.valueOf(100)).longValue(), appUser.getOpenId(),"购买保险","");
        return R.ok(result);
    }
 
 
 
    @ApiOperation(value = "保险模块--缴费记录",response = TInsuranceOrder.class)
    @PostMapping("/getInsuranceOrderList")
    public R<?> getInsuranceOrderList(@RequestBody BasePage query) {
        String userId = tokenService.getLoginUserApplet().getUserId();
        Page<TInsuranceOrder> page = tInsuranceOrderService.page(new Page<>(query.getPageNum(), query.getPageSize()), Wrappers.<TInsuranceOrder>lambdaQuery().eq(TInsuranceOrder::getAppUserId, userId).ne(TInsuranceOrder::getStatus,0).orderByDesc(BaseModel::getCreateTime));
        return R.ok(page);
    }
 
 
 
 
    @PostMapping("/refundOrder")
    @ApiOperation(value = "保险模块--退款")
    public R<?> refundOrder(@RequestBody TInsuranceOrder order) {
        String userId = tokenService.getLoginUserApplet().getUserId();
        TInsuranceOrder order1 = tInsuranceOrderService.getById(order.getId());
        if(order1.getPayType()==2){
            return R.fail("线下支付无法退款");
        }
 
        order1.setRefundReason(order.getRefundReason());
        order1.setRefundReasonPictures(order.getRefundReasonPictures());
 
        order1.setRequestRefundTime(new Date());
        order1.setRefundMoney(order1.getPayMoney().doubleValue());
        Integer type = order1.getType();
 
 
        String insuranceId = order1.getInsuranceId();
 
        TInsurance insurance = tInsuranceService.getById(insuranceId);
        if(insurance==null){
            return R.fail("保险已关闭,无法退款");
        }
 
        // 判断是否过期
        if(order1.getEndTime().before(new Date())){
            return R.fail("保险已过期,无法退款");
        }
 
        // 判断是否有当前的课程 待支付 和已完成
        int count = tCourseOrderService.countOrderNum( userId, type);
        if(count>0){
            return R.fail("请先完成课程");
        }
 
        if(!userId.equals(order1.getAppUserId())){
            return R.fail("无权限操作");
        }
        if(order1.getStatus()!=2){
            return R.fail("状态不允许退款");
        }
        order1.setStatus(4);
        LocalDateTime payTime = order1.getPayTime();
        if(payTime==null){
            return R.fail("状态不允许退款");
        }
        if(payTime.plusDays(2).isBefore(LocalDateTime.now()) || order1.getPayType()==2){
            order1.setBankCardNumber(order.getBankCardNumber());
            order1.setCardholderName(order.getCardholderName());
            order1.setRefundStatus(1);
            tInsuranceOrderService.updateById(order1);
        }else {
            String outRefundNo = OrderNos.getDid();
            order1.setRefundNumber(outRefundNo);
            tInsuranceOrderService.updateById(order1);
//            tInsuranceOrderService refundModel = new WxPaymentRefundModel();
////            refundModel.setSub_mchid(weixinPayProperties.getMchId());
////            refundModel.setSp_appid(weixinPayProperties.getAppId());
////            refundModel.setTransaction_id(courseOrder1.getTransactionId());
//            refundModel.setOut_trade_no(courseOrder1.getOrderNumber());
//            refundModel.setOut_refund_no(OrderNos.getDid());
//            refundModel.setReason(courseOrder1.getRefundReason());
//            refundModel.setGoods_detail(new ArrayList<>());
//            refundModel.setAmount(new WxPaymentRefundModel.RefundAmount(courseOrder1.getPayMoney().multiply(BigDecimal.valueOf(100)).intValue(), "CNY", courseOrder1.getPayMoney().multiply(BigDecimal.valueOf(100)).intValue()));
//            Map<String, Object> refund = wxV3Pay.refund(refundModel);
//            System.out.println(refund);
            String result = wxRefundUtil.refund(order1.getPayMoney().multiply(BigDecimal.valueOf(100)).intValue(), order1.getPayNo(),outRefundNo);
            if(result!=null){
                return R.fail(result);
            }
        }
        return R.ok();
    }
 
    @Autowired
    private WxRefundUtil wxRefundUtil;
}