package com.hx.phip.tool.refund;
|
|
import com.alibaba.fastjson.JSON;
|
import com.hx.common.service.CommonService;
|
import com.hx.exception.TipsException;
|
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.model.BaseEntity;
|
import com.hx.phiappt.model.activity.ActivityAction;
|
import com.hx.phiappt.model.activity.ActivityRule;
|
import com.hx.phiappt.model.cardItem.CardEquity;
|
import com.hx.phiappt.model.cardItem.CardItemInfo;
|
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.UserProjectItem;
|
import com.hx.phiappt.model.userMoney.UserMoneyUnclaimed;
|
import com.hx.phip.dao.mapper.*;
|
import com.hx.phip.service.order.impl.OrderRefundServiceImpl;
|
import com.hx.phip.tool.user.UserProjectTool;
|
import com.hx.phip.util.api.UserMoneyUtil;
|
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.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.stream.Collectors;
|
|
/**
|
* 订单退款单作废工具
|
* @author CJH
|
*/
|
public class OrderRefundCancelTool {
|
|
/**log4j日志*/
|
private static final Logger logger = LoggerFactory.getLogger(OrderRefundServiceImpl.class.getName());
|
|
|
/**处理总退款方式数据*/
|
public static void refundRecordMotnedHandle(RefundRecord refundRecord,OrdersTotal ordersTotal,CommonService commonService){
|
SqlSentence sqlSentence = new SqlSentence();
|
Map<String,Object> values = new HashMap<>();
|
|
//获取总退款方式
|
values.put("refundRecordId",refundRecord.getId());
|
sqlSentence.sqlSentence("SELECT * FROM refund_record_method WHERE isDel = 0 AND refundRecordId = #{m.refundRecordId}",values);
|
List<RefundRecordMethod> refundRecordMethodList = commonService.selectList(RefundRecordMethodMapper.class,sqlSentence);
|
//退款的支付方式记录数据
|
values.clear();
|
values.put("refundRecordId",refundRecord.getId());
|
sqlSentence.sqlSentence("SELECT * FROM refund_record_consume_pay WHERE isDel = 0 AND refundRecordId = #{m.refundRecordId}",values);
|
List<RefundRecordConsumePay> refundConsumePayList = commonService.selectList(RefundRecordConsumePayMapper.class,sqlSentence);
|
|
|
for(RefundRecordConsumePay refundRecordConsumePay:refundConsumePayList){
|
//更新支付记录退款信息
|
values.clear();
|
values.put("refundTotal",refundRecordConsumePay.getRefundTotal().negate());
|
values.put("status",RefundRecordConsumePay.NO);
|
values.put("statusOld",RefundRecordConsumePay.YES);
|
values.put("id",refundRecordConsumePay.getConsumePayId());
|
sqlSentence.sqlUpdate("refundTotal = refundTotal + #{m.refundTotal} WHERE id = #{m.id} AND actualTotal >= refundTotal + #{m.refundTotal}",values);
|
if(commonService.updateWhere(ConsumePayMapper.class,sqlSentence) != 1){
|
throw new TipsException("退款金额错误[33]!");
|
}
|
|
//更新支付记录退款信息
|
values.clear();
|
values.put("status",RefundRecordConsumePay.NO);
|
values.put("statusOld",RefundRecordConsumePay.YES);
|
values.put("id",refundRecordConsumePay.getId());
|
sqlSentence.sqlUpdate("status = #{m.status} WHERE id = #{m.id} AND statusOld = #{m.statusOld}",values);
|
if(commonService.updateWhere(RefundRecordConsumePayMapper.class,sqlSentence) != 1){
|
throw new TipsException("退款金额记录状态错误!");
|
}
|
|
}
|
|
//处理回退到账
|
for(RefundRecordMethod refundRecordMethod:refundRecordMethodList){
|
if(PayMethodTypeConstants.PAY_STORED.equals(refundRecordMethod.getRefundNumberNo())){
|
//储值金额
|
//判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
|
if(refundRecordMethod.getActualTotal().compareTo(BigDecimal.ZERO)>0){
|
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);
|
OrderLog orderLog = RefundToolUtil.setOrderLog(refundRecord, refundRecord.getInvalidId(), refundRecord.getInvalidName(), refundRecordMethod.getName()+"作废退款金额:"+refundRecordMethod.getRealRefundTotal(), 1, OrderLogConstants.LOG_TYPE_REFUND_UNDO);
|
commonService.insert(OrderLogMapper.class,orderLog);
|
}
|
}else if(PayMethodTypeConstants.PAY_ADD_FUND.equals(refundRecordMethod.getRefundNumberNo())){
|
//增值金
|
//判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
|
if(refundRecordMethod.getActualTotal().compareTo(BigDecimal.ZERO)!=0){
|
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);
|
OrderLog orderLog = RefundToolUtil.setOrderLog(refundRecord,refundRecord.getInvalidId(),refundRecord.getInvalidName(),refundRecordMethod.getName(),1, OrderLogConstants.LOG_TYPE_REFUND_UNDO);
|
commonService.insert(OrderLogMapper.class,orderLog);
|
}
|
}else if(PayMethodTypeConstants.PAY_INTEGRAL.equals(refundRecordMethod.getRefundNumberNo())){
|
//积分
|
//判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
|
if(refundRecordMethod.getActualTotal().compareTo(BigDecimal.ZERO)!=0){
|
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);
|
OrderLog orderLog = RefundToolUtil.setOrderLog(refundRecord,refundRecord.getInvalidId(),refundRecord.getInvalidName(),refundRecordMethod.getName()+"作废退款金额:"+refundRecordMethod.getRealRefundTotal(),1, OrderLogConstants.LOG_TYPE_REFUND_UNDO);
|
commonService.insert(OrderLogMapper.class,orderLog);
|
}
|
}else {
|
//现金支付
|
if(refundRecordMethod.getActualTotal().compareTo(BigDecimal.ZERO)<1){
|
continue;
|
}
|
OrderLog orderLog = RefundToolUtil.setOrderLog(refundRecord,refundRecord.getInvalidId(),refundRecord.getInvalidName(),refundRecordMethod.getName()+"作废退款金额:"+refundRecordMethod.getRealRefundTotal(),1, OrderLogConstants.LOG_TYPE_REFUND_UNDO);
|
orderLog.setOrderId(ordersTotal.getId());
|
commonService.insert(OrderLogMapper.class,orderLog);
|
}
|
}
|
}
|
|
/**充值单特需处理
|
* @param ordersTotal 订单
|
* @param refundRecord 退款单
|
* @param optType 操作类型:0退款执行,1作废退款
|
* @param commonService 映射
|
*/
|
public static void rechargeHandle(OrdersTotal ordersTotal, RefundRecord refundRecord,int optType, CommonService commonService){
|
|
if(!OrderTotalConstants.TYPE_RECHARGE.equals(ordersTotal.getType())){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"操作失败,不是充值单!");
|
}
|
if(ordersTotal.getTotal().compareTo(BigDecimal.ZERO) < 1){
|
return;
|
}
|
if(optType == 0){
|
//充值金额扣减
|
UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getInvalidRemarks(),"退款单:"+refundRecord.getCode()+" 申请退款,扣减充值资金:"+ordersTotal.getTotal(),refundRecord.getInvalidId(),refundRecord.getOrderId(),refundRecord.getInvalidAppCode(),refundRecord.getId(),ordersTotal.getTotal().negate(), UserMoneyUnclaimed.FUND_TYPE_STORED_VALUE_FUND, OperationReasonConstants.OP_REASON_REFUND_CANCEL,commonService,UserMoneyUnclaimed.NO);
|
}else if(optType == 1){
|
//充值金额回退
|
UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getInvalidRemarks(),"退款单:"+refundRecord.getCode()+" 作废退款,退回充值资金:"+ordersTotal.getTotal(),refundRecord.getInvalidId(),refundRecord.getOrderId(),refundRecord.getInvalidAppCode(),refundRecord.getId(),ordersTotal.getTotal(), UserMoneyUnclaimed.FUND_TYPE_STORED_VALUE_FUND, OperationReasonConstants.OP_REASON_REFUND_CANCEL,commonService,UserMoneyUnclaimed.NO);
|
}else{
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"操作类型错误[1]["+optType+"]!");
|
}
|
|
}
|
|
/**活动规则处理
|
* @param ordersTotal 订单
|
* @param refundRecord 退款单
|
* @param optType 操作类型:0退款执行,1作废退款
|
* @param commonService 映射
|
*/
|
public static void activityRuleHandle(OrdersTotal ordersTotal, RefundRecord refundRecord,int optType,CommonService commonService){
|
|
//先获取活动规则
|
SqlSentence sqlSentence = new SqlSentence();
|
Map<String,Object> values = new HashMap<>();
|
|
values.put("orderId",ordersTotal.getId());
|
sqlSentence.sqlSentence("SELECT * FROM orders_activity_info WHERE isDel = 0 AND orderId = #{m.orderId}",values);
|
List<OrdersActivityInfo> ordersActivityInfoList = commonService.selectList(OrdersActivityInfoMapper.class,sqlSentence);
|
if(ordersActivityInfoList.size() > 0){
|
//订单活动信息处理
|
activityRulePollingHandle2(ordersActivityInfoList,optType,ordersTotal,refundRecord,commonService);
|
}else{
|
//没有订单活动信息处理,就获取保存在订单里面的活动标识
|
values.clear();
|
values.put("orderId",ordersTotal.getId());
|
sqlSentence.sqlSentence("SELECT * FROM orders_info WHERE isDel = 0 AND orderId = #{m.orderId}",values);
|
OrderInfo orderInfo = commonService.selectOne(OrderInfoMapper.class,sqlSentence);
|
if(orderInfo!=null && StringUtils.noNull(orderInfo.getActivityId())){
|
activityRulePollingHandle1(orderInfo.getActivityId(),optType,ordersTotal,refundRecord,commonService);
|
}
|
}
|
}
|
|
/**处理活动标识保存在订单里面的
|
* @param activityId 活动标识
|
* @param optType 操作类型:0退款执行,1退款作废
|
* @param ordersTotal 订单
|
* @param refundRecord 退款单
|
* @param commonService 映射
|
*/
|
public static void activityRulePollingHandle1(String activityId,int optType,OrdersTotal ordersTotal,RefundRecord refundRecord,CommonService commonService){
|
SqlSentence sqlSentence = new SqlSentence();
|
Map<String,Object> values = new HashMap<>();
|
|
//查找活动规则,没有找到就跳出循环
|
ActivityRule activityRule = commonService.selectOneByKeyBlob(ActivityRuleMapper.class,activityId);
|
if(activityRule == null){
|
return;
|
}
|
|
values.put("activityRuleId",activityRule.getId());
|
values.put("type", ActivityAction.TYPE_INTEGRAL);
|
values.put("type1",ActivityAction.TYPE_VALUEADDEDFUND);
|
values.put("type2",ActivityAction.TYPE_COUPON);
|
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);
|
List<ActivityAction> 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()).compareTo(BigDecimal.ZERO) == 0){
|
continue;
|
}
|
if(optType == 0){
|
UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"充值单:"+ordersTotal.getOrderNo()+" 申请退款,预扣除活动规则赠送储值金金额:"+activityAction.getWorth(),refundRecord.getOperatorId(),refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),new BigDecimal(activityAction.getWorth()).negate(), UserMoneyUnclaimed.FUND_TYPE_INTEGRAL,OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES);
|
}else if(optType == 1){
|
UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"充值单:"+ordersTotal.getOrderNo()+" 作废退款,返还赠送储值金预扣除金额:"+activityAction.getWorth(),refundRecord.getOperatorId(),refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),new BigDecimal(activityAction.getWorth()), UserMoneyUnclaimed.FUND_TYPE_INTEGRAL,OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES);
|
}else{
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"操作类型错误[2]["+optType+"]!");
|
}
|
}else if(ActivityAction.TYPE_VALUEADDEDFUND.equals(activityAction.getType())){
|
//判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
|
if(new BigDecimal(activityAction.getWorth()).compareTo(BigDecimal.ZERO) == 0 ){
|
continue;
|
}
|
if(optType == 0){
|
UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"充值单:"+ordersTotal.getOrderNo()+" 申请退款,预扣除活动规则赠送增值金金额:"+activityAction.getWorth(),refundRecord.getOperatorId(),refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),new BigDecimal(activityAction.getWorth()).negate(), UserMoneyUnclaimed.FUND_TYPE_VALUE_ADDED_FUND,OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES);
|
}else if(optType == 1){
|
UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"充值单:"+ordersTotal.getOrderNo()+" 作废退款,返还赠送增值金预扣除金额:"+activityAction.getWorth(),refundRecord.getOperatorId(),refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),new BigDecimal(activityAction.getWorth()), UserMoneyUnclaimed.FUND_TYPE_VALUE_ADDED_FUND,OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES);
|
}else{
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"操作类型错误[3]["+optType+"]!");
|
}
|
}else if(ActivityAction.TYPE_COUPON.equals(activityAction.getType())){
|
values.put("oldValidState",optType==0? BaseEntity.YES:BaseEntity.NO);
|
values.put("newValidState",optType==0?BaseEntity.NO:BaseEntity.YES);
|
values.put("couponId",activityAction.getCrmCouponId());
|
values.put("commonId",ordersTotal.getId());
|
sqlSentence.sqlSentence(" validState=#{m.newValidState} where couponId=#{m.couponId} and commonId=#{m.commonId} and validState=#{m.oldValidState} ",values);
|
commonService.updateWhere(CouponNumberMapper.class,sqlSentence);
|
}
|
}
|
}
|
}
|
|
/**处理活动保存在订单活动信息里面的
|
* @param ordersActivityInfoList 订单活动数据
|
* @param optType 操作类型:0退款退回,1作废退款
|
* @param ordersTotal 订单
|
* @param refundRecord 退款单
|
* @param commonService 映射
|
*/
|
public static void activityRulePollingHandle2(List<OrdersActivityInfo> ordersActivityInfoList,int optType,OrdersTotal ordersTotal
|
,RefundRecord refundRecord,CommonService commonService){
|
if(ordersActivityInfoList == null || ordersActivityInfoList.size()==0){
|
return;
|
}
|
SqlSentence sqlSentence = new SqlSentence();
|
Map<String,Object> values = new HashMap<>();
|
|
//获取订单活动条目
|
List<OrdersActivityInfoItem> ordersActivityInfoItemList;
|
for(OrdersActivityInfo ordersActivityInfo:ordersActivityInfoList){
|
values.clear();
|
values.put("activityInfoId",ordersActivityInfo.getId());
|
values.put("performStatus",OrdersActivityInfoItem.STATUS_PERFORM_SEND_OUT);
|
values.put("handleStatus",optType==0?OrdersActivityInfoItem.HANDLE_STATUS_SUCCESS:OrdersActivityInfoItem.HANDLE_STATUS_CANCEL);
|
sqlSentence.sqlSentence("SELECT * FROM orders_activity_info_item WHERE isDel = 0 AND activityInfoId = #{m.activityInfoId} AND performStatus = #{m.performStatus} AND handleStatus = #{m.handleStatus}",values);
|
ordersActivityInfoItemList = commonService.selectList(OrdersActivityInfoItemMapper.class,sqlSentence);
|
for(OrdersActivityInfoItem ordersActivityInfoItem:ordersActivityInfoItemList){
|
if(ActivityAction.TYPE_INTEGRAL.equals(ordersActivityInfoItem.getType())){
|
//判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
|
if(new BigDecimal(ordersActivityInfoItem.getWorth()).compareTo(BigDecimal.ZERO) == 0){
|
continue;
|
}
|
if(optType == 0){
|
UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"充值单:"+ordersTotal.getOrderNo()+" 申请退款,预扣除活动规则赠送储值金金额:"+ordersActivityInfoItem.getWorth(),refundRecord.getOperatorId(),refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),new BigDecimal(ordersActivityInfoItem.getWorth()).negate(), UserMoneyUnclaimed.FUND_TYPE_INTEGRAL,OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES);
|
}else if(optType == 1){
|
UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"充值单:"+ordersTotal.getOrderNo()+" 作废退款,返还赠送储值金预扣除金额:"+ordersActivityInfoItem.getWorth(),refundRecord.getOperatorId(),refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),new BigDecimal(ordersActivityInfoItem.getWorth()), UserMoneyUnclaimed.FUND_TYPE_INTEGRAL,OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES);
|
}else{
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"操作类型错误[2]["+optType+"]!");
|
}
|
}else if(ActivityAction.TYPE_VALUEADDEDFUND.equals(ordersActivityInfoItem.getType())){
|
//判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
|
if(new BigDecimal(ordersActivityInfoItem.getWorth()).compareTo(BigDecimal.ZERO) == 0 ){
|
continue;
|
}
|
if(optType == 0){
|
UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"充值单:"+ordersTotal.getOrderNo()+" 申请退款,预扣除活动规则赠送增值金金额:"+ordersActivityInfoItem.getWorth(),refundRecord.getOperatorId(),refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),new BigDecimal(ordersActivityInfoItem.getWorth()).negate(), UserMoneyUnclaimed.FUND_TYPE_VALUE_ADDED_FUND,OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES);
|
}else if(optType == 1){
|
UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"充值单:"+ordersTotal.getOrderNo()+" 作废退款,返还赠送增值金预扣除金额:"+ordersActivityInfoItem.getWorth(),refundRecord.getOperatorId(),refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),new BigDecimal(ordersActivityInfoItem.getWorth()), UserMoneyUnclaimed.FUND_TYPE_VALUE_ADDED_FUND,OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES);
|
}else{
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"操作类型错误[3]["+optType+"]!");
|
}
|
}else if(ActivityAction.TYPE_COUPON.equals(ordersActivityInfoItem.getType())){
|
values.put("oldValidState",optType==0? BaseEntity.YES:BaseEntity.NO);
|
values.put("newValidState",optType==0?BaseEntity.NO:BaseEntity.YES);
|
values.put("id",ordersActivityInfoItem.getCouponNumberId());
|
sqlSentence.sqlSentence(" validState=#{m.newValidState} where id=#{m.id} AND validState=#{m.oldValidState} ",values);
|
//优惠券不判断回退
|
commonService.updateWhere(CouponNumberMapper.class,sqlSentence);
|
}
|
|
values.clear();
|
values.put("handleStatus",optType==0?OrdersActivityInfoItem.HANDLE_STATUS_CANCEL:OrdersActivityInfoItem.HANDLE_STATUS_SUCCESS);
|
values.put("handleStatusOld",optType==0?OrdersActivityInfoItem.HANDLE_STATUS_SUCCESS:OrdersActivityInfoItem.HANDLE_STATUS_CANCEL);
|
values.put("performStatusOld",OrdersActivityInfoItem.STATUS_PERFORM_SEND_OUT);
|
values.put("id",ordersActivityInfoItem.getId());
|
sqlSentence.sqlSentence("handleStatus = #{m.handleStatus} WHERE id = #{m.id} AND handleStatus = #{m.handleStatusOld} AND performStatus = #{m.performStatusOld}",values);
|
if(commonService.updateWhere(OrdersActivityInfoItemMapper.class,sqlSentence) != 1){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"活动条目状态变化错误:");
|
}
|
|
}
|
}
|
}
|
|
/**
|
* 退款作废-处理优惠券
|
* @param ordersTotal 订单
|
* @param refundRecord 退款总数据
|
* @param commonService 映射
|
*/
|
public static void handCoupon(OrdersTotal ordersTotal,RefundRecord refundRecord,CommonService commonService) {
|
|
//回退的优惠券变成使用状态
|
//如果该优惠券在别的订单使用了,那么就不再理会
|
|
SqlSentence sqlSentence = new SqlSentence();
|
Map<String, Object> values = new HashMap<>();
|
|
values.put("refundRecordId",refundRecord.getId());
|
sqlSentence.sqlSentence("select * from refund_record_coupon where refundRecordId=#{m.refundRecordId} and isDel=0 ",values);
|
List<RefundRecordCoupon> refundRecordCouponList=commonService.selectList(RefundRecordCouponMapper.class,sqlSentence);
|
|
CouponOrderDiscountLog couponOrderDiscountLog;
|
for (RefundRecordCoupon refundRecordCoupon : refundRecordCouponList) {
|
|
couponOrderDiscountLog=commonService.selectOneByKey(CouponOrderDiscountLogMapper.class,refundRecordCoupon.getCouponOrderId());
|
if(couponOrderDiscountLog==null){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"订单优惠卷标识不正确");
|
}
|
|
//领建优惠券跳过回退
|
if(PartialRefundUtil.HIS_COUPON_CODE.equals(couponOrderDiscountLog.getCouponNumberId())){
|
//变更订单优惠券记录状态
|
values.clear();
|
values.put("status", BaseEntity.NO);
|
values.put("oldStatus", BaseEntity.YES);
|
values.put("id", couponOrderDiscountLog.getId());
|
sqlSentence.sqlSentence("status = #{m.status} WHERE id = #{m.id} AND status = #{m.oldStatus}",values);
|
if(commonService.updateWhere(CouponOrderDiscountLogMapper.class,sqlSentence) != 1){
|
throw new TipsException("优惠券回退失败!");
|
}
|
}else{
|
//优惠券状态变化
|
values.clear();
|
values.put("isUse", BaseEntity.YES);
|
values.put("isUseOld", BaseEntity.NO);
|
values.put("useTime", couponOrderDiscountLog.getCreateTime());
|
values.put("useType", CouponNumber.USE_TYPE_USER);
|
values.put("id", couponOrderDiscountLog.getCouponNumberId());
|
sqlSentence.sqlSentence(" isUse=#{m.isUse},useTime=#{m.useTime},useType=#{m.useType},isUse=#{m.isUse} WHERE id = #{m.id} AND isUse = #{m.isUseOld}",values);
|
if(commonService.updateWhere(CouponNumberMapper.class,sqlSentence) == 1){
|
//变更订单优惠券记录状态
|
values.clear();
|
values.put("status", BaseEntity.NO);
|
values.put("oldStatus", BaseEntity.YES);
|
values.put("id", couponOrderDiscountLog.getId());
|
sqlSentence.sqlSentence("status = #{m.status} WHERE id = #{m.id} AND status = #{m.oldStatus}",values);
|
if(commonService.updateWhere(CouponOrderDiscountLogMapper.class,sqlSentence) != 1) {
|
throw new TipsException("优惠券回退失败!");
|
}
|
}
|
}
|
}
|
}
|
|
/**退款子项处理*/
|
public static void refundRecordItemHandle(OrdersTotal ordersTotal,RefundRecord refundRecord,CommonService commonService){
|
|
SqlSentence sqlSentence = new SqlSentence();
|
Map<String,Object> values = new HashMap<>();
|
|
//获取退款子单
|
values.put("refundRecordId",refundRecord.getId());
|
sqlSentence.sqlSentence("SELECT * FROM refund_record_item WHERE isDel = 0 AND refundRecordId = #{m.refundRecordId}",values);
|
List<RefundRecordItem> refundRecordItems =commonService.selectList(RefundRecordItemMapper.class,sqlSentence);
|
if(refundRecordItems == null || refundRecordItems.size() == 0){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到退款子订单信息");
|
}
|
|
for (RefundRecordItem refundRecordItem: refundRecordItems) {
|
//商品类型判断
|
switch (refundRecordItem.getType()){
|
case OrderItemConstants.TYPE_RETAIL:
|
//商品/药品
|
rerailHandleOne(refundRecordItem,commonService);
|
break;
|
case OrderItemConstants.TYPE_DRUG:
|
//处方单药品
|
rerailHandleOne(refundRecordItem,commonService);
|
break;
|
case OrderItemConstants.TYPE_PROJECT:
|
//项目
|
projectHandleOne(refundRecordItem,refundRecord,commonService);
|
break;
|
case OrderItemConstants.TYPE_PROMOTION:
|
//促销
|
handRefundPromotion( refundRecordItem, refundRecord, commonService);
|
break;
|
case OrderItemConstants.TYPE_CARD:
|
//卡项
|
cardHandleOne( refundRecord, refundRecordItem, commonService);
|
break;
|
case OrderItemConstants.CARD_BAG:
|
//卡包
|
handRefundPromotion( refundRecordItem, refundRecord, commonService);
|
break;
|
default:break;
|
}
|
}
|
}
|
|
|
/**一级订单-商品/药品处理*/
|
public static void rerailHandleOne(RefundRecordItem refundRecordItem,CommonService commonService){
|
//判断操作完了去修改子订单状态
|
OrderItem orderItem=commonService.selectOneByKey(OrderItemMapper.class,refundRecordItem.getOrderItemId());
|
if (orderItem==null){
|
throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"未找到子订单信息");
|
}
|
|
//剩余可退款数量
|
if(refundRecordItem.getRefundNum() > orderItem.getHasReNum()){
|
throw new TipsException(orderItem.getGoodsName()+"["+orderItem.getGoodsNo()+"]"+"作废退款数量不能大于已退款数量!");
|
}
|
|
//变更退款子项信息
|
int refundStatus;
|
if (orderItem.getHasReNum().equals(refundRecordItem.getRefundNum())){
|
refundStatus = OrderTotalConstants.STATUS_REFUND_NONE;
|
}else{
|
refundStatus = OrderTotalConstants.STATUS_REFUND_PART;
|
}
|
|
//更新子订单信息
|
PartialRefundUtil.updateOrderItemOne(orderItem,refundStatus,refundRecordItem.getRealRefundTotal().negate(),-refundRecordItem.getRealRefundNum(), commonService);
|
}
|
|
/**一级订单-项目处理*/
|
public static void projectHandleOne(RefundRecordItem refundRecordItem,RefundRecord refundRecord,CommonService commonService){
|
//找到子单
|
OrderItem orderItem=commonService.selectOneByKey(OrderItemMapper.class,refundRecordItem.getOrderItemId());
|
if (orderItem==null){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到子订单信息");
|
}
|
//计算子单是否还有剩余的可扣疗程数
|
if(refundRecordItem.getRefundNum() > orderItem.getHasReNum()){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,orderItem.getGoodsName()+"["+orderItem.getGoodsNo()+"]"+"退款次数大于可退次数[54]!");
|
}
|
|
//找到用户项目
|
UserProjectItem userProjectItem = PartialRefundUtil.getUserProject(refundRecordItem.getId(),commonService);
|
if (userProjectItem == null) {
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,orderItem.getGoodsName()+"["+orderItem.getGoodsNo()+"]"+"未找到用户子项项目信息");
|
}
|
if(userProjectItem.getCancelNum() < refundRecordItem.getRefundNum()){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,orderItem.getGoodsName()+"["+orderItem.getGoodsNo()+"]"+"作废退款次数大于用户项目已退次数!");
|
}
|
|
//用户项目操作
|
UserProjectTool.userProjectDeduction(userProjectItem, UserProjectUsedCon.USED_METHOD_ORDER_REFUND,UserProjectUsedCon.USED_TYPE_RETURN,refundRecordItem.getDeductionTotalUser()
|
,refundRecordItem.getId(),refundRecordItem.getRealRefundNum(),refundRecord.getInvalidAppId(),refundRecord.getInvalidAppName(),refundRecord.getRefundShopId(),refundRecord.getRefundShopName(),"员工备注:"+refundRecord.getInvalidRemarks(),commonService);
|
|
int refundStatus;
|
if(refundRecordItem.getRefundNum().equals(orderItem.getHasReNum())){
|
refundStatus=OrderTotalConstants.STATUS_REFUND_NONE;
|
}else{
|
refundStatus=OrderTotalConstants.STATUS_REFUND_PART;
|
}
|
|
//更新子订单信息
|
PartialRefundUtil.updateOrderItemOne(orderItem,refundStatus,refundRecordItem.getRealRefundTotal().negate(),-refundRecordItem.getRealRefundNum(),commonService);
|
}
|
|
/**一级子单-处理促销*/
|
private static void handRefundPromotion(RefundRecordItem refundRecordItem, RefundRecord refundRecord, CommonService commonService) {
|
|
//查看订单信息
|
OrderItem orderItem = commonService.selectOneByKey(OrderItemMapper.class,refundRecordItem.getOrderItemId());
|
if(orderItem == null ){
|
throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"未找到子订单信息[84]!");
|
}
|
|
SqlSentence sqlSentence = new SqlSentence();
|
Map<String, Object> map = new HashMap<>();
|
|
//获取退款二级单
|
map.put("refundRecordItemId",refundRecordItem.getId());
|
sqlSentence.sqlSentence("select * from refund_record_item_source where isDel = 0 AND refundRecordItemId = #{m.refundRecordItemId}",map);
|
List<RefundRecordItemSource> sons = commonService.selectList(RefundRecordItemSourceMapper.class, sqlSentence);
|
|
for (RefundRecordItemSource son : sons) {
|
switch (GroupTypeEnum.getCode(son.getType())){
|
case PROJECT:
|
projectHandleTwo(refundRecord,refundRecordItem, son,orderItem.getUserCardId(),commonService);
|
break;
|
case RETAIL:
|
rerailHandleTwo(refundRecord,refundRecordItem,son,orderItem.getUserCardId(),commonService);
|
break;
|
case INCREMENT:
|
//增值金
|
//判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
|
if(son.getRealRefundTotal().negate().compareTo(BigDecimal.ZERO)!=0){
|
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);
|
}
|
break;
|
case STORED:
|
//储值金额
|
//判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
|
if(son.getRealRefundTotal().negate().compareTo(BigDecimal.ZERO)!=0){
|
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);
|
}
|
break;
|
case INTEGRAL:
|
//积分
|
//判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
|
if(son.getRealRefundTotal().negate().compareTo(BigDecimal.ZERO)!=0){
|
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);
|
}
|
break;
|
}
|
|
}
|
|
//获取其子项
|
map.put("orderItemId",orderItem.getId());
|
sqlSentence.sqlSentence("select refundStatus from order_item_source WHERE orderItemId=#{m.orderItemId} and isDel=0",map);
|
List<OrderItemSon> orderItemSonList=commonService.selectList(OrderItemSonMapper.class,sqlSentence);
|
int refundStatus;
|
Integer refundNum = 0;
|
List<Integer> collect = orderItemSonList.stream().map(OrderItemSon::getRefundStatus).collect(Collectors.toList());
|
|
if(collect.contains(OrderTotalConstants.STATUS_REFUND_PART)){
|
refundStatus = OrderTotalConstants.STATUS_REFUND_PART;
|
}else if (collect.contains(OrderTotalConstants.STATUS_REFUND_NONE)){
|
if(collect.contains(OrderTotalConstants.STATUS_REFUND_FINSH)){
|
refundStatus = OrderTotalConstants.STATUS_REFUND_PART;
|
}else{
|
refundStatus = OrderTotalConstants.STATUS_REFUND_NONE;
|
}
|
}else if (collect.contains(OrderTotalConstants.STATUS_REFUND_FINSH)){
|
refundStatus = OrderTotalConstants.STATUS_REFUND_FINSH;
|
refundNum = orderItem.getBuyNum();
|
}else {
|
refundStatus = OrderTotalConstants.STATUS_REFUND_NONE;
|
}
|
|
//更新子订单信息
|
PartialRefundUtil.updateOrderItemOne(orderItem,refundStatus,refundRecordItem.getRealRefundTotal().negate(),-refundNum,commonService);
|
|
}
|
|
/**二级子订单-项目类型
|
* @param refundRecord 退款总记录
|
* @param refundRecordItem 退款一级子单
|
* @param refundRecordItemSource 退款二级子单
|
* @param userCardId 用户卡包标识
|
* @param commonService 映射
|
*/
|
private static void projectHandleTwo(RefundRecord refundRecord,RefundRecordItem refundRecordItem, RefundRecordItemSource refundRecordItemSource
|
,String userCardId,CommonService commonService) {
|
|
//判断操作完了去修改子订单状态
|
OrderItemSon orderItemSon = commonService.selectOneByKey(OrderItemSonMapper.class,refundRecordItemSource.getOrderItemSonId());
|
if (orderItemSon==null){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到二级子订单信息[02]");
|
}
|
//计算子单是否还有剩余的可扣疗程数
|
if(refundRecordItemSource.getRefundNum() > orderItemSon.getHasReNum()){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,orderItemSon.getGoodsName()+"["+orderItemSon.getGoodsNo()+"]"+"作废退款次数大于已退次数[56]!");
|
}
|
|
UserProjectItem userProjectItem = PartialRefundUtil.getUserProject(orderItemSon.getId(),commonService);
|
if(userProjectItem == null){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"用户项目获取失败[06]!");
|
}
|
//比较作废退款数量
|
if(refundRecordItemSource.getRefundNum() > userProjectItem.getCancelNum()){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,orderItemSon.getGoodsName()+"["+orderItemSon.getGoodsNo()+"]"+"作废退款次数大于用户项目已退次数[56]!");
|
}
|
|
//处理用户项目,减去用户项目数量
|
UserProjectTool.userProjectDeduction(userProjectItem,UserProjectUsedCon.USED_METHOD_ORDER_REFUND,UserProjectUsedCon.USED_TYPE_RETURN,refundRecordItemSource.getDeductionTotalUser()
|
,refundRecordItemSource.getId(),refundRecordItemSource.getRealRefundNum(),refundRecord.getInvalidAppId(),refundRecord.getInvalidAppName(),refundRecord.getRefundShopId(),refundRecord.getRefundShopName(),"员工备注:"+refundRecord.getInvalidRemarks(),commonService);
|
|
int refundStatus;
|
if(orderItemSon.getHasReNum().equals(refundRecordItemSource.getRefundNum())){
|
refundStatus=OrderTotalConstants.STATUS_REFUND_NONE;
|
}else{
|
refundStatus=OrderTotalConstants.STATUS_REFUND_PART;
|
}
|
|
//更新子单信息
|
PartialRefundUtil.updateOrderItemTwo(orderItemSon,refundStatus,refundRecordItemSource.getRealRefundTotal().negate(),-refundRecordItemSource.getRealRefundNum(),commonService);
|
|
if(refundRecordItem.getType().equals(OrderItemConstants.CARD_BAG)){
|
//是卡包的,找回卡包使用记录
|
userCardUsedHandle(userCardId,orderItemSon.getCardItemInfoId(),orderItemSon.getId(),orderItemSon.getOrderId(),refundRecordItemSource.getRefundNum(),commonService);
|
}
|
|
}
|
|
/**二级订单-商品/药品
|
* @param refundRecord 退款总记录
|
* @param refundRecordItem 退款一级子单
|
* @param refundRecordItemSource 退款二级子单
|
* @param commonService 映射
|
* @return 全局携带参数结构
|
*/
|
private static void rerailHandleTwo(RefundRecord refundRecord,RefundRecordItem refundRecordItem, RefundRecordItemSource refundRecordItemSource
|
,String userCardId,CommonService commonService) {
|
|
//判断操作完了去修改子订单状态
|
OrderItemSon orderItemSon=commonService.selectOneByKey(OrderItemSonMapper.class,refundRecordItemSource.getOrderItemSonId());
|
if (orderItemSon==null){
|
throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"未找到二级子订单信息");
|
}
|
|
//计算子单是否还有剩余的可扣疗程数
|
if(refundRecordItemSource.getRefundNum() > orderItemSon.getHasReNum()){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"作废退款次数大于已退次数[84]!");
|
}
|
|
int refundStatus;
|
if(orderItemSon.getHasReNum().equals(refundRecordItemSource.getRefundNum())){
|
refundStatus=OrderTotalConstants.STATUS_REFUND_NONE;
|
}else{
|
refundStatus=OrderTotalConstants.STATUS_REFUND_PART;
|
}
|
|
PartialRefundUtil.updateOrderItemTwo(orderItemSon,refundStatus,refundRecordItemSource.getRealRefundTotal().negate(),-refundRecordItemSource.getRealRefundNum(),commonService);
|
|
if(refundRecordItem.getType().equals(OrderItemConstants.CARD_BAG)){
|
//是卡包的,刪除卡包使用
|
userCardUsedHandle(userCardId,orderItemSon.getCardItemInfoId(),orderItemSon.getId(),orderItemSon.getOrderId(),refundRecordItemSource.getRefundNum(),commonService);
|
}
|
|
}
|
|
/**卡包使用记录-状态变化
|
* @param userCardId 用户卡包
|
* @param cardItemInfoId 卡包的卡项子项
|
* @param sourceId 卡包使用记录来源标识
|
* @param orderId 卡包使用记录来源总表标识
|
* @param refundNum 退款数量
|
* @param commonService 映射
|
*/
|
public static void userCardUsedHandle(String userCardId,String cardItemInfoId,String sourceId,String orderId,Integer refundNum
|
,CommonService commonService){
|
|
//查出用户,commonId:卡项的组合项标识
|
CardItemInfo cardItemInfo = commonService.selectOneByKey(CardItemInfoMapper.class,cardItemInfoId);
|
if(cardItemInfo == null){
|
logger.error("未找到该卡包的组合项-卡包作废退款失败,userCardId:{},cardItemInfoId:{},sourceId:{},orderId:{},refundNum:{}",userCardId,cardItemInfoId,sourceId,orderId,refundNum);
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到该卡包的组合项");
|
}
|
|
//获取权益类型
|
CardEquity cardEquity = commonService.selectOneByKey(CardEquityMapper.class,cardItemInfo.getCardEquityId());
|
if(cardEquity == null){
|
logger.error("未找到该卡包的组合项权益类型-卡包作废退款失败,userCardId:{},cardItemInfoId:{},sourceId:{},orderId:{},refundNum:{}",userCardId,cardItemInfoId,sourceId,orderId,refundNum);
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到该卡包的组合项权益类型");
|
}
|
|
SqlSentence sqlSentence = new SqlSentence();
|
Map<String,Object> map = new HashMap<>();
|
|
//获取作废使用记录
|
map.put("cardItemInfoId",cardItemInfoId);
|
map.put("sourceId",sourceId);
|
map.put("userCardId",userCardId);
|
sqlSentence.sqlSentence(" SELECT * FROM user_card_used WHERE isDel = 1 AND userCardId = #{m.userCardId}" +
|
" AND cardItemInfoId = #{m.cardItemInfoId} AND sourceId = #{m.sourceId}",map);
|
List<UserCardUsed> userCardUsedList = commonService.selectList(UserCardUsedMapper.class,sqlSentence);
|
|
//因为之前的使用记录有些没有关联到订单,所以找不到
|
if(userCardUsedList == null || userCardUsedList.size() == 0){
|
map.clear();
|
map.put("cardItemInfoId",cardItemInfoId);
|
map.put("userCardId",userCardId);
|
map.put("orderId",orderId);
|
sqlSentence.sqlSentence(" SELECT * FROM user_card_used WHERE isDel = 1 AND userCardId = #{m.userCardId}" +
|
" AND cardItemInfoId = #{m.cardItemInfoId} AND orderId = #{m.orderId} AND sourceId IS NULL",map);
|
userCardUsedList = commonService.selectList(UserCardUsedMapper.class,sqlSentence);
|
if(userCardUsedList.size() == 0){
|
sqlSentence.sqlSentence(" SELECT * FROM user_card_used WHERE isDel = 1 AND userCardId = #{m.userCardId}" +
|
" AND cardItemInfoId = #{m.cardItemInfoId} AND sourceId IS NULL",map);
|
userCardUsedList = commonService.selectList(UserCardUsedMapper.class,sqlSentence);
|
}
|
}
|
|
//计算卡次
|
int returnNum;
|
//因为有些是算次数抵扣的,所以要算
|
if(CardEquity.EQUITY_TYPE_TIME_CARD.equals(cardEquity.getEquityType())){
|
//次卡:当前需要使用总次数 = 开单的购买数量*每次划扣的次数
|
returnNum= refundNum * cardItemInfo.getEveryDrawNum();
|
}else if(CardEquity.EQUITY_TYPE_N_M.equals(cardEquity.getEquityType())){
|
returnNum = refundNum;
|
}else{
|
returnNum = refundNum;
|
}
|
|
if(returnNum > userCardUsedList.size()){
|
logger.error("卡包可退数量错误-卡包作废退款失败,查询语句:{},传入参数:{}",sqlSentence.getSqlSentence(), JSON.toJSONString(sqlSentence.getM()));
|
logger.error("卡包可退数量错误-卡包作废退款失败,userCardId:{},cardItemInfoId:{},sourceId:{},orderId:{},refundNum:{},returnNum:{}",userCardId,cardItemInfoId,sourceId,orderId,refundNum,returnNum);
|
throw new TipsException("卡包作废退款次数量大于可退次数!");
|
}
|
|
//已经操作数量
|
int opNum = 0;
|
StringBuilder sql;
|
for(UserCardUsed userCardUsed:userCardUsedList){
|
//判断是否够了,跳出循环
|
if(opNum == returnNum){
|
break;
|
}
|
sql = new StringBuilder();
|
sql.append("isDel = 0");
|
map.clear();
|
map.put("id",userCardUsed.getId());
|
if(StringUtils.isEmpty(userCardUsed.getOrderId())){
|
sql.append(",orderId = #{m.orderId}");
|
map.put("orderId",orderId);
|
}
|
if(StringUtils.isEmpty(userCardUsed.getSourceId())){
|
sql.append(",sourceId = #{m.sourceId}");
|
map.put("sourceId",sourceId);
|
}
|
sql.append(" WHERE id = #{m.id} AND isDel = 1");
|
sqlSentence.sqlUpdate(sql.toString(),map);
|
opNum = opNum+commonService.updateWhere(UserCardUsedMapper.class,sqlSentence);
|
}
|
if(opNum != returnNum){
|
logger.error("卡包回退数量错误-卡包作废退款失败,userCardId:{},cardItemInfoId:{},sourceId:{},orderId:{},refundNum:{},returnNum:{},opNum:{}",userCardId,cardItemInfoId,sourceId,orderId,refundNum,returnNum,opNum);
|
throw new TipsException("卡包作废回退数量错误!");
|
}
|
|
}
|
|
/**一级子单-处理卡项
|
* @param refundRecord 退款总记录
|
* @param refundRecordItem 退款一级记录
|
* @param commonService 映射
|
* @return 全局携带参数结构
|
*/
|
public static void cardHandleOne(RefundRecord refundRecord, RefundRecordItem refundRecordItem,CommonService commonService) {
|
|
//判断操作完了去修改子订单状态
|
OrderItem orderItem=commonService.selectOneByKey(OrderItemMapper.class,refundRecordItem.getOrderItemId());
|
if (orderItem==null){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到子订单信息");
|
}
|
|
if(refundRecordItem.getRefundNum() > orderItem.getHasReNum()){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"作废退款-卡包提示:作废卡包退款数量大于已退款数量[020]");
|
}
|
|
SqlSentence sqlSentence = new SqlSentence();
|
Map<String,Object> values = new HashMap<>();
|
|
|
//找到已退款卡包
|
values.put("status",RefundRecordCard.YES);
|
values.put("refundRecordItemId",refundRecordItem.getId());
|
sqlSentence.sqlSentence("SELECT * FROM refund_record_card WHERE isDel = 0 AND status = #{m.status} AND refundRecordItemId = #{m.refundRecordItemId}",values);
|
List<RefundRecordCard> refundRecordCardList = commonService.selectList(RefundRecordCardMapper.class,sqlSentence);
|
if(refundRecordItem.getRefundNum() > refundRecordCardList.size()){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"作废退款-卡包提示:作废卡包退款数量大于已退款数量[021]");
|
}
|
|
//变更卡项状态
|
RefundRecordCard refundRecordCard;
|
for (int i = 0; i < refundRecordItem.getRefundNum(); i++) {
|
refundRecordCard= refundRecordCardList.get(i);
|
values.clear();
|
values.put("id",refundRecordCard.getUserCardId());
|
values.put("statusOld", UserCard.TYPE_NO_USED);
|
values.put("effectiveStatus", UserProjectConstants.EFF_STATUS_YES);
|
values.put("oldEffectiveStatus", UserProjectConstants.EFF_STATUS_CANCEL);
|
sqlSentence.sqlSentence(" effectiveStatus=#{m.effectiveStatus} WHERE id = #{m.id} AND effectiveStatus = #{m.oldEffectiveStatus} AND status = #{m.statusOld}",values);
|
if(commonService.updateWhere(UserCardMapper.class,sqlSentence) != 1){
|
throw new TipsException("用户卡包状态已发生变化,请重试!");
|
}
|
|
values.clear();
|
values.put("id",refundRecordCard.getId());
|
values.put("status",RefundRecordCard.NO);
|
values.put("statusOld",RefundRecordCard.YES);
|
sqlSentence.sqlSentence("status = #{m.status} WHERE id = #{m.id} AND status = #{m.statusOld}",values);
|
if(commonService.updateWhere(RefundRecordCardMapper.class,sqlSentence) != 1){
|
throw new TipsException("退款卡包状态已发生变化,请重试!");
|
}
|
}
|
|
int refundStatus;
|
if (orderItem.getHasReNum().equals(refundRecordItem.getRefundNum())){
|
refundStatus = OrderTotalConstants.STATUS_REFUND_NONE;
|
}else {
|
refundStatus = OrderTotalConstants.STATUS_REFUND_PART;
|
|
}
|
//更新子订单信息
|
PartialRefundUtil.updateOrderItemOne(orderItem,refundStatus,refundRecordItem.getRealRefundTotal().negate(),-refundRecordItem.getRealRefundNum(),commonService);
|
}
|
|
}
|