chenjiahe
2023-05-10 01fbcb62fbd9ac4d72d1bed3d87da5a6c73af698
提交 | 用户 | age
fd7281 1 package com.hx.phip.tool.refund;
C 2
160c33 3 import com.alibaba.fastjson.JSON;
fd7281 4 import com.hx.common.service.CommonService;
160c33 5 import com.hx.exception.TipsException;
528195 6 import com.hx.mybatisTool.SqlSentence;
160c33 7 import com.hx.phiappt.common.*;
C 8 import com.hx.phiappt.constants.enums.GroupTypeEnum;
9 import com.hx.phiappt.constants.tool.RefundToolUtil;
fd7281 10 import com.hx.phiappt.model.BaseEntity;
C 11 import com.hx.phiappt.model.activity.ActivityAction;
12 import com.hx.phiappt.model.activity.ActivityRule;
160c33 13 import com.hx.phiappt.model.cardItem.CardEquity;
C 14 import com.hx.phiappt.model.cardItem.CardItemInfo;
15 import com.hx.phiappt.model.coupon.CouponNumber;
16 import com.hx.phiappt.model.coupon.CouponOrderDiscountLog;
17 import com.hx.phiappt.model.order.*;
18 import com.hx.phiappt.model.refund.*;
19 import com.hx.phiappt.model.user.UserCard;
20 import com.hx.phiappt.model.user.UserCardUsed;
21 import com.hx.phiappt.model.user.UserProjectItem;
fd7281 22 import com.hx.phiappt.model.userMoney.UserMoneyUnclaimed;
528195 23 import com.hx.phip.dao.mapper.*;
160c33 24 import com.hx.phip.service.order.impl.OrderRefundServiceImpl;
4ebb90 25 import com.hx.phip.tool.user.UserCardTool;
160c33 26 import com.hx.phip.tool.user.UserProjectTool;
fd7281 27 import com.hx.phip.util.api.UserMoneyUtil;
4ebb90 28 import com.hx.phip.vo.user.UserCardItemInfoVo;
fd7281 29 import com.hx.util.StringUtils;
C 30 import com.platform.exception.PlatTipsException;
31 import com.platform.resultTool.PlatformCode;
160c33 32 import org.slf4j.Logger;
C 33 import org.slf4j.LoggerFactory;
fd7281 34
C 35 import java.math.BigDecimal;
528195 36 import java.util.HashMap;
fd7281 37 import java.util.List;
528195 38 import java.util.Map;
160c33 39 import java.util.stream.Collectors;
fd7281 40
C 41 /**
42  * 订单退款单作废工具
160c33 43  * @author CJH
fd7281 44  */
C 45 public class OrderRefundCancelTool {
46
160c33 47     /**log4j日志*/
C 48     private static final Logger logger = LoggerFactory.getLogger(OrderRefundServiceImpl.class.getName());
49
4ebb90 50     /**操作类型-0退款执行*/
C 51     public static final int OPT_TYPE_EXECUTE = 0;
52     /**操作类型—1作废退款*/
53     public static final int OPT_TYPE_CANCEL = 1;
54
160c33 55
C 56     /**处理总退款方式数据*/
57     public static void refundRecordMotnedHandle(RefundRecord refundRecord,OrdersTotal ordersTotal,CommonService commonService){
58         SqlSentence sqlSentence = new SqlSentence();
59         Map<String,Object> values = new HashMap<>();
60
61         //获取总退款方式
62         values.put("refundRecordId",refundRecord.getId());
63         sqlSentence.sqlSentence("SELECT * FROM refund_record_method WHERE isDel = 0 AND refundRecordId = #{m.refundRecordId}",values);
64         List<RefundRecordMethod> refundRecordMethodList = commonService.selectList(RefundRecordMethodMapper.class,sqlSentence);
65         //退款的支付方式记录数据
66         values.clear();
67         values.put("refundRecordId",refundRecord.getId());
68         sqlSentence.sqlSentence("SELECT * FROM refund_record_consume_pay WHERE isDel = 0 AND refundRecordId = #{m.refundRecordId}",values);
69         List<RefundRecordConsumePay> refundConsumePayList = commonService.selectList(RefundRecordConsumePayMapper.class,sqlSentence);
70
71         for(RefundRecordConsumePay refundRecordConsumePay:refundConsumePayList){
72             //更新支付记录退款信息
73             values.clear();
74             values.put("refundTotal",refundRecordConsumePay.getRefundTotal().negate());
75             values.put("id",refundRecordConsumePay.getConsumePayId());
4ebb90 76             sqlSentence.sqlUpdate("refundTotal = refundTotal + #{m.refundTotal} WHERE id = #{m.id} AND refundTotal + #{m.refundTotal} >= 0",values);
160c33 77             if(commonService.updateWhere(ConsumePayMapper.class,sqlSentence) != 1){
C 78                 throw new TipsException("退款金额错误[33]!");
79             }
80
81             //更新支付记录退款信息
82             values.clear();
83             values.put("status",RefundRecordConsumePay.NO);
84             values.put("statusOld",RefundRecordConsumePay.YES);
85             values.put("id",refundRecordConsumePay.getId());
01fbcb 86             sqlSentence.sqlUpdate("status = #{m.status} WHERE id = #{m.id} AND status = #{m.statusOld}",values);
160c33 87             if(commonService.updateWhere(RefundRecordConsumePayMapper.class,sqlSentence) != 1){
C 88                 throw new TipsException("退款金额记录状态错误!");
89             }
90
91         }
92
93         //处理回退到账
94         for(RefundRecordMethod refundRecordMethod:refundRecordMethodList){
95             if(PayMethodTypeConstants.PAY_STORED.equals(refundRecordMethod.getRefundNumberNo())){
96                 //储值金额 
97                 //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
98                 if(refundRecordMethod.getActualTotal().compareTo(BigDecimal.ZERO)>0){
99                     UserMoneyUtil.setNewUserMoneyUnclaimed(ordersTotal.getPayUserId()==null?ordersTotal.getUserId():ordersTotal.getPayUserId(),refundRecord.getInvalidRemarks(),"作废退款:支付方式储值金额扣减",refundRecord.getInvalidId(),refundRecord.getOrderId(),refundRecord.getInvalidAppCode(),refundRecord.getId(),refundRecordMethod.getRealRefundTotal().negate(), UserMoneyUnclaimed.FUND_TYPE_STORED_VALUE_FUND, OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.NO);
100                     OrderLog orderLog = RefundToolUtil.setOrderLog(refundRecord, refundRecord.getInvalidId(), refundRecord.getInvalidName(), refundRecordMethod.getName()+"作废退款金额:"+refundRecordMethod.getRealRefundTotal(), 1, OrderLogConstants.LOG_TYPE_REFUND_UNDO);
101                     commonService.insert(OrderLogMapper.class,orderLog);
102                 }
103             }else if(PayMethodTypeConstants.PAY_ADD_FUND.equals(refundRecordMethod.getRefundNumberNo())){
104                 //增值金
105                 //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
106                 if(refundRecordMethod.getActualTotal().compareTo(BigDecimal.ZERO)!=0){
107                     UserMoneyUtil.setNewUserMoneyUnclaimed(ordersTotal.getPayUserId()==null?ordersTotal.getUserId():ordersTotal.getPayUserId(),refundRecord.getRemarks(),"作废退款:支付方式增值金退回",refundRecord.getInvalidId(),refundRecord.getOrderId(),refundRecord.getInvalidAppCode(),refundRecord.getId(),refundRecordMethod.getRealRefundTotal().negate(), UserMoneyUnclaimed.FUND_TYPE_VALUE_ADDED_FUND, OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.NO);
108                     OrderLog orderLog = RefundToolUtil.setOrderLog(refundRecord,refundRecord.getInvalidId(),refundRecord.getInvalidName(),refundRecordMethod.getName(),1, OrderLogConstants.LOG_TYPE_REFUND_UNDO);
109                     commonService.insert(OrderLogMapper.class,orderLog);
110                 }
111             }else if(PayMethodTypeConstants.PAY_INTEGRAL.equals(refundRecordMethod.getRefundNumberNo())){
112                 //积分
113                 //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
114                 if(refundRecordMethod.getActualTotal().compareTo(BigDecimal.ZERO)!=0){
115                     UserMoneyUtil.setNewUserMoneyUnclaimed(ordersTotal.getPayUserId()==null?ordersTotal.getUserId():ordersTotal.getPayUserId(),refundRecord.getRemarks(),"作废退款:支付方式积分退回",refundRecord.getInvalidId(),refundRecord.getOrderId(),refundRecord.getInvalidAppCode(),refundRecord.getId(),refundRecordMethod.getRealRefundTotal().negate(), UserMoneyUnclaimed.FUND_TYPE_INTEGRAL,OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.NO);
116                     OrderLog orderLog = RefundToolUtil.setOrderLog(refundRecord,refundRecord.getInvalidId(),refundRecord.getInvalidName(),refundRecordMethod.getName()+"作废退款金额:"+refundRecordMethod.getRealRefundTotal(),1, OrderLogConstants.LOG_TYPE_REFUND_UNDO);
117                     commonService.insert(OrderLogMapper.class,orderLog);
118                 }
119             }else {
120                 //现金支付
121                 if(refundRecordMethod.getActualTotal().compareTo(BigDecimal.ZERO)<1){
122                     continue;
123                 }
124                 OrderLog orderLog = RefundToolUtil.setOrderLog(refundRecord,refundRecord.getInvalidId(),refundRecord.getInvalidName(),refundRecordMethod.getName()+"作废退款金额:"+refundRecordMethod.getRealRefundTotal(),1, OrderLogConstants.LOG_TYPE_REFUND_UNDO);
125                 orderLog.setOrderId(ordersTotal.getId());
126                 commonService.insert(OrderLogMapper.class,orderLog);
127             }
128         }
129     }
130
131     /**充值单特需处理
132      * @param ordersTotal 订单
133      * @param refundRecord 退款单
134      * @param optType 操作类型:0退款执行,1作废退款
135      * @param commonService 映射
136      */
137     public static void rechargeHandle(OrdersTotal ordersTotal, RefundRecord refundRecord,int optType, CommonService commonService){
755975 138
fd7281 139         if(!OrderTotalConstants.TYPE_RECHARGE.equals(ordersTotal.getType())){
C 140             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"操作失败,不是充值单!");
141         }
142         if(ordersTotal.getTotal().compareTo(BigDecimal.ZERO) < 1){
143             return;
144         }
4ebb90 145         OrderLog orderLog;
C 146         if(optType == OPT_TYPE_EXECUTE){
160c33 147             //充值金额扣减
C 148             UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getInvalidRemarks(),"退款单:"+refundRecord.getCode()+" 申请退款,扣减充值资金:"+ordersTotal.getTotal(),refundRecord.getInvalidId(),refundRecord.getOrderId(),refundRecord.getInvalidAppCode(),refundRecord.getId(),ordersTotal.getTotal().negate(), UserMoneyUnclaimed.FUND_TYPE_STORED_VALUE_FUND, OperationReasonConstants.OP_REASON_REFUND_CANCEL,commonService,UserMoneyUnclaimed.NO);
4ebb90 149             orderLog = RefundToolUtil.setOrderLog(refundRecord, refundRecord.getOperatorId(), refundRecord.getOperatorName(), "充值单:"+ordersTotal.getOrderNo()+"申请退款,预扣除金额:"+ordersTotal.getTotal(), 0, OrderLogConstants.LOG_TYPE_REFUND);
C 150         }else if(optType == OPT_TYPE_CANCEL){
160c33 151             //充值金额回退
C 152             UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getInvalidRemarks(),"退款单:"+refundRecord.getCode()+" 作废退款,退回充值资金:"+ordersTotal.getTotal(),refundRecord.getInvalidId(),refundRecord.getOrderId(),refundRecord.getInvalidAppCode(),refundRecord.getId(),ordersTotal.getTotal(), UserMoneyUnclaimed.FUND_TYPE_STORED_VALUE_FUND, OperationReasonConstants.OP_REASON_REFUND_CANCEL,commonService,UserMoneyUnclaimed.NO);
4ebb90 153             orderLog = RefundToolUtil.setOrderLog(refundRecord, refundRecord.getOperatorId(), refundRecord.getOperatorName(), "充值单:"+ordersTotal.getOrderNo()+"作废退款,退回预扣除金额:"+ordersTotal.getTotal(), 0, OrderLogConstants.LOG_TYPE_REFUND);
160c33 154         }else{
C 155             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"操作类型错误[1]["+optType+"]!");
156         }
4ebb90 157         orderLog.setOrderId(ordersTotal.getId());
C 158         commonService.insert(OrderLogMapper.class,orderLog);
fd7281 159     }
C 160
160c33 161     /**活动规则处理
C 162      * @param ordersTotal 订单
163      * @param refundRecord 退款单
164      * @param optType 操作类型:0退款执行,1作废退款
165      * @param commonService 映射
166      */
167     public static void activityRuleHandle(OrdersTotal ordersTotal, RefundRecord refundRecord,int optType,CommonService commonService){
fd7281 168
528195 169         //先获取活动规则
C 170         SqlSentence sqlSentence = new SqlSentence();
171         Map<String,Object> values = new HashMap<>();
172
173         values.put("orderId",ordersTotal.getId());
174         sqlSentence.sqlSentence("SELECT * FROM orders_activity_info WHERE isDel = 0 AND orderId = #{m.orderId}",values);
175         List<OrdersActivityInfo> ordersActivityInfoList = commonService.selectList(OrdersActivityInfoMapper.class,sqlSentence);
176         if(ordersActivityInfoList.size() > 0){
160c33 177             //订单活动信息处理
C 178             activityRulePollingHandle2(ordersActivityInfoList,optType,ordersTotal,refundRecord,commonService);
528195 179         }else{
160c33 180             //没有订单活动信息处理,就获取保存在订单里面的活动标识
C 181             values.clear();
182             values.put("orderId",ordersTotal.getId());
528195 183             sqlSentence.sqlSentence("SELECT * FROM orders_info WHERE isDel = 0 AND orderId = #{m.orderId}",values);
C 184             OrderInfo orderInfo = commonService.selectOne(OrderInfoMapper.class,sqlSentence);
160c33 185             if(orderInfo!=null && StringUtils.noNull(orderInfo.getActivityId())){
C 186                 activityRulePollingHandle1(orderInfo.getActivityId(),optType,ordersTotal,refundRecord,commonService);
187             }
188         }
189     }
528195 190
160c33 191     /**处理活动标识保存在订单里面的
C 192      * @param activityId 活动标识
193      * @param optType 操作类型:0退款执行,1退款作废
194      * @param ordersTotal 订单
195      * @param refundRecord 退款单
196      * @param commonService 映射
197      */
198     public static void activityRulePollingHandle1(String activityId,int optType,OrdersTotal ordersTotal,RefundRecord refundRecord,CommonService commonService){
199         SqlSentence sqlSentence = new SqlSentence();
200         Map<String,Object> values = new HashMap<>();
201
202         //查找活动规则,没有找到就跳出循环
203         ActivityRule activityRule = commonService.selectOneByKeyBlob(ActivityRuleMapper.class,activityId);
204         if(activityRule == null){
205             return;
528195 206         }
C 207
160c33 208         values.put("activityRuleId",activityRule.getId());
C 209         values.put("type", ActivityAction.TYPE_INTEGRAL);
210         values.put("type1",ActivityAction.TYPE_VALUEADDEDFUND);
211         values.put("type2",ActivityAction.TYPE_COUPON);
212         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",values);
213         List<ActivityAction> activityActions = commonService.selectList(ActivityActionMapper.class, sqlSentence);
214         if(activityActions!=null && activityActions.size()>0){
215             for (ActivityAction activityAction : activityActions) {
216                 if(ActivityAction.TYPE_INTEGRAL.equals(activityAction.getType())){
217                     //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
218                     if(new BigDecimal(activityAction.getWorth()).compareTo(BigDecimal.ZERO) == 0){
219                         continue;
220                     }
4ebb90 221                     if(optType == OPT_TYPE_EXECUTE){
C 222                         UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"订单:"+ordersTotal.getOrderNo()+" 申请退款,预扣除活动规则赠送储值金金额:"+activityAction.getWorth(),refundRecord.getOperatorId(),refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),new BigDecimal(activityAction.getWorth()).negate(), UserMoneyUnclaimed.FUND_TYPE_INTEGRAL,OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES);
223                     }else if(optType == OPT_TYPE_CANCEL){
224                         UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"订单:"+ordersTotal.getOrderNo()+" 作废退款,返还赠送储值金预扣除金额:"+activityAction.getWorth(),refundRecord.getOperatorId(),refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),new BigDecimal(activityAction.getWorth()), UserMoneyUnclaimed.FUND_TYPE_INTEGRAL,OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES);
160c33 225                     }else{
C 226                         throw new PlatTipsException(PlatformCode.ERROR_TIPS,"操作类型错误[2]["+optType+"]!");
227                     }
228                 }else if(ActivityAction.TYPE_VALUEADDEDFUND.equals(activityAction.getType())){
229                     //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
755975 230                     if(new BigDecimal(activityAction.getWorth()).compareTo(BigDecimal.ZERO) == 0 ){
160c33 231                         continue;
C 232                     }
4ebb90 233                     if(optType == OPT_TYPE_EXECUTE){
C 234                         UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"订单:"+ordersTotal.getOrderNo()+" 申请退款,预扣除活动规则赠送增值金金额:"+activityAction.getWorth(),refundRecord.getOperatorId(),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);
235                     }else if(optType == OPT_TYPE_CANCEL){
236                         UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"订单:"+ordersTotal.getOrderNo()+" 作废退款,返还赠送增值金预扣除金额:"+activityAction.getWorth(),refundRecord.getOperatorId(),refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),new BigDecimal(activityAction.getWorth()), UserMoneyUnclaimed.FUND_TYPE_VALUE_ADDED_FUND,OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES);
160c33 237                     }else{
C 238                         throw new PlatTipsException(PlatformCode.ERROR_TIPS,"操作类型错误[3]["+optType+"]!");
239                     }
240                 }else if(ActivityAction.TYPE_COUPON.equals(activityAction.getType())){
4ebb90 241                     values.put("oldValidState",optType==OPT_TYPE_EXECUTE? BaseEntity.YES:BaseEntity.NO);
C 242                     values.put("newValidState",optType==OPT_TYPE_EXECUTE?BaseEntity.NO:BaseEntity.YES);
160c33 243                     values.put("couponId",activityAction.getCrmCouponId());
C 244                     values.put("commonId",ordersTotal.getId());
245                     sqlSentence.sqlSentence(" validState=#{m.newValidState} where couponId=#{m.couponId} and commonId=#{m.commonId} and validState=#{m.oldValidState} ",values);
246                     commonService.updateWhere(CouponNumberMapper.class,sqlSentence);
247                 }
248             }
249         }
250     }
fd7281 251
160c33 252     /**处理活动保存在订单活动信息里面的
C 253      * @param ordersActivityInfoList 订单活动数据
254      * @param optType 操作类型:0退款退回,1作废退款
255      * @param ordersTotal 订单
256      * @param refundRecord 退款单
257      * @param commonService 映射
258      */
259     public static void activityRulePollingHandle2(List<OrdersActivityInfo> ordersActivityInfoList,int optType,OrdersTotal ordersTotal
260             ,RefundRecord refundRecord,CommonService commonService){
261         if(ordersActivityInfoList == null || ordersActivityInfoList.size()==0){
262             return;
263         }
264         SqlSentence sqlSentence = new SqlSentence();
265         Map<String,Object> values = new HashMap<>();
266
267         //获取订单活动条目
268         List<OrdersActivityInfoItem> ordersActivityInfoItemList;
269         for(OrdersActivityInfo ordersActivityInfo:ordersActivityInfoList){
270             values.clear();
271             values.put("activityInfoId",ordersActivityInfo.getId());
272             values.put("performStatus",OrdersActivityInfoItem.STATUS_PERFORM_SEND_OUT);
4ebb90 273             values.put("handleStatus",optType==OPT_TYPE_EXECUTE?OrdersActivityInfoItem.HANDLE_STATUS_SUCCESS:OrdersActivityInfoItem.HANDLE_STATUS_CANCEL);
160c33 274             sqlSentence.sqlSentence("SELECT * FROM orders_activity_info_item WHERE isDel = 0 AND activityInfoId = #{m.activityInfoId} AND performStatus = #{m.performStatus} AND handleStatus = #{m.handleStatus}",values);
C 275             ordersActivityInfoItemList = commonService.selectList(OrdersActivityInfoItemMapper.class,sqlSentence);
276             for(OrdersActivityInfoItem ordersActivityInfoItem:ordersActivityInfoItemList){
277                 if(ActivityAction.TYPE_INTEGRAL.equals(ordersActivityInfoItem.getType())){
278                     //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
279                     if(new BigDecimal(ordersActivityInfoItem.getWorth()).compareTo(BigDecimal.ZERO) == 0){
280                         continue;
281                     }
4ebb90 282                     if(optType == OPT_TYPE_EXECUTE){
C 283                         UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"订单:"+ordersTotal.getOrderNo()+" 申请退款,预扣除活动规则赠送储值金金额:"+ordersActivityInfoItem.getWorth(),refundRecord.getOperatorId(),refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),new BigDecimal(ordersActivityInfoItem.getWorth()).negate(), UserMoneyUnclaimed.FUND_TYPE_INTEGRAL,OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES);
284                     }else if(optType == OPT_TYPE_CANCEL){
285                         UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"订单:"+ordersTotal.getOrderNo()+" 作废退款,返还赠送储值金预扣除金额:"+ordersActivityInfoItem.getWorth(),refundRecord.getOperatorId(),refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),new BigDecimal(ordersActivityInfoItem.getWorth()), UserMoneyUnclaimed.FUND_TYPE_INTEGRAL,OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES);
160c33 286                     }else{
C 287                         throw new PlatTipsException(PlatformCode.ERROR_TIPS,"操作类型错误[2]["+optType+"]!");
288                     }
289                 }else if(ActivityAction.TYPE_VALUEADDEDFUND.equals(ordersActivityInfoItem.getType())){
290                     //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
755975 291                     if(new BigDecimal(ordersActivityInfoItem.getWorth()).compareTo(BigDecimal.ZERO) == 0 ){
160c33 292                         continue;
C 293                     }
4ebb90 294                     if(optType == OPT_TYPE_EXECUTE){
C 295                         UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"订单:"+ordersTotal.getOrderNo()+" 申请退款,预扣除活动规则赠送增值金金额:"+ordersActivityInfoItem.getWorth(),refundRecord.getOperatorId(),refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),new BigDecimal(ordersActivityInfoItem.getWorth()).negate(), UserMoneyUnclaimed.FUND_TYPE_VALUE_ADDED_FUND,OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES);
296                     }else if(optType == OPT_TYPE_CANCEL){
297                         UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"订单:"+ordersTotal.getOrderNo()+" 作废退款,返还赠送增值金预扣除金额:"+ordersActivityInfoItem.getWorth(),refundRecord.getOperatorId(),refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),new BigDecimal(ordersActivityInfoItem.getWorth()), UserMoneyUnclaimed.FUND_TYPE_VALUE_ADDED_FUND,OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES);
160c33 298                     }else{
C 299                         throw new PlatTipsException(PlatformCode.ERROR_TIPS,"操作类型错误[3]["+optType+"]!");
300                     }
301                 }else if(ActivityAction.TYPE_COUPON.equals(ordersActivityInfoItem.getType())){
4ebb90 302                     values.clear();
C 303                     values.put("oldValidState",optType==OPT_TYPE_EXECUTE? BaseEntity.YES:BaseEntity.NO);
304                     values.put("newValidState",optType==OPT_TYPE_EXECUTE?BaseEntity.NO:BaseEntity.YES);
160c33 305                     values.put("id",ordersActivityInfoItem.getCouponNumberId());
4ebb90 306                     sqlSentence.sqlSentence(" validState=#{m.newValidState} where id=#{m.id} AND validState = #{m.oldValidState} ",values);
160c33 307                     //优惠券不判断回退
C 308                     commonService.updateWhere(CouponNumberMapper.class,sqlSentence);
309                 }
310
4ebb90 311                 //更新订单活动信息状态
160c33 312                 values.clear();
4ebb90 313                 values.put("handleStatus",optType==OPT_TYPE_EXECUTE?OrdersActivityInfoItem.HANDLE_STATUS_CANCEL:OrdersActivityInfoItem.HANDLE_STATUS_SUCCESS);
C 314                 values.put("handleStatusOld",optType==OPT_TYPE_EXECUTE?OrdersActivityInfoItem.HANDLE_STATUS_SUCCESS:OrdersActivityInfoItem.HANDLE_STATUS_CANCEL);
160c33 315                 values.put("performStatusOld",OrdersActivityInfoItem.STATUS_PERFORM_SEND_OUT);
C 316                 values.put("id",ordersActivityInfoItem.getId());
317                 sqlSentence.sqlSentence("handleStatus = #{m.handleStatus} WHERE id = #{m.id} AND handleStatus = #{m.handleStatusOld} AND performStatus = #{m.performStatusOld}",values);
318                 if(commonService.updateWhere(OrdersActivityInfoItemMapper.class,sqlSentence) != 1){
319                     throw new PlatTipsException(PlatformCode.ERROR_TIPS,"活动条目状态变化错误:");
320                 }
321             }
322         }
323     }
324
325     /**
326      * 退款作废-处理优惠券
327      * @param ordersTotal 订单
328      * @param refundRecord 退款总数据
329      * @param commonService 映射
330      */
331     public static void handCoupon(OrdersTotal ordersTotal,RefundRecord refundRecord,CommonService commonService) {
332
333         //回退的优惠券变成使用状态
334         //如果该优惠券在别的订单使用了,那么就不再理会
335
336         SqlSentence sqlSentence = new SqlSentence();
337         Map<String, Object> values = new HashMap<>();
338
339         values.put("refundRecordId",refundRecord.getId());
4ebb90 340         sqlSentence.sqlSentence("select * from refund_record_coupon where refundRecordId = #{m.refundRecordId} and isDel=0 ",values);
160c33 341         List<RefundRecordCoupon> refundRecordCouponList=commonService.selectList(RefundRecordCouponMapper.class,sqlSentence);
C 342
343         CouponOrderDiscountLog couponOrderDiscountLog;
344         for (RefundRecordCoupon refundRecordCoupon : refundRecordCouponList) {
345
346             couponOrderDiscountLog=commonService.selectOneByKey(CouponOrderDiscountLogMapper.class,refundRecordCoupon.getCouponOrderId());
347             if(couponOrderDiscountLog==null){
348                 throw new PlatTipsException(PlatformCode.ERROR_TIPS,"订单优惠卷标识不正确");
349             }
350
351             //领建优惠券跳过回退
352             if(PartialRefundUtil.HIS_COUPON_CODE.equals(couponOrderDiscountLog.getCouponNumberId())){
353                 //变更订单优惠券记录状态
354                 values.clear();
355                 values.put("status", BaseEntity.NO);
356                 values.put("oldStatus", BaseEntity.YES);
357                 values.put("id", couponOrderDiscountLog.getId());
358                 sqlSentence.sqlSentence("status = #{m.status} WHERE id = #{m.id} AND status = #{m.oldStatus}",values);
359                 if(commonService.updateWhere(CouponOrderDiscountLogMapper.class,sqlSentence) != 1){
360                     throw new TipsException("优惠券回退失败!");
361                 }
362             }else{
363                 //优惠券状态变化
364                 values.clear();
365                 values.put("isUse", BaseEntity.YES);
366                 values.put("isUseOld", BaseEntity.NO);
367                 values.put("useTime", couponOrderDiscountLog.getCreateTime());
368                 values.put("useType", CouponNumber.USE_TYPE_USER);
369                 values.put("id", couponOrderDiscountLog.getCouponNumberId());
370                 sqlSentence.sqlSentence("  isUse=#{m.isUse},useTime=#{m.useTime},useType=#{m.useType},isUse=#{m.isUse} WHERE id = #{m.id} AND isUse = #{m.isUseOld}",values);
371                 if(commonService.updateWhere(CouponNumberMapper.class,sqlSentence) == 1){
372                     //变更订单优惠券记录状态
373                     values.clear();
374                     values.put("status", BaseEntity.NO);
375                     values.put("oldStatus", BaseEntity.YES);
376                     values.put("id", couponOrderDiscountLog.getId());
377                     sqlSentence.sqlSentence("status = #{m.status} WHERE id = #{m.id} AND status = #{m.oldStatus}",values);
378                     if(commonService.updateWhere(CouponOrderDiscountLogMapper.class,sqlSentence) != 1) {
379                         throw new TipsException("优惠券回退失败!");
fd7281 380                     }
C 381                 }
382             }
383         }
384     }
385
160c33 386     /**退款子项处理*/
C 387     public static void refundRecordItemHandle(OrdersTotal ordersTotal,RefundRecord refundRecord,CommonService commonService){
fd7281 388
160c33 389         SqlSentence sqlSentence = new SqlSentence();
C 390         Map<String,Object> values = new HashMap<>();
391
392         //获取退款子单
393         values.put("refundRecordId",refundRecord.getId());
394         sqlSentence.sqlSentence("SELECT * FROM refund_record_item WHERE isDel = 0 AND refundRecordId = #{m.refundRecordId}",values);
395         List<RefundRecordItem> refundRecordItems =commonService.selectList(RefundRecordItemMapper.class,sqlSentence);
396         if(refundRecordItems == null || refundRecordItems.size() == 0){
397             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到退款子订单信息");
398         }
399
400         for (RefundRecordItem refundRecordItem: refundRecordItems) {
401             //商品类型判断
402             switch (refundRecordItem.getType()){
403                 case OrderItemConstants.TYPE_RETAIL:
755975 404                     //商品/药品
160c33 405                     rerailHandleOne(refundRecordItem,commonService);
C 406                     break;
407                 case OrderItemConstants.TYPE_DRUG:
755975 408                     //处方单药品
160c33 409                     rerailHandleOne(refundRecordItem,commonService);
C 410                     break;
411                 case OrderItemConstants.TYPE_PROJECT:
755975 412                     //项目
160c33 413                     projectHandleOne(refundRecordItem,refundRecord,commonService);
C 414                     break;
415                 case OrderItemConstants.TYPE_PROMOTION:
755975 416                     //促销
160c33 417                     handRefundPromotion( refundRecordItem, refundRecord, commonService);
C 418                     break;
419                 case OrderItemConstants.TYPE_CARD:
755975 420                     //卡项
160c33 421                     cardHandleOne( refundRecord,  refundRecordItem, commonService);
C 422                     break;
423                 case OrderItemConstants.CARD_BAG:
755975 424                     //卡包
160c33 425                     handRefundPromotion( refundRecordItem, refundRecord, commonService);
C 426                     break;
427                 default:break;
428             }
429         }
430     }
431
432
433     /**一级订单-商品/药品处理*/
434     public static void rerailHandleOne(RefundRecordItem refundRecordItem,CommonService commonService){
435         //判断操作完了去修改子订单状态
436         OrderItem orderItem=commonService.selectOneByKey(OrderItemMapper.class,refundRecordItem.getOrderItemId());
437         if (orderItem==null){
438             throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"未找到子订单信息");
439         }
440
441         //剩余可退款数量
442         if(refundRecordItem.getRefundNum() > orderItem.getHasReNum()){
755975 443             throw new TipsException(orderItem.getGoodsName()+"["+orderItem.getGoodsNo()+"]"+"作废退款数量不能大于已退款数量!");
160c33 444         }
C 445
446         //变更退款子项信息
447         int refundStatus;
448         if (orderItem.getHasReNum().equals(refundRecordItem.getRefundNum())){
449             refundStatus = OrderTotalConstants.STATUS_REFUND_NONE;
450         }else{
451             refundStatus = OrderTotalConstants.STATUS_REFUND_PART;
452         }
453
454         //更新子订单信息
455         PartialRefundUtil.updateOrderItemOne(orderItem,refundStatus,refundRecordItem.getRealRefundTotal().negate(),-refundRecordItem.getRealRefundNum(), commonService);
456     }
457
458     /**一级订单-项目处理*/
459     public static void projectHandleOne(RefundRecordItem refundRecordItem,RefundRecord refundRecord,CommonService commonService){
460         //找到子单
461         OrderItem orderItem=commonService.selectOneByKey(OrderItemMapper.class,refundRecordItem.getOrderItemId());
462         if (orderItem==null){
463             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到子订单信息");
464         }
755975 465         //计算子单是否还有剩余的可扣疗程数
C 466         if(refundRecordItem.getRefundNum() > orderItem.getHasReNum()){
467             throw new PlatTipsException(PlatformCode.ERROR_TIPS,orderItem.getGoodsName()+"["+orderItem.getGoodsNo()+"]"+"退款次数大于可退次数[54]!");
468         }
160c33 469
C 470         //找到用户项目
01fbcb 471         UserProjectItem userProjectItem = PartialRefundUtil.getUserProject(orderItem.getId(),commonService);
160c33 472         if (userProjectItem == null) {
755975 473             throw new PlatTipsException(PlatformCode.ERROR_TIPS,orderItem.getGoodsName()+"["+orderItem.getGoodsNo()+"]"+"未找到用户子项项目信息");
160c33 474         }
C 475         if(userProjectItem.getCancelNum() < refundRecordItem.getRefundNum()){
755975 476             throw new PlatTipsException(PlatformCode.ERROR_TIPS,orderItem.getGoodsName()+"["+orderItem.getGoodsNo()+"]"+"作废退款次数大于用户项目已退次数!");
160c33 477         }
C 478
479         //用户项目操作
755975 480         UserProjectTool.userProjectDeduction(userProjectItem, UserProjectUsedCon.USED_METHOD_ORDER_REFUND,UserProjectUsedCon.USED_TYPE_RETURN,refundRecordItem.getDeductionTotalUser()
C 481                 ,refundRecordItem.getId(),refundRecordItem.getRealRefundNum(),refundRecord.getInvalidAppId(),refundRecord.getInvalidAppName(),refundRecord.getRefundShopId(),refundRecord.getRefundShopName(),"员工备注:"+refundRecord.getInvalidRemarks(),commonService);
160c33 482
C 483         int refundStatus;
484         if(refundRecordItem.getRefundNum().equals(orderItem.getHasReNum())){
485             refundStatus=OrderTotalConstants.STATUS_REFUND_NONE;
486         }else{
487             refundStatus=OrderTotalConstants.STATUS_REFUND_PART;
488         }
489
490         //更新子订单信息
491         PartialRefundUtil.updateOrderItemOne(orderItem,refundStatus,refundRecordItem.getRealRefundTotal().negate(),-refundRecordItem.getRealRefundNum(),commonService);
492     }
493
494     /**一级子单-处理促销*/
495     private static void handRefundPromotion(RefundRecordItem refundRecordItem, RefundRecord refundRecord, CommonService commonService) {
496
497         //查看订单信息
498         OrderItem orderItem = commonService.selectOneByKey(OrderItemMapper.class,refundRecordItem.getOrderItemId());
499         if(orderItem == null ){
500             throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"未找到子订单信息[84]!");
501         }
502
503         SqlSentence sqlSentence = new SqlSentence();
504         Map<String, Object> map = new HashMap<>();
505
506         //获取退款二级单
507         map.put("refundRecordItemId",refundRecordItem.getId());
508         sqlSentence.sqlSentence("select * from refund_record_item_source where isDel = 0 AND refundRecordItemId = #{m.refundRecordItemId}",map);
509         List<RefundRecordItemSource> sons = commonService.selectList(RefundRecordItemSourceMapper.class, sqlSentence);
510
511         for (RefundRecordItemSource son : sons) {
512             switch (GroupTypeEnum.getCode(son.getType())){
513                 case PROJECT:
514                     projectHandleTwo(refundRecord,refundRecordItem, son,orderItem.getUserCardId(),commonService);
515                     break;
516                 case RETAIL:
517                     rerailHandleTwo(refundRecord,refundRecordItem,son,orderItem.getUserCardId(),commonService);
518                     break;
519                 case INCREMENT:
520                     //增值金
521                     //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
522                     if(son.getRealRefundTotal().negate().compareTo(BigDecimal.ZERO)!=0){
523                         UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"作废退款:促销赠送增值金扣减",refundRecord.getInvalidId(),refundRecord.getOrderId(),refundRecord.getInvalidAppCode(),refundRecord.getId(),son.getRealRefundTotal(), UserMoneyUnclaimed.FUND_TYPE_VALUE_ADDED_FUND, OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES);
524                     }
525                     break;
526                 case STORED:
527                     //储值金额
528                     //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
529                     if(son.getRealRefundTotal().negate().compareTo(BigDecimal.ZERO)!=0){
530                         UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"作废退款:促销赠送储值金额扣减",refundRecord.getInvalidId(),refundRecord.getOrderId(),refundRecord.getInvalidAppCode(),refundRecord.getId(),son.getRealRefundTotal(), UserMoneyUnclaimed.FUND_TYPE_STORED_VALUE_FUND, OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.NO);
531                     }
532                     break;
533                 case INTEGRAL:
534                     //积分
535                     //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
536                     if(son.getRealRefundTotal().negate().compareTo(BigDecimal.ZERO)!=0){
537                         UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"作废退款:促销赠送积分扣减",refundRecord.getInvalidId(),refundRecord.getOrderId(),refundRecord.getInvalidAppCode(),refundRecord.getId(),son.getRealRefundTotal(), UserMoneyUnclaimed.FUND_TYPE_INTEGRAL,OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES);
538                     }
539                     break;
540             }
541
542         }
543
544         //获取其子项
545         map.put("orderItemId",orderItem.getId());
546         sqlSentence.sqlSentence("select refundStatus from order_item_source WHERE orderItemId=#{m.orderItemId} and isDel=0",map);
547         List<OrderItemSon> orderItemSonList=commonService.selectList(OrderItemSonMapper.class,sqlSentence);
548         int refundStatus;
549         Integer refundNum = 0;
550         List<Integer> collect = orderItemSonList.stream().map(OrderItemSon::getRefundStatus).collect(Collectors.toList());
551
552         if(collect.contains(OrderTotalConstants.STATUS_REFUND_PART)){
553             refundStatus = OrderTotalConstants.STATUS_REFUND_PART;
554         }else if (collect.contains(OrderTotalConstants.STATUS_REFUND_NONE)){
555             if(collect.contains(OrderTotalConstants.STATUS_REFUND_FINSH)){
556                 refundStatus = OrderTotalConstants.STATUS_REFUND_PART;
557             }else{
558                 refundStatus = OrderTotalConstants.STATUS_REFUND_NONE;
559             }
560         }else if (collect.contains(OrderTotalConstants.STATUS_REFUND_FINSH)){
561             refundStatus = OrderTotalConstants.STATUS_REFUND_FINSH;
562             refundNum = orderItem.getBuyNum();
563         }else {
564             refundStatus = OrderTotalConstants.STATUS_REFUND_NONE;
565         }
566
567         //更新子订单信息
568         PartialRefundUtil.updateOrderItemOne(orderItem,refundStatus,refundRecordItem.getRealRefundTotal().negate(),-refundNum,commonService);
569
570     }
571
572     /**二级子订单-项目类型
573      * @param refundRecord 退款总记录
574      * @param refundRecordItem 退款一级子单
575      * @param refundRecordItemSource  退款二级子单
576      * @param userCardId 用户卡包标识
577      * @param commonService 映射
578      */
579     private static void projectHandleTwo(RefundRecord refundRecord,RefundRecordItem refundRecordItem, RefundRecordItemSource refundRecordItemSource
580             ,String userCardId,CommonService commonService) {
581
582         //判断操作完了去修改子订单状态
583         OrderItemSon orderItemSon = commonService.selectOneByKey(OrderItemSonMapper.class,refundRecordItemSource.getOrderItemSonId());
584         if (orderItemSon==null){
585             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到二级子订单信息[02]");
586         }
755975 587         //计算子单是否还有剩余的可扣疗程数
C 588         if(refundRecordItemSource.getRefundNum() > orderItemSon.getHasReNum()){
589             throw new PlatTipsException(PlatformCode.ERROR_TIPS,orderItemSon.getGoodsName()+"["+orderItemSon.getGoodsNo()+"]"+"作废退款次数大于已退次数[56]!");
590         }
160c33 591
C 592         UserProjectItem userProjectItem = PartialRefundUtil.getUserProject(orderItemSon.getId(),commonService);
593         if(userProjectItem == null){
594             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"用户项目获取失败[06]!");
595         }
755975 596         //比较作废退款数量
160c33 597         if(refundRecordItemSource.getRefundNum() > userProjectItem.getCancelNum()){
755975 598             throw new PlatTipsException(PlatformCode.ERROR_TIPS,orderItemSon.getGoodsName()+"["+orderItemSon.getGoodsNo()+"]"+"作废退款次数大于用户项目已退次数[56]!");
160c33 599         }
C 600
601         //处理用户项目,减去用户项目数量
755975 602         UserProjectTool.userProjectDeduction(userProjectItem,UserProjectUsedCon.USED_METHOD_ORDER_REFUND,UserProjectUsedCon.USED_TYPE_RETURN,refundRecordItemSource.getDeductionTotalUser()
C 603                 ,refundRecordItemSource.getId(),refundRecordItemSource.getRealRefundNum(),refundRecord.getInvalidAppId(),refundRecord.getInvalidAppName(),refundRecord.getRefundShopId(),refundRecord.getRefundShopName(),"员工备注:"+refundRecord.getInvalidRemarks(),commonService);
160c33 604
C 605         int refundStatus;
755975 606         if(orderItemSon.getHasReNum().equals(refundRecordItemSource.getRefundNum())){
160c33 607             refundStatus=OrderTotalConstants.STATUS_REFUND_NONE;
C 608         }else{
609             refundStatus=OrderTotalConstants.STATUS_REFUND_PART;
610         }
611
612         //更新子单信息
755975 613         PartialRefundUtil.updateOrderItemTwo(orderItemSon,refundStatus,refundRecordItemSource.getRealRefundTotal().negate(),-refundRecordItemSource.getRealRefundNum(),commonService);
160c33 614
C 615         if(refundRecordItem.getType().equals(OrderItemConstants.CARD_BAG)){
755975 616             //是卡包的,找回卡包使用记录
160c33 617             userCardUsedHandle(userCardId,orderItemSon.getCardItemInfoId(),orderItemSon.getId(),orderItemSon.getOrderId(),refundRecordItemSource.getRefundNum(),commonService);
C 618         }
619
620     }
621
622     /**二级订单-商品/药品
623      * @param refundRecord 退款总记录
624      * @param refundRecordItem 退款一级子单
625      * @param refundRecordItemSource  退款二级子单
626      * @param commonService 映射
627      * @return 全局携带参数结构
628      */
629     private static void rerailHandleTwo(RefundRecord refundRecord,RefundRecordItem refundRecordItem, RefundRecordItemSource refundRecordItemSource
630             ,String userCardId,CommonService commonService) {
631
632         //判断操作完了去修改子订单状态
633         OrderItemSon orderItemSon=commonService.selectOneByKey(OrderItemSonMapper.class,refundRecordItemSource.getOrderItemSonId());
634         if (orderItemSon==null){
635             throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"未找到二级子订单信息");
636         }
637
638         //计算子单是否还有剩余的可扣疗程数
639         if(refundRecordItemSource.getRefundNum() > orderItemSon.getHasReNum()){
640             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"作废退款次数大于已退次数[84]!");
641         }
642
643         int refundStatus;
644         if(orderItemSon.getHasReNum().equals(refundRecordItemSource.getRefundNum())){
645             refundStatus=OrderTotalConstants.STATUS_REFUND_NONE;
646         }else{
647             refundStatus=OrderTotalConstants.STATUS_REFUND_PART;
648         }
649
650         PartialRefundUtil.updateOrderItemTwo(orderItemSon,refundStatus,refundRecordItemSource.getRealRefundTotal().negate(),-refundRecordItemSource.getRealRefundNum(),commonService);
651
652         if(refundRecordItem.getType().equals(OrderItemConstants.CARD_BAG)){
653             //是卡包的,刪除卡包使用
654             userCardUsedHandle(userCardId,orderItemSon.getCardItemInfoId(),orderItemSon.getId(),orderItemSon.getOrderId(),refundRecordItemSource.getRefundNum(),commonService);
655         }
656
657     }
658
659     /**卡包使用记录-状态变化
660      * @param userCardId 用户卡包
661      * @param cardItemInfoId 卡包的卡项子项
662      * @param sourceId 卡包使用记录来源标识
663      * @param orderId 卡包使用记录来源总表标识
664      * @param refundNum 退款数量
665      * @param commonService 映射
666      */
667     public static void userCardUsedHandle(String userCardId,String cardItemInfoId,String sourceId,String orderId,Integer refundNum
668             ,CommonService commonService){
669
670         //查出用户,commonId:卡项的组合项标识
755975 671         CardItemInfo cardItemInfo = commonService.selectOneByKey(CardItemInfoMapper.class,cardItemInfoId);
C 672         if(cardItemInfo == null){
160c33 673             logger.error("未找到该卡包的组合项-卡包作废退款失败,userCardId:{},cardItemInfoId:{},sourceId:{},orderId:{},refundNum:{}",userCardId,cardItemInfoId,sourceId,orderId,refundNum);
C 674             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到该卡包的组合项");
675         }
676
4ebb90 677         UserCard userCard = commonService.selectOneByKey(UserCardMapper.class,userCardId);
C 678         if(userCard == null){
679             logger.error("未找到该卡包-卡包作废退款失败,userCardId:{},cardItemInfoId:{},sourceId:{},orderId:{},refundNum:{}",userCardId,cardItemInfoId,sourceId,orderId,refundNum);
680             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到该卡包信息");
681         }
682
160c33 683         //获取权益类型
C 684         CardEquity cardEquity = commonService.selectOneByKey(CardEquityMapper.class,cardItemInfo.getCardEquityId());
685         if(cardEquity == null){
686             logger.error("未找到该卡包的组合项权益类型-卡包作废退款失败,userCardId:{},cardItemInfoId:{},sourceId:{},orderId:{},refundNum:{}",userCardId,cardItemInfoId,sourceId,orderId,refundNum);
687             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到该卡包的组合项权益类型");
688         }
689
690         SqlSentence sqlSentence = new SqlSentence();
691         Map<String,Object> map = new HashMap<>();
692
693         //获取作废使用记录
694         map.put("cardItemInfoId",cardItemInfoId);
695         map.put("sourceId",sourceId);
696         map.put("userCardId",userCardId);
697         sqlSentence.sqlSentence(" SELECT * FROM user_card_used WHERE isDel = 1 AND userCardId = #{m.userCardId}" +
698                 " AND cardItemInfoId = #{m.cardItemInfoId} AND sourceId = #{m.sourceId}",map);
699         List<UserCardUsed> userCardUsedList = commonService.selectList(UserCardUsedMapper.class,sqlSentence);
700
701         //因为之前的使用记录有些没有关联到订单,所以找不到
702         if(userCardUsedList == null || userCardUsedList.size() == 0){
703             map.clear();
704             map.put("cardItemInfoId",cardItemInfoId);
705             map.put("userCardId",userCardId);
706             map.put("orderId",orderId);
707             sqlSentence.sqlSentence(" SELECT * FROM user_card_used WHERE isDel = 1 AND userCardId = #{m.userCardId}" +
708                     " AND cardItemInfoId = #{m.cardItemInfoId} AND orderId = #{m.orderId} AND sourceId IS NULL",map);
709             userCardUsedList = commonService.selectList(UserCardUsedMapper.class,sqlSentence);
710             if(userCardUsedList.size() == 0){
711                 sqlSentence.sqlSentence(" SELECT * FROM user_card_used WHERE isDel = 1 AND userCardId = #{m.userCardId}" +
712                         " AND cardItemInfoId = #{m.cardItemInfoId} AND sourceId IS NULL",map);
713                 userCardUsedList = commonService.selectList(UserCardUsedMapper.class,sqlSentence);
714             }
715         }
716
4ebb90 717         //计算当前数量要扣的卡次
C 718         int returnNum = UserCardTool.countUsedNumber(refundNum,cardItemInfo,cardEquity);
719         //获取可扣卡次
720         UserCardItemInfoVo userCardItemInfoVo = UserCardTool.getSurplusNumber(userCard,cardItemInfo,cardEquity,commonService);
721         if(!userCardItemInfoVo.isNumByNum()){
722             throw new TipsException("作废退款-卡包扣减失败,不符合N选M规则!");
723         }
724         if(returnNum > userCardItemInfoVo.getSurplusNum()){
725             throw new TipsException("作废退款-卡包扣减失败,抵扣次数不足!");
160c33 726         }
C 727
728         if(returnNum > userCardUsedList.size()){
729             logger.error("卡包可退数量错误-卡包作废退款失败,查询语句:{},传入参数:{}",sqlSentence.getSqlSentence(), JSON.toJSONString(sqlSentence.getM()));
730             logger.error("卡包可退数量错误-卡包作废退款失败,userCardId:{},cardItemInfoId:{},sourceId:{},orderId:{},refundNum:{},returnNum:{}",userCardId,cardItemInfoId,sourceId,orderId,refundNum,returnNum);
755975 731             throw new TipsException("卡包作废退款次数量大于可退次数!");
160c33 732         }
C 733
734         //已经操作数量
4ebb90 735         int opNum = returnNum;
160c33 736         StringBuilder sql;
C 737         for(UserCardUsed userCardUsed:userCardUsedList){
738             //判断是否够了,跳出循环
739             if(opNum == returnNum){
740                 break;
741             }
4ebb90 742             if(userCardUsed.getCancelNum() <= 0){
C 743                 continue;
744             }
160c33 745             sql = new StringBuilder();
C 746             map.clear();
747             map.put("id",userCardUsed.getId());
4ebb90 748             map.put("cancelNumOld",userCardUsed.getCancelNum());
C 749             if(opNum >= userCardUsed.getCancelNum()){
750                 map.put("isDel",UserCardUsed.NO);
751                 map.put("countNum",userCardUsed.getCancelNum());
752                 opNum = opNum - userCardUsed.getCancelNum();
753             }else{
754                 map.put("isDel",UserCardUsed.YES);
755                 map.put("countNum",opNum);
756                 opNum = 0;
757             }
758             sql.append("isDel = #{m.isDel}");
759             sql.append(",usedNum =  usedNum + #{m.countNum}");
760             sql.append(",cancelNum =  cancelNum - #{m.countNum}");
160c33 761             if(StringUtils.isEmpty(userCardUsed.getOrderId())){
C 762                 sql.append(",orderId = #{m.orderId}");
763                 map.put("orderId",orderId);
764             }
765             if(StringUtils.isEmpty(userCardUsed.getSourceId())){
766                 sql.append(",sourceId = #{m.sourceId}");
767                 map.put("sourceId",sourceId);
768             }
4ebb90 769             sql.append(" WHERE id = #{m.id} AND cancelNum = #{m.cancelNumOld}");
160c33 770             sqlSentence.sqlUpdate(sql.toString(),map);
4ebb90 771             if(commonService.updateWhere(UserCardUsedMapper.class,sqlSentence) != 1){
C 772                 throw new TipsException("卡包使用记录已发生变化,请重试!");
773             }
160c33 774         }
4ebb90 775         if(opNum != 0){
160c33 776             logger.error("卡包回退数量错误-卡包作废退款失败,userCardId:{},cardItemInfoId:{},sourceId:{},orderId:{},refundNum:{},returnNum:{},opNum:{}",userCardId,cardItemInfoId,sourceId,orderId,refundNum,returnNum,opNum);
C 777             throw new TipsException("卡包作废回退数量错误!");
778         }
779
780     }
781
782     /**一级子单-处理卡项
783      * @param refundRecord 退款总记录
784      * @param refundRecordItem 退款一级记录
785      * @param commonService 映射
786      * @return 全局携带参数结构
787      */
788     public static void cardHandleOne(RefundRecord refundRecord, RefundRecordItem refundRecordItem,CommonService commonService) {
789
790         //判断操作完了去修改子订单状态
791         OrderItem orderItem=commonService.selectOneByKey(OrderItemMapper.class,refundRecordItem.getOrderItemId());
792         if (orderItem==null){
793             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到子订单信息");
794         }
795
796         if(refundRecordItem.getRefundNum() > orderItem.getHasReNum()){
797             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"作废退款-卡包提示:作废卡包退款数量大于已退款数量[020]");
798         }
799
800         SqlSentence sqlSentence = new SqlSentence();
801         Map<String,Object> values = new HashMap<>();
802
803
804         //找到已退款卡包
805         values.put("status",RefundRecordCard.YES);
806         values.put("refundRecordItemId",refundRecordItem.getId());
807         sqlSentence.sqlSentence("SELECT * FROM refund_record_card WHERE isDel = 0 AND status = #{m.status} AND refundRecordItemId = #{m.refundRecordItemId}",values);
808         List<RefundRecordCard> refundRecordCardList = commonService.selectList(RefundRecordCardMapper.class,sqlSentence);
809         if(refundRecordItem.getRefundNum() > refundRecordCardList.size()){
810             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"作废退款-卡包提示:作废卡包退款数量大于已退款数量[021]");
811         }
812
813         //变更卡项状态
814         RefundRecordCard refundRecordCard;
815         for (int i = 0; i < refundRecordItem.getRefundNum(); i++) {
816             refundRecordCard= refundRecordCardList.get(i);
817             values.clear();
818             values.put("id",refundRecordCard.getUserCardId());
819             values.put("statusOld", UserCard.TYPE_NO_USED);
820             values.put("effectiveStatus", UserProjectConstants.EFF_STATUS_YES);
821             values.put("oldEffectiveStatus", UserProjectConstants.EFF_STATUS_CANCEL);
822             sqlSentence.sqlSentence(" effectiveStatus=#{m.effectiveStatus} WHERE id = #{m.id} AND effectiveStatus = #{m.oldEffectiveStatus} AND status = #{m.statusOld}",values);
823             if(commonService.updateWhere(UserCardMapper.class,sqlSentence) != 1){
824                 throw new TipsException("用户卡包状态已发生变化,请重试!");
825             }
826
827             values.clear();
828             values.put("id",refundRecordCard.getId());
829             values.put("status",RefundRecordCard.NO);
830             values.put("statusOld",RefundRecordCard.YES);
831             sqlSentence.sqlSentence("status = #{m.status} WHERE id = #{m.id} AND status = #{m.statusOld}",values);
832             if(commonService.updateWhere(RefundRecordCardMapper.class,sqlSentence) != 1){
833                 throw new TipsException("退款卡包状态已发生变化,请重试!");
834             }
835         }
836
837         int refundStatus;
838         if (orderItem.getHasReNum().equals(refundRecordItem.getRefundNum())){
839             refundStatus =  OrderTotalConstants.STATUS_REFUND_NONE;
840         }else {
841             refundStatus =  OrderTotalConstants.STATUS_REFUND_PART;
842
843         }
844         //更新子订单信息
845         PartialRefundUtil.updateOrderItemOne(orderItem,refundStatus,refundRecordItem.getRealRefundTotal().negate(),-refundRecordItem.getRealRefundNum(),commonService);
846     }
fd7281 847
C 848 }