chenjiahe
2023-04-01 510678fe2f81501914ea4905fb7026b9c7be414f
提交 | 用户 | age
a1c527 1 package com.hx.phip.tool.refund;
ae6ff7 2
C 3 import com.hx.common.service.CommonService;
4 import com.hx.exception.TipsException;
5 import com.hx.mybatisTool.SqlSentence;
6 import com.hx.phiappt.common.*;
7 import com.hx.phiappt.constants.enums.GroupTypeEnum;
8 import com.hx.phiappt.constants.tool.RefundToolUtil;
9 import com.hx.phiappt.model.BaseEntity;
10 import com.hx.phiappt.model.UserMoney;
11 import com.hx.phiappt.model.activity.ActivityAction;
12 import com.hx.phiappt.model.activity.ActivityRule;
a1c527 13 import com.hx.phiappt.model.cardItem.CardEquity;
C 14 import com.hx.phiappt.model.cardItem.CardItemInfo;
ae6ff7 15 import com.hx.phiappt.model.consume.ConsumePay;
C 16 import com.hx.phiappt.model.consume.ConsumePayItem;
17 import com.hx.phiappt.model.coupon.CouponNumber;
18 import com.hx.phiappt.model.coupon.CouponOrderDiscountLog;
19 import com.hx.phiappt.model.order.*;
20 import com.hx.phiappt.model.refund.*;
21 import com.hx.phiappt.model.user.UserCard;
22 import com.hx.phiappt.model.user.UserCardUsed;
23 import com.hx.phiappt.model.user.UserProjectItem;
24 import com.hx.phiappt.model.userMoney.UserMoneyUnclaimed;
25 import com.hx.phip.dao.mapper.*;
26 import com.hx.phip.tool.user.UserProjectTool;
a1c527 27 import com.hx.phip.util.api.UserMoneyUtil;
1869f3 28 import com.hx.phip.vo.order.refund.RefundCarryVo;
C 29 import com.hx.phip.vo.user.UserProjectDeductionVo;
ae6ff7 30 import com.hx.util.StringUtils;
C 31 import com.platform.exception.PlatTipsException;
32 import com.platform.resultTool.PlatformCode;
33
34 import java.math.BigDecimal;
35 import java.math.RoundingMode;
36 import java.util.*;
37 import java.util.stream.Collectors;
38
39 /**
40  * @Author
41  */
42 public class PartialRefundUtil {
43
44     /**领建优惠券标识*/
45     public static final String HIS_COUPON_CODE = "his_coupon_code";
46
47     /**
48      * 退款总流程工具
a1c527 49      * @param commonService 映射
C 50      * @param operationId 操作人标识
51      * @param operationNme 操作人名称
52      * @param refundId 退款总单标识
ae6ff7 53      */
a1c527 54     public static OrdersTotal refundProcess(CommonService commonService, String operationId, String operationNme, String refundId) {
ae6ff7 55
C 56         RefundRecord refundRecord = commonService.selectOneByKeyBlob(RefundRecordMapper.class,refundId);
57         if(refundRecord ==null){
58             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"找不到该退款信息!");
59         }
60
61         SqlSentence sqlSentence = new SqlSentence();
62         Map<String,Object> values = new HashMap<>();
63
64         //退款总订单状态变更
65         values.put("refundStatus", RefundStatus.STATUS_SUCC_REFUND);
66         values.put("oldRefundStatus", RefundStatus.STATUS_APPLY_REFUND);
67         values.put("isDel", BaseEntity.NO);
68         values.put("id",refundRecord.getId());
a1c527 69         sqlSentence.sqlSentence(" refundStatus=#{m.refundStatus} WHERE id = #{m.refundId} AND isDel=#{m.isDel} AND refundStatus = #{m.oldRefundStatus}",values);
ae6ff7 70         if(commonService.updateWhere(RefundRecordMapper.class,sqlSentence) != 1){
C 71             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"操作失败,退款单状态已改变!");
72         }
73
510678 74         //获取订单信息
ae6ff7 75         OrdersTotal ordersTotal = commonService.selectOneByKeyBlob(OrdersTotalMapper.class,refundRecord.getOrderId());
C 76         if(ordersTotal == null ){
77             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到订单信息!");
78         }
79
80         //查询用户是否有账户信息,退款需要处理资金
a1c527 81         values.clear();
ae6ff7 82         values.put("userId",refundRecord.getUserId());
C 83         sqlSentence.setSqlSentence("SELECT * FROM user_money WHERE  userId=#{m.userId} AND isDel=0");
84         UserMoney userMoney=commonService.selectOne(UserMoneyMapper.class,sqlSentence);
85         if(userMoney==null){
86             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到该用户的资金信息");
87         }
1869f3 88
C 89         //获取总退款方式
90         values.put("refundRecordId",refundRecord.getId());
91         sqlSentence.sqlSentence("SELECT * FROM refund_record_method WHERE isDel = 0 AND refundRecordId = #{m.refundRecordId}",values);
92         List<RefundRecordMethod> refundRecordMethodList = commonService.selectList(RefundRecordMethodMapper.class,sqlSentence);
93
94         //全程使用携带参数对象
95         RefundCarryVo refundCarryVo = new RefundCarryVo();
96         refundCarryVo.setRefundRecordMethodList(refundRecordMethodList);
ae6ff7 97
C 98         if(OrderTotalConstants.TYPE_RECHARGE.equals(ordersTotal.getType())){
99             //充值订单需要把充值的储值金 拿回来   因为储值金需要开单来处理,所以不需要增加日志
100             //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
101             if(ordersTotal.getActualTotal().compareTo(BigDecimal.ZERO)!=0){
102                 //交给审批前操作
103              /*   UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"审核通过退款:支付方式储值金额退回",operationId,refundRecord.getOrderId(),appIdCode,refundRecord.getId(),new BigDecimal(String.valueOf(ordersTotal.getTotal())).negate(), UserMoneyUnclaimed.FUND_TYPE_STORED_VALUE_FUND, OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.NO);
104                 OrderLog orderLog = RefundToolUtil.setOrderLog(refundRecord, operationId, operationNme, "储值金客户退回金额:"+ordersTotal.getTotal(), 0, OrderLogConstants.LOG_TYPE_REFUND);
105                 commonService.insert(OrderLogMapper.class,orderLog);*/
106                 //活动规则退款处理
107                 // handActivityRule(commonService, operationId, operationNme, sqlSentence, map, refundRecord,ordersTotal,orderInfo);
108             }
a1c527 109             //处理支付方式和退款方式关联
C 110             refundCarryVo = rechargeRefundMothedHandle(refundCarryVo,refundRecord,commonService);
111         }else{
112             //子单退款项处理
113             refundCarryVo = numberOfRefunds(refundCarryVo,operationId, refundRecord,ordersTotal,commonService);
ae6ff7 114         }
a1c527 115         //处理优惠券
C 116         handCoupon(refundRecord,commonService);
117         //处理总退款方式数据
118         refundCarryVo = refundRecordMotnedHandle(refundCarryVo,operationId,operationNme,refundRecord,ordersTotal,commonService);
ae6ff7 119
C 120         //更改总订单退款状态
121         values.clear();
122         values.put("orderId",ordersTotal.getId());
123         sqlSentence.setSqlSentence("select * from order_item WHERE  orderId=#{m.orderId} and isDel=0");
124         List<OrderItem> orderItemList=commonService.selectList(OrderItemMapper.class,sqlSentence);
125
a1c527 126         List<Integer> collect = orderItemList.stream().map(OrderItem::getRefundStatus).collect(Collectors.toList());
ae6ff7 127         if(collect.contains(OrderTotalConstants.STATUS_REFUND_PART)){
C 128             ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_PART);
129             ordersTotal.setReTotal(orderItemList.stream().map(OrderItem::getReTotal).reduce(BigDecimal.ZERO,BigDecimal::add));
130         }else if (collect.contains(OrderTotalConstants.STATUS_REFUND_NONE) && collect.contains(OrderTotalConstants.STATUS_REFUND_FINSH)){
131             ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_PART);
132             ordersTotal.setReTotal(orderItemList.stream().map(OrderItem::getReTotal).reduce(BigDecimal.ZERO,BigDecimal::add));
133         }else if (collect.contains(OrderTotalConstants.STATUS_REFUND_NONE)){
134             ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_NONE);
135         }else if (collect.contains(OrderTotalConstants.STATUS_REFUND_FINSH)){
136             ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_FINSH);
137             ordersTotal.setStatus(OrderTotalConstants.STATUS_CANCEL);
138             ordersTotal.setReTotal(orderItemList.stream().map(OrderItem::getReTotal).reduce(BigDecimal.ZERO,BigDecimal::add));
139         }else {
140             if(OrderTotalConstants.TYPE_RECHARGE.equals(ordersTotal.getType())){
141                 if(ordersTotal.getActualTotal().compareTo(refundRecord.getRefundTotal()) <= 0){
142                     ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_FINSH);
143                 }else{
144                     ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_PART);
145                 }
146             }else{
147                 ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_NONE);
148             }
149         }
150         ordersTotal.setIsSyncOrder(BaseEntity.NO);
151         ordersTotal.setEditTime(new Date());
152         commonService.updateAll(OrdersTotalMapper.class,ordersTotal);
153         return ordersTotal;
154     }
155
156     /**处理总退款方式数据*/
a1c527 157     public static RefundCarryVo refundRecordMotnedHandle(RefundCarryVo refundCarryVo,String operationId,String operationNme
C 158             ,RefundRecord refundRecord,OrdersTotal ordersTotal,CommonService commonService){
ae6ff7 159         SqlSentence sqlSentence = new SqlSentence();
C 160         Map<String,Object> map = new HashMap<>();
161
162         //获取总退款方式
a1c527 163         List<RefundRecordMethod> refundRecordMethodList = refundCarryVo.getRefundRecordMethodList();
ae6ff7 164
a1c527 165         List<RefundRecordConsumePay> refundConsumePayList = refundCarryVo.getRefundConsumePayList();
C 166         Map<String,List<RefundRecordConsumePay>> refundConsumePayMap= new HashMap<>();
167         List<RefundRecordConsumePay> refundRecordConsumePays;
168         for(RefundRecordConsumePay refundRecordConsumePay:refundConsumePayList){
169             refundRecordConsumePays = refundConsumePayMap.computeIfAbsent(refundRecordConsumePay.getNumberNo(),k->new ArrayList<>());
170             refundRecordConsumePays.add(refundRecordConsumePay);
171         }
ae6ff7 172
a1c527 173         List<RefundRecordConsumePay> refundRecordConsumePayList = new ArrayList<>();
C 174         RefundRecordConsumePay refundRecordConsumePay1;
ae6ff7 175
C 176         for(RefundRecordMethod refundRecordMethod:refundRecordMethodList){
a1c527 177             if(refundRecordMethod.getpTotal().compareTo(BigDecimal.ZERO) > 0){
C 178                 throw new TipsException("退款错误[20]!");
179             }
180             refundRecordConsumePays = refundConsumePayMap.get(refundRecordMethod.getNumberNo());
181             if(refundRecordConsumePays == null){
182                 throw new TipsException("退款错误[21]!");
183             }
184             for(RefundRecordConsumePay refundRecordConsumePay:refundRecordConsumePays){
185                 //更新支付记录退款信息
186                 map.clear();
187                 map.put("refundTotal",refundRecordConsumePay.getRefundTotal());
188                 map.put("id",refundRecordConsumePay.getConsumePayId());
189                 sqlSentence.sqlUpdate("refundTotal = refundTotal + #{m.refundTotal} WHERE id = #{m.id} AND actualTotal >= refundTotal + #{m.refundTotal}",map);
190                 if(commonService.updateWhere(ConsumePayMapper.class,sqlSentence) != 1){
191                     throw new TipsException("退款金额错误[33]!");
192                 }
193
194                 //生成关联记录
195                 refundRecordConsumePay1 = insertRefundRecordConsumePay(refundRecordConsumePay.getRefundTotal(),null,null,refundRecord.getOrderId(),refundRecordConsumePay.getNumberNo()
196                         ,refundRecordConsumePay.getName(),refundRecordConsumePay.getConsumePayId(),refundRecordMethod.getId(),null,refundRecord.getId(),commonService);
197                 refundRecordConsumePayList.add(refundRecordConsumePay1);
198             }
199             //修改实际退款金额
200             map.put("realRefundTotal",refundRecordMethod.getActualTotal());
201             map.put("id",refundRecordMethod.getId());
202             sqlSentence.sqlUpdate(" realRefundTotal = realRefundTotal + #{m.realRefundTotal} where id = #{m.id}",map);
203             if(commonService.updateWhere(RefundRecordMethodMapper.class,sqlSentence) != 1){
204                 throw new TipsException("更新退款方式信息错误[001]");
205             }
206
207         }
208         refundCarryVo.setRefundConsumePayList(refundRecordConsumePayList);
209
210         //处理回退到账
211         for(RefundRecordMethod refundRecordMethod:refundRecordMethodList){
ae6ff7 212             if(PayMethodTypeConstants.PAY_STORED.equals(refundRecordMethod.getRefundNumberNo())){
C 213                 //储值金额 
214                 //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
215                 if(refundRecordMethod.getActualTotal().compareTo(BigDecimal.ZERO)>0){
216                     UserMoneyUtil.setNewUserMoneyUnclaimed(ordersTotal.getPayUserId()==null?ordersTotal.getUserId():ordersTotal.getPayUserId(),refundRecord.getRemarks(),"审核通过退款:支付方式储值金额退回",operationId,refundRecord.getOrderId(),refundRecord.getOperatorAppCode(),refundRecord.getId(),refundRecordMethod.getActualTotal(), UserMoneyUnclaimed.FUND_TYPE_STORED_VALUE_FUND, OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.NO);
217                     OrderLog orderLog = RefundToolUtil.setOrderLog(refundRecord, operationId, operationNme, refundRecordMethod.getName()+"退款金额:"+refundRecordMethod.getActualTotal(), 0, OrderLogConstants.LOG_TYPE_REFUND);
218                     commonService.insert(OrderLogMapper.class,orderLog);
219                 }
220             }else if(PayMethodTypeConstants.PAY_ADD_FUND.equals(refundRecordMethod.getRefundNumberNo())){
221                 //增值金
222                 //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
223                 if(refundRecordMethod.getActualTotal().compareTo(BigDecimal.ZERO)!=0){
224                     UserMoneyUtil.setNewUserMoneyUnclaimed(ordersTotal.getPayUserId()==null?ordersTotal.getUserId():ordersTotal.getPayUserId(),refundRecord.getRemarks(),"审核通过退款:支付方式增值金退回",operationId,refundRecord.getOrderId(),refundRecord.getOperatorAppCode(),refundRecord.getId(),refundRecordMethod.getActualTotal(), UserMoneyUnclaimed.FUND_TYPE_VALUE_ADDED_FUND, OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES);
225                     OrderLog orderLog = RefundToolUtil.setOrderLog(refundRecord,operationId,operationNme,refundRecordMethod.getName(),0, OrderLogConstants.LOG_TYPE_REFUND);
226                     commonService.insert(OrderLogMapper.class,orderLog);
227                 }
228             }else if(PayMethodTypeConstants.PAY_INTEGRAL.equals(refundRecordMethod.getRefundNumberNo())){
229                 //积分
230                 //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
231                 if(refundRecordMethod.getActualTotal().compareTo(BigDecimal.ZERO)!=0){
232                     UserMoneyUtil.setNewUserMoneyUnclaimed(ordersTotal.getPayUserId()==null?ordersTotal.getUserId():ordersTotal.getPayUserId(),refundRecord.getRemarks(),"审核通过退款:支付方式积分退回",operationId,refundRecord.getOrderId(),refundRecord.getOperatorAppCode(),refundRecord.getId(),refundRecordMethod.getActualTotal(), UserMoneyUnclaimed.FUND_TYPE_INTEGRAL,OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES);
233                     OrderLog orderLog = RefundToolUtil.setOrderLog(refundRecord,operationId,operationNme,refundRecordMethod.getName()+"退款金额:"+refundRecordMethod.getActualTotal(),0, OrderLogConstants.LOG_TYPE_REFUND);
234                     commonService.insert(OrderLogMapper.class,orderLog);
235                 }
236             }else {
237                 //现金支付
238                 if(refundRecordMethod.getActualTotal().compareTo(BigDecimal.ZERO)<1){
239                     continue;
240                 }
241                 OrderLog orderLog = RefundToolUtil.setOrderLog(refundRecord,operationId,operationNme,refundRecordMethod.getName()+"退款金额:"+refundRecordMethod.getActualTotal(),0, OrderLogConstants.LOG_TYPE_REFUND);
242                 orderLog.setOrderId(ordersTotal.getId());
243                 commonService.insert(OrderLogMapper.class,orderLog);
244
245                 //现金支付需要创建创建退款单进行退款
246                 RefundNote refundNote = new RefundNote(refundRecordMethod.getActualTotal(), refundRecordMethod.getNumberNo(),refundRecordMethod.getName(),ordersTotal.getId(),refundRecord.getUserId());
247                 refundNote.setRefundRecordId(refundRecord.getId());
248                 commonService.insert(RefundNoteMapper.class,refundNote);
249             }
a1c527 250         }
C 251         return refundCarryVo;
252     }
253
254     /**充值订单退款方式的支付方式记录处理*/
255     public static RefundCarryVo rechargeRefundMothedHandle(RefundCarryVo refundCarryVo,RefundRecord refundRecord,CommonService commonService){
256
257         SqlSentence sqlSentence = new SqlSentence();
258         Map<String,Object> map = new HashMap<>();
259
260         List<RefundRecordMethod> refundRecordMethodList = refundCarryVo.getRefundRecordMethodList();
261
510678 262         //装载分配好的支付方式记录的金额数据
a1c527 263         List<RefundRecordConsumePay> refundConsumePayList = new ArrayList<>();
C 264         RefundRecordConsumePay refundRecordConsumePay;
265
510678 266         ///////获取订单支付方式记录
C 267         map.put("orderId",refundRecord.getOrderId());
268         sqlSentence.sqlSentence("SELECT *,ROUND(actualTotal-refundTotal,2) AS pTotal FROM consume_pay WHERE isDel = 0 AND orderId = #{m.orderId} ORDER BY pTotal ASC",map);
269         List<ConsumePay> consumePayList = commonService.selectList(ConsumePayMapper.class,sqlSentence);
270         //根据支付编号进行区分,key值:支付编号
271         Map<String,List<ConsumePay>> consumePayMap = new HashMap<>();
272         List<ConsumePay> consumePays;
273         for(ConsumePay consumePay:consumePayList){
274             consumePays = consumePayMap.computeIfAbsent(consumePay.getNumberNo(),k->new ArrayList<>());
275             consumePays.add(consumePay);
276         }
277
278         ////引用对象
279         //退款方式的金额
a1c527 280         BigDecimal mothedTotal;
510678 281         //分配支付方式金额
a1c527 282         BigDecimal mothedCutTotal;
C 283         for(RefundRecordMethod refundRecordMethod:refundRecordMethodList){
510678 284             if(refundRecordMethod.getActualTotal().compareTo(BigDecimal.ZERO) < 1){
C 285                 continue;
286             }
a1c527 287             mothedTotal = refundRecordMethod.getActualTotal();
510678 288             consumePays = consumePayMap.get(refundRecordMethod.getNumberNo());
C 289             if(consumePays == null){
290                 throw new TipsException("没有找到该支付记录:"+refundRecordMethod.getName()+"["+refundRecordMethod.getNumberNo()+"]");
291             }
292             for(ConsumePay consumePay:consumePays){
ae6ff7 293                 if(consumePay.getpTotal().compareTo(BigDecimal.ZERO) < 1){
C 294                     continue;
295                 }
296                 //计算扣减金额
297                 if(consumePay.getpTotal().compareTo(mothedTotal) > 0){
298                     mothedCutTotal = mothedTotal;
299                 }else{
300                     mothedCutTotal = consumePay.getpTotal();
301                 }
a1c527 302                 //减去已经分配的金额
ae6ff7 303                 mothedTotal = mothedTotal.subtract(mothedCutTotal).setScale(2, RoundingMode.HALF_UP);
a1c527 304
C 305                 refundRecordConsumePay = new RefundRecordConsumePay();
306                 refundRecordConsumePay.setNumberNo(consumePay.getNumberNo());
307                 refundRecordConsumePay.setName(consumePay.getName());
308                 refundRecordConsumePay.setRefundTotal(mothedCutTotal);
309                 refundRecordConsumePay.setConsumePayId(consumePay.getId());
310                 refundConsumePayList.add(refundRecordConsumePay);
311
ae6ff7 312                 //已经分配完成,跳出循环
C 313                 if(mothedTotal.compareTo(BigDecimal.ZERO) < 1){
314                     //跳出循环
315                     break;
316                 }
317             }
318             //判断退款金额是否已经全部分配
319             if(mothedTotal.compareTo(BigDecimal.ZERO) > 0){
320                 throw new TipsException("退款金额错误[426]!");
321             }
a1c527 322             refundRecordMethod.setpTotal(BigDecimal.ZERO);
ae6ff7 323         }
a1c527 324         refundCarryVo.setRefundConsumePayList(refundConsumePayList);
C 325         return refundCarryVo;
ae6ff7 326     }
C 327
1869f3 328     /**处理一级子退款方式数据
C 329      * @param deductionTotalUser 用户项目被扣减的划扣金额,可空
330      * @param refundRecord 退款记录总表
331      * @param refundRecordItem 退款记录子表
332      * @param refundCarryVo 总携带参数结构
333      * @param commonService 映射
334      * @return 总携带参数结构
335      */
336     public static RefundCarryVo refundRecordMotnedItemHandle(BigDecimal deductionTotalUser,RefundRecord refundRecord,RefundRecordItem refundRecordItem
337             ,RefundCarryVo refundCarryVo,CommonService commonService){
338
510678 339         //没有退款方式,跳过当前处理,因为这里是处理退款方式金额的,如果没有退款金额,那么可以跳过该环节
1869f3 340         if(refundCarryVo.getRefundRecordMethodList().size() == 0){
C 341             return refundCarryVo;
342         }
343         //支付方式占比
344         BigDecimal payMothedPercentage;
345         //计算退款方式的占比
346         if(refundRecord.getRefundTotal().compareTo(BigDecimal.ZERO) > 0){
347             payMothedPercentage = refundRecordItem.getRefundMoney().divide(refundRecord.getRefundTotal(),15,RoundingMode.HALF_UP);
348         }else{
349             payMothedPercentage = BigDecimal.ZERO;
ae6ff7 350         }
C 351
1869f3 352         //处理退款方式
C 353         refundCarryVo = insertRefundItemMothed(refundRecordItem.getId(),refundRecordItem.getRefundMoney(),payMothedPercentage,OrderSourceConstans.TYPE_PROJECT
354                 ,refundRecordItem.getOrderItemId(),refundCarryVo,refundRecord,commonService);
a1c527 355
C 356         SqlSentence sqlSentence = new SqlSentence();
357         Map<String,Object> values = new HashMap<>();
ae6ff7 358
510678 359         //更新退款子单的信息,更新现金和划扣金额保存
1869f3 360         values.put("deductionTotal",refundCarryVo.getDeductionTotal());
C 361         values.put("deductionTotalUser",deductionTotalUser==null?BigDecimal.ZERO:deductionTotalUser);
362         values.put("cashTotal",refundCarryVo.getCashTotal());
363         values.put("id",refundRecordItem.getId());
364         sqlSentence.sqlUpdate("deductionTotal = #{m.deductionTotal},deductionTotalUser = #{m.deductionTotalUser},cashTotal = #{m.cashTotal} WHERE id = #{m.id}",values);
365         if(commonService.updateWhere(RefundRecordItemMapper.class,sqlSentence) != 1){
366             throw new TipsException("更新退款子项信息失败!");
367         }
368
369         return refundCarryVo;
370     }
371
372     /**处理二级子退款方式数据
373      * @param deductionTotalUser 用户项目被扣减的划扣金额
374      * @param refundRecord  退款记录总表
375      * @param refundRecordItemSource 退款记录子表
376      * @param refundCarryVo 总携带参数结构
377      * @param commonService 映射
378      * @return 总携带参数结构
379      */
380     public static RefundCarryVo refundRecordMotnedItemTwoHandle(BigDecimal deductionTotalUser,RefundRecord refundRecord,RefundRecordItemSource refundRecordItemSource
381             ,RefundCarryVo refundCarryVo,CommonService commonService){
382
383         //装载支付方式退款信息
384         refundCarryVo.setRefundConsumePayList(new ArrayList<>());
385         //没有退款方式,跳过当前处理
386         if(refundCarryVo.getRefundRecordMethodList().size() == 0){
387             return refundCarryVo;
388         }
389
390         SqlSentence sqlSentence = new SqlSentence();
391         Map<String,Object> values = new HashMap<>();
392
393         //支付方式占比
394         BigDecimal payMothedPercentage;
395         //计算退款方式的占比
396         if(refundRecord.getRefundTotal().compareTo(BigDecimal.ZERO) > 0){
397             payMothedPercentage = refundRecordItemSource.getRefundMoney().divide(refundRecord.getRefundTotal(),15,RoundingMode.HALF_UP);
398         }else{
399             payMothedPercentage = BigDecimal.ZERO;
400         }
401
402         //处理退款方式
403         refundCarryVo = insertRefundItemMothed(refundRecordItemSource.getId(),refundRecordItemSource.getRefundMoney(),payMothedPercentage,OrderSourceConstans.TYPE_RETAIL
404                 ,refundRecordItemSource.getOrderItemSonId(),refundCarryVo,refundRecord,commonService);
405
406         values.put("deductionTotal",refundCarryVo.getDeductionTotal());
407         values.put("deductionTotalUser",deductionTotalUser==null?BigDecimal.ZERO:deductionTotalUser);
408         values.put("cashTotal",refundCarryVo.getCashTotal());
409         values.put("id",refundRecordItemSource.getId());
410         sqlSentence.sqlUpdate("deductionTotal = #{m.deductionTotal},deductionTotalUser = #{m.deductionTotalUser},cashTotal = #{m.cashTotal} WHERE id = #{m.id}",values);
411         if(commonService.updateWhere(RefundRecordItemSourceMapper.class,sqlSentence) != 1){
412             throw new TipsException("更新退款子项信息失败[94]!");
413         }
414
415         return refundCarryVo;
416     }
417
418     /**退款子单退款方式处理保存
419      * @param refundItemId 退款子单标识
420      * @param refundItemTotal 退款子单实际需要退款总金额
421      * @param payMothedPercentage 支付方式占比
422      * @param orderItemType 订单子单级别
423      * @param orderItemId 订单子单标识
424      * @param refundCarryVo 总结构对象
425      * @param refundRecord 退款总记录
426      * @param commonService 映射
427      * @return 总结构对象
428      */
429     public static RefundCarryVo insertRefundItemMothed(String refundItemId,BigDecimal refundItemTotal,BigDecimal payMothedPercentage,String orderItemType
430             ,String orderItemId,RefundCarryVo refundCarryVo,RefundRecord refundRecord,CommonService commonService){
431
432         //总退款方式金额数据
433         List<RefundRecordMethod> refundRecordMethodList = refundCarryVo.getRefundRecordMethodList();
434         //进行升序排序,避免后面不够分配
435         refundRecordMethodList = refundRecordMethodList.stream().sorted(Comparator.comparing(RefundRecordMethod::getpTotal)).collect(Collectors.toList());
436
510678 437         //获取子单的支付方式记录,计算可退款金额
1869f3 438         List<ConsumePayItem> consumePayItemList;
C 439         if(OrderSourceConstans.TYPE_PROJECT.equals(orderItemType)){
510678 440             //一级订单
1869f3 441             consumePayItemList = getOrderItemOneConsumePay(orderItemId,commonService);
C 442         }else{
510678 443             //二级订单
1869f3 444             consumePayItemList = getOrderItemTwoConsumePay(orderItemId,commonService);
C 445         }
446
510678 447         ////存储支付方式编号的可退款金额,根据支付编号求和,后面的业务判断会用到,key值:支付编号,value:可退金额
1869f3 448         Map<String,BigDecimal> noMap = new HashMap<>();
C 449         BigDecimal surplusTotal;
510678 450         ////存储支付方式编号的支付方式记录,根据支付编号整合,后面的业务会用到,key值:支付编号,value:支付记录集合
1869f3 451         Map<String,List<ConsumePayItem>> noPayItemMap = new HashMap<>();
C 452         List<ConsumePayItem> noPayItemList;
453
454         ////////填充支付方式记录的退款金额,计算剩余可退金额,支付方式记录的退款金额需要去查询计算获取
510678 455         //获取订单子单已经退款的退款方式金额,根据支付方式记录的标识求和返回
1869f3 456         List<RefundRecordConsumePay> refundRecordConsumePayList = getRefundRecordConsumePay(orderItemId,null,false,true,commonService);
C 457         //转化成map,可以根据支付方式记录的标识直接获取到数据
ae6ff7 458         Map<String, RefundRecordConsumePay> refundRecordConsumePayMap = refundRecordConsumePayList.stream().collect(
C 459                 Collectors.toMap(RefundRecordConsumePay::getConsumePayId,(a) -> a));
460         RefundRecordConsumePay refundRecordConsumePay;
461         for(ConsumePayItem consumePayItem:consumePayItemList){
462             refundRecordConsumePay = refundRecordConsumePayMap.get(consumePayItem.getConsumePayId());
463             if(refundRecordConsumePay != null){
464                 //已退款金额
465                 consumePayItem.setRefundTotal(refundRecordConsumePay.getRefundTotal());
466                 //可退款金额
467                 consumePayItem.setpTotal(consumePayItem.getpTotal().subtract(refundRecordConsumePay.getRefundTotal()).setScale(2,RoundingMode.HALF_UP));
468             }
1869f3 469             //计算每个支付编码可退款金额
C 470             surplusTotal = noMap.computeIfAbsent(consumePayItem.getNumberNo(),k->BigDecimal.ZERO);
471             surplusTotal = surplusTotal.add(consumePayItem.getpTotal()).setScale(2,RoundingMode.HALF_UP);
472             noMap.put(consumePayItem.getNumberNo(),surplusTotal);
ae6ff7 473
1869f3 474             //支付编码集合整合
C 475             noPayItemList = noPayItemMap.computeIfAbsent(consumePayItem.getNumberNo(),k->new ArrayList<>());
476             noPayItemList.add(consumePayItem);
477         }
ae6ff7 478
1869f3 479         List<RefundRecordConsumePay> refundConsumePayList = new ArrayList<>();
C 480
481         ////引用对象
ae6ff7 482         RefundRecordItemMethod refundRecordItemMethod;
1869f3 483         RefundRecordMethod refundRecordMethod;
C 484         //需要退的退款编号金额
485         BigDecimal mothedTotal;
486         //支付编号分配的退款金额
487         BigDecimal mothedCutTotal;
488         //分配的划扣金额总和
489         BigDecimal deductionTotal = BigDecimal.ZERO;
490         //分配的现金金额总和
491         BigDecimal cashTotal = BigDecimal.ZERO;
ae6ff7 492
1869f3 493         ////退款方式金额分配
C 494         for(int i = 0;i <refundRecordMethodList.size();i++){
495             refundRecordMethod = refundRecordMethodList.get(i);
496
497             ////子项退款方式填充
498             refundRecordItemMethod = new RefundRecordItemMethod();
499             refundRecordItemMethod.setNumberNo(refundRecordMethod.getNumberNo());
500             refundRecordItemMethod.setName(refundRecordMethod.getName());
501             //支付方式
502             refundRecordItemMethod.setPaymentMethodId(refundRecordMethod.getPaymentMethodId());
503             refundRecordItemMethod.setIsMoneyPay(refundRecordMethod.getIsMoneyPay());
504             refundRecordItemMethod.setIsExecute(refundRecordMethod.getIsExecute());
505             refundRecordItemMethod.setIsPay(refundRecordMethod.getIsPay());
506             //退款方式
507             refundRecordItemMethod.setRefundNumberNo(refundRecordMethod.getRefundNumberNo());
508             refundRecordItemMethod.setRefundName(refundRecordMethod.getRefundName());
509             refundRecordItemMethod.setRefundMethodId(refundRecordMethod.getRefundMethodId());
510
511             //计算退款方式的金额
512             if(i == refundRecordMethodList.size()-1){
513                 ////最后一个
514                 refundRecordItemMethod.setActualTotal(refundItemTotal);
515             }else {
516                 ////不是最后一个
517                 refundRecordItemMethod.setActualTotal(refundRecordMethod.getActualTotal().multiply(payMothedPercentage).setScale(2,RoundingMode.UP));
ae6ff7 518             }
1869f3 519             //判断与剩余的未分配退款方式金额
C 520             if (refundRecordItemMethod.getActualTotal().compareTo(refundRecordMethod.getpTotal()) > 0){
521                 refundRecordItemMethod.setActualTotal(refundRecordMethod.getpTotal());
522             }
523             //判断与剩下的未分配金额校验
524             if(refundRecordItemMethod.getActualTotal().compareTo(refundItemTotal) > 0){
525                 refundRecordItemMethod.setActualTotal(refundItemTotal);
526             }
527             //可支付方式可退款金额
528             surplusTotal = noMap.computeIfAbsent(refundRecordItemMethod.getNumberNo(),k-> BigDecimal.ZERO);
529             if(refundRecordItemMethod.getActualTotal().compareTo(surplusTotal) > 0){
530                 refundRecordItemMethod.setActualTotal(surplusTotal);
531             }
ae6ff7 532
1869f3 533             refundRecordItemMethod.setCommonType(orderItemType);
C 534             refundRecordItemMethod.setCommonId(orderItemId);
535             refundRecordItemMethod.setOrderId(refundRecordMethod.getOrderId());
536             refundRecordItemMethod.setRefundRecordItemId(refundItemId);
537             refundRecordItemMethod.setRefundRecordId(refundRecord.getId());
538             commonService.insert(RefundRecordItemMethodMapper.class,refundRecordItemMethod);
ae6ff7 539
1869f3 540             //减去已经分配的退款方式金额
C 541             refundRecordMethod.setpTotal(refundRecordMethod.getpTotal().subtract(refundRecordItemMethod.getActualTotal()).setScale(2,RoundingMode.HALF_UP));
542             //减去已经分配的退款金额
543             refundItemTotal = refundItemTotal.subtract(refundRecordItemMethod.getActualTotal()).setScale(2,RoundingMode.HALF_UP);
544             //减去已经分配的可退款金额
545             surplusTotal = surplusTotal.subtract(refundRecordItemMethod.getActualTotal()).setScale(2,RoundingMode.HALF_UP);
546             noMap.put(refundRecordItemMethod.getNumberNo(),surplusTotal);
ae6ff7 547
1869f3 548             ///////生成关联支付方式记录和退款方式关联
510678 549             //根据支付编号获取支付方式记录
1869f3 550             noPayItemList = noPayItemMap.computeIfAbsent(refundRecordItemMethod.getNumberNo(),k->new ArrayList<>());
510678 551             //进行升序排序,避免后面不够分配
C 552             noPayItemList = noPayItemList.stream().sorted(Comparator.comparing(ConsumePayItem::getpTotal)).collect(Collectors.toList());
ae6ff7 553             mothedTotal = refundRecordItemMethod.getActualTotal();
1869f3 554             for(ConsumePayItem consumePayItem:noPayItemList){
ae6ff7 555                 if(consumePayItem.getpTotal().compareTo(BigDecimal.ZERO) < 1){
C 556                     continue;
557                 }
558                 //计算扣减金额
559                 if(consumePayItem.getpTotal().compareTo(mothedTotal) > 0){
560                     mothedCutTotal = mothedTotal;
561                 }else{
562                     mothedCutTotal = consumePayItem.getpTotal();
563                 }
564
565                 //划扣金额
566                 if(consumePayItem.getIsMoneyPay().equals(ConsumePayItem.YES)){
567                     deductionTotal = deductionTotal.add(mothedCutTotal).setScale(2,RoundingMode.HALF_UP);
568                 }
1869f3 569                 //现金金额
ae6ff7 570                 if(consumePayItem.getIsExecute().equals(ConsumePayItem.YES)){
C 571                     cashTotal = cashTotal.add(mothedCutTotal).setScale(2,RoundingMode.HALF_UP);
572                 }
573
574                 //生成关联记录
575                 refundRecordConsumePay = insertRefundRecordConsumePay(mothedCutTotal,refundRecordItemMethod.getCommonType(),refundRecordItemMethod.getCommonId(),refundRecord.getOrderId(),consumePayItem.getNumberNo()
1869f3 576                         ,consumePayItem.getName(),consumePayItem.getConsumePayId(),refundRecordItemMethod.getId(),refundItemId,refundRecord.getId(),commonService);
C 577                 refundConsumePayList.add(refundRecordConsumePay);
ae6ff7 578
1869f3 579                 //支付方式记录减掉已经分配退款方式金额
C 580                 consumePayItem.setpTotal(consumePayItem.getpTotal().subtract(mothedCutTotal).setScale(2,RoundingMode.HALF_UP));
581                 //减掉已经分配退款方式金额
ae6ff7 582                 mothedTotal = mothedTotal.subtract(mothedCutTotal).setScale(2, RoundingMode.HALF_UP);
1869f3 583                 //分配完成,跳出循环
ae6ff7 584                 if(mothedTotal.compareTo(BigDecimal.ZERO) < 1){
C 585                     //跳出循环
586                     break;
587                 }
588             }
589         }
1869f3 590         //判断是否已经分配完
C 591         if(refundItemTotal.compareTo(BigDecimal.ZERO) > 0){
592             throw new TipsException("分配退款金额错误["+orderItemType+"]");
ae6ff7 593         }
1869f3 594
C 595         refundCarryVo.setDeductionTotal(deductionTotal);
596         refundCarryVo.setCashTotal(cashTotal);
597         refundCarryVo.setRefundConsumePayList(refundConsumePayList);
598
599         return refundCarryVo;
ae6ff7 600     }
C 601
602
603     /**
604      * 退款-处理优惠券
605      * @param refundRecord 退款总数据
606      * @param commonService 映射
607      */
608     public static void handCoupon(RefundRecord refundRecord,CommonService commonService) {
609         SqlSentence sqlSentence = new SqlSentence();
610         Map<String, Object> map = new HashMap<>();
611
612         //回去回退优惠券
613         map.put("refundRecordId",refundRecord.getId());
a1c527 614         sqlSentence.sqlSentence("select * from refund_record_coupon where refundRecordId=#{m.refundRecordId} and isDel=0 ",map);
ae6ff7 615         List<RefundRecordCoupon> refundRecordCouponList=commonService.selectList(RefundRecordCouponMapper.class,sqlSentence);
C 616
617         //退款成功
a1c527 618         CouponOrderDiscountLog couponOrderDiscountLog;
ae6ff7 619         for (RefundRecordCoupon refundRecordCoupon : refundRecordCouponList) {
a1c527 620
C 621             couponOrderDiscountLog=commonService.selectOneByKey(CouponOrderDiscountLogMapper.class,refundRecordCoupon.getCouponOrderId());
622             if(couponOrderDiscountLog==null){
623                 throw new PlatTipsException(PlatformCode.ERROR_TIPS,"订单优惠卷标识不正确");
624             }
ae6ff7 625
C 626             //变更订单优惠券记录状态
627             map.clear();
628             map.put("status", BaseEntity.YES);
a1c527 629             map.put("id", couponOrderDiscountLog.getId());
C 630             sqlSentence.setSqlSentence("status = #{m.status} WHERE id = #{m.id}");
ae6ff7 631             if(commonService.updateWhere(CouponOrderDiscountLogMapper.class,sqlSentence) != 1){
C 632                 throw new TipsException("优惠券回退失败!");
633             }
634
635             //领建优惠券跳过回退
636             if(HIS_COUPON_CODE.equals(couponOrderDiscountLog.getCouponNumberId())){
637                 continue;
638             }
639
640             //优惠券状态变化
641             map.put("isUse", BaseEntity.NO);
642             map.put("useTime", null);
643             map.put("useType", CouponNumber.USE_TYPE_UNKNOW);
644             map.put("id", couponOrderDiscountLog.getCouponNumberId());
645             sqlSentence.setSqlSentence("  isUse=#{m.isUse},useTime=#{m.useTime},useType=#{m.useType},isUse=#{m.isUse} WHERE id = #{m.id}  ");
646             if(commonService.updateWhere(CouponNumberMapper.class,sqlSentence) != 1){
647                 throw new TipsException("优惠券回退失败[67]!");
648             }
649         }
650     }
651
652     /**
653      * 退款-处理活动规则增值金和积分
654      */
655     private static void handActivityRule(CommonService commonService, String operationId, String operationNme, SqlSentence sqlSentence,
656                                          Map<String, Object> map, RefundRecord refundRecord, OrdersTotal ordersTotal, OrderInfo orderInfo) {
657         if(orderInfo!=null && StringUtils.noNull(orderInfo.getActivityId())){
658             ActivityRule activityRule=commonService.selectOneByKeyBlob(ActivityRuleMapper.class,orderInfo.getActivityId());
659             if(activityRule!=null){
660                 map.put("activityRuleId",activityRule.getId());
661                 map.put("type", ActivityAction.TYPE_INTEGRAL);
662                 map.put("type1",ActivityAction.TYPE_VALUEADDEDFUND);
663                 map.put("type2",ActivityAction.TYPE_COUPON);
664                 sqlSentence.setSqlSentence("select * from activity_action where activityRuleId=#{m.activityRuleId} and (type=#{m.type} or type=#{m.type1} or type=#{m.type2}) and isDel=0");
665                 List<ActivityAction> activityActions = commonService.selectList(ActivityActionMapper.class, sqlSentence);
666                 if(activityActions!=null && activityActions.size()>0){
667                     for (ActivityAction activityAction : activityActions) {
668                         if(ActivityAction.TYPE_INTEGRAL.equals(activityAction.getType())){
669                             //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
670                             if(new BigDecimal(activityAction.getWorth()).negate().compareTo(BigDecimal.ZERO)!=0){
671                                 UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"退款扣减活动规则赠送积分",operationId,refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),new BigDecimal(activityAction.getWorth()).negate(), UserMoneyUnclaimed.FUND_TYPE_INTEGRAL,OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES);
672                             }
673                         }else if(ActivityAction.TYPE_VALUEADDEDFUND.equals(activityAction.getType())){
674                             //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
675                             if(new BigDecimal(activityAction.getWorth()).negate().compareTo(BigDecimal.ZERO)!=0){
676                                 UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"退款扣减活动规则赠送增值金",operationId,refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),new BigDecimal(activityAction.getWorth()).negate(), UserMoneyUnclaimed.FUND_TYPE_VALUE_ADDED_FUND,OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES);
677                             }
678                         }else if(ActivityAction.TYPE_COUPON.equals(activityAction.getType())){
679                             map.put("oldValidState",BaseEntity.YES);
680                             map.put("newValidState",BaseEntity.NO);
681                             map.put("couponId",activityAction.getCrmCouponId());
682                             map.put("commonId",ordersTotal.getId());
683                             sqlSentence.setSqlSentence(" validState=#{m.newValidState} where couponId=#{m.couponId} and commonId=#{m.commonId} and validState=#{m.oldValidState} ");
684                             commonService.updateWhere(CouponNumberMapper.class,sqlSentence);
685                         }
686                     }
687                 }
688             }
689         }
690
691     }
a1c527 692
C 693     /**退款-处理普通订单信息(比如:项目、促销、卡项)
694      * @param refundCarryVo 全局携带参数结构
695      * @param operationId 操作人标识
696      * @param refundRecord 退款总标识
697      * @param ordersTotal 订单
698      * @param commonService 映射
699      * @return 全局携带参数结构
ae6ff7 700      */
a1c527 701     public static RefundCarryVo numberOfRefunds(RefundCarryVo refundCarryVo,String operationId, RefundRecord refundRecord,OrdersTotal ordersTotal,CommonService commonService) {
ae6ff7 702
C 703         SqlSentence sqlSentence = new SqlSentence();
704         Map<String, Object> map = new HashMap<>();
705
706         //获取退款子单
707         map.put("refundRecordId",refundRecord.getId());
a1c527 708         sqlSentence.sqlSentence("SELECT * FROM refund_record_item WHERE isDel = 0 AND refundRecordId = #{m.refundRecordId}",map);
ae6ff7 709         List<RefundRecordItem> refundRecordItems =commonService.selectList(RefundRecordItemMapper.class,sqlSentence);
a1c527 710         if(refundRecordItems == null || refundRecordItems.size() == 0){
ae6ff7 711             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到退款子订单信息");
C 712         }
713
1869f3 714         //根据支付记录总表的标识来整合已退的金额,key值:支付记录总表标识,value:金额数据
C 715         Map<String,RefundRecordConsumePay> refundRecordConsumePayMap = new HashMap<>();
716         RefundRecordConsumePay refundRecordConsumePay;
ae6ff7 717
C 718         for (RefundRecordItem refundRecordItem: refundRecordItems) {
1869f3 719             //初始化总结构携带参数
C 720             refundCarryVo.setRefundConsumePayList(new ArrayList<>());
721             refundCarryVo.setDeductionTotal(BigDecimal.ZERO);
722             refundCarryVo.setCashTotal(BigDecimal.ZERO);
510678 723             //商品类型判断
ae6ff7 724             switch (refundRecordItem.getType()){
C 725                 case OrderItemConstants.TYPE_RETAIL:
1869f3 726                     refundCarryVo = handRefundRerail(refundRecord,refundRecordItem,refundCarryVo,commonService);
ae6ff7 727                     break;
C 728                 case OrderItemConstants.TYPE_DRUG:
1869f3 729                     refundCarryVo = handRefundRerail(refundRecord,refundRecordItem,refundCarryVo,commonService);
ae6ff7 730                     break;
C 731                 case OrderItemConstants.TYPE_PROJECT:
1869f3 732                     refundCarryVo = handRefundNoExecution(refundRecord,refundRecordItem,refundCarryVo,commonService);
ae6ff7 733                     break;
C 734                 case OrderItemConstants.TYPE_PROMOTION:
1869f3 735                     refundCarryVo = handRefundPromotion(operationId, refundRecord, ordersTotal,refundRecordItem,refundCarryVo,commonService);
ae6ff7 736                     break;
C 737                 case OrderItemConstants.TYPE_CARD:
1869f3 738                     refundCarryVo = handRefundCard(refundRecord,refundRecordItem,refundCarryVo,commonService);
ae6ff7 739                     break;
C 740                 case OrderItemConstants.CARD_BAG:
1869f3 741                     refundCarryVo = handRefundPromotion(operationId, refundRecord, ordersTotal,refundRecordItem,refundCarryVo,commonService);
ae6ff7 742                     break;
C 743                 default:break;
744             }
1869f3 745
C 746             //遍历叠加支付方式记录的退款金额
747             for(RefundRecordConsumePay re:refundCarryVo.getRefundConsumePayList()){
748                 refundRecordConsumePay = refundRecordConsumePayMap.computeIfAbsent(re.getConsumePayId(),k->new RefundRecordConsumePay(BigDecimal.ZERO,re.getNumberNo(),re.getConsumePayId()));
749                 refundRecordConsumePay.setRefundTotal(refundRecordConsumePay.getRefundTotal().add(re.getRefundTotal()));
750             }
ae6ff7 751         }
1869f3 752
C 753         //转载返回支付方式记录的退款金额
754         List<RefundRecordConsumePay> refundRecordConsumePayList = new ArrayList<>();
755         for (Map.Entry<String, RefundRecordConsumePay> entry : refundRecordConsumePayMap.entrySet()) {
756             refundRecordConsumePayList.add(entry.getValue());
a1c527 757         }
C 758
759         //校验金额是不是已经分配完
760         for(RefundRecordMethod refundRecordMethod:refundCarryVo.getRefundRecordMethodList()){
761             if(refundRecordMethod.getpTotal().compareTo(BigDecimal.ZERO) > 0){
762                 throw new TipsException("退款金额分配错误!");
763             }
1869f3 764         }
C 765
766         refundCarryVo.setRefundConsumePayList(refundRecordConsumePayList);
767         return refundCarryVo;
ae6ff7 768     }
C 769
770     /**
771      * 退款一级是商品
772      * @param refundRecord 退款总信息
773      * @param refundRecordItem 退款子单
774      * @param commonService 映射
775      */
1869f3 776     private static RefundCarryVo handRefundRerail(RefundRecord refundRecord, RefundRecordItem refundRecordItem
C 777             , RefundCarryVo refundCarryVo, CommonService commonService) {
ae6ff7 778
C 779         //判断操作完了去修改子订单状态
780         OrderItem orderItem=commonService.selectOneByKey(OrderItemMapper.class,refundRecordItem.getOrderItemId());
781         if (orderItem==null){
782             throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"未找到子订单信息");
783         }
784         //剩余可退款数量
785         Integer surplusNum = orderItem.getBuyNum()-orderItem.getHasReNum();
786         if(refundRecordItem.getRefundNum() > surplusNum){
787             throw new TipsException("退款数量不能大于可退款数量!");
788         }
789
790         //变更退款子项信息
a1c527 791         int refundStatus;
ae6ff7 792         if (surplusNum.equals(refundRecordItem.getRefundNum())){
1869f3 793             refundStatus = OrderTotalConstants.STATUS_REFUND_FINSH;
ae6ff7 794         }else{
1869f3 795             refundStatus = OrderTotalConstants.STATUS_REFUND_PART;
ae6ff7 796         }
C 797
1869f3 798         //更新子订单信息
C 799         updateOrderItemOne(orderItem,refundStatus,refundRecordItem.getRefundMoney(),refundRecordItem.getRefundNum(),commonService);
800
ae6ff7 801         //退款方式处理
1869f3 802         return refundRecordMotnedItemHandle(null,refundRecord,refundRecordItem,refundCarryVo,commonService);
ae6ff7 803     }
C 804
805     /**
806      * 退款-处理未执行划扣   项目类型
a1c527 807      * @param refundRecord 退款总记录
C 808      * @param refundRecordItem 退款一级子记录
ae6ff7 809      */
1869f3 810     private static RefundCarryVo handRefundNoExecution(RefundRecord refundRecord, RefundRecordItem refundRecordItem
C 811             ,RefundCarryVo refundCarryVo,CommonService commonService) {
ae6ff7 812
C 813         //找到子单
814         OrderItem orderItem=commonService.selectOneByKey(OrderItemMapper.class,refundRecordItem.getOrderItemId());
815         if (orderItem==null){
816             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到子订单信息");
817         }
a1c527 818
C 819         SqlSentence sqlSentence = new SqlSentence();
820         Map<String, Object> map = new HashMap<>();
821
ae6ff7 822         //找到用户项目
C 823         map.put("commonId",refundRecordItem.getOrderItemId());
824         sqlSentence.sqlSentence("select * from user_project_item where isDel = 0 and commonId = #{m.commonId} and isTransfer = 0",map);
825         UserProjectItem userProjectItem =commonService.selectOne(UserProjectItemMapper.class,sqlSentence);
826         if (userProjectItem == null) {
827             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到用户子项项目信息");
828         }
829
830         if(userProjectItem.getNotUsedNum() < refundRecordItem.getRefundNum()){
831             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款次数大于可退次数!");
832         }
833
834         //用户项目操作
1869f3 835         UserProjectDeductionVo  userProjectDeductionVo = UserProjectTool.userProjectDeduction(userProjectItem,UserProjectUsedCon.USED_METHOD_ORDER_REFUND,UserProjectUsedCon.USED_TYPE_DEDUCTION,null
ae6ff7 836                 ,refundRecordItem.getId(),refundRecordItem.getRefundNum(),refundRecord.getOperatorAppId(),refundRecord.getOperatorAppName(),refundRecord.getRefundShopId(),refundRecord.getRefundShopName(),"员工备注:"+refundRecord.getRemarks()+"|用户备注:"+refundRecord.getRefundReason(),commonService);
C 837
838         //计算子单是否还有剩余的可扣疗程数
839         int surplusNum = orderItem.getUsedTotal()-orderItem.getHasReNum();
840         if(refundRecordItem.getRefundNum() > surplusNum){
841             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款次数大于子单可退次数[54]!");
842         }
843
a1c527 844         int refundStatus;
ae6ff7 845         if(refundRecordItem.getRefundNum() == surplusNum){
C 846             refundStatus=OrderTotalConstants.STATUS_REFUND_FINSH;
847         }else{
848             refundStatus=OrderTotalConstants.STATUS_REFUND_PART;
849         }
850
1869f3 851         //更新子订单信息
C 852         updateOrderItemOne(orderItem,refundStatus,refundRecordItem.getRefundMoney(),refundRecordItem.getRefundNum(),commonService);
ae6ff7 853
C 854         //退款方式处理
1869f3 855         return refundRecordMotnedItemHandle(userProjectDeductionVo.getDeductionTotal(),refundRecord,refundRecordItem,refundCarryVo,commonService);
ae6ff7 856     }
C 857
a1c527 858     /**退款-处理卡项
C 859      * @param refundRecord 退款总记录
860      * @param refundRecordItem 退款一级记录
861      * @param refundCarryVo 全局携带参数结构
862      * @param commonService 映射
863      * @return 全局携带参数结构
ae6ff7 864      */
1869f3 865     public static RefundCarryVo handRefundCard(RefundRecord refundRecord, RefundRecordItem refundRecordItem,RefundCarryVo refundCarryVo,CommonService commonService) {
ae6ff7 866
C 867         //判断操作完了去修改子订单状态
868         OrderItem orderItem=commonService.selectOneByKey(OrderItemMapper.class,refundRecordItem.getOrderItemId());
869         if (orderItem==null){
1869f3 870             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到子订单信息");
ae6ff7 871         }
C 872         Integer surplusNum = orderItem.getBuyNum() - orderItem.getHasReNum();
873         if(refundRecordItem.getRefundNum() > surplusNum){
874             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款作废卡包提示:没有找到对应的卡包可退[020]");
875         }
876
877         //找到可退款卡包
878         List<UserCard> userCardList = PartialRefundUtil.getRefundCard(orderItem.getId(),commonService);
879         if(refundRecordItem.getRefundNum() > userCardList.size()){
880             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款作废卡包提示:没有找到对应的卡包可退[021]");
881         }
882
a1c527 883         SqlSentence sqlSentence = new SqlSentence();
C 884         Map<String, Object> map = new HashMap<>();
885
1869f3 886         //变更卡项状态
ae6ff7 887         UserCard userCard;
1869f3 888         RefundRecordCard refundRecordCard;
ae6ff7 889         for (int i = 0; i < refundRecordItem.getRefundNum(); i++) {
C 890             userCard= userCardList.get(i);
891             map.put("id",userCard.getId());
892             map.put("effectiveStatus", UserProjectConstants.EFF_STATUS_CANCEL);
510678 893             sqlSentence.sqlSentence(" effectiveStatus=#{m.effectiveStatus} WHERE id = #{m.id}",map);
ae6ff7 894             commonService.updateWhere(UserCardMapper.class,sqlSentence);
1869f3 895
C 896             //生成退款关联
897             refundRecordCard = new RefundRecordCard();
898             refundRecordCard.setUserCardId(userCard.getId());
899             refundRecordCard.setRefundRecordId(refundRecord.getId());
900             refundRecordCard.setRefundRecordItemId(refundRecordItem.getId());
a1c527 901             commonService.insert(RefundRecordCardMapper.class,refundRecordCard);
ae6ff7 902         }
C 903
a1c527 904         int refundStatus;
ae6ff7 905         if (surplusNum.equals(refundRecordItem.getRefundNum())){
1869f3 906             refundStatus =  OrderTotalConstants.STATUS_REFUND_FINSH;
ae6ff7 907         }else {
1869f3 908             refundStatus =  OrderTotalConstants.STATUS_REFUND_PART;
C 909
ae6ff7 910         }
1869f3 911         //更新子订单信息
C 912         updateOrderItemOne(orderItem,refundStatus,refundRecordItem.getRefundMoney(),refundRecordItem.getRefundNum(),commonService);
ae6ff7 913
C 914         //退款方式处理
1869f3 915         refundCarryVo = refundRecordMotnedItemHandle(null,refundRecord,refundRecordItem,refundCarryVo,commonService);
C 916         return refundCarryVo;
ae6ff7 917     }
C 918
a1c527 919     /**退款-处理卡包
ae6ff7 920      */
1869f3 921     public static void handCardBag(String operationId,RefundRecord refundRecord, OrdersTotal ordersTotal
C 922             ,RefundRecordItem refundRecordItem,RefundCarryVo refundCarryVo,CommonService commonService) {
ae6ff7 923
a1c527 924         //查看订单信息
C 925         OrderItem orderItem=commonService.selectOneByKey(OrderItemMapper.class,refundRecordItem.getOrderItemId());
926         if(orderItem == null ){
927             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到子订单信息!");
928         }
929
ae6ff7 930         SqlSentence sqlSentence = new SqlSentence();
C 931         Map<String, Object> map = new HashMap<>();
932
933         map.put("refundRecordItemId",refundRecordItem.getId());
a1c527 934         sqlSentence.sqlSentence("select * from refund_record_item_source where refundRecordItemId =#{m.refundRecordItemId}",map);
ae6ff7 935         List<RefundRecordItemSource> sons = commonService.selectList(RefundRecordItemSourceMapper.class, sqlSentence);
C 936         if(sons.size() == 0 ){
937             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到子订单信息[015]!");
938         }
939
940         for (RefundRecordItemSource son : sons) {
1869f3 941             refundCarryVo.setRefundConsumePayList(new ArrayList<>());
C 942             refundCarryVo.setDeductionTotal(BigDecimal.ZERO);
943             refundCarryVo.setCashTotal(BigDecimal.ZERO);
ae6ff7 944             switch (GroupTypeEnum.getCode(son.getType())){
C 945                 case PROJECT:
1869f3 946                     //handRefundNoSonExecution(refundRecord,son,refundCarryVo,commonService);
ae6ff7 947                     break;
C 948                 case RETAIL:
1869f3 949                     //handRefundSonRerail(refundRecord,son,refundCarryVo,commonService );
C 950                     //deleteUserCardUsed(sqlSentence,map,ordersTotal,commonService,son,orderItem);
ae6ff7 951                     break;
C 952                 case INCREMENT:
953                     //增值金
954                     //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
955                     if(son.getRealRefundTotal().negate().compareTo(BigDecimal.ZERO)!=0){
956                         UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"审核通过退款:促销赠送增值金扣减",operationId,refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),son.getRealRefundTotal().negate(), UserMoneyUnclaimed.FUND_TYPE_VALUE_ADDED_FUND, OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES);
957                     }
958                     break;
959                 case STORED:
960                     //储值金额
961                     //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
962                     if(son.getRealRefundTotal().negate().compareTo(BigDecimal.ZERO)!=0){
963                         UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"审核通过退款:促销赠送储值金额扣减",operationId,refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),son.getRealRefundTotal().negate(), UserMoneyUnclaimed.FUND_TYPE_STORED_VALUE_FUND, OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.NO);
964                     }
965                     break;
966                 case INTEGRAL:
967                     //积分
968                     //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
969                     if(son.getRealRefundTotal().negate().compareTo(BigDecimal.ZERO)!=0){
970                         UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"审核通过退款:促销赠送积分扣减",operationId,refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),son.getRealRefundTotal().negate(), UserMoneyUnclaimed.FUND_TYPE_INTEGRAL,OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES);
971                     }
972                     break;
973             }
974         }
975
976         //更改二级子订单退款状态
977         map.put("orderItemId",orderItem.getId());
978         sqlSentence.setSqlSentence("select * from order_item_source WHERE  orderItemId=#{m.orderItemId} and isDel=0");
979         List<OrderItemSon> orderItemSonList=commonService.selectList(OrderItemSonMapper.class,sqlSentence);
980
a1c527 981         List<Integer> collect = orderItemSonList.stream().map(OrderItemSon::getRefundStatus).collect(Collectors.toList());
ae6ff7 982         if(collect.contains(OrderTotalConstants.STATUS_REFUND_PART)){
C 983             orderItem.setRefundStatus(OrderTotalConstants.STATUS_REFUND_PART);
984             orderItem.setReTotal(orderItemSonList.stream().map(OrderItemSon::getReTotal).reduce(BigDecimal.ZERO,BigDecimal::add));
985         }else if (collect.contains(OrderTotalConstants.STATUS_REFUND_NONE) && collect.contains(OrderTotalConstants.STATUS_REFUND_FINSH)){
986             orderItem.setRefundStatus(OrderTotalConstants.STATUS_REFUND_PART);
987             orderItem.setReTotal(orderItemSonList.stream().map(OrderItemSon::getReTotal).reduce(BigDecimal.ZERO,BigDecimal::add));
988         }else if (collect.contains(OrderTotalConstants.STATUS_REFUND_NONE)){
989             orderItem.setRefundStatus(OrderTotalConstants.STATUS_REFUND_NONE);
990         }else  if (collect.contains(OrderTotalConstants.STATUS_REFUND_FINSH)){
991             orderItem.setRefundStatus(OrderTotalConstants.STATUS_REFUND_FINSH);
992             orderItem.setReTotal(orderItemSonList.stream().map(OrderItemSon::getReTotal).reduce(BigDecimal.ZERO,BigDecimal::add));
993             orderItem.setHasReNum(orderItem.getBuyNum());
994         }else {
995             orderItem.setRefundStatus(OrderTotalConstants.STATUS_REFUND_NONE);
996         }
997
998         commonService.updateAll(OrderItemMapper.class,orderItem);
999     }
1000
a1c527 1001     /**退款需要删除用户卡包使用记录
C 1002      * @param cardItemInfoId 卡包的卡项子项
1003      * @param sourceId 卡包使用记录来源标识
1004      * @param refundNum 退款数量
1005      * @param commonService 映射
ae6ff7 1006      */
a1c527 1007     public static void deleteUserCardUsed(String cardItemInfoId,String sourceId,Integer refundNum
C 1008             ,CommonService commonService){
ae6ff7 1009
C 1010         SqlSentence sqlSentence = new SqlSentence();
1869f3 1011         Map<String,Object> map = new HashMap<>();
ae6ff7 1012
1869f3 1013         //获取使用记录
C 1014         map.put("cardItemInfoId",cardItemInfoId);
1015         map.put("sourceId",sourceId);
1016         sqlSentence.setSqlSentence(" SELECT * FROM user_card_used WHERE isDel = 0" +
1017                 " AND cardItemInfoId = #{m.cardItemInfoId} AND sourceId = #{m.sourceId}");
1018         List<UserCardUsed> userCardUsedList = commonService.selectList(UserCardUsedMapper.class,sqlSentence);
1019
a1c527 1020         //查出用户,commonId:卡项的组合项标识
C 1021         CardItemInfo cardItemInfo=commonService.selectOneByKey(CardItemInfoMapper.class,cardItemInfoId);
1022         if(cardItemInfo ==null){
1023             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到该卡包的组合项");
1024         }
1025
1026         //获取权益类型
1027         CardEquity cardEquity = commonService.selectOneByKey(CardEquityMapper.class,cardItemInfo.getCardEquityId());
1028         if(cardEquity == null){
1029             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到该卡包的组合项权益类型");
1030         }
1031
1032         //计算退回的卡次
1033         int returnNum;
1034         //因为有些是算次数抵扣的,所以要算
1035         if(CardEquity.EQUITY_TYPE_TIME_CARD.equals(cardEquity.getEquityType())){
1036             //次卡:总次数=开单的总次数*每次划扣的次数
510678 1037             returnNum= refundNum * cardItemInfo.getEveryDrawNum();
a1c527 1038         }else if(CardEquity.EQUITY_TYPE_N_M.equals(cardEquity.getEquityType())){
C 1039             returnNum = refundNum;
1040         }else{
1041             returnNum = refundNum;
1042         }
1043
1044         if(returnNum > userCardUsedList.size()){
1869f3 1045             throw new TipsException("卡包可退数量错误!");
C 1046         }
1047
1048         int opNum = 0;
1049         sqlSentence.sqlUpdate("isDel = 1 WHERE id = #{m.id} AND isDel = 0",map);
1050         for(UserCardUsed userCardUsed:userCardUsedList){
1051             map.put("id",userCardUsed.getId());
1052             opNum = opNum+commonService.updateWhere(UserCardUsedMapper.class,sqlSentence);
1053         }
a1c527 1054         if(opNum != returnNum){
C 1055             throw new TipsException("卡包回退数量错误!");
1869f3 1056         }
C 1057
ae6ff7 1058     }
1869f3 1059
a1c527 1060     /**退款-处理促销
C 1061      * @param operationId 操作人标识
1062      * @param refundRecord 退款总记录
1063      * @param ordersTotal 订单
1064      * @param refundRecordItem 退款一级记录
1065      * @param refundCarryVo 全局携带参数结构
1066      * @param commonService 映射
1067      * @return 全局携带参数结构
ae6ff7 1068      */
1869f3 1069     private static RefundCarryVo handRefundPromotion(String operationId, RefundRecord refundRecord, OrdersTotal ordersTotal
C 1070             , RefundRecordItem refundRecordItem,RefundCarryVo refundCarryVo,CommonService commonService) {
ae6ff7 1071
C 1072         //查看订单信息
1869f3 1073         OrderItem orderItem = commonService.selectOneByKey(OrderItemMapper.class,refundRecordItem.getOrderItemId());
ae6ff7 1074         if(orderItem == null ){
C 1075             throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"未找到子订单信息[84]!");
1076         }
1077
a1c527 1078         SqlSentence sqlSentence = new SqlSentence();
C 1079         Map<String, Object> map = new HashMap<>();
1080
ae6ff7 1081         //获取退款二级单
C 1082         map.put("refundRecordItemId",refundRecordItem.getId());
a1c527 1083         sqlSentence.sqlSentence("select * from refund_record_item_source where isDel = 0 AND refundRecordItemId = #{m.refundRecordItemId}",map);
ae6ff7 1084         List<RefundRecordItemSource> sons = commonService.selectList(RefundRecordItemSourceMapper.class, sqlSentence);
C 1085
1869f3 1086         //根据支付记录总表的标识来整合已退的金额,key值:支付记录总表标识,value:金额数据
C 1087         Map<String,RefundRecordConsumePay> refundRecordConsumePayMap = new HashMap<>();
1088         RefundRecordConsumePay refundRecordConsumePay;
1089         //计算本次退款方式的划扣金额
1090         BigDecimal deductionTotal = BigDecimal.ZERO;
1091         //计算本次退款方式的现金金额
1092         BigDecimal cashTotal = BigDecimal.ZERO;
1093
1094         for (RefundRecordItemSource son : sons) {
1095             //初始化总结构携带参数
1096             refundCarryVo.setRefundConsumePayList(new ArrayList<>());
1097             refundCarryVo.setDeductionTotal(BigDecimal.ZERO);
1098             refundCarryVo.setCashTotal(BigDecimal.ZERO);
1099             switch (GroupTypeEnum.getCode(son.getType())){
1100                 case PROJECT:
1101                     refundCarryVo = handRefundNoSonExecution(refundRecord,refundRecordItem,son,refundCarryVo,commonService);
1102                     break;
1103                 case RETAIL:
a1c527 1104                     refundCarryVo = handRefundSonRerail(refundRecord,refundRecordItem,son,refundCarryVo,commonService);
1869f3 1105                     break;
C 1106                 case INCREMENT:
1107                     //增值金
1108                     //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
1109                     if(son.getRealRefundTotal().negate().compareTo(BigDecimal.ZERO)!=0){
1110                         UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"审核通过退款:促销赠送增值金扣减",operationId,refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),son.getRealRefundTotal().negate(), UserMoneyUnclaimed.FUND_TYPE_VALUE_ADDED_FUND, OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES);
1111                     }
1112                     break;
1113                 case STORED:
1114                     //储值金额
1115                     //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
1116                     if(son.getRealRefundTotal().negate().compareTo(BigDecimal.ZERO)!=0){
1117                         UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"审核通过退款:促销赠送储值金额扣减",operationId,refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),son.getRealRefundTotal().negate(), UserMoneyUnclaimed.FUND_TYPE_STORED_VALUE_FUND, OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.NO);
1118                     }
1119                     break;
1120                 case INTEGRAL:
1121                     //积分
1122                     //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
1123                     if(son.getRealRefundTotal().negate().compareTo(BigDecimal.ZERO)!=0){
1124                         UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"审核通过退款:促销赠送积分扣减",operationId,refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),son.getRealRefundTotal().negate(), UserMoneyUnclaimed.FUND_TYPE_INTEGRAL,OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES);
1125                     }
1126                     break;
1127             }
1128             deductionTotal = deductionTotal.add(refundCarryVo.getDeductionTotal());
1129             cashTotal = cashTotal.add(refundCarryVo.getCashTotal());
1130             //遍历叠加支付方式记录的退款金额
1131             for(RefundRecordConsumePay re:refundCarryVo.getRefundConsumePayList()){
1132                 refundRecordConsumePay = refundRecordConsumePayMap.computeIfAbsent(re.getConsumePayId(),k->new RefundRecordConsumePay(BigDecimal.ZERO,re.getNumberNo(),re.getConsumePayId()));
1133                 refundRecordConsumePay.setRefundTotal(refundRecordConsumePay.getRefundTotal().add(re.getRefundTotal()));
ae6ff7 1134             }
C 1135         }
1136
1869f3 1137         //转载返回支付方式记录的退款金额
C 1138         List<RefundRecordConsumePay> refundRecordConsumePayList = new ArrayList<>();
1139         for (Map.Entry<String, RefundRecordConsumePay> entry : refundRecordConsumePayMap.entrySet()) {
1140             refundRecordConsumePayList.add(entry.getValue());
1141         }
1142         refundCarryVo.setCashTotal(cashTotal);
1143         refundCarryVo.setDeductionTotal(deductionTotal);
1144         refundCarryVo.setRefundConsumePayList(refundRecordConsumePayList);
ae6ff7 1145
1869f3 1146         //获取其子项
ae6ff7 1147         map.put("orderItemId",orderItem.getId());
1869f3 1148         sqlSentence.setSqlSentence("select refundStatus from order_item_source WHERE orderItemId=#{m.orderItemId} and isDel=0");
ae6ff7 1149         List<OrderItemSon> orderItemSonList=commonService.selectList(OrderItemSonMapper.class,sqlSentence);
a1c527 1150         int refundStatus;
1869f3 1151         Integer refundNum = 0;
a1c527 1152         List<Integer> collect = orderItemSonList.stream().map(OrderItemSon::getRefundStatus).collect(Collectors.toList());
1869f3 1153
ae6ff7 1154         if(collect.contains(OrderTotalConstants.STATUS_REFUND_PART)){
1869f3 1155             refundStatus = OrderTotalConstants.STATUS_REFUND_PART;
ae6ff7 1156         }else if (collect.contains(OrderTotalConstants.STATUS_REFUND_NONE)){
1869f3 1157             if(collect.contains(OrderTotalConstants.STATUS_REFUND_FINSH)){
C 1158                 refundStatus = OrderTotalConstants.STATUS_REFUND_PART;
1159             }else{
1160                 refundStatus = OrderTotalConstants.STATUS_REFUND_NONE;
1161             }
ae6ff7 1162         }else if (collect.contains(OrderTotalConstants.STATUS_REFUND_FINSH)){
1869f3 1163             refundStatus = OrderTotalConstants.STATUS_REFUND_FINSH;
C 1164             refundNum = orderItem.getBuyNum();
ae6ff7 1165         }else {
1869f3 1166             refundStatus = OrderTotalConstants.STATUS_REFUND_NONE;
ae6ff7 1167         }
C 1168
1869f3 1169         //更新子订单信息
C 1170         updateOrderItemOne(orderItem,refundStatus,refundRecordItem.getRefundMoney(),refundNum,commonService);
1171
1172         return refundCarryVo;
ae6ff7 1173     }
C 1174
1869f3 1175     /**退款-处理二级子订单未执行划扣   项目类型
a1c527 1176      * @param refundRecord 退款总记录
C 1177      * @param refundRecordItem 退款一级子单
1178      * @param refundRecordItemSource  退款二级子单
1179      * @param refundCarryVo 全局携带参数接口
1180      * @param commonService 映射
ae6ff7 1181      */
1869f3 1182     private static RefundCarryVo handRefundNoSonExecution(RefundRecord refundRecord,RefundRecordItem refundRecordItem, RefundRecordItemSource refundRecordItemSource
C 1183             ,RefundCarryVo refundCarryVo,CommonService commonService) {
ae6ff7 1184
C 1185         //判断操作完了去修改子订单状态
510678 1186         OrderItemSon orderItemSon = commonService.selectOneByKey(OrderItemSonMapper.class,refundRecordItemSource.getOrderItemSonId());
ae6ff7 1187         if (orderItemSon==null){
510678 1188             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到二级子订单信息[02]");
ae6ff7 1189         }
C 1190
1191         UserProjectItem userProjectItem = getUserProject(orderItemSon.getId(),commonService);
1192         if(userProjectItem == null){
510678 1193             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"用户项目获取失败[06]!");
ae6ff7 1194         }
C 1195         if(userProjectItem.getNotUsedNum()<refundRecordItemSource.getRefundNum()){
1196             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款次数大于可退次数[83]!");
1197         }
1198
1199         //计算子单是否还有剩余的可扣疗程数
1200         int surplusNum = orderItemSon.getUsedTotal() - orderItemSon.getHasReNum();
1201         if(refundRecordItemSource.getRefundNum() > surplusNum){
510678 1202             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款次数大于可退次数[56]!");
ae6ff7 1203         }
C 1204
1869f3 1205         //处理用户项目,减去用户项目数量
C 1206         UserProjectDeductionVo userProjectDeductionVo= UserProjectTool.userProjectDeduction(userProjectItem,UserProjectUsedCon.USED_METHOD_ORDER_REFUND,UserProjectUsedCon.USED_TYPE_DEDUCTION,null
ae6ff7 1207                 ,refundRecordItemSource.getId(),refundRecordItemSource.getRefundNum(),refundRecord.getOperatorAppId(),refundRecord.getOperatorAppName(),refundRecord.getRefundShopId(),refundRecord.getRefundShopName(),"员工备注:"+refundRecord.getRemarks()+"|用户备注:"+refundRecord.getRefundReason(),commonService);
C 1208
a1c527 1209         int refundStatus;
ae6ff7 1210         if(surplusNum == refundRecordItemSource.getRefundNum()){
C 1211             refundStatus=OrderTotalConstants.STATUS_REFUND_FINSH;
1212         }else{
1213             refundStatus=OrderTotalConstants.STATUS_REFUND_PART;
1214         }
1215
1869f3 1216         //更新子单信息
C 1217         updateOrderItemTwo(orderItemSon,refundStatus,refundRecordItemSource.getRefundMoney(),refundRecordItemSource.getRefundNum(),commonService);
1218
1219         //处理退款支付方式
1220         refundCarryVo = refundRecordMotnedItemTwoHandle(userProjectDeductionVo.getDeductionTotal(),refundRecord,refundRecordItemSource,refundCarryVo,commonService);
1221
1222         if(refundRecordItem.getType().equals(OrderItemConstants.CARD_BAG)){
1223             //是卡包的,刪除卡包使用
1224             deleteUserCardUsed(orderItemSon.getCardItemInfoId(),orderItemSon.getId(),refundRecordItemSource.getRefundNum(),commonService);
ae6ff7 1225         }
C 1226
1869f3 1227         return refundCarryVo;
ae6ff7 1228     }
1869f3 1229
C 1230     /**退款二级是商品
a1c527 1231      * @param refundRecord 退款总记录
C 1232      * @param refundRecordItem 退款一级子单
1233      * @param refundRecordItemSource  退款二级子单
1234      * @param refundCarryVo 全局携带参数结构
1235      * @param commonService 映射
1236      * @return 全局携带参数结构
ae6ff7 1237      */
a1c527 1238     private static RefundCarryVo handRefundSonRerail(RefundRecord refundRecord,RefundRecordItem refundRecordItem, RefundRecordItemSource refundRecordItemSource
1869f3 1239             ,RefundCarryVo refundCarryVo,CommonService commonService) {
ae6ff7 1240
C 1241         //判断操作完了去修改子订单状态
1869f3 1242         OrderItemSon orderItemSon=commonService.selectOneByKey(OrderItemSonMapper.class,refundRecordItemSource.getOrderItemSonId());
C 1243         if (orderItemSon==null){
ae6ff7 1244             throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"未找到二级子订单信息");
C 1245         }
1246
1869f3 1247         //计算子单是否还有剩余的可扣疗程数
C 1248         int surplusNum = orderItemSon.getBuyNum() - orderItemSon.getHasReNum();
1249         if(refundRecordItemSource.getRefundNum() > surplusNum){
1250             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款次数大于可退次数[84]!");
ae6ff7 1251         }
C 1252
a1c527 1253         int refundStatus;
1869f3 1254         if(surplusNum == refundRecordItemSource.getRefundNum()){
C 1255             refundStatus=OrderTotalConstants.STATUS_REFUND_FINSH;
1256         }else{
1257             refundStatus=OrderTotalConstants.STATUS_REFUND_PART;
1258         }
1259
1260         updateOrderItemTwo(orderItemSon,refundStatus,refundRecordItemSource.getRefundMoney(),refundRecordItemSource.getRefundNum(),commonService);
1261
1262         //处理退款支付方式
1263         refundCarryVo = refundRecordMotnedItemTwoHandle(null,refundRecord,refundRecordItemSource,refundCarryVo,commonService);
a1c527 1264
C 1265         if(refundRecordItem.getType().equals(OrderItemConstants.CARD_BAG)){
1266             //是卡包的,刪除卡包使用
1267             deleteUserCardUsed(orderItemSon.getCardItemInfoId(),orderItemSon.getId(),refundRecordItemSource.getRefundNum(),commonService);
1268         }
1869f3 1269
C 1270         return refundCarryVo;
1271     }
1272
1273     /**更新订单一级子单的信息
1274      * @param orderItem 订单子单
1275      * @param refundStatus 退款状态
1276      * @param refundTotal 退款金额
1277      * @param refundNum 退款数量
1278      * @param commonService 映射
1279      */
1280     public static void updateOrderItemOne(OrderItem orderItem,Integer refundStatus,BigDecimal refundTotal,Integer refundNum
1281             ,CommonService commonService){
1282         SqlSentence sqlSentence = new SqlSentence();
1283         Map<String,Object> values = new HashMap<>();
1284
510678 1285         values.put("id", orderItem.getId());
1869f3 1286         values.put("refundStatus", refundStatus);
C 1287         values.put("refundTotal",refundTotal);
1288         values.put("refundNum",refundNum);
1289         values.put("oldHasReNum",orderItem.getHasReNum());
1290         sqlSentence.sqlUpdate(" refundStatus=#{m.refundStatus},reTotal=reTotal+#{m.refundTotal},hasReNum=hasReNum+#{m.refundNum}" +
510678 1291                 " WHERE isDel = 0 AND id = #{m.id} AND hasReNum = #{m.oldHasReNum}",values);
1869f3 1292         if(commonService.updateWhere(OrderItemMapper.class,sqlSentence) != 1){
C 1293             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"当前订单信息已发生变化,请重试[012]!");
1294         }
1295     }
1296
1297     /**更新订单二级子单的信息
1298      * @param orderItemSon 订单子单
1299      * @param refundStatus 退款状态
1300      * @param refundTotal 退款金额
1301      * @param refundNum 退款数量
1302      * @param commonService 映射
1303      */
1304     public static void updateOrderItemTwo(OrderItemSon orderItemSon,Integer refundStatus,BigDecimal refundTotal,Integer refundNum
1305             ,CommonService commonService){
1306         SqlSentence sqlSentence = new SqlSentence();
1307         Map<String,Object> values = new HashMap<>();
1308
1309         values.put("id", orderItemSon.getId());
1310         values.put("refundStatus", refundStatus);
1311         values.put("refundTotal",refundTotal);
1312         values.put("refundNum",refundNum);
1313         values.put("oldHasReNum",orderItemSon.getHasReNum());
510678 1314         sqlSentence.sqlUpdate(" refundStatus = #{m.refundStatus},reTotal=reTotal+#{m.refundTotal},hasReNum=hasReNum+#{m.refundNum}" +
1869f3 1315                 " where isDel=0 AND id = #{m.id} AND hasReNum = #{m.oldHasReNum}",values);
C 1316         if(commonService.updateWhere(OrderItemSonMapper.class,sqlSentence) != 1){
1317             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"当前订单信息已发生变化,请重试[012]!");
1318         }
1319     }
1320
a1c527 1321     /**获取订单一级子单的支付方式记录
C 1322      * @param orderItemId 订单一级子单标识
1323      * @param commonService 映射
1324      * @return 订单一级子单的支付方式记录
1869f3 1325      */
C 1326     public static List<ConsumePayItem> getOrderItemOneConsumePay(String orderItemId,CommonService commonService){
1327
1328         SqlSentence sqlSentence = new SqlSentence();
1329         Map<String,Object> values = new HashMap<>();
1330
1331         //获取子单的支付方式,一级子单支付记录,计算可退款金额
1332         values.put("typeId",orderItemId);
1333         sqlSentence.sqlSentence("SELECT * FROM consume_pay_item WHERE isDel = 0 AND typeId = #{m.typeId}",values);
1334         return commonService.selectList(ConsumePayMapper.class,sqlSentence);
1335     }
1336
a1c527 1337     /**获取订单二级子单的支付方式记录
C 1338      * @param orderItemId 订单一级子单标识
1339      * @param commonService 映射
1340      * @return 订单二级子单的支付方式记录
1869f3 1341      */
C 1342     public static List<ConsumePayItem> getOrderItemTwoConsumePay(String orderItemId,CommonService commonService){
1343
1344         SqlSentence sqlSentence = new SqlSentence();
1345         Map<String,Object> values = new HashMap<>();
1346
1347         //获取子单的支付方式,一级子单支付记录,计算可退款金额
1348         values.put("typeId",orderItemId);
1349         sqlSentence.sqlSentence("SELECT * FROM consume_pay_item_son WHERE isDel = 0 AND typeId = #{m.typeId}",values);
1350         return commonService.selectList(ConsumePayMapper.class,sqlSentence);
ae6ff7 1351     }
C 1352
1353     /**获取已退款方式金额,根据支付方式编号求和返回
1354      * 注意:groupByONumberNo和groupByConsumePayId不能同时使用
1355      * @param itemId 子单标识
1356      * @param refundRecordItemId 退款子单标识
1357      * @param groupByONumberNo 根据支付方式编号求和
1358      * @param groupByConsumePayId 根据支付方式记录标识求和
1359      * @param commonService 映射
1360      * @return 返回已退款方式金额集合
1361      */
1362     public static List<RefundRecordConsumePay> getRefundRecordConsumePay(String itemId,String refundRecordItemId,boolean groupByONumberNo
1363             ,boolean groupByConsumePayId,CommonService commonService){
1364         SqlSentence sqlSentence = new SqlSentence();
1365         Map<String,Object> values = new HashMap<>();
1366         StringBuilder sql = new StringBuilder();
1367
1368         sql.append("SELECT");
1369         if(groupByONumberNo){
1370             sql.append(" numberNo,");
1371         }
1372         if(groupByConsumePayId){
1373             sql.append(" consumePayId,");
1374         }
1375         sql.append("SUM(refundTotal) AS refundTotal FROM refund_record_consume_pay a");
1376         sql.append(" JOIN refund_record rr ON rr.isDel = 0 AND rr.status = #{status}");
1377         sql.append("  WHERE a.isDel = 0");
1378         if(StringUtils.noNull(itemId)){
1379             sql.append("  AND a.commonId = #{m.commonId}");
1380         }
1381         if(StringUtils.noNull(refundRecordItemId)){
1382             sql.append("  AND a.refundRecordItemId = #{m.refundRecordItemId}");
1383         }
1384         if(groupByONumberNo){
1385             sql.append(" GROUP BY a.numberNo");
1386         }
1387         if(groupByConsumePayId){
1388             sql.append(" GROUP BY a.consumePayId");
1389         }
1390         //获取已退款的支付方式
1391         values.put("commonId",itemId);
1392         values.put("status", RefundStatus.STATUS_SUCC_REFUND);
1393         sqlSentence.sqlSentence(sql.toString(),values);
1394         return commonService.selectList(RefundRecordConsumePayMapper.class,sqlSentence);
1395     }
1396
1397     /**保存退款方式和支付方式关联
1398      * @param refundTotal 退款金额
1399      * @param commonType 子单级别,可空
1400      * @param commonId 子单标识,可空
1401      * @param orderId  总单标识
1402      * @param numberNo 支付方式编号
1403      * @param name 支付方式名称
1404      * @param consumePayId 支付方式记录标识
1405      * @param refundMethodId 退款方式标识
1406      * @param refundRecordItemId 退款子记录标识,可空
1407      * @param refundRecordId 退款总记录标识
1408      * @param commonService 映射
1409      * @return 返回关联记录
1410      */
1411     public static RefundRecordConsumePay insertRefundRecordConsumePay(BigDecimal refundTotal, String commonType, String commonId, String orderId
1412             , String numberNo,String name, String consumePayId, String refundMethodId,String refundRecordItemId,String refundRecordId,CommonService commonService){
1413         //生成关联记录
1414         RefundRecordConsumePay refundRecordConsumePay = new RefundRecordConsumePay();
1415         refundRecordConsumePay.setRefundTotal(refundTotal);
1416         refundRecordConsumePay.setCommonType(commonType);
1417         refundRecordConsumePay.setCommonId(commonId);
1418         refundRecordConsumePay.setOrderId(orderId);
1419         refundRecordConsumePay.setName(name);
1420         refundRecordConsumePay.setNumberNo(numberNo);
1421         refundRecordConsumePay.setConsumePayId(consumePayId);
1422         refundRecordConsumePay.setRefundMethodId(refundMethodId);
1423         refundRecordConsumePay.setRefundRecordItemId(refundRecordItemId);
1424         refundRecordConsumePay.setRefundRecordId(refundRecordId);
1425         commonService.insert(RefundRecordConsumePayMapper.class,refundRecordConsumePay);
1426         return refundRecordConsumePay;
1427     }
1428
1429     /**获取可退款的用户卡项
1430      * @param sourceId 订单子单标识
1431      * @return 可退款的用户卡项
1432      */
1433     public static List<UserCard> getRefundCard(String sourceId,CommonService commonService){
1434         SqlSentence sqlSentence = new SqlSentence();
1435         Map<String,Object> sqlMap = new HashMap<>();
1436         //获取用户卡项
1437         sqlMap.put("sourceId",sourceId);
1438         sqlMap.put("status",UserCard.TYPE_NO_USED);
1439         sqlMap.put("effectiveStatus",UserProjectConstants.EFF_STATUS_YES);
1440         sqlSentence.sqlSentence("SELECT * FROM user_card WHERE isDel = 0 AND sourceId = #{m.sourceId} AND status = #{m.status}" +
1441                 " AND effectiveStatus = #{m.effectiveStatus} AND turnAddId IS NULL",sqlMap);
1442         return commonService.selectList(UserCardMapper.class,sqlSentence);
1443
1444     }
1445
1446     /**获取用户项目*/
1447     public static UserProjectItem getUserProject(String commonId,CommonService commonService){
1448         SqlSentence sqlSentence = new SqlSentence();
1449         Map<String, Object> map = new HashMap<>();
1450
1451         map.put("commonId",commonId);
1452         sqlSentence.sqlSentence("select * from user_project_item where isDel=0 and commonId=#{m.commonId} and isTransfer = 0",map);
a1c527 1453         return commonService.selectOne(UserProjectItemMapper.class,sqlSentence);
ae6ff7 1454     }
C 1455
1456 }