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.constants.tool.TimerHandleTool;
|
import com.hx.phiappt.model.BaseEntity;
|
import com.hx.phiappt.model.TimerHandleItem;
|
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.cardItem.CardEquity;
|
import com.hx.phiappt.model.cardItem.CardItemInfo;
|
import com.hx.phiappt.model.consume.ConsumePay;
|
import com.hx.phiappt.model.consume.ConsumePayItem;
|
import com.hx.phiappt.model.consume.ConsumePayItemSon;
|
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.deduction.DeductionSingleService;
|
import com.hx.phip.service.order.impl.OrderRefundServiceImpl;
|
import com.hx.phip.tool.order.OrderTool;
|
import com.hx.phip.tool.user.UserCardTool;
|
import com.hx.phip.tool.user.UserProjectTool;
|
import com.hx.phip.util.api.UserMoneyUtil;
|
import com.hx.phip.vo.order.refund.RefundCarryVo;
|
import com.hx.phip.vo.user.UserProjectDeductionVo;
|
import com.hx.util.StringUtils;
|
import com.hz.his.dto.deduction.DeductionDto;
|
import com.platform.exception.PlatTipsException;
|
import com.platform.resultTool.PlatformCode;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.beans.BeanUtils;
|
|
import java.math.BigDecimal;
|
import java.math.RoundingMode;
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
/**
|
* @Author
|
*/
|
public class PartialRefundUtil {
|
|
/**log4j日志*/
|
private static final Logger logger = LoggerFactory.getLogger(OrderRefundServiceImpl.class.getName());
|
|
/**有子项退款的类型*/
|
public static Set<String> CONTAIN_SON_TYPE_SET;
|
|
/**领建优惠券标识*/
|
public static final String HIS_COUPON_CODE = "his_coupon_code";
|
|
static {
|
CONTAIN_SON_TYPE_SET = new HashSet<>();
|
CONTAIN_SON_TYPE_SET.add(OrderItemConstants.TYPE_PROMOTION);
|
CONTAIN_SON_TYPE_SET.add(OrderItemConstants.CARD_BAG);
|
}
|
|
/**
|
* 退款总流程工具
|
* @param commonService 映射
|
* @param operationId 操作人标识
|
* @param operationNme 操作人名称
|
* @param refundId 退款总单标识
|
*/
|
public static OrdersTotal refundProcess(CommonService commonService,DeductionSingleService deductionSingleService
|
, String operationId, String operationNme, String refundId) {
|
|
RefundRecord refundRecord = commonService.selectOneByKeyBlob(RefundRecordMapper.class,refundId);
|
if(refundRecord ==null){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"找不到该退款信息!");
|
}
|
|
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("refundStatus", RefundStatus.STATUS_SUCC_REFUND);
|
values.put("createTime", new Date());
|
values.put("oldRefundStatus", RefundStatus.STATUS_APPLY_REFUND);
|
values.put("isDel", BaseEntity.NO);
|
values.put("id",refundRecord.getId());
|
sqlSentence.sqlSentence(" refundStatus=#{m.refundStatus},createTime = #{m.createTime}" +
|
" WHERE id = #{m.id} AND isDel=#{m.isDel} AND refundStatus = #{m.oldRefundStatus}",values);
|
|
if(commonService.updateWhere(RefundRecordMapper.class,sqlSentence) != 1){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"操作失败,退款单状态已改变!");
|
}
|
|
//全程使用携带参数对象
|
RefundCarryVo refundCarryVo = new RefundCarryVo();
|
refundCarryVo.setRefundRecordMethodList(refundRecordMethodList);
|
|
//获取订单信息
|
OrdersTotal ordersTotal = commonService.selectOneByKeyBlob(OrdersTotalMapper.class,refundRecord.getOrderId());
|
if(ordersTotal == null ){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到订单信息!");
|
}
|
|
//查询用户是否有账户信息,退款需要处理资金
|
values.clear();
|
values.put("userId",refundRecord.getUserId());
|
sqlSentence.sqlSentence("SELECT * FROM user_money WHERE userId=#{m.userId} AND isDel=0",values);
|
UserMoney userMoney=commonService.selectOne(UserMoneyMapper.class,sqlSentence);
|
if(userMoney==null){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到该用户的资金信息");
|
}
|
|
if(OrderTotalConstants.TYPE_RECHARGE.equals(ordersTotal.getType())){
|
//处理支付方式和退款方式关联
|
refundCarryVo = rechargeRefundMothedHandle(refundCarryVo,refundRecord,commonService);
|
}else{
|
//子单退款项处理
|
refundCarryVo = numberOfRefunds(refundCarryVo,operationId, refundRecord,ordersTotal,commonService,deductionSingleService);
|
}
|
//处理优惠券
|
handCoupon(refundRecord,commonService);
|
//处理总退款方式数据
|
refundCarryVo = refundRecordMotnedHandle(refundCarryVo,operationId,operationNme,refundRecord,ordersTotal,commonService);
|
|
//更改总订单退款状态
|
values.clear();
|
values.put("orderId",ordersTotal.getId());
|
sqlSentence.sqlSentence("select * from order_item WHERE orderId=#{m.orderId} and isDel=0",values);
|
List<OrderItem> orderItemList=commonService.selectList(OrderItemMapper.class,sqlSentence);
|
|
values.clear();
|
values.put("oldStatus",ordersTotal.getStatus());
|
values.put("oldRefundStatus",ordersTotal.getRefundStatus());
|
|
List<Integer> collect = orderItemList.stream().map(OrderItem::getRefundStatus).collect(Collectors.toList());
|
if(collect.contains(OrderTotalConstants.STATUS_REFUND_PART)){
|
ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_PART);
|
}else if (collect.contains(OrderTotalConstants.STATUS_REFUND_NONE) && collect.contains(OrderTotalConstants.STATUS_REFUND_FINSH)){
|
ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_PART);
|
}else if (collect.contains(OrderTotalConstants.STATUS_REFUND_NONE)){
|
ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_NONE);
|
}else if (collect.contains(OrderTotalConstants.STATUS_REFUND_FINSH)){
|
ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_FINSH);
|
ordersTotal.setStatus(OrderTotalConstants.STATUS_CANCEL);
|
}else {
|
if(OrderTotalConstants.TYPE_RECHARGE.equals(ordersTotal.getType())){
|
if(ordersTotal.getActualTotal().compareTo(refundRecord.getRefundTotal()) <= 0){
|
ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_FINSH);
|
ordersTotal.setStatus(OrderTotalConstants.STATUS_CANCEL);
|
}else{
|
ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_PART);
|
}
|
}else{
|
ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_NONE);
|
}
|
}
|
ordersTotal.setIsSyncOrder(BaseEntity.NO);
|
|
values.put("isSyncOrder",BaseEntity.NO);
|
values.put("status",ordersTotal.getStatus());
|
values.put("refundStatus",ordersTotal.getRefundStatus());
|
values.put("reTotal",refundRecord.getRealRefundTotal());
|
values.put("reCashTotal",refundRecord.getCashTotal());
|
values.put("reCashPurenessTotal",refundRecord.getCashPurenessTotal());
|
values.put("reRechargeTotal",refundRecord.getRechargeTotal());
|
values.put("reIncrementTotal",refundRecord.getIncrementTotal());
|
values.put("reIntegralTotal",refundRecord.getRealRefundIntegral());
|
values.put("id",ordersTotal.getId());
|
sqlSentence.sqlUpdate("isSyncOrder = #{m.isSyncOrder},status = #{m.status},refundStatus = #{m.refundStatus},reTotal = reTotal+#{m.reTotal}" +
|
",reCashTotal = reCashTotal+#{m.reCashTotal},reCashPurenessTotal = reCashPurenessTotal+#{m.reCashPurenessTotal},reRechargeTotal = #{m.reRechargeTotal}" +
|
",reIncrementTotal = reIncrementTotal+#{m.reIncrementTotal},reIntegralTotal = reIntegralTotal+#{m.reIntegralTotal}" +
|
" WHERE id = #{m.id} AND status = #{m.oldStatus} AND refundStatus = #{m.oldRefundStatus}",values);
|
if(commonService.updateWhere(OrdersTotalMapper.class,sqlSentence) != 1){
|
throw new TipsException("操作失败,订单状态已发生改变!");
|
}
|
|
//爆款活动规则退款,暂时不屏蔽保存提示
|
activityPageGiveIncrease(ordersTotal,refundRecord,0,commonService);
|
|
//初始化初复诊信息
|
if(OrderTotalConstants.STATUS_CANCEL == ordersTotal.getStatus()){
|
// OrderUtil.reCalcOrderBothTheOneData(commonService, VisitRecordMapper.class,null,ordersTotal.getUserId(), BaseEntity.NO);
|
// 处理方法调整
|
TimerHandleTool.addTask(commonService, TimerHandleItem.TYPE_RECALC_ORDER_BOTH, ordersTotal.getUserId(), null);
|
}
|
|
return ordersTotal;
|
}
|
|
/**处理总退款方式数据*/
|
public static RefundCarryVo refundRecordMotnedHandle(RefundCarryVo refundCarryVo,String operationId,String operationNme
|
,RefundRecord refundRecord,OrdersTotal ordersTotal,CommonService commonService){
|
SqlSentence sqlSentence = new SqlSentence();
|
Map<String,Object> map = new HashMap<>();
|
|
//获取总退款方式
|
List<RefundRecordMethod> refundRecordMethodList = refundCarryVo.getRefundRecordMethodList();
|
//退款的支付方式记录数据
|
List<RefundRecordConsumePay> refundConsumePayList = refundCarryVo.getRefundConsumePayList();
|
//通过支付编号,装载分组好支付方式记录,key值:支付方式编号,value:支付方式记录集合
|
Map<String,List<RefundRecordConsumePay>> refundConsumePayMap= new HashMap<>();
|
List<RefundRecordConsumePay> refundRecordConsumePays;
|
for(RefundRecordConsumePay refundRecordConsumePay:refundConsumePayList){
|
refundRecordConsumePays = refundConsumePayMap.computeIfAbsent(refundRecordConsumePay.getNumberNo(),k->new ArrayList<>());
|
refundRecordConsumePays.add(refundRecordConsumePay);
|
}
|
|
List<RefundRecordConsumePay> refundRecordConsumePayList = new ArrayList<>();
|
RefundRecordConsumePay refundRecordConsumePay1;
|
|
for(RefundRecordMethod refundRecordMethod:refundRecordMethodList){
|
if(refundRecordMethod.getActualTotal().compareTo(BigDecimal.ZERO) < 1){
|
continue;
|
}
|
//判断是否已经被分配完
|
if(refundRecordMethod.getpTotal().compareTo(BigDecimal.ZERO) > 0){
|
throw new TipsException("退款错误[20]!");
|
}
|
refundRecordConsumePays = refundConsumePayMap.get(refundRecordMethod.getNumberNo());
|
if(refundRecordConsumePays == null){
|
throw new TipsException("退款错误[21]!");
|
}
|
for(RefundRecordConsumePay refundRecordConsumePay:refundRecordConsumePays){
|
//更新支付记录退款信息
|
map.clear();
|
map.put("refundTotal",refundRecordConsumePay.getRefundTotal());
|
map.put("id",refundRecordConsumePay.getConsumePayId());
|
sqlSentence.sqlUpdate("refundTotal = refundTotal + #{m.refundTotal} WHERE id = #{m.id} AND actualTotal >= refundTotal + #{m.refundTotal}",map);
|
if(commonService.updateWhere(ConsumePayMapper.class,sqlSentence) != 1){
|
throw new TipsException("退款金额错误[33]!");
|
}
|
|
//生成关联记录
|
refundRecordConsumePay1 = insertRefundRecordConsumePay(refundRecordConsumePay.getRefundTotal(),null,null,refundRecord.getOrderId(),refundRecordConsumePay.getNumberNo()
|
,refundRecordConsumePay.getName(),refundRecordConsumePay.getIsMoneyPay(),refundRecordConsumePay.getIsExecute(),refundRecordConsumePay.getConsumePayId(),refundRecordMethod.getId(),null,refundRecord.getId(),commonService);
|
refundRecordConsumePayList.add(refundRecordConsumePay1);
|
}
|
//修改实际退款金额
|
map.put("realRefundTotal",refundRecordMethod.getActualTotal());
|
map.put("id",refundRecordMethod.getId());
|
sqlSentence.sqlUpdate(" realRefundTotal = realRefundTotal + #{m.realRefundTotal} where id = #{m.id}",map);
|
if(commonService.updateWhere(RefundRecordMethodMapper.class,sqlSentence) != 1){
|
throw new TipsException("更新退款方式信息错误[001]");
|
}
|
|
}
|
refundCarryVo.setRefundConsumePayList(refundRecordConsumePayList);
|
|
//处理回退到账
|
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(),OrderRefundCancelTool.REFUND_REMARK+"["+ordersTotal.getOrderNo()+"]","操作原因:退款审核通过,退款方式:退回储值金",operationId,refundRecord.getOrderId(),refundRecord.getOperatorAppCode(),refundRecord.getId(),refundRecordMethod.getActualTotal(), UserMoneyUnclaimed.FUND_TYPE_STORED_VALUE_FUND, OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.NO);
|
OrderLog orderLog = RefundToolUtil.setOrderLog(refundRecord, operationId, operationNme, refundRecordMethod.getName()+"退款金额:"+refundRecordMethod.getActualTotal(), 0, OrderLogConstants.LOG_TYPE_REFUND);
|
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(),OrderRefundCancelTool.REFUND_REMARK+"["+ordersTotal.getOrderNo()+"]","操作原因:退款审核通过,退款方式:退回增值金",operationId,refundRecord.getOrderId(),refundRecord.getOperatorAppCode(),refundRecord.getId(),refundRecordMethod.getActualTotal(), UserMoneyUnclaimed.FUND_TYPE_VALUE_ADDED_FUND, OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES);
|
OrderLog orderLog = RefundToolUtil.setOrderLog(refundRecord,operationId,operationNme,refundRecordMethod.getName()+"退款金额:"+refundRecordMethod.getActualTotal(),0, OrderLogConstants.LOG_TYPE_REFUND);
|
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(),OrderRefundCancelTool.REFUND_REMARK+"["+ordersTotal.getOrderNo()+"]","操作原因:退款审核通过,退款方式:退回积分",operationId,refundRecord.getOrderId(),refundRecord.getOperatorAppCode(),refundRecord.getId(),refundRecordMethod.getActualTotal(), UserMoneyUnclaimed.FUND_TYPE_INTEGRAL,OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES);
|
OrderLog orderLog = RefundToolUtil.setOrderLog(refundRecord,operationId,operationNme,refundRecordMethod.getName()+"退款金额:"+refundRecordMethod.getActualTotal(),0, OrderLogConstants.LOG_TYPE_REFUND);
|
commonService.insert(OrderLogMapper.class,orderLog);
|
}
|
}else {
|
if(refundRecordMethod.getActualTotal().compareTo(BigDecimal.ZERO)<1){
|
continue;
|
}
|
OrderLog orderLog = RefundToolUtil.setOrderLog(refundRecord,operationId,operationNme,refundRecordMethod.getName()+"退款金额:"+refundRecordMethod.getActualTotal(),0, OrderLogConstants.LOG_TYPE_REFUND);
|
orderLog.setOrderId(ordersTotal.getId());
|
commonService.insert(OrderLogMapper.class,orderLog);
|
}
|
}
|
return refundCarryVo;
|
}
|
|
/**充值订单退款方式的支付方式记录处理*/
|
public static RefundCarryVo rechargeRefundMothedHandle(RefundCarryVo refundCarryVo,RefundRecord refundRecord,CommonService commonService){
|
|
SqlSentence sqlSentence = new SqlSentence();
|
Map<String,Object> map = new HashMap<>();
|
|
List<RefundRecordMethod> refundRecordMethodList = refundCarryVo.getRefundRecordMethodList();
|
|
//装载分配好的支付方式记录的金额数据
|
List<RefundRecordConsumePay> refundConsumePayList = new ArrayList<>();
|
RefundRecordConsumePay refundRecordConsumePay;
|
|
///////获取订单支付方式记录
|
map.put("orderId",refundRecord.getOrderId());
|
sqlSentence.sqlSentence("SELECT *,ROUND(actualTotal-refundTotal,2) AS pTotal FROM consume_pay WHERE isDel = 0 AND orderId = #{m.orderId} ORDER BY pTotal ASC",map);
|
List<ConsumePay> consumePayList = commonService.selectList(ConsumePayMapper.class,sqlSentence);
|
//根据支付编号进行区分,key值:支付编号
|
Map<String,List<ConsumePay>> consumePayMap = new HashMap<>();
|
List<ConsumePay> consumePays;
|
for(ConsumePay consumePay:consumePayList){
|
consumePays = consumePayMap.computeIfAbsent(consumePay.getNumberNo(),k->new ArrayList<>());
|
consumePays.add(consumePay);
|
}
|
|
////引用对象
|
//退款方式的金额
|
BigDecimal mothedTotal;
|
//分配支付方式金额
|
BigDecimal mothedCutTotal;
|
for(RefundRecordMethod refundRecordMethod:refundRecordMethodList){
|
if(refundRecordMethod.getActualTotal().compareTo(BigDecimal.ZERO) < 1){
|
continue;
|
}
|
mothedTotal = refundRecordMethod.getActualTotal();
|
consumePays = consumePayMap.get(refundRecordMethod.getNumberNo());
|
if(consumePays == null){
|
throw new TipsException("没有找到该支付记录:"+refundRecordMethod.getName()+"["+refundRecordMethod.getNumberNo()+"]");
|
}
|
for(ConsumePay consumePay:consumePays){
|
if(consumePay.getpTotal().compareTo(BigDecimal.ZERO) < 1){
|
continue;
|
}
|
//计算扣减金额
|
if(consumePay.getpTotal().compareTo(mothedTotal) > 0){
|
mothedCutTotal = mothedTotal;
|
}else{
|
mothedCutTotal = consumePay.getpTotal();
|
}
|
//减去已经分配的金额
|
mothedTotal = mothedTotal.subtract(mothedCutTotal).setScale(2, RoundingMode.HALF_UP);
|
|
refundRecordConsumePay = new RefundRecordConsumePay();
|
refundRecordConsumePay.setNumberNo(consumePay.getNumberNo());
|
refundRecordConsumePay.setName(consumePay.getName());
|
refundRecordConsumePay.setRefundTotal(mothedCutTotal);
|
refundRecordConsumePay.setConsumePayId(consumePay.getId());
|
refundRecordConsumePay.setIsMoneyPay(consumePay.getIsMoneyPay());
|
refundRecordConsumePay.setIsExecute(consumePay.getIsPay());
|
refundConsumePayList.add(refundRecordConsumePay);
|
|
//已经分配完成,跳出循环
|
if(mothedTotal.compareTo(BigDecimal.ZERO) < 1){
|
//跳出循环
|
break;
|
}
|
}
|
//判断退款金额是否已经全部分配
|
if(mothedTotal.compareTo(BigDecimal.ZERO) > 0){
|
throw new TipsException("退款金额错误[426]!");
|
}
|
refundRecordMethod.setpTotal(BigDecimal.ZERO);
|
}
|
refundCarryVo.setRefundConsumePayList(refundConsumePayList);
|
return refundCarryVo;
|
}
|
|
/**处理一级子退款方式数据
|
* @param refundRecord 退款记录总表
|
* @param refundRecordItem 退款记录子表
|
* @param refundCarryVo 总携带参数结构
|
* @param commonService 映射
|
* @return 总携带参数结构
|
*/
|
public static RefundCarryVo refundRecordMotnedItemHandle(RefundRecord refundRecord,RefundRecordItem refundRecordItem
|
,RefundCarryVo refundCarryVo,CommonService commonService){
|
|
//没有退款方式,跳过当前处理,因为这里是处理退款方式金额的,如果没有退款金额,那么可以跳过该环节
|
if(refundCarryVo.getRefundRecordMethodList().size() == 0){
|
return refundCarryVo;
|
}
|
//支付方式占比
|
BigDecimal payMothedPercentage;
|
//计算退款方式的占比
|
if(refundRecord.getRefundTotal().compareTo(BigDecimal.ZERO) > 0){
|
payMothedPercentage = refundRecordItem.getRefundMoney().divide(refundRecord.getRefundTotal(),15,RoundingMode.HALF_UP);
|
}else{
|
payMothedPercentage = BigDecimal.ZERO;
|
}
|
|
//处理退款方式,存储信息
|
refundCarryVo = insertRefundItemMothed(refundRecordItem.getId(),refundRecordItem.getRefundMoney(),refundRecordItem.getRefundIntegral(),payMothedPercentage,OrderSourceConstans.TYPE_PROJECT
|
,refundRecordItem.getOrderItemId(),refundCarryVo,refundRecord,commonService);
|
|
//更新退款子单
|
updateRefundItem(refundCarryVo,refundRecordItem.getId(),commonService);
|
|
return refundCarryVo;
|
}
|
|
/**更新退款一级子单的信息
|
* @param refundItemId 退款记录一级子单标识
|
* @param commonService 映射
|
*/
|
public static void updateRefundItem(RefundCarryVo refundCarryVo
|
,String refundItemId,CommonService commonService){
|
SqlSentence sqlSentence = new SqlSentence();
|
Map<String,Object> values = new HashMap<>();
|
|
//更新退款子单的信息,更新现金和划扣金额保存
|
values.put("realRefundTotal",refundCarryVo.getRefundTotal());
|
values.put("realRefundIntegral",refundCarryVo.getIntegralTotal());
|
values.put("deductionTotal",refundCarryVo.getDeductionTotal());
|
values.put("deductionTotalUser",refundCarryVo.getDeductionTotalUser());
|
values.put("cashTotal",refundCarryVo.getCashTotal());
|
values.put("cashPurenessTotal",refundCarryVo.getCashPurenessTotal());
|
values.put("rechargeTotal",refundCarryVo.getRechargeTotal());
|
values.put("incrementTotal",refundCarryVo.getIncrementTotal());
|
values.put("id",refundItemId);
|
sqlSentence.sqlUpdate("realRefundTotal = #{m.realRefundTotal},realRefundIntegral = #{m.realRefundIntegral},deductionTotal = #{m.deductionTotal},deductionTotalUser = #{m.deductionTotalUser},cashTotal = #{m.cashTotal}" +
|
",cashPurenessTotal = #{m.cashPurenessTotal},rechargeTotal = #{m.rechargeTotal},incrementTotal = #{m.incrementTotal} WHERE id = #{m.id}",values);
|
if(commonService.updateWhere(RefundRecordItemMapper.class,sqlSentence) != 1){
|
throw new TipsException("更新退款子项信息失败!");
|
}
|
}
|
|
/**处理二级子退款方式数据
|
* @param refundRecord 退款记录总表
|
* @param refundRecordItemSource 退款记录子表
|
* @param refundCarryVo 总携带参数结构
|
* @param commonService 映射
|
* @return 总携带参数结构
|
*/
|
public static RefundCarryVo refundRecordMotnedItemTwoHandle(RefundRecord refundRecord,RefundRecordItemSource refundRecordItemSource
|
,RefundCarryVo refundCarryVo,CommonService commonService){
|
|
//装载支付方式退款信息
|
refundCarryVo.setRefundConsumePayList(new ArrayList<>());
|
//没有退款方式,跳过当前处理
|
if(refundCarryVo.getRefundRecordMethodList().size() == 0){
|
return refundCarryVo;
|
}
|
|
SqlSentence sqlSentence = new SqlSentence();
|
Map<String,Object> values = new HashMap<>();
|
|
//支付方式占比
|
BigDecimal payMothedPercentage;
|
//计算退款方式的占比
|
if(refundRecord.getRefundTotal().compareTo(BigDecimal.ZERO) > 0){
|
payMothedPercentage = refundRecordItemSource.getRefundMoney().divide(refundRecord.getRefundTotal(),15,RoundingMode.HALF_UP);
|
}else{
|
payMothedPercentage = BigDecimal.ZERO;
|
}
|
|
//处理退款方式
|
refundCarryVo = insertRefundItemMothed(refundRecordItemSource.getId(),refundRecordItemSource.getRefundMoney(),refundRecordItemSource.getRefundIntegral(),payMothedPercentage,OrderSourceConstans.TYPE_RETAIL
|
,refundRecordItemSource.getOrderItemSonId(),refundCarryVo,refundRecord,commonService);
|
|
values.put("deductionTotal",refundCarryVo.getDeductionTotal());
|
values.put("deductionTotalUser",refundCarryVo.getDeductionTotalUser());
|
values.put("realRefundTotal",refundCarryVo.getRefundTotal());
|
values.put("realRefundIntegral",refundCarryVo.getIntegralTotal());
|
values.put("cashTotal",refundCarryVo.getCashTotal());
|
values.put("cashPurenessTotal",refundCarryVo.getCashPurenessTotal());
|
values.put("rechargeTotal",refundCarryVo.getRechargeTotal());
|
values.put("incrementTotal",refundCarryVo.getIncrementTotal());
|
values.put("id",refundRecordItemSource.getId());
|
sqlSentence.sqlUpdate("deductionTotal = #{m.deductionTotal},deductionTotalUser = #{m.deductionTotalUser},cashTotal = #{m.cashTotal}" +
|
",realRefundTotal = #{m.realRefundTotal},realRefundIntegral = #{m.realRefundIntegral},cashPurenessTotal = #{m.cashPurenessTotal}" +
|
",rechargeTotal = #{m.rechargeTotal},incrementTotal = #{m.incrementTotal} WHERE id = #{m.id}",values);
|
if(commonService.updateWhere(RefundRecordItemSourceMapper.class,sqlSentence) != 1){
|
throw new TipsException("更新退款子项信息失败[94]!");
|
}
|
|
return refundCarryVo;
|
}
|
|
/**退款子单退款方式处理保存
|
* @param refundItemId 退款子单标识
|
* @param refundItemTotal 退款子单实际需要退款总金额
|
* @param refundItemIntegral 退款子单实际需要退款总积分
|
* @param payMothedPercentage 支付方式占比
|
* @param orderItemType 订单子单级别
|
* @param orderItemId 订单子单标识
|
* @param refundCarryVo 总结构对象
|
* @param refundRecord 退款总记录
|
* @param commonService 映射
|
* @return 总结构对象
|
*/
|
public static RefundCarryVo insertRefundItemMothed(String refundItemId,BigDecimal refundItemTotal,BigDecimal refundItemIntegral,BigDecimal payMothedPercentage,String orderItemType
|
,String orderItemId,RefundCarryVo refundCarryVo,RefundRecord refundRecord,CommonService commonService){
|
|
//-----金额和积分区分出来
|
//总退款方式金额数据
|
List<RefundRecordMethod> refundRecordMethodList = new ArrayList<>();
|
//积分
|
RefundRecordMethod refundRecordMethodIntegral = null;
|
for(RefundRecordMethod refundRecordMethod:refundCarryVo.getRefundRecordMethodList()){
|
if(PayMethodTypeConstants.PAY_INTEGRAL.equals(refundRecordMethod.getNumberNo())){
|
refundRecordMethodIntegral = refundRecordMethod;
|
}else{
|
refundRecordMethodList.add(refundRecordMethod);
|
}
|
}
|
|
//进行升序排序,避免后面不够分配
|
refundRecordMethodList = refundRecordMethodList.stream().sorted(Comparator.comparing(RefundRecordMethod::getpTotal)).collect(Collectors.toList());
|
|
//获取子单的支付方式记录,计算可退款金额
|
List<ConsumePayItem> consumePayItemList;
|
if(OrderSourceConstans.TYPE_PROJECT.equals(orderItemType)){
|
//一级订单
|
consumePayItemList = getOrderItemOneConsumePay(orderItemId,commonService);
|
}else{
|
//二级订单
|
consumePayItemList = getOrderItemTwoConsumePay(orderItemId,commonService);
|
}
|
|
////存储支付方式编号的可退款金额,根据支付编号求和,后面的业务判断会用到,key值:支付编号,value:可退金额
|
Map<String,BigDecimal> noMap = new HashMap<>();
|
BigDecimal surplusTotal;
|
////存储支付方式编号的支付方式记录,根据支付编号整合,后面的业务会用到,key值:支付编号,value:支付记录集合
|
Map<String,List<ConsumePayItem>> noPayItemMap = new HashMap<>();
|
List<ConsumePayItem> noPayItemList;
|
|
////////填充支付方式记录的退款金额,计算剩余可退金额,支付方式记录的退款金额需要去查询计算获取
|
//获取订单子单已经退款的退款方式金额,根据支付方式记录的标识求和返回
|
List<RefundRecordConsumePay> refundRecordConsumePayList = RefundTool.getRefundRecordConsumePay(orderItemId,null,false,true,commonService);
|
//转化成map,可以根据支付方式记录的标识直接获取到数据
|
Map<String, RefundRecordConsumePay> refundRecordConsumePayMap = refundRecordConsumePayList.stream().collect(
|
Collectors.toMap(RefundRecordConsumePay::getConsumePayId,(a) -> a));
|
RefundRecordConsumePay refundRecordConsumePay;
|
for(ConsumePayItem consumePayItem:consumePayItemList){
|
refundRecordConsumePay = refundRecordConsumePayMap.get(consumePayItem.getConsumePayId());
|
if(refundRecordConsumePay != null){
|
//已退款金额
|
consumePayItem.setRefundTotal(refundRecordConsumePay.getRefundTotal());
|
//可退款金额
|
consumePayItem.setpTotal(consumePayItem.getpTotal().subtract(refundRecordConsumePay.getRefundTotal()).setScale(2,RoundingMode.HALF_UP));
|
}
|
//计算每个支付编码可退款金额
|
surplusTotal = noMap.computeIfAbsent(consumePayItem.getNumberNo(),k->BigDecimal.ZERO);
|
surplusTotal = surplusTotal.add(consumePayItem.getpTotal()).setScale(2,RoundingMode.HALF_UP);
|
noMap.put(consumePayItem.getNumberNo(),surplusTotal);
|
|
//支付编码集合整合
|
noPayItemList = noPayItemMap.computeIfAbsent(consumePayItem.getNumberNo(),k->new ArrayList<>());
|
noPayItemList.add(consumePayItem);
|
}
|
|
List<RefundRecordConsumePay> refundConsumePayList = new ArrayList<>();
|
|
////引用对象
|
RefundRecordItemMethod refundRecordItemMethod;
|
RefundRecordMethod refundRecordMethod;
|
//需要退的退款编号金额
|
BigDecimal mothedTotal;
|
//支付编号分配的退款金额
|
BigDecimal mothedCutTotal;
|
|
//装载已经分配的退款金额记录,key值:总退款方式记录标识
|
Map<String,RefundRecordItemMethod> refundRecordItemMethodMap = new HashMap<>();
|
////退款方式金额分配
|
for(int i = 0;i <refundRecordMethodList.size();i++) {
|
refundRecordMethod = refundRecordMethodList.get(i);
|
|
////子项退款方式填充
|
refundRecordItemMethod = new RefundRecordItemMethod();
|
refundRecordItemMethod.setNumberNo(refundRecordMethod.getNumberNo());
|
refundRecordItemMethod.setName(refundRecordMethod.getName());
|
//支付方式
|
refundRecordItemMethod.setPaymentMethodId(refundRecordMethod.getPaymentMethodId());
|
refundRecordItemMethod.setIsMoneyPay(refundRecordMethod.getIsMoneyPay());
|
refundRecordItemMethod.setIsExecute(refundRecordMethod.getIsExecute());
|
refundRecordItemMethod.setIsPay(refundRecordMethod.getIsPay());
|
//退款方式
|
refundRecordItemMethod.setRefundNumberNo(refundRecordMethod.getRefundNumberNo());
|
refundRecordItemMethod.setRefundName(refundRecordMethod.getRefundName());
|
refundRecordItemMethod.setRefundMethodId(refundRecordMethod.getRefundMethodId());
|
refundRecordItemMethod.setIsMoneyPayRefund(refundRecordMethod.getIsMoneyPayRefund());
|
refundRecordItemMethod.setIsExecuteRefund(refundRecordMethod.getIsExecuteRefund());
|
|
//计算退款方式的金额
|
if (i == refundRecordMethodList.size() - 1) {
|
////最后一个
|
refundRecordItemMethod.setActualTotal(refundItemTotal);
|
} else {
|
////不是最后一个
|
refundRecordItemMethod.setActualTotal(refundRecordMethod.getActualTotal().multiply(payMothedPercentage).setScale(2, RoundingMode.UP));
|
}
|
//判断与剩余的未分配退款方式金额
|
if (refundRecordItemMethod.getActualTotal().compareTo(refundRecordMethod.getpTotal()) > 0) {
|
refundRecordItemMethod.setActualTotal(refundRecordMethod.getpTotal());
|
}
|
//判断与剩下的未分配金额校验
|
if (refundRecordItemMethod.getActualTotal().compareTo(refundItemTotal) > 0) {
|
refundRecordItemMethod.setActualTotal(refundItemTotal);
|
}
|
//可支付方式可退款金额
|
surplusTotal = noMap.computeIfAbsent(refundRecordItemMethod.getNumberNo(), k -> BigDecimal.ZERO);
|
if (refundRecordItemMethod.getActualTotal().compareTo(surplusTotal) > 0) {
|
refundRecordItemMethod.setActualTotal(surplusTotal);
|
}
|
|
refundRecordItemMethod.setRealRefundTotal(refundRecordItemMethod.getActualTotal());
|
refundRecordItemMethod.setCommonType(orderItemType);
|
refundRecordItemMethod.setCommonId(orderItemId);
|
refundRecordItemMethod.setOrderId(refundRecordMethod.getOrderId());
|
refundRecordItemMethod.setRefundRecordItemId(refundItemId);
|
refundRecordItemMethod.setRefundRecordId(refundRecord.getId());
|
refundRecordItemMethod.setRefundRecordMethodId(refundRecordMethod.getId());
|
|
refundRecordItemMethodMap.put(refundRecordMethod.getId(),refundRecordItemMethod);
|
|
//减去已经分配的退款方式金额
|
refundRecordMethod.setpTotal(refundRecordMethod.getpTotal().subtract(refundRecordItemMethod.getActualTotal()).setScale(2,RoundingMode.HALF_UP));
|
//减去已经分配的退款金额
|
refundItemTotal = refundItemTotal.subtract(refundRecordItemMethod.getActualTotal()).setScale(2,RoundingMode.HALF_UP);
|
//减去已经分配的可退款金额
|
surplusTotal = surplusTotal.subtract(refundRecordItemMethod.getActualTotal()).setScale(2,RoundingMode.HALF_UP);
|
noMap.put(refundRecordItemMethod.getNumberNo(),surplusTotal);
|
|
}
|
|
//没有分配完,再分配
|
if(refundItemTotal.compareTo(BigDecimal.ZERO) > 0){
|
logger.info("没有分配完refundRecordMethodList{}:,未分配金额:{}",JSON.toJSONString(refundRecordMethodList),refundItemTotal);
|
BigDecimal frontMoney;
|
while (refundItemTotal.compareTo(BigDecimal.ZERO) > 0){
|
//循环前的金额赋值
|
frontMoney = refundItemTotal;
|
refundItemTotal = redistributionRefundMoney(refundItemTotal,refundRecordMethodList,refundRecordItemMethodMap,noMap);
|
//避免死循环,如果金额不再分配,那么就跳出循环
|
if(frontMoney.compareTo(refundItemTotal) == 0){
|
break;
|
}
|
}
|
}
|
//判断是否已经分配完
|
if(refundItemTotal.compareTo(BigDecimal.ZERO) > 0){
|
logger.error("refundRecordMethodList{}:",JSON.toJSONString(refundRecordMethodList));
|
logger.error("分配退款金额错误:{},剩余未分配金额:{},支付方式:{}",refundItemId,refundItemTotal,JSON.toJSONString(consumePayItemList));
|
throw new TipsException("分配退款金额错误["+orderItemType+"]");
|
}
|
|
//积分
|
if(refundRecordMethodIntegral != null){
|
////子项退款方式填充
|
refundRecordItemMethod = new RefundRecordItemMethod();
|
refundRecordItemMethod.setNumberNo(refundRecordMethodIntegral.getNumberNo());
|
refundRecordItemMethod.setName(refundRecordMethodIntegral.getName());
|
//支付方式
|
refundRecordItemMethod.setPaymentMethodId(refundRecordMethodIntegral.getPaymentMethodId());
|
refundRecordItemMethod.setIsMoneyPay(refundRecordMethodIntegral.getIsMoneyPay());
|
refundRecordItemMethod.setIsExecute(refundRecordMethodIntegral.getIsExecute());
|
refundRecordItemMethod.setIsPay(refundRecordMethodIntegral.getIsPay());
|
//退款方式
|
refundRecordItemMethod.setRefundNumberNo(refundRecordMethodIntegral.getRefundNumberNo());
|
refundRecordItemMethod.setRefundName(refundRecordMethodIntegral.getRefundName());
|
refundRecordItemMethod.setRefundMethodId(refundRecordMethodIntegral.getRefundMethodId());
|
refundRecordItemMethod.setIsMoneyPayRefund(refundRecordMethodIntegral.getIsMoneyPayRefund());
|
refundRecordItemMethod.setIsExecuteRefund(refundRecordMethodIntegral.getIsExecuteRefund());
|
|
refundRecordItemMethod.setActualTotal(refundItemIntegral);
|
|
//判断与剩余的未分配退款方式金额
|
if (refundRecordItemMethod.getActualTotal().compareTo(refundRecordMethodIntegral.getpTotal()) > 0) {
|
refundRecordItemMethod.setActualTotal(refundRecordMethodIntegral.getpTotal());
|
}
|
|
//可支付方式可退款金额
|
surplusTotal = noMap.computeIfAbsent(refundRecordItemMethod.getNumberNo(), k -> BigDecimal.ZERO);
|
if (refundRecordItemMethod.getActualTotal().compareTo(surplusTotal) > 0) {
|
refundRecordItemMethod.setActualTotal(surplusTotal);
|
}
|
|
refundRecordItemMethod.setRealRefundTotal(refundRecordItemMethod.getActualTotal());
|
refundRecordItemMethod.setCommonType(orderItemType);
|
refundRecordItemMethod.setCommonId(orderItemId);
|
refundRecordItemMethod.setOrderId(refundRecordMethodIntegral.getOrderId());
|
refundRecordItemMethod.setRefundRecordItemId(refundItemId);
|
refundRecordItemMethod.setRefundRecordId(refundRecord.getId());
|
refundRecordItemMethod.setRefundRecordMethodId(refundRecordMethodIntegral.getId());
|
|
refundRecordItemMethodMap.put(refundRecordMethodIntegral.getId(),refundRecordItemMethod);
|
|
//减去已经分配的退款方式金额
|
refundRecordMethodIntegral.setpTotal(refundRecordMethodIntegral.getpTotal().subtract(refundRecordItemMethod.getActualTotal()).setScale(2,RoundingMode.HALF_UP));
|
//减去已经分配的退款金额
|
refundItemIntegral = refundItemIntegral.subtract(refundRecordItemMethod.getActualTotal());
|
//减去已经分配的可退款金额
|
surplusTotal = surplusTotal.subtract(refundRecordItemMethod.getActualTotal()).setScale(2,RoundingMode.HALF_UP);
|
noMap.put(refundRecordItemMethod.getNumberNo(),surplusTotal);
|
|
}
|
//判断是否已经分配完
|
if(refundItemIntegral.compareTo(BigDecimal.ZERO) > 0){
|
logger.error("refundRecordMethodList{}:",JSON.toJSONString(refundRecordMethodList));
|
logger.error("分配退款金额错误:{},剩余未分配金额:{},支付方式:{}",refundItemId,refundItemIntegral,JSON.toJSONString(consumePayItemList));
|
throw new TipsException("分配退款积分错误["+orderItemType+"]");
|
}
|
|
List<RefundRecordItemMethod> refundRecordItemMethodList = new ArrayList<>();
|
//保存数据
|
for(Map.Entry<String, RefundRecordItemMethod> entry : refundRecordItemMethodMap.entrySet()) {
|
refundRecordItemMethod = entry.getValue();
|
commonService.insert(RefundRecordItemMethodMapper.class,refundRecordItemMethod);
|
refundRecordItemMethodList.add(refundRecordItemMethod);
|
}
|
|
////退款方式金额分配
|
for(int i = 0;i <refundCarryVo.getRefundRecordMethodList().size();i++){
|
refundRecordMethod = refundCarryVo.getRefundRecordMethodList().get(i);
|
|
////子项退款方式填充
|
//获取分配的退款方式记录
|
refundRecordItemMethod = refundRecordItemMethodMap.get(refundRecordMethod.getId());
|
|
if(PayMethodTypeConstants.PAY_INTEGRAL.equals(refundRecordItemMethod.getRefundNumberNo())){
|
//积分
|
refundCarryVo.setIntegralTotal(refundCarryVo.getIntegralTotal().add(refundRecordItemMethod.getActualTotal()));
|
}else{
|
if(PayMethodTypeConstants.PAY_STORED.equals(refundRecordItemMethod.getRefundNumberNo())){
|
//储值金额
|
refundCarryVo.setRechargeTotal(refundCarryVo.getRechargeTotal().add(refundRecordItemMethod.getActualTotal()));
|
}else if(PayMethodTypeConstants.PAY_ADD_FUND.equals(refundRecordItemMethod.getRefundNumberNo())){
|
//增值金
|
refundCarryVo.setIncrementTotal(refundCarryVo.getIncrementTotal().add(refundRecordItemMethod.getActualTotal()));
|
}
|
refundCarryVo.setRefundTotal(refundCarryVo.getRefundTotal().add(refundRecordItemMethod.getActualTotal()));
|
}
|
|
//现金金额
|
if(refundRecordItemMethod.getIsMoneyPayRefund().equals(ConsumePayItem.YES)){
|
refundCarryVo.setCashTotal(refundCarryVo.getCashTotal().add(refundRecordItemMethod.getActualTotal()));
|
refundCarryVo.setCashPurenessTotal(refundCarryVo.getCashPurenessTotal().add(refundRecordItemMethod.getActualTotal()));
|
}
|
//划扣金额
|
if(refundRecordItemMethod.getIsExecuteRefund().equals(ConsumePayItem.YES)){
|
refundCarryVo.setDeductionTotal(refundCarryVo.getDeductionTotal().add(refundRecordItemMethod.getActualTotal()));
|
}
|
|
///////生成关联支付方式记录和退款方式关联
|
//根据支付编号获取支付方式记录
|
noPayItemList = noPayItemMap.computeIfAbsent(refundRecordItemMethod.getNumberNo(),k->new ArrayList<>());
|
//进行升序排序,避免后面不够分配
|
noPayItemList = noPayItemList.stream().sorted(Comparator.comparing(ConsumePayItem::getpTotal)).collect(Collectors.toList());
|
mothedTotal = refundRecordItemMethod.getActualTotal();
|
for(ConsumePayItem consumePayItem:noPayItemList){
|
if(consumePayItem.getpTotal().compareTo(BigDecimal.ZERO) < 1){
|
continue;
|
}
|
//计算扣减金额
|
if(consumePayItem.getpTotal().compareTo(mothedTotal) > 0){
|
mothedCutTotal = mothedTotal;
|
}else{
|
mothedCutTotal = consumePayItem.getpTotal();
|
}
|
|
//生成关联记录
|
refundRecordConsumePay = insertRefundRecordConsumePay(mothedCutTotal,refundRecordItemMethod.getCommonType(),refundRecordItemMethod.getCommonId(),refundRecord.getOrderId(),consumePayItem.getNumberNo()
|
,consumePayItem.getName(),consumePayItem.getIsMoneyPay(),consumePayItem.getIsExecute(),consumePayItem.getConsumePayId(),refundRecordItemMethod.getId(),refundItemId,refundRecord.getId(),commonService);
|
refundConsumePayList.add(refundRecordConsumePay);
|
|
//支付方式记录减掉已经分配退款方式金额
|
consumePayItem.setpTotal(consumePayItem.getpTotal().subtract(mothedCutTotal).setScale(2,RoundingMode.HALF_UP));
|
//减掉已经分配退款方式金额
|
mothedTotal = mothedTotal.subtract(mothedCutTotal).setScale(2, RoundingMode.HALF_UP);
|
//分配完成,跳出循环
|
if(mothedTotal.compareTo(BigDecimal.ZERO) < 1){
|
//跳出循环
|
break;
|
}
|
}
|
if(mothedTotal.compareTo(BigDecimal.ZERO) != 0){
|
logger.error("分配退款金额错误[14]:{},剩余未分配金额:{},支付方式:{}",refundItemId,refundItemTotal,JSON.toJSONString(consumePayItemList));
|
throw new TipsException("分配退款金额错误[14]:"+orderItemType);
|
}
|
}
|
|
refundCarryVo.setRefundConsumePayList(refundConsumePayList);
|
refundCarryVo.setRefundRecordItemMethodList(refundRecordItemMethodList);
|
|
return refundCarryVo;
|
}
|
|
/**重新分配未分配完的金额*/
|
public static BigDecimal redistributionRefundMoney(BigDecimal refundItemTotal, List<RefundRecordMethod> refundRecordMethodList, Map<String, RefundRecordItemMethod> refundRecordItemMethodMap
|
, Map<String, BigDecimal> noMap){
|
//每次1分钱
|
BigDecimal money = new BigDecimal("0.01");
|
RefundRecordMethod refundRecordMethod;
|
RefundRecordItemMethod refundRecordItemMethod;
|
BigDecimal surplusTotal;
|
for (RefundRecordMethod recordMethod : refundRecordMethodList) {
|
refundRecordMethod = recordMethod;
|
if (refundRecordMethod.getpTotal().compareTo(BigDecimal.ZERO) <= 0) {
|
continue;
|
}
|
//获取分配的退款方式记录
|
refundRecordItemMethod = refundRecordItemMethodMap.get(refundRecordMethod.getId());
|
|
//可支付方式可退款金额
|
surplusTotal = noMap.computeIfAbsent(refundRecordItemMethod.getNumberNo(), k -> BigDecimal.ZERO);
|
if (surplusTotal.compareTo(BigDecimal.ZERO) <= 0) {
|
continue;
|
}
|
|
//判断与剩余的未分配退款方式金额
|
if (money.compareTo(refundRecordMethod.getpTotal()) > 0) {
|
money = refundRecordMethod.getpTotal();
|
}
|
//判断与剩下的未分配金额校验
|
if (money.compareTo(refundItemTotal) > 0) {
|
money = refundItemTotal;
|
}
|
if (money.compareTo(surplusTotal) > 0) {
|
money = surplusTotal;
|
}
|
|
//减去已经分配的退款方式金额
|
refundRecordMethod.setpTotal(refundRecordMethod.getpTotal().subtract(money));
|
//减去已经分配的退款金额
|
refundItemTotal = refundItemTotal.subtract(money);
|
//减去已经分配的可退款金额
|
surplusTotal = surplusTotal.subtract(money);
|
noMap.put(refundRecordItemMethod.getNumberNo(), surplusTotal);
|
//叠加到分配的退款方式记录
|
refundRecordItemMethod.setActualTotal(refundRecordItemMethod.getActualTotal().add(money));
|
//重新赋值
|
refundRecordItemMethod.setRealRefundTotal(refundRecordItemMethod.getActualTotal());
|
if (refundItemTotal.compareTo(BigDecimal.ZERO) < 1) {
|
break;
|
}
|
}
|
return refundItemTotal;
|
}
|
|
|
|
/**
|
* 退款-处理优惠券
|
* @param refundRecord 退款总数据
|
* @param commonService 映射
|
*/
|
public static void handCoupon(RefundRecord refundRecord,CommonService commonService) {
|
SqlSentence sqlSentence = new SqlSentence();
|
Map<String, Object> map = new HashMap<>();
|
|
//回去回退优惠券
|
map.put("refundRecordId",refundRecord.getId());
|
sqlSentence.sqlSentence("select * from refund_record_coupon where refundRecordId=#{m.refundRecordId} and isDel=0 ",map);
|
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,"订单优惠卷标识不正确");
|
}
|
|
//变更订单优惠券记录状态
|
map.clear();
|
map.put("status", BaseEntity.YES);
|
map.put("oldStatus", BaseEntity.NO);
|
map.put("id", couponOrderDiscountLog.getId());
|
sqlSentence.sqlSentence("status = #{m.status}, editTime = now() WHERE id = #{m.id} AND status = #{m.oldStatus}",map);
|
if(commonService.updateWhere(CouponOrderDiscountLogMapper.class,sqlSentence) != 1){
|
throw new TipsException("优惠券回退失败!");
|
}
|
|
//领建优惠券跳过回退
|
if(HIS_COUPON_CODE.equals(couponOrderDiscountLog.getCouponNumberId())){
|
continue;
|
}
|
|
//优惠券状态变化
|
map.put("isUse", BaseEntity.NO);
|
map.put("isUseOld", BaseEntity.YES);
|
map.put("useTime", null);
|
map.put("useType", CouponNumber.USE_TYPE_UNKNOW);
|
map.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}",map);
|
if(commonService.updateWhere(CouponNumberMapper.class,sqlSentence) != 1){
|
throw new TipsException("优惠券回退失败[67]!");
|
}
|
}
|
}
|
|
/**页面活动赠送的增值金退款处理
|
* @param ordersTotal 订单
|
* @param refundRecord 退款订单
|
* @param circulateNum 避免死循环,调用的时候传0
|
* @param commonService 映射
|
*/
|
public static void activityPageGiveIncrease(OrdersTotal ordersTotal,RefundRecord refundRecord,int circulateNum,CommonService commonService){
|
|
if(circulateNum > 1){
|
return;
|
}
|
if(ordersTotal.getActivityPageType() != OrderTotalConstants.ACTIVITY_TYPE_BURST){
|
return;
|
}
|
//订单取消状态才处理
|
if(ordersTotal.getRefundStatus() != OrderTotalConstants.STATUS_REFUND_FINSH){
|
return;
|
}
|
|
//查找领取记录释放存在
|
//领取记录是否已领取
|
UserMoneyUnclaimed userMoneyUnclaimed = OrderTool.getActivityGive(ordersTotal,commonService);
|
if(userMoneyUnclaimed == null){
|
return;
|
}
|
SqlSentence sqlSentence = new SqlSentence();
|
Map<String,Object> values = new HashMap<>();
|
|
//-----生成一条记录,可以回溯
|
RefundUserAssets refundUserAssets;
|
//生成记录
|
refundUserAssets = new RefundUserAssets();
|
//设置为102是当前的记录类型
|
refundUserAssets.setIsDel(RefundUserAssets.DEL_BURST);
|
refundUserAssets.setType(userMoneyUnclaimed.getOperationReason());
|
refundUserAssets.setStatus(RefundUserAssets.STATUS_NORMAL);
|
refundUserAssets.setAmountType(userMoneyUnclaimed.getFundType());
|
refundUserAssets.setAmount(userMoneyUnclaimed.getOpNumber().negate());
|
refundUserAssets.setOperatorId(refundRecord.getOperatorId());
|
refundUserAssets.setOperatorNo(refundRecord.getOperatorNo());
|
refundUserAssets.setOperatorName(refundRecord.getOperatorName());
|
refundUserAssets.setOrderId(refundRecord.getOrderId());
|
refundUserAssets.setRefundRecordId(refundRecord.getId());
|
refundUserAssets.setUserMoneyUnclaimedId(userMoneyUnclaimed.getId());
|
refundUserAssets.setUserId(userMoneyUnclaimed.getUserId());
|
|
if(userMoneyUnclaimed.getStatus() == UserMoneyUnclaimed.STATUS_SUCCEED){
|
//已领取,那么就扣回来
|
if(userMoneyUnclaimed.getOpNumber().compareTo(BigDecimal.ZERO) < 1){
|
return;
|
}
|
refundUserAssets.setAmountStatus(RefundUserAssets.AMOUNT_STATUS_NORMAL);
|
UserMoneyUtil.setUserMoneyHandle(userMoneyUnclaimed.getUserId(),UserMoneyUnclaimed.FUND_TYPE_VALUE_ADDED_FUND,userMoneyUnclaimed.getOpNumber().negate(),BaseEntity.NO,OperationReasonConstants.OP_REASON_INVITE_ORDER_CANCEL,"好友订单退款-活动赠送扣除["+ordersTotal.getOrderNo()+"]","操作原因:退款审核通过,活动赠送扣除资金,退款方式:退回增值金",ordersTotal.getOperatorId()
|
,ordersTotal.getId(),ordersTotal.getActivityPageCode(),null,commonService);
|
}else{
|
if(!UserMoneyUnclaimed.YES.equals(userMoneyUnclaimed.getIsValid())){
|
return;
|
}
|
refundUserAssets.setAmountStatus(RefundUserAssets.AMOUNT_STATUS_NOT_RECEIVE);
|
//没有领取就作废
|
values.put("id",userMoneyUnclaimed.getId());
|
values.put("isValid",UserMoneyUnclaimed.NO);
|
values.put("status",UserMoneyUnclaimed.STATUS_WAI);
|
sqlSentence.sqlWhere("isValid = #{m.isValid} WHERE id = #{m.id} AND status = #{m.status}",values);
|
if(commonService.updateWhere(UserMoneyUnclaimedMapper.class,sqlSentence) != 1){
|
circulateNum++;
|
activityPageGiveIncrease(ordersTotal,refundRecord,circulateNum,commonService);
|
}
|
}
|
//保存记录,正常扣减和未领取才去保存这个记录
|
commonService.insert(RefundUserAssetsMapper.class,refundUserAssets);
|
|
//查看是否有第二张订单符合
|
values.clear();
|
values.put("status",OrderTotalConstants.STATUS_CANCEL);
|
values.put("payStatus",OrderTotalConstants.PAY_STATUS_SUC);
|
values.put("id",ordersTotal.getId());
|
values.put("activityPageType",ordersTotal.getActivityPageType());
|
values.put("activityPageCode",ordersTotal.getActivityPageCode());
|
sqlSentence.sqlSentence("SELECT * FROM orders_total WHERE isDel = 0 AND status != #{m.status} AND payStatus = #{m.payStatus}" +
|
" AND id != #{m.id} AND activityPageType = #{m.activityPageType} AND activityPageCode = #{m.activityPageCode} ORDER BY payTime ASC LIMIT 1",values);
|
OrdersTotal ordersTotal1 = commonService.selectOne(OrdersTotalMapper.class,sqlSentence);
|
if(ordersTotal1 != null){
|
Integer opType = BaseEntity.YES;
|
if(userMoneyUnclaimed.getStatus() == UserMoneyUnclaimed.STATUS_SUCCEED){
|
opType = BaseEntity.NO;
|
}
|
//增值金到账处理
|
UserMoneyUtil.setUserMoneyHandle(userMoneyUnclaimed.getUserId(), UserMoneyUnclaimed.FUND_TYPE_VALUE_ADDED_FUND,userMoneyUnclaimed.getOpNumber(),opType, OperationReasonConstants.OP_REASON_INVITE_ORDER,userMoneyUnclaimed.getOriginSubject(),userMoneyUnclaimed.getRemarks(),userMoneyUnclaimed.getOperatorId()
|
,ordersTotal1.getId(),ordersTotal.getActivityPageCode(),userMoneyUnclaimed.getMoneyRuleId(),commonService);
|
}
|
|
}
|
|
/**
|
* 退款-处理活动规则增值金和积分
|
*/
|
private static void handActivityRule(CommonService commonService, String operationId, String operationNme, SqlSentence sqlSentence,
|
Map<String, Object> 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.sqlSentence("select * from activity_action where activityRuleId=#{m.activityRuleId} and (type=#{m.type} or type=#{m.type1} or type=#{m.type2}) and isDel=0",map);
|
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()).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.sqlSentence(" validState=#{m.newValidState} where couponId=#{m.couponId} and commonId=#{m.commonId} and validState=#{m.oldValidState} ",map);
|
commonService.updateWhere(CouponNumberMapper.class,sqlSentence);
|
}
|
}
|
}
|
}
|
}
|
|
}
|
|
/**退款-处理普通订单信息(比如:项目、促销、卡项)
|
* @param refundCarryVo 全局携带参数结构
|
* @param operationId 操作人标识
|
* @param refundRecord 退款总标识
|
* @param ordersTotal 订单
|
* @param commonService 映射
|
* @return 全局携带参数结构
|
*/
|
public static RefundCarryVo numberOfRefunds(RefundCarryVo refundCarryVo,String operationId, RefundRecord refundRecord
|
,OrdersTotal ordersTotal,CommonService commonService,DeductionSingleService deductionSingleService) {
|
|
SqlSentence sqlSentence = new SqlSentence();
|
Map<String, Object> map = new HashMap<>();
|
|
//获取退款子单
|
map.put("refundRecordId",refundRecord.getId());
|
sqlSentence.sqlSentence("SELECT * FROM refund_record_item WHERE isDel = 0 AND refundRecordId = #{m.refundRecordId}",map);
|
List<RefundRecordItem> refundRecordItems =commonService.selectList(RefundRecordItemMapper.class,sqlSentence);
|
if(refundRecordItems == null || refundRecordItems.size() == 0){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到退款子订单信息");
|
}
|
|
//根据支付记录总表的标识来整合已退的金额,key值:支付记录总表标识,value:金额数据
|
Map<String,RefundRecordConsumePay> refundRecordConsumePayMap = new HashMap<>();
|
RefundRecordConsumePay refundRecordConsumePay;
|
|
for (RefundRecordItem refundRecordItem: refundRecordItems) {
|
//初始化总结构携带参数
|
refundCarryVo.setRefundConsumePayList(new ArrayList<>());
|
refundCarryVo.setDeductionTotal(BigDecimal.ZERO);
|
refundCarryVo.setCashTotal(BigDecimal.ZERO);
|
refundCarryVo.setDeductionTotalUser(BigDecimal.ZERO);
|
refundCarryVo.setCashPurenessTotal(BigDecimal.ZERO);
|
refundCarryVo.setRechargeTotal(BigDecimal.ZERO);
|
refundCarryVo.setIntegralTotal(BigDecimal.ZERO);
|
refundCarryVo.setIncrementTotal(BigDecimal.ZERO);
|
refundCarryVo.setRefundTotal(BigDecimal.ZERO);
|
//商品类型判断
|
switch (refundRecordItem.getType()){
|
case OrderItemConstants.TYPE_RETAIL:
|
refundCarryVo = handRefundRerail(refundRecord,refundRecordItem,refundCarryVo,commonService);
|
break;
|
case OrderItemConstants.TYPE_DRUG:
|
refundCarryVo = handRefundRerail(refundRecord,refundRecordItem,refundCarryVo,commonService);
|
break;
|
case OrderItemConstants.TYPE_PROJECT:
|
refundCarryVo = handRefundNoExecution(refundRecord,refundRecordItem,refundCarryVo,commonService,deductionSingleService);
|
break;
|
case OrderItemConstants.TYPE_PROMOTION:
|
refundCarryVo = handRefundPromotion(operationId, refundRecord, ordersTotal,refundRecordItem,refundCarryVo,commonService,deductionSingleService);
|
break;
|
case OrderItemConstants.TYPE_CARD:
|
refundCarryVo = handRefundCard(refundRecord,refundRecordItem,refundCarryVo,commonService);
|
break;
|
case OrderItemConstants.CARD_BAG:
|
refundCarryVo = handRefundPromotion(operationId, refundRecord, ordersTotal,refundRecordItem,refundCarryVo,commonService,deductionSingleService);
|
break;
|
case OrderItemConstants.TYPE_COUPON:
|
refundCarryVo = handRefundRerail(refundRecord,refundRecordItem,refundCarryVo,commonService);
|
break;
|
default:
|
//其他
|
refundCarryVo = handRefundRerail(refundRecord,refundRecordItem,refundCarryVo,commonService);
|
break;
|
}
|
|
//遍历叠加支付方式记录的退款金额
|
for(RefundRecordConsumePay re:refundCarryVo.getRefundConsumePayList()){
|
refundRecordConsumePay = refundRecordConsumePayMap.computeIfAbsent(re.getConsumePayId(),k->new RefundRecordConsumePay(BigDecimal.ZERO,re.getNumberNo(),re.getName(),re.getIsMoneyPay(),re.getIsExecute(),re.getConsumePayId()));
|
refundRecordConsumePay.setRefundTotal(refundRecordConsumePay.getRefundTotal().add(re.getRefundTotal()));
|
}
|
}
|
|
//转载返回支付方式记录的退款金额
|
List<RefundRecordConsumePay> refundRecordConsumePayList = new ArrayList<>();
|
for (Map.Entry<String, RefundRecordConsumePay> entry : refundRecordConsumePayMap.entrySet()) {
|
refundRecordConsumePayList.add(entry.getValue());
|
}
|
|
//校验金额是不是已经分配完
|
for(RefundRecordMethod refundRecordMethod:refundCarryVo.getRefundRecordMethodList()){
|
if(refundRecordMethod.getpTotal().compareTo(BigDecimal.ZERO) > 0){
|
throw new TipsException("退款金额分配错误!");
|
}
|
}
|
|
refundCarryVo.setRefundConsumePayList(refundRecordConsumePayList);
|
return refundCarryVo;
|
}
|
|
/**
|
* 退款一级是商品
|
* @param refundRecord 退款总信息
|
* @param refundRecordItem 退款子单
|
* @param commonService 映射
|
*/
|
private static RefundCarryVo handRefundRerail(RefundRecord refundRecord, RefundRecordItem refundRecordItem
|
, RefundCarryVo refundCarryVo, CommonService commonService) {
|
|
//判断操作完了去修改子订单状态
|
OrderItem orderItem=commonService.selectOneByKey(OrderItemMapper.class,refundRecordItem.getOrderItemId());
|
if (orderItem==null){
|
throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"未找到子订单信息");
|
}
|
//剩余可退款数量
|
Integer surplusNum = orderItem.getBuyNum()-orderItem.getHasReNum();
|
if(refundRecordItem.getRefundNum() > surplusNum){
|
throw new TipsException("退款数量不能大于可退款数量!");
|
}
|
|
//变更退款子项信息
|
int refundStatus;
|
if (surplusNum.equals(refundRecordItem.getRefundNum())){
|
refundStatus = OrderTotalConstants.STATUS_REFUND_FINSH;
|
}else{
|
refundStatus = OrderTotalConstants.STATUS_REFUND_PART;
|
}
|
|
//退款方式处理
|
refundCarryVo = refundRecordMotnedItemHandle(refundRecord,refundRecordItem,refundCarryVo,commonService);
|
|
//更新子订单信息
|
updateOrderItemOne(orderItem,refundStatus,refundCarryVo,refundRecordItem.getRefundNum(),commonService);
|
|
return refundCarryVo;
|
}
|
|
/**
|
* 退款-处理未执行划扣 项目类型
|
* @param refundRecord 退款总记录
|
* @param refundRecordItem 退款一级子记录
|
*/
|
private static RefundCarryVo handRefundNoExecution(RefundRecord refundRecord, RefundRecordItem refundRecordItem
|
,RefundCarryVo refundCarryVo,CommonService commonService,DeductionSingleService deductionSingleService) {
|
|
//找到子单
|
OrderItem orderItem=commonService.selectOneByKey(OrderItemMapper.class,refundRecordItem.getOrderItemId());
|
if (orderItem==null){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到子订单信息");
|
}
|
|
SqlSentence sqlSentence = new SqlSentence();
|
Map<String, Object> map = new HashMap<>();
|
|
//先作废划扣,不然数量对不上
|
deductionCancel(refundRecord,refundRecordItem.getId(),commonService,deductionSingleService);
|
|
//找到用户项目
|
map.put("commonId",refundRecordItem.getOrderItemId());
|
sqlSentence.sqlSentence("select * from user_project_item where isDel = 0 and commonId = #{m.commonId} and isTransfer = 0",map);
|
UserProjectItem userProjectItem =commonService.selectOne(UserProjectItemMapper.class,sqlSentence);
|
if (userProjectItem == null) {
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到用户子项项目信息");
|
}
|
|
if(userProjectItem.getNotUsedNum() < refundRecordItem.getRefundNum()){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款次数大于可退次数!");
|
}
|
|
//用户项目操作
|
UserProjectDeductionVo userProjectDeductionVo = UserProjectTool.userProjectDeduction(userProjectItem,UserProjectUsedCon.USED_METHOD_ORDER_REFUND,UserProjectUsedCon.USED_TYPE_DEDUCTION,null
|
,refundRecordItem.getId(),refundRecordItem.getRefundNum(),refundRecord.getOperatorAppId(),refundRecord.getOperatorAppName(),refundRecord.getRefundShopId(),refundRecord.getRefundShopName(),"员工备注:"+refundRecord.getRemarks()+"|用户备注:"+refundRecord.getRefundReason(),commonService);
|
|
refundCarryVo.setDeductionTotalUser(userProjectDeductionVo.getDeductionTotal());
|
|
//计算子单是否还有剩余的可扣疗程数
|
int surplusNum = orderItem.getUsedTotal()-orderItem.getHasReNum();
|
if(refundRecordItem.getRefundNum() > surplusNum){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款次数大于子单可退次数[54]!");
|
}
|
|
int refundStatus;
|
if(refundRecordItem.getRefundNum() == surplusNum){
|
refundStatus=OrderTotalConstants.STATUS_REFUND_FINSH;
|
}else{
|
refundStatus=OrderTotalConstants.STATUS_REFUND_PART;
|
}
|
|
//退款方式处理
|
refundCarryVo = refundRecordMotnedItemHandle(refundRecord,refundRecordItem,refundCarryVo,commonService);
|
|
//更新子订单信息
|
updateOrderItemOne(orderItem,refundStatus,refundCarryVo,refundRecordItem.getRefundNum(),commonService);
|
|
return refundCarryVo;
|
}
|
|
/**退款-处理卡项
|
* @param refundRecord 退款总记录
|
* @param refundRecordItem 退款一级记录
|
* @param refundCarryVo 全局携带参数结构
|
* @param commonService 映射
|
* @return 全局携带参数结构
|
*/
|
public static RefundCarryVo handRefundCard(RefundRecord refundRecord, RefundRecordItem refundRecordItem,RefundCarryVo refundCarryVo,CommonService commonService) {
|
|
//判断操作完了去修改子订单状态
|
OrderItem orderItem=commonService.selectOneByKey(OrderItemMapper.class,refundRecordItem.getOrderItemId());
|
if (orderItem==null){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到子订单信息");
|
}
|
Integer surplusNum = orderItem.getBuyNum() - orderItem.getHasReNum();
|
if(refundRecordItem.getRefundNum() > surplusNum){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款作废卡包提示:没有找到对应的卡包可退[020]");
|
}
|
|
//找到可退款卡包
|
List<UserCard> userCardList = PartialRefundUtil.getRefundCard(orderItem.getId(),UserProjectConstants.EFF_STATUS_YES,commonService);
|
if(refundRecordItem.getRefundNum() > userCardList.size()){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款作废卡包提示:没有找到对应的卡包可退[021]");
|
}
|
|
SqlSentence sqlSentence = new SqlSentence();
|
Map<String, Object> map = new HashMap<>();
|
|
//变更卡项状态
|
UserCard userCard;
|
RefundRecordCard refundRecordCard;
|
for (int i = 0; i < refundRecordItem.getRefundNum(); i++) {
|
userCard= userCardList.get(i);
|
map.put("id",userCard.getId());
|
map.put("statusOld", UserCard.TYPE_NO_USED);
|
map.put("effectiveStatus", UserProjectConstants.EFF_STATUS_CANCEL);
|
map.put("oldEffectiveStatus", UserProjectConstants.EFF_STATUS_YES);
|
sqlSentence.sqlSentence(" effectiveStatus=#{m.effectiveStatus} WHERE id = #{m.id} AND effectiveStatus = #{m.oldEffectiveStatus} AND status = #{m.statusOld}",map);
|
if(commonService.updateWhere(UserCardMapper.class,sqlSentence) != 1){
|
throw new TipsException("用户卡包状态已发生变化,请重试!");
|
}
|
|
//生成退款关联
|
refundRecordCard = new RefundRecordCard();
|
refundRecordCard.setUserCardId(userCard.getId());
|
refundRecordCard.setRefundRecordId(refundRecord.getId());
|
refundRecordCard.setRefundRecordItemId(refundRecordItem.getId());
|
commonService.insert(RefundRecordCardMapper.class,refundRecordCard);
|
}
|
|
int refundStatus;
|
//卡项的,不管有没有退数量,只要走进这里,都是部分退或者全部退,可能有卡项部分退
|
if (surplusNum.equals(refundRecordItem.getRefundNum())){
|
refundStatus = OrderTotalConstants.STATUS_REFUND_FINSH;
|
}else {
|
refundStatus = OrderTotalConstants.STATUS_REFUND_PART;
|
}
|
|
//退款方式处理
|
refundCarryVo = refundRecordMotnedItemHandle(refundRecord,refundRecordItem,refundCarryVo,commonService);
|
|
//更新子订单信息
|
updateOrderItemOne(orderItem,refundStatus,refundCarryVo,refundRecordItem.getRefundNum(),commonService);
|
|
//--卡项部分退处理
|
if(RefundSoruceConstants.TYPE_SOURCE_USER_CARD.equals(refundRecord.getSourceAssistantType())){
|
CardRefundTool.realRefund(refundRecord,refundRecordItem,refundCarryVo,commonService);
|
}
|
|
return refundCarryVo;
|
}
|
|
/**退款-处理卡包
|
*/
|
public static void handCardBag(String operationId,RefundRecord refundRecord, OrdersTotal ordersTotal
|
,RefundRecordItem refundRecordItem,RefundCarryVo refundCarryVo,CommonService commonService) {
|
|
//查看订单信息
|
OrderItem orderItem=commonService.selectOneByKey(OrderItemMapper.class,refundRecordItem.getOrderItemId());
|
if(orderItem == null ){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到子订单信息!");
|
}
|
|
SqlSentence sqlSentence = new SqlSentence();
|
Map<String, Object> map = new HashMap<>();
|
|
map.put("refundRecordItemId",refundRecordItem.getId());
|
sqlSentence.sqlSentence("select * from refund_record_item_source where refundRecordItemId =#{m.refundRecordItemId}",map);
|
List<RefundRecordItemSource> sons = commonService.selectList(RefundRecordItemSourceMapper.class, sqlSentence);
|
if(sons.size() == 0 ){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到子订单信息[015]!");
|
}
|
|
for (RefundRecordItemSource son : sons) {
|
refundCarryVo.setRefundConsumePayList(new ArrayList<>());
|
refundCarryVo.setDeductionTotal(BigDecimal.ZERO);
|
refundCarryVo.setCashTotal(BigDecimal.ZERO);
|
switch (GroupTypeEnum.getCode(son.getType())){
|
case PROJECT:
|
//handRefundNoSonExecution(refundRecord,son,refundCarryVo,commonService);
|
break;
|
case RETAIL:
|
//handRefundSonRerail(refundRecord,son,refundCarryVo,commonService );
|
//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<OrderItemSon> orderItemSonList=commonService.selectList(OrderItemSonMapper.class,sqlSentence);
|
|
List<Integer> collect = orderItemSonList.stream().map(OrderItemSon::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 if (collect.contains(OrderTotalConstants.STATUS_REFUND_FINSH)){
|
orderItem.setRefundStatus(OrderTotalConstants.STATUS_REFUND_FINSH);
|
orderItem.setReTotal(orderItemSonList.stream().map(OrderItemSon::getReTotal).reduce(BigDecimal.ZERO,BigDecimal::add));
|
orderItem.setHasReNum(orderItem.getBuyNum());
|
}else {
|
orderItem.setRefundStatus(OrderTotalConstants.STATUS_REFUND_NONE);
|
}
|
|
commonService.updateAll(OrderItemMapper.class,orderItem);
|
}
|
|
/**退款需要删除用户卡包使用记录
|
* @param userCardId 用户卡包
|
* @param cardItemInfoId 卡包的卡项子项
|
* @param sourceId 卡包使用记录来源标识
|
* @param orderId 卡包使用记录来源总表标识
|
* @param refundNum 退款数量
|
* @param commonService 映射
|
*/
|
public static void deleteUserCardUsed(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);
|
map.put("sourceType",UserCardUsed.SOURCE_TYPE_ORDER_ITEM_TWO);
|
sqlSentence.sqlSentence(" SELECT * FROM user_card_used WHERE isDel = 0 AND userCardId = #{m.userCardId}" +
|
" AND cardItemInfoId = #{m.cardItemInfoId} AND sourceId = #{m.sourceId} AND sourceType = #{m.sourceType}",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);
|
map.put("sourceType",UserCardUsed.SOURCE_TYPE_ORDER_ITEM_TWO);
|
sqlSentence.sqlSentence(" SELECT * FROM user_card_used WHERE isDel = 0 AND sourceType = #{m.sourceType} 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 = 0 AND sourceType = #{m.sourceType} AND userCardId = #{m.userCardId}" +
|
" AND cardItemInfoId = #{m.cardItemInfoId} AND sourceId IS NULL",map);
|
userCardUsedList = commonService.selectList(UserCardUsedMapper.class,sqlSentence);
|
}
|
}
|
|
//获取总使用数量
|
int usedNum = 0;
|
for(UserCardUsed userCardUsed:userCardUsedList){
|
usedNum = usedNum+userCardUsed.getUsedNum();
|
}
|
|
//计算退回的卡次
|
int returnNum = UserCardTool.countUsedNumber(refundNum,cardItemInfo,cardEquity);
|
|
if(returnNum > usedNum){
|
logger.error("卡包可退数量错误-卡包退款失败,查询语句:{},传入参数:{}",sqlSentence.getSqlSentence(), JSON.toJSONString(sqlSentence.getM()));
|
logger.error("卡包可退数量错误-卡包退款失败,userCardId:{},cardItemInfoId:{},sourceId:{},orderId:{},refundNum:{},returnNum:{},usedNum:{}",userCardId,cardItemInfoId,sourceId,orderId,refundNum,returnNum,usedNum);
|
throw new TipsException("卡包可退数量错误!");
|
}
|
|
//已经操作数量
|
int opNum = returnNum;
|
StringBuilder sql;
|
for(UserCardUsed userCardUsed:userCardUsedList){
|
//判断是否够了,跳出循环
|
if(opNum <= 0){
|
break;
|
}
|
if(userCardUsed.getUsedNum() <= 0){
|
continue;
|
}
|
map.clear();
|
sql = new StringBuilder();
|
map.put("id",userCardUsed.getId());
|
map.put("usedNumOld",userCardUsed.getUsedNum());
|
if(opNum >= userCardUsed.getUsedNum()){
|
map.put("isDel",UserCardUsed.YES);
|
map.put("countNum",userCardUsed.getUsedNum());
|
opNum = opNum - userCardUsed.getUsedNum();
|
}else{
|
map.put("isDel",UserCardUsed.NO);
|
map.put("countNum",opNum);
|
opNum = 0;
|
}
|
sql.append("isDel = #{m.isDel}");
|
sql.append(",usedNum = usedNum - #{m.countNum}");
|
sql.append(",cancelNum = cancelNum + #{m.countNum}");
|
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 = 0 AND usedNum = #{m.usedNumOld}");
|
sqlSentence.sqlUpdate(sql.toString(),map);
|
if(commonService.updateWhere(UserCardUsedMapper.class,sqlSentence)!=1){
|
throw new TipsException("卡包使用记录已发生变化,请重试!");
|
}
|
}
|
if(opNum != 0){
|
logger.error("卡包回退数量错误-卡包退款失败,userCardId:{},cardItemInfoId:{},sourceId:{},orderId:{},refundNum:{},returnNum:{},opNum:{}",userCardId,cardItemInfoId,sourceId,orderId,refundNum,returnNum,opNum);
|
throw new TipsException("卡包回退数量错误!");
|
}
|
}
|
|
/**退款-处理促销
|
* @param operationId 操作人标识
|
* @param refundRecord 退款总记录
|
* @param ordersTotal 订单
|
* @param refundRecordItem 退款一级记录
|
* @param refundCarryVo 全局携带参数结构
|
* @param commonService 映射
|
* @return 全局携带参数结构
|
*/
|
private static RefundCarryVo handRefundPromotion(String operationId, RefundRecord refundRecord, OrdersTotal ordersTotal
|
, RefundRecordItem refundRecordItem,RefundCarryVo refundCarryVo,CommonService commonService,DeductionSingleService deductionSingleService) {
|
|
//查看订单信息
|
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);
|
|
//根据支付记录总表的标识来整合已退的金额,key值:支付记录总表标识,value:金额数据
|
Map<String,RefundRecordConsumePay> refundRecordConsumePayMap = new HashMap<>();
|
RefundRecordConsumePay refundRecordConsumePay;
|
//计算本次退款方式的划扣金额
|
BigDecimal deductionTotal = BigDecimal.ZERO;
|
//分配的用户项目划扣金额
|
BigDecimal deductionTotalUser = BigDecimal.ZERO;
|
//计算本次退款方式的现金金额
|
BigDecimal cashTotal = BigDecimal.ZERO;
|
//退款现金总金额(元,不包含退到储值金的)
|
BigDecimal cashPurenessTotal = BigDecimal.ZERO;
|
//退款储值金总金额(元)
|
BigDecimal rechargeTotal = BigDecimal.ZERO;
|
//退款增值金总金额(元)
|
BigDecimal incrementTotal = BigDecimal.ZERO;
|
//退款积分
|
BigDecimal integralTotal = BigDecimal.ZERO;
|
//退款总金额(元)
|
BigDecimal refundTotal = BigDecimal.ZERO;
|
|
for (RefundRecordItemSource son : sons) {
|
//初始化总结构携带参数
|
refundCarryVo.setRefundConsumePayList(new ArrayList<>());
|
refundCarryVo.setDeductionTotal(BigDecimal.ZERO);
|
refundCarryVo.setCashTotal(BigDecimal.ZERO);
|
refundCarryVo.setDeductionTotalUser(BigDecimal.ZERO);
|
refundCarryVo.setDeductionTotalUser(BigDecimal.ZERO);
|
refundCarryVo.setCashPurenessTotal(BigDecimal.ZERO);
|
refundCarryVo.setRechargeTotal(BigDecimal.ZERO);
|
refundCarryVo.setIntegralTotal(BigDecimal.ZERO);
|
refundCarryVo.setIncrementTotal(BigDecimal.ZERO);
|
refundCarryVo.setRefundTotal(BigDecimal.ZERO);
|
switch (GroupTypeEnum.getCode(son.getType())){
|
case PROJECT:
|
refundCarryVo = handRefundNoSonExecution(refundRecord,refundRecordItem,son,refundCarryVo,orderItem.getUserCardId(),commonService,deductionSingleService);
|
break;
|
case RETAIL:
|
refundCarryVo = handRefundSonRerail(refundRecord,refundRecordItem,son,refundCarryVo,orderItem.getUserCardId(),commonService);
|
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;
|
default:
|
//其他
|
refundCarryVo = handRefundSonRerail(refundRecord,refundRecordItem,son,refundCarryVo,orderItem.getUserCardId(),commonService);
|
break;
|
}
|
deductionTotal = deductionTotal.add(refundCarryVo.getDeductionTotal());
|
deductionTotalUser = deductionTotalUser.add(refundCarryVo.getDeductionTotalUser());
|
cashTotal = cashTotal.add(refundCarryVo.getCashTotal());
|
cashPurenessTotal = cashPurenessTotal.add(refundCarryVo.getCashPurenessTotal());
|
rechargeTotal = rechargeTotal.add(refundCarryVo.getRechargeTotal());
|
incrementTotal = incrementTotal.add(refundCarryVo.getIncrementTotal());
|
integralTotal = integralTotal.add(refundCarryVo.getIntegralTotal());
|
refundTotal = refundTotal.add(refundCarryVo.getRefundTotal());
|
//遍历叠加支付方式记录的退款金额
|
for(RefundRecordConsumePay re:refundCarryVo.getRefundConsumePayList()){
|
refundRecordConsumePay = refundRecordConsumePayMap.computeIfAbsent(re.getConsumePayId(),k->new RefundRecordConsumePay(BigDecimal.ZERO,re.getNumberNo(),re.getName(),re.getIsMoneyPay(),re.getIsExecute(),re.getConsumePayId()));
|
refundRecordConsumePay.setRefundTotal(refundRecordConsumePay.getRefundTotal().add(re.getRefundTotal()));
|
}
|
}
|
|
//转载返回支付方式记录的退款金额
|
List<RefundRecordConsumePay> refundRecordConsumePayList = new ArrayList<>();
|
for (Map.Entry<String, RefundRecordConsumePay> entry : refundRecordConsumePayMap.entrySet()) {
|
refundRecordConsumePayList.add(entry.getValue());
|
}
|
refundCarryVo.setRefundConsumePayList(refundRecordConsumePayList);
|
refundCarryVo.setDeductionTotal(deductionTotal);
|
refundCarryVo.setDeductionTotalUser(deductionTotalUser);
|
refundCarryVo.setCashTotal(cashTotal);
|
refundCarryVo.setCashPurenessTotal(cashPurenessTotal);
|
refundCarryVo.setRechargeTotal(rechargeTotal);
|
refundCarryVo.setIncrementTotal(incrementTotal);
|
refundCarryVo.setIntegralTotal(integralTotal);
|
refundCarryVo.setRefundTotal(refundTotal);
|
|
//更新退款子单
|
updateRefundItem(refundCarryVo,refundRecordItem.getId(),commonService);
|
|
//获取其子项
|
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;
|
}
|
|
//更新子订单信息
|
updateOrderItemOne(orderItem,refundStatus,refundCarryVo,refundNum,commonService);
|
|
return refundCarryVo;
|
}
|
|
/**退款-处理二级子订单未执行划扣 项目类型
|
* @param refundRecord 退款总记录
|
* @param refundRecordItem 退款一级子单
|
* @param refundRecordItemSource 退款二级子单
|
* @param refundCarryVo 全局携带参数接口
|
* @param commonService 映射
|
*/
|
private static RefundCarryVo handRefundNoSonExecution(RefundRecord refundRecord,RefundRecordItem refundRecordItem, RefundRecordItemSource refundRecordItemSource
|
,RefundCarryVo refundCarryVo,String userCardId,CommonService commonService,DeductionSingleService deductionSingleService) {
|
|
//判断操作完了去修改子订单状态
|
OrderItemSon orderItemSon = commonService.selectOneByKey(OrderItemSonMapper.class,refundRecordItemSource.getOrderItemSonId());
|
if (orderItemSon==null){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到二级子订单信息[02]");
|
}
|
|
//先作废划扣,不然数量对不上
|
deductionCancel(refundRecord,refundRecordItemSource.getId(),commonService,deductionSingleService);
|
|
UserProjectItem userProjectItem = getUserProject(orderItemSon.getId(),commonService);
|
if(userProjectItem == null){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"用户项目获取失败[06]!");
|
}
|
if(userProjectItem.getNotUsedNum()<refundRecordItemSource.getRefundNum()){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款次数大于可退次数[83]!");
|
}
|
|
//计算子单是否还有剩余的可扣疗程数
|
int surplusNum = orderItemSon.getUsedTotal() - orderItemSon.getHasReNum();
|
if(refundRecordItemSource.getRefundNum() > surplusNum){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款次数大于可退次数[56]!");
|
}
|
|
//处理用户项目,减去用户项目数量
|
UserProjectDeductionVo userProjectDeductionVo= UserProjectTool.userProjectDeduction(userProjectItem,UserProjectUsedCon.USED_METHOD_ORDER_REFUND,UserProjectUsedCon.USED_TYPE_DEDUCTION,null
|
,refundRecordItemSource.getId(),refundRecordItemSource.getRefundNum(),refundRecord.getOperatorAppId(),refundRecord.getOperatorAppName(),refundRecord.getRefundShopId(),refundRecord.getRefundShopName(),"员工备注:"+refundRecord.getRemarks()+"|用户备注:"+refundRecord.getRefundReason(),commonService);
|
|
refundCarryVo.setDeductionTotalUser(userProjectDeductionVo.getDeductionTotal());
|
|
int refundStatus;
|
if(surplusNum == refundRecordItemSource.getRefundNum()){
|
refundStatus=OrderTotalConstants.STATUS_REFUND_FINSH;
|
}else{
|
refundStatus=OrderTotalConstants.STATUS_REFUND_PART;
|
}
|
|
//处理退款支付方式
|
refundCarryVo = refundRecordMotnedItemTwoHandle(refundRecord,refundRecordItemSource,refundCarryVo,commonService);
|
//更新子单信息
|
updateOrderItemTwo(orderItemSon,refundStatus,refundCarryVo,refundRecordItemSource.getRefundNum(),commonService);
|
|
if(refundRecordItem.getType().equals(OrderItemConstants.CARD_BAG)){
|
//是卡包的,刪除卡包使用
|
deleteUserCardUsed(userCardId,orderItemSon.getCardItemInfoId(),orderItemSon.getId(),orderItemSon.getOrderId(),refundRecordItemSource.getRefundNum(),commonService);
|
}
|
|
return refundCarryVo;
|
}
|
|
/**退款二级是商品
|
* @param refundRecord 退款总记录
|
* @param refundRecordItem 退款一级子单
|
* @param refundRecordItemSource 退款二级子单
|
* @param refundCarryVo 全局携带参数结构
|
* @param commonService 映射
|
* @return 全局携带参数结构
|
*/
|
private static RefundCarryVo handRefundSonRerail(RefundRecord refundRecord,RefundRecordItem refundRecordItem, RefundRecordItemSource refundRecordItemSource
|
,RefundCarryVo refundCarryVo,String userCardId,CommonService commonService) {
|
|
//判断操作完了去修改子订单状态
|
OrderItemSon orderItemSon=commonService.selectOneByKey(OrderItemSonMapper.class,refundRecordItemSource.getOrderItemSonId());
|
if (orderItemSon==null){
|
throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"未找到二级子订单信息");
|
}
|
|
//计算子单是否还有剩余的可扣疗程数
|
int surplusNum = orderItemSon.getBuyNum() - orderItemSon.getHasReNum();
|
if(refundRecordItemSource.getRefundNum() > surplusNum){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款次数大于可退次数[84]!");
|
}
|
|
int refundStatus;
|
if(surplusNum == refundRecordItemSource.getRefundNum()){
|
refundStatus=OrderTotalConstants.STATUS_REFUND_FINSH;
|
}else{
|
refundStatus=OrderTotalConstants.STATUS_REFUND_PART;
|
}
|
|
//处理退款支付方式
|
refundCarryVo = refundRecordMotnedItemTwoHandle(refundRecord,refundRecordItemSource,refundCarryVo,commonService);
|
|
//子单信息处理
|
updateOrderItemTwo(orderItemSon,refundStatus,refundCarryVo,refundRecordItemSource.getRefundNum(),commonService);
|
|
if(refundRecordItem.getType().equals(OrderItemConstants.CARD_BAG)){
|
//是卡包的,刪除卡包使用
|
deleteUserCardUsed(userCardId,orderItemSon.getCardItemInfoId(),orderItemSon.getId(),orderItemSon.getOrderId(),refundRecordItemSource.getRefundNum(),commonService);
|
}
|
|
return refundCarryVo;
|
}
|
|
/**更新订单一级子单的信息
|
* @param orderItem 订单子单
|
* @param refundStatus 退款状态
|
* @param refundCarryVo 传参
|
* @param refundNum 退款数量 正负数
|
* @param commonService 映射
|
*/
|
public static void updateOrderItemOne(OrderItem orderItem,Integer refundStatus,RefundCarryVo refundCarryVo,Integer refundNum
|
,CommonService commonService){
|
SqlSentence sqlSentence = new SqlSentence();
|
Map<String,Object> values = new HashMap<>();
|
|
values.put("id", orderItem.getId());
|
values.put("refundStatus", refundStatus);
|
values.put("oldHasReNum",orderItem.getHasReNum());
|
values.put("refundNum",refundNum);
|
values.put("reTotal",refundCarryVo.getRefundTotal());
|
values.put("reIntegralTotal",refundCarryVo.getIntegralTotal());
|
values.put("reCashTotal",refundCarryVo.getCashTotal());
|
values.put("reCashPurenessTotal",refundCarryVo.getCashPurenessTotal());
|
values.put("reRechargeTotal",refundCarryVo.getRechargeTotal());
|
values.put("reIncrementTotal",refundCarryVo.getIncrementTotal());
|
sqlSentence.sqlUpdate(" refundStatus=#{m.refundStatus},reTotal=reTotal+#{m.reTotal},hasReNum=hasReNum+#{m.refundNum}" +
|
",reIntegralTotal = reIntegralTotal+#{m.reIntegralTotal},reCashTotal = reCashTotal+#{m.reCashTotal},reCashPurenessTotal = reCashPurenessTotal+#{m.reCashPurenessTotal}" +
|
",reRechargeTotal = reRechargeTotal+#{m.reRechargeTotal},reIncrementTotal = reIncrementTotal+#{m.reIncrementTotal}" +
|
" WHERE isDel = 0 AND id = #{m.id} AND hasReNum = #{m.oldHasReNum}",values);
|
if(commonService.updateWhere(OrderItemMapper.class,sqlSentence) != 1){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"当前订单信息已发生变化,请重试[012]!");
|
}
|
}
|
|
/**更新订单二级子单的信息
|
* @param orderItemSon 订单子单
|
* @param refundStatus 退款状态
|
* @param refundCarryVo 传参
|
* @param refundNum 退款数量
|
* @param commonService 映射
|
*/
|
public static void updateOrderItemTwo(OrderItemSon orderItemSon,Integer refundStatus,RefundCarryVo refundCarryVo,Integer refundNum
|
,CommonService commonService){
|
SqlSentence sqlSentence = new SqlSentence();
|
Map<String,Object> values = new HashMap<>();
|
|
values.put("id", orderItemSon.getId());
|
values.put("refundStatus", refundStatus);
|
values.put("oldHasReNum",orderItemSon.getHasReNum());
|
values.put("reTotal",refundCarryVo.getRefundTotal());
|
values.put("refundNum",refundNum);
|
values.put("reIntegralTotal",refundCarryVo.getIntegralTotal());
|
values.put("reCashTotal",refundCarryVo.getCashTotal());
|
values.put("reCashPurenessTotal",refundCarryVo.getCashPurenessTotal());
|
values.put("reRechargeTotal",refundCarryVo.getRechargeTotal());
|
values.put("reIncrementTotal",refundCarryVo.getIncrementTotal());
|
sqlSentence.sqlUpdate(" refundStatus = #{m.refundStatus},reTotal=reTotal+#{m.reTotal},hasReNum=hasReNum+#{m.refundNum}" +
|
",reIntegralTotal = reIntegralTotal+#{m.reIntegralTotal},reCashTotal = reCashTotal+#{m.reCashTotal},reCashPurenessTotal = reCashPurenessTotal+#{m.reCashPurenessTotal}" +
|
",reRechargeTotal = reRechargeTotal+#{m.reRechargeTotal},reIncrementTotal = reIncrementTotal+#{m.reIncrementTotal}" +
|
" where isDel=0 AND id = #{m.id} AND hasReNum = #{m.oldHasReNum}",values);
|
if(commonService.updateWhere(OrderItemSonMapper.class,sqlSentence) != 1){
|
throw new PlatTipsException(PlatformCode.ERROR_TIPS,"当前订单信息已发生变化,请重试[012]!");
|
}
|
}
|
|
/**获取订单一级子单的支付方式记录
|
* @param orderItemId 订单一级子单标识
|
* @param commonService 映射
|
* @return 订单一级子单的支付方式记录
|
*/
|
public static List<ConsumePayItem> getOrderItemOneConsumePay(String orderItemId,CommonService commonService){
|
|
SqlSentence sqlSentence = new SqlSentence();
|
Map<String,Object> values = new HashMap<>();
|
|
//获取子单的支付方式,一级子单支付记录,计算可退款金额
|
values.put("typeId",orderItemId);
|
sqlSentence.sqlSentence("SELECT * FROM consume_pay_item WHERE isDel = 0 AND typeId = #{m.typeId}",values);
|
return commonService.selectList(ConsumePayItemMapper.class,sqlSentence);
|
}
|
|
/**获取订单二级子单的支付方式记录
|
* @param orderItemId 订单一级子单标识
|
* @param commonService 映射
|
* @return 订单二级子单的支付方式记录
|
*/
|
public static List<ConsumePayItem> getOrderItemTwoConsumePay(String orderItemId,CommonService commonService){
|
|
SqlSentence sqlSentence = new SqlSentence();
|
Map<String,Object> values = new HashMap<>();
|
|
//获取子单的支付方式,一级子单支付记录,计算可退款金额
|
values.put("typeId",orderItemId);
|
sqlSentence.sqlSentence("SELECT * FROM consume_pay_item_son WHERE isDel = 0 AND typeId = #{m.typeId}",values);
|
List<ConsumePayItemSon> consumePayItemSonList = commonService.selectList(ConsumePayItemSonMapper.class,sqlSentence);
|
List<ConsumePayItem> consumePayItemList = new ArrayList<>();
|
ConsumePayItem consumePayItem;
|
for(ConsumePayItemSon consumePayItemSon:consumePayItemSonList){
|
consumePayItem = new ConsumePayItem();
|
BeanUtils.copyProperties(consumePayItemSon,consumePayItem);
|
consumePayItemList.add(consumePayItem);
|
}
|
return consumePayItemList;
|
}
|
|
/**保存退款方式和支付方式关联
|
* @param refundTotal 退款金额
|
* @param commonType 子单级别,可空
|
* @param commonId 子单标识,可空
|
* @param orderId 总单标识
|
* @param numberNo 支付方式编号
|
* @param name 支付方式名称
|
* @param consumePayId 支付方式记录标识
|
* @param refundMethodId 退款方式标识
|
* @param refundRecordItemId 退款子记录标识,可空
|
* @param refundRecordId 退款总记录标识
|
* @param commonService 映射
|
* @return 返回关联记录
|
*/
|
public static RefundRecordConsumePay insertRefundRecordConsumePay(BigDecimal refundTotal, String commonType, String commonId, String orderId
|
, String numberNo,String name,Integer isMoneyPay,Integer isExecute, String consumePayId, String refundMethodId,String refundRecordItemId,String refundRecordId,CommonService commonService){
|
//生成关联记录
|
RefundRecordConsumePay refundRecordConsumePay = new RefundRecordConsumePay();
|
refundRecordConsumePay.setRefundTotal(refundTotal);
|
refundRecordConsumePay.setCommonType(commonType);
|
refundRecordConsumePay.setCommonId(commonId);
|
refundRecordConsumePay.setOrderId(orderId);
|
refundRecordConsumePay.setName(name);
|
refundRecordConsumePay.setNumberNo(numberNo);
|
refundRecordConsumePay.setIsMoneyPay(isMoneyPay);
|
refundRecordConsumePay.setIsExecute(isExecute);
|
refundRecordConsumePay.setConsumePayId(consumePayId);
|
refundRecordConsumePay.setRefundMethodId(refundMethodId);
|
refundRecordConsumePay.setRefundRecordItemId(refundRecordItemId);
|
refundRecordConsumePay.setRefundRecordId(refundRecordId);
|
commonService.insert(RefundRecordConsumePayMapper.class,refundRecordConsumePay);
|
return refundRecordConsumePay;
|
}
|
|
/**获取可退款的用户卡项
|
* @param sourceId 订单子单标识
|
* @return 可退款的用户卡项
|
*/
|
public static List<UserCard> getRefundCard(String sourceId,int effectiveStatus,CommonService commonService){
|
SqlSentence sqlSentence = new SqlSentence();
|
Map<String,Object> sqlMap = new HashMap<>();
|
//获取用户卡项
|
sqlMap.put("sourceId",sourceId);
|
sqlMap.put("status",UserCard.TYPE_NO_USED);
|
sqlMap.put("effectiveStatus",effectiveStatus);
|
sqlSentence.sqlSentence("SELECT * FROM user_card WHERE isDel = 0 AND sourceId = #{m.sourceId} AND status = #{m.status}" +
|
" AND effectiveStatus = #{m.effectiveStatus} AND turnAddId IS NULL",sqlMap);
|
List<UserCard> userCardList = commonService.selectList(UserCardMapper.class,sqlSentence);
|
if(userCardList.size() == 0){
|
return userCardList;
|
}
|
|
//获取已经部分退的用户卡包数量
|
List<RefundRecordItem> refundRecordItemList = CardRefundTool.findRefundUserCard(sourceId,null,commonService);
|
//过滤掉没有使用但有部分退款
|
if(refundRecordItemList.size() == 0){
|
return userCardList;
|
}
|
|
Map<String,UserCard> userCardMap = new HashMap<>();
|
for(UserCard userCard:userCardList){
|
userCardMap.put(userCard.getId(),userCard);
|
}
|
//去除掉参与部分退款的用户卡包
|
for(RefundRecordItem refundRecordItem:refundRecordItemList){
|
userCardMap.remove(refundRecordItem.getUserCardId());
|
}
|
|
userCardList = new ArrayList<>();
|
List<UserCardUsed> userCardUsedList;
|
for (Map.Entry<String,UserCard> entry : userCardMap.entrySet()) {
|
//获取使用记录,如果有使用记录,那么就跳过
|
userCardUsedList = UserCardTool.getUsedRecord(entry.getValue().getId(),null,null,commonService);
|
if(userCardUsedList.size() > 0){
|
continue;
|
}
|
userCardList.add(entry.getValue());
|
}
|
|
return userCardList;
|
|
}
|
|
/**获取用户项目*/
|
public static UserProjectItem getUserProject(String commonId,CommonService commonService){
|
SqlSentence sqlSentence = new SqlSentence();
|
Map<String, Object> map = new HashMap<>();
|
|
map.put("commonId",commonId);
|
sqlSentence.sqlSentence("select * from user_project_item where isDel=0 and commonId=#{m.commonId} and isTransfer = 0",map);
|
return commonService.selectOne(UserProjectItemMapper.class,sqlSentence);
|
}
|
|
/**作废划扣记录
|
* @param refundRecordItemId 退款子记录标识(RefundRecordItem/RefundRecordItemSource)
|
* @param refundRecord 退款总记录
|
* @param commonService 映射
|
* @param deductionSingleService 映射
|
*/
|
public static void deductionCancel(RefundRecord refundRecord,String refundRecordItemId, CommonService commonService, DeductionSingleService deductionSingleService){
|
|
SqlSentence sqlSentence = new SqlSentence();
|
Map<String,Object> values = new HashMap<>();
|
|
values.put("refundRecordItemId",refundRecordItemId);
|
sqlSentence.sqlSentence("SELECT * FROM refund_deduction_cancel WHERE isDel = 0 AND refundRecordItemId = #{m.refundRecordItemId}",values);
|
List<RefundDeductionCancel> refundDeductionCancelList = commonService.selectList(RefundDeductionCancelMapper.class,sqlSentence);
|
|
//变更状态
|
values.put("status",RefundDeductionCancel.STATUS_CANCEL);
|
values.put("statusNot",RefundDeductionCancel.STATUS_NOT);
|
sqlSentence.sqlUpdate("status = #{m.status} WHERE id = #{m.id} AND status = #{m.statusNot}",values);
|
|
//开始作废划扣
|
DeductionDto deductionDto;
|
for(RefundDeductionCancel refundDeductionCancel:refundDeductionCancelList){
|
if(refundDeductionCancel.getStatus() == RefundDeductionCancel.STATUS_CANCEL){
|
continue;
|
}
|
deductionDto = new DeductionDto();
|
deductionDto.setDeductionSingleId(refundDeductionCancel.getDeductionSingleId());
|
deductionDto.setOperatorId(refundRecord.getOperatorId());
|
deductionSingleService.deleteOne(deductionDto,refundRecord.getOperatorAppId(),refundRecord.getOperatorAppName(),DeductionSingleConstants.STATUS_RESCINDED, BaseEntity.NO);
|
|
//变更状态
|
values.put("id",refundDeductionCancel.getId());
|
if(commonService.updateWhere(RefundDeductionCancelMapper.class,sqlSentence) != 1){
|
throw new TipsException("作废划扣记录失败");
|
}
|
}
|
}
|
|
}
|