chenjiahe
2023-04-02 2a45d42cebd76d0f0add392c0e06d2cbbb36c63f
phi_platform_user/src/main/java/com/hx/phip/tool/refund/PartialRefundUtil.java
@@ -14,6 +14,7 @@
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.*;
@@ -30,6 +31,7 @@
import com.hx.util.StringUtils;
import com.platform.exception.PlatTipsException;
import com.platform.resultTool.PlatformCode;
import org.springframework.beans.BeanUtils;
import java.math.BigDecimal;
import java.math.RoundingMode;
@@ -41,8 +43,17 @@
 */
public class PartialRefundUtil {
    /**有子项退款的类型*/
    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);
    }
    /**
     * 退款总流程工具
@@ -64,9 +75,10 @@
        //退款总订单状态变更
        values.put("refundStatus", RefundStatus.STATUS_SUCC_REFUND);
        values.put("oldRefundStatus", RefundStatus.STATUS_APPLY_REFUND);
        values.put("refundTotal", refundRecord.getRefundTotal());
        values.put("isDel", BaseEntity.NO);
        values.put("id",refundRecord.getId());
        sqlSentence.sqlSentence(" refundStatus=#{m.refundStatus} WHERE id = #{m.refundId} AND isDel=#{m.isDel} AND refundStatus = #{m.oldRefundStatus}",values);
        sqlSentence.sqlSentence(" refundTotal = #{m.refundTotal},refundStatus=#{m.refundStatus} 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,"操作失败,退款单状态已改变!");
        }
@@ -161,8 +173,9 @@
        //获取总退款方式
        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){
@@ -174,6 +187,7 @@
        RefundRecordConsumePay refundRecordConsumePay1;
        for(RefundRecordMethod refundRecordMethod:refundRecordMethodList){
            //判断是否已经被分配完
            if(refundRecordMethod.getpTotal().compareTo(BigDecimal.ZERO) > 0){
                throw new TipsException("退款错误[20]!");
            }
@@ -193,7 +207,7 @@
                //生成关联记录
                refundRecordConsumePay1 = insertRefundRecordConsumePay(refundRecordConsumePay.getRefundTotal(),null,null,refundRecord.getOrderId(),refundRecordConsumePay.getNumberNo()
                        ,refundRecordConsumePay.getName(),refundRecordConsumePay.getConsumePayId(),refundRecordMethod.getId(),null,refundRecord.getId(),commonService);
                        ,refundRecordConsumePay.getName(),refundRecordConsumePay.getIsMoneyPay(),refundRecordConsumePay.getIsExecute(),refundRecordConsumePay.getConsumePayId(),refundRecordMethod.getId(),null,refundRecord.getId(),commonService);
                refundRecordConsumePayList.add(refundRecordConsumePay1);
            }
            //修改实际退款金额
@@ -353,20 +367,34 @@
        refundCarryVo = insertRefundItemMothed(refundRecordItem.getId(),refundRecordItem.getRefundMoney(),payMothedPercentage,OrderSourceConstans.TYPE_PROJECT
                ,refundRecordItem.getOrderItemId(),refundCarryVo,refundRecord,commonService);
        //更新退款子单
        updateRefundItem(refundCarryVo.getDeductionTotal(),refundCarryVo.getCashTotal(),deductionTotalUser
                ,refundRecordItem.getId(),commonService);
        return refundCarryVo;
    }
    /**更新退款一级子单的信息
     * @param deductionTotal 退款方式的划扣金额
     * @param cashTotal 退款方式的现金金额
     * @param deductionTotalUser 用户项目的划扣金额
     * @param refundItemId 退款记录一级子单标识
     * @param commonService 映射
     */
    public static void updateRefundItem(BigDecimal deductionTotal,BigDecimal cashTotal,BigDecimal deductionTotalUser
            ,String refundItemId,CommonService commonService){
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
        //更新退款子单的信息,更新现金和划扣金额保存
        values.put("deductionTotal",refundCarryVo.getDeductionTotal());
        values.put("deductionTotal",deductionTotal);
        values.put("deductionTotalUser",deductionTotalUser==null?BigDecimal.ZERO:deductionTotalUser);
        values.put("cashTotal",refundCarryVo.getCashTotal());
        values.put("id",refundRecordItem.getId());
        values.put("cashTotal",cashTotal);
        values.put("id",refundItemId);
        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;
    }
    /**处理二级子退款方式数据
@@ -530,6 +558,7 @@
                refundRecordItemMethod.setActualTotal(surplusTotal);
            }
            refundRecordItemMethod.setRealRefundTotal(refundRecordItemMethod.getActualTotal());
            refundRecordItemMethod.setCommonType(orderItemType);
            refundRecordItemMethod.setCommonId(orderItemId);
            refundRecordItemMethod.setOrderId(refundRecordMethod.getOrderId());
@@ -564,16 +593,16 @@
                //划扣金额
                if(consumePayItem.getIsMoneyPay().equals(ConsumePayItem.YES)){
                    deductionTotal = deductionTotal.add(mothedCutTotal).setScale(2,RoundingMode.HALF_UP);
                    cashTotal = cashTotal.add(mothedCutTotal).setScale(2,RoundingMode.HALF_UP);
                }
                //现金金额
                if(consumePayItem.getIsExecute().equals(ConsumePayItem.YES)){
                    cashTotal = cashTotal.add(mothedCutTotal).setScale(2,RoundingMode.HALF_UP);
                    deductionTotal = deductionTotal.add(mothedCutTotal).setScale(2,RoundingMode.HALF_UP);
                }
                //生成关联记录
                refundRecordConsumePay = insertRefundRecordConsumePay(mothedCutTotal,refundRecordItemMethod.getCommonType(),refundRecordItemMethod.getCommonId(),refundRecord.getOrderId(),consumePayItem.getNumberNo()
                        ,consumePayItem.getName(),consumePayItem.getConsumePayId(),refundRecordItemMethod.getId(),refundItemId,refundRecord.getId(),commonService);
                        ,consumePayItem.getName(),consumePayItem.getIsMoneyPay(),consumePayItem.getIsExecute(),consumePayItem.getConsumePayId(),refundRecordItemMethod.getId(),refundItemId,refundRecord.getId(),commonService);
                refundConsumePayList.add(refundRecordConsumePay);
                //支付方式记录减掉已经分配退款方式金额
@@ -720,6 +749,7 @@
            refundCarryVo.setRefundConsumePayList(new ArrayList<>());
            refundCarryVo.setDeductionTotal(BigDecimal.ZERO);
            refundCarryVo.setCashTotal(BigDecimal.ZERO);
            refundCarryVo.setDeductionTotalUser(BigDecimal.ZERO);
            //商品类型判断
            switch (refundRecordItem.getType()){
                case OrderItemConstants.TYPE_RETAIL:
@@ -745,7 +775,7 @@
            //遍历叠加支付方式记录的退款金额
            for(RefundRecordConsumePay re:refundCarryVo.getRefundConsumePayList()){
                refundRecordConsumePay = refundRecordConsumePayMap.computeIfAbsent(re.getConsumePayId(),k->new RefundRecordConsumePay(BigDecimal.ZERO,re.getNumberNo(),re.getConsumePayId()));
                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()));
            }
        }
@@ -834,6 +864,8 @@
        //用户项目操作
        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();
@@ -1023,6 +1055,8 @@
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到该卡包的组合项");
        }
        System.out.println("cardItemInfo.getCardEquityId():"+cardItemInfo.getCardEquityId());
        //获取权益类型
        CardEquity cardEquity = commonService.selectOneByKey(CardEquityMapper.class,cardItemInfo.getCardEquityId());
        if(cardEquity == null){
@@ -1090,12 +1124,15 @@
        BigDecimal deductionTotal = BigDecimal.ZERO;
        //计算本次退款方式的现金金额
        BigDecimal cashTotal = BigDecimal.ZERO;
        //分配的用户项目划扣金额
        BigDecimal deductionTotalUser = BigDecimal.ZERO;
        for (RefundRecordItemSource son : sons) {
            //初始化总结构携带参数
            refundCarryVo.setRefundConsumePayList(new ArrayList<>());
            refundCarryVo.setDeductionTotal(BigDecimal.ZERO);
            refundCarryVo.setCashTotal(BigDecimal.ZERO);
            refundCarryVo.setDeductionTotalUser(BigDecimal.ZERO);
            switch (GroupTypeEnum.getCode(son.getType())){
                case PROJECT:
                    refundCarryVo = handRefundNoSonExecution(refundRecord,refundRecordItem,son,refundCarryVo,commonService);
@@ -1127,9 +1164,10 @@
            }
            deductionTotal = deductionTotal.add(refundCarryVo.getDeductionTotal());
            cashTotal = cashTotal.add(refundCarryVo.getCashTotal());
            deductionTotalUser = deductionTotalUser.add(refundCarryVo.getDeductionTotalUser());
            //遍历叠加支付方式记录的退款金额
            for(RefundRecordConsumePay re:refundCarryVo.getRefundConsumePayList()){
                refundRecordConsumePay = refundRecordConsumePayMap.computeIfAbsent(re.getConsumePayId(),k->new RefundRecordConsumePay(BigDecimal.ZERO,re.getNumberNo(),re.getConsumePayId()));
                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()));
            }
        }
@@ -1141,7 +1179,12 @@
        }
        refundCarryVo.setCashTotal(cashTotal);
        refundCarryVo.setDeductionTotal(deductionTotal);
        refundCarryVo.setDeductionTotalUser(deductionTotalUser);
        refundCarryVo.setRefundConsumePayList(refundRecordConsumePayList);
        //更新退款子单
        updateRefundItem(refundCarryVo.getDeductionTotal(),refundCarryVo.getCashTotal(),refundCarryVo.getDeductionTotalUser()
                ,refundRecordItem.getId(),commonService);
        //获取其子项
        map.put("orderItemId",orderItem.getId());
@@ -1205,6 +1248,8 @@
        //处理用户项目,减去用户项目数量
        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()){
@@ -1331,7 +1376,7 @@
        //获取子单的支付方式,一级子单支付记录,计算可退款金额
        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);
        return commonService.selectList(ConsumePayItemMapper.class,sqlSentence);
    }
    /**获取订单二级子单的支付方式记录
@@ -1347,12 +1392,20 @@
        //获取子单的支付方式,一级子单支付记录,计算可退款金额
        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);
        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;
    }
    /**获取已退款方式金额,根据支付方式编号求和返回
     * 注意:groupByONumberNo和groupByConsumePayId不能同时使用
     * @param itemId 子单标识
     * @param itemId 订单子单标识
     * @param refundRecordItemId 退款子单标识
     * @param groupByONumberNo 根据支付方式编号求和
     * @param groupByConsumePayId 根据支付方式记录标识求和
@@ -1367,13 +1420,13 @@
        sql.append("SELECT");
        if(groupByONumberNo){
            sql.append(" numberNo,");
            sql.append(" a.numberNo,");
        }
        if(groupByConsumePayId){
            sql.append(" consumePayId,");
            sql.append(" a.consumePayId,");
        }
        sql.append("SUM(refundTotal) AS refundTotal FROM refund_record_consume_pay a");
        sql.append(" JOIN refund_record rr ON rr.isDel = 0 AND rr.status = #{status}");
        sql.append("SUM(a.refundTotal) AS refundTotal FROM refund_record_consume_pay a");
        sql.append(" JOIN refund_record rr ON rr.id = a.refundRecordId AND rr.isDel = 0 AND rr.refundStatus = #{m.refundStatus}");
        sql.append("  WHERE a.isDel = 0");
        if(StringUtils.noNull(itemId)){
            sql.append("  AND a.commonId = #{m.commonId}");
@@ -1389,7 +1442,7 @@
        }
        //获取已退款的支付方式
        values.put("commonId",itemId);
        values.put("status", RefundStatus.STATUS_SUCC_REFUND);
        values.put("refundStatus", RefundStatus.STATUS_SUCC_REFUND);
        sqlSentence.sqlSentence(sql.toString(),values);
        return commonService.selectList(RefundRecordConsumePayMapper.class,sqlSentence);
    }
@@ -1409,7 +1462,7 @@
     * @return 返回关联记录
     */
    public static RefundRecordConsumePay insertRefundRecordConsumePay(BigDecimal refundTotal, String commonType, String commonId, String orderId
            , String numberNo,String name, String consumePayId, String refundMethodId,String refundRecordItemId,String refundRecordId,CommonService commonService){
            , 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);
@@ -1418,6 +1471,8 @@
        refundRecordConsumePay.setOrderId(orderId);
        refundRecordConsumePay.setName(name);
        refundRecordConsumePay.setNumberNo(numberNo);
        refundRecordConsumePay.setIsMoneyPay(isMoneyPay);
        refundRecordConsumePay.setIsExecute(isExecute);
        refundRecordConsumePay.setConsumePayId(consumePayId);
        refundRecordConsumePay.setRefundMethodId(refundMethodId);
        refundRecordConsumePay.setRefundRecordItemId(refundRecordItemId);