|
@@ -7,6 +7,7 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
|
public class ProductLevelService {
|
|
@@ -101,25 +102,42 @@ public class ProductLevelService {
|
|
|
return list;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取目录上的所有id
|
|
|
+ */
|
|
|
public List<Long> getAllIds(long id) {
|
|
|
+ List<ProductLevel> ls = productLevelDao.getList();
|
|
|
+ ProductLevel productLevel = null;
|
|
|
+ for (ProductLevel p : ls) {
|
|
|
+ if (p.getId() == id) {
|
|
|
+ productLevel = p;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ List<ProductLevel> list = new ArrayList<>();
|
|
|
+ getMaximumParent(list, ls, productLevel);
|
|
|
+ list.add(productLevel);
|
|
|
+
|
|
|
List<Long> ids = new ArrayList<>();
|
|
|
- ProductLevel productLevel = this.get(id);
|
|
|
- getIds(ids, productLevel);
|
|
|
- ids.add(id);
|
|
|
+ list.forEach(ll -> ids.add(ll.getId()));
|
|
|
return ids;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取目录下的所有id
|
|
|
+ * 根据当前目录获取根目录
|
|
|
*/
|
|
|
- private void getIds(List<Long> ids, ProductLevel productLevel) {
|
|
|
- List<ProductLevel> children = productLevel.getChildren();
|
|
|
- if (children.size() > 0) {
|
|
|
- for (ProductLevel p : children) {
|
|
|
- ids.add(p.getId());
|
|
|
- getIds(ids, p);
|
|
|
- }
|
|
|
+ public ProductLevel getMaximumParent(List<ProductLevel> list, List<ProductLevel> deptAll, ProductLevel deptChild) {
|
|
|
+ ProductLevel dept = null;
|
|
|
+ long parentId = deptChild.getParentId();
|
|
|
+ if (parentId == -1) {
|
|
|
+ dept = deptChild;
|
|
|
+ } else {
|
|
|
+ List<ProductLevel> parent = deptAll.stream().filter(item -> item.getId() == parentId).collect(Collectors.toList());
|
|
|
+ list.addAll(parent);
|
|
|
+ ProductLevel maximumParent = getMaximumParent(list, deptAll, parent.get(0));
|
|
|
+ dept = maximumParent;
|
|
|
}
|
|
|
+ return dept;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
}
|