美文网首页
优先级退金额 小算法

优先级退金额 小算法

作者: 川流不息attitude | 来源:发表于2023-09-13 15:06 被阅读0次

优先级退金额 小算法

背景 :用户需要 退钱 按照对应的规则优先级 退,例如 用户最大要退 50 ,这个时候 让优先级 现金 福利卡 礼包 这几个优先退 其他次之。例如 用户 混合支付 用了 20 现金 20 福利卡 20 礼包,这个时候要退 50,应该是 依次 退 现金 20 福利卡 20 礼包 10

思路
  1. 首先获取 用户每种 类型的最大 退款数据。

  2. 让优先级 获取最大 退款数据,获取到 存在新的 List 里面 (这样就保证顺序) ,在把 其他的数据 不相关的数据 排除 优先级的 加入。

  3. 遍历 新的List 依次 相减,并且记录到 对应的相减的结果,并且判断 是不是已经足够了,把结果加入到新的List 。

showcode
/**
 * 运费优先级计算
 * @param workOrderMaximumRefundTO
 */
private void setUserFreightCostInfoList(WorkOrderMaximumRefundTO workOrderMaximumRefundTO) {
    BigDecimal maxUserFreight = workOrderMaximumRefundTO.getMaxUserFreight();
    List<CostInfoTO> maxCostInfoTOList = workOrderMaximumRefundTO.getMaxCostInfoTOList();
    // 默认优先退现金、其次礼品余额、最后福利卡券
    // CASH GIFT_BALANCE WELFARE_CARD
    Map<String, CostInfoTO> costInfoTOMap = maxCostInfoTOList
            .stream()
            .collect(Collectors.toMap(e -> e.getType(), t -> t));

    CostInfoTO cashCostInfoTO = costInfoTOMap.get(CostTypeEnum.CASH.getCode());
    CostInfoTO giftCostInfoTO = costInfoTOMap.get(CostTypeEnum.GIFT_BALANCE.getCode());
    CostInfoTO cardCostInfoTO = costInfoTOMap.get(CostTypeEnum.WELFARE_CARD.getCode());
    List<String> exists = Lists.newArrayList(CostTypeEnum.CASH.getCode(),CostTypeEnum.GIFT_BALANCE.getCode(),CostTypeEnum.WELFARE_CARD.getCode());

    List<CostInfoTO> costInfoTOList = Lists.newArrayList();
    if(null != cashCostInfoTO){
        costInfoTOList.add(cashCostInfoTO);
    }
    if(null != giftCostInfoTO){
        costInfoTOList.add(giftCostInfoTO);
    }
    if(null != cardCostInfoTO){
        costInfoTOList.add(cardCostInfoTO);
    }
    costInfoTOMap.forEach((k,v)->{
        if(!exists.contains(k)){
            costInfoTOList.add(v);
        }
    });

    if(CollectionUtils.isEmpty(costInfoTOList)) {
        List<CostInfoTO> costInfoTOS = maxCostInfoTOList.stream().map(e -> {
            CostInfoTO costInfo = new CostInfoTO();
            costInfo.setName(e.getName());
            String type = e.getType();
            costInfo.setType(type);
            costInfo.setUseNum(BigDecimal.ZERO);
            return costInfo;
        }).collect(Collectors.toList());
        workOrderMaximumRefundTO.setUserFreightCostInfoList(costInfoTOS);
        return;
    }
    boolean flag = false;
    List<CostInfoTO> costInfoTOS = new ArrayList<>();

    for (int i = 0; i < costInfoTOList.size(); i++) {
        CostInfoTO newCostInfoTO = new CostInfoTO();
        CostInfoTO costInfoTO = costInfoTOList.get(i);

        newCostInfoTO.setName(costInfoTO.getName());
        newCostInfoTO.setType(costInfoTO.getType());
        BigDecimal useNum = costInfoTO.getUseNum();
        //是否已经有足够
        if(flag){
            newCostInfoTO.setUseNum(BigDecimal.ZERO);
            costInfoTOS.add(newCostInfoTO);
            continue;
        }
        // 如果现金足够
        if(useNum.compareTo(maxUserFreight) >= CommonConstants.ZERO){
            flag = true;
            newCostInfoTO.setUseNum(maxUserFreight);
        }else {
            // 减去现金 剩余多少
            newCostInfoTO.setUseNum(useNum);
            maxUserFreight = NumberUtil.sub(maxUserFreight, useNum);
        }
        costInfoTOS.add(newCostInfoTO);
    }
    workOrderMaximumRefundTO.setUserFreightCostInfoList(costInfoTOS);

}

测试结果

image.png

用户运费信息[CostInfoTO(name=现金, type=CASH, useNum=12), CostInfoTO(name=福利卡, type=WELFARE_CARD, useNum=10), CostInfoTO(name=福利礼包, type=WELFARE_PACKAGE, useNum=10), CostInfoTO(name=能量, type=POWER, useNum=8)]

换成 15 算

image.png

用户运费信息[CostInfoTO(name=现金, type=CASH, useNum=12), CostInfoTO(name=福利卡, type=WELFARE_CARD, useNum=3), CostInfoTO(name=福利礼包, type=WELFARE_PACKAGE, useNum=0), CostInfoTO(name=能量, type=POWER, useNum=0)]

总结:有一点难度。

相关文章

  • mysql 联接查询算法之实践篇(六)和优化join总结

    mysql选择使用连接算法的优先级在选择Join算法时,会有优先级,理论上会优先判断能否使用INLJ、BNLJ:内...

  • 如何在javascript中使用优先级队列

    摘要:学习优先级队列很重要,因为它被用于许多算法中,例如 Dijkstra 的最短路径算法使用优先级队列。 介绍先...

  • CSS常见问题

    CSS优先级算法如何计算? 样式优先级规则: 优先级顺序为:!important>style>权重值权重规则:第一...

  • 计算任意字符串算式的结果

    该算法不包含括号运算!主要思路是利用递归算法1、先把减法变成加法,把除法变成乘法2、截取优先级低的算式,将优先级高...

  • 最早截止时间优先算法

    EDF算法根据任务的开始截至时间来确定任务的优先级,即任务的开始截至时间越早,其优先级越高。在实现该算法时,要求系...

  • 感恩日志|学会赞美也是一种美

    记录下每天的小确幸:前几天在朋友的贝店买的火龙果,有个坏了,跟朋友说,可以退相应的金额。朋友帮我申请了退10元,虽...

  • PriorityBlockingQueue

    JDK 中无界优先级队列PriorityBlockingQueue 内部使用堆算法保证每次出队都是优先级最高的元素...

  • [机制]优先级-次级支付

    一、机制描述 本专项计划的收益及本金的偿付采用优先级/中间级/次级支付机制,其中,优先级资产支持证券金额为{ N}...

  • 常见调度算法

    先来先服务(FCFS)调度算法短作业优先(SJF)调度算法优先级调度算法高响应比优先调度算法时间片轮转调度算法多级...

  • 遗传算法求解混合流水车间调度问题(HFSP)二:算法实现一

    遗传算法的设计 编码:对工件进行优先级编码,编码越小,优先级越高。 解码:按照工件优先级进行生产,求出整体完工时间...

网友评论

      本文标题:优先级退金额 小算法

      本文链接:https://www.haomeiwen.com/subject/cvdpvdtx.html