package com.hx.phip.util.api; import com.alibaba.fastjson.JSON; import com.hx.common.service.CommonService; import com.hx.mybatisTool.SqlSentence; import com.hx.phiappt.common.*; import com.hx.phiappt.constants.enums.GroupTypeEnum; import com.hx.phiappt.constants.tool.RefundToolUtil; import com.hx.phiappt.constants.tool.SqlSplicingTool; import com.hx.phiappt.model.BaseEntity; import com.hx.phiappt.model.UserMoney; import com.hx.phiappt.model.activity.ActivityAction; import com.hx.phiappt.model.activity.ActivityRule; import com.hx.phiappt.model.consume.ConsumePay; import com.hx.phiappt.model.coupon.CouponNumber; import com.hx.phiappt.model.coupon.CouponOrderDiscountLog; import com.hx.phiappt.model.order.*; import com.hx.phiappt.model.refund.*; import com.hx.phiappt.model.user.UserCard; import com.hx.phiappt.model.user.UserCardUsed; import com.hx.phiappt.model.user.UserProject; import com.hx.phiappt.model.user.UserProjectItem; import com.hx.phiappt.model.userMoney.UserMoneyUnclaimed; import com.hx.phip.config.CustomParameter; import com.hx.phip.dao.mapper.*; import com.hx.util.StringUtils; import com.platform.exception.PlatTipsException; import com.platform.resultTool.PlatformCode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.math.BigDecimal; import java.util.*; import java.util.stream.Collectors; /** * @Author */ public class PartialRefundUtil { private static final Logger logger = LoggerFactory.getLogger(PartialRefundUtil.class); /** * 退款总流程工具 * @param commonService * @param operationId * @param operationNme * @param refundId * @param * @param appIdCode */ public static OrdersTotal refundProcess(CommonService commonService, String operationId, String operationNme, String refundId, CustomParameter customParameter, String appIdCode) { SqlSentence sqlSentence = new SqlSentence(); Map map=new HashMap<>(); sqlSentence.setM(map); //退款成功 map.put("refundStatus", RefundStatus.STATUS_APPLY_REFUND); map.put("oldRefundStatus", RefundStatus.STATUS_APPLY_REFUND); map.put("refundId", refundId); sqlSentence.setSqlSentence("select * from refund_record where isDel=0 and refundStatus=#{m.refundStatus} and id=#{m.refundId}"); RefundRecord refundRecord =commonService.selectOne(RefundRecordMapper.class,sqlSentence); if(refundRecord ==null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"找不到该退款信息!"); } StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("select name,actualTotal,refundRecordId,refundNumberNo as numberNo,id,consumePayId from refund_record_method WHERE refundRecordId in ("); SqlSplicingTool.selectIn(stringBuffer, Arrays.asList((String) map.get("refundId"))); sqlSentence.setSqlSentence(stringBuffer.toString()); List> refundMethodList = commonService.selectListMap(RefundRecordMethodMapper.class,sqlSentence); if(refundMethodList==null && refundMethodList.size()==0){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"没有找到该退款订单对应的支付方式"); } //查看订单信息 OrdersTotal ordersTotal=commonService.selectOneByKeyBlob(OrdersTotalMapper.class,refundRecord.getOrderId()); if(ordersTotal==null ){ throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"未找到订单信息!"); } //查看订单其他信息 map.put("orderId", ordersTotal.getId()); sqlSentence.setSqlSentence("select * from orders_info where isDel=0 and orderId=#{m.orderId}"); OrderInfo orderInfo=commonService.selectOne(OrderInfoMapper.class,sqlSentence); //查询用户是否有账户信息,退款需要处理资金 map.put("userId",refundRecord.getUserId()); sqlSentence.setSqlSentence("select * from user_money WHERE userId=#{m.userId} and isDel=0"); UserMoney userMoney=commonService.selectOne(UserMoneyMapper.class,sqlSentence); if(userMoney==null){ throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"未找到该用户的资金信息"); } //处理优惠券 handCoupon(commonService, sqlSentence, map, refundRecord,ordersTotal); if(OrderTotalConstants.TYPE_RECHARGE.equals(ordersTotal.getType())){ //充值订单需要把充值的储值金 拿回来 因为储值金需要开单来处理,所以不需要增加日志 //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0 if(ordersTotal.getTotal().compareTo(BigDecimal.ZERO)!=0){ UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"审核通过退款:支付方式储值金额退回",operationId,refundRecord.getOrderId(),appIdCode,refundRecord.getId(),new BigDecimal(String.valueOf(ordersTotal.getTotal())).negate(), UserMoneyUnclaimed.FUND_TYPE_STORED_VALUE_FUND, OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.NO); OrderLog orderLog = RefundToolUtil.setOrderLog(refundRecord, operationId, operationNme, "储值金客户退回金额:"+ordersTotal.getTotal(), 0, OrderLogConstants.LOG_TYPE_REFUND); commonService.insert(OrderLogMapper.class,orderLog); //活动规则退款处理 handActivityRule(commonService, operationId, operationNme, sqlSentence, map, refundRecord,ordersTotal,orderInfo); } }else if(OrderTotalConstants.TYPE_CARD_BAG.equals(ordersTotal.getType())){ //卡包处理 numberOfRefunds(commonService, operationId, operationNme, sqlSentence, map, refundRecord,ordersTotal,customParameter); } else { //普通订单处理 if(refundRecord.getRefundType() ==0){ numberOfRefunds(commonService, operationId, operationNme, sqlSentence, map, refundRecord,ordersTotal,customParameter); } } //增加增值金 boolean cashStatus=false;//判断是否有现金或者其他现金支付 ConsumePay consumePay; for(Map methodMap:refundMethodList){ BigDecimal actualTotal=(BigDecimal) methodMap.get("actualTotal"); String name=(String) methodMap.get("name"); String id=(String) methodMap.get("id"); String consumePayId=(String) methodMap.get("consumePayId"); if(PayMethodTypeConstants.PAY_STORED.equals(methodMap.get("numberNo"))){ //储值金额  //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0 if(actualTotal.compareTo(BigDecimal.ZERO)!=0){ UserMoneyUtil.setNewUserMoneyUnclaimed(ordersTotal.getPayUserId()==null?ordersTotal.getUserId():ordersTotal.getPayUserId(),refundRecord.getRemarks(),"审核通过退款:支付方式储值金额退回",operationId,refundRecord.getOrderId(),appIdCode,refundRecord.getId(),actualTotal, UserMoneyUnclaimed.FUND_TYPE_STORED_VALUE_FUND, OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.NO); OrderLog orderLog = RefundToolUtil.setOrderLog(refundRecord, operationId, operationNme, name+"退款金额:"+actualTotal, 0, OrderLogConstants.LOG_TYPE_REFUND); commonService.insert(OrderLogMapper.class,orderLog); } }else if(PayMethodTypeConstants.PAY_ADD_FUND.equals(methodMap.get("numberNo"))){ //增值金 //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0 if(actualTotal.compareTo(BigDecimal.ZERO)!=0){ UserMoneyUtil.setNewUserMoneyUnclaimed(ordersTotal.getPayUserId()==null?ordersTotal.getUserId():ordersTotal.getPayUserId(),refundRecord.getRemarks(),"审核通过退款:支付方式增值金退回",operationId,refundRecord.getOrderId(),appIdCode,refundRecord.getId(),actualTotal, UserMoneyUnclaimed.FUND_TYPE_VALUE_ADDED_FUND, OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES); OrderLog orderLog = RefundToolUtil.setOrderLog(refundRecord,operationId,operationNme,name,0, OrderLogConstants.LOG_TYPE_REFUND); commonService.insert(OrderLogMapper.class,orderLog); } }else if(PayMethodTypeConstants.PAY_INTEGRAL.equals(methodMap.get("numberNo"))){ //积分 //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0 if(actualTotal.compareTo(BigDecimal.ZERO)!=0){ UserMoneyUtil.setNewUserMoneyUnclaimed(ordersTotal.getPayUserId()==null?ordersTotal.getUserId():ordersTotal.getPayUserId(),refundRecord.getRemarks(),"审核通过退款:支付方式积分退回",operationId,refundRecord.getOrderId(),appIdCode,refundRecord.getId(),actualTotal, UserMoneyUnclaimed.FUND_TYPE_INTEGRAL,OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES); OrderLog orderLog = RefundToolUtil.setOrderLog(refundRecord,operationId,operationNme,name+"退款金额:"+actualTotal,0, OrderLogConstants.LOG_TYPE_REFUND); commonService.insert(OrderLogMapper.class,orderLog); } }else { cashStatus=true; //现金支付 if(actualTotal.compareTo(BigDecimal.ZERO)<1){ continue; } OrderLog orderLog = RefundToolUtil.setOrderLog(refundRecord,operationId,operationNme,name+"退款金额:"+actualTotal,0, OrderLogConstants.LOG_TYPE_REFUND); orderLog.setOrderId(ordersTotal.getId()); commonService.insert(OrderLogMapper.class,orderLog); //现金支付需要创建创建退款单进行退款 RefundNote refundNote=new RefundNote(actualTotal, (String) methodMap.get("numberNo"),name,ordersTotal.getId(),refundRecord.getUserId()); refundNote.setRefundRecordId(refundRecord.getId()); commonService.insert(RefundNoteMapper.class,refundNote); } //修改实际退款金额 sqlSentence.setSqlSentence(" realRefundTotal=#{m.realRefundTotal} where id=#{m.refundMethodId}"); map.put("realRefundTotal",actualTotal); map.put("refundMethodId",id); commonService.updateWhere(RefundRecordMethodMapper.class,sqlSentence); //修改支付方式已退款金额 consumePay=commonService.selectOneByKey(ConsumePayMapper.class,consumePayId); consumePay.setRefundTotal(consumePay.getRefundTotal().add(actualTotal)); commonService.updateAll(ConsumePayMapper.class,consumePay); } //退款总订单 map.put("refundStatus", RefundStatus.STATUS_SUCC_REFUND); map.put("oldRefundStatus", RefundStatus.STATUS_APPLY_REFUND); map.put("editTime", new Date()); map.put("refundId", refundId); map.put("isDel", BaseEntity.NO); sqlSentence.setSqlSentence(" refundStatus=#{m.refundStatus},editTime=#{m.editTime} where id=#{m.refundId} and isDel=#{m.isDel} and refundStatus=#{m.oldRefundStatus}"); commonService.updateWhere(RefundRecordMapper.class,sqlSentence); //更改总订单退款状态 map.put("orderId",ordersTotal.getId()); sqlSentence.setSqlSentence("select * from order_item WHERE orderId=#{m.orderId} and isDel=0"); List orderItemList=commonService.selectList(OrderItemMapper.class,sqlSentence); List collect = orderItemList.stream().map(o -> o.getRefundStatus()).collect(Collectors.toList()); if(collect.contains(OrderTotalConstants.STATUS_REFUND_PART)){ ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_PART); ordersTotal.setReTotal(orderItemList.stream().map(OrderItem::getReTotal).reduce(BigDecimal.ZERO,BigDecimal::add)); }else if (collect.contains(OrderTotalConstants.STATUS_REFUND_NONE) && collect.contains(OrderTotalConstants.STATUS_REFUND_FINSH)){ ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_PART); ordersTotal.setReTotal(orderItemList.stream().map(OrderItem::getReTotal).reduce(BigDecimal.ZERO,BigDecimal::add)); }else if (collect.contains(OrderTotalConstants.STATUS_REFUND_NONE)){ ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_NONE); }else { ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_FINSH); ordersTotal.setStatus(OrderTotalConstants.STATUS_CANCEL); ordersTotal.setReTotal(orderItemList.stream().map(OrderItem::getReTotal).reduce(BigDecimal.ZERO,BigDecimal::add)); } ordersTotal.setIsSyncOrder(BaseEntity.NO); ordersTotal.setEditTime(new Date()); commonService.updateAll(OrdersTotalMapper.class,ordersTotal); return ordersTotal; } /** * 退款-处理优惠券 * @param commonService * @param sqlSentence * @param map * @param refundRecord */ public static void handCoupon(CommonService commonService, SqlSentence sqlSentence, Map map, RefundRecord refundRecord,OrdersTotal ordersTotal) { map.put("refundRecordId",refundRecord.getId()); sqlSentence.setSqlSentence("select * from refund_record_coupon where refundRecordId=#{m.refundRecordId} and isDel=0 "); List refundRecordCouponList=commonService.selectList(RefundRecordCouponMapper.class,sqlSentence); //退款成功 map.put("status", BaseEntity.YES); CouponOrderDiscountLog couponOrderDiscountLog=null; for (RefundRecordCoupon refundRecordCoupon : refundRecordCouponList) { sqlSentence.setSqlSentence(" status=#{m.status} WHERE id = #{m.id} "); map.put("id", refundRecordCoupon.getCouponOrderId()); couponOrderDiscountLog=commonService.selectOneByKey(CouponOrderDiscountLogMapper.class,refundRecordCoupon.getCouponOrderId()); if(couponOrderDiscountLog==null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"订单优惠卷id不正确"); } commonService.updateWhere(CouponOrderDiscountLogMapper.class,sqlSentence); if("his_coupon_code".equals(couponOrderDiscountLog.getCouponNumberId())){ continue; } map.put("isUse", BaseEntity.NO); map.put("useTime", null); map.put("useType", CouponNumber.USE_TYPE_UNKNOW); map.put("id", couponOrderDiscountLog.getCouponNumberId()); sqlSentence.setSqlSentence(" isUse=#{m.isUse},useTime=#{m.useTime},useType=#{m.useType},isUse=#{m.isUse} WHERE id = #{m.id} "); commonService.updateWhere(CouponNumberMapper.class,sqlSentence); } } /** * 退款-处理活动规则增值金和积分 * @param commonService * @param operationId * @param operationNme * @param sqlSentence * @param map * @param refundRecord * @param ordersTotal */ private static void handActivityRule(CommonService commonService, String operationId, String operationNme, SqlSentence sqlSentence, Map map, RefundRecord refundRecord, OrdersTotal ordersTotal, OrderInfo orderInfo) { if(orderInfo!=null && StringUtils.noNull(orderInfo.getActivityId())){ ActivityRule activityRule=commonService.selectOneByKeyBlob(ActivityRuleMapper.class,orderInfo.getActivityId()); if(activityRule!=null){ map.put("activityRuleId",activityRule.getId()); map.put("type", ActivityAction.TYPE_INTEGRAL); map.put("type1",ActivityAction.TYPE_VALUEADDEDFUND); map.put("type2",ActivityAction.TYPE_COUPON); sqlSentence.setSqlSentence("select * from activity_action where activityRuleId=#{m.activityRuleId} and (type=#{m.type} or type=#{m.type1} or type=#{m.type2}) and isDel=0"); List activityActions = commonService.selectList(ActivityActionMapper.class, sqlSentence); if(activityActions!=null && activityActions.size()>0){ for (ActivityAction activityAction : activityActions) { if(ActivityAction.TYPE_INTEGRAL.equals(activityAction.getType())){ //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0 if(new BigDecimal(activityAction.getWorth()).negate().compareTo(BigDecimal.ZERO)!=0){ UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"退款扣减活动规则赠送积分",operationId,refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),new BigDecimal(activityAction.getWorth()).negate(), UserMoneyUnclaimed.FUND_TYPE_INTEGRAL,OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES); } }else if(ActivityAction.TYPE_VALUEADDEDFUND.equals(activityAction.getType())){ //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0 if(new BigDecimal(activityAction.getWorth()).negate().compareTo(BigDecimal.ZERO)!=0){ UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"退款扣减活动规则赠送增值金",operationId,refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),new BigDecimal(activityAction.getWorth()).negate(), UserMoneyUnclaimed.FUND_TYPE_VALUE_ADDED_FUND,OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES); } }else if(ActivityAction.TYPE_COUPON.equals(activityAction.getType())){ map.put("oldValidState",BaseEntity.YES); map.put("newValidState",BaseEntity.NO); map.put("couponId",activityAction.getCrmCouponId()); map.put("commonId",ordersTotal.getId()); sqlSentence.setSqlSentence(" validState=#{m.newValidState} where couponId=#{m.couponId} and commonId=#{m.commonId} and validState=#{m.oldValidState} "); commonService.updateWhere(CouponNumberMapper.class,sqlSentence); } } } } } } /** * 退款-处理普通订单信息(比如:项目、促销、卡项) * @param commonService * @param operationId 操作人标识 * @param operationNme 操作人名称 * @param sqlSentence * @param map * @param refundRecord */ public static void numberOfRefunds(CommonService commonService, String operationId, String operationNme, SqlSentence sqlSentence, Map map, RefundRecord refundRecord,OrdersTotal ordersTotal,CustomParameter customParameter) { map.put("refundId",map.get("refundId")); sqlSentence.setSqlSentence("select * from refund_record_item where isDel=0 and refundRecordId=#{m.refundId}"); List refundRecordItems =commonService.selectList(RefundRecordItemMapper.class,sqlSentence); if(refundRecordItems==null && refundRecordItems.size()==0){ throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"未找到退款子订单信息"); } for (RefundRecordItem v: refundRecordItems) { switch (v.getType()){ case OrderItemConstants.TYPE_RETAIL: handRefundRerail(commonService, sqlSentence, map, refundRecord, v); break; case OrderItemConstants.TYPE_DRUG: handRefundRerail(commonService, sqlSentence, map, refundRecord, v); break; case OrderItemConstants.TYPE_PROJECT: handRefundNoExecution(commonService, operationId, operationNme, sqlSentence, map, refundRecord, v); break; case OrderItemConstants.TYPE_PROMOTION: handRefundPromotion(commonService, operationId, operationNme, sqlSentence, map, refundRecord, ordersTotal, v); break; case OrderItemConstants.TYPE_CARD: handRefundCard(commonService, operationId, sqlSentence, map, refundRecord, ordersTotal, v); break; case OrderItemConstants.CARD_BAG: handCardBag(commonService, operationId, operationNme, sqlSentence, map, refundRecord,ordersTotal,v,customParameter); break; default:break; } } } /** * 退款一级是商品 * @param commonService * @param sqlSentence * @param map * @param refundRecord * @param v */ private static void handRefundRerail(CommonService commonService,SqlSentence sqlSentence, Map map, RefundRecord refundRecord, RefundRecordItem v) { //判断操作完了去修改子订单状态 OrderItem orderItem=commonService.selectOneByKey(OrderItemMapper.class,v.getOrderItemId()); if (orderItem==null){ throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"未找到子订单信息"); } map.put("isDel", BaseEntity.NO); map.put("orderItemId", v.getOrderItemId()); map.put("reTotal",orderItem.getReTotal().add(v.getRefundMoney())); Integer hasReNum=orderItem.getHasReNum()+v.getRefundNum(); map.put("hasReNum",hasReNum); if (hasReNum==orderItem.getBuyNum()){ map.put("refundStatus", OrderTotalConstants.STATUS_REFUND_FINSH); }else { map.put("refundStatus", OrderTotalConstants.STATUS_REFUND_PART); } sqlSentence.setSqlSentence(" refundStatus=#{m.refundStatus}, reTotal=#{m.reTotal}, hasReNum=#{m.hasReNum} where isDel=#{m.isDel} AND id=#{m.orderItemId} "); commonService.updateWhere(OrderItemMapper.class,sqlSentence); } /** * 退款-处理未执行划扣 项目类型 * @param commonService * @param operationId 操作人 * @param operationNme 操作人名称 * @param sqlSentence * @param map * @param refundRecord * @param v */ private static void handRefundNoExecution(CommonService commonService, String operationId, String operationNme, SqlSentence sqlSentence, Map map, RefundRecord refundRecord, RefundRecordItem v) { sqlSentence.setSqlSentence("select * from user_project_item where isDel=0 and commonId='"+v.getOrderItemId()+"' and shopId='"+refundRecord.getRefundShopId()+"' " + " and userId ='"+refundRecord.getUserId()+"'"); UserProjectItem userProjectItem =commonService.selectOne(UserProjectItemMapper.class,sqlSentence); if (userProjectItem == null) { throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"未找到用户子项项目信息"); } Integer usedNum=userProjectItem.getNotUsedNum()-v.getRefundNum(); Integer cancelNum=userProjectItem.getCancelNum()+v.getRefundNum(); map.put("usedNum", usedNum); map.put("isDel", BaseEntity.NO); map.put("cancelNum", cancelNum); map.put("userProjectItemId", userProjectItem.getId()); sqlSentence.setSqlSentence(" cancelNum=#{m.cancelNum},notUsedNum=#{m.usedNum} where isDel=#{m.isDel} AND id=#{m.userProjectItemId} AND notUsedNum > 0 "); commonService.updateWhere(UserProjectItemMapper.class,sqlSentence); UserProject userProject=commonService.selectOneByKey(UserProjectMapper.class,userProjectItem.getUserProjectId()); userProject.setNotUsedNum(userProject.getNotUsedNum()-v.getRefundNum()); commonService.updateAll(UserProjectMapper.class,userProject); //判断操作完了去修改子订单状态 OrderItem orderItem=commonService.selectOneByKey(OrderItemMapper.class,v.getOrderItemId()); if (orderItem==null){ throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"未找到子订单信息"); } Integer refundStatus=OrderTotalConstants.STATUS_REFUND_NONE; if(usedNum==0){ //已经没有可以执行数量 refundStatus=OrderTotalConstants.STATUS_REFUND_FINSH; }else { refundStatus=OrderTotalConstants.STATUS_REFUND_PART; } map.put("orderItemId", v.getOrderItemId()); map.put("refundStatus", refundStatus); map.put("reTotal",orderItem.getReTotal().add(v.getRefundMoney())); map.put("hasReNum",cancelNum/(orderItem.getUsedTotal()/orderItem.getBuyNum())); sqlSentence.setSqlSentence(" refundStatus=#{m.refundStatus}, reTotal=#{m.reTotal}, hasReNum=#{m.hasReNum} where isDel=#{m.isDel} AND id=#{m.orderItemId} "); commonService.updateWhere(OrderItemMapper.class,sqlSentence); } /** * 退款-处理卡项 * @param commonService * @param operationId * @param sqlSentence * @param map * @param refundRecord * @param ordersTotal * @param v */ public static void handRefundCard(CommonService commonService, String operationId, SqlSentence sqlSentence, Map map, RefundRecord refundRecord, OrdersTotal ordersTotal, RefundRecordItem v) { //判断操作完了去修改子订单状态 OrderItem orderItem=commonService.selectOneByKey(OrderItemMapper.class,v.getOrderItemId()); if (orderItem==null){ throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"未找到子订单信息"); } //作废卡包 map.put("hisOrderId",ordersTotal.getHisOrderId()); map.put("orderId",ordersTotal.getId()); map.put("effectiveStatus",UserProjectConstants.EFF_STATUS_YES); map.put("status",UserCard.TYPE_NO_USED); map.put("isDel",BaseEntity.NO); sqlSentence.setSqlSentence("select id from user_card where (hisOrderId =#{m.hisOrderId} OR orderId =#{m.orderId}) AND effectiveStatus=#{m.effectiveStatus} AND status=#{m.status} AND doneeUserId IS NULL AND isDel=#{m.isDel} "); List userCards = commonService.selectList(UserCardMapper.class, sqlSentence); if(userCards==null || userCards.size()==0){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款作废卡包提示:没有找到对应的卡包可退:"+ordersTotal.getId()); }else { UserCard userCard=null; for (int i = 0; i < v.getRefundNum(); i++) { userCard=userCards.get(i); map.put("id",userCard.getId()); map.put("effectiveStatus", UserProjectConstants.EFF_STATUS_CANCEL); sqlSentence.setSqlSentence(" effectiveStatus=#{m.effectiveStatus} where id=#{m.id} "); commonService.updateWhere(UserCardMapper.class,sqlSentence); } } //更改订单子订单状态 map.put("orderItemId", v.getOrderItemId()); map.put("reTotal",orderItem.getReTotal().add(v.getRefundMoney())); Integer hasReNum=orderItem.getHasReNum()+v.getRefundNum(); map.put("hasReNum",hasReNum); if (hasReNum==orderItem.getBuyNum()){ map.put("refundStatus", OrderTotalConstants.STATUS_REFUND_FINSH); }else { map.put("refundStatus", OrderTotalConstants.STATUS_REFUND_PART); } sqlSentence.setSqlSentence(" refundStatus=#{m.refundStatus}, reTotal=#{m.reTotal}, hasReNum=#{m.hasReNum} where isDel=#{m.isDel} AND id=#{m.orderItemId} "); commonService.updateWhere(OrderItemMapper.class,sqlSentence); } /** * 退款-处理卡包 * @param commonService * @param operationId * @param operationNme * @param sqlSentence * @param map * @param refundRecord * @param ordersTotal * @param v */ public static void handCardBag(CommonService commonService, String operationId, String operationNme, SqlSentence sqlSentence, Map map, RefundRecord refundRecord, OrdersTotal ordersTotal,RefundRecordItem v,CustomParameter customParameter) { //查看订单信息 OrderItem orderItem=commonService.selectOneByKey(OrderItemMapper.class,v.getOrderItemId()); if(orderItem==null ){ throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"未找到子订单信息!"); } map.put("refundRecordItemId",v.getId()); sqlSentence.setSqlSentence("select * from refund_record_item_source where refundRecordItemId =#{m.refundRecordItemId}"); List sons = commonService.selectList(RefundRecordItemSourceMapper.class, sqlSentence); if( sons!=null && sons.size()>0 ){ for (RefundRecordItemSource son : sons) { switch (GroupTypeEnum.getCode(son.getType())){ case PROJECT: handRefundNoSonExecution(commonService, sqlSentence, map, refundRecord, son); deleteUserCardUsed(sqlSentence,map,ordersTotal,commonService,son,orderItem); break; case RETAIL: handRefundSonRerail(commonService, sqlSentence, map, refundRecord, son); deleteUserCardUsed(sqlSentence,map,ordersTotal,commonService,son,orderItem); break; case INCREMENT: //增值金 //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0 if(son.getRealRefundTotal().negate().compareTo(BigDecimal.ZERO)!=0){ UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"审核通过退款:促销赠送增值金扣减",operationId,refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),son.getRealRefundTotal().negate(), UserMoneyUnclaimed.FUND_TYPE_VALUE_ADDED_FUND, OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES); } break; case STORED: //储值金额 //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0 if(son.getRealRefundTotal().negate().compareTo(BigDecimal.ZERO)!=0){ UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"审核通过退款:促销赠送储值金额扣减",operationId,refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),son.getRealRefundTotal().negate(), UserMoneyUnclaimed.FUND_TYPE_STORED_VALUE_FUND, OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.NO); } break; case INTEGRAL: //积分 //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0 if(son.getRealRefundTotal().negate().compareTo(BigDecimal.ZERO)!=0){ UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"审核通过退款:促销赠送积分扣减",operationId,refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),son.getRealRefundTotal().negate(), UserMoneyUnclaimed.FUND_TYPE_INTEGRAL,OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES); } break; } } } //更改二级子订单退款状态 map.put("orderItemId",orderItem.getId()); sqlSentence.setSqlSentence("select * from order_item_source WHERE orderItemId=#{m.orderItemId} and isDel=0"); List orderItemSonList=commonService.selectList(OrderItemSonMapper.class,sqlSentence); List collect = orderItemSonList.stream().map(o -> o.getRefundStatus()).collect(Collectors.toList()); if(collect.contains(OrderTotalConstants.STATUS_REFUND_PART)){ orderItem.setRefundStatus(OrderTotalConstants.STATUS_REFUND_PART); orderItem.setReTotal(orderItemSonList.stream().map(OrderItemSon::getReTotal).reduce(BigDecimal.ZERO,BigDecimal::add)); }else if (collect.contains(OrderTotalConstants.STATUS_REFUND_NONE) && collect.contains(OrderTotalConstants.STATUS_REFUND_FINSH)){ orderItem.setRefundStatus(OrderTotalConstants.STATUS_REFUND_PART); orderItem.setReTotal(orderItemSonList.stream().map(OrderItemSon::getReTotal).reduce(BigDecimal.ZERO,BigDecimal::add)); }else if (collect.contains(OrderTotalConstants.STATUS_REFUND_NONE)){ orderItem.setRefundStatus(OrderTotalConstants.STATUS_REFUND_NONE); }else { orderItem.setRefundStatus(OrderTotalConstants.STATUS_REFUND_FINSH); orderItem.setReTotal(orderItemSonList.stream().map(OrderItemSon::getReTotal).reduce(BigDecimal.ZERO,BigDecimal::add)); orderItem.setHasReNum(orderItem.getBuyNum()); } commonService.updateAll(OrderItemMapper.class,orderItem); } /** * 退款需要删除用户卡包使用记录 * @param sqlSentence * @param map * @param ordersTotal * @param commonService * @param son */ public static void deleteUserCardUsed(SqlSentence sqlSentence, Map map,OrdersTotal ordersTotal,CommonService commonService,RefundRecordItemSource son,OrderItem orderItem){ OrderItemSon orderItemSon=commonService.selectOneByKey(OrderItemSonMapper.class,son.getOrderItemSonId()); Integer hasReNum=orderItemSon.getHasReNum(); map.put("isDel",BaseEntity.NO); map.put("orderId",ordersTotal.getId()); map.put("commonId",orderItemSon.getGoodsId()); sqlSentence.setSqlSentence(" SELECT * FROM user_card_used WHERE orderId = #{m.orderId} and commonId=#{m.commonId} ORDER BY isDel,createTime "); List userCardUsedList=commonService.selectList(UserCardUsedMapper.class,sqlSentence); if(userCardUsedList==null || userCardUsedList.size()==0){ //因为可能领建的老订单所以伪造的操作记录是没数据的 map.put("userCardId",orderItem.getCommonId()); sqlSentence.setSqlSentence(" SELECT * FROM user_card_used WHERE commonId=#{m.commonId} and userCardId=#{m.userCardId} ORDER BY isDel,createTime "); userCardUsedList=commonService.selectList(UserCardUsedMapper.class,sqlSentence); } UserCardUsed userCardUsed; for (int i = 0; i < hasReNum; i++) { userCardUsed=userCardUsedList.get(i); userCardUsed.setIsDel(BaseEntity.YES); commonService.updateAll(UserCardUsedMapper.class,userCardUsed); } } /** * 退款-处理促销 * @param commonService * @param operationId * @param operationNme * @param sqlSentence * @param map * @param refundRecord * @param ordersTotal * @param v */ private static void handRefundPromotion(CommonService commonService, String operationId, String operationNme, SqlSentence sqlSentence, Map map, RefundRecord refundRecord, OrdersTotal ordersTotal, RefundRecordItem v) { //查看订单信息 OrderItem orderItem=commonService.selectOneByKey(OrderItemMapper.class,v.getOrderItemId()); if(orderItem==null ){ throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"未找到子订单信息!"); } map.put("refundRecordItemId",v.getId()); sqlSentence.setSqlSentence("select * from refund_record_item_source where refundRecordItemId =#{m.refundRecordItemId}"); List sons = commonService.selectList(RefundRecordItemSourceMapper.class, sqlSentence); if( sons!=null && sons.size()>0 ){ for (RefundRecordItemSource son : sons) { switch (GroupTypeEnum.getCode(son.getType())){ case PROJECT: v.setOrderItemId(son.getId()); v.setRefundNum(son.getRefundNum()); handRefundNoSonExecution(commonService, sqlSentence, map, refundRecord, son); break; case RETAIL: v.setOrderItemId(son.getId()); v.setRefundNum(son.getRefundNum()); handRefundSonRerail(commonService, sqlSentence, map, refundRecord, son); break; case INCREMENT: //增值金 //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0 if(son.getRealRefundTotal().negate().compareTo(BigDecimal.ZERO)!=0){ UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"审核通过退款:促销赠送增值金扣减",operationId,refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),son.getRealRefundTotal().negate(), UserMoneyUnclaimed.FUND_TYPE_VALUE_ADDED_FUND, OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES); } break; case STORED: //储值金额 //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0 if(son.getRealRefundTotal().negate().compareTo(BigDecimal.ZERO)!=0){ UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"审核通过退款:促销赠送储值金额扣减",operationId,refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),son.getRealRefundTotal().negate(), UserMoneyUnclaimed.FUND_TYPE_STORED_VALUE_FUND, OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.NO); } break; case INTEGRAL: //积分 //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0 if(son.getRealRefundTotal().negate().compareTo(BigDecimal.ZERO)!=0){ UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"审核通过退款:促销赠送积分扣减",operationId,refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),son.getRealRefundTotal().negate(), UserMoneyUnclaimed.FUND_TYPE_INTEGRAL,OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES); } break; } } } //更改二级子订单退款状态 map.put("orderItemId",orderItem.getId()); sqlSentence.setSqlSentence("select * from order_item_source WHERE orderItemId=#{m.orderItemId} and isDel=0"); List orderItemSonList=commonService.selectList(OrderItemSonMapper.class,sqlSentence); List collect = orderItemSonList.stream().map(o -> o.getRefundStatus()).collect(Collectors.toList()); if(collect.contains(OrderTotalConstants.STATUS_REFUND_PART)){ orderItem.setRefundStatus(OrderTotalConstants.STATUS_REFUND_PART); orderItem.setReTotal(orderItemSonList.stream().map(OrderItemSon::getReTotal).reduce(BigDecimal.ZERO,BigDecimal::add)); }else if (collect.contains(OrderTotalConstants.STATUS_REFUND_NONE) && collect.contains(OrderTotalConstants.STATUS_REFUND_FINSH)){ orderItem.setRefundStatus(OrderTotalConstants.STATUS_REFUND_PART); orderItem.setReTotal(orderItemSonList.stream().map(OrderItemSon::getReTotal).reduce(BigDecimal.ZERO,BigDecimal::add)); }else if (collect.contains(OrderTotalConstants.STATUS_REFUND_NONE)){ orderItem.setRefundStatus(OrderTotalConstants.STATUS_REFUND_NONE); }else { orderItem.setRefundStatus(OrderTotalConstants.STATUS_REFUND_FINSH); orderItem.setReTotal(orderItemSonList.stream().map(OrderItemSon::getReTotal).reduce(BigDecimal.ZERO,BigDecimal::add)); orderItem.setHasReNum(orderItem.getBuyNum()); } commonService.updateAll(OrderItemMapper.class,orderItem); } /** * 退款-处理二级子订单未执行划扣 项目类型 * @param commonService * @param sqlSentence * @param map * @param refundRecord * @param v */ private static void handRefundNoSonExecution(CommonService commonService, SqlSentence sqlSentence, Map map, RefundRecord refundRecord, RefundRecordItemSource v) { sqlSentence.setSqlSentence("select * from user_project_item where isDel=0 and commonId='"+v.getOrderItemSonId()+"' and shopId='"+refundRecord.getRefundShopId()+"' " + " and userId ='"+refundRecord.getUserId()+"'"); UserProjectItem userProjectItem =commonService.selectOne(UserProjectItemMapper.class,sqlSentence); if (userProjectItem == null) { throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"未找到用户子项项目信息"); } Integer usedNum=userProjectItem.getNotUsedNum()-v.getRefundNum(); Integer cancelNum=userProjectItem.getCancelNum()+v.getRefundNum(); map.put("usedNum", usedNum); map.put("isDel", BaseEntity.NO); map.put("cancelNum", cancelNum); map.put("userProjectItemId", userProjectItem.getId()); sqlSentence.setSqlSentence(" cancelNum=#{m.cancelNum},notUsedNum=#{m.usedNum} where isDel=#{m.isDel} AND id=#{m.userProjectItemId} AND notUsedNum > 0 "); commonService.updateWhere(UserProjectItemMapper.class,sqlSentence); UserProject userProject=commonService.selectOneByKey(UserProjectMapper.class,userProjectItem.getUserProjectId()); userProject.setNotUsedNum(userProject.getNotUsedNum()-v.getRefundNum()); commonService.updateAll(UserProjectMapper.class,userProject); //判断操作完了去修改子订单状态 OrderItemSon orderItem=commonService.selectOneByKey(OrderItemSonMapper.class,v.getOrderItemSonId()); if (orderItem==null){ logger.info("二级子订单id:{}",v.getOrderItemSonId()); throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"未找到二级子订单信息"); } Integer refundStatus=OrderTotalConstants.STATUS_REFUND_NONE; if(usedNum==0){ //已经没有可以执行数量 refundStatus=OrderTotalConstants.STATUS_REFUND_FINSH; }else { refundStatus=OrderTotalConstants.STATUS_REFUND_PART; } map.put("orderItemId", v.getOrderItemSonId()); map.put("reTotal",orderItem.getReTotal().add(v.getRefundMoney())); map.put("hasReNum",cancelNum/(orderItem.getUsedTotal()/orderItem.getBuyNum())); map.put("refundStatus", refundStatus); sqlSentence.setSqlSentence(" refundStatus=#{m.refundStatus}, reTotal=#{m.reTotal}, hasReNum=#{m.hasReNum} where isDel=#{m.isDel} AND id=#{m.orderItemId} "); commonService.updateWhere(OrderItemSonMapper.class,sqlSentence); } /** * 退款二级是商品 * @param commonService * @param sqlSentence * @param map * @param refundRecord * @param v */ private static void handRefundSonRerail(CommonService commonService,SqlSentence sqlSentence, Map map, RefundRecord refundRecord, RefundRecordItemSource v) { //判断操作完了去修改子订单状态 OrderItemSon orderItem=commonService.selectOneByKey(OrderItemSonMapper.class,v.getOrderItemSonId()); if (orderItem==null){ throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"未找到二级子订单信息"); } map.put("orderItemId", v.getOrderItemSonId()); map.put("reTotal",orderItem.getReTotal().add(v.getRefundMoney())); Integer hasReNum=orderItem.getHasReNum()+v.getRefundNum(); map.put("hasReNum",hasReNum); map.put("isDel",BaseEntity.NO); if (hasReNum==orderItem.getBuyNum()){ map.put("refundStatus", OrderTotalConstants.STATUS_REFUND_FINSH); }else { map.put("refundStatus", OrderTotalConstants.STATUS_REFUND_PART); } sqlSentence.setSqlSentence(" refundStatus=#{m.refundStatus}, reTotal=#{m.reTotal}, hasReNum=#{m.hasReNum} where isDel=#{m.isDel} AND id=#{m.orderItemId} "); commonService.updateWhere(OrderItemSonMapper.class,sqlSentence); } }