|
@@ -1,10 +1,10 @@
|
|
|
package org.jeecg.modules.baseCode.controller;
|
|
|
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.List;
|
|
|
+import java.util.*;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
|
import org.jeecg.modules.baseCode.entity.BaseProductClass;
|
|
|
import org.jeecg.modules.baseCode.service.IBaseProductClassService;
|
|
|
import org.jeecg.modules.baseCode.service.ISerialPatternService;
|
|
@@ -147,7 +147,11 @@ public class BaseProductClassController extends JeecgController<BaseProductClass
|
|
|
}
|
|
|
|
|
|
if(StringUtils.isBlank(baseProductClass.getParentId())){
|
|
|
- baseProductClass.setParentId("");
|
|
|
+ baseProductClass.setParentId(null);
|
|
|
+ baseProductClassService.update(baseProductClass,
|
|
|
+ new UpdateWrapper<BaseProductClass>().lambda()
|
|
|
+ .set(BaseProductClass::getParentId,null)
|
|
|
+ .eq(BaseProductClass::getId,baseProductClass.getId()));
|
|
|
}
|
|
|
|
|
|
baseProductClassService.updateById(baseProductClass);
|
|
@@ -246,6 +250,101 @@ public class BaseProductClassController extends JeecgController<BaseProductClass
|
|
|
return Result.OK(list);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 树形结构数据
|
|
|
+ *
|
|
|
+ */
|
|
|
+ @ApiOperation(value = "产品分类-树形结构数据", notes = "产品分类-树形结构数据")
|
|
|
+ @GetMapping(value = "/queryByTree")
|
|
|
+ public Result<List<BaseProductClass>> queryByTree() {
|
|
|
+
|
|
|
+ QueryWrapper<BaseProductClass> queryWrapper = new QueryWrapper();
|
|
|
+ queryWrapper.eq("del_flag", "0");
|
|
|
+
|
|
|
+ List<BaseProductClass> list = baseProductClassService.list(queryWrapper);
|
|
|
+
|
|
|
+ // 构建树形结构
|
|
|
+ List<BaseProductClass> tree = buildTree(list);
|
|
|
+
|
|
|
+ return Result.OK(tree);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<BaseProductClass> buildTree(List<BaseProductClass> allList) {
|
|
|
+ // 用于快速查找每个节点的 Map
|
|
|
+ Map<String, BaseProductClass> idToNodeMap = new HashMap<>();
|
|
|
+
|
|
|
+ // 初始化 Map
|
|
|
+ for (BaseProductClass node : allList) {
|
|
|
+ idToNodeMap.put(node.getId(), node);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构建树形结构的根节点列表
|
|
|
+ List<BaseProductClass> rootList = new ArrayList<>();
|
|
|
+
|
|
|
+ for (BaseProductClass node : allList) {
|
|
|
+ String parentId = node.getParentId();
|
|
|
+
|
|
|
+ if (parentId == null || parentId.isEmpty()) {
|
|
|
+ // 如果没有 parentId,说明是根节点
|
|
|
+ rootList.add(node);
|
|
|
+ } else {
|
|
|
+ // 如果有 parentId,将当前节点添加到父节点的 childList 中
|
|
|
+ BaseProductClass parentNode = idToNodeMap.get(parentId);
|
|
|
+ if (parentNode != null) {
|
|
|
+ parentNode.getChildren().add(node);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return rootList;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ // 示例数据
|
|
|
+ List<BaseProductClass> allList = new ArrayList<>();
|
|
|
+
|
|
|
+ BaseProductClass category1 = new BaseProductClass();
|
|
|
+ category1.setId("1");
|
|
|
+ category1.setName("Electronics");
|
|
|
+ category1.setParentId(null);
|
|
|
+
|
|
|
+ BaseProductClass category2 = new BaseProductClass();
|
|
|
+ category2.setId("2");
|
|
|
+ category2.setName("Phones");
|
|
|
+ category2.setParentId("1");
|
|
|
+
|
|
|
+ BaseProductClass category3 = new BaseProductClass();
|
|
|
+ category3.setId("3");
|
|
|
+ category3.setName("Laptops");
|
|
|
+ category3.setParentId("1");
|
|
|
+
|
|
|
+ BaseProductClass category4 = new BaseProductClass();
|
|
|
+ category4.setId("4");
|
|
|
+ category4.setName("iPhone");
|
|
|
+ category4.setParentId("2");
|
|
|
+
|
|
|
+ BaseProductClass category5 = new BaseProductClass();
|
|
|
+ category5.setId("5");
|
|
|
+ category5.setName("Gaming Laptops");
|
|
|
+ category5.setParentId("3");
|
|
|
+
|
|
|
+ allList.add(category1);
|
|
|
+ allList.add(category2);
|
|
|
+ allList.add(category3);
|
|
|
+ allList.add(category4);
|
|
|
+ allList.add(category5);
|
|
|
+
|
|
|
+ // 构建树形结构
|
|
|
+ List<BaseProductClass> tree = buildTree(allList);
|
|
|
+
|
|
|
+ // 打印树形结构
|
|
|
+ for (BaseProductClass root : tree) {
|
|
|
+ System.out.println(root);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 导出excel
|
|
|
*
|