|
@ -10,12 +10,20 @@ import com.epmet.commons.tools.validator.group.UpdateGroup; |
|
|
import com.epmet.commons.tools.validator.group.DefaultGroup; |
|
|
import com.epmet.commons.tools.validator.group.DefaultGroup; |
|
|
import com.epmet.dto.CategoryDictDTO; |
|
|
import com.epmet.dto.CategoryDictDTO; |
|
|
import com.epmet.service.CategoryDictService; |
|
|
import com.epmet.service.CategoryDictService; |
|
|
|
|
|
import lombok.AllArgsConstructor; |
|
|
|
|
|
import lombok.Data; |
|
|
|
|
|
import lombok.NoArgsConstructor; |
|
|
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.web.bind.annotation.*; |
|
|
import org.springframework.web.bind.annotation.*; |
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
import java.util.HashMap; |
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
import java.util.Map; |
|
|
import java.util.Map; |
|
|
|
|
|
import java.util.function.Function; |
|
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
@ -31,12 +39,87 @@ public class CategoryDictController { |
|
|
@Autowired |
|
|
@Autowired |
|
|
private CategoryDictService categoryDictService; |
|
|
private CategoryDictService categoryDictService; |
|
|
|
|
|
|
|
|
|
|
|
@Data |
|
|
|
|
|
@NoArgsConstructor |
|
|
|
|
|
public static class TreeNode { |
|
|
|
|
|
|
|
|
|
|
|
private String pid; |
|
|
|
|
|
private String id; |
|
|
|
|
|
private String categoryName; |
|
|
|
|
|
|
|
|
|
|
|
private List<TreeNode> children = new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
|
|
public TreeNode(String pid, String id, String categoryName) { |
|
|
|
|
|
this.pid = pid; |
|
|
|
|
|
this.id = id; |
|
|
|
|
|
this.categoryName = categoryName; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@GetMapping("tree") |
|
|
|
|
|
public Result<List<TreeNode>> tree() { |
|
|
|
|
|
List<TreeNode> treeNodes = buildTree(categoryDictService.list(1, 100)); |
|
|
|
|
|
return new Result().ok(treeNodes); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 树形结构输出 |
|
|
|
|
|
*/ |
|
|
@RequestMapping("list") |
|
|
@RequestMapping("list") |
|
|
public Result<List<CategoryDictDTO>> list(@RequestParam Integer pageNo, @RequestParam Integer pageSize){ |
|
|
public Result<List<CategoryDictDTO>> list(@RequestParam Integer pageNo, @RequestParam Integer pageSize){ |
|
|
List<CategoryDictDTO> list = categoryDictService.list(pageNo, pageSize); |
|
|
List<CategoryDictDTO> list = categoryDictService.list(pageNo, pageSize); |
|
|
return new Result<List<CategoryDictDTO>>().ok(list); |
|
|
return new Result<List<CategoryDictDTO>>().ok(list); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @description: 根据父级栏目id查询子栏目列表 |
|
|
|
|
|
* @param parentCategoryId: 父级栏目id |
|
|
|
|
|
* @return |
|
|
|
|
|
* @author: WangXianZhang |
|
|
|
|
|
* @date: 2023/4/25 3:33 PM |
|
|
|
|
|
*/ |
|
|
|
|
|
@GetMapping("subCategories") |
|
|
|
|
|
public Result<List<CategoryDictDTO>> listSubCategories(@RequestParam("parentCategoryId") String parentCategoryId) { |
|
|
|
|
|
List<CategoryDictDTO> list = categoryDictService.listSubCategories(parentCategoryId); |
|
|
|
|
|
return new Result<List<CategoryDictDTO>>().ok(list); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @description: 构建树状结构 |
|
|
|
|
|
* @param list: |
|
|
|
|
|
* @return |
|
|
|
|
|
* @author: WangXianZhang |
|
|
|
|
|
* @date: 2023/4/25 3:38 PM |
|
|
|
|
|
*/ |
|
|
|
|
|
private List<TreeNode> buildTree(List<CategoryDictDTO> list) { |
|
|
|
|
|
|
|
|
|
|
|
//总共2级,直接构建就行了。
|
|
|
|
|
|
List<TreeNode> pNodes = new ArrayList<>(); |
|
|
|
|
|
List<TreeNode> sNodes = new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
|
|
// 过滤出一级和二级节点
|
|
|
|
|
|
for (CategoryDictDTO ele : list) { |
|
|
|
|
|
if (StringUtils.isBlank(ele.getPid()) || "0".equals(ele.getPid())) { |
|
|
|
|
|
// 是一级节点
|
|
|
|
|
|
pNodes.add(new TreeNode(ele.getPid(), ele.getId(), ele.getCategoryName())); |
|
|
|
|
|
} else { |
|
|
|
|
|
// 二级节点
|
|
|
|
|
|
sNodes.add(new TreeNode(ele.getPid(), ele.getId(), ele.getCategoryName())); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 追加二级节点
|
|
|
|
|
|
for (TreeNode sNode : sNodes) { |
|
|
|
|
|
for (TreeNode pNode : pNodes) { |
|
|
|
|
|
if (pNode.getId().equals(sNode.getPid())) { |
|
|
|
|
|
pNode.getChildren().add(sNode); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return pNodes; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@RequestMapping(value = "{id}",method = {RequestMethod.POST,RequestMethod.GET}) |
|
|
@RequestMapping(value = "{id}",method = {RequestMethod.POST,RequestMethod.GET}) |
|
|
public Result<CategoryDictDTO> get(@PathVariable("id") String id){ |
|
|
public Result<CategoryDictDTO> get(@PathVariable("id") String id){ |
|
|
CategoryDictDTO data = categoryDictService.get(id); |
|
|
CategoryDictDTO data = categoryDictService.get(id); |
|
|