chenjiahe
2023-04-01 510678fe2f81501914ea4905fb7026b9c7be414f
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
package com.hx.phip.service.refund.impl;
 
import javax.annotation.Resource;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hx.common.dao.CommonDao;
import com.hx.common.service.CommonService;
import com.hx.phiappt.common.*;
import com.hx.phiappt.constants.enums.ApprovalTypeEnum;
import com.hx.phiappt.constants.tool.JbpmTaskComTool;
import com.hx.phiappt.constants.tool.RefundToolUtil;
import com.hx.phiappt.model.*;
import com.hx.phiappt.model.refund.*;
import com.hx.phiappt.model.userMoney.UserMoneyUnclaimed;
import com.hx.phiappt.vo.workFlow.JbpmParamVo;
import com.hx.phip.config.CustomParameter;
import com.hx.phip.dao.mapper.*;
import com.hx.phip.service.SystemParameterService;
import com.hx.phip.util.api.ApiOrderUtil;
import com.hx.phip.util.api.RefundUtil;
import com.hx.phip.util.api.UserMoneyUtil;
import com.hx.util.StringUtils;
import com.hz.his.dto.refund.RefundFundsDto;
import com.platform.exception.PlatTipsException;
import com.platform.resultTool.PlatformCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.hx.exception.TipsException;
import com.hx.phip.service.refund.RefundRecordService;
import com.hx.mybatisTool.SqlSentence;
 
import java.math.BigDecimal;
import java.util.*;
 
@Transactional
@Service
public class RefundRecordServiceImpl implements RefundRecordService {
 
    // log4j日志
    private static final Logger logger = LoggerFactory.getLogger(RefundRecordServiceImpl.class.getName());
 
    @Resource
    private CommonDao commonDao;
    @Resource
    private CustomParameter customParameter;
    @Resource
    private RefundRecordMapper refundRecordMapper;
    @Resource
    private SystemParameterService systemParameterService;
 
    /**查询列表*/
    @Override
    public List<RefundRecord> selectList(SqlSentence sqlSentence) {
        return refundRecordMapper.selectList(sqlSentence);
    }
 
    /**查询列表*/
    @Override
    public List<Map<String,Object>> selectListMap(SqlSentence sqlSentence) {
        return refundRecordMapper.selectListMap(sqlSentence);
    }
 
    /**查询单个*/
    @Override
    public RefundRecord selectOne(SqlSentence sqlSentence) {
        return refundRecordMapper.selectOne(sqlSentence);
    }
 
    /**查询单个*/
    @Override
    public Map<String,Object> selectOneMap(SqlSentence sqlSentence) {
        return refundRecordMapper.selectOneMap(sqlSentence);
    }
 
    /**查询单个,大数据不拿取*/
    @Override
    public RefundRecord selectOneByKey(Object object) {
        return refundRecordMapper.selectOneByKey(object);
    }
 
    /**查询单个,大数据拿取*/
    @Override
    public RefundRecord selectOneByKeyBlob(Object object) {
        return refundRecordMapper.selectOneByKeyBlob(object);
    }
 
    /**新增*/
    @Override
    public void insert(RefundRecord refundRecord) {
        int count = refundRecordMapper.insert(refundRecord);
        if(count != 1) {
            throw new TipsException("新增失败!");
        }
    }
 
    /**修改*/
    @Override
    public void updateAll(RefundRecord refundRecord) {
        int count = refundRecordMapper.updateAll(refundRecord);
        if(count!=1) {
            throw new TipsException("保存失败!");
        }
    }
 
    /**修改*/
    @Override
    public void updateWhere(SqlSentence sqlSentence) {
        int count = refundRecordMapper.updateWhere(sqlSentence);
        if(count!=1) {
            throw new TipsException("保存失败!");
        }
    }
    
    /**删除一个*/
    @Override
    public void deleteOne(String delId) {
        int count = refundRecordMapper.deleteById(delId);
        if(count!=1) {
            throw new TipsException("删除失败!");
        }
    }
 
    @Override
    public RefundRecord selectOrderId(String orderId) {
        return refundRecordMapper.selectOrderId(orderId);
    }
 
    @Override
    public Map<String, Object> refund(JSONArray refundPayMethodList,String remarks, String operatorId, String shopId, String userId, CommonService commonService) {
        Map<String, String> operator = ApiOrderUtil.getOperator(commonService, operatorId);//获取操作人信息
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> map=new HashMap<>();
        sqlSentence.setM(map);
 
        String totalCode = systemParameterService.createRecordNo("R", 16, "yyyyMMddHHmmss"); //总订单编号
        BigDecimal sumMoney=BigDecimal.ZERO;
        RefundRecord refundRecord =null;
        RefundRecordItem refundRecordItem=null;
        RefundRecordMethod refundRecordMethod=null;
 
        Shop shop =commonService.selectOneByKey(ShopMapper.class,shopId);
        if(shop==null){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到操作人的门店信息");
        }
        //退款总记录
        refundRecord=new RefundRecord(totalCode,shop.getId(),shop.getName(),sumMoney,OrderTotalConstants.STATUS_REFUND_APPLY,1, RefundSoruceConstants.TYPE_SOURCE_ORDER,remarks,null,userId);
//        refundRecordService.insert(refundRecord);
        commonService.insert(RefundRecordMapper.class,refundRecord);
 
        List<PaymentMethod> payMethodList=null;
        //记录退回客户的支付方式
        for(int i=0;i<refundPayMethodList.size();i++){
 
            JSONObject jsonObject = refundPayMethodList.getJSONObject(i);
            String payMethodNo = jsonObject.getString("payMethodNo");//支付方式编码
            BigDecimal money = jsonObject.getBigDecimal("money");//实退金额
            String payee = jsonObject.getString("payee");//收款方
            String openBank = jsonObject.getString("openBank");//开户行
            String openBranch = jsonObject.getString("openBranch");//开户支行
            String account = jsonObject.getString("account");//账号
            String remark = jsonObject.getString("remarks");//备注
            map.put("method",payMethodNo);
            sqlSentence.setSqlSentence("select * from payment_method where numberNo=#{m.method} and isDel=0 and isUp=1");
            PaymentMethod paymentMethod =commonService.selectOne(PaymentMethodMapper.class,sqlSentence);
            if(paymentMethod==null){
                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到该支付方式编码"+payMethodNo);
            }
            if(money ==null){
                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"实退金额为空");
            }
            sumMoney=sumMoney.add(money);
            refundRecordMethod=new RefundRecordMethod(payMethodNo,paymentMethod.getName(),money, ConsumePayConstants.STATUS_ORDER,null,refundRecord.getId(),paymentMethod.getId());
            refundRecordMethod.setPayee(payee);
            refundRecordMethod.setOpenBank(openBank);
            refundRecordMethod.setOpenBranch(openBranch);
            refundRecordMethod.setAccount(account);
            refundRecordMethod.setRemarks(remark);
            commonService.insert(RefundRecordMethodMapper.class,refundRecordMethod);
//            if(PayMethodTypeConstants.PAY_STORED.equals(paymentMethod.getNumberNo())){
                payMethodList=new ArrayList();
                payMethodList.add(paymentMethod);
//            }
        }
        //保存退款详情订单
        refundRecordItem = new RefundRecordItem(payMethodList.get(0).getNumberNo(), payMethodList.get(0).getName(), payMethodList.get(0).getId(), 0, sumMoney, "", refundRecord.getId(), "[]", "");
        refundRecordItem.setType("userMoney");
        commonService.insert(RefundRecordItemMapper.class,refundRecordItem);
        //处理提交审核流程
        JbpmParamVo jbpmParamVo = new JbpmParamVo();
        jbpmParamVo.setType(ApprovalTypeEnum.CHARGEBACK.getCode());
        jbpmParamVo.setShopId(refundRecord.getRefundShopId());
        jbpmParamVo.setTotal(sumMoney);
        jbpmParamVo.setAmount(sumMoney);
        jbpmParamVo.setApplyId(refundRecord.getId());
        jbpmParamVo.setOrderId(refundRecord.getId());
        Integer integer = JbpmTaskComTool.checkConditions(jbpmParamVo, commonService);
        if(integer==null){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"预约系统的审核流程接口返回为空");
        }
        if(integer==0){
            //没有审批流程走完成退款流程
            RefundToolUtil.completeRefund(commonService,operator.get("operatorId"),operator.get("operatorName"),refundRecord.getId(),customParameter.getAesFixedKey());
        }else if(integer==1){
            Integer data = JbpmTaskComTool.checkConditionsWithInsert(jbpmParamVo, commonService);
            if(data==0){
//                logger.info("无退款流程,请添加重试,退款订单:"+refundRecord.getId());
                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"审核退款任务生成失败,请重试");
            }
        }else if(integer==2){
            //没有审批流程走完成退款流程
            RefundToolUtil.completeRefund(commonService,operator.get("operatorId"),operator.get("operatorName"),refundRecord.getId(),customParameter.getAesFixedKey());
        }
        //返回数据
        Map<String,Object> data=new HashMap<>();
        data.put("refundRecordId",refundRecord.getId());
        data.put("refundRecordCode",refundRecord.getCode());
        return data;
    }
 
    /**查询条数*/
    @Override
    public int selectCount(SqlSentence sqlSentence) {
        int count = refundRecordMapper.selectCount(sqlSentence);
        return count;
    }
 
    @Override
    public void refundAduit(CommonService commonService, String operationId,String operationNme, String refundId, CustomParameter customParameter,String appIdCode) {
        RefundUtil.refundProcess(commonService, operationId, operationNme, refundId, customParameter, appIdCode,1);
    }
 
    @Override
    public void reject(RefundRecord refundRecord,String refundId,CommonService commonService) {
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> map=new HashMap<>();
        sqlSentence.setM(map);
        map.put("isDel", BaseEntity.NO);
        map.put("refundId", refundId);
        //拒绝退款
        map.put("refundStatus", RefundStatus.STATUS_FAIL_REFUND);
        map.put("oldRefundStatus", RefundStatus.STATUS_APPLY_REFUND);
        sqlSentence.setSqlSentence(" refundStatus=#{m.refundStatus} where id=#{m.refundId} and isDel=#{m.isDel} and refundStatus=#{m.oldRefundStatus}");
        commonService.updateWhere(RefundRecordMapper.class,sqlSentence);
 
        //更新总订单状态
        map.put("refundStatus", OrderTotalConstants.STATUS_REFUND_REFUSED);
        map.put("status", OrderTotalConstants.STATUS_DONE);
        map.put("oldRefundStatus", RefundStatus.STATUS_APPLY_REFUND);
        map.put("orderId", refundRecord.getOrderId());
        map.put("isDel", BaseEntity.NO);
        sqlSentence.setSqlSentence(" status=#{m.status},refundStatus=#{m.refundStatus} where id=#{m.orderId} and isDel=#{m.isDel} and refundStatus=#{m.oldRefundStatus}");
        commonService.updateWhere(OrdersTotalMapper.class,sqlSentence);
    }
 
    /**
     * 获取支付方法
     * @param code 编号
     * @return 返回
     */
    private PaymentMethod getPaymentMethodByCode(String code){
        SqlSentence sqlSentence = new SqlSentence();
        Map<String, Object> sqlValues = new HashMap<>();
        sqlValues.put("method", code);
        sqlValues.put("isDel", BaseEntity.NO);
        String sql = "select * from payment_method where numberNo=#{m.method} and isDel=#{m.isDel}";
        sqlSentence.sqlSentence(sql, sqlValues);
        return commonDao.selectOne(PaymentMethodMapper.class, sqlSentence);
    }
 
    /**
     * 获取退款客户的支付方式
     * @param refundRecordId 退款总记录标识
     * @return 返回
     */
    private RefundRecordMethod getRefundRecordMethodByRefundRecordId(String refundRecordId){
        SqlSentence sqlSentence = new SqlSentence();
        Map<String, Object> sqlValues = new HashMap<>();
        sqlValues.put("refundRecordId", refundRecordId);
        sqlValues.put("isDel", BaseEntity.NO);
        String sql = "select * from refund_record_method where refundRecordId=#{m.refundRecordId} and isDel=#{m.isDel}";
        sqlSentence.sqlSentence(sql, sqlValues);
        return commonDao.selectOne(RefundRecordMethodMapper.class, sqlSentence);
    }
 
    /**
     * 获取退款客户的审核记录
     * @param refundRecordId 退款总记录标识
     * @return 返回
     */
    private RefundApprove getRefundApproveByRefundRecordId(String refundRecordId){
        SqlSentence sqlSentence = new SqlSentence();
        Map<String, Object> sqlValues = new HashMap<>();
        sqlValues.put("isDel", BaseEntity.NO);
        sqlValues.put("refundRecordId", refundRecordId);
        String sql = "select * from refund_approve where refundRecordId=#{m.refundRecordId} and isDel=#{m.isDel}";
        sqlSentence.sqlSentence(sql, sqlValues);
        return commonDao.selectOne(RefundApproveMapper.class, sqlSentence);
    }
 
    /**
     * 操作用户资金
     * @param refundRecord 资金记录
     * @param type 是否为提现处理 0 否 1 是 (否就是表示为不通过返回资金)
     */
    private void opUserMoneyUnclaimed(RefundRecord refundRecord, Integer type) {
        // 构建操作资金对象
        UserMoneyUnclaimed userMoneyUnclaimed = new UserMoneyUnclaimed();
        userMoneyUnclaimed.setUserId(refundRecord.getUserId());
        userMoneyUnclaimed.setFundType(UserMoneyUnclaimed.FUND_TYPE_STORED_VALUE_FUND);
        userMoneyUnclaimed.setOriginChannel(OriginChannelConstants.ORIGIN_CHANNEL_PHIS);
        userMoneyUnclaimed.setOperationReason(OperationReasonConstants.OP_REASON_WITHDRAW);
        userMoneyUnclaimed.setCommonId(refundRecord.getId());
        // 处理操作人信息
        if (RefundRecord.OPERATOR_TYPE_EMPLOYEE == refundRecord.getOperatorType()) {
            userMoneyUnclaimed.setOperatorType(UserMoneyUnclaimed.OPERATOR_TYPE_EMPLOYEE);
            userMoneyUnclaimed.setOperatorId(refundRecord.getOperatorId());
            userMoneyUnclaimed.setOperatorName(refundRecord.getOperatorName());
        }
        if (RefundRecord.OPERATOR_TYPE_USER == refundRecord.getOperatorType()) {
            userMoneyUnclaimed.setOperatorType(UserMoneyUnclaimed.OPERATOR_TYPE_USER);
            userMoneyUnclaimed.setOperatorId(refundRecord.getOperatorId());
            userMoneyUnclaimed.setOperatorName(refundRecord.getOperatorName());
        }
        if (RefundRecord.OPERATOR_TYPE_ADMIN == refundRecord.getOperatorType()) {
            userMoneyUnclaimed.setOperatorType(UserMoneyUnclaimed.OPERATOR_TYPE_ADMIN);
            userMoneyUnclaimed.setOperatorId(refundRecord.getOperatorId());
            userMoneyUnclaimed.setOperatorName(refundRecord.getOperatorName());
        }
        String info;
        String title = "客户储值金提现";
        if (BaseEntity.YES.equals(type)) {
            title += "-提现冻结金额";
            info = ", 提现冻结金额等待审核出款";
            userMoneyUnclaimed.setOpNumber(refundRecord.getRefundTotal().negate());
        } else {
            title += "-回退冻结金额";
            info = ", 回退冻结金额审核不通过";
            userMoneyUnclaimed.setOpNumber(refundRecord.getRefundTotal());
        }
        info = title + info +", 提现金额:" + refundRecord.getRefundTotal();
        // 标题
        userMoneyUnclaimed.setOriginSubject(title);
        // 备注
        userMoneyUnclaimed.setRemarks(info);
        // 调用工具类处理
        UserMoneyUtil.addUserMoneyUnclaimedData(userMoneyUnclaimed);
    }
 
    /**
     * 审核处理
     * @param refundFundsDto 参数
     * @param refundRecord 退款对象
     * @param type 状态 0 审核失败 1 审核成功
     */
    private void handlerApprove(RefundFundsDto refundFundsDto, RefundRecord refundRecord, Integer type) {
        RefundApprove refundApprove = this.getRefundApproveByRefundRecordId(refundRecord.getId());
        if (refundApprove != null) {
            // 营销助手审批标识
            refundApprove.setApplyId(refundFundsDto.getApplyId());
            // 审核员
            Employee employee = commonDao.selectOneByKey(EmployeeMapper.class, refundFundsDto.getExamEmplId());
            if (employee != null) {
                refundApprove.setExamManId(employee.getId());
                refundApprove.setExamManName(employee.getCnName());
            }
            // 是否审核 0 审核失败 1 审核成功
            if (BaseEntity.YES.equals(type)) {
                refundApprove.setApprovalStatus(RefundApprove.SUCCESS);
            } else {
                refundApprove.setApprovalStatus(RefundApprove.FAIL);
            }
            // 备注
            if (!StringUtils.isEmpty(refundFundsDto.getRemarks())) {
                refundApprove.setRemarks(refundFundsDto.getRemarks());
            }
            // 更新时间
            refundApprove.setEditTime(new Date());
            // 处理
            commonDao.updateAll(RefundApproveMapper.class, refundApprove);
        }
    }
 
    /**
     * 用户资金退款
     * @param refundFundsDto 参数
     */
    @Override
    public String refundUserFunds(RefundFundsDto refundFundsDto) {
        // 用户判断
        User user = commonDao.selectOneByKey(UserMapper.class, refundFundsDto.getUserId());
        if (user == null) {
            throw new PlatTipsException(PlatformCode.ERROR_TIPS, "用户信息不存在!");
        }
        // 门店判断
        Shop shop = commonDao.selectOneByKey(ShopMapper.class, refundFundsDto.getShopId());
        if (shop == null) {
            throw new PlatTipsException(PlatformCode.ERROR_TIPS, "门店信息不存在!");
        }
        // 退款总记录
        RefundRecord refundRecord = new RefundRecord();
        // 操作人为员工
        if (RefundRecord.OPERATOR_TYPE_EMPLOYEE == refundFundsDto.getOperatorType()) {
            Employee employee = commonDao.selectOneByKey(EmployeeMapper.class, refundFundsDto.getOperatorId());
            if (employee == null) {
                throw new PlatTipsException(PlatformCode.ERROR_TIPS, "操作人的信息不存在");
            } else {
                refundRecord.setOperatorType(RefundRecord.OPERATOR_TYPE_EMPLOYEE);
                refundRecord.setOperatorId(employee.getId());
                refundRecord.setOperatorName(employee.getCnName());
            }
        }
 
        // 操作人为后台用户
        if (RefundRecord.OPERATOR_TYPE_ADMIN == refundFundsDto.getOperatorType()) {
            SysAdmin sysAdmin = commonDao.selectOneByKey(SysAdminMapper.class, refundFundsDto.getOperatorId());
            if (sysAdmin == null) {
                throw new PlatTipsException(PlatformCode.ERROR_TIPS, "操作人的信息不存在");
            } else {
                refundRecord.setOperatorType(RefundRecord.OPERATOR_TYPE_ADMIN);
                refundRecord.setOperatorId(sysAdmin.getId());
                refundRecord.setOperatorName(sysAdmin.getName());
            }
        }
 
        // 操作人为用户
        if (RefundRecord.OPERATOR_TYPE_USER == refundFundsDto.getOperatorType()) {
            User opUser = commonDao.selectOneByKey(UserMapper.class, refundFundsDto.getOperatorId());
            if (opUser == null) {
                throw new PlatTipsException(PlatformCode.ERROR_TIPS, "操作人的信息不存在");
            } else {
                refundRecord.setOperatorType(RefundRecord.OPERATOR_TYPE_USER);
                refundRecord.setOperatorId(opUser.getId());
                refundRecord.setOperatorName(opUser.getName());
            }
        }
        // 退款记录单号
        String totalCode = systemParameterService.createRecordNo("R", 16, "yyyyMMddHHmmss");
        refundRecord.setRefundType(1);
        refundRecord.setCode(totalCode);
        refundRecord.setUserId(user.getId());
        refundRecord.setRefundStatus(OrderTotalConstants.STATUS_REFUND_APPLY);
        refundRecord.setRefundShopId(shop.getId());
        refundRecord.setRefundShopName(shop.getName());
        refundRecord.setRefundTotal(refundFundsDto.getRefundTotal());
        refundRecord.setRealRefundTotal(refundFundsDto.getRefundTotal());
        refundRecord.setSourceType(RefundSoruceConstants.TYPE_SOURCE_USER);
        refundRecord.setRemarks(refundRecord.getRemarks());
        // 插入数据
        int count = refundRecordMapper.insert(refundRecord);
        if (count != 1) {
            throw new TipsException("新增失败!");
        } else {
            // 获取银行卡退款方式
            PaymentMethod bank = getPaymentMethodByCode("PM95");
            if (bank == null){
                throw new PlatTipsException(PlatformCode.ERROR_TIPS, "银行卡支付方式不存在");
            }
            // 获取储值金退款方式
            PaymentMethod storedValueFund = getPaymentMethodByCode("00000");
            if (storedValueFund == null){
                throw new PlatTipsException(PlatformCode.ERROR_TIPS, "储值金支付方式不存在");
            }
 
            // 退款方式转换表
            RefundMethodTransformation refundMethodTransformation = new RefundMethodTransformation();
            refundMethodTransformation.setRefundRecordId(refundRecord.getId());
            refundMethodTransformation.setPayMethodNo(storedValueFund.getNumberNo());
            refundMethodTransformation.setPayMethodName(storedValueFund.getName());
            refundMethodTransformation.setPayTotal(refundFundsDto.getRefundTotal());
            refundMethodTransformation.setRefundNumberNo(bank.getNumberNo());
            refundMethodTransformation.setRefundNumberName(bank.getName());
            refundMethodTransformation.setRefundTotal(refundFundsDto.getRefundTotal());
            commonDao.insert(RefundMethodTransformationMapper.class, refundMethodTransformation);
 
            // 退款方式处理
            RefundRecordMethod refundRecordMethod = new RefundRecordMethod();
            refundRecordMethod.setNumberNo(storedValueFund.getNumberNo());
            refundRecordMethod.setRefundNumberNo(bank.getNumberNo());
            refundRecordMethod.setName(storedValueFund.getName());
            refundRecordMethod.setPaymentMethodId(storedValueFund.getId());
            refundRecordMethod.setActualTotal(refundFundsDto.getRefundTotal());
            refundRecordMethod.setRealRefundTotal(refundFundsDto.getRefundTotal());
            refundRecordMethod.setIsMoneyPay(BaseEntity.NO);
            refundRecordMethod.setIsExecute(BaseEntity.NO);
            refundRecordMethod.setIsPay(BaseEntity.NO);
            refundRecordMethod.setType(ConsumePayConstants.STATUS_WITHDRAW);
            refundRecordMethod.setRefundRecordId(refundRecord.getId());
            refundRecordMethod.setPayee(refundFundsDto.getPayee());
            refundRecordMethod.setOpenBank(refundFundsDto.getOpenBank());
            refundRecordMethod.setOpenBranch(refundFundsDto.getOpenBranch());
            refundRecordMethod.setAccount(refundFundsDto.getAccount());
            refundRecordMethod.setRemarks(refundFundsDto.getRemarks());
            commonDao.insert(RefundRecordMethodMapper.class, refundRecordMethod);
 
            // 退款审批处理
            RefundApprove refundApprove = new RefundApprove();
            refundApprove.setRefundRecordId(refundRecord.getId());
            refundApprove.setOperatorType(refundRecord.getOperatorType());
            refundApprove.setOperatorId(refundApprove.getOperatorId());
            refundApprove.setOperatorName(refundApprove.getOperatorName());
            Employee opEmployee = commonDao.selectOneByKey(EmployeeMapper.class, refundFundsDto.getOperatorId());
            if (opEmployee != null) {
                refundApprove.setOperatorType(RefundRecord.OPERATOR_TYPE_EMPLOYEE);
                refundApprove.setOperatorId(opEmployee.getId());
                refundApprove.setOperatorName(opEmployee.getCnName());
                refundApprove.setRoleId(opEmployee.getRoleId());
                refundApprove.setRoleStr(opEmployee.getRoleStr());
            }
            refundApprove.setShopId(shop.getId());
            refundApprove.setShopName(shop.getName());
            refundApprove.setIsApproval(BaseEntity.YES);
            refundApprove.setPlatformSource(refundFundsDto.getPlatformSource());
            commonDao.insert(RefundApproveMapper.class, refundApprove);
            // 先减扣冻结用户资金处理
            this.opUserMoneyUnclaimed(refundRecord, BaseEntity.YES);
        }
        return refundRecord.getId();
    }
 
    /**
     * 用户资金退款审核
     * @param refundFundsDto 参数
     */
    @Override
    public void refundUserFundsApprove(RefundFundsDto refundFundsDto) {
        RefundRecord refundRecord = refundRecordMapper.selectOneByKey(refundFundsDto.getRefundId());
        if (refundRecord == null){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS, "退款记录不存在");
        }
        if(OrderTotalConstants.STATUS_REFUND_APPLY != refundRecord.getRefundStatus()
                && OrderTotalConstants.STATUS_REFUND_WAIT != refundRecord.getRefundStatus()) {
            throw new PlatTipsException(PlatformCode.ERROR_TIPS, "退款记录已被操作请不要重复操作!");
        }
        RefundRecordMethod refundRecordMethod = getRefundRecordMethodByRefundRecordId(refundRecord.getId());
        if (refundRecordMethod == null){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS, "退款记录的退款方式不存在");
        }
        if (!ConsumePayConstants.STATUS_WITHDRAW.equals(refundRecordMethod.getType())){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS, "退款记录不是提现单!");
        }
        // 审核通过处理
        if (BaseEntity.YES.equals(refundFundsDto.getType())){
            refundRecord.setRefundStatus(OrderTotalConstants.STATUS_REFUND_FINSH);
        } else {
            refundRecord.setRefundStatus(OrderTotalConstants.STATUS_REFUND_REFUSED);
        }
        // 备注处理
        if (!StringUtils.isEmpty(refundFundsDto.getRemarks())) {
            refundRecord.setRemarks(refundFundsDto.getRemarks());
        }
        refundRecord.setEditTime(new Date());
        int count = refundRecordMapper.updateAll(refundRecord);
        if(count!=1) {
            throw new TipsException("保存失败!");
        } else {
            // 审核通过处理
            if (BaseEntity.YES.equals(refundFundsDto.getType())){
                refundRecordMethod.setIsPay(BaseEntity.YES);
                refundRecordMethod.setEditTime(new Date());
                // 更新数据
                commonDao.updateAll(RefundRecordMethodMapper.class, refundRecordMethod);
                // 审核成功
                handlerApprove(refundFundsDto, refundRecord, BaseEntity.YES);
            } else {
                // 审核失败
                handlerApprove(refundFundsDto, refundRecord, BaseEntity.NO);
                // 审核不通过退回资金
                this.opUserMoneyUnclaimed(refundRecord, BaseEntity.NO);
            }
        }
    }
 
    /**
     * 用户资金退款作废
     * @param refundFundsDto 参数
     */
    @Override
    public void refundUserFundsCancel(RefundFundsDto refundFundsDto) {
        RefundRecord refundRecord = refundRecordMapper.selectOneByKey(refundFundsDto.getRefundId());
        if (refundRecord == null){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS, "退款记录不存在");
        }
        if(OrderTotalConstants.STATUS_REFUND_APPLY != refundRecord.getRefundStatus()
                && OrderTotalConstants.STATUS_REFUND_WAIT != refundRecord.getRefundStatus()) {
            throw new PlatTipsException(PlatformCode.ERROR_TIPS, "退款记录已被操作请不要重复操作!");
        }
        RefundRecordMethod refundRecordMethod = getRefundRecordMethodByRefundRecordId(refundRecord.getId());
        if (refundRecordMethod == null){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS, "退款记录的退款方式不存在");
        }
        if (!ConsumePayConstants.STATUS_WITHDRAW.equals(refundRecordMethod.getType())){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS, "退款记录不是提现单!");
        }
        // 作废处理
        refundRecord.setRefundStatus(OrderTotalConstants.STATUS_REFUND_REVOKE);
        // 备注处理
        if (!StringUtils.isEmpty(refundFundsDto.getRemarks())) {
            refundRecord.setRemarks(refundFundsDto.getRemarks());
        }
        refundRecord.setEditTime(new Date());
        int count = refundRecordMapper.updateAll(refundRecord);
        if(count!=1) {
            throw new TipsException("保存失败!");
        } else {
            // 审核失败
            handlerApprove(refundFundsDto, refundRecord, BaseEntity.NO);
            // 审核不通过退回资金
            this.opUserMoneyUnclaimed(refundRecord, BaseEntity.NO);
        }
    }
}