Pu Zhibing
2025-03-26 cbf2486983b77a27af9968bbb362cb8d43789115
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
package com.ruoyi.account.controller;
 
 
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.account.api.model.AppUser;
import com.ruoyi.account.api.model.BalanceChangeRecord;
import com.ruoyi.account.api.model.WithdrawalRequests;
import com.ruoyi.account.dto.WithQuery;
import com.ruoyi.account.dto.WithdrawalRequestsDTO;
import com.ruoyi.account.service.AppUserService;
import com.ruoyi.account.service.BalanceChangeRecordService;
import com.ruoyi.account.service.WithdrawalRequestsService;
import com.ruoyi.account.util.payment.TransferUtil;
import com.ruoyi.account.util.payment.model.AccountBalanceQueryResult;
import com.ruoyi.account.util.payment.model.SinglePay;
import com.ruoyi.account.util.payment.model.SinglePayCallbackResult;
import com.ruoyi.account.util.payment.model.SinglePayResult;
import com.ruoyi.account.vo.WithdrawalAuth;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.system.api.model.LoginUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
 
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author luodangjia
 * @since 2024-11-21
 */
@RestController
@RequestMapping("/withdrawal-requests")
@Api(tags = "提现申请")
public class WithdrawalRequestsController {
 
    @Resource
    private WithdrawalRequestsService withdrawalRequestsService;
    @Resource
    private AppUserService appUserService;
    
    @Resource
    private BalanceChangeRecordService balanceChangeRecordService;
    @Resource
    private TokenService tokenService;
 
    /**
     * 提现申请
     */
    @PostMapping("/withdrawalApply")
    @ApiOperation(value = "提现申请", tags = {"提现申请-小程序"})
    public AjaxResult withdrawalApply(@RequestBody WithdrawalRequestsDTO params){
        withdrawalRequestsService.withdrawalApply(params);
        return AjaxResult.success();
    }
 
    
    
    
    @PostMapping("/page")
    @ApiOperation(value = "提现申请列表", tags = {"后台"})
    public R<IPage<WithdrawalRequests>> page(@RequestBody WithQuery withQuery){
        IPage<WithdrawalRequests> withdrawalRequestsIPage = withdrawalRequestsService.pageList(withQuery);
        for (WithdrawalRequests record : withdrawalRequestsIPage.getRecords()) {
            record.setIdStr(record.getId().toString());
        }
        return R.ok(withdrawalRequestsIPage);
    }
    
    
    
    
    
    @PostMapping("/auth")
    @ApiOperation(value = "提现申请审批", tags = {"后台"})
    public R auth(@RequestBody WithdrawalAuth withdrawalAuth){
        WithdrawalRequests withdrawal = withdrawalRequestsService.getById(withdrawalAuth.getId());
        BigDecimal withdrawalAmount = withdrawal.getWithdrawalAmount();
        if(withdrawal.getAuditStatus() != 1){
            return R.fail("不能重复审核");
        }
        Integer auditStatus = withdrawalAuth.getAuditStatus();
        if (auditStatus==2){
            //执行转账操作
            if(withdrawal.getWithdrawalMethod() == 2){
                //先检查账户余额是否充足
                AccountBalanceQueryResult accountBalanceQueryResult = TransferUtil.accountBalanceQuery();
                if(null == accountBalanceQueryResult){
                    return R.fail("查询账户余额出错");
                }
                Double useAbleSettAmount = accountBalanceQueryResult.getUseAbleSettAmount();
                //计算代付手续费
                if(useAbleSettAmount < (withdrawal.getArrivalAmount().doubleValue() + 1)){
                    return R.fail("账户可用余额不足,请先补充账户余额");
                }
                //银行卡转账
                SinglePay singlePay = new SinglePay();
                singlePay.setTradeMerchantNo(TransferUtil.sysTradeMerchantNo);
                singlePay.setMerchantOrderNo(withdrawal.getId().toString());
                singlePay.setReceiverAccountNoEnc(withdrawal.getBankCardNumber());
                singlePay.setReceiverNameEnc(withdrawal.getAccountHolder());
                singlePay.setReceiverAccountType(201);
                singlePay.setPaidAmount(withdrawal.getArrivalAmount().doubleValue());
                singlePay.setPaidDesc("账户余额提现");
                singlePay.setPaidUse("205");
                singlePay.setCallbackUrl("/account/withdrawal-requests/withdrawalCallback");
                SinglePayResult singlePayResult = TransferUtil.singlePay(singlePay);
                if(null == singlePayResult){
                    LoginUser loginUserApplet = tokenService.getLoginUserApplet();
                    AppUser appUser = appUserService.getById(loginUserApplet.getUserid());
                    BigDecimal balance = appUser.getBalance();
                    BalanceChangeRecord balanceChangeRecord = new BalanceChangeRecord();
                    balanceChangeRecord.setAppUserId(appUser.getId());
                    balanceChangeRecord.setVipId(appUser.getVipId());
//                    balanceChangeRecord.setOrderId(withdrawalRequests.getId());
                    balanceChangeRecord.setChangeType(2);
                    balanceChangeRecord.setChangeAmount(withdrawalAmount);
                    balanceChangeRecord.setDelFlag(0);
                    balanceChangeRecord.setCreateTime(LocalDateTime.now());
                    balanceChangeRecord.setChangeDirection(-1);
                    balanceChangeRecordService.save(balanceChangeRecord);
                    return R.fail("转账失败");
                }
                withdrawal.setStatus(1);
            }else{
                //微信转账
                return R.fail("微信转账开发中");
            }
        }
        if(3 == auditStatus){
            //回退扣除的金额,添加明细记录
            //修改用户的可提现金额
            AppUser appUser = appUserService.getById(withdrawal.getAppUserId());
            BigDecimal withdrawableAmount = appUser.getWithdrawableAmount();
            BigDecimal withdrawnAmount = appUser.getWithdrawnAmount();
            BigDecimal balance = appUser.getBalance();
            appUser.setWithdrawableAmount(withdrawableAmount.add(withdrawalAmount).setScale(2, RoundingMode.HALF_EVEN));
            appUser.setWithdrawnAmount(withdrawnAmount.subtract(withdrawalAmount).setScale(2, RoundingMode.HALF_EVEN));
            appUser.setBalance(appUser.getBalance().add(withdrawalAmount).setScale(2, RoundingMode.HALF_EVEN));
            appUserService.updateById(appUser);
            //添加变动明细
            BalanceChangeRecord balanceChangeRecord = new BalanceChangeRecord();
            balanceChangeRecord.setAppUserId(appUser.getId());
            balanceChangeRecord.setVipId(appUser.getVipId());
            balanceChangeRecord.setOrderId(withdrawal.getId());
            balanceChangeRecord.setChangeType(2);
            balanceChangeRecord.setChangeAmount(withdrawalAmount);
            balanceChangeRecord.setDelFlag(0);
            balanceChangeRecord.setCreateTime(LocalDateTime.now());
            balanceChangeRecord.setChangeDirection(-1);
            balanceChangeRecordService.save(balanceChangeRecord);
        }
        withdrawal.setAuditStatus(auditStatus);
        withdrawalRequestsService.updateById(withdrawal);
        return R.ok();
    }
    
    
    /**
     * 提现审核通过后转账回调通知
     * @param singlePayCallbackResult
     */
    @ResponseBody
    @PostMapping("/withdrawalCallback")
    public Object withdrawalCallback(@RequestBody SinglePayCallbackResult singlePayCallbackResult){
        Integer status = singlePayCallbackResult.getStatus();
        String merchantOrderNo = singlePayCallbackResult.getMerchantOrderNo();
        WithdrawalRequests withdrawalRequests = withdrawalRequestsService.getById(merchantOrderNo);
        if(203 == status || 205 == status){
            if(1 == withdrawalRequests.getStatus()){
                withdrawalRequests.setStatus(2);
                withdrawalRequests.setArrivalTime(LocalDateTime.now());
                withdrawalRequestsService.updateById(withdrawalRequests);
            }
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("statusCode", 2001);
            return jsonObject;
        }else{
            //回退扣除的金额,添加明细记录
            //修改用户的可提现金额
            BigDecimal withdrawalAmount = withdrawalRequests.getWithdrawalAmount();
            AppUser appUser = appUserService.getById(withdrawalRequests.getAppUserId());
            BigDecimal withdrawableAmount = appUser.getWithdrawableAmount();
            BigDecimal withdrawnAmount = appUser.getWithdrawnAmount();
            BigDecimal balance = appUser.getBalance();
            appUser.setWithdrawableAmount(withdrawableAmount.add(withdrawalAmount).setScale(2, RoundingMode.HALF_EVEN));
            appUser.setWithdrawnAmount(withdrawnAmount.subtract(withdrawalAmount).setScale(2, RoundingMode.HALF_EVEN));
            appUser.setBalance(appUser.getBalance().add(withdrawalAmount).setScale(2, RoundingMode.HALF_EVEN));
            appUserService.updateById(appUser);
            //添加变动明细
            BalanceChangeRecord balanceChangeRecord = new BalanceChangeRecord();
            balanceChangeRecord.setAppUserId(appUser.getId());
            balanceChangeRecord.setVipId(appUser.getVipId());
            balanceChangeRecord.setOrderId(withdrawalRequests.getId());
            balanceChangeRecord.setChangeType(2);
            balanceChangeRecord.setChangeAmount(withdrawalAmount);
            balanceChangeRecord.setDelFlag(0);
            balanceChangeRecord.setCreateTime(LocalDateTime.now());
            balanceChangeRecord.setChangeDirection(-1);
            balanceChangeRecordService.save(balanceChangeRecord);
 
            withdrawalRequests.setStatus(3);
            withdrawalRequests.setRemark(singlePayCallbackResult.getErrorCodeDesc());
            withdrawalRequestsService.updateById(withdrawalRequests);
 
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("statusCode", 2001);
            return jsonObject;
        }
    }
}