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