chenjiahe
2023-03-31 1869f358a6aaccf4e4f76df73f10ef5e1e5d8313
phi_platform_user/src/main/java/com/hx/phip/util/api/PartialRefundUtil.java
File was renamed from phi_platform_common/src/main/java/com/hx/phip/tool/order/PartialRefundUtil.java
@@ -1,4 +1,4 @@
package com.hx.phip.tool.order;
package com.hx.phip.util.api;
import com.hx.common.service.CommonService;
import com.hx.exception.TipsException;
@@ -12,7 +12,6 @@
import com.hx.phiappt.model.activity.ActivityRule;
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.*;
@@ -24,7 +23,8 @@
import com.hx.phip.config.CustomParameter;
import com.hx.phip.dao.mapper.*;
import com.hx.phip.tool.user.UserProjectTool;
import com.hx.phip.vo.order.payment.PayMethodVo;
import com.hx.phip.vo.order.refund.RefundCarryVo;
import com.hx.phip.vo.user.UserProjectDeductionVo;
import com.hx.util.StringUtils;
import com.platform.exception.PlatTipsException;
import com.platform.resultTool.PlatformCode;
@@ -83,6 +83,15 @@
        if(userMoney==null){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到该用户的资金信息");
        }
        //获取总退款方式
        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);
        //全程使用携带参数对象
        RefundCarryVo refundCarryVo = new RefundCarryVo();
        refundCarryVo.setRefundRecordMethodList(refundRecordMethodList);
        //处理优惠券
        handCoupon(refundRecord,commonService);
@@ -251,39 +260,140 @@
        }
    }
    /**处理一级子退款方式数据*/
    public static List<RefundRecordConsumePay> refundRecordMotnedItemHandle(RefundRecord refundRecord,RefundRecordItem refundRecordItem
            ,List<RefundRecordMethod> refundRecordMethodList,CommonService commonService){
    /**处理一级子退款方式数据
     * @param deductionTotalUser 用户项目被扣减的划扣金额,可空
     * @param refundRecord 退款记录总表
     * @param refundRecordItem 退款记录子表
     * @param refundCarryVo 总携带参数结构
     * @param commonService 映射
     * @return 总携带参数结构
     */
    public static RefundCarryVo refundRecordMotnedItemHandle(BigDecimal deductionTotalUser,RefundRecord refundRecord,RefundRecordItem refundRecordItem
            ,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<>();
        //获取退款方式信息
        values.put("refundRecordItemId",refundRecordItem.getId());
        sqlSentence.sqlSentence("SELECT * FROM refund_record_item_method WHERE isDel = 0 AND refundRecordItemId = #{m.refundRecordItemId}",values);
        List<RefundRecordItemMethod> refundRecordItemMethodList = commonService.selectList(RefundRecordItemMethodMapper.class,sqlSentence);
        //装载支付方式退款信息
        List<RefundRecordConsumePay> refundConsumePayList = new ArrayList<>();
        //没有退款方式,跳过当前处理
        if(refundRecordItemMethodList.size() == 0){
            return refundConsumePayList;
        //支付方式占比
        BigDecimal payMothedPercentage;
        //计算退款方式的占比
        if(refundRecord.getRefundTotal().compareTo(BigDecimal.ZERO) > 0){
            payMothedPercentage = refundRecordItem.getRefundMoney().divide(refundRecord.getRefundTotal(),15,RoundingMode.HALF_UP);
        }else{
            payMothedPercentage = BigDecimal.ZERO;
        }
        //获取子单的支付方式,一级子单支付记录
        values.clear();
        values.put("typeId",refundRecordItem.getOrderItemId());
        sqlSentence.sqlSentence("SELECT * FROM consume_pay_item WHERE isDel = 0 AND typeId = #{m.typeId}",values);
        List<ConsumePayItem> consumePayItemList = commonService.selectList(ConsumePayMapper.class,sqlSentence);
        //处理退款方式
        refundCarryVo = insertRefundItemMothed(refundRecordItem.getId(),refundRecordItem.getRefundMoney(),payMothedPercentage,OrderSourceConstans.TYPE_PROJECT
                ,refundRecordItem.getOrderItemId(),refundCarryVo,refundRecord,commonService);
        //更新现金和划扣金额保存
        values.put("deductionTotal",refundCarryVo.getDeductionTotal());
        values.put("deductionTotalUser",deductionTotalUser==null?BigDecimal.ZERO:deductionTotalUser);
        values.put("cashTotal",refundCarryVo.getCashTotal());
        values.put("id",refundRecordItem.getId());
        sqlSentence.sqlUpdate("deductionTotal = #{m.deductionTotal},deductionTotalUser = #{m.deductionTotalUser},cashTotal = #{m.cashTotal} WHERE id = #{m.id}",values);
        if(commonService.updateWhere(RefundRecordItemMapper.class,sqlSentence) != 1){
            throw new TipsException("更新退款子项信息失败!");
        }
        return refundCarryVo;
    }
    /**处理二级子退款方式数据
     * @param deductionTotalUser 用户项目被扣减的划扣金额
     * @param refundRecord  退款记录总表
     * @param refundRecordItemSource 退款记录子表
     * @param refundCarryVo 总携带参数结构
     * @param commonService 映射
     * @return 总携带参数结构
     */
    public static RefundCarryVo refundRecordMotnedItemTwoHandle(BigDecimal deductionTotalUser,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(),payMothedPercentage,OrderSourceConstans.TYPE_RETAIL
                ,refundRecordItemSource.getOrderItemSonId(),refundCarryVo,refundRecord,commonService);
        values.put("deductionTotal",refundCarryVo.getDeductionTotal());
        values.put("deductionTotalUser",deductionTotalUser==null?BigDecimal.ZERO:deductionTotalUser);
        values.put("cashTotal",refundCarryVo.getCashTotal());
        values.put("id",refundRecordItemSource.getId());
        sqlSentence.sqlUpdate("deductionTotal = #{m.deductionTotal},deductionTotalUser = #{m.deductionTotalUser},cashTotal = #{m.cashTotal} WHERE id = #{m.id}",values);
        if(commonService.updateWhere(RefundRecordItemSourceMapper.class,sqlSentence) != 1){
            throw new TipsException("更新退款子项信息失败[94]!");
        }
        return refundCarryVo;
    }
    /**退款子单退款方式处理保存
     * @param refundItemId 退款子单标识
     * @param refundItemTotal 退款子单实际需要退款总金额
     * @param payMothedPercentage 支付方式占比
     * @param orderItemType 订单子单级别
     * @param orderItemId 订单子单标识
     * @param refundCarryVo 总结构对象
     * @param refundRecord 退款总记录
     * @param commonService 映射
     * @return 总结构对象
     */
    public static RefundCarryVo insertRefundItemMothed(String refundItemId,BigDecimal refundItemTotal,BigDecimal payMothedPercentage,String orderItemType
            ,String orderItemId,RefundCarryVo refundCarryVo,RefundRecord refundRecord,CommonService commonService){
        //总退款方式金额数据
        List<RefundRecordMethod> refundRecordMethodList = refundCarryVo.getRefundRecordMethodList();
        //进行升序排序,避免后面不够分配
        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 = getRefundRecordConsumePay(refundRecordItem.getOrderItemId(),null,false,true,commonService);
        //转化成map
        List<RefundRecordConsumePay> refundRecordConsumePayList = 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){
@@ -292,97 +402,93 @@
                //可退款金额
                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);
        }
        //进行升序排序,避免后面不够分配
        refundRecordMethodList = refundRecordMethodList.stream().sorted(Comparator.comparing(RefundRecordMethod::getpTotal)).collect(Collectors.toList());
        consumePayItemList = consumePayItemList.stream().sorted(Comparator.comparing(ConsumePayItem::getpTotal)).collect(Collectors.toList());
        for(ConsumePayItem consumePayItem:consumePayItemList){
            //支付编码集合整合
            noPayItemList = noPayItemMap.computeIfAbsent(consumePayItem.getNumberNo(),k->new ArrayList<>());
            noPayItemList.add(consumePayItem);
        }
        //退款的划扣金额
        BigDecimal deductionTotal = BigDecimal.ZERO;
        //退款的现金金额
        BigDecimal cashTotal = BigDecimal.ZERO;
        //退款方式总金额
        BigDecimal mothedTotal;
        //支付方式扣减金额
        BigDecimal mothedCutTotal;
        ///////引用对象
        List<RefundRecordConsumePay> refundConsumePayList = new ArrayList<>();
        ////引用对象
        RefundRecordItemMethod refundRecordItemMethod;
        //支付方式占比
        BigDecimal payMothedPercentage;
        RefundRecordMethod refundRecordMethod;
        //需要退的退款编号金额
        BigDecimal mothedTotal;
        //支付编号分配的退款金额
        BigDecimal mothedCutTotal;
        //分配的划扣金额总和
        BigDecimal deductionTotal = BigDecimal.ZERO;
        //分配的现金金额总和
        BigDecimal cashTotal = BigDecimal.ZERO;
        for(RefundRecordMethod refundRecordMethod:refundRecordMethodList){
            //计算退款方式的占比
            if(refundRecord.getRefundTotal().compareTo(BigDecimal.ZERO) > 0){
                payMothedPercentage = refundRecordItem.getRefundMoney().divide(refundRecord.getRefundTotal(),15,RoundingMode.HALF_UP);
            }else{
                payMothedPercentage = BigDecimal.ZERO;
        ////退款方式金额分配
        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());
            //计算退款方式的金额
            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);
            }
            //子单退款方式
            BigDecimal refundTotalItem = refundRecordItem.getRefundMoney();
            refundRecordItemMethod.setCommonType(orderItemType);
            refundRecordItemMethod.setCommonId(orderItemId);
            refundRecordItemMethod.setOrderId(refundRecordMethod.getOrderId());
            refundRecordItemMethod.setRefundRecordItemId(refundItemId);
            refundRecordItemMethod.setRefundRecordId(refundRecord.getId());
            commonService.insert(RefundRecordItemMethodMapper.class,refundRecordItemMethod);
            for(int j = 0;j <refundRecordMethodList.size();j++){
                refundRecordMethod = refundRecordMethodList.get(j);
            //减去已经分配的退款方式金额
            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);
                ////子项退款方式填充
                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());
                //退款金额
                if(j == refundRecordMethodList.size()-1){
                    ////最后一个
                    refundRecordItemMethod.setActualTotal(refundTotalItem);
                }else {
                    ////不是最后一个
                    refundRecordItemMethod.setActualTotal(refundRecordMethod.getActualTotal().multiply(payMothedPercentage).setScale(2,RoundingMode.UP));
                }
                if(refundRecordItemMethod.getActualTotal().compareTo(refundRecordMethod.getActualTotal()) > 0){
                    refundRecordItemMethod.setActualTotal(refundRecordMethod.getActualTotal());
                }
                if(refundRecordItemMethod.getActualTotal().compareTo(refundTotalItem) > 0){
                    refundRecordItemMethod.setActualTotal(refundTotalItem);
                }
                //判断与剩余的未分配退款方式金额
                if (refundRecordItemMethod.getActualTotal().compareTo(refundRecordMethod.getpTotal()) > 0){
                    refundRecordItemMethod.setActualTotal(refundRecordMethod.getpTotal());
                }
                refundRecordItemMethod.setCommonType(OrderSourceConstans.TYPE_PROJECT);
                refundRecordItemMethod.setCommonId(refundRecordItem.getOrderItemId());
                refundRecordItemMethod.setOrderId(refundRecordMethod.getOrderId());
                refundRecordItemMethod.setRefundRecordItemId(refundRecordItem.getId());
                refundRecordItemMethod.setRefundRecordId(refundRecord.getId());
                commonService.insert(RefundRecordItemMethodMapper.class,refundRecordItemMethod);
                //转载分配好的
                refundRecordItemMethodList.add(refundRecordItemMethod);
                //减去已经分配的退款方式金额
                refundRecordMethod.setpTotal(refundRecordMethod.getpTotal().subtract(refundRecordItemMethod.getActualTotal()).setScale(2,RoundingMode.HALF_UP));
                //计算子项剩余可分配金额
                refundTotalItem = refundTotalItem.subtract(refundRecordItemMethod.getActualTotal()).setScale(2,RoundingMode.HALF_UP);
        }
        if(refundTotalItem.compareTo(BigDecimal.ZERO) > 0){
            throw new TipsException("分配退款金额错误[77]");
        }
        }
        //增加增值金
        for(RefundRecordItemMethod refundRecordItemMethod:refundRecordItemMethodList){
            //当前退款方式的退款金额
            ///////生成关联支付方式记录和退款方式关联
            noPayItemList = noPayItemMap.computeIfAbsent(refundRecordItemMethod.getNumberNo(),k->new ArrayList<>());
            mothedTotal = refundRecordItemMethod.getActualTotal();
            for(ConsumePayItem consumePayItem:consumePayItemList){
            for(ConsumePayItem consumePayItem:noPayItemList){
                if(consumePayItem.getpTotal().compareTo(BigDecimal.ZERO) < 1){
                    continue;
                }
@@ -393,147 +499,41 @@
                    mothedCutTotal = consumePayItem.getpTotal();
                }
                //更新支付记录信息
                values.clear();
                values.put("refundTotal",mothedCutTotal);
                values.put("id",consumePayItem.getId());
                sqlSentence.sqlUpdate("refundTotal = refundTotal + #{m.refundTotal} WHERE id = #{m.id} AND actualTotal >= refundTotal + #{m.refundTotal}",values);
                if(commonService.updateWhere(ConsumePayItemMapper.class,sqlSentence) != 1){
                    throw new TipsException("退款金额错误[424]!");
                }
                //划扣金额
                if(consumePayItem.getIsMoneyPay().equals(ConsumePayItem.YES)){
                    deductionTotal = deductionTotal.add(mothedCutTotal).setScale(2,RoundingMode.HALF_UP);
                }
                //现金金额
                if(consumePayItem.getIsExecute().equals(ConsumePayItem.YES)){
                    cashTotal = cashTotal.add(mothedCutTotal).setScale(2,RoundingMode.HALF_UP);
                }
                //生成关联记录
                refundRecordConsumePay = insertRefundRecordConsumePay(mothedCutTotal,refundRecordItemMethod.getCommonType(),refundRecordItemMethod.getCommonId(),refundRecord.getOrderId(),consumePayItem.getNumberNo()
                        ,consumePayItem.getName(),consumePayItem.getId(),refundRecordItemMethod.getId(),refundRecordItem.getId(),refundRecord.getId(),commonService);
                refundRecordConsumePayList.add(refundRecordConsumePay);
                        ,consumePayItem.getName(),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){
                throw new TipsException("退款金额错误[543]!");
            }
            //更新现金和划扣金额保存
            values.put("deductionTotal",deductionTotal);
            values.put("cashTotal",cashTotal);
            values.put("id",refundRecordItem.getId());
            sqlSentence.sqlUpdate("deductionTotal = #{m.deductionTotal},cashTotal = #{m.cashTotal} WHERE id = #{m.id}",values);
            if(commonService.updateWhere(RefundRecordItemMapper.class,sqlSentence) != 1){
                throw new TipsException("更新退款子项信息失败!");
            }
        }
        return refundRecordConsumePayList;
    }
    /**处理二级子退款方式数据*/
    public static void refundRecordMotnedItemTwoHandle(RefundRecord refundRecord,RefundRecordItemSource refundRecordItemSource,CommonService commonService){
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
        //获取退款方式
        values.put("refundRecordItemId",refundRecordItemSource.getId());
        sqlSentence.sqlSentence("SELECT * FROM refund_record_item_method WHERE isDel = 0 AND refundRecordItemId = #{m.refundRecordItemId}",values);
        List<RefundRecordItemMethod> refundRecordItemMethodList = commonService.selectList(RefundRecordItemMethodMapper.class,sqlSentence);
        //退款方式总金额
        BigDecimal mothedTotal;
        //支付方式扣减金额
        BigDecimal mothedCutTotal;
        //引用对象
        RefundRecordConsumePay refundRecordConsumePay;
        List<ConsumePayItemSon> consumePayItemSonList;
        BigDecimal deductionTotal = BigDecimal.ZERO;
        BigDecimal cashTotal = BigDecimal.ZERO;
        //增加增值金
        for(RefundRecordItemMethod refundRecordItemMethod:refundRecordItemMethodList){
            mothedTotal = refundRecordItemMethod.getActualTotal();
            //修改实际退款金额
            values.put("realRefundTotal",refundRecordItemMethod.getActualTotal());
            values.put("id",refundRecordItemMethod.getId());
            sqlSentence.sqlUpdate(" realRefundTotal=#{m.realRefundTotal} where id = #{m.id}",values);
            if(commonService.updateWhere(RefundRecordItemMethodMapper.class,sqlSentence) != 1){
                throw new TipsException("更新退款信息错误[001]");
            }
            //获取支付方式
            values.put("orderId",refundRecord.getOrderId());
            values.put("numberNo",refundRecordItemMethod.getNumberNo());
            values.put("typeId",refundRecordItemMethod.getCommonId());
            //一级子单支付记录
            sqlSentence.sqlSentence("SELECT *,ROUND(actualTotal-refundTotal,2) AS pTotal FROM consume_pay_item_son WHERE isDel = 0 AND orderId = #{m.orderId} AND numberNo = #{m.numberNo} AND typeId = #{m.typeId} ORDER BY pTotal ASC",values);
            consumePayItemSonList = commonService.selectList(ConsumePayMapper.class,sqlSentence);
            for(ConsumePayItemSon consumePayItemSon:consumePayItemSonList){
                if(consumePayItemSon.getpTotal().compareTo(BigDecimal.ZERO) < 1){
                    continue;
                }
                //计算扣减金额
                if(consumePayItemSon.getpTotal().compareTo(mothedTotal) > 0){
                    mothedCutTotal = mothedTotal;
                }else{
                    mothedCutTotal = consumePayItemSon.getpTotal();
                }
                //更新支付记录信息
                values.clear();
                values.put("refundTotal",mothedCutTotal);
                values.put("id",consumePayItemSon.getId());
                sqlSentence.sqlUpdate("refundTotal = refundTotal + #{m.refundTotal} WHERE id = #{m.id} AND actualTotal >= refundTotal + #{m.refundTotal}",values);
                if(commonService.updateWhere(ConsumePayItemSonMapper.class,sqlSentence) != 1){
                    throw new TipsException("退款金额错误[426]!");
                }
                //划扣金额
                if(consumePayItemSon.getIsMoneyPay().equals(ConsumePayItem.YES)){
                    deductionTotal = deductionTotal.add(mothedCutTotal).setScale(2,RoundingMode.HALF_UP);
                }
                if(consumePayItemSon.getIsExecute().equals(ConsumePayItem.YES)){
                    cashTotal = cashTotal.add(mothedCutTotal).setScale(2,RoundingMode.HALF_UP);
                }
                //生成关联记录
                insertRefundRecordConsumePay(mothedCutTotal,refundRecordItemMethod.getCommonType(),refundRecordItemMethod.getCommonId(),refundRecord.getOrderId(),consumePayItemSon.getNumberNo()
                        ,consumePayItemSon.getName(),consumePayItemSon.getId(),refundRecordItemMethod.getId(),refundRecordItemSource.getId(),refundRecord.getId(),commonService);
                //计算为分配金额
                mothedTotal = mothedTotal.subtract(mothedCutTotal).setScale(2, RoundingMode.HALF_UP);
                if(mothedTotal.compareTo(BigDecimal.ZERO) < 1){
                    //跳出循环
                    break;
                }
            }
            values.put("deductionTotal",deductionTotal);
            values.put("cashTotal",cashTotal);
            values.put("id",refundRecordItemSource.getId());
            sqlSentence.sqlUpdate("deductionTotal = #{m.deductionTotal},cashTotal = #{m.cashTotal} WHERE id = #{m.id}",values);
            if(commonService.updateWhere(RefundRecordItemSourceMapper.class,sqlSentence) != 1){
                throw new TipsException("更新退款子项信息失败!");
            }
            if(mothedTotal.compareTo(BigDecimal.ZERO) > 0){
                throw new TipsException("退款金额错误[427]!");
            }
        //判断是否已经分配完
        if(refundItemTotal.compareTo(BigDecimal.ZERO) > 0){
            throw new TipsException("分配退款金额错误["+orderItemType+"]");
        }
        refundCarryVo.setDeductionTotal(deductionTotal);
        refundCarryVo.setCashTotal(cashTotal);
        refundCarryVo.setRefundConsumePayList(refundConsumePayList);
        return refundCarryVo;
    }
@@ -635,15 +635,10 @@
     * @param refundRecord
     * @param commonService
     */
    public static void numberOfRefunds(String operationId, String operationNme, RefundRecord refundRecord,OrdersTotal ordersTotal,CustomParameter customParameter,CommonService commonService) {
    public static RefundCarryVo numberOfRefunds(RefundCarryVo refundCarryVo,String operationId, String operationNme, RefundRecord refundRecord,OrdersTotal ordersTotal,CustomParameter customParameter,CommonService commonService) {
        SqlSentence sqlSentence = new SqlSentence();
        Map<String, Object> map = new HashMap<>();
        //获取总退款方式
        map.put("refundRecordId",refundRecord.getId());
        sqlSentence.sqlSentence("SELECT * FROM refund_record_method WHERE isDel = 0 AND refundRecordId = #{m.refundRecordId}",map);
        List<RefundRecordMethod> refundRecordMethodList = commonService.selectList(RefundRecordMethodMapper.class,sqlSentence);
        //获取退款子单
        map.clear();
@@ -654,31 +649,54 @@
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到退款子订单信息");
        }
        List<RefundRecordConsumePay> refundRecordConsumePayList;
        //根据支付记录总表的标识来整合已退的金额,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);
            switch (refundRecordItem.getType()){
                case OrderItemConstants.TYPE_RETAIL:
                    handRefundRerail(refundRecord,refundRecordItem,commonService);
                    refundCarryVo = handRefundRerail(refundRecord,refundRecordItem,refundCarryVo,commonService);
                    break;
                case OrderItemConstants.TYPE_DRUG:
                    handRefundRerail(refundRecord,refundRecordItem,commonService);
                    refundCarryVo = handRefundRerail(refundRecord,refundRecordItem,refundCarryVo,commonService);
                    break;
                case OrderItemConstants.TYPE_PROJECT:
                    handRefundNoExecution(commonService, operationId, operationNme, refundRecord,v);
                    refundCarryVo = handRefundNoExecution(refundRecord,refundRecordItem,refundCarryVo,commonService);
                    break;
                case OrderItemConstants.TYPE_PROMOTION:
                    handRefundPromotion(commonService, operationId, operationNme, sqlSentence, map, refundRecord, ordersTotal, v);
                    refundCarryVo = handRefundPromotion(operationId, refundRecord, ordersTotal,refundRecordItem,refundCarryVo,commonService);
                    break;
                case OrderItemConstants.TYPE_CARD:
                    handRefundCard(commonService, operationId, sqlSentence, map, refundRecord, ordersTotal, v);
                    refundCarryVo = handRefundCard(refundRecord,refundRecordItem,refundCarryVo,commonService);
                    break;
                case OrderItemConstants.CARD_BAG:
                    handCardBag(commonService, operationId, operationNme, sqlSentence, map, refundRecord,ordersTotal,v,customParameter);
                    refundCarryVo = handRefundPromotion(operationId, refundRecord, ordersTotal,refundRecordItem,refundCarryVo,commonService);
                    //refundCarryVo = handCardBag(commonService, operationId, operationNme, sqlSentence, map, refundRecord,ordersTotal,refundRecordItem,customParameter);
                    break;
                default:break;
            }
            //遍历叠加支付方式记录的退款金额
            for(RefundRecordConsumePay re:refundCarryVo.getRefundConsumePayList()){
                refundRecordConsumePay = refundRecordConsumePayMap.computeIfAbsent(re.getConsumePayId(),k->new RefundRecordConsumePay(BigDecimal.ZERO,re.getNumberNo(),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);
        return refundCarryVo;
    }
    /**
@@ -687,8 +705,8 @@
     * @param refundRecordItem 退款子单
     * @param commonService 映射
     */
    private static List<RefundRecordConsumePay> handRefundRerail(RefundRecord refundRecord, RefundRecordItem refundRecordItem
            ,List<RefundRecordMethod> refundRecordMethodList,CommonService commonService) {
    private static RefundCarryVo handRefundRerail(RefundRecord refundRecord, RefundRecordItem refundRecordItem
            , RefundCarryVo refundCarryVo, CommonService commonService) {
        //判断操作完了去修改子订单状态
        OrderItem orderItem=commonService.selectOneByKey(OrderItemMapper.class,refundRecordItem.getOrderItemId());
@@ -700,28 +718,20 @@
        if(refundRecordItem.getRefundNum() > surplusNum){
            throw new TipsException("退款数量不能大于可退款数量!");
        }
        SqlSentence sqlSentence = new SqlSentence();
        Map<String, Object> map = new HashMap<>();
        //变更退款子项信息
        Integer refundStatus;
        if (surplusNum.equals(refundRecordItem.getRefundNum())){
            map.put("refundStatus", OrderTotalConstants.STATUS_REFUND_FINSH);
            refundStatus = OrderTotalConstants.STATUS_REFUND_FINSH;
        }else{
            map.put("refundStatus", OrderTotalConstants.STATUS_REFUND_PART);
        }
        map.put("isDel", BaseEntity.NO);
        map.put("orderItemId", refundRecordItem.getOrderItemId());
        map.put("refundTotal",refundRecordItem.getRefundMoney());
        map.put("refundNum",refundRecordItem.getRefundNum());
        map.put("oldHasReNum",orderItem.getHasReNum());
        sqlSentence.setSqlSentence(" refundStatus = #{m.refundStatus}, reTotal = reTotal+#{m.refundTotal}, hasReNum=hasReNum+#{m.refundNum}" +
                " WHERE isDel = #{m.isDel} AND id = #{m.orderItemId} AND hasReNum = #{m.oldHasReNum} ");
        if(commonService.updateWhere(OrderItemMapper.class,sqlSentence) != 1){
            throw new TipsException("退款失败,订单已发生变化,请重试!");
            refundStatus = OrderTotalConstants.STATUS_REFUND_PART;
        }
        //更新子订单信息
        updateOrderItemOne(orderItem,refundStatus,refundRecordItem.getRefundMoney(),refundRecordItem.getRefundNum(),commonService);
        //退款方式处理
        return refundRecordMotnedItemHandle(refundRecord,refundRecordItem,refundRecordMethodList,commonService);
        return refundRecordMotnedItemHandle(null,refundRecord,refundRecordItem,refundCarryVo,commonService);
    }
    /**
@@ -729,7 +739,8 @@
     * @param refundRecord
     * @param refundRecordItem
     */
    private static void handRefundNoExecution(RefundRecord refundRecord, RefundRecordItem refundRecordItem,CommonService commonService) {
    private static RefundCarryVo handRefundNoExecution(RefundRecord refundRecord, RefundRecordItem refundRecordItem
            ,RefundCarryVo refundCarryVo,CommonService commonService) {
        SqlSentence sqlSentence = new SqlSentence();
        Map<String, Object> map = new HashMap<>();
@@ -752,7 +763,7 @@
        }
        //用户项目操作
        UserProjectTool.userProjectDeduction(userProjectItem,UserProjectUsedCon.USED_METHOD_ORDER_REFUND,UserProjectUsedCon.USED_TYPE_DEDUCTION,null
        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);
        //计算子单是否还有剩余的可扣疗程数
@@ -768,30 +779,20 @@
            refundStatus=OrderTotalConstants.STATUS_REFUND_PART;
        }
        map.clear();
        map.put("orderItemId", orderItem.getId());
        map.put("refundStatus", refundStatus);
        map.put("refundTotal",refundRecordItem.getRefundMoney());
        map.put("refundNum",refundRecordItem.getRefundNum());
        map.put("oldHasReNum",orderItem.getHasReNum());
        sqlSentence.sqlUpdate(" refundStatus=#{m.refundStatus},reTotal=reTotal+#{m.refundTotal},hasReNum=hasReNum+#{m.refundNum}" +
                " where isDel=0 AND id=#{m.orderItemId} AND hasReNum = #{m.oldHasReNum}",map);
        if(commonService.updateWhere(OrderItemMapper.class,sqlSentence) != 1){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"当前订单信息已发生变化,请重试[012]!");
        }
        //更新子订单信息
        updateOrderItemOne(orderItem,refundStatus,refundRecordItem.getRefundMoney(),refundRecordItem.getRefundNum(),commonService);
        //退款方式处理
        refundRecordMotnedItemHandle(refundRecord,refundRecordItem,commonService);
        return refundRecordMotnedItemHandle(userProjectDeductionVo.getDeductionTotal(),refundRecord,refundRecordItem,refundCarryVo,commonService);
    }
    /**
     * 退款-处理卡项
     * @param commonService
     * @param refundRecord
     * @param ordersTotal
     * @param refundRecordItem
     */
    public static void handRefundCard(RefundRecord refundRecord, OrdersTotal ordersTotal, RefundRecordItem refundRecordItem,CommonService commonService) {
    public static RefundCarryVo handRefundCard(RefundRecord refundRecord, RefundRecordItem refundRecordItem,RefundCarryVo refundCarryVo,CommonService commonService) {
        SqlSentence sqlSentence = new SqlSentence();
        Map<String, Object> map = new HashMap<>();
@@ -799,7 +800,7 @@
        //判断操作完了去修改子订单状态
        OrderItem orderItem=commonService.selectOneByKey(OrderItemMapper.class,refundRecordItem.getOrderItemId());
        if (orderItem==null){
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"未找到子订单信息");
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到子订单信息");
        }
        Integer surplusNum = orderItem.getBuyNum() - orderItem.getHasReNum();
        if(refundRecordItem.getRefundNum() > surplusNum){
@@ -812,33 +813,38 @@
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款作废卡包提示:没有找到对应的卡包可退[021]");
        }
        //变更卡项状态
        UserCard userCard;
        RefundRecordCard refundRecordCard;
        for (int i = 0; i < refundRecordItem.getRefundNum(); i++) {
            userCard= userCardList.get(i);
            map.put("id",userCard.getId());
            map.put("effectiveStatus", UserProjectConstants.EFF_STATUS_CANCEL);
            sqlSentence.setSqlSentence(" effectiveStatus=#{m.effectiveStatus} where id=#{m.id} ");
            sqlSentence.setSqlSentence(" effectiveStatus=#{m.effectiveStatus} where id = #{m.id} ");
            commonService.updateWhere(UserCardMapper.class,sqlSentence);
            //生成退款关联
            refundRecordCard = new RefundRecordCard();
            refundRecordCard.setUserCardId(userCard.getId());
            refundRecordCard.setRefundRecordId(refundRecord.getId());
            refundRecordCard.setRefundRecordItemId(refundRecordItem.getId());
            退款卡包关联保存
        }
        Integer refundStatus;
        if (surplusNum.equals(refundRecordItem.getRefundNum())){
            map.put("refundStatus", OrderTotalConstants.STATUS_REFUND_FINSH);
            refundStatus =  OrderTotalConstants.STATUS_REFUND_FINSH;
        }else {
            map.put("refundStatus", OrderTotalConstants.STATUS_REFUND_PART);
            refundStatus =  OrderTotalConstants.STATUS_REFUND_PART;
        }
        //更改订单子订单状态
        map.put("orderItemId", refundRecordItem.getOrderItemId());
        map.put("refundTotal",refundRecordItem.getRefundMoney());
        map.put("refundNum",refundRecordItem.getRefundNum());
        map.put("oldHasReNum",refundRecordItem.getRefundNum());
        sqlSentence.setSqlSentence(" refundStatus=#{m.refundStatus}, reTotal=reTotal+#{m.refundTotal}, hasReNum=hasReNum+#{m.refundNum}" +
                " where isDel=#{m.isDel} AND id=#{m.orderItemId} AND hasReNum = #{m.oldHasReNum}");
        if(commonService.updateWhere(OrderItemMapper.class,sqlSentence) != 1){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"当前订单信息已发生变化,请重试[012]!");
        }
        //更新子订单信息
        updateOrderItemOne(orderItem,refundStatus,refundRecordItem.getRefundMoney(),refundRecordItem.getRefundNum(),commonService);
        //退款方式处理
        refundRecordMotnedItemHandle(refundRecord,refundRecordItem,commonService);
        refundCarryVo = refundRecordMotnedItemHandle(null,refundRecord,refundRecordItem,refundCarryVo,commonService);
        return refundCarryVo;
    }
    /**
@@ -849,8 +855,8 @@
     * @param ordersTotal
     * @param refundRecordItem
     */
    public static void handCardBag(String operationId,RefundRecord refundRecord
            , OrdersTotal ordersTotal,RefundRecordItem refundRecordItem,CommonService commonService) {
    public static void handCardBag(String operationId,RefundRecord refundRecord, OrdersTotal ordersTotal
            ,RefundRecordItem refundRecordItem,RefundCarryVo refundCarryVo,CommonService commonService) {
        SqlSentence sqlSentence = new SqlSentence();
        Map<String, Object> map = new HashMap<>();
@@ -868,14 +874,16 @@
        }
        for (RefundRecordItemSource son : sons) {
            refundCarryVo.setRefundConsumePayList(new ArrayList<>());
            refundCarryVo.setDeductionTotal(BigDecimal.ZERO);
            refundCarryVo.setCashTotal(BigDecimal.ZERO);
            switch (GroupTypeEnum.getCode(son.getType())){
                case PROJECT:
                    handRefundNoSonExecution(commonService, sqlSentence, map, refundRecord, son);
                    deleteUserCardUsed(sqlSentence,map,ordersTotal,commonService,son,orderItem);
                    //handRefundNoSonExecution(refundRecord,son,refundCarryVo,commonService);
                    break;
                case RETAIL:
                    handRefundSonRerail(commonService, sqlSentence, map, refundRecord, son);
                    deleteUserCardUsed(sqlSentence,map,ordersTotal,commonService,son,orderItem);
                    //handRefundSonRerail(refundRecord,son,refundCarryVo,commonService );
                    //deleteUserCardUsed(sqlSentence,map,ordersTotal,commonService,son,orderItem);
                    break;
                case INCREMENT:
                    //增值金
@@ -931,31 +939,49 @@
     * 退款需要删除用户卡包使用记录
     * @param commonService
     */
    public static void deleteUserCardUsed(String userCardId,CommonService commonService){
    public static void deleteUserCardUsed(String cardItemInfoId,String sourceId,Integer hasReNum,CommonService commonService){
        SqlSentence sqlSentence = new SqlSentence();
        Map<String, Object> map = new HashMap<>();
        Map<String,Object> map = new HashMap<>();
        map.put("isDel",UserCardUsed.YES);
        map.put("userCardId",userCardId);
        sqlSentence.sqlUpdate("isDel = #{m.isDel} WHERE userCardId = #{m.userCardId}",map);
        commonService.updateWhere(UserCardUsedMapper.class,sqlSentence);
        //获取使用记录
        map.put("cardItemInfoId",cardItemInfoId);
        map.put("sourceId",sourceId);
        sqlSentence.setSqlSentence(" SELECT * FROM user_card_used WHERE isDel = 0" +
                " AND cardItemInfoId = #{m.cardItemInfoId} AND sourceId = #{m.sourceId}");
        List<UserCardUsed> userCardUsedList = commonService.selectList(UserCardUsedMapper.class,sqlSentence);
        if(hasReNum > userCardUsedList.size()){
            throw new TipsException("卡包可退数量错误!");
        }
        int opNum = 0;
        sqlSentence.sqlUpdate("isDel = 1 WHERE id = #{m.id} AND isDel = 0",map);
        for(UserCardUsed userCardUsed:userCardUsedList){
            map.put("id",userCardUsed.getId());
            opNum = opNum+commonService.updateWhere(UserCardUsedMapper.class,sqlSentence);
        }
        if(opNum != hasReNum){
            throw new TipsException("卡包回退错误!");
        }
    }
    /**
     * 退款-处理促销
     * @param commonService
     * @param operationId
     * @param refundRecord
     * @param ordersTotal
     * @param v
     * @param refundRecordItem
     */
    private static void handRefundPromotion(String operationId, RefundRecord refundRecord, OrdersTotal ordersTotal
            , RefundRecordItem refundRecordItem,CommonService commonService) {
    private static RefundCarryVo handRefundPromotion(String operationId, RefundRecord refundRecord, OrdersTotal ordersTotal
            , RefundRecordItem refundRecordItem,RefundCarryVo refundCarryVo,CommonService commonService) {
        SqlSentence sqlSentence = new SqlSentence();
        Map<String, Object> map = new HashMap<>();
        //查看订单信息
        OrderItem orderItem=commonService.selectOneByKey(OrderItemMapper.class,refundRecordItem.getOrderItemId());
        OrderItem orderItem = commonService.selectOneByKey(OrderItemMapper.class,refundRecordItem.getOrderItemId());
        if(orderItem == null ){
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"未找到子订单信息[84]!");
        }
@@ -965,82 +991,107 @@
        sqlSentence.setSqlSentence("select * from refund_record_item_source where isDel = 0 AND refundRecordItemId = #{m.refundRecordItemId}");
        List<RefundRecordItemSource> sons = commonService.selectList(RefundRecordItemSourceMapper.class, sqlSentence);
        if( sons!=null && sons.size()>0 ){
            for (RefundRecordItemSource son : sons) {
                switch (GroupTypeEnum.getCode(son.getType())){
                    case PROJECT:
                        v.setOrderItemId(son.getId());
                        v.setRefundNum(son.getRefundNum());
                        handRefundNoSonExecution(commonService, sqlSentence, map, refundRecord, son);
                        break;
                    case RETAIL:
                        v.setOrderItemId(son.getId());
                        v.setRefundNum(son.getRefundNum());
                        handRefundSonRerail(commonService, sqlSentence, map, refundRecord, son);
                        break;
                    case INCREMENT:
                        //增值金
                        //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
                        if(son.getRealRefundTotal().negate().compareTo(BigDecimal.ZERO)!=0){
                            UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"审核通过退款:促销赠送增值金扣减",operationId,refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),son.getRealRefundTotal().negate(), UserMoneyUnclaimed.FUND_TYPE_VALUE_ADDED_FUND, OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES);
                        }
                        break;
                    case STORED:
                        //储值金额
                        //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
                        if(son.getRealRefundTotal().negate().compareTo(BigDecimal.ZERO)!=0){
                            UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"审核通过退款:促销赠送储值金额扣减",operationId,refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),son.getRealRefundTotal().negate(), UserMoneyUnclaimed.FUND_TYPE_STORED_VALUE_FUND, OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.NO);
                        }
                        break;
                    case INTEGRAL:
                        //积分
                        //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
                        if(son.getRealRefundTotal().negate().compareTo(BigDecimal.ZERO)!=0){
                            UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"审核通过退款:促销赠送积分扣减",operationId,refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),son.getRealRefundTotal().negate(), UserMoneyUnclaimed.FUND_TYPE_INTEGRAL,OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.YES);
                        }
                        break;
                }
        //根据支付记录总表的标识来整合已退的金额,key值:支付记录总表标识,value:金额数据
        Map<String,RefundRecordConsumePay> refundRecordConsumePayMap = new HashMap<>();
        RefundRecordConsumePay refundRecordConsumePay;
        //计算本次退款方式的划扣金额
        BigDecimal deductionTotal = BigDecimal.ZERO;
        //计算本次退款方式的现金金额
        BigDecimal cashTotal = BigDecimal.ZERO;
        for (RefundRecordItemSource son : sons) {
            //初始化总结构携带参数
            refundCarryVo.setRefundConsumePayList(new ArrayList<>());
            refundCarryVo.setDeductionTotal(BigDecimal.ZERO);
            refundCarryVo.setCashTotal(BigDecimal.ZERO);
            switch (GroupTypeEnum.getCode(son.getType())){
                case PROJECT:
                    refundCarryVo = handRefundNoSonExecution(refundRecord,refundRecordItem,son,refundCarryVo,commonService);
                    break;
                case RETAIL:
                    refundCarryVo = handRefundSonRerail(refundRecord,son,refundCarryVo,commonService);
                    if(refundRecordItem.getType().equals(OrderItemConstants.CARD_BAG)){
                        //是卡包的,刪除卡包使用
                        deleteUserCardUsed(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;
            }
            deductionTotal = deductionTotal.add(refundCarryVo.getDeductionTotal());
            cashTotal = cashTotal.add(refundCarryVo.getCashTotal());
            //遍历叠加支付方式记录的退款金额
            for(RefundRecordConsumePay re:refundCarryVo.getRefundConsumePayList()){
                refundRecordConsumePay = refundRecordConsumePayMap.computeIfAbsent(re.getConsumePayId(),k->new RefundRecordConsumePay(BigDecimal.ZERO,re.getNumberNo(),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.setCashTotal(cashTotal);
        refundCarryVo.setDeductionTotal(deductionTotal);
        refundCarryVo.setRefundConsumePayList(refundRecordConsumePayList);
        //获取其子项
        map.put("orderItemId",orderItem.getId());
        sqlSentence.setSqlSentence("select * from order_item_source WHERE  orderItemId=#{m.orderItemId} and isDel=0");
        sqlSentence.setSqlSentence("select refundStatus from order_item_source WHERE orderItemId=#{m.orderItemId} and isDel=0");
        List<OrderItemSon> orderItemSonList=commonService.selectList(OrderItemSonMapper.class,sqlSentence);
        Integer refundStatus;
        Integer refundNum = 0;
        List<Integer> collect = orderItemSonList.stream().map(o -> o.getRefundStatus()).collect(Collectors.toList());
        if(collect.contains(OrderTotalConstants.STATUS_REFUND_PART)){
            orderItem.setRefundStatus(OrderTotalConstants.STATUS_REFUND_PART);
            orderItem.setReTotal(orderItemSonList.stream().map(OrderItemSon::getReTotal).reduce(BigDecimal.ZERO,BigDecimal::add));
        }else if (collect.contains(OrderTotalConstants.STATUS_REFUND_NONE) && collect.contains(OrderTotalConstants.STATUS_REFUND_FINSH)){
            orderItem.setRefundStatus(OrderTotalConstants.STATUS_REFUND_PART);
            orderItem.setReTotal(orderItemSonList.stream().map(OrderItemSon::getReTotal).reduce(BigDecimal.ZERO,BigDecimal::add));
            refundStatus = OrderTotalConstants.STATUS_REFUND_PART;
        }else if (collect.contains(OrderTotalConstants.STATUS_REFUND_NONE)){
            orderItem.setRefundStatus(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)){
            orderItem.setRefundStatus(OrderTotalConstants.STATUS_REFUND_FINSH);
            orderItem.setReTotal(orderItemSonList.stream().map(OrderItemSon::getReTotal).reduce(BigDecimal.ZERO,BigDecimal::add));
            orderItem.setHasReNum(orderItem.getBuyNum());
            refundStatus = OrderTotalConstants.STATUS_REFUND_FINSH;
            refundNum = orderItem.getBuyNum();
        }else {
            orderItem.setRefundStatus(OrderTotalConstants.STATUS_REFUND_NONE);
            refundStatus = OrderTotalConstants.STATUS_REFUND_NONE;
        }
        commonService.updateAll(OrderItemMapper.class,orderItem);
        //更新子订单信息
        updateOrderItemOne(orderItem,refundStatus,refundRecordItem.getRefundMoney(),refundNum,commonService);
        return refundCarryVo;
    }
    /**
     * 退款-处理二级子订单未执行划扣   项目类型
     * @param commonService
     * @param sqlSentence
     * @param map
    /**退款-处理二级子订单未执行划扣   项目类型
     * @param refundRecord
     * @param v
     * @param refundRecordItemSource
     * @param refundCarryVo
     * @param commonService
     */
    private static void handRefundNoSonExecution(RefundRecord refundRecord, RefundRecordItemSource refundRecordItemSource,CommonService commonService) {
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> map = new HashMap<>();
    private static RefundCarryVo handRefundNoSonExecution(RefundRecord refundRecord,RefundRecordItem refundRecordItem, RefundRecordItemSource refundRecordItemSource
            ,RefundCarryVo refundCarryVo,CommonService commonService) {
        //判断操作完了去修改子订单状态
        OrderItemSon orderItemSon=commonService.selectOneByKey(OrderItemSonMapper.class,refundRecordItemSource.getOrderItemSonId());
@@ -1062,11 +1113,9 @@
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款次数大于可退次数[84]!");
        }
        //处理用户项目
        //用户项目操作
        UserProjectTool.userProjectDeduction(userProjectItem,UserProjectUsedCon.USED_METHOD_ORDER_REFUND,UserProjectUsedCon.USED_TYPE_DEDUCTION,null
        //处理用户项目,减去用户项目数量
        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);
        Integer refundStatus;
        if(surplusNum == refundRecordItemSource.getRefundNum()){
@@ -1075,50 +1124,139 @@
            refundStatus=OrderTotalConstants.STATUS_REFUND_PART;
        }
        map.clear();
        map.put("id",refundRecordItemSource.getOrderItemSonId());
        map.put("refundTotal",refundRecordItemSource.getRefundMoney());
        map.put("refundNum",refundRecordItemSource.getRefundNum());
        map.put("oldHasReNum",orderItemSon.getHasReNum());
        map.put("refundStatus", refundStatus);
        sqlSentence.sqlUpdate(" refundStatus=#{m.refundStatus}, reTotal=reTotal+#{m.refundTotal}, hasReNum=hasReNum+#{m.refundNum}" +
                " where isDel=0 AND id=#{m.id} AND hasReNum = #{m.oldHasReNum}",map);
        if(commonService.updateWhere(OrderItemSonMapper.class,sqlSentence) != 1){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"当前订单信息已发生变化,请重试[013]!");
        //更新子单信息
        updateOrderItemTwo(orderItemSon,refundStatus,refundRecordItemSource.getRefundMoney(),refundRecordItemSource.getRefundNum(),commonService);
        //处理退款支付方式
        refundCarryVo = refundRecordMotnedItemTwoHandle(userProjectDeductionVo.getDeductionTotal(),refundRecord,refundRecordItemSource,refundCarryVo,commonService);
        if(refundRecordItem.getType().equals(OrderItemConstants.CARD_BAG)){
            //是卡包的,刪除卡包使用
            deleteUserCardUsed(orderItemSon.getCardItemInfoId(),orderItemSon.getId(),refundRecordItemSource.getRefundNum(),commonService);
        }
        //刪除卡包使用
        deleteUserCardUsed(orderItemSon.get,commonService);
        return refundCarryVo;
    }
    /**
     * 退款二级是商品
     * @param commonService
     * @param sqlSentence
     * @param map
    /**退款二级是商品
     * @param refundRecord
     * @param v
     * @param refundRecordItemSource
     * @param refundCarryVo
     * @param commonService
     * @return
     */
    private static void handRefundSonRerail(CommonService commonService,SqlSentence sqlSentence, Map<String, Object> map, RefundRecord refundRecord, RefundRecordItemSource v) {
    private static RefundCarryVo handRefundSonRerail(RefundRecord refundRecord, RefundRecordItemSource refundRecordItemSource
            ,RefundCarryVo refundCarryVo,CommonService commonService) {
        //判断操作完了去修改子订单状态
        OrderItemSon orderItem=commonService.selectOneByKey(OrderItemSonMapper.class,v.getOrderItemSonId());
        if (orderItem==null){
        OrderItemSon orderItemSon=commonService.selectOneByKey(OrderItemSonMapper.class,refundRecordItemSource.getOrderItemSonId());
        if (orderItemSon==null){
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"未找到二级子订单信息");
        }
        map.put("orderItemId", v.getOrderItemSonId());
        map.put("reTotal",orderItem.getReTotal().add(v.getRefundMoney()));
        Integer hasReNum=orderItem.getHasReNum()+v.getRefundNum();
        map.put("hasReNum",hasReNum);
        map.put("isDel",BaseEntity.NO);
        if (hasReNum==orderItem.getBuyNum()){
            map.put("refundStatus", OrderTotalConstants.STATUS_REFUND_FINSH);
        }else {
            map.put("refundStatus", OrderTotalConstants.STATUS_REFUND_PART);
        //计算子单是否还有剩余的可扣疗程数
        int surplusNum = orderItemSon.getBuyNum() - orderItemSon.getHasReNum();
        if(refundRecordItemSource.getRefundNum() > surplusNum){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款次数大于可退次数[84]!");
        }
        sqlSentence.setSqlSentence(" refundStatus=#{m.refundStatus}, reTotal=#{m.reTotal}, hasReNum=#{m.hasReNum} where isDel=#{m.isDel} AND id=#{m.orderItemId} ");
        commonService.updateWhere(OrderItemSonMapper.class,sqlSentence);
        Integer refundStatus;
        if(surplusNum == refundRecordItemSource.getRefundNum()){
            refundStatus=OrderTotalConstants.STATUS_REFUND_FINSH;
        }else{
            refundStatus=OrderTotalConstants.STATUS_REFUND_PART;
        }
        updateOrderItemTwo(orderItemSon,refundStatus,refundRecordItemSource.getRefundMoney(),refundRecordItemSource.getRefundNum(),commonService);
        //处理退款支付方式
        refundCarryVo = refundRecordMotnedItemTwoHandle(null,refundRecord,refundRecordItemSource,refundCarryVo,commonService);
        return refundCarryVo;
    }
    /**更新订单一级子单的信息
     * @param orderItem 订单子单
     * @param refundStatus 退款状态
     * @param refundTotal 退款金额
     * @param refundNum 退款数量
     * @param commonService 映射
     */
    public static void updateOrderItemOne(OrderItem orderItem,Integer refundStatus,BigDecimal refundTotal,Integer refundNum
            ,CommonService commonService){
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
        values.put("orderItemId", orderItem.getId());
        values.put("refundStatus", refundStatus);
        values.put("refundTotal",refundTotal);
        values.put("refundNum",refundNum);
        values.put("oldHasReNum",orderItem.getHasReNum());
        sqlSentence.sqlUpdate(" refundStatus=#{m.refundStatus},reTotal=reTotal+#{m.refundTotal},hasReNum=hasReNum+#{m.refundNum}" +
                " where isDel=0 AND id=#{m.orderItemId} AND hasReNum = #{m.oldHasReNum}",values);
        if(commonService.updateWhere(OrderItemMapper.class,sqlSentence) != 1){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"当前订单信息已发生变化,请重试[012]!");
        }
    }
    /**更新订单二级子单的信息
     * @param orderItemSon 订单子单
     * @param refundStatus 退款状态
     * @param refundTotal 退款金额
     * @param refundNum 退款数量
     * @param commonService 映射
     */
    public static void updateOrderItemTwo(OrderItemSon orderItemSon,Integer refundStatus,BigDecimal refundTotal,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("refundTotal",refundTotal);
        values.put("refundNum",refundNum);
        values.put("oldHasReNum",orderItemSon.getHasReNum());
        sqlSentence.sqlUpdate(" refundStatus=#{m.refundStatus},reTotal=reTotal+#{m.refundTotal},hasReNum=hasReNum+#{m.refundNum}" +
                " 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.clear();
        values.put("typeId",orderItemId);
        sqlSentence.sqlSentence("SELECT * FROM consume_pay_item WHERE isDel = 0 AND typeId = #{m.typeId}",values);
        return commonService.selectList(ConsumePayMapper.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.clear();
        values.put("typeId",orderItemId);
        sqlSentence.sqlSentence("SELECT * FROM consume_pay_item_son WHERE isDel = 0 AND typeId = #{m.typeId}",values);
        return commonService.selectList(ConsumePayMapper.class,sqlSentence);
    }
    /**获取已退款方式金额,根据支付方式编号求和返回