chenjiahe
2023-10-08 99213f3d390cd357f8acb679f0ec52af711b187e
提交 | 用户 | age
a1c527 1 package com.hx.phip.tool.refund;
ae6ff7 2
2b1cfa 3 import com.alibaba.fastjson.JSON;
ae6ff7 4 import com.hx.common.service.CommonService;
C 5 import com.hx.exception.TipsException;
6 import com.hx.mybatisTool.SqlSentence;
7 import com.hx.phiappt.common.*;
8 import com.hx.phiappt.constants.enums.GroupTypeEnum;
9 import com.hx.phiappt.constants.tool.RefundToolUtil;
9198b5 10 import com.hx.phiappt.constants.tool.TimerHandleTool;
ae6ff7 11 import com.hx.phiappt.model.BaseEntity;
9198b5 12 import com.hx.phiappt.model.TimerHandleItem;
ae6ff7 13 import com.hx.phiappt.model.UserMoney;
C 14 import com.hx.phiappt.model.activity.ActivityAction;
15 import com.hx.phiappt.model.activity.ActivityRule;
a1c527 16 import com.hx.phiappt.model.cardItem.CardEquity;
C 17 import com.hx.phiappt.model.cardItem.CardItemInfo;
ae6ff7 18 import com.hx.phiappt.model.consume.ConsumePay;
C 19 import com.hx.phiappt.model.consume.ConsumePayItem;
2a45d4 20 import com.hx.phiappt.model.consume.ConsumePayItemSon;
ae6ff7 21 import com.hx.phiappt.model.coupon.CouponNumber;
C 22 import com.hx.phiappt.model.coupon.CouponOrderDiscountLog;
23 import com.hx.phiappt.model.order.*;
24 import com.hx.phiappt.model.refund.*;
25 import com.hx.phiappt.model.user.UserCard;
26 import com.hx.phiappt.model.user.UserCardUsed;
27 import com.hx.phiappt.model.user.UserProjectItem;
28 import com.hx.phiappt.model.userMoney.UserMoneyUnclaimed;
29 import com.hx.phip.dao.mapper.*;
e2a6c2 30 import com.hx.phip.service.deduction.DeductionSingleService;
b91aa3 31 import com.hx.phip.service.order.impl.OrderRefundServiceImpl;
a88f94 32 import com.hx.phip.tool.user.UserCardTool;
ae6ff7 33 import com.hx.phip.tool.user.UserProjectTool;
a1c527 34 import com.hx.phip.util.api.UserMoneyUtil;
1869f3 35 import com.hx.phip.vo.order.refund.RefundCarryVo;
C 36 import com.hx.phip.vo.user.UserProjectDeductionVo;
ae6ff7 37 import com.hx.util.StringUtils;
e2a6c2 38 import com.hz.his.dto.deduction.DeductionDto;
C 39 import com.platform.entity.ThirtApplication;
ae6ff7 40 import com.platform.exception.PlatTipsException;
C 41 import com.platform.resultTool.PlatformCode;
b91aa3 42 import org.slf4j.Logger;
C 43 import org.slf4j.LoggerFactory;
2a45d4 44 import org.springframework.beans.BeanUtils;
ae6ff7 45
C 46 import java.math.BigDecimal;
47 import java.math.RoundingMode;
48 import java.util.*;
49 import java.util.stream.Collectors;
50
51 /**
52  * @Author
53  */
54 public class PartialRefundUtil {
b91aa3 55
C 56     /**log4j日志*/
57     private static final Logger logger = LoggerFactory.getLogger(OrderRefundServiceImpl.class.getName());
ae6ff7 58
2a45d4 59     /**有子项退款的类型*/
C 60     public static Set<String> CONTAIN_SON_TYPE_SET;
61
ae6ff7 62     /**领建优惠券标识*/
C 63     public static final String HIS_COUPON_CODE = "his_coupon_code";
2a45d4 64
C 65     static {
66         CONTAIN_SON_TYPE_SET = new HashSet<>();
67         CONTAIN_SON_TYPE_SET.add(OrderItemConstants.TYPE_PROMOTION);
68         CONTAIN_SON_TYPE_SET.add(OrderItemConstants.CARD_BAG);
69     }
ae6ff7 70
C 71     /**
72      * 退款总流程工具
a1c527 73      * @param commonService 映射
C 74      * @param operationId 操作人标识
75      * @param operationNme 操作人名称
76      * @param refundId 退款总单标识
ae6ff7 77      */
c0efa2 78     public static OrdersTotal refundProcess(CommonService commonService,DeductionSingleService deductionSingleService
C 79             , String operationId, String operationNme, String refundId) {
ae6ff7 80
C 81         RefundRecord refundRecord = commonService.selectOneByKeyBlob(RefundRecordMapper.class,refundId);
82         if(refundRecord ==null){
83             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"找不到该退款信息!");
84         }
85
86         SqlSentence sqlSentence = new SqlSentence();
87         Map<String,Object> values = new HashMap<>();
88
89         //退款总订单状态变更
90         values.put("refundStatus", RefundStatus.STATUS_SUCC_REFUND);
91         values.put("oldRefundStatus", RefundStatus.STATUS_APPLY_REFUND);
ecdbf6 92         values.put("createTime", new Date());
ae6ff7 93         values.put("isDel", BaseEntity.NO);
C 94         values.put("id",refundRecord.getId());
99213f 95         sqlSentence.sqlSentence(" refundStatus=#{m.refundStatus},createTime = #{m.createTime} WHERE id = #{m.id} AND isDel=#{m.isDel} AND refundStatus = #{m.oldRefundStatus}",values);
ae6ff7 96         if(commonService.updateWhere(RefundRecordMapper.class,sqlSentence) != 1){
C 97             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"操作失败,退款单状态已改变!");
98         }
99
510678 100         //获取订单信息
ae6ff7 101         OrdersTotal ordersTotal = commonService.selectOneByKeyBlob(OrdersTotalMapper.class,refundRecord.getOrderId());
C 102         if(ordersTotal == null ){
103             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到订单信息!");
104         }
105
106         //查询用户是否有账户信息,退款需要处理资金
a1c527 107         values.clear();
ae6ff7 108         values.put("userId",refundRecord.getUserId());
e9e32c 109         sqlSentence.sqlSentence("SELECT * FROM user_money WHERE  userId=#{m.userId} AND isDel=0",values);
ae6ff7 110         UserMoney userMoney=commonService.selectOne(UserMoneyMapper.class,sqlSentence);
C 111         if(userMoney==null){
112             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到该用户的资金信息");
113         }
1869f3 114
C 115         //获取总退款方式
116         values.put("refundRecordId",refundRecord.getId());
117         sqlSentence.sqlSentence("SELECT * FROM refund_record_method WHERE isDel = 0 AND refundRecordId = #{m.refundRecordId}",values);
118         List<RefundRecordMethod> refundRecordMethodList = commonService.selectList(RefundRecordMethodMapper.class,sqlSentence);
119
120         //全程使用携带参数对象
121         RefundCarryVo refundCarryVo = new RefundCarryVo();
122         refundCarryVo.setRefundRecordMethodList(refundRecordMethodList);
ae6ff7 123
C 124         if(OrderTotalConstants.TYPE_RECHARGE.equals(ordersTotal.getType())){
a1c527 125             //处理支付方式和退款方式关联
C 126             refundCarryVo = rechargeRefundMothedHandle(refundCarryVo,refundRecord,commonService);
127         }else{
128             //子单退款项处理
e2a6c2 129             refundCarryVo = numberOfRefunds(refundCarryVo,operationId, refundRecord,ordersTotal,commonService,deductionSingleService);
ae6ff7 130         }
a1c527 131         //处理优惠券
C 132         handCoupon(refundRecord,commonService);
133         //处理总退款方式数据
134         refundCarryVo = refundRecordMotnedHandle(refundCarryVo,operationId,operationNme,refundRecord,ordersTotal,commonService);
99213f 135
C 136         //-----存储退款金额
137         values.put("id",refundRecord.getId());
138         values.put("realRefundTotal",refundCarryVo.getRefundTotal());
139         values.put("realRefundIntegral",refundCarryVo.getIntegralTotal());
140         values.put("deductionTotal",refundRecord.getDeductionTotal());
141         values.put("cashTotal",refundRecord.getCashTotal());
142         values.put("cashPurenessTotal",refundRecord.getCashPurenessTotal());
143         values.put("rechargeTotal",refundRecord.getRechargeTotal());
144         values.put("incrementTotal",refundRecord.getIncrementTotal());
145         sqlSentence.sqlSentence(" realRefundTotal = #{m.realRefundTotal},realRefundIntegral = #{m.realRefundIntegral},deductionTotal=#{m.deductionTotal},cashTotal = #{m.cashTotal}" +
146                 ",cashPurenessTotal = #{m.cashPurenessTotal},rechargeTotal = #{m.rechargeTotal},incrementTotal = #{m.incrementTotal} WHERE id = #{m.id}",values);
147         if(commonService.updateWhere(RefundRecordMapper.class,sqlSentence) != 1){
148             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"操作失败,变更退款单金额失败!");
149         }
ae6ff7 150
C 151         //更改总订单退款状态
152         values.clear();
153         values.put("orderId",ordersTotal.getId());
e9e32c 154         sqlSentence.sqlSentence("select * from order_item WHERE  orderId=#{m.orderId} and isDel=0",values);
ae6ff7 155         List<OrderItem> orderItemList=commonService.selectList(OrderItemMapper.class,sqlSentence);
C 156
f72f73 157         values.clear();
C 158         values.put("oldStatus",ordersTotal.getStatus());
159         values.put("oldRefundStatus",ordersTotal.getRefundStatus());
160
a1c527 161         List<Integer> collect = orderItemList.stream().map(OrderItem::getRefundStatus).collect(Collectors.toList());
ae6ff7 162         if(collect.contains(OrderTotalConstants.STATUS_REFUND_PART)){
C 163             ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_PART);
164             ordersTotal.setReTotal(orderItemList.stream().map(OrderItem::getReTotal).reduce(BigDecimal.ZERO,BigDecimal::add));
165         }else if (collect.contains(OrderTotalConstants.STATUS_REFUND_NONE) && collect.contains(OrderTotalConstants.STATUS_REFUND_FINSH)){
166             ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_PART);
167             ordersTotal.setReTotal(orderItemList.stream().map(OrderItem::getReTotal).reduce(BigDecimal.ZERO,BigDecimal::add));
168         }else if (collect.contains(OrderTotalConstants.STATUS_REFUND_NONE)){
169             ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_NONE);
170         }else if (collect.contains(OrderTotalConstants.STATUS_REFUND_FINSH)){
171             ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_FINSH);
172             ordersTotal.setStatus(OrderTotalConstants.STATUS_CANCEL);
173             ordersTotal.setReTotal(orderItemList.stream().map(OrderItem::getReTotal).reduce(BigDecimal.ZERO,BigDecimal::add));
174         }else {
175             if(OrderTotalConstants.TYPE_RECHARGE.equals(ordersTotal.getType())){
176                 if(ordersTotal.getActualTotal().compareTo(refundRecord.getRefundTotal()) <= 0){
177                     ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_FINSH);
f72f73 178                     ordersTotal.setStatus(OrderTotalConstants.STATUS_CANCEL);
C 179                     ordersTotal.setReTotal(ordersTotal.getReTotal().add(refundRecord.getRefundTotal()));
ae6ff7 180                 }else{
C 181                     ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_PART);
f72f73 182                     ordersTotal.setReTotal(ordersTotal.getReTotal().add(refundRecord.getRefundTotal()));
ae6ff7 183                 }
C 184             }else{
185                 ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_NONE);
186             }
187         }
188         ordersTotal.setIsSyncOrder(BaseEntity.NO);
f72f73 189
C 190         values.put("isSyncOrder",BaseEntity.NO);
191         values.put("status",ordersTotal.getStatus());
192         values.put("refundStatus",ordersTotal.getRefundStatus());
193         values.put("reTotal",ordersTotal.getReTotal());
194         values.put("id",ordersTotal.getId());
195         sqlSentence.sqlUpdate("isSyncOrder = #{m.isSyncOrder},status = #{m.status},refundStatus = #{m.refundStatus},reTotal = #{m.reTotal}" +
196                 " WHERE id = #{m.id} AND status = #{m.oldStatus} AND refundStatus = #{m.oldRefundStatus}",values);
197         if(commonService.updateWhere(OrdersTotalMapper.class,sqlSentence) != 1){
198             throw new TipsException("操作失败,订单状态已发生改变!");
199         }
7c2538 200
C 201         //初始化初复诊信息
202         if(OrderTotalConstants.STATUS_CANCEL == ordersTotal.getStatus()){
6ed28c 203             // OrderUtil.reCalcOrderBothTheOneData(commonService, VisitRecordMapper.class,null,ordersTotal.getUserId(), BaseEntity.NO);
A 204             // 处理方法调整
9198b5 205             TimerHandleTool.addTask(commonService, TimerHandleItem.TYPE_RECALC_ORDER_BOTH, ordersTotal.getUserId());
7c2538 206         }
C 207
ae6ff7 208         return ordersTotal;
C 209     }
210
211     /**处理总退款方式数据*/
a1c527 212     public static RefundCarryVo refundRecordMotnedHandle(RefundCarryVo refundCarryVo,String operationId,String operationNme
C 213             ,RefundRecord refundRecord,OrdersTotal ordersTotal,CommonService commonService){
ae6ff7 214         SqlSentence sqlSentence = new SqlSentence();
C 215         Map<String,Object> map = new HashMap<>();
216
217         //获取总退款方式
a1c527 218         List<RefundRecordMethod> refundRecordMethodList = refundCarryVo.getRefundRecordMethodList();
2a45d4 219         //退款的支付方式记录数据
a1c527 220         List<RefundRecordConsumePay> refundConsumePayList = refundCarryVo.getRefundConsumePayList();
2a45d4 221         //通过支付编号,装载分组好支付方式记录,key值:支付方式编号,value:支付方式记录集合
a1c527 222         Map<String,List<RefundRecordConsumePay>> refundConsumePayMap= new HashMap<>();
C 223         List<RefundRecordConsumePay> refundRecordConsumePays;
224         for(RefundRecordConsumePay refundRecordConsumePay:refundConsumePayList){
225             refundRecordConsumePays = refundConsumePayMap.computeIfAbsent(refundRecordConsumePay.getNumberNo(),k->new ArrayList<>());
226             refundRecordConsumePays.add(refundRecordConsumePay);
227         }
ae6ff7 228
a1c527 229         List<RefundRecordConsumePay> refundRecordConsumePayList = new ArrayList<>();
C 230         RefundRecordConsumePay refundRecordConsumePay1;
ae6ff7 231
C 232         for(RefundRecordMethod refundRecordMethod:refundRecordMethodList){
8b5222 233             if(refundRecordMethod.getActualTotal().compareTo(BigDecimal.ZERO) < 1){
C 234                 continue;
235             }
2a45d4 236             //判断是否已经被分配完
a1c527 237             if(refundRecordMethod.getpTotal().compareTo(BigDecimal.ZERO) > 0){
C 238                 throw new TipsException("退款错误[20]!");
239             }
240             refundRecordConsumePays = refundConsumePayMap.get(refundRecordMethod.getNumberNo());
241             if(refundRecordConsumePays == null){
242                 throw new TipsException("退款错误[21]!");
243             }
244             for(RefundRecordConsumePay refundRecordConsumePay:refundRecordConsumePays){
245                 //更新支付记录退款信息
246                 map.clear();
247                 map.put("refundTotal",refundRecordConsumePay.getRefundTotal());
248                 map.put("id",refundRecordConsumePay.getConsumePayId());
249                 sqlSentence.sqlUpdate("refundTotal = refundTotal + #{m.refundTotal} WHERE id = #{m.id} AND actualTotal >= refundTotal + #{m.refundTotal}",map);
250                 if(commonService.updateWhere(ConsumePayMapper.class,sqlSentence) != 1){
251                     throw new TipsException("退款金额错误[33]!");
252                 }
253
254                 //生成关联记录
255                 refundRecordConsumePay1 = insertRefundRecordConsumePay(refundRecordConsumePay.getRefundTotal(),null,null,refundRecord.getOrderId(),refundRecordConsumePay.getNumberNo()
2a45d4 256                         ,refundRecordConsumePay.getName(),refundRecordConsumePay.getIsMoneyPay(),refundRecordConsumePay.getIsExecute(),refundRecordConsumePay.getConsumePayId(),refundRecordMethod.getId(),null,refundRecord.getId(),commonService);
a1c527 257                 refundRecordConsumePayList.add(refundRecordConsumePay1);
C 258             }
259             //修改实际退款金额
260             map.put("realRefundTotal",refundRecordMethod.getActualTotal());
261             map.put("id",refundRecordMethod.getId());
262             sqlSentence.sqlUpdate(" realRefundTotal = realRefundTotal + #{m.realRefundTotal} where id = #{m.id}",map);
263             if(commonService.updateWhere(RefundRecordMethodMapper.class,sqlSentence) != 1){
264                 throw new TipsException("更新退款方式信息错误[001]");
265             }
266
267         }
268         refundCarryVo.setRefundConsumePayList(refundRecordConsumePayList);
269
99213f 270
C 271         //计算各种金额
272         refundCarryVo.setRefundTotal(BigDecimal.ZERO);
273         refundCarryVo.setDeductionTotal(BigDecimal.ZERO);
274         refundCarryVo.setCashTotal(BigDecimal.ZERO);
275         refundCarryVo.setCashPurenessTotal(BigDecimal.ZERO);
276         refundCarryVo.setRechargeTotal(BigDecimal.ZERO);
277         refundCarryVo.setIncrementTotal(BigDecimal.ZERO);
278         refundCarryVo.setIntegralTotal(BigDecimal.ZERO);
279
a1c527 280         //处理回退到账
C 281         for(RefundRecordMethod refundRecordMethod:refundRecordMethodList){
ae6ff7 282             if(PayMethodTypeConstants.PAY_STORED.equals(refundRecordMethod.getRefundNumberNo())){
C 283                 //储值金额 
99213f 284                 refundCarryVo.setRechargeTotal(refundCarryVo.getRechargeTotal().add(refundRecordMethod.getActualTotal()));
C 285                 refundCarryVo.setRefundTotal(refundCarryVo.getRefundTotal().add(refundRecordMethod.getActualTotal()));
ae6ff7 286                 //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
C 287                 if(refundRecordMethod.getActualTotal().compareTo(BigDecimal.ZERO)>0){
b66e4c 288                     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);
ae6ff7 289                     OrderLog orderLog = RefundToolUtil.setOrderLog(refundRecord, operationId, operationNme, refundRecordMethod.getName()+"退款金额:"+refundRecordMethod.getActualTotal(), 0, OrderLogConstants.LOG_TYPE_REFUND);
C 290                     commonService.insert(OrderLogMapper.class,orderLog);
291                 }
292             }else if(PayMethodTypeConstants.PAY_ADD_FUND.equals(refundRecordMethod.getRefundNumberNo())){
293                 //增值金
99213f 294                 refundCarryVo.setIncrementTotal(refundCarryVo.getIncrementTotal().add(refundRecordMethod.getActualTotal()));
C 295                 refundCarryVo.setRefundTotal(refundCarryVo.getRefundTotal().add(refundRecordMethod.getActualTotal()));
ae6ff7 296                 //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
99213f 297                 if(refundRecordMethod.getActualTotal().compareTo(BigDecimal.ZERO)>0){
b66e4c 298                     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);
ae6ff7 299                     OrderLog orderLog = RefundToolUtil.setOrderLog(refundRecord,operationId,operationNme,refundRecordMethod.getName(),0, OrderLogConstants.LOG_TYPE_REFUND);
C 300                     commonService.insert(OrderLogMapper.class,orderLog);
301                 }
302             }else if(PayMethodTypeConstants.PAY_INTEGRAL.equals(refundRecordMethod.getRefundNumberNo())){
303                 //积分
99213f 304                 refundCarryVo.setIntegralTotal(refundCarryVo.getIntegralTotal().add(refundRecordMethod.getActualTotal()));
ae6ff7 305                 //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
99213f 306                 if(refundRecordMethod.getActualTotal().compareTo(BigDecimal.ZERO)>0){
b66e4c 307                     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);
ae6ff7 308                     OrderLog orderLog = RefundToolUtil.setOrderLog(refundRecord,operationId,operationNme,refundRecordMethod.getName()+"退款金额:"+refundRecordMethod.getActualTotal(),0, OrderLogConstants.LOG_TYPE_REFUND);
C 309                     commonService.insert(OrderLogMapper.class,orderLog);
310                 }
311             }else {
99213f 312                 refundCarryVo.setRefundTotal(refundCarryVo.getRefundTotal().add(refundRecordMethod.getActualTotal()));
ae6ff7 313                 if(refundRecordMethod.getActualTotal().compareTo(BigDecimal.ZERO)<1){
C 314                     continue;
315                 }
316                 OrderLog orderLog = RefundToolUtil.setOrderLog(refundRecord,operationId,operationNme,refundRecordMethod.getName()+"退款金额:"+refundRecordMethod.getActualTotal(),0, OrderLogConstants.LOG_TYPE_REFUND);
317                 orderLog.setOrderId(ordersTotal.getId());
318                 commonService.insert(OrderLogMapper.class,orderLog);
319             }
99213f 320             //是否现金
C 321             if(refundRecordMethod.getIsMoneyPay().equals(BaseEntity.YES)){
322                 refundCarryVo.setCashTotal(refundCarryVo.getCashTotal().add(refundRecordMethod.getActualTotal()));
323                 //不是退到储值金、增值金、积分
324                 if(!PayMethodTypeConstants.PAY_STORED.equals(refundRecordMethod.getRefundNumberNo())
325                     &&!PayMethodTypeConstants.PAY_ADD_FUND.equals(refundRecordMethod.getRefundNumberNo())
326                         &&!PayMethodTypeConstants.PAY_INTEGRAL.equals(refundRecordMethod.getRefundNumberNo())){
327                     refundCarryVo.setCashPurenessTotal(refundCarryVo.getCashPurenessTotal().add(refundRecordMethod.getActualTotal()));
328                 }
329             }
330             //判断是否划扣的
331             if(refundRecordMethod.getIsExecute().equals(BaseEntity.YES)){
332                 refundCarryVo.setDeductionTotal(refundCarryVo.getDeductionTotal().add(refundRecordMethod.getActualTotal()));
333             }
334
a1c527 335         }
C 336         return refundCarryVo;
337     }
338
339     /**充值订单退款方式的支付方式记录处理*/
340     public static RefundCarryVo rechargeRefundMothedHandle(RefundCarryVo refundCarryVo,RefundRecord refundRecord,CommonService commonService){
341
342         SqlSentence sqlSentence = new SqlSentence();
343         Map<String,Object> map = new HashMap<>();
344
345         List<RefundRecordMethod> refundRecordMethodList = refundCarryVo.getRefundRecordMethodList();
346
510678 347         //装载分配好的支付方式记录的金额数据
a1c527 348         List<RefundRecordConsumePay> refundConsumePayList = new ArrayList<>();
C 349         RefundRecordConsumePay refundRecordConsumePay;
350
510678 351         ///////获取订单支付方式记录
C 352         map.put("orderId",refundRecord.getOrderId());
353         sqlSentence.sqlSentence("SELECT *,ROUND(actualTotal-refundTotal,2) AS pTotal FROM consume_pay WHERE isDel = 0 AND orderId = #{m.orderId} ORDER BY pTotal ASC",map);
354         List<ConsumePay> consumePayList = commonService.selectList(ConsumePayMapper.class,sqlSentence);
355         //根据支付编号进行区分,key值:支付编号
356         Map<String,List<ConsumePay>> consumePayMap = new HashMap<>();
357         List<ConsumePay> consumePays;
358         for(ConsumePay consumePay:consumePayList){
359             consumePays = consumePayMap.computeIfAbsent(consumePay.getNumberNo(),k->new ArrayList<>());
360             consumePays.add(consumePay);
361         }
362
363         ////引用对象
364         //退款方式的金额
a1c527 365         BigDecimal mothedTotal;
510678 366         //分配支付方式金额
a1c527 367         BigDecimal mothedCutTotal;
C 368         for(RefundRecordMethod refundRecordMethod:refundRecordMethodList){
510678 369             if(refundRecordMethod.getActualTotal().compareTo(BigDecimal.ZERO) < 1){
C 370                 continue;
371             }
a1c527 372             mothedTotal = refundRecordMethod.getActualTotal();
510678 373             consumePays = consumePayMap.get(refundRecordMethod.getNumberNo());
C 374             if(consumePays == null){
375                 throw new TipsException("没有找到该支付记录:"+refundRecordMethod.getName()+"["+refundRecordMethod.getNumberNo()+"]");
376             }
377             for(ConsumePay consumePay:consumePays){
ae6ff7 378                 if(consumePay.getpTotal().compareTo(BigDecimal.ZERO) < 1){
C 379                     continue;
380                 }
381                 //计算扣减金额
382                 if(consumePay.getpTotal().compareTo(mothedTotal) > 0){
383                     mothedCutTotal = mothedTotal;
384                 }else{
385                     mothedCutTotal = consumePay.getpTotal();
386                 }
a1c527 387                 //减去已经分配的金额
ae6ff7 388                 mothedTotal = mothedTotal.subtract(mothedCutTotal).setScale(2, RoundingMode.HALF_UP);
a1c527 389
C 390                 refundRecordConsumePay = new RefundRecordConsumePay();
391                 refundRecordConsumePay.setNumberNo(consumePay.getNumberNo());
392                 refundRecordConsumePay.setName(consumePay.getName());
393                 refundRecordConsumePay.setRefundTotal(mothedCutTotal);
394                 refundRecordConsumePay.setConsumePayId(consumePay.getId());
8b5222 395                 refundRecordConsumePay.setIsMoneyPay(consumePay.getIsMoneyPay());
C 396                 refundRecordConsumePay.setIsExecute(consumePay.getIsPay());
a1c527 397                 refundConsumePayList.add(refundRecordConsumePay);
C 398
ae6ff7 399                 //已经分配完成,跳出循环
C 400                 if(mothedTotal.compareTo(BigDecimal.ZERO) < 1){
401                     //跳出循环
402                     break;
403                 }
404             }
405             //判断退款金额是否已经全部分配
406             if(mothedTotal.compareTo(BigDecimal.ZERO) > 0){
407                 throw new TipsException("退款金额错误[426]!");
408             }
a1c527 409             refundRecordMethod.setpTotal(BigDecimal.ZERO);
ae6ff7 410         }
a1c527 411         refundCarryVo.setRefundConsumePayList(refundConsumePayList);
C 412         return refundCarryVo;
ae6ff7 413     }
C 414
1869f3 415     /**处理一级子退款方式数据
C 416      * @param refundRecord 退款记录总表
417      * @param refundRecordItem 退款记录子表
418      * @param refundCarryVo 总携带参数结构
419      * @param commonService 映射
420      * @return 总携带参数结构
421      */
99213f 422     public static RefundCarryVo refundRecordMotnedItemHandle(RefundRecord refundRecord,RefundRecordItem refundRecordItem
1869f3 423             ,RefundCarryVo refundCarryVo,CommonService commonService){
C 424
510678 425         //没有退款方式,跳过当前处理,因为这里是处理退款方式金额的,如果没有退款金额,那么可以跳过该环节
1869f3 426         if(refundCarryVo.getRefundRecordMethodList().size() == 0){
C 427             return refundCarryVo;
428         }
429         //支付方式占比
430         BigDecimal payMothedPercentage;
431         //计算退款方式的占比
432         if(refundRecord.getRefundTotal().compareTo(BigDecimal.ZERO) > 0){
433             payMothedPercentage = refundRecordItem.getRefundMoney().divide(refundRecord.getRefundTotal(),15,RoundingMode.HALF_UP);
434         }else{
435             payMothedPercentage = BigDecimal.ZERO;
ae6ff7 436         }
C 437
99213f 438         //处理退款方式,存储信息
1869f3 439         refundCarryVo = insertRefundItemMothed(refundRecordItem.getId(),refundRecordItem.getRefundMoney(),payMothedPercentage,OrderSourceConstans.TYPE_PROJECT
C 440                 ,refundRecordItem.getOrderItemId(),refundCarryVo,refundRecord,commonService);
a1c527 441
2a45d4 442         //更新退款子单
99213f 443         updateRefundItem(refundCarryVo,refundRecordItem.getId(),commonService);
2a45d4 444
C 445         return refundCarryVo;
446     }
447
448     /**更新退款一级子单的信息
449      * @param refundItemId 退款记录一级子单标识
450      * @param commonService 映射
451      */
99213f 452     public static void updateRefundItem(RefundCarryVo refundCarryVo
2a45d4 453             ,String refundItemId,CommonService commonService){
a1c527 454         SqlSentence sqlSentence = new SqlSentence();
C 455         Map<String,Object> values = new HashMap<>();
ae6ff7 456
510678 457         //更新退款子单的信息,更新现金和划扣金额保存
99213f 458         values.put("realRefundTotal",refundCarryVo.getRefundTotal());
C 459         values.put("realRefundIntegral",refundCarryVo.getIntegralTotal());
460         values.put("deductionTotal",refundCarryVo.getDeductionTotal());
461         values.put("deductionTotalUser",refundCarryVo.getDeductionTotalUser());
462         values.put("cashTotal",refundCarryVo.getCashTotal());
463         values.put("cashPurenessTotal",refundCarryVo.getCashPurenessTotal());
464         values.put("rechargeTotal",refundCarryVo.getRechargeTotal());
465         values.put("incrementTotal",refundCarryVo.getIncrementTotal());
2a45d4 466         values.put("id",refundItemId);
99213f 467         sqlSentence.sqlUpdate("realRefundTotal = #{m.realRefundTotal},realRefundIntegral = #{m.realRefundIntegral},deductionTotal = #{m.deductionTotal},deductionTotalUser = #{m.deductionTotalUser},cashTotal = #{m.cashTotal}" +
C 468                 ",cashPurenessTotal = #{m.cashPurenessTotal},rechargeTotal = #{m.rechargeTotal},incrementTotal = #{m.incrementTotal} WHERE id = #{m.id}",values);
1869f3 469         if(commonService.updateWhere(RefundRecordItemMapper.class,sqlSentence) != 1){
C 470             throw new TipsException("更新退款子项信息失败!");
471         }
472     }
473
474     /**处理二级子退款方式数据
475      * @param refundRecord  退款记录总表
476      * @param refundRecordItemSource 退款记录子表
477      * @param refundCarryVo 总携带参数结构
478      * @param commonService 映射
479      * @return 总携带参数结构
480      */
99213f 481     public static RefundCarryVo refundRecordMotnedItemTwoHandle(RefundRecord refundRecord,RefundRecordItemSource refundRecordItemSource
1869f3 482             ,RefundCarryVo refundCarryVo,CommonService commonService){
C 483
484         //装载支付方式退款信息
485         refundCarryVo.setRefundConsumePayList(new ArrayList<>());
486         //没有退款方式,跳过当前处理
487         if(refundCarryVo.getRefundRecordMethodList().size() == 0){
488             return refundCarryVo;
489         }
490
491         SqlSentence sqlSentence = new SqlSentence();
492         Map<String,Object> values = new HashMap<>();
493
494         //支付方式占比
495         BigDecimal payMothedPercentage;
496         //计算退款方式的占比
497         if(refundRecord.getRefundTotal().compareTo(BigDecimal.ZERO) > 0){
498             payMothedPercentage = refundRecordItemSource.getRefundMoney().divide(refundRecord.getRefundTotal(),15,RoundingMode.HALF_UP);
499         }else{
500             payMothedPercentage = BigDecimal.ZERO;
501         }
502
503         //处理退款方式
504         refundCarryVo = insertRefundItemMothed(refundRecordItemSource.getId(),refundRecordItemSource.getRefundMoney(),payMothedPercentage,OrderSourceConstans.TYPE_RETAIL
505                 ,refundRecordItemSource.getOrderItemSonId(),refundCarryVo,refundRecord,commonService);
506
507         values.put("deductionTotal",refundCarryVo.getDeductionTotal());
99213f 508         values.put("deductionTotalUser",refundCarryVo.getDeductionTotalUser());
C 509         values.put("realRefundTotal",refundCarryVo.getRefundTotal());
510         values.put("realRefundIntegral",refundCarryVo.getIntegralTotal());
1869f3 511         values.put("cashTotal",refundCarryVo.getCashTotal());
99213f 512         values.put("cashPurenessTotal",refundCarryVo.getCashPurenessTotal());
C 513         values.put("rechargeTotal",refundCarryVo.getRechargeTotal());
514         values.put("incrementTotal",refundCarryVo.getIncrementTotal());
1869f3 515         values.put("id",refundRecordItemSource.getId());
99213f 516         sqlSentence.sqlUpdate("deductionTotal = #{m.deductionTotal},deductionTotalUser = #{m.deductionTotalUser},cashTotal = #{m.cashTotal}" +
C 517                 ",realRefundTotal = #{m.realRefundTotal},realRefundIntegral = #{m.realRefundIntegral},cashPurenessTotal = #{m.cashPurenessTotal}" +
518                 ",rechargeTotal = #{m.rechargeTotal},incrementTotal = #{m.incrementTotal} WHERE id = #{m.id}",values);
1869f3 519         if(commonService.updateWhere(RefundRecordItemSourceMapper.class,sqlSentence) != 1){
C 520             throw new TipsException("更新退款子项信息失败[94]!");
521         }
522
523         return refundCarryVo;
524     }
525
526     /**退款子单退款方式处理保存
527      * @param refundItemId 退款子单标识
528      * @param refundItemTotal 退款子单实际需要退款总金额
529      * @param payMothedPercentage 支付方式占比
530      * @param orderItemType 订单子单级别
531      * @param orderItemId 订单子单标识
532      * @param refundCarryVo 总结构对象
533      * @param refundRecord 退款总记录
534      * @param commonService 映射
535      * @return 总结构对象
536      */
537     public static RefundCarryVo insertRefundItemMothed(String refundItemId,BigDecimal refundItemTotal,BigDecimal payMothedPercentage,String orderItemType
538             ,String orderItemId,RefundCarryVo refundCarryVo,RefundRecord refundRecord,CommonService commonService){
539
540         //总退款方式金额数据
541         List<RefundRecordMethod> refundRecordMethodList = refundCarryVo.getRefundRecordMethodList();
a57453 542         //进行升序排序,避免后面不够分配
C 543         refundRecordMethodList = refundRecordMethodList.stream().sorted(Comparator.comparing(RefundRecordMethod::getpTotal)).collect(Collectors.toList());
1869f3 544
510678 545         //获取子单的支付方式记录,计算可退款金额
1869f3 546         List<ConsumePayItem> consumePayItemList;
C 547         if(OrderSourceConstans.TYPE_PROJECT.equals(orderItemType)){
510678 548             //一级订单
1869f3 549             consumePayItemList = getOrderItemOneConsumePay(orderItemId,commonService);
C 550         }else{
510678 551             //二级订单
1869f3 552             consumePayItemList = getOrderItemTwoConsumePay(orderItemId,commonService);
C 553         }
554
510678 555         ////存储支付方式编号的可退款金额,根据支付编号求和,后面的业务判断会用到,key值:支付编号,value:可退金额
1869f3 556         Map<String,BigDecimal> noMap = new HashMap<>();
C 557         BigDecimal surplusTotal;
510678 558         ////存储支付方式编号的支付方式记录,根据支付编号整合,后面的业务会用到,key值:支付编号,value:支付记录集合
1869f3 559         Map<String,List<ConsumePayItem>> noPayItemMap = new HashMap<>();
C 560         List<ConsumePayItem> noPayItemList;
561
562         ////////填充支付方式记录的退款金额,计算剩余可退金额,支付方式记录的退款金额需要去查询计算获取
510678 563         //获取订单子单已经退款的退款方式金额,根据支付方式记录的标识求和返回
a036dc 564         List<RefundRecordConsumePay> refundRecordConsumePayList = RefundTool.getRefundRecordConsumePay(orderItemId,null,false,true,commonService);
1869f3 565         //转化成map,可以根据支付方式记录的标识直接获取到数据
ae6ff7 566         Map<String, RefundRecordConsumePay> refundRecordConsumePayMap = refundRecordConsumePayList.stream().collect(
C 567                 Collectors.toMap(RefundRecordConsumePay::getConsumePayId,(a) -> a));
568         RefundRecordConsumePay refundRecordConsumePay;
569         for(ConsumePayItem consumePayItem:consumePayItemList){
570             refundRecordConsumePay = refundRecordConsumePayMap.get(consumePayItem.getConsumePayId());
571             if(refundRecordConsumePay != null){
572                 //已退款金额
573                 consumePayItem.setRefundTotal(refundRecordConsumePay.getRefundTotal());
574                 //可退款金额
575                 consumePayItem.setpTotal(consumePayItem.getpTotal().subtract(refundRecordConsumePay.getRefundTotal()).setScale(2,RoundingMode.HALF_UP));
576             }
1869f3 577             //计算每个支付编码可退款金额
C 578             surplusTotal = noMap.computeIfAbsent(consumePayItem.getNumberNo(),k->BigDecimal.ZERO);
579             surplusTotal = surplusTotal.add(consumePayItem.getpTotal()).setScale(2,RoundingMode.HALF_UP);
580             noMap.put(consumePayItem.getNumberNo(),surplusTotal);
ae6ff7 581
1869f3 582             //支付编码集合整合
C 583             noPayItemList = noPayItemMap.computeIfAbsent(consumePayItem.getNumberNo(),k->new ArrayList<>());
584             noPayItemList.add(consumePayItem);
585         }
ae6ff7 586
1869f3 587         List<RefundRecordConsumePay> refundConsumePayList = new ArrayList<>();
C 588
589         ////引用对象
ae6ff7 590         RefundRecordItemMethod refundRecordItemMethod;
1869f3 591         RefundRecordMethod refundRecordMethod;
C 592         //需要退的退款编号金额
593         BigDecimal mothedTotal;
594         //支付编号分配的退款金额
595         BigDecimal mothedCutTotal;
ae6ff7 596
d96661 597         //装载已经分配的退款金额记录,key值:总退款方式记录标识
C 598         Map<String,RefundRecordItemMethod> refundRecordItemMethodMap = new HashMap<>();
1869f3 599         ////退款方式金额分配
d96661 600         for(int i = 0;i <refundRecordMethodList.size();i++) {
1869f3 601             refundRecordMethod = refundRecordMethodList.get(i);
C 602
603             ////子项退款方式填充
604             refundRecordItemMethod = new RefundRecordItemMethod();
605             refundRecordItemMethod.setNumberNo(refundRecordMethod.getNumberNo());
606             refundRecordItemMethod.setName(refundRecordMethod.getName());
607             //支付方式
608             refundRecordItemMethod.setPaymentMethodId(refundRecordMethod.getPaymentMethodId());
609             refundRecordItemMethod.setIsMoneyPay(refundRecordMethod.getIsMoneyPay());
610             refundRecordItemMethod.setIsExecute(refundRecordMethod.getIsExecute());
611             refundRecordItemMethod.setIsPay(refundRecordMethod.getIsPay());
612             //退款方式
613             refundRecordItemMethod.setRefundNumberNo(refundRecordMethod.getRefundNumberNo());
614             refundRecordItemMethod.setRefundName(refundRecordMethod.getRefundName());
615             refundRecordItemMethod.setRefundMethodId(refundRecordMethod.getRefundMethodId());
9cff8d 616             refundRecordItemMethod.setIsMoneyPayRefund(refundRecordMethod.getIsMoneyPayRefund());
C 617             refundRecordItemMethod.setIsExecuteRefund(refundRecordMethod.getIsExecuteRefund());
1869f3 618
C 619             //计算退款方式的金额
d96661 620             if (i == refundRecordMethodList.size() - 1) {
1869f3 621                 ////最后一个
C 622                 refundRecordItemMethod.setActualTotal(refundItemTotal);
d96661 623             } else {
1869f3 624                 ////不是最后一个
d96661 625                 refundRecordItemMethod.setActualTotal(refundRecordMethod.getActualTotal().multiply(payMothedPercentage).setScale(2, RoundingMode.UP));
ae6ff7 626             }
1869f3 627             //判断与剩余的未分配退款方式金额
d96661 628             if (refundRecordItemMethod.getActualTotal().compareTo(refundRecordMethod.getpTotal()) > 0) {
1869f3 629                 refundRecordItemMethod.setActualTotal(refundRecordMethod.getpTotal());
C 630             }
631             //判断与剩下的未分配金额校验
d96661 632             if (refundRecordItemMethod.getActualTotal().compareTo(refundItemTotal) > 0) {
1869f3 633                 refundRecordItemMethod.setActualTotal(refundItemTotal);
C 634             }
635             //可支付方式可退款金额
d96661 636             surplusTotal = noMap.computeIfAbsent(refundRecordItemMethod.getNumberNo(), k -> BigDecimal.ZERO);
C 637             if (refundRecordItemMethod.getActualTotal().compareTo(surplusTotal) > 0) {
1869f3 638                 refundRecordItemMethod.setActualTotal(surplusTotal);
C 639             }
ae6ff7 640
2a45d4 641             refundRecordItemMethod.setRealRefundTotal(refundRecordItemMethod.getActualTotal());
1869f3 642             refundRecordItemMethod.setCommonType(orderItemType);
C 643             refundRecordItemMethod.setCommonId(orderItemId);
644             refundRecordItemMethod.setOrderId(refundRecordMethod.getOrderId());
645             refundRecordItemMethod.setRefundRecordItemId(refundItemId);
646             refundRecordItemMethod.setRefundRecordId(refundRecord.getId());
9cff8d 647             refundRecordItemMethod.setRefundRecordMethodId(refundRecordMethod.getId());
d96661 648
a57453 649             refundRecordItemMethodMap.put(refundRecordMethod.getId(),refundRecordItemMethod);
ae6ff7 650
1869f3 651             //减去已经分配的退款方式金额
C 652             refundRecordMethod.setpTotal(refundRecordMethod.getpTotal().subtract(refundRecordItemMethod.getActualTotal()).setScale(2,RoundingMode.HALF_UP));
653             //减去已经分配的退款金额
654             refundItemTotal = refundItemTotal.subtract(refundRecordItemMethod.getActualTotal()).setScale(2,RoundingMode.HALF_UP);
655             //减去已经分配的可退款金额
656             surplusTotal = surplusTotal.subtract(refundRecordItemMethod.getActualTotal()).setScale(2,RoundingMode.HALF_UP);
657             noMap.put(refundRecordItemMethod.getNumberNo(),surplusTotal);
d96661 658
C 659         }
660
661         //没有分配完,再分配
662         if(refundItemTotal.compareTo(BigDecimal.ZERO) > 0){
aeb85f 663             logger.info("没有分配完refundRecordMethodList{}:,未分配金额:{}",JSON.toJSONString(refundRecordMethodList),refundItemTotal);
C 664             BigDecimal frontMoney;
665             while (refundItemTotal.compareTo(BigDecimal.ZERO) > 0){
666                 //循环前的金额赋值
667                 frontMoney = refundItemTotal;
668                 refundItemTotal = redistributionRefundMoney(refundItemTotal,refundRecordMethodList,refundRecordItemMethodMap,noMap);
669                 //避免死循环,如果金额不再分配,那么就跳出循环
670                 if(frontMoney.compareTo(refundItemTotal) == 0){
d96661 671                     break;
C 672                 }
673             }
674         }
675         //判断是否已经分配完
676         if(refundItemTotal.compareTo(BigDecimal.ZERO) > 0){
a57453 677             logger.error("refundRecordMethodList{}:",JSON.toJSONString(refundRecordMethodList));
d96661 678             logger.error("分配退款金额错误:{},剩余未分配金额:{},支付方式:{}",refundItemId,refundItemTotal,JSON.toJSONString(consumePayItemList));
C 679             throw new TipsException("分配退款金额错误["+orderItemType+"]");
680         }
681
ea4351 682         List<RefundRecordItemMethod> refundRecordItemMethodList = new ArrayList<>();
a57453 683         //保存数据
C 684         for(Map.Entry<String, RefundRecordItemMethod> entry : refundRecordItemMethodMap.entrySet()) {
ea4351 685             refundRecordItemMethod = entry.getValue();
C 686             commonService.insert(RefundRecordItemMethodMapper.class,refundRecordItemMethod);
687             refundRecordItemMethodList.add(refundRecordItemMethod);
a57453 688         }
C 689
d96661 690         ////退款方式金额分配
C 691         for(int i = 0;i <refundRecordMethodList.size();i++){
692             refundRecordMethod = refundRecordMethodList.get(i);
693
694             ////子项退款方式填充
695             //获取分配的退款方式记录
696             refundRecordItemMethod = refundRecordItemMethodMap.get(refundRecordMethod.getId());
ae6ff7 697
99213f 698             if(PayMethodTypeConstants.PAY_STORED.equals(refundRecordItemMethod.getRefundNumberNo())){
C 699                 //储值金额 
700                 refundCarryVo.setRechargeTotal(refundCarryVo.getRechargeTotal().add(refundRecordItemMethod.getActualTotal()));
701                 refundCarryVo.setRefundTotal(refundCarryVo.getRefundTotal().add(refundRecordItemMethod.getActualTotal()));
702             }else if(PayMethodTypeConstants.PAY_ADD_FUND.equals(refundRecordItemMethod.getRefundNumberNo())){
703                 //增值金
704                 refundCarryVo.setIncrementTotal(refundCarryVo.getIncrementTotal().add(refundRecordItemMethod.getActualTotal()));
705                 refundCarryVo.setRefundTotal(refundCarryVo.getRefundTotal().add(refundRecordItemMethod.getActualTotal()));
706             }else if(PayMethodTypeConstants.PAY_INTEGRAL.equals(refundRecordItemMethod.getRefundNumberNo())){
707                 //积分
708                 refundCarryVo.setIntegralTotal(refundCarryVo.getIntegralTotal().add(refundRecordItemMethod.getActualTotal()));
709             }else {
710                 refundCarryVo.setRefundTotal(refundCarryVo.getRefundTotal().add(refundRecordItemMethod.getActualTotal()));
711             }
712
1869f3 713             ///////生成关联支付方式记录和退款方式关联
510678 714             //根据支付编号获取支付方式记录
1869f3 715             noPayItemList = noPayItemMap.computeIfAbsent(refundRecordItemMethod.getNumberNo(),k->new ArrayList<>());
510678 716             //进行升序排序,避免后面不够分配
C 717             noPayItemList = noPayItemList.stream().sorted(Comparator.comparing(ConsumePayItem::getpTotal)).collect(Collectors.toList());
ae6ff7 718             mothedTotal = refundRecordItemMethod.getActualTotal();
1869f3 719             for(ConsumePayItem consumePayItem:noPayItemList){
ae6ff7 720                 if(consumePayItem.getpTotal().compareTo(BigDecimal.ZERO) < 1){
C 721                     continue;
722                 }
723                 //计算扣减金额
724                 if(consumePayItem.getpTotal().compareTo(mothedTotal) > 0){
725                     mothedCutTotal = mothedTotal;
726                 }else{
727                     mothedCutTotal = consumePayItem.getpTotal();
728                 }
729
084cb8 730                 //现金金额
ae6ff7 731                 if(consumePayItem.getIsMoneyPay().equals(ConsumePayItem.YES)){
99213f 732                     refundCarryVo.setCashTotal(refundCarryVo.getCashTotal().add(mothedCutTotal).setScale(2,RoundingMode.HALF_UP));
C 733                     //不是退到储值金、增值金、积分
734                     if(!PayMethodTypeConstants.PAY_STORED.equals(refundRecordItemMethod.getRefundNumberNo())
735                             &&!PayMethodTypeConstants.PAY_ADD_FUND.equals(refundRecordItemMethod.getRefundNumberNo())
736                             &&!PayMethodTypeConstants.PAY_INTEGRAL.equals(refundRecordItemMethod.getRefundNumberNo())){
737                         refundCarryVo.setCashPurenessTotal(refundCarryVo.getCashPurenessTotal().add(mothedCutTotal));
738                     }
ae6ff7 739                 }
084cb8 740                 //划扣金额
ae6ff7 741                 if(consumePayItem.getIsExecute().equals(ConsumePayItem.YES)){
99213f 742                     refundCarryVo.setDeductionTotal(refundCarryVo.getDeductionTotal().add(mothedCutTotal).setScale(2,RoundingMode.HALF_UP));
ae6ff7 743                 }
C 744
745                 //生成关联记录
746                 refundRecordConsumePay = insertRefundRecordConsumePay(mothedCutTotal,refundRecordItemMethod.getCommonType(),refundRecordItemMethod.getCommonId(),refundRecord.getOrderId(),consumePayItem.getNumberNo()
2a45d4 747                         ,consumePayItem.getName(),consumePayItem.getIsMoneyPay(),consumePayItem.getIsExecute(),consumePayItem.getConsumePayId(),refundRecordItemMethod.getId(),refundItemId,refundRecord.getId(),commonService);
1869f3 748                 refundConsumePayList.add(refundRecordConsumePay);
ae6ff7 749
1869f3 750                 //支付方式记录减掉已经分配退款方式金额
C 751                 consumePayItem.setpTotal(consumePayItem.getpTotal().subtract(mothedCutTotal).setScale(2,RoundingMode.HALF_UP));
752                 //减掉已经分配退款方式金额
ae6ff7 753                 mothedTotal = mothedTotal.subtract(mothedCutTotal).setScale(2, RoundingMode.HALF_UP);
1869f3 754                 //分配完成,跳出循环
ae6ff7 755                 if(mothedTotal.compareTo(BigDecimal.ZERO) < 1){
C 756                     //跳出循环
757                     break;
758                 }
759             }
d96661 760             if(mothedTotal.compareTo(BigDecimal.ZERO) != 0){
C 761                 logger.error("分配退款金额错误[14]:{},剩余未分配金额:{},支付方式:{}",refundItemId,refundItemTotal,JSON.toJSONString(consumePayItemList));
762                 throw new TipsException("分配退款金额错误[14]:"+orderItemType);
763             }
ae6ff7 764         }
1869f3 765
C 766         refundCarryVo.setRefundConsumePayList(refundConsumePayList);
ea4351 767         refundCarryVo.setRefundRecordItemMethodList(refundRecordItemMethodList);
1869f3 768
C 769         return refundCarryVo;
ae6ff7 770     }
C 771
aeb85f 772     /**重新分配未分配完的金额*/
C 773     public static BigDecimal redistributionRefundMoney(BigDecimal refundItemTotal, List<RefundRecordMethod> refundRecordMethodList, Map<String, RefundRecordItemMethod> refundRecordItemMethodMap
774             , Map<String, BigDecimal> noMap){
775         //每次1分钱
776         BigDecimal money = new BigDecimal("0.01");
777         RefundRecordMethod refundRecordMethod;
778         RefundRecordItemMethod refundRecordItemMethod;
779         BigDecimal surplusTotal;
780         for(int i = 0;i <refundRecordMethodList.size();i++){
781             refundRecordMethod = refundRecordMethodList.get(i);
782             if(refundRecordMethod.getpTotal().compareTo(BigDecimal.ZERO) <= 0){
783                 continue;
784             }
785             //获取分配的退款方式记录
786             refundRecordItemMethod = refundRecordItemMethodMap.get(refundRecordMethod.getId());
787
788             //可支付方式可退款金额
789             surplusTotal = noMap.computeIfAbsent(refundRecordItemMethod.getNumberNo(),k-> BigDecimal.ZERO);
790             if(surplusTotal.compareTo(BigDecimal.ZERO) <= 0){
791                 continue;
792             }
793
794             //判断与剩余的未分配退款方式金额
795             if (money.compareTo(refundRecordMethod.getpTotal()) > 0){
796                 money = refundRecordMethod.getpTotal();
797             }
798             //判断与剩下的未分配金额校验
799             if(money.compareTo(refundItemTotal) > 0){
800                 money = refundItemTotal;
801             }
802             if(money.compareTo(surplusTotal) > 0){
803                 money = surplusTotal;
804             }
805
806             //减去已经分配的退款方式金额
807             refundRecordMethod.setpTotal(refundRecordMethod.getpTotal().subtract(money));
808             //减去已经分配的退款金额
809             refundItemTotal = refundItemTotal.subtract(money);
810             //减去已经分配的可退款金额
811             surplusTotal = surplusTotal.subtract(money);
812             noMap.put(refundRecordItemMethod.getNumberNo(),surplusTotal);
813             //叠加到分配的退款方式记录
814             refundRecordItemMethod.setActualTotal(refundRecordItemMethod.getActualTotal().add(money));
815             if(refundItemTotal.compareTo(BigDecimal.ZERO) < 1){
816                 break;
817             }
818         }
819         return refundItemTotal;
820     }
821
822
ae6ff7 823
C 824     /**
825      * 退款-处理优惠券
826      * @param refundRecord 退款总数据
827      * @param commonService 映射
828      */
829     public static void handCoupon(RefundRecord refundRecord,CommonService commonService) {
830         SqlSentence sqlSentence = new SqlSentence();
831         Map<String, Object> map = new HashMap<>();
832
833         //回去回退优惠券
834         map.put("refundRecordId",refundRecord.getId());
a1c527 835         sqlSentence.sqlSentence("select * from refund_record_coupon where refundRecordId=#{m.refundRecordId} and isDel=0 ",map);
ae6ff7 836         List<RefundRecordCoupon> refundRecordCouponList=commonService.selectList(RefundRecordCouponMapper.class,sqlSentence);
C 837
838         //退款成功
a1c527 839         CouponOrderDiscountLog couponOrderDiscountLog;
ae6ff7 840         for (RefundRecordCoupon refundRecordCoupon : refundRecordCouponList) {
a1c527 841
C 842             couponOrderDiscountLog=commonService.selectOneByKey(CouponOrderDiscountLogMapper.class,refundRecordCoupon.getCouponOrderId());
843             if(couponOrderDiscountLog==null){
844                 throw new PlatTipsException(PlatformCode.ERROR_TIPS,"订单优惠卷标识不正确");
845             }
ae6ff7 846
C 847             //变更订单优惠券记录状态
848             map.clear();
849             map.put("status", BaseEntity.YES);
61a0db 850             map.put("oldStatus", BaseEntity.NO);
a1c527 851             map.put("id", couponOrderDiscountLog.getId());
1f66d3 852             sqlSentence.sqlSentence("status = #{m.status}, editTime = now() WHERE id = #{m.id} AND status = #{m.oldStatus}",map);
ae6ff7 853             if(commonService.updateWhere(CouponOrderDiscountLogMapper.class,sqlSentence) != 1){
C 854                 throw new TipsException("优惠券回退失败!");
855             }
856
857             //领建优惠券跳过回退
858             if(HIS_COUPON_CODE.equals(couponOrderDiscountLog.getCouponNumberId())){
859                 continue;
860             }
861
862             //优惠券状态变化
863             map.put("isUse", BaseEntity.NO);
160c33 864             map.put("isUseOld", BaseEntity.YES);
ae6ff7 865             map.put("useTime", null);
C 866             map.put("useType", CouponNumber.USE_TYPE_UNKNOW);
867             map.put("id", couponOrderDiscountLog.getCouponNumberId());
160c33 868             sqlSentence.sqlSentence("  isUse=#{m.isUse},useTime=#{m.useTime},useType=#{m.useType},isUse=#{m.isUse} WHERE id = #{m.id} AND isUse = #{m.isUseOld}",map);
ae6ff7 869             if(commonService.updateWhere(CouponNumberMapper.class,sqlSentence) != 1){
C 870                 throw new TipsException("优惠券回退失败[67]!");
871             }
872         }
873     }
874
875     /**
876      * 退款-处理活动规则增值金和积分
877      */
878     private static void handActivityRule(CommonService commonService, String operationId, String operationNme, SqlSentence sqlSentence,
879                                          Map<String, Object> map, RefundRecord refundRecord, OrdersTotal ordersTotal, OrderInfo orderInfo) {
880         if(orderInfo!=null && StringUtils.noNull(orderInfo.getActivityId())){
881             ActivityRule activityRule=commonService.selectOneByKeyBlob(ActivityRuleMapper.class,orderInfo.getActivityId());
882             if(activityRule!=null){
883                 map.put("activityRuleId",activityRule.getId());
884                 map.put("type", ActivityAction.TYPE_INTEGRAL);
885                 map.put("type1",ActivityAction.TYPE_VALUEADDEDFUND);
886                 map.put("type2",ActivityAction.TYPE_COUPON);
e9e32c 887                 sqlSentence.sqlSentence("select * from activity_action where activityRuleId=#{m.activityRuleId} and (type=#{m.type} or type=#{m.type1} or type=#{m.type2}) and isDel=0",map);
ae6ff7 888                 List<ActivityAction> activityActions = commonService.selectList(ActivityActionMapper.class, sqlSentence);
C 889                 if(activityActions!=null && activityActions.size()>0){
890                     for (ActivityAction activityAction : activityActions) {
891                         if(ActivityAction.TYPE_INTEGRAL.equals(activityAction.getType())){
892                             //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
893                             if(new BigDecimal(activityAction.getWorth()).negate().compareTo(BigDecimal.ZERO)!=0){
894                                 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);
895                             }
896                         }else if(ActivityAction.TYPE_VALUEADDEDFUND.equals(activityAction.getType())){
897                             //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
898                             if(new BigDecimal(activityAction.getWorth()).negate().compareTo(BigDecimal.ZERO)!=0){
899                                 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);
900                             }
901                         }else if(ActivityAction.TYPE_COUPON.equals(activityAction.getType())){
902                             map.put("oldValidState",BaseEntity.YES);
903                             map.put("newValidState",BaseEntity.NO);
904                             map.put("couponId",activityAction.getCrmCouponId());
905                             map.put("commonId",ordersTotal.getId());
e9e32c 906                             sqlSentence.sqlSentence(" validState=#{m.newValidState} where couponId=#{m.couponId} and commonId=#{m.commonId} and validState=#{m.oldValidState} ",map);
ae6ff7 907                             commonService.updateWhere(CouponNumberMapper.class,sqlSentence);
C 908                         }
909                     }
910                 }
911             }
912         }
913
914     }
a1c527 915
C 916     /**退款-处理普通订单信息(比如:项目、促销、卡项)
917      * @param refundCarryVo 全局携带参数结构
918      * @param operationId 操作人标识
919      * @param refundRecord 退款总标识
920      * @param ordersTotal 订单
921      * @param commonService 映射
922      * @return 全局携带参数结构
ae6ff7 923      */
c0efa2 924     public static RefundCarryVo numberOfRefunds(RefundCarryVo refundCarryVo,String operationId, RefundRecord refundRecord
C 925             ,OrdersTotal ordersTotal,CommonService commonService,DeductionSingleService deductionSingleService) {
ae6ff7 926
C 927         SqlSentence sqlSentence = new SqlSentence();
928         Map<String, Object> map = new HashMap<>();
929
930         //获取退款子单
931         map.put("refundRecordId",refundRecord.getId());
a1c527 932         sqlSentence.sqlSentence("SELECT * FROM refund_record_item WHERE isDel = 0 AND refundRecordId = #{m.refundRecordId}",map);
ae6ff7 933         List<RefundRecordItem> refundRecordItems =commonService.selectList(RefundRecordItemMapper.class,sqlSentence);
a1c527 934         if(refundRecordItems == null || refundRecordItems.size() == 0){
ae6ff7 935             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到退款子订单信息");
C 936         }
937
1869f3 938         //根据支付记录总表的标识来整合已退的金额,key值:支付记录总表标识,value:金额数据
C 939         Map<String,RefundRecordConsumePay> refundRecordConsumePayMap = new HashMap<>();
940         RefundRecordConsumePay refundRecordConsumePay;
ae6ff7 941
C 942         for (RefundRecordItem refundRecordItem: refundRecordItems) {
1869f3 943             //初始化总结构携带参数
C 944             refundCarryVo.setRefundConsumePayList(new ArrayList<>());
945             refundCarryVo.setDeductionTotal(BigDecimal.ZERO);
946             refundCarryVo.setCashTotal(BigDecimal.ZERO);
2a45d4 947             refundCarryVo.setDeductionTotalUser(BigDecimal.ZERO);
99213f 948             refundCarryVo.setCashPurenessTotal(BigDecimal.ZERO);
C 949             refundCarryVo.setRechargeTotal(BigDecimal.ZERO);
950             refundCarryVo.setIntegralTotal(BigDecimal.ZERO);
951             refundCarryVo.setIncrementTotal(BigDecimal.ZERO);
952             refundCarryVo.setRefundTotal(BigDecimal.ZERO);
510678 953             //商品类型判断
ae6ff7 954             switch (refundRecordItem.getType()){
C 955                 case OrderItemConstants.TYPE_RETAIL:
1869f3 956                     refundCarryVo = handRefundRerail(refundRecord,refundRecordItem,refundCarryVo,commonService);
ae6ff7 957                     break;
C 958                 case OrderItemConstants.TYPE_DRUG:
1869f3 959                     refundCarryVo = handRefundRerail(refundRecord,refundRecordItem,refundCarryVo,commonService);
ae6ff7 960                     break;
C 961                 case OrderItemConstants.TYPE_PROJECT:
e2a6c2 962                     refundCarryVo = handRefundNoExecution(refundRecord,refundRecordItem,refundCarryVo,commonService,deductionSingleService);
ae6ff7 963                     break;
C 964                 case OrderItemConstants.TYPE_PROMOTION:
e2a6c2 965                     refundCarryVo = handRefundPromotion(operationId, refundRecord, ordersTotal,refundRecordItem,refundCarryVo,commonService,deductionSingleService);
ae6ff7 966                     break;
C 967                 case OrderItemConstants.TYPE_CARD:
1869f3 968                     refundCarryVo = handRefundCard(refundRecord,refundRecordItem,refundCarryVo,commonService);
ae6ff7 969                     break;
C 970                 case OrderItemConstants.CARD_BAG:
e2a6c2 971                     refundCarryVo = handRefundPromotion(operationId, refundRecord, ordersTotal,refundRecordItem,refundCarryVo,commonService,deductionSingleService);
ae6ff7 972                     break;
89b4d7 973                 case OrderItemConstants.TYPE_COUPON:
F 974                     refundCarryVo = handRefundRerail(refundRecord,refundRecordItem,refundCarryVo,commonService);
975                     break;
ae6ff7 976                 default:break;
C 977             }
1869f3 978
C 979             //遍历叠加支付方式记录的退款金额
980             for(RefundRecordConsumePay re:refundCarryVo.getRefundConsumePayList()){
2a45d4 981                 refundRecordConsumePay = refundRecordConsumePayMap.computeIfAbsent(re.getConsumePayId(),k->new RefundRecordConsumePay(BigDecimal.ZERO,re.getNumberNo(),re.getName(),re.getIsMoneyPay(),re.getIsExecute(),re.getConsumePayId()));
1869f3 982                 refundRecordConsumePay.setRefundTotal(refundRecordConsumePay.getRefundTotal().add(re.getRefundTotal()));
C 983             }
ae6ff7 984         }
1869f3 985
C 986         //转载返回支付方式记录的退款金额
987         List<RefundRecordConsumePay> refundRecordConsumePayList = new ArrayList<>();
988         for (Map.Entry<String, RefundRecordConsumePay> entry : refundRecordConsumePayMap.entrySet()) {
989             refundRecordConsumePayList.add(entry.getValue());
a1c527 990         }
C 991
992         //校验金额是不是已经分配完
993         for(RefundRecordMethod refundRecordMethod:refundCarryVo.getRefundRecordMethodList()){
994             if(refundRecordMethod.getpTotal().compareTo(BigDecimal.ZERO) > 0){
995                 throw new TipsException("退款金额分配错误!");
996             }
1869f3 997         }
C 998
999         refundCarryVo.setRefundConsumePayList(refundRecordConsumePayList);
1000         return refundCarryVo;
ae6ff7 1001     }
C 1002
1003     /**
1004      * 退款一级是商品
1005      * @param refundRecord 退款总信息
1006      * @param refundRecordItem 退款子单
1007      * @param commonService 映射
1008      */
1869f3 1009     private static RefundCarryVo handRefundRerail(RefundRecord refundRecord, RefundRecordItem refundRecordItem
C 1010             , RefundCarryVo refundCarryVo, CommonService commonService) {
ae6ff7 1011
C 1012         //判断操作完了去修改子订单状态
1013         OrderItem orderItem=commonService.selectOneByKey(OrderItemMapper.class,refundRecordItem.getOrderItemId());
1014         if (orderItem==null){
1015             throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"未找到子订单信息");
1016         }
1017         //剩余可退款数量
1018         Integer surplusNum = orderItem.getBuyNum()-orderItem.getHasReNum();
1019         if(refundRecordItem.getRefundNum() > surplusNum){
1020             throw new TipsException("退款数量不能大于可退款数量!");
1021         }
1022
1023         //变更退款子项信息
a1c527 1024         int refundStatus;
ae6ff7 1025         if (surplusNum.equals(refundRecordItem.getRefundNum())){
1869f3 1026             refundStatus = OrderTotalConstants.STATUS_REFUND_FINSH;
ae6ff7 1027         }else{
1869f3 1028             refundStatus = OrderTotalConstants.STATUS_REFUND_PART;
ae6ff7 1029         }
C 1030
1031         //退款方式处理
99213f 1032         refundCarryVo = refundRecordMotnedItemHandle(refundRecord,refundRecordItem,refundCarryVo,commonService);
C 1033
1034         //更新子订单信息
1035         updateOrderItemOne(orderItem,refundStatus,refundCarryVo,refundRecordItem.getRefundNum(),commonService);
1036
1037         return refundCarryVo;
ae6ff7 1038     }
C 1039
1040     /**
1041      * 退款-处理未执行划扣   项目类型
a1c527 1042      * @param refundRecord 退款总记录
C 1043      * @param refundRecordItem 退款一级子记录
ae6ff7 1044      */
1869f3 1045     private static RefundCarryVo handRefundNoExecution(RefundRecord refundRecord, RefundRecordItem refundRecordItem
e2a6c2 1046             ,RefundCarryVo refundCarryVo,CommonService commonService,DeductionSingleService deductionSingleService) {
ae6ff7 1047
C 1048         //找到子单
1049         OrderItem orderItem=commonService.selectOneByKey(OrderItemMapper.class,refundRecordItem.getOrderItemId());
1050         if (orderItem==null){
1051             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到子订单信息");
1052         }
a1c527 1053
C 1054         SqlSentence sqlSentence = new SqlSentence();
1055         Map<String, Object> map = new HashMap<>();
1056
c40d5d 1057         //先作废划扣,不然数量对不上
4215f9 1058         deductionCancel(refundRecord,refundRecordItem.getId(),commonService,deductionSingleService);
ae6ff7 1059
C 1060         //找到用户项目
1061         map.put("commonId",refundRecordItem.getOrderItemId());
1062         sqlSentence.sqlSentence("select * from user_project_item where isDel = 0 and commonId = #{m.commonId} and isTransfer = 0",map);
1063         UserProjectItem userProjectItem =commonService.selectOne(UserProjectItemMapper.class,sqlSentence);
1064         if (userProjectItem == null) {
1065             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到用户子项项目信息");
1066         }
1067
1068         if(userProjectItem.getNotUsedNum() < refundRecordItem.getRefundNum()){
1069             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款次数大于可退次数!");
1070         }
1071
1072         //用户项目操作
1869f3 1073         UserProjectDeductionVo  userProjectDeductionVo = UserProjectTool.userProjectDeduction(userProjectItem,UserProjectUsedCon.USED_METHOD_ORDER_REFUND,UserProjectUsedCon.USED_TYPE_DEDUCTION,null
ae6ff7 1074                 ,refundRecordItem.getId(),refundRecordItem.getRefundNum(),refundRecord.getOperatorAppId(),refundRecord.getOperatorAppName(),refundRecord.getRefundShopId(),refundRecord.getRefundShopName(),"员工备注:"+refundRecord.getRemarks()+"|用户备注:"+refundRecord.getRefundReason(),commonService);
2a45d4 1075
C 1076         refundCarryVo.setDeductionTotalUser(userProjectDeductionVo.getDeductionTotal());
ae6ff7 1077
C 1078         //计算子单是否还有剩余的可扣疗程数
1079         int surplusNum = orderItem.getUsedTotal()-orderItem.getHasReNum();
1080         if(refundRecordItem.getRefundNum() > surplusNum){
1081             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款次数大于子单可退次数[54]!");
1082         }
1083
a1c527 1084         int refundStatus;
ae6ff7 1085         if(refundRecordItem.getRefundNum() == surplusNum){
C 1086             refundStatus=OrderTotalConstants.STATUS_REFUND_FINSH;
1087         }else{
1088             refundStatus=OrderTotalConstants.STATUS_REFUND_PART;
1089         }
1090
1091         //退款方式处理
99213f 1092         refundCarryVo = refundRecordMotnedItemHandle(refundRecord,refundRecordItem,refundCarryVo,commonService);
C 1093
1094         //更新子订单信息
1095         updateOrderItemOne(orderItem,refundStatus,refundCarryVo,refundRecordItem.getRefundNum(),commonService);
1096
1097         return refundCarryVo;
ae6ff7 1098     }
C 1099
a1c527 1100     /**退款-处理卡项
C 1101      * @param refundRecord 退款总记录
1102      * @param refundRecordItem 退款一级记录
1103      * @param refundCarryVo 全局携带参数结构
1104      * @param commonService 映射
1105      * @return 全局携带参数结构
ae6ff7 1106      */
1869f3 1107     public static RefundCarryVo handRefundCard(RefundRecord refundRecord, RefundRecordItem refundRecordItem,RefundCarryVo refundCarryVo,CommonService commonService) {
ae6ff7 1108
C 1109         //判断操作完了去修改子订单状态
1110         OrderItem orderItem=commonService.selectOneByKey(OrderItemMapper.class,refundRecordItem.getOrderItemId());
1111         if (orderItem==null){
1869f3 1112             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到子订单信息");
ae6ff7 1113         }
C 1114         Integer surplusNum = orderItem.getBuyNum() - orderItem.getHasReNum();
1115         if(refundRecordItem.getRefundNum() > surplusNum){
1116             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款作废卡包提示:没有找到对应的卡包可退[020]");
1117         }
1118
1119         //找到可退款卡包
160c33 1120         List<UserCard> userCardList = PartialRefundUtil.getRefundCard(orderItem.getId(),UserProjectConstants.EFF_STATUS_YES,commonService);
ae6ff7 1121         if(refundRecordItem.getRefundNum() > userCardList.size()){
C 1122             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款作废卡包提示:没有找到对应的卡包可退[021]");
1123         }
1124
a1c527 1125         SqlSentence sqlSentence = new SqlSentence();
C 1126         Map<String, Object> map = new HashMap<>();
1127
1869f3 1128         //变更卡项状态
ae6ff7 1129         UserCard userCard;
1869f3 1130         RefundRecordCard refundRecordCard;
ae6ff7 1131         for (int i = 0; i < refundRecordItem.getRefundNum(); i++) {
C 1132             userCard= userCardList.get(i);
1133             map.put("id",userCard.getId());
ea2ceb 1134             map.put("statusOld", UserCard.TYPE_NO_USED);
ae6ff7 1135             map.put("effectiveStatus", UserProjectConstants.EFF_STATUS_CANCEL);
2e8393 1136             map.put("oldEffectiveStatus", UserProjectConstants.EFF_STATUS_YES);
ea2ceb 1137             sqlSentence.sqlSentence(" effectiveStatus=#{m.effectiveStatus} WHERE id = #{m.id} AND effectiveStatus = #{m.oldEffectiveStatus} AND status = #{m.statusOld}",map);
2e8393 1138             if(commonService.updateWhere(UserCardMapper.class,sqlSentence) != 1){
C 1139                 throw new TipsException("用户卡包状态已发生变化,请重试!");
1140             }
1869f3 1141
C 1142             //生成退款关联
1143             refundRecordCard = new RefundRecordCard();
1144             refundRecordCard.setUserCardId(userCard.getId());
1145             refundRecordCard.setRefundRecordId(refundRecord.getId());
1146             refundRecordCard.setRefundRecordItemId(refundRecordItem.getId());
a1c527 1147             commonService.insert(RefundRecordCardMapper.class,refundRecordCard);
ae6ff7 1148         }
C 1149
a1c527 1150         int refundStatus;
ea4351 1151         //卡项的,不管有没有退数量,只要走进这里,都是部分退或者全部退,可能有卡项部分退
ae6ff7 1152         if (surplusNum.equals(refundRecordItem.getRefundNum())){
1869f3 1153             refundStatus =  OrderTotalConstants.STATUS_REFUND_FINSH;
ae6ff7 1154         }else {
1869f3 1155             refundStatus =  OrderTotalConstants.STATUS_REFUND_PART;
ae6ff7 1156         }
ea4351 1157
ae6ff7 1158         //退款方式处理
99213f 1159         refundCarryVo = refundRecordMotnedItemHandle(refundRecord,refundRecordItem,refundCarryVo,commonService);
C 1160
1161         //更新子订单信息
1162         updateOrderItemOne(orderItem,refundStatus,refundCarryVo,refundRecordItem.getRefundNum(),commonService);
ea4351 1163
C 1164         //--卡项部分退处理
3badb7 1165         if(RefundSoruceConstants.TYPE_SOURCE_USER_CARD.equals(refundRecord.getSourceAssistantType())){
ea4351 1166             CardRefundTool.realRefund(refundRecord,refundRecordItem,refundCarryVo,commonService);
C 1167         }
1168
1869f3 1169         return refundCarryVo;
ae6ff7 1170     }
C 1171
a1c527 1172     /**退款-处理卡包
ae6ff7 1173      */
1869f3 1174     public static void handCardBag(String operationId,RefundRecord refundRecord, OrdersTotal ordersTotal
C 1175             ,RefundRecordItem refundRecordItem,RefundCarryVo refundCarryVo,CommonService commonService) {
ae6ff7 1176
a1c527 1177         //查看订单信息
C 1178         OrderItem orderItem=commonService.selectOneByKey(OrderItemMapper.class,refundRecordItem.getOrderItemId());
1179         if(orderItem == null ){
1180             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到子订单信息!");
1181         }
1182
ae6ff7 1183         SqlSentence sqlSentence = new SqlSentence();
C 1184         Map<String, Object> map = new HashMap<>();
1185
1186         map.put("refundRecordItemId",refundRecordItem.getId());
a1c527 1187         sqlSentence.sqlSentence("select * from refund_record_item_source where refundRecordItemId =#{m.refundRecordItemId}",map);
ae6ff7 1188         List<RefundRecordItemSource> sons = commonService.selectList(RefundRecordItemSourceMapper.class, sqlSentence);
C 1189         if(sons.size() == 0 ){
1190             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到子订单信息[015]!");
1191         }
1192
1193         for (RefundRecordItemSource son : sons) {
1869f3 1194             refundCarryVo.setRefundConsumePayList(new ArrayList<>());
C 1195             refundCarryVo.setDeductionTotal(BigDecimal.ZERO);
1196             refundCarryVo.setCashTotal(BigDecimal.ZERO);
ae6ff7 1197             switch (GroupTypeEnum.getCode(son.getType())){
C 1198                 case PROJECT:
1869f3 1199                     //handRefundNoSonExecution(refundRecord,son,refundCarryVo,commonService);
ae6ff7 1200                     break;
C 1201                 case RETAIL:
1869f3 1202                     //handRefundSonRerail(refundRecord,son,refundCarryVo,commonService );
C 1203                     //deleteUserCardUsed(sqlSentence,map,ordersTotal,commonService,son,orderItem);
ae6ff7 1204                     break;
C 1205                 case INCREMENT:
1206                     //增值金
1207                     //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
1208                     if(son.getRealRefundTotal().negate().compareTo(BigDecimal.ZERO)!=0){
1209                         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);
1210                     }
1211                     break;
1212                 case STORED:
1213                     //储值金额
1214                     //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
1215                     if(son.getRealRefundTotal().negate().compareTo(BigDecimal.ZERO)!=0){
1216                         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);
1217                     }
1218                     break;
1219                 case INTEGRAL:
1220                     //积分
1221                     //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
1222                     if(son.getRealRefundTotal().negate().compareTo(BigDecimal.ZERO)!=0){
1223                         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);
1224                     }
1225                     break;
1226             }
1227         }
1228
1229         //更改二级子订单退款状态
1230         map.put("orderItemId",orderItem.getId());
1231         sqlSentence.setSqlSentence("select * from order_item_source WHERE  orderItemId=#{m.orderItemId} and isDel=0");
1232         List<OrderItemSon> orderItemSonList=commonService.selectList(OrderItemSonMapper.class,sqlSentence);
1233
a1c527 1234         List<Integer> collect = orderItemSonList.stream().map(OrderItemSon::getRefundStatus).collect(Collectors.toList());
ae6ff7 1235         if(collect.contains(OrderTotalConstants.STATUS_REFUND_PART)){
C 1236             orderItem.setRefundStatus(OrderTotalConstants.STATUS_REFUND_PART);
1237             orderItem.setReTotal(orderItemSonList.stream().map(OrderItemSon::getReTotal).reduce(BigDecimal.ZERO,BigDecimal::add));
1238         }else if (collect.contains(OrderTotalConstants.STATUS_REFUND_NONE) && collect.contains(OrderTotalConstants.STATUS_REFUND_FINSH)){
1239             orderItem.setRefundStatus(OrderTotalConstants.STATUS_REFUND_PART);
1240             orderItem.setReTotal(orderItemSonList.stream().map(OrderItemSon::getReTotal).reduce(BigDecimal.ZERO,BigDecimal::add));
1241         }else if (collect.contains(OrderTotalConstants.STATUS_REFUND_NONE)){
1242             orderItem.setRefundStatus(OrderTotalConstants.STATUS_REFUND_NONE);
1243         }else  if (collect.contains(OrderTotalConstants.STATUS_REFUND_FINSH)){
1244             orderItem.setRefundStatus(OrderTotalConstants.STATUS_REFUND_FINSH);
1245             orderItem.setReTotal(orderItemSonList.stream().map(OrderItemSon::getReTotal).reduce(BigDecimal.ZERO,BigDecimal::add));
1246             orderItem.setHasReNum(orderItem.getBuyNum());
1247         }else {
1248             orderItem.setRefundStatus(OrderTotalConstants.STATUS_REFUND_NONE);
1249         }
1250
1251         commonService.updateAll(OrderItemMapper.class,orderItem);
1252     }
1253
a1c527 1254     /**退款需要删除用户卡包使用记录
b91aa3 1255      * @param userCardId 用户卡包
a1c527 1256      * @param cardItemInfoId 卡包的卡项子项
C 1257      * @param sourceId 卡包使用记录来源标识
e9e32c 1258      * @param orderId 卡包使用记录来源总表标识
a1c527 1259      * @param refundNum 退款数量
C 1260      * @param commonService 映射
ae6ff7 1261      */
b91aa3 1262     public static void deleteUserCardUsed(String userCardId,String cardItemInfoId,String sourceId,String orderId,Integer refundNum
a1c527 1263             ,CommonService commonService){
1869f3 1264
a1c527 1265         //查出用户,commonId:卡项的组合项标识
C 1266         CardItemInfo cardItemInfo=commonService.selectOneByKey(CardItemInfoMapper.class,cardItemInfoId);
1267         if(cardItemInfo ==null){
b91aa3 1268             logger.error("未找到该卡包的组合项-卡包退款失败,userCardId:{},cardItemInfoId:{},sourceId:{},orderId:{},refundNum:{}",userCardId,cardItemInfoId,sourceId,orderId,refundNum);
a1c527 1269             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到该卡包的组合项");
C 1270         }
1271
1272         //获取权益类型
1273         CardEquity cardEquity = commonService.selectOneByKey(CardEquityMapper.class,cardItemInfo.getCardEquityId());
1274         if(cardEquity == null){
b91aa3 1275             logger.error("未找到该卡包的组合项权益类型-卡包退款失败,userCardId:{},cardItemInfoId:{},sourceId:{},orderId:{},refundNum:{}",userCardId,cardItemInfoId,sourceId,orderId,refundNum);
a1c527 1276             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到该卡包的组合项权益类型");
e9e32c 1277         }
C 1278
1279         SqlSentence sqlSentence = new SqlSentence();
1280         Map<String,Object> map = new HashMap<>();
1281
1282         //获取使用记录
1283         map.put("cardItemInfoId",cardItemInfoId);
1284         map.put("sourceId",sourceId);
b91aa3 1285         map.put("userCardId",userCardId);
ea4351 1286         map.put("sourceType",UserCardUsed.SOURCE_TYPE_ORDER_ITEM_TWO);
b91aa3 1287         sqlSentence.sqlSentence(" SELECT * FROM user_card_used WHERE isDel = 0 AND userCardId = #{m.userCardId}" +
ea4351 1288                 " AND cardItemInfoId = #{m.cardItemInfoId} AND sourceId = #{m.sourceId} AND sourceType = #{m.sourceType}",map);
e9e32c 1289         List<UserCardUsed> userCardUsedList = commonService.selectList(UserCardUsedMapper.class,sqlSentence);
C 1290
1291         //因为之前的使用记录有些没有关联到订单,所以找不到
1292         if(userCardUsedList == null || userCardUsedList.size() == 0){
1293             map.clear();
1294             map.put("cardItemInfoId",cardItemInfoId);
b91aa3 1295             map.put("userCardId",userCardId);
C 1296             map.put("orderId",orderId);
ea4351 1297             map.put("sourceType",UserCardUsed.SOURCE_TYPE_ORDER_ITEM_TWO);
C 1298             sqlSentence.sqlSentence(" SELECT * FROM user_card_used WHERE isDel = 0 AND sourceType = #{m.sourceType} AND userCardId = #{m.userCardId}" +
b91aa3 1299                     " AND cardItemInfoId = #{m.cardItemInfoId} AND orderId = #{m.orderId} AND sourceId IS NULL",map);
e9e32c 1300             userCardUsedList = commonService.selectList(UserCardUsedMapper.class,sqlSentence);
b91aa3 1301             if(userCardUsedList.size() == 0){
ea4351 1302                 sqlSentence.sqlSentence(" SELECT * FROM user_card_used WHERE isDel = 0 AND sourceType = #{m.sourceType} AND userCardId = #{m.userCardId}" +
b91aa3 1303                         " AND cardItemInfoId = #{m.cardItemInfoId} AND sourceId IS NULL",map);
C 1304                 userCardUsedList = commonService.selectList(UserCardUsedMapper.class,sqlSentence);
1305             }
a1c527 1306         }
C 1307
a88f94 1308         //获取总使用数量
C 1309         int usedNum = 0;
1310         for(UserCardUsed userCardUsed:userCardUsedList){
1311             usedNum = usedNum+userCardUsed.getUsedNum();
a1c527 1312         }
C 1313
1314         //计算退回的卡次
a88f94 1315         int returnNum = UserCardTool.countUsedNumber(refundNum,cardItemInfo,cardEquity);
a1c527 1316
a88f94 1317         if(returnNum > usedNum){
2b1cfa 1318             logger.error("卡包可退数量错误-卡包退款失败,查询语句:{},传入参数:{}",sqlSentence.getSqlSentence(), JSON.toJSONString(sqlSentence.getM()));
a88f94 1319             logger.error("卡包可退数量错误-卡包退款失败,userCardId:{},cardItemInfoId:{},sourceId:{},orderId:{},refundNum:{},returnNum:{},usedNum:{}",userCardId,cardItemInfoId,sourceId,orderId,refundNum,returnNum,usedNum);
1869f3 1320             throw new TipsException("卡包可退数量错误!");
C 1321         }
1322
e9e32c 1323         //已经操作数量
a88f94 1324         int opNum = returnNum;
e9e32c 1325         StringBuilder sql;
1869f3 1326         for(UserCardUsed userCardUsed:userCardUsedList){
1d573f 1327             //判断是否够了,跳出循环
a88f94 1328             if(opNum <= 0){
1d573f 1329                 break;
C 1330             }
a88f94 1331             if(userCardUsed.getUsedNum() <= 0){
C 1332                 continue;
1333             }
e9e32c 1334             map.clear();
a88f94 1335             sql = new StringBuilder();
1869f3 1336             map.put("id",userCardUsed.getId());
a88f94 1337             map.put("usedNumOld",userCardUsed.getUsedNum());
C 1338             if(opNum >= userCardUsed.getUsedNum()){
1339                 map.put("isDel",UserCardUsed.YES);
1340                 map.put("countNum",userCardUsed.getUsedNum());
1341                 opNum = opNum - userCardUsed.getUsedNum();
1342             }else{
1343                 map.put("isDel",UserCardUsed.NO);
1344                 map.put("countNum",opNum);
1345                 opNum = 0;
1346             }
1347             sql.append("isDel = #{m.isDel}");
1348             sql.append(",usedNum =  usedNum - #{m.countNum}");
1349             sql.append(",cancelNum =  cancelNum + #{m.countNum}");
e9e32c 1350             if(StringUtils.isEmpty(userCardUsed.getOrderId())){
C 1351                 sql.append(",orderId = #{m.orderId}");
1352                 map.put("orderId",orderId);
1353             }
1354             if(StringUtils.isEmpty(userCardUsed.getSourceId())){
1355                 sql.append(",sourceId = #{m.sourceId}");
1356                 map.put("sourceId",sourceId);
1357             }
a88f94 1358             sql.append(" WHERE id = #{m.id} AND isDel = 0 AND usedNum = #{m.usedNumOld}");
e9e32c 1359             sqlSentence.sqlUpdate(sql.toString(),map);
a88f94 1360             if(commonService.updateWhere(UserCardUsedMapper.class,sqlSentence)!=1){
db3dfa 1361                 throw new TipsException("卡包使用记录已发生变化,请重试!");
a88f94 1362             }
1869f3 1363         }
a88f94 1364         if(opNum != 0){
b91aa3 1365             logger.error("卡包回退数量错误-卡包退款失败,userCardId:{},cardItemInfoId:{},sourceId:{},orderId:{},refundNum:{},returnNum:{},opNum:{}",userCardId,cardItemInfoId,sourceId,orderId,refundNum,returnNum,opNum);
a1c527 1366             throw new TipsException("卡包回退数量错误!");
1869f3 1367         }
ae6ff7 1368     }
1869f3 1369
a1c527 1370     /**退款-处理促销
C 1371      * @param operationId 操作人标识
1372      * @param refundRecord 退款总记录
1373      * @param ordersTotal 订单
1374      * @param refundRecordItem 退款一级记录
1375      * @param refundCarryVo 全局携带参数结构
1376      * @param commonService 映射
1377      * @return 全局携带参数结构
ae6ff7 1378      */
1869f3 1379     private static RefundCarryVo handRefundPromotion(String operationId, RefundRecord refundRecord, OrdersTotal ordersTotal
e2a6c2 1380             , RefundRecordItem refundRecordItem,RefundCarryVo refundCarryVo,CommonService commonService,DeductionSingleService deductionSingleService) {
ae6ff7 1381
C 1382         //查看订单信息
1869f3 1383         OrderItem orderItem = commonService.selectOneByKey(OrderItemMapper.class,refundRecordItem.getOrderItemId());
ae6ff7 1384         if(orderItem == null ){
C 1385             throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"未找到子订单信息[84]!");
1386         }
1387
a1c527 1388         SqlSentence sqlSentence = new SqlSentence();
C 1389         Map<String, Object> map = new HashMap<>();
1390
ae6ff7 1391         //获取退款二级单
C 1392         map.put("refundRecordItemId",refundRecordItem.getId());
a1c527 1393         sqlSentence.sqlSentence("select * from refund_record_item_source where isDel = 0 AND refundRecordItemId = #{m.refundRecordItemId}",map);
ae6ff7 1394         List<RefundRecordItemSource> sons = commonService.selectList(RefundRecordItemSourceMapper.class, sqlSentence);
C 1395
1869f3 1396         //根据支付记录总表的标识来整合已退的金额,key值:支付记录总表标识,value:金额数据
C 1397         Map<String,RefundRecordConsumePay> refundRecordConsumePayMap = new HashMap<>();
1398         RefundRecordConsumePay refundRecordConsumePay;
1399         //计算本次退款方式的划扣金额
1400         BigDecimal deductionTotal = BigDecimal.ZERO;
2a45d4 1401         //分配的用户项目划扣金额
C 1402         BigDecimal deductionTotalUser = BigDecimal.ZERO;
99213f 1403         //计算本次退款方式的现金金额
C 1404         BigDecimal cashTotal = BigDecimal.ZERO;
1405         //退款现金总金额(元,不包含退到储值金的)
1406         BigDecimal cashPurenessTotal = BigDecimal.ZERO;;
1407         //退款储值金总金额(元)
1408          BigDecimal rechargeTotal = BigDecimal.ZERO;;
1409         //退款增值金总金额(元)
1410          BigDecimal incrementTotal = BigDecimal.ZERO;;
1411         //退款积分
1412          BigDecimal integralTotal = BigDecimal.ZERO;;
1413         //退款总金额(元)
1414         BigDecimal refundTotal = BigDecimal.ZERO;;
1869f3 1415
C 1416         for (RefundRecordItemSource son : sons) {
1417             //初始化总结构携带参数
1418             refundCarryVo.setRefundConsumePayList(new ArrayList<>());
1419             refundCarryVo.setDeductionTotal(BigDecimal.ZERO);
1420             refundCarryVo.setCashTotal(BigDecimal.ZERO);
2a45d4 1421             refundCarryVo.setDeductionTotalUser(BigDecimal.ZERO);
99213f 1422             refundCarryVo.setDeductionTotalUser(BigDecimal.ZERO);
C 1423             refundCarryVo.setCashPurenessTotal(BigDecimal.ZERO);
1424             refundCarryVo.setRechargeTotal(BigDecimal.ZERO);
1425             refundCarryVo.setIntegralTotal(BigDecimal.ZERO);
1426             refundCarryVo.setIncrementTotal(BigDecimal.ZERO);
1427             refundCarryVo.setRefundTotal(BigDecimal.ZERO);
1869f3 1428             switch (GroupTypeEnum.getCode(son.getType())){
C 1429                 case PROJECT:
e2a6c2 1430                     refundCarryVo = handRefundNoSonExecution(refundRecord,refundRecordItem,son,refundCarryVo,orderItem.getUserCardId(),commonService,deductionSingleService);
1869f3 1431                     break;
C 1432                 case RETAIL:
b91aa3 1433                     refundCarryVo = handRefundSonRerail(refundRecord,refundRecordItem,son,refundCarryVo,orderItem.getUserCardId(),commonService);
1869f3 1434                     break;
C 1435                 case INCREMENT:
1436                     //增值金
1437                     //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
1438                     if(son.getRealRefundTotal().negate().compareTo(BigDecimal.ZERO)!=0){
1439                         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);
1440                     }
1441                     break;
1442                 case STORED:
1443                     //储值金额
1444                     //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
1445                     if(son.getRealRefundTotal().negate().compareTo(BigDecimal.ZERO)!=0){
1446                         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);
1447                     }
1448                     break;
1449                 case INTEGRAL:
1450                     //积分
1451                     //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
1452                     if(son.getRealRefundTotal().negate().compareTo(BigDecimal.ZERO)!=0){
1453                         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);
1454                     }
1455                     break;
1456             }
1457             deductionTotal = deductionTotal.add(refundCarryVo.getDeductionTotal());
1458             cashTotal = cashTotal.add(refundCarryVo.getCashTotal());
2a45d4 1459             deductionTotalUser = deductionTotalUser.add(refundCarryVo.getDeductionTotalUser());
99213f 1460             cashPurenessTotal = cashPurenessTotal.add(refundCarryVo.getCashPurenessTotal());
C 1461             rechargeTotal = rechargeTotal.add(refundCarryVo.getRechargeTotal());
1462             incrementTotal = incrementTotal.add(refundCarryVo.getIncrementTotal());
1463             integralTotal = integralTotal.add(refundCarryVo.getIntegralTotal());
1464             refundTotal = refundTotal.add(refundCarryVo.getRefundTotal());
1869f3 1465             //遍历叠加支付方式记录的退款金额
C 1466             for(RefundRecordConsumePay re:refundCarryVo.getRefundConsumePayList()){
2a45d4 1467                 refundRecordConsumePay = refundRecordConsumePayMap.computeIfAbsent(re.getConsumePayId(),k->new RefundRecordConsumePay(BigDecimal.ZERO,re.getNumberNo(),re.getName(),re.getIsMoneyPay(),re.getIsExecute(),re.getConsumePayId()));
1869f3 1468                 refundRecordConsumePay.setRefundTotal(refundRecordConsumePay.getRefundTotal().add(re.getRefundTotal()));
ae6ff7 1469             }
C 1470         }
1471
1869f3 1472         //转载返回支付方式记录的退款金额
C 1473         List<RefundRecordConsumePay> refundRecordConsumePayList = new ArrayList<>();
1474         for (Map.Entry<String, RefundRecordConsumePay> entry : refundRecordConsumePayMap.entrySet()) {
1475             refundRecordConsumePayList.add(entry.getValue());
1476         }
1477         refundCarryVo.setCashTotal(cashTotal);
1478         refundCarryVo.setDeductionTotal(deductionTotal);
2a45d4 1479         refundCarryVo.setDeductionTotalUser(deductionTotalUser);
1869f3 1480         refundCarryVo.setRefundConsumePayList(refundRecordConsumePayList);
99213f 1481         refundCarryVo.setCashPurenessTotal(cashPurenessTotal);
C 1482         refundCarryVo.setRechargeTotal(rechargeTotal);
1483         refundCarryVo.setIncrementTotal(incrementTotal);
1484         refundCarryVo.setRefundTotal(refundTotal);
2a45d4 1485
C 1486         //更新退款子单
99213f 1487         updateRefundItem(refundCarryVo,refundRecordItem.getId(),commonService);
ae6ff7 1488
1869f3 1489         //获取其子项
ae6ff7 1490         map.put("orderItemId",orderItem.getId());
e9e32c 1491         sqlSentence.sqlSentence("select refundStatus from order_item_source WHERE orderItemId=#{m.orderItemId} and isDel=0",map);
ae6ff7 1492         List<OrderItemSon> orderItemSonList=commonService.selectList(OrderItemSonMapper.class,sqlSentence);
a1c527 1493         int refundStatus;
1869f3 1494         Integer refundNum = 0;
a1c527 1495         List<Integer> collect = orderItemSonList.stream().map(OrderItemSon::getRefundStatus).collect(Collectors.toList());
1869f3 1496
ae6ff7 1497         if(collect.contains(OrderTotalConstants.STATUS_REFUND_PART)){
1869f3 1498             refundStatus = OrderTotalConstants.STATUS_REFUND_PART;
ae6ff7 1499         }else if (collect.contains(OrderTotalConstants.STATUS_REFUND_NONE)){
1869f3 1500             if(collect.contains(OrderTotalConstants.STATUS_REFUND_FINSH)){
C 1501                 refundStatus = OrderTotalConstants.STATUS_REFUND_PART;
1502             }else{
1503                 refundStatus = OrderTotalConstants.STATUS_REFUND_NONE;
1504             }
ae6ff7 1505         }else if (collect.contains(OrderTotalConstants.STATUS_REFUND_FINSH)){
1869f3 1506             refundStatus = OrderTotalConstants.STATUS_REFUND_FINSH;
C 1507             refundNum = orderItem.getBuyNum();
ae6ff7 1508         }else {
1869f3 1509             refundStatus = OrderTotalConstants.STATUS_REFUND_NONE;
ae6ff7 1510         }
C 1511
1869f3 1512         //更新子订单信息
99213f 1513         updateOrderItemOne(orderItem,refundStatus,refundCarryVo,refundNum,commonService);
1869f3 1514
C 1515         return refundCarryVo;
ae6ff7 1516     }
C 1517
1869f3 1518     /**退款-处理二级子订单未执行划扣   项目类型
a1c527 1519      * @param refundRecord 退款总记录
C 1520      * @param refundRecordItem 退款一级子单
1521      * @param refundRecordItemSource  退款二级子单
1522      * @param refundCarryVo 全局携带参数接口
1523      * @param commonService 映射
ae6ff7 1524      */
1869f3 1525     private static RefundCarryVo handRefundNoSonExecution(RefundRecord refundRecord,RefundRecordItem refundRecordItem, RefundRecordItemSource refundRecordItemSource
e2a6c2 1526             ,RefundCarryVo refundCarryVo,String userCardId,CommonService commonService,DeductionSingleService deductionSingleService) {
ae6ff7 1527
C 1528         //判断操作完了去修改子订单状态
510678 1529         OrderItemSon orderItemSon = commonService.selectOneByKey(OrderItemSonMapper.class,refundRecordItemSource.getOrderItemSonId());
ae6ff7 1530         if (orderItemSon==null){
510678 1531             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到二级子订单信息[02]");
ae6ff7 1532         }
e2a6c2 1533
C 1534         //先作废划扣,不然数量对不上
4215f9 1535         deductionCancel(refundRecord,refundRecordItemSource.getId(),commonService,deductionSingleService);
ae6ff7 1536
C 1537         UserProjectItem userProjectItem = getUserProject(orderItemSon.getId(),commonService);
1538         if(userProjectItem == null){
510678 1539             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"用户项目获取失败[06]!");
ae6ff7 1540         }
C 1541         if(userProjectItem.getNotUsedNum()<refundRecordItemSource.getRefundNum()){
1542             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款次数大于可退次数[83]!");
1543         }
1544
1545         //计算子单是否还有剩余的可扣疗程数
1546         int surplusNum = orderItemSon.getUsedTotal() - orderItemSon.getHasReNum();
1547         if(refundRecordItemSource.getRefundNum() > surplusNum){
510678 1548             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款次数大于可退次数[56]!");
ae6ff7 1549         }
C 1550
1869f3 1551         //处理用户项目,减去用户项目数量
C 1552         UserProjectDeductionVo userProjectDeductionVo= UserProjectTool.userProjectDeduction(userProjectItem,UserProjectUsedCon.USED_METHOD_ORDER_REFUND,UserProjectUsedCon.USED_TYPE_DEDUCTION,null
ae6ff7 1553                 ,refundRecordItemSource.getId(),refundRecordItemSource.getRefundNum(),refundRecord.getOperatorAppId(),refundRecord.getOperatorAppName(),refundRecord.getRefundShopId(),refundRecord.getRefundShopName(),"员工备注:"+refundRecord.getRemarks()+"|用户备注:"+refundRecord.getRefundReason(),commonService);
2a45d4 1554
C 1555         refundCarryVo.setDeductionTotalUser(userProjectDeductionVo.getDeductionTotal());
ae6ff7 1556
a1c527 1557         int refundStatus;
ae6ff7 1558         if(surplusNum == refundRecordItemSource.getRefundNum()){
C 1559             refundStatus=OrderTotalConstants.STATUS_REFUND_FINSH;
1560         }else{
1561             refundStatus=OrderTotalConstants.STATUS_REFUND_PART;
1562         }
1563
1869f3 1564         //处理退款支付方式
99213f 1565         refundCarryVo = refundRecordMotnedItemTwoHandle(refundRecord,refundRecordItemSource,refundCarryVo,commonService);
C 1566         //更新子单信息
1567         updateOrderItemTwo(orderItemSon,refundStatus,refundCarryVo,refundRecordItemSource.getRefundNum(),commonService);
1869f3 1568
C 1569         if(refundRecordItem.getType().equals(OrderItemConstants.CARD_BAG)){
1570             //是卡包的,刪除卡包使用
b91aa3 1571             deleteUserCardUsed(userCardId,orderItemSon.getCardItemInfoId(),orderItemSon.getId(),orderItemSon.getOrderId(),refundRecordItemSource.getRefundNum(),commonService);
ae6ff7 1572         }
C 1573
1869f3 1574         return refundCarryVo;
ae6ff7 1575     }
1869f3 1576
C 1577     /**退款二级是商品
a1c527 1578      * @param refundRecord 退款总记录
C 1579      * @param refundRecordItem 退款一级子单
1580      * @param refundRecordItemSource  退款二级子单
1581      * @param refundCarryVo 全局携带参数结构
1582      * @param commonService 映射
1583      * @return 全局携带参数结构
ae6ff7 1584      */
a1c527 1585     private static RefundCarryVo handRefundSonRerail(RefundRecord refundRecord,RefundRecordItem refundRecordItem, RefundRecordItemSource refundRecordItemSource
b91aa3 1586             ,RefundCarryVo refundCarryVo,String userCardId,CommonService commonService) {
ae6ff7 1587
C 1588         //判断操作完了去修改子订单状态
1869f3 1589         OrderItemSon orderItemSon=commonService.selectOneByKey(OrderItemSonMapper.class,refundRecordItemSource.getOrderItemSonId());
C 1590         if (orderItemSon==null){
ae6ff7 1591             throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"未找到二级子订单信息");
C 1592         }
1593
1869f3 1594         //计算子单是否还有剩余的可扣疗程数
C 1595         int surplusNum = orderItemSon.getBuyNum() - orderItemSon.getHasReNum();
1596         if(refundRecordItemSource.getRefundNum() > surplusNum){
1597             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款次数大于可退次数[84]!");
ae6ff7 1598         }
C 1599
a1c527 1600         int refundStatus;
1869f3 1601         if(surplusNum == refundRecordItemSource.getRefundNum()){
C 1602             refundStatus=OrderTotalConstants.STATUS_REFUND_FINSH;
1603         }else{
1604             refundStatus=OrderTotalConstants.STATUS_REFUND_PART;
1605         }
1606
1607         //处理退款支付方式
99213f 1608         refundCarryVo = refundRecordMotnedItemTwoHandle(refundRecord,refundRecordItemSource,refundCarryVo,commonService);
C 1609
1610         updateOrderItemTwo(orderItemSon,refundStatus,refundCarryVo,refundRecordItemSource.getRefundNum(),commonService);
a1c527 1611
C 1612         if(refundRecordItem.getType().equals(OrderItemConstants.CARD_BAG)){
1613             //是卡包的,刪除卡包使用
b91aa3 1614             deleteUserCardUsed(userCardId,orderItemSon.getCardItemInfoId(),orderItemSon.getId(),orderItemSon.getOrderId(),refundRecordItemSource.getRefundNum(),commonService);
a1c527 1615         }
1869f3 1616
C 1617         return refundCarryVo;
1618     }
1619
1620     /**更新订单一级子单的信息
1621      * @param orderItem 订单子单
1622      * @param refundStatus 退款状态
99213f 1623      * @param refundCarryVo 传参
160c33 1624      * @param refundNum 退款数量 正负数
1869f3 1625      * @param commonService 映射
C 1626      */
99213f 1627     public static void updateOrderItemOne(OrderItem orderItem,Integer refundStatus,RefundCarryVo refundCarryVo,Integer refundNum
1869f3 1628             ,CommonService commonService){
C 1629         SqlSentence sqlSentence = new SqlSentence();
1630         Map<String,Object> values = new HashMap<>();
1631
510678 1632         values.put("id", orderItem.getId());
1869f3 1633         values.put("refundStatus", refundStatus);
C 1634         values.put("oldHasReNum",orderItem.getHasReNum());
99213f 1635         values.put("refundNum",refundNum);
C 1636         values.put("refundTotal",refundCarryVo.getRefundTotal());
1637         values.put("reIntegralTotal",refundCarryVo.getIntegralTotal());
1638         values.put("reCashTotal",refundCarryVo.getCashTotal());
1639         values.put("reCashPurenessTotal",refundCarryVo.getCashPurenessTotal());
1640         values.put("reRechargeTotal",refundCarryVo.getRechargeTotal());
1641         values.put("reIncrementTotal",refundCarryVo.getIncrementTotal());
1869f3 1642         sqlSentence.sqlUpdate(" refundStatus=#{m.refundStatus},reTotal=reTotal+#{m.refundTotal},hasReNum=hasReNum+#{m.refundNum}" +
99213f 1643                 ",reIntegralTotal = reIntegralTotal+#{m.reIntegralTotal},reCashTotal = reCashTotal+#{m.reCashTotal}" +
C 1644                 ",reCashPurenessTotal = reCashPurenessTotal+#{m.reCashPurenessTotal},reRechargeTotal = reRechargeTotal+#{m.reRechargeTotal}" +
1645                 ",reIncrementTotal = reIncrementTotal+#{m.reIncrementTotal}" +
510678 1646                 " WHERE isDel = 0 AND id = #{m.id} AND hasReNum = #{m.oldHasReNum}",values);
1869f3 1647         if(commonService.updateWhere(OrderItemMapper.class,sqlSentence) != 1){
C 1648             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"当前订单信息已发生变化,请重试[012]!");
1649         }
1650     }
1651
1652     /**更新订单二级子单的信息
1653      * @param orderItemSon 订单子单
1654      * @param refundStatus 退款状态
99213f 1655      * @param refundCarryVo 传参
1869f3 1656      * @param refundNum 退款数量
C 1657      * @param commonService 映射
1658      */
99213f 1659     public static void updateOrderItemTwo(OrderItemSon orderItemSon,Integer refundStatus,RefundCarryVo refundCarryVo,Integer refundNum
1869f3 1660             ,CommonService commonService){
C 1661         SqlSentence sqlSentence = new SqlSentence();
1662         Map<String,Object> values = new HashMap<>();
1663
1664         values.put("id", orderItemSon.getId());
1665         values.put("refundStatus", refundStatus);
1666         values.put("oldHasReNum",orderItemSon.getHasReNum());
99213f 1667         values.put("refundTotal",refundCarryVo.getRefundTotal());
C 1668         values.put("refundNum",refundNum);
1669         values.put("reIntegralTotal",refundCarryVo.getIntegralTotal());
1670         values.put("reCashTotal",refundCarryVo.getCashTotal());
1671         values.put("reCashPurenessTotal",refundCarryVo.getCashPurenessTotal());
1672         values.put("reRechargeTotal",refundCarryVo.getRechargeTotal());
1673         values.put("reIncrementTotal",refundCarryVo.getIncrementTotal());
510678 1674         sqlSentence.sqlUpdate(" refundStatus = #{m.refundStatus},reTotal=reTotal+#{m.refundTotal},hasReNum=hasReNum+#{m.refundNum}" +
99213f 1675                 ",reIntegralTotal = reIntegralTotal+#{m.reIntegralTotal},reCashTotal = reCashTotal+#{m.reCashTotal}" +
C 1676                 ",reCashPurenessTotal = reCashPurenessTotal+#{m.reCashPurenessTotal},reRechargeTotal = reRechargeTotal+#{m.reRechargeTotal}" +
1677                 ",reIncrementTotal = reIncrementTotal+#{m.reIncrementTotal}" +
1869f3 1678                 " where isDel=0 AND id = #{m.id} AND hasReNum = #{m.oldHasReNum}",values);
C 1679         if(commonService.updateWhere(OrderItemSonMapper.class,sqlSentence) != 1){
1680             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"当前订单信息已发生变化,请重试[012]!");
1681         }
1682     }
1683
a1c527 1684     /**获取订单一级子单的支付方式记录
C 1685      * @param orderItemId 订单一级子单标识
1686      * @param commonService 映射
1687      * @return 订单一级子单的支付方式记录
1869f3 1688      */
C 1689     public static List<ConsumePayItem> getOrderItemOneConsumePay(String orderItemId,CommonService commonService){
1690
1691         SqlSentence sqlSentence = new SqlSentence();
1692         Map<String,Object> values = new HashMap<>();
1693
1694         //获取子单的支付方式,一级子单支付记录,计算可退款金额
1695         values.put("typeId",orderItemId);
1696         sqlSentence.sqlSentence("SELECT * FROM consume_pay_item WHERE isDel = 0 AND typeId = #{m.typeId}",values);
2a45d4 1697         return commonService.selectList(ConsumePayItemMapper.class,sqlSentence);
1869f3 1698     }
C 1699
a1c527 1700     /**获取订单二级子单的支付方式记录
C 1701      * @param orderItemId 订单一级子单标识
1702      * @param commonService 映射
1703      * @return 订单二级子单的支付方式记录
1869f3 1704      */
C 1705     public static List<ConsumePayItem> getOrderItemTwoConsumePay(String orderItemId,CommonService commonService){
1706
1707         SqlSentence sqlSentence = new SqlSentence();
1708         Map<String,Object> values = new HashMap<>();
1709
1710         //获取子单的支付方式,一级子单支付记录,计算可退款金额
1711         values.put("typeId",orderItemId);
1712         sqlSentence.sqlSentence("SELECT * FROM consume_pay_item_son WHERE isDel = 0 AND typeId = #{m.typeId}",values);
2a45d4 1713         List<ConsumePayItemSon> consumePayItemSonList = commonService.selectList(ConsumePayItemSonMapper.class,sqlSentence);
C 1714         List<ConsumePayItem> consumePayItemList = new ArrayList<>();
1715         ConsumePayItem consumePayItem;
1716         for(ConsumePayItemSon consumePayItemSon:consumePayItemSonList){
1717             consumePayItem = new ConsumePayItem();
1718             BeanUtils.copyProperties(consumePayItemSon,consumePayItem);
1719             consumePayItemList.add(consumePayItem);
1720         }
1721         return consumePayItemList;
ae6ff7 1722     }
C 1723
1724     /**保存退款方式和支付方式关联
1725      * @param refundTotal 退款金额
1726      * @param commonType 子单级别,可空
1727      * @param commonId 子单标识,可空
1728      * @param orderId  总单标识
1729      * @param numberNo 支付方式编号
1730      * @param name 支付方式名称
1731      * @param consumePayId 支付方式记录标识
1732      * @param refundMethodId 退款方式标识
1733      * @param refundRecordItemId 退款子记录标识,可空
1734      * @param refundRecordId 退款总记录标识
1735      * @param commonService 映射
1736      * @return 返回关联记录
1737      */
1738     public static RefundRecordConsumePay insertRefundRecordConsumePay(BigDecimal refundTotal, String commonType, String commonId, String orderId
2a45d4 1739             , String numberNo,String name,Integer isMoneyPay,Integer isExecute, String consumePayId, String refundMethodId,String refundRecordItemId,String refundRecordId,CommonService commonService){
ae6ff7 1740         //生成关联记录
C 1741         RefundRecordConsumePay refundRecordConsumePay = new RefundRecordConsumePay();
1742         refundRecordConsumePay.setRefundTotal(refundTotal);
1743         refundRecordConsumePay.setCommonType(commonType);
1744         refundRecordConsumePay.setCommonId(commonId);
1745         refundRecordConsumePay.setOrderId(orderId);
1746         refundRecordConsumePay.setName(name);
1747         refundRecordConsumePay.setNumberNo(numberNo);
2a45d4 1748         refundRecordConsumePay.setIsMoneyPay(isMoneyPay);
C 1749         refundRecordConsumePay.setIsExecute(isExecute);
ae6ff7 1750         refundRecordConsumePay.setConsumePayId(consumePayId);
C 1751         refundRecordConsumePay.setRefundMethodId(refundMethodId);
1752         refundRecordConsumePay.setRefundRecordItemId(refundRecordItemId);
1753         refundRecordConsumePay.setRefundRecordId(refundRecordId);
1754         commonService.insert(RefundRecordConsumePayMapper.class,refundRecordConsumePay);
1755         return refundRecordConsumePay;
1756     }
1757
1758     /**获取可退款的用户卡项
1759      * @param sourceId 订单子单标识
1760      * @return 可退款的用户卡项
1761      */
160c33 1762     public static List<UserCard> getRefundCard(String sourceId,int effectiveStatus,CommonService commonService){
ae6ff7 1763         SqlSentence sqlSentence = new SqlSentence();
C 1764         Map<String,Object> sqlMap = new HashMap<>();
1765         //获取用户卡项
1766         sqlMap.put("sourceId",sourceId);
1767         sqlMap.put("status",UserCard.TYPE_NO_USED);
160c33 1768         sqlMap.put("effectiveStatus",effectiveStatus);
ae6ff7 1769         sqlSentence.sqlSentence("SELECT * FROM user_card WHERE isDel = 0 AND sourceId = #{m.sourceId} AND status = #{m.status}" +
C 1770                 " AND effectiveStatus = #{m.effectiveStatus} AND turnAddId IS NULL",sqlMap);
949373 1771         List<UserCard> userCardList = commonService.selectList(UserCardMapper.class,sqlSentence);
C 1772         if(userCardList.size() == 0){
1773             return userCardList;
1774         }
ea4351 1775
949373 1776         //获取已经部分退的用户卡包数量
ea4351 1777         List<RefundRecordItem> refundRecordItemList = CardRefundTool.findRefundUserCard(sourceId,null,commonService);
949373 1778         //过滤掉没有使用但有部分退款
C 1779         if(refundRecordItemList.size() == 0){
1780             return userCardList;
1781         }
1782
1783         Map<String,UserCard> userCardMap = new HashMap<>();
1784         for(UserCard userCard:userCardList){
1785             userCardMap.put(userCard.getId(),userCard);
1786         }
1787         //去除掉参与部分退款的用户卡包
1788         for(RefundRecordItem refundRecordItem:refundRecordItemList){
1789             userCardMap.remove(refundRecordItem.getUserCardId());
1790         }
1791
1792         userCardList = new ArrayList<>();
ea4351 1793         List<UserCardUsed> userCardUsedList;
949373 1794         for (Map.Entry<String,UserCard> entry : userCardMap.entrySet()) {
ea4351 1795             //获取使用记录,如果有使用记录,那么就跳过
C 1796             userCardUsedList = UserCardTool.getUsedRecord(entry.getValue().getId(),null,null,commonService);
1797             if(userCardUsedList.size() > 0){
1798                 continue;
1799             }
949373 1800             userCardList.add(entry.getValue());
C 1801         }
1802
1803         return userCardList;
ae6ff7 1804
C 1805     }
1806
1807     /**获取用户项目*/
1808     public static UserProjectItem getUserProject(String commonId,CommonService commonService){
1809         SqlSentence sqlSentence = new SqlSentence();
1810         Map<String, Object> map = new HashMap<>();
1811
1812         map.put("commonId",commonId);
1813         sqlSentence.sqlSentence("select * from user_project_item where isDel=0 and commonId=#{m.commonId} and isTransfer = 0",map);
a1c527 1814         return commonService.selectOne(UserProjectItemMapper.class,sqlSentence);
dd4c9b 1815     }
C 1816
e2a6c2 1817     /**作废划扣记录
C 1818      * @param refundRecordItemId 退款子记录标识(RefundRecordItem/RefundRecordItemSource)
4215f9 1819      * @param refundRecord 退款总记录
e2a6c2 1820      * @param commonService 映射
C 1821      * @param deductionSingleService 映射
1822      */
4215f9 1823     public static void deductionCancel(RefundRecord refundRecord,String refundRecordItemId, CommonService commonService, DeductionSingleService deductionSingleService){
e2a6c2 1824
C 1825         SqlSentence sqlSentence = new SqlSentence();
1826         Map<String,Object> values = new HashMap<>();
1827
1828         values.put("refundRecordItemId",refundRecordItemId);
1829         sqlSentence.sqlSentence("SELECT * FROM refund_deduction_cancel WHERE isDel = 0 AND refundRecordItemId = #{m.refundRecordItemId}",values);
1830         List<RefundDeductionCancel> refundDeductionCancelList = commonService.selectList(RefundDeductionCancelMapper.class,sqlSentence);
1831
a88f0b 1832         //变更状态
C 1833         values.put("status",RefundDeductionCancel.STATUS_CANCEL);
1834         values.put("statusNot",RefundDeductionCancel.STATUS_NOT);
1835         sqlSentence.sqlUpdate("status = #{m.status} WHERE id = #{m.id} AND status = #{m.statusNot}",values);
1836
e2a6c2 1837         //开始作废划扣
C 1838         DeductionDto deductionDto;
1839         for(RefundDeductionCancel refundDeductionCancel:refundDeductionCancelList){
a88f0b 1840             if(refundDeductionCancel.getStatus() == RefundDeductionCancel.STATUS_CANCEL){
C 1841                 continue;
1842             }
e2a6c2 1843             deductionDto = new DeductionDto();
C 1844             deductionDto.setDeductionSingleId(refundDeductionCancel.getDeductionSingleId());
4215f9 1845             deductionDto.setOperatorId(refundRecord.getOperatorId());
C 1846             deductionSingleService.deleteOne(deductionDto,refundRecord.getOperatorAppId(),refundRecord.getOperatorAppName(),DeductionSingleConstants.STATUS_RESCINDED, BaseEntity.NO);
a88f0b 1847
C 1848             //变更状态
1849             values.put("id",refundDeductionCancel.getId());
1850             if(commonService.updateWhere(RefundDeductionCancelMapper.class,sqlSentence) != 1){
1851                 throw new TipsException("作废划扣记录失败");
1852             }
e2a6c2 1853         }
C 1854     }
1855
ae6ff7 1856 }