@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>