Java-递归(二)

@Override
public List<Wyz> getListByPwd() {
    // 查询所有的五元组列表
    List<Wyz> allWyzList = wyzMapper.getAllWyzList();

    if (allWyzList != null && allWyzList.size() > 0) {
        for (Wyz wyz : allWyzList) {
            // 封装分类_规则树形结构
            List<Categories> cateAndRules = getCateAndRules(wyz.getId());

            // 为五元组设置分类和规则
            wyz.setChildCategories(cateAndRules);
        }
    }

    return allWyzList;
}

// 封装分类规则树形结构
public List<Categories> getCateAndRules(Integer id) {
    // 获取除一级分类之外的所有分类列表
    List<Categories> root = categoriesMapper.getChildCateList();

    // 根据五元组id查询得到所有一级分类 ancestry = 0
    List<Categories> topCate = categoriesMapper.getCatByCatType(id);

    if (topCate != null && topCate.size() > 0) {
        for (Categories c : topCate) {
            // 为一级分类设置规则
            List<Rules> list = rulesMapper.getNotNUllRulesListByCatId(c.getId());
            if (list != null && list.size() > 0) {
                c.setChildRules(list);
            }

            // 为一级分类设置子分类,getChild递归调用
            c.setChild(getChild(c.getId(), root));
        }
    }

    return topCate;
}

public List<Categories> getChild(Integer id, List<Categories> root) {
    List<Categories> childList = new ArrayList<>();

    for (Categories categories : root) {
        if (categories.getAncestry().equals(id)) {
            // 为分类设置规则
            List<Rules> list = rulesMapper.getNotNUllRulesListByCatId(categories.getId());
            categories.setChildRules(list);

            childList.add(categories);
        }
    }

    // 递归终止的条件,没有子权限时
    if (childList.size() == 0) {
        return null;
    }

    // 如果有子分类还有子分类,把子分类的子分类再循环一遍
    for (Categories categories : childList) {
        categories.setChild(getChild(categories.getId(), root));
    }

    return childList;
}
<select id="getNotNUllRulesListByCatId" parameterType="java.lang.Integer" resultMap="BaseResultMap">
  SELECT r.id, r.company, r.product, r.producturl, r.rule, r.created_at, r.updated_at, r.user_id, r.published,
  r.encrypted_rule_iv, r.encrypted_rule, r.desc, r.from_rule_id, r.taged, r.company_taged, r.pub_fofa_show,
  r.garbage_rule, r.country_code, r.soft_hard_code, r.level_code, r.price, r.company_desc, r.en_product,
  r.en_company, r.industry_id
  FROM categories_rules cr,rules r
  <where>
    and r.id = cr.rule_id
    and r.encrypted_rule_iv IS NOT NULL
    and r.encrypted_rule IS NOT NULL
    and cr.category_id = #{id,jdbcType=INTEGER}
  </where>
  ORDER BY r.created_at DESC
</select>
<select id="getRulesListByCatId" parameterType="java.util.List" resultMap="BaseResultMap">
  SELECT rules.id, rules.company, rules.product, rules.producturl, rules.rule, rules.created_at, rules.updated_at, rules.user_id, rules.published,
  rules.encrypted_rule_iv, rules.encrypted_rule, rules.desc, rules.from_rule_id, rules.taged, rules.company_taged, rules.pub_fofa_show,
  rules.garbage_rule, rules.country_code, rules.soft_hard_code, rules.level_code, rules.price, rules.company_desc, rules.en_product,
  rules.en_company, rules.industry_id
  FROM categories,categories_rules cr,rules
  <where>
    and categories.published = 1
    and categories.id = cr.category_id
    and cr.rule_id = rules.id
    <if test="list != null and list.size() != 0">
      AND categories.id in
      <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
        #{item}
      </foreach>
    </if>
  </where>
  ORDER BY rules.created_at DESC
</select>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值