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