java组合模式构建递归树

1.实体类 CouponAllocationRuleReqVo.java

public class CouponAllocationRuleReqVo implements Serializable {

    private static final long serialVersionUID = 7018091454603844174L;


    private Long id;

    private Long parentId;


    private Long couponId;


    private Integer allocationType;


    private String allocationName;


    private BigDecimal allocationAmount;

    /**
     * 子节点
     */
    List<CouponAllocationRuleReqVo> childrenlist;
}

2.服务层 CouponSettlementService.java

插入方法:saveCouponAllocationRuleInfo()

更新方法:updateCouponAllocationRuleInfo()

查询方法:selectCouponAllocationRuleInfo()

/**
     * 插入
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveCouponAllocationRuleInfo(CouponAllocationRuleReqVo couponAllocationRuleReqVo,Long couponId) {
        log.info("CouponSettlementService.saveCouponAllocationRuleInfo->入参:{},{}", JSON.toJSONString(couponAllocationRuleReqVo),couponId);
        //没有数据直接返回
        if(couponAllocationRuleReqVo == null){
            return;
        }
        //没有子节点不需要遍历,直接插入根节点
        List<CouponAllocationRuleReqVo> childrenlist = couponAllocationRuleReqVo.getChildrenlist();
        if(childrenlist.isEmpty()){
            addSettlementCouponAllocationRule(couponAllocationRuleReqVo,couponId);
            return;
        }
        //递归遍历并插入所有节点
        saveSettlementCouponAllocationRule(couponAllocationRuleReqVo,couponId);
    }

    /**
     * 递归遍历并插入所有节点
     * @param couponAllocationRuleReqVo
     * @param couponId
     */
    private void saveSettlementCouponAllocationRule(CouponAllocationRuleReqVo couponAllocationRuleReqVo,Long couponId) {
        log.info("CouponSettlementService.saveSettlementCouponAllocationRule->入参:{},{}", JSON.toJSONString(couponAllocationRuleReqVo),couponId);
        this.addSettlementCouponAllocationRule(couponAllocationRuleReqVo,couponId);
        List<CouponAllocationRuleReqVo> childrenlist = couponAllocationRuleReqVo.getChildrenlist();
        if (CollectionUtils.isNotEmpty(childrenlist)) {
            childrenlist.forEach(x->{
              this.saveSettlementCouponAllocationRule(x,couponId);
            });
        }
    }

    /**
     *保存
     * @param couponAllocationRuleReqVo
     * @param couponId
     */
    private void addSettlementCouponAllocationRule(CouponAllocationRuleReqVo couponAllocationRuleReqVo,Long couponId) {
        log.info("CouponSettlementService.addSettlementCouponAllocationRule->入参:{},{}", JSON.toJSONString(couponAllocationRuleReqVo),couponId);
        SettlementCouponAllocationRule settlementCouponAllocationRule = BeanUtils.copyProperties(couponAllocationRuleReqVo, SettlementCouponAllocationRule.class);
        settlementCouponAllocationRule.setCouponId(couponId);
        couponSettlementMapper.addSettlementCouponAllocationRule(settlementCouponAllocationRule);
    }

    /**
     * 更新
     * @param couponAllocationRuleReqVo
     * @param
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateCouponAllocationRuleInfo(CouponAllocationRuleReqVo couponAllocationRuleReqVo) {
        log.info("CouponSettlementService.updateCouponAllocationRuleInfo->入参:{}", JSON.toJSONString(couponAllocationRuleReqVo));
        //没有数据直接返回
        if(couponAllocationRuleReqVo == null){
            return;
        }
        //没有子节点不需要遍历,直接更新根节点
        List<CouponAllocationRuleReqVo> childrenlist = couponAllocationRuleReqVo.getChildrenlist();
        if(childrenlist.isEmpty()){
            updateSettlementCouponAllocationRule(couponAllocationRuleReqVo);
            return;
        }
        //递归遍历并更新所有节点
        refreshSettlementCouponAllocationRule(couponAllocationRuleReqVo);
    }

    /**
     * 递归遍历并更新所有节点
     * @param couponAllocationRuleReqVo
     */
    private void refreshSettlementCouponAllocationRule(CouponAllocationRuleReqVo couponAllocationRuleReqVo) {
        log.info("CouponSettlementService.refreshSettlementCouponAllocationRule->入参:{}", JSON.toJSONString(couponAllocationRuleReqVo));
        this.updateSettlementCouponAllocationRule(couponAllocationRuleReqVo);
        List<CouponAllocationRuleReqVo> childrenlist = couponAllocationRuleReqVo.getChildrenlist();
        if (CollectionUtils.isNotEmpty(childrenlist)) {
            childrenlist.forEach(x->{
                this.refreshSettlementCouponAllocationRule(x);
            });
        }
    }

    /**
     *更新规则
     * @param couponAllocationRuleReqVo
     */
    private void updateSettlementCouponAllocationRule(CouponAllocationRuleReqVo couponAllocationRuleReqVo) {
        log.info("CouponSettlementService.updateSettlementCouponAllocationRule->入参:{}", JSON.toJSONString(couponAllocationRuleReqVo));
        SettlementCouponAllocationRule settlementCouponAllocationRule = BeanUtils.copyProperties(couponAllocationRuleReqVo, SettlementCouponAllocationRule.class);
        couponSettlementMapper.updateSettlementCouponAllocationRule(settlementCouponAllocationRule);
    }

    /**
     * 查看规则
     * @param couponId
     * @return
     */
    public CouponAllocationRuleRecordVo selectCouponAllocationRuleInfo(Long couponId) {
        log.info("CouponSettlementService.selectCouponAllocationRuleInfo->入参:{}", JSON.toJSONString(couponId));
        List<CouponAllocationRuleRecordVo> couponAllocationRuleRecordVoList = couponSettlementMapper.selectSettlementCouponAllocationRule(couponId);
        CouponAllocationRuleRecordVo rootCouponAllocationRuleRecordVo = couponAllocationRuleRecordVoList.stream().filter(e -> e.getAllocationType() == 2).collect(Collectors.toList()).get(0);
        List<CouponAllocationRuleRecordVo> untopList = couponAllocationRuleRecordVoList.stream().filter(e -> e.getAllocationType() != 2).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(untopList)) {
            Map<Long, Long> objectObjectHashMap = new HashMap<>(untopList.size());
            getChild(rootCouponAllocationRuleRecordVo,untopList,objectObjectHashMap);
        }
        return rootCouponAllocationRuleRecordVo;
    }

    /**
     * 递归构造子节点
     * @param couponAllocationRuleRecordVo
     * @param couponAllocationRuleRecordVoList
     * @param map
     */
    private void getChild(CouponAllocationRuleRecordVo couponAllocationRuleRecordVo,
                          List<CouponAllocationRuleRecordVo> couponAllocationRuleRecordVoList,
                          Map<Long,Long> map) {
        log.info("CouponSettlementService.getChild->入参:{}", JSON.toJSONString(couponAllocationRuleRecordVo));
        ArrayList<CouponAllocationRuleRecordVo> childList = new ArrayList<>();
        couponAllocationRuleRecordVoList.stream()
                .filter(e -> !map.containsKey(e.getId()))
                .filter(e -> NumberUtils.compare(e.getParentId(),couponAllocationRuleRecordVo.getId()) == 0)
                .forEach(e -> {
                    map.put(e.getId(),e.getParentId());
                    getChild(e,couponAllocationRuleRecordVoList,map);
                    childList.add(e);
                });
        couponAllocationRuleRecordVo.setChildrenlist(childList);
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值