/**
* 获取所有分类
*
* @return
*/
public List<classify> queryCatList(BackstageUser backstageUser) {
// 用boot获取分类的数据 selectAll在springboot中是获取数据表里的所有数据
List<classify> data=null;
String shopIds="";
if(backstageUser.getRoleId()==3){
data = classifyMapper.query();
}else if(backstageUser.getRoleId()==2) {//user
if (backstageUser.getShopIds() == null) {
shopIds = "0";
} else {
if (backstageUser.getShopIds().endsWith(",")) {
String str = backstageUser.getShopIds();
shopIds = str.substring(0, str.length() - 1);
}
}
data=classifyMapper.selectByShopIds(shopIds);
}
//定义新的list
List<classify> categoriesList = new ArrayList<>();
//先找到所有的一级分类
for (classify category : data) {
// 一级菜单的parentId是0
if (category.getParentid() == 0) {
categoriesList.add(category);
}
}
// 为一级菜单设置子菜单,getChild是递归调用的
for (classify category : categoriesList) {
category.setChildren(getChilde(category.getId(), data));
}
return categoriesList;
}
/**
* 递归查找子菜单
*
* @param id 当前菜单id
* @param rootList 要查找的列表
* @return
*/
private List<classify> getChilde(Integer id, List<classify> rootList) {
//子菜单
List<classify> childList = new ArrayList<>();
for (classify category : rootList) {
// 遍历所有节点,将父菜单id与传过来的id比较
if (category.getParentid() == id) {
childList.add(category);
}
}
// 把子菜单的子菜单再循环一遍
for (classify category : childList) {
category.setChildren(getChilde(category.getId(), rootList));
}
// 递归退出条件
if (childList.size() == 0) {
return null;
}
return childList;
}
其他
/**
* 下拉框树机构
* @return
*/
public List<measurement> query1(String shopId) {
List<measurement> query1 =null;
if("".equals(shopId)){
query1=measurementMapper.query1();
}else{
query1=measurementMapper.selectByShopIds(Integer.valueOf(shopId));
}
List<measurement> measurementList = new ArrayList<>();
for (measurement measurement : query1) {
if (measurement.getParentid() == 0) {
measurementList.add(measurement);
}
}
for (measurement measurement : measurementList) {
measurement.setChildren(queryCatList1(measurement.getId(), query1));
}
return measurementList;
}
List<measurement> queryCatList1(Integer id, List<measurement> query2) {
List<measurement> childList = new ArrayList<>();
for (measurement measurement : query2) {
if (measurement.getParentid() == id) {
childList.add(measurement);
}
}
for (measurement measurement : childList) {
measurement.setChildren(queryCatList1(measurement.getId(), query2));
}
// 递归退出条件
if (childList.size() == 0) {
return null;
}
return childList;
}