xuhy
2025-01-09 712f70b2936079a131ecb1e63c6d337171618cad
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package com.stylefeng.guns.modular.account.controller;
 
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.account.model.ElectronicLedger;
import com.stylefeng.guns.modular.account.model.TUserBankAccount;
import com.stylefeng.guns.modular.account.req.UserWithdrawalReq;
import com.stylefeng.guns.modular.account.server.ElectronicLedgerService;
import com.stylefeng.guns.modular.account.server.UserBankAccountService;
import com.stylefeng.guns.modular.account.server.UserWithdrawalService;
import com.stylefeng.guns.modular.account.util.Base64Util;
import com.stylefeng.guns.modular.account.util.OssUploadUtil;
import com.stylefeng.guns.modular.cloudPayment.example.*;
import com.stylefeng.guns.modular.cloudPayment.req.BalanceAcctReq;
import com.stylefeng.guns.modular.cloudPayment.req.UserBankAccountReq;
import com.stylefeng.guns.modular.system.model.*;
import com.stylefeng.guns.modular.system.service.IBankCardService;
import com.stylefeng.guns.modular.system.service.ICompanyService;
import com.stylefeng.guns.modular.system.service.IUserInfoService;
import com.stylefeng.guns.modular.system.service.IUserService;
import com.stylefeng.guns.modular.system.util.ResultUtil;
import com.unionpay.upyzt.exception.UpyztException;
import com.unionpay.upyzt.param.FailureMsgParam;
import com.unionpay.upyzt.resp.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;
 
@Api(tags = "用户进件(提现操作)/创建电子账簿")
@RestController
@Slf4j
@RequestMapping("/api/electronicLedger")
public class ElectronicLedgerController {
    @Autowired
    private ElectronicLedgerService electronicLedgerService;
 
    @Resource
    private UserWithdrawalService userWithdrawalService;
 
    @Resource
    private UserBankAccountService userBankAccountService;
    @Resource
    private IUserService userService;
    @Autowired
    private IBankCardService bankCardService;
 
    @ResponseBody
    @RequestMapping(value = "/addElectronicLedger", method = RequestMethod.POST)
    @ApiOperation(value = "用户进件(提现操作)", tags = {"用户端-用户进件(提现操作)"}, notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Bearer +token", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9.....")
    })
    @Transactional(rollbackFor = UpyztException.class,propagation = Propagation.REQUIRES_NEW)
    public ResultUtil addElectronicLedger( UserWithdrawalReq userWithdrawalReq,HttpServletRequest request) throws UnsupportedEncodingException {
        System.out.println("进件信息:"+userWithdrawalReq);
        // TODO 用户有无提现密码,无则设置开户密码
        if(StringUtils.hasLength(userWithdrawalReq.getBankName())){
            userWithdrawalReq.setBankName(userWithdrawalReq.getBankName().replace("& #40;","("));
            userWithdrawalReq.setBankName(userWithdrawalReq.getBankName().replace("& #41;",")"));
 
        }
 
 
 
//        userWithdrawalReq.setOpenAccountName(new String(userWithdrawalReq.getOpenAccountName().getBytes("ISO-8859-1"),"UTF-8"));
//        userWithdrawalReq.setBankName(new String(userWithdrawalReq.getBankName().getBytes("ISO-8859-1"),"UTF-8"));
//        userWithdrawalReq.setCertificateName(new String(userWithdrawalReq.getCertificateName().getBytes("ISO-8859-1"),"UTF-8"));
//        userWithdrawalReq.setLicenceIssuingAuthority(new String(userWithdrawalReq.getLicenceIssuingAuthority().getBytes("ISO-8859-1"),"UTF-8"));
        System.out.println("编码后:"+userWithdrawalReq);
 
 
 
        userWithdrawalReq.setOutRequestNo(ToolUtil.getRandomString(32));
        UserWithdrawal userWithdrawal = new UserWithdrawal();
        BeanUtils.copyProperties(userWithdrawalReq,userWithdrawal);
        try {
            String password=userWithdrawal.getTransactionAuthorizationCode();
            Integer id = userInfoService.getUserIdFormRedis(request);
            if(null == id){
                return ResultUtil.tokenErr();
            }
            System.out.println("验证码:"+userWithdrawalReq.getCode());
            userWithdrawalReq.setCopy1(userWithdrawalReq.getCopy1().replaceAll(" ","+"));
            userWithdrawalReq.setCopy2(userWithdrawalReq.getCopy2().replaceAll(" ","+"));
            // 用户进件
            CusApplicationResp cusApplicationResp = CusApplicationExample.create(userWithdrawalReq);
            System.out.println("进件状态:"+cusApplicationResp);
            if("succeeded".equals(cusApplicationResp.getApplicationStatus())) {
                userWithdrawal.setTransactionAuthorizationCode(Base64Util.encode(password));
                userWithdrawal.setUserType(1);
                userWithdrawal.setUserId(id);
                userWithdrawal.setImgUrl(userWithdrawalReq.getIdCardFrontPicture());
                userWithdrawal.setImgUrlOne(userWithdrawalReq.getIdCardReversePicture());
                userWithdrawal.setIdCardFrontPicture(userWithdrawalReq.getCopy1());
                userWithdrawal.setIdCardReversePicture(userWithdrawalReq.getCopy2());
 
                userWithdrawalService.insert(userWithdrawal);
                UserWithdrawal userWithdrawal1 = new UserWithdrawal();
                userWithdrawal1.setId(userWithdrawal.getId());
                BeanUtils.copyProperties(cusApplicationResp, userWithdrawal1);
                userWithdrawalService.updateById(userWithdrawal1);
                // 开通电子账簿
                BalanceAcctReq balanceAcctReq = new BalanceAcctReq();
                balanceAcctReq.setCusId(userWithdrawal1.getCusId());
                balanceAcctReq.setOutRequestNo(ToolUtil.getRandomString(32));
                balanceAcctReq.setPassword(password);
                ElectronicLedger electronicLedger = new ElectronicLedger();
                electronicLedger.setBalanceAcctId(cusApplicationResp.getBalanceAcctId());
                electronicLedger.setCreateTime(new Date());
                electronicLedger.setCusId(cusApplicationResp.getCusId());
                electronicLedger.setOutRequestNo(cusApplicationResp.getOutRequestNo());
                electronicLedger.setUserId(userWithdrawal.getUserId());
                electronicLedger.setPassword(SecureUtil.md5(password));
                electronicLedger.setRelAcctNo(cusApplicationResp.getRelAcctNo());
                electronicLedger.setUserType(1);
                electronicLedgerService.insert(electronicLedger);
                // 绑卡 存卡
                UserBankAccount userBankAccount = new UserBankAccount();
                userBankAccount.setBankAcctType("1");
                userBankAccount.setOutRequestNo(ToolUtil.getRandomString(32));
                userBankAccount.setUserType(1);
                userBankAccount.setCreateTime(new Date());
                userBankAccount.setBankCardCode(userWithdrawal.getBankCardCode());
                // TODO 页面没有输入地方 需要查询
                userBankAccount.setBankCode(userWithdrawal.getBankCode());
                // TODO 行联号
                userBankAccount.setBankBranchCode(userWithdrawal.getBankBranchCode());
                userBankAccount.setPhone(userWithdrawal.getPhone());
 
                userBankAccount.setUserId(userWithdrawal.getUserId());
                userBankAccount.setBankName(userWithdrawal.getBankName());
                userBankAccount.setOpenAccountName(userWithdrawalReq.getOpenAccountName());
                int count = userBankAccountService.selectCount(new EntityWrapper<UserBankAccount>()
                        .eq("bankCardCode", userWithdrawalReq.getBankCardCode()).eq("phone",userWithdrawalReq.getPhone()).eq("status",1));
                if(count<1){
                    userBankAccountService.insert(userBankAccount);
                }
                BankCard bankCard = new BankCard();
                bankCard.setDriverId(userWithdrawal.getUserId());
                bankCard.setBankAcctType("1");
                bankCard.setOutRequestNo(ToolUtil.getRandomString(32));
                bankCard.setInsertTime(new Date());
                bankCard.setCode(userWithdrawal.getBankCardCode());
                bankCard.setBankCode(userWithdrawal.getBankCode());
                bankCard.setBankBranchCode(userWithdrawal.getBankBranchCode());
                bankCard.setBank(userWithdrawal.getBankName());
                bankCard.setPhone(userWithdrawal.getPhone());
                bankCard.setName(userWithdrawal.getOpenAccountName());
                bankCard.setIfDefault(true);
                bankCardService.insert(bankCard);
 
 
                UserBankAccountReq userBankAccountReq = new UserBankAccountReq();
                BeanUtils.copyProperties(userBankAccount, userBankAccountReq);
                System.err.println(userWithdrawalReq.getCode());
                userBankAccountReq.setCusId(cusApplicationResp.getCusId());
                userBankAccountReq.setCode(userWithdrawalReq.getCode());
                return ResultUtil.success();
            }
            if("failed".equals(cusApplicationResp.getApplicationStatus())){
                List<FailureMsgParam> failureMsgs = cusApplicationResp.getFailureMsgs();
                if(failureMsgs != null && failureMsgs.size()>0){
                    return ResultUtil.error(failureMsgs.get(0).getReason());
                }
            }
            return ResultUtil.success();
        }catch (UpyztException e){
            if(e.getCode().equals("100101")){
                return ResultUtil.error(e.getIssue());
            }
            e.printStackTrace();
            String message = e.getMessage();
            return ResultUtil.error(message);
        }catch (Exception e){
            String message = e.getMessage();
            return ResultUtil.error(message);
        }
    }
 
 
    @ResponseBody
    @RequestMapping(value = "/updateElectronicLedgerInfo", method = RequestMethod.POST)
    @ApiOperation(value = "用户修改进件信息", tags = {"用户端-用户修改进件信息"}, notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Bearer +token", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9.....")
    })
    public ResultUtil updateElectronicLedgerInfo(String idCardFrontPicture,
                                                 String idCardReversePicture,
                                                 String certificateCode,
                                                 String certificateName,
                                                 String certificateExpireTime,
                                                 String openAccountName,
                                                 String bankCardCode,
                                                 String phone,
                                                 String bankBranchCode,
                                                 String bankName,
                                                 String copy1,
                                                 String copy2,
                                                 String code,
                                                 String bankCode,
                                                 HttpServletRequest request){
        try {
            Integer driverId = userInfoService.getUserIdFormRedis(request);
            if(null == driverId){
                return ResultUtil.tokenErr();
            }
            UserInfo userInfo = userInfoService.selectById(driverId);
            UserWithdrawal userWithdrawal1 = userWithdrawalService.selectOne(new EntityWrapper<UserWithdrawal>()
                    .eq("phone", userInfo.getPhone())
                    .last("LIMIT 1"));
            if(userWithdrawal1==null|| userWithdrawal1.getCusId()==null){
                return ResultUtil.runErr();
            }
            UserWithdrawal userWithdrawal = new UserWithdrawal();
            userWithdrawal.setIdCardFrontPicture(copy1);
            userWithdrawal.setIdCardReversePicture(copy2);
            userWithdrawal.setCertificateCode(certificateCode);
            userWithdrawal.setCertificateName(certificateName);
            userWithdrawal.setCertificateExpireTime(certificateExpireTime);
            userWithdrawal.setOpenAccountName(openAccountName);
            userWithdrawal.setBankCardCode(bankCardCode);
            userWithdrawal.setPhone(phone);
            userWithdrawal.setCusId(userWithdrawal1.getCusId());
            userWithdrawal.setBankBranchCode(bankBranchCode);
            userWithdrawal.setBankName(bankName);
            userWithdrawal.setCode(code);
            userWithdrawal.setBankCode(bankCode);
            CusApplicationResp renew = CusApplicationExample.renew(userWithdrawal);
            log.info("修改个人用户进件信息返回:{}",renew);
            if("succeeded".equals(renew.getApplicationStatus())){
                userWithdrawal1.setIdCardFrontPicture(copy1);
                userWithdrawal1.setIdCardReversePicture(copy2);
                userWithdrawal1.setCertificateCode(certificateCode);
                userWithdrawal1.setCertificateName(certificateName);
                userWithdrawal1.setCertificateExpireTime(certificateExpireTime);
                userWithdrawal1.setOpenAccountName(openAccountName);
                userWithdrawal1.setBankCardCode(bankCardCode);
                userWithdrawal1.setPhone(phone);
                userWithdrawal1.setBankName(bankName);
                userWithdrawal1.setImgUrl(idCardFrontPicture);
                userWithdrawal1.setImgUrlOne(idCardReversePicture);
                userWithdrawalService.updateById(userWithdrawal1);
 
                int count = userBankAccountService.selectCount(new EntityWrapper<UserBankAccount>()
                        .eq("bankCardCode", bankCardCode).eq("userId",driverId).eq("userType",1).eq("status",1));
                System.out.println(count);
                if(count<1){
                    UserBankAccount userBankAccount = new UserBankAccount();
                    userBankAccount.setBankAcctType("1");
                    userBankAccount.setOutRequestNo(ToolUtil.getRandomString(32));
                    userBankAccount.setUserType(1);
                    userBankAccount.setCreateTime(new Date());
                    userBankAccount.setBankCardCode(userWithdrawal.getBankCardCode());
                    userBankAccount.setBankCode(userWithdrawal.getBankCode());
                    userBankAccount.setBankBranchCode(userWithdrawal.getBankBranchCode());
                    userBankAccount.setPhone(userWithdrawal.getPhone());
 
                    userBankAccount.setUserId(driverId);
                    userBankAccount.setBankName(userWithdrawal.getBankName());
                    userBankAccountService.insert(userBankAccount);
 
 
                    BankCard bankCard = new BankCard();
                    bankCard.setDriverId(userWithdrawal.getUserId());
                    bankCard.setBankAcctType("1");
                    bankCard.setOutRequestNo(ToolUtil.getRandomString(32));
                    bankCard.setInsertTime(new Date());
                    bankCard.setCode(userWithdrawal.getBankCardCode());
                    bankCard.setBankCode(userWithdrawal.getBankCode());
                    bankCard.setBankBranchCode(userWithdrawal.getBankBranchCode());
                    bankCard.setBank(userWithdrawal.getBankName());
                    bankCard.setPhone(userWithdrawal.getPhone());
                    bankCard.setName(userWithdrawal.getOpenAccountName());
                    bankCard.setIfDefault(true);
                    bankCardService.insert(bankCard);
                }
                return ResultUtil.success();
            }
            if("failed".equals(renew.getApplicationStatus())){
                List<FailureMsgParam> failureMsgs = renew.getFailureMsgs();
                if(failureMsgs != null && failureMsgs.size()>0){
                    return ResultUtil.error(failureMsgs.get(0).getReason());
                }
            }
            return ResultUtil.success();
        }catch (UpyztException e) {
            e.printStackTrace();
            String message = e.getMessage();
            return ResultUtil.error(message);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.runErr();
        }
    }
 
 
    @Autowired
    private IUserInfoService userInfoService;
 
    @ResponseBody
    @RequestMapping(value = "/isElectronicLedger", method = RequestMethod.POST)
    @ApiOperation(value = "用户是否需要创建电子账簿", tags = {"用户端-用户是否需要创建电子账簿"}, notes = "")
    @ApiImplicitParams({
    })
    public ResultUtil addElectronicLedger(HttpServletRequest request){
        try {
            Integer id = userInfoService.getUserIdFormRedis(request);
            if(null == id){
                return ResultUtil.tokenErr();
            }
            UserInfo userInfo = userInfoService.selectById(id);
            List<UserWithdrawal> userId = userWithdrawalService.selectList(new EntityWrapper<UserWithdrawal>().eq("phone", userInfo.getPhone()));
            if(userId.size()==0){
                return ResultUtil.success("yes");
            }
            return ResultUtil.success("no");
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.runErr();
        }
    }
 
    @ResponseBody
    @RequestMapping(value = "/getUserInfo", method = RequestMethod.POST)
    @ApiOperation(value = "用户进件信息", tags = {"用户端-用户进件信息"}, notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Bearer +token", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9.....")
    })
    public ResultUtil getUserInfo(HttpServletRequest request){
        try {
            Integer id = userInfoService.getUserIdFormRedis(request);
            if(null == id){
                return ResultUtil.tokenErr();
            }
            UserInfo userInfo = userInfoService.selectById(id);
            UserWithdrawal userWithdrawal = userWithdrawalService.selectOne(new EntityWrapper<UserWithdrawal>().eq("phone", userInfo.getPhone()));
            return ResultUtil.success(userWithdrawal==null?new UserWithdrawal():userWithdrawal);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.runErr();
        }
    }
 
 
    @ResponseBody
    @RequestMapping(value = "/withdrawDeposit", method = RequestMethod.POST)
    @ApiOperation(value = "用户提现", tags = {"用户端-用户提现"}, notes = "amount-提现金额,password-交易授权码")
    @ApiImplicitParams({
    })
    public ResultUtil withdrawDeposit(Integer amount,String bankAcctNo){
 
        UserInfo user = userInfoService.selectById(Objects.requireNonNull(ShiroKit.getUser()).getId());
        UserWithdrawal driverWith = userWithdrawalService.selectOne(new EntityWrapper<UserWithdrawal>()
                .eq("phone", user.getPhone())
                .last("LIMIT 1"));
        Boolean b = userWithdrawalService.withdrawDeposit(amount,bankAcctNo,driverWith.getTransactionAuthorizationCode());
        if(b){
            return ResultUtil.success();
        }else {
            return ResultUtil.runErr();
        }
    }
 
 
 
    @ResponseBody
    @RequestMapping(value = "/withdrawDepositGetMoney", method = RequestMethod.POST)
    @ApiOperation(value = "用户获取可提现余额", tags = {"用户端-用户获取可提现余额"}, notes = "")
    @ApiImplicitParams({
    })
    public ResultUtil withdrawDepositGetMoney(HttpServletRequest request) throws Exception {
 
        UserInfo user = userInfoService.selectById(userInfoService.getUserIdFormRedis(request));
        UserWithdrawal driverWith = userWithdrawalService.selectOne(new EntityWrapper<UserWithdrawal>()
                .eq("phone", user.getPhone())
                .last("LIMIT 1"));
        if(driverWith==null){
            return  ResultUtil.error("请先进行进件操作");
        }
        try {
            BalanceAcctReq balanceAcctReq = new BalanceAcctReq();
            balanceAcctReq.setCusId(driverWith.getCusId());
            BalanceAcctListResp balanceAcctListResp = BalanceAcctExample.retrieveByUserId(balanceAcctReq);
            List<BalanceAcctItem> balanceAccts = balanceAcctListResp.getBalanceAccts();
            for (BalanceAcctItem balanceAcct : balanceAccts) {
                if(balanceAcct.getBalanceAcctId().equals(driverWith.getBalanceAcctId())){
                    if(balanceAcct.getSettledAmount().doubleValue()==0){
                        return ResultUtil.success(new BigDecimal(0));
                    }
                    return ResultUtil.success(new BigDecimal(balanceAcct.getSettledAmount()).divide(new BigDecimal(100)));
                }
            }
        } catch (UpyztException e) {
            e.printStackTrace();
            System.out.println("用户获取可提现余额:"+e);
            return ResultUtil.error(e.getMessage());
        }
        return  ResultUtil.success(new BigDecimal(0));
    }
 
 
 
    @ResponseBody
    @RequestMapping(value = "/uploadImg", method = RequestMethod.POST)
    @ApiOperation(value = "进件上传图片", tags = {"用户端-进件上传图片"}, notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Bearer +token", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9.....")
    })
    public ResultUtil uploadImg(MultipartFile multipartFile,HttpServletRequest request){
        try {
            MediaResp mediaResp = MediaExample.uploadImage(multipartFile);
            String pictureName = OssUploadUtil.obsUpload(request, multipartFile);
            String mediaId = mediaResp.getMediaId();
            userService.insertImg(mediaId,pictureName);
            return ResultUtil.success(mediaId);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.runErr();
        }
    }
 
}