Administrator
2024-03-12 3f17034c768ba4fc330e01f014b4f880e6a1569c
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
package com.dy.pipIrrSell.virtualCard;
 
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.dy.common.aop.SsoAop;
import com.dy.common.webUtil.BaseResponse;
import com.dy.common.webUtil.BaseResponseUtils;
import com.dy.common.webUtil.ResultCodeMsg;
import com.dy.pipIrrGlobal.pojoSe.SeVcRefund;
import com.dy.pipIrrGlobal.pojoSe.SeVcRefundItem;
import com.dy.pipIrrGlobal.pojoSe.SeVirtualCard;
import com.dy.pipIrrSell.result.SellResultCode;
import com.dy.pipIrrSell.util.PayHelper;
import com.dy.pipIrrSell.virtualCard.dto.DtoAudit;
import com.dy.pipIrrSell.virtualCard.dto.DtoRefund;
import com.dy.pipIrrSell.virtualCard.enums.LastOperateENUM;
import com.dy.pipIrrSell.virtualCard.enums.RefundItemStateENUM;
import com.dy.pipIrrSell.virtualCard.enums.RefundStateENUM;
import com.dy.pipIrrSell.wechatpay.dto.Refund;
import com.dy.pipIrrSell.wechatpay.dto.ToRefund;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import javax.crypto.NoSuchPaddingException;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.security.spec.InvalidKeySpecException;
import java.util.Date;
import java.util.List;
import java.util.Objects;
 
/**
 * @author ZhuBaoMin
 * @date 2024-03-06 8:40
 * @LastEditTime 2024-03-06 8:40
 * @Description
 */
 
@Slf4j
@Tag(name = "虚拟卡管理", description = "虚拟卡管理")
@RestController
@RequestMapping(path="virtual_card")
@RequiredArgsConstructor
@Validated
public class VirtualCardCtrl {
    private final VirtualCardSv virtualCardSv;
    private final PayHelper payHelper;
 
    /**
     * 虚拟卡账号注册
     * @param clientId
     * @return
     */
    @Operation(summary = "注册虚拟卡", description = "注册虚拟卡")
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
                    description = "操作结果:true:成功,false:失败(BaseResponse.content)",
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
                            schema = @Schema(implementation = Boolean.class))}
            )
    })
    @PostMapping(path = "add_vc")
    @SsoAop()
    public BaseResponse<Boolean> addWallet(@RequestParam("clientId") @NotNull(message = "农户编号不能为空") Long clientId){
        if(clientId == null || clientId < 0) {
            return BaseResponseUtils.buildFail(SellResultCode.CLIENT_ID_CANNOT_BE_NULL.getMessage());
        }
 
        SeVirtualCard seVirtualCard = new SeVirtualCard();
        seVirtualCard.setClientId(clientId);
        seVirtualCard.setMoney(0d);
        seVirtualCard.setLastOperate(LastOperateENUM.OPEN_ACCOUNT.getCode());
        seVirtualCard.setLastOperateTime(new Date());
        seVirtualCard.setCreateTime(new Date());
        Long rec = virtualCardSv.insertVirtualCard(seVirtualCard);
        if(rec == null) {
            return BaseResponseUtils.buildFail(SellResultCode.WALLET_OPEN_ACCOUNT_FAIL.getMessage());
        }
        return BaseResponseUtils.buildSuccess(true) ;
    }
 
 
    /**
     * 用户申请退款
     * @param po
     * @param bindingResult
     * @return
     */
    @Operation(summary = "虚拟卡申请退款", description = "虚拟卡申请退款")
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
                    description = "操作结果:true:成功,false:失败(BaseResponse.content)",
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
                            schema = @Schema(implementation = Boolean.class))}
            )
    })
    @PostMapping(path = "add_refund", consumes = MediaType.APPLICATION_JSON_VALUE)
    @Transactional(rollbackFor = Exception.class)
    @SsoAop()
    public BaseResponse<Boolean> addRefund(@RequestBody @Valid DtoRefund po, BindingResult bindingResult){
        if(bindingResult != null && bindingResult.hasErrors()){
            return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
 
        Long virtualId = po.getVirtualId();
        Integer refundAmount = po.getRefundAmount();
 
        // 根据虚拟卡ID获取虚拟卡对象
        SeVirtualCard seVirtualCard = virtualCardSv.selectVirtuCardById(virtualId);
        if(seVirtualCard == null) {
            return BaseResponseUtils.buildFail(SellResultCode.VIRTUAL_CARD_NOT_EXIST.getMessage());
        }
        Long clientId = seVirtualCard.getClientId();
        Double money = seVirtualCard.getMoney();
 
        // 验证退款金额是否大于余额
        if(refundAmount > money) {
            return BaseResponseUtils.buildFail(SellResultCode.REFUND_AMOUNT_CANNOT_GREATER_THAN_MONEY.getMessage());
        }
 
        // 计算消费后余额
        Double afterRefund = money - refundAmount;
 
        SeVcRefund seVcRefund = new SeVcRefund();
        seVcRefund.setVcId(virtualId);
        seVcRefund.setClientId(clientId);
        seVcRefund.setMoney(money);
        seVcRefund.setRefundAmount(refundAmount);
        seVcRefund.setAfterRefund(afterRefund);
        seVcRefund.setApplicationTime(new Date());
        seVcRefund.setRefundStatus(RefundStateENUM.TO_AUDIT.getCode());
 
        Long rec = virtualCardSv.addRefund(seVcRefund);
        if(rec == 0) {
            return BaseResponseUtils.buildFail(SellResultCode.APPLICATION_REFUND_FAIL.getMessage());
        }
        return BaseResponseUtils.buildSuccess(true) ;
    }
 
    /**
     * 审核退款申请
     * @param po
     * @param bindingResult
     * @return
     */
    @Operation(summary = "审核退款申请", description = "审核退款申请")
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
                    description = "操作结果:true:成功,false:失败(BaseResponse.content)",
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
                            schema = @Schema(implementation = Boolean.class))}
            )
    })
    @PostMapping(path = "audit_refund", consumes = MediaType.APPLICATION_JSON_VALUE)
    @Transactional(rollbackFor = Exception.class)
    @SsoAop()
    public BaseResponse<Boolean> auditRefund(@RequestBody @Valid DtoAudit po, BindingResult bindingResult) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeySpecException, IOException, SignatureException, InvalidKeyException {
        if(bindingResult != null && bindingResult.hasErrors()){
            return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
 
        // 根据退款ID获取退款对象,并更新审核人、审核时间、审核备注、退款状态字段
        SeVcRefund seVcRefund = virtualCardSv.selectRefundByRefundId(po.getRefundId());
        Long virtualId = seVcRefund.getVcId();
        Integer refundAmount = seVcRefund.getRefundAmount();
        seVcRefund.setAuditor(po.getAuditor());
        seVcRefund.setAuditTime(new Date());
        seVcRefund.setRemarks(po.getRemarks());
        seVcRefund.setRefundStatus(RefundStateENUM.TO_REFUND.getCode());
        Integer rec = virtualCardSv.updateRefund(seVcRefund);
        if(rec == 0) {
            return BaseResponseUtils.buildFail(SellResultCode.AUDIT_REFUND_FAIL.getMessage());
        }
 
        // 完成审核后获取待退款订单列表
        List<ToRefund> list_ToRefund = payHelper.getToRefunds(virtualId, refundAmount);
        if(list_ToRefund == null || list_ToRefund.size() <=0)
            return BaseResponseUtils.buildFail(SellResultCode.NOT_SUFFICIENT_FUNDS.getMessage());
 
        //遍历待退款列表
        JSONArray array_ToRefund = (JSONArray) JSON.toJSON(list_ToRefund);
        for(int i = 0; i < array_ToRefund.size(); i++) {
            JSONObject job_ToRefund = array_ToRefund.getJSONObject(i);
            String orderNumber_ToRefund = job_ToRefund.getString("orderNumber");
            Integer refundAmount_ToRefund = job_ToRefund.getInteger("refundAmount");
 
            // 生成退款分项记录
            SeVcRefundItem seVcRefundItem = new SeVcRefundItem();
            seVcRefundItem.setRefundId(po.getRefundId());
            seVcRefundItem.setOrderNumber(orderNumber_ToRefund);
            String refundNumber = virtualCardSv.generateRefundNumber(orderNumber_ToRefund);
            seVcRefundItem.setRefundNumber(refundNumber);
            seVcRefundItem.setRefundAmount(refundAmount_ToRefund);
            seVcRefundItem.setCreateTime(new Date());
            seVcRefundItem.setRefundStatus(RefundItemStateENUM.NO_REFUND.getCode());
            Long refundItemId = virtualCardSv.addRefundItem(seVcRefundItem);
 
            // 调用微信退款申请接口
            Refund refund = new Refund();
            refund.setTradeNo(orderNumber_ToRefund);
            refund.setRefundNo(refundNumber);
            refund.setRefund(refundAmount_ToRefund);
            BaseResponse rep = payHelper.refunds(refund);
        }
 
        return BaseResponseUtils.buildSuccess(true) ;
    }
 
}