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