chenjiahe
2023-04-01 510678fe2f81501914ea4905fb7026b9c7be414f
phi_platform_user/src/main/java/com/hx/phip/tool/refund/PartialRefundUtil.java
@@ -71,7 +71,7 @@
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"操作失败,退款单状态已改变!");
        }
        //回去订单信息
        //获取订单信息
        OrdersTotal ordersTotal = commonService.selectOneByKeyBlob(OrdersTotalMapper.class,refundRecord.getOrderId());
        if(ordersTotal == null ){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到订单信息!");
@@ -259,22 +259,37 @@
        List<RefundRecordMethod> refundRecordMethodList = refundCarryVo.getRefundRecordMethodList();
        //装载分配好的支付方式记录的金额数据
        List<RefundRecordConsumePay> refundConsumePayList = new ArrayList<>();
        RefundRecordConsumePay refundRecordConsumePay;
        //引用对象
        List<ConsumePay> consumePayList;
        ///////获取订单支付方式记录
        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();
            //获取支付方式
            map.put("orderId",refundRecord.getOrderId());
            map.put("numberNo",refundRecordMethod.getNumberNo());
            sqlSentence.sqlSentence("SELECT *,ROUND(actualTotal-refundTotal,2) AS pTotal FROM consume_pay WHERE isDel = 0 AND orderId = #{m.orderId} AND numberNo = #{m.numberNo} ORDER BY pTotal ASC",map);
            consumePayList = commonService.selectList(ConsumePayMapper.class,sqlSentence);
            for(ConsumePay consumePay:consumePayList){
            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;
                }
@@ -321,7 +336,7 @@
    public static RefundCarryVo refundRecordMotnedItemHandle(BigDecimal deductionTotalUser,RefundRecord refundRecord,RefundRecordItem refundRecordItem
            ,RefundCarryVo refundCarryVo,CommonService commonService){
        //没有退款方式,跳过当前处理
        //没有退款方式,跳过当前处理,因为这里是处理退款方式金额的,如果没有退款金额,那么可以跳过该环节
        if(refundCarryVo.getRefundRecordMethodList().size() == 0){
            return refundCarryVo;
        }
@@ -341,7 +356,7 @@
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
        //更新现金和划扣金额保存
        //更新退款子单的信息,更新现金和划扣金额保存
        values.put("deductionTotal",refundCarryVo.getDeductionTotal());
        values.put("deductionTotalUser",deductionTotalUser==null?BigDecimal.ZERO:deductionTotalUser);
        values.put("cashTotal",refundCarryVo.getCashTotal());
@@ -419,23 +434,25 @@
        //进行升序排序,避免后面不够分配
        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:可退金额
        ////存储支付方式编号的可退款金额,根据支付编号求和,后面的业务判断会用到,key值:支付编号,value:可退金额
        Map<String,BigDecimal> noMap = new HashMap<>();
        BigDecimal surplusTotal;
        ////存储支付方式编号的支付方式记录,根据支付编号整合,后面的判断会用到,key值:支付编号,value:支付记录集合
        ////存储支付方式编号的支付方式记录,根据支付编号整合,后面的业务会用到,key值:支付编号,value:支付记录集合
        Map<String,List<ConsumePayItem>> noPayItemMap = new HashMap<>();
        List<ConsumePayItem> noPayItemList;
        ////////填充支付方式记录的退款金额,计算剩余可退金额,支付方式记录的退款金额需要去查询计算获取
        //获取已经退款的退款方式金额,根据支付方式记录的标识求和返回
        //获取订单子单已经退款的退款方式金额,根据支付方式记录的标识求和返回
        List<RefundRecordConsumePay> refundRecordConsumePayList = getRefundRecordConsumePay(orderItemId,null,false,true,commonService);
        //转化成map,可以根据支付方式记录的标识直接获取到数据
        Map<String, RefundRecordConsumePay> refundRecordConsumePayMap = refundRecordConsumePayList.stream().collect(
@@ -454,10 +471,6 @@
            surplusTotal = surplusTotal.add(consumePayItem.getpTotal()).setScale(2,RoundingMode.HALF_UP);
            noMap.put(consumePayItem.getNumberNo(),surplusTotal);
        }
        //进行升序排序,避免后面不够分配
        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);
@@ -533,7 +546,10 @@
            noMap.put(refundRecordItemMethod.getNumberNo(),surplusTotal);
            ///////生成关联支付方式记录和退款方式关联
            //根据支付编号获取支付方式记录
            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){
@@ -704,7 +720,7 @@
            refundCarryVo.setRefundConsumePayList(new ArrayList<>());
            refundCarryVo.setDeductionTotal(BigDecimal.ZERO);
            refundCarryVo.setCashTotal(BigDecimal.ZERO);
            //商品类型判断
            switch (refundRecordItem.getType()){
                case OrderItemConstants.TYPE_RETAIL:
                    refundCarryVo = handRefundRerail(refundRecord,refundRecordItem,refundCarryVo,commonService);
@@ -874,7 +890,7 @@
            userCard= userCardList.get(i);
            map.put("id",userCard.getId());
            map.put("effectiveStatus", UserProjectConstants.EFF_STATUS_CANCEL);
            sqlSentence.sqlSentence(" effectiveStatus=#{m.effectiveStatus} where id = #{m.id}",map);
            sqlSentence.sqlSentence(" effectiveStatus=#{m.effectiveStatus} WHERE id = #{m.id}",map);
            commonService.updateWhere(UserCardMapper.class,sqlSentence);
            //生成退款关联
@@ -1018,7 +1034,7 @@
        //因为有些是算次数抵扣的,所以要算
        if(CardEquity.EQUITY_TYPE_TIME_CARD.equals(cardEquity.getEquityType())){
            //次卡:总次数=开单的总次数*每次划扣的次数
            returnNum= refundNum *cardItemInfo.getEveryDrawNum();
            returnNum= refundNum * cardItemInfo.getEveryDrawNum();
        }else if(CardEquity.EQUITY_TYPE_N_M.equals(cardEquity.getEquityType())){
            returnNum = refundNum;
        }else{
@@ -1052,7 +1068,6 @@
     */
    private static RefundCarryVo handRefundPromotion(String operationId, RefundRecord refundRecord, OrdersTotal ordersTotal
            , RefundRecordItem refundRecordItem,RefundCarryVo refundCarryVo,CommonService commonService) {
        //查看订单信息
        OrderItem orderItem = commonService.selectOneByKey(OrderItemMapper.class,refundRecordItem.getOrderItemId());
@@ -1168,14 +1183,14 @@
            ,RefundCarryVo refundCarryVo,CommonService commonService) {
        //判断操作完了去修改子订单状态
        OrderItemSon orderItemSon=commonService.selectOneByKey(OrderItemSonMapper.class,refundRecordItemSource.getOrderItemSonId());
        OrderItemSon orderItemSon = commonService.selectOneByKey(OrderItemSonMapper.class,refundRecordItemSource.getOrderItemSonId());
        if (orderItemSon==null){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到二级子订单信息");
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到二级子订单信息[02]");
        }
        UserProjectItem userProjectItem = getUserProject(orderItemSon.getId(),commonService);
        if(userProjectItem == null){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"用户项目获取失败!");
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"用户项目获取失败[06]!");
        }
        if(userProjectItem.getNotUsedNum()<refundRecordItemSource.getRefundNum()){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款次数大于可退次数[83]!");
@@ -1184,7 +1199,7 @@
        //计算子单是否还有剩余的可扣疗程数
        int surplusNum = orderItemSon.getUsedTotal() - orderItemSon.getHasReNum();
        if(refundRecordItemSource.getRefundNum() > surplusNum){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款次数大于可退次数[84]!");
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款次数大于可退次数[56]!");
        }
        //处理用户项目,减去用户项目数量
@@ -1267,13 +1282,13 @@
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
        values.put("orderItemId", orderItem.getId());
        values.put("id", 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);
                " 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]!");
        }
@@ -1296,7 +1311,7 @@
        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}" +
        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]!");