系统中有一个三级下拉的Tree
下拉最多三级
数据表信息
一级的PID,为空
二级的PID,为一级的ID
三级的PID,为二级的ID
返回所有
@Override
public List<AssayItemClassExt> selectAllList() {
List<AssayItemClassExt> list = assayItemClassExtMapper.selectAllList();
// 一级、二级、三级
List<AssayItemClassExt> listOne = new ArrayList<>();
List<AssayItemClassExt> listTwo = new ArrayList<>();
List<AssayItemClassExt> listThree = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
AssayItemClassExt assayItemClassExt = list.get(i);
Short index = assayItemClassExt.getTier();
if (index == 1) {
listOne.add(assayItemClassExt);
}
if (index == 2) {
listTwo.add(assayItemClassExt);
}
if (index == 3) {
listThree.add(assayItemClassExt);
}
}
// 一级PID
Map<String, List<AssayItemClassExt>> parentIdOne = new HashMap<>();
for (int i = 0; i < listOne.size(); i++) {
String key = listOne.get(i).getId();
List<AssayItemClassExt> parentIdOneList = new ArrayList<>();
parentIdOne.put(key, parentIdOneList);
}
// 过滤二级节点
for (Map.Entry<String, List<AssayItemClassExt>> entry : parentIdOne.entrySet()) {
String key = entry.getKey();
List<AssayItemClassExt> value = entry.getValue();
for (int i = 0; i < listTwo.size(); i++) {
AssayItemClassExt entity = listTwo.get(i);
if (key.equals(entity.getPid())) {
value.add(entity);
}
}
}
// 二级PID
Map<String, List<AssayItemClassExt>> parentIdTwo = new HashMap<>();
for (int i = 0; i < listTwo.size(); i++) {
String key = listTwo.get(i).getId();
List<AssayItemClassExt> parentIdTwoList = new ArrayList<>();
parentIdTwo.put(key, parentIdTwoList);
}
// 过滤三级节点
for (Map.Entry<String, List<AssayItemClassExt>> entry : parentIdTwo.entrySet()) {
String key = entry.getKey();
List<AssayItemClassExt> value = entry.getValue();
for (int i = 0; i < listThree.size(); i++) {
AssayItemClassExt entity = listThree.get(i);
if (key.equals(entity.getPid())) {
value.add(entity);
}
}
}
// 清空list
list.clear();
// 重组list
for (int i = 0; i < listOne.size(); i++) {
AssayItemClassExt entityOne = listOne.get(i);
list.add(entityOne);
String keyOne = entityOne.getId();
List<AssayItemClassExt> valueOne = parentIdOne.get(keyOne);
if (valueOne.size() > 0) {
for (AssayItemClassExt entityTwo : valueOne) {
list.add(entityTwo);
String keyTwo = entityTwo.getId();
List<AssayItemClassExt> valueTwo = parentIdTwo.get(keyTwo);
if (valueTwo.size() > 0) {
list.addAll(valueTwo);
}
}
}
}
return list;
}
根据三级ids,返回所有
@Override
public List<AssayItemClassExt> selectListByIds(String[] ids){
if (ids != null && !ids.equals("")) {
List<AssayItemClassExt> list = assayItemClassExtMapper.selectAllList();
// 一级、二级、三级
List<AssayItemClassExt> listOne = new ArrayList<>();
List<AssayItemClassExt> listTwo = new ArrayList<>();
List<AssayItemClassExt> listThree = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
AssayItemClassExt assayItemClassExt = list.get(i);
Short index = assayItemClassExt.getTier();
if (index == 1) {
listOne.add(assayItemClassExt);
}
if (index == 2) {
listTwo.add(assayItemClassExt);
}
if (index == 3) {
listThree.add(assayItemClassExt);
}
}
List<AssayItemClassExt> listNew=new ArrayList<>();
for(String id:ids){
for(AssayItemClassExt assayItemClassExt:listThree){
if(id.equals(assayItemClassExt.getId())){
listNew.add(assayItemClassExt);
}
}
}
listThree.clear();
listThree.addAll(listNew);
// 二级
Map<String, AssayItemClassExt> mapTwo = new HashMap<>();
for (int i = 0; i < listThree.size(); i++) {
String key = listThree.get(i).getPid();
if (!mapTwo.containsKey(key)) {
AssayItemClassExt assayItemClassExt = null;
mapTwo.put(key, assayItemClassExt);
}
}
for (Map.Entry<String, AssayItemClassExt> entry : mapTwo.entrySet()) {
String key = entry.getKey();
for (int i = 0; i < listTwo.size(); i++) {
if (key.equals(listTwo.get(i).getId())) {
mapTwo.put(key, listTwo.get(i));
}
}
}
listTwo.clear();
for (AssayItemClassExt value : mapTwo.values()) {
listTwo.add(value);
}
// 一级
Map<String, AssayItemClassExt> mapOne = new HashMap<>();
for (Map.Entry<String, AssayItemClassExt> entry : mapTwo.entrySet()) {
AssayItemClassExt value = entry.getValue();
String key = value.getPid();
if (!mapOne.containsKey(key)) {
AssayItemClassExt assayItemClassExt = null;
mapOne.put(key, assayItemClassExt);
}
}
for (Map.Entry<String, AssayItemClassExt> entry : mapOne.entrySet()) {
String key = entry.getKey();
for (int i = 0; i < listOne.size(); i++) {
if (key.equals(listOne.get(i).getId())) {
mapOne.put(key, listOne.get(i));
}
}
}
listOne.clear();
for (AssayItemClassExt value : mapOne.values()) {
listOne.add(value);
}
// 清空list
list.clear();
// 重组list
for (AssayItemClassExt entityOne : listOne) {
list.add(entityOne);
String idOne = entityOne.getId();
for (AssayItemClassExt entityTwo : listTwo) {
if (idOne.equals(entityTwo.getPid())) {
list.add(entityTwo);
String idTwo = entityTwo.getId();
for (AssayItemClassExt entityThree : listThree) {
if (idTwo.equals(entityThree.getPid())) {
list.add(entityThree);
}
}
}
}
}
return list;
}
return null;
}