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