Commit 18051dc8fdb9f2539f9a2ba21a4b52002270d198
1 parent
9e93e31a
品类添加树形结构返回接口
Showing
8 changed files
with
181 additions
and
18 deletions
assistant-product/src/main/java/com/diligrp/assistant/product/controller/CategoryOpenApiController.java
1 | package com.diligrp.assistant.product.controller; | 1 | package com.diligrp.assistant.product.controller; |
2 | 2 | ||
3 | -import com.diligrp.assistant.product.domain.CategoryVO; | ||
4 | -import com.diligrp.assistant.product.domain.CategoryPageQuery; | ||
5 | -import com.diligrp.assistant.product.domain.ListCategory; | 3 | +import com.diligrp.assistant.product.domain.*; |
6 | import com.diligrp.assistant.product.model.Category; | 4 | import com.diligrp.assistant.product.model.Category; |
7 | import com.diligrp.assistant.product.service.ProductCategoryService; | 5 | import com.diligrp.assistant.product.service.ProductCategoryService; |
8 | import com.diligrp.assistant.shared.domain.Message; | 6 | import com.diligrp.assistant.shared.domain.Message; |
@@ -30,21 +28,21 @@ public class CategoryOpenApiController { | @@ -30,21 +28,21 @@ public class CategoryOpenApiController { | ||
30 | public Message<CategoryVO> findById(@RequestParam("id") Long id) { | 28 | public Message<CategoryVO> findById(@RequestParam("id") Long id) { |
31 | Category category = productCategoryService.findCategoryById(id); | 29 | Category category = productCategoryService.findCategoryById(id); |
32 | return Message.success(CategoryVO.of(category.getId(), category.getParentId(), | 30 | return Message.success(CategoryVO.of(category.getId(), category.getParentId(), |
33 | - category.getName(), category.getAlias(), category.getLevel(), category.getIcon(), category.getPath())); | 31 | + category.getName(), category.getAlias(), category.getLevel(), category.getIcon(), category.getPath())); |
34 | } | 32 | } |
35 | 33 | ||
36 | @RequestMapping(value = "/findParentById.do") | 34 | @RequestMapping(value = "/findParentById.do") |
37 | public Message<CategoryVO> findParentById(@RequestParam("id") Long id) { | 35 | public Message<CategoryVO> findParentById(@RequestParam("id") Long id) { |
38 | Category category = productCategoryService.findParentCategoryById(id); | 36 | Category category = productCategoryService.findParentCategoryById(id); |
39 | return Message.success(CategoryVO.of(category.getId(), category.getParentId(), | 37 | return Message.success(CategoryVO.of(category.getId(), category.getParentId(), |
40 | - category.getName(), category.getAlias(), category.getLevel(), category.getIcon(), category.getPath())); | 38 | + category.getName(), category.getAlias(), category.getLevel(), category.getIcon(), category.getPath())); |
41 | } | 39 | } |
42 | 40 | ||
43 | @RequestMapping(value = "/listParentsById.do") | 41 | @RequestMapping(value = "/listParentsById.do") |
44 | public Message<List<CategoryVO>> listParentsById(@RequestParam("id") Long id) { | 42 | public Message<List<CategoryVO>> listParentsById(@RequestParam("id") Long id) { |
45 | List<CategoryVO> categories = productCategoryService.listParentsById(id).stream().map(category -> | 43 | List<CategoryVO> categories = productCategoryService.listParentsById(id).stream().map(category -> |
46 | - CategoryVO.of(category.getId(), category.getParentId(), category.getName(), category.getAlias(), | ||
47 | - category.getLevel(), category.getIcon(), category.getPath())).collect(Collectors.toList()); | 44 | + CategoryVO.of(category.getId(), category.getParentId(), category.getName(), category.getAlias(), |
45 | + category.getLevel(), category.getIcon(), category.getPath())).collect(Collectors.toList()); | ||
48 | return Message.success(categories); | 46 | return Message.success(categories); |
49 | } | 47 | } |
50 | 48 | ||
@@ -59,8 +57,8 @@ public class CategoryOpenApiController { | @@ -59,8 +57,8 @@ public class CategoryOpenApiController { | ||
59 | query.from(request.getPageNo(), request.getPageSize()); | 57 | query.from(request.getPageNo(), request.getPageSize()); |
60 | 58 | ||
61 | List<CategoryVO> categories = productCategoryService.listChildrenById(query).stream().map(category -> | 59 | List<CategoryVO> categories = productCategoryService.listChildrenById(query).stream().map(category -> |
62 | - CategoryVO.of(category.getId(), category.getParentId(), category.getName(), category.getAlias(), | ||
63 | - category.getLevel(), category.getIcon(), category.getPath())).collect(Collectors.toList()); | 60 | + CategoryVO.of(category.getId(), category.getParentId(), category.getName(), category.getAlias(), |
61 | + category.getLevel(), category.getIcon(), category.getPath())).collect(Collectors.toList()); | ||
64 | return Message.success(categories); | 62 | return Message.success(categories); |
65 | } | 63 | } |
66 | 64 | ||
@@ -75,8 +73,8 @@ public class CategoryOpenApiController { | @@ -75,8 +73,8 @@ public class CategoryOpenApiController { | ||
75 | query.from(request.getPageNo(), request.getPageSize()); | 73 | query.from(request.getPageNo(), request.getPageSize()); |
76 | 74 | ||
77 | List<CategoryVO> categories = productCategoryService.listCategoriesByLevel(query).stream().map(category -> | 75 | List<CategoryVO> categories = productCategoryService.listCategoriesByLevel(query).stream().map(category -> |
78 | - CategoryVO.of(category.getId(), category.getParentId(), category.getName(), category.getAlias(), | ||
79 | - category.getLevel(), category.getIcon(), category.getPath())).collect(Collectors.toList()); | 76 | + CategoryVO.of(category.getId(), category.getParentId(), category.getName(), category.getAlias(), |
77 | + category.getLevel(), category.getIcon(), category.getPath())).collect(Collectors.toList()); | ||
80 | return Message.success(categories); | 78 | return Message.success(categories); |
81 | } | 79 | } |
82 | 80 | ||
@@ -89,4 +87,14 @@ public class CategoryOpenApiController { | @@ -89,4 +87,14 @@ public class CategoryOpenApiController { | ||
89 | category.getLevel(), category.getIcon(), category.getPath())).collect(Collectors.toList()); | 87 | category.getLevel(), category.getIcon(), category.getPath())).collect(Collectors.toList()); |
90 | return Message.success(categories); | 88 | return Message.success(categories); |
91 | } | 89 | } |
90 | + | ||
91 | + | ||
92 | + @RequestMapping(value = "/amis/tree.do") | ||
93 | + public Message<List<CategoryNodeVO>> findTree(@RequestParam(value = "term", required = false) String term) { | ||
94 | + CategoryTreeQuery categoryTreeQuery = new CategoryTreeQuery(); | ||
95 | + categoryTreeQuery.setTerm(term); | ||
96 | + return Message.success(productCategoryService.tree(categoryTreeQuery)); | ||
97 | + } | ||
98 | + | ||
99 | + | ||
92 | } | 100 | } |
assistant-product/src/main/java/com/diligrp/assistant/product/dao/ProductCategoryDao.java
1 | package com.diligrp.assistant.product.dao; | 1 | package com.diligrp.assistant.product.dao; |
2 | 2 | ||
3 | +import com.diligrp.assistant.product.domain.CategoryNodeVO; | ||
3 | import com.diligrp.assistant.product.domain.CategoryPageQuery; | 4 | import com.diligrp.assistant.product.domain.CategoryPageQuery; |
5 | +import com.diligrp.assistant.product.domain.CategoryTreeQuery; | ||
4 | import com.diligrp.assistant.product.model.Category; | 6 | import com.diligrp.assistant.product.model.Category; |
5 | import com.diligrp.assistant.shared.mybatis.MybatisMapperSupport; | 7 | import com.diligrp.assistant.shared.mybatis.MybatisMapperSupport; |
6 | import org.springframework.stereotype.Repository; | 8 | import org.springframework.stereotype.Repository; |
@@ -27,4 +29,6 @@ public interface ProductCategoryDao extends MybatisMapperSupport { | @@ -27,4 +29,6 @@ public interface ProductCategoryDao extends MybatisMapperSupport { | ||
27 | List<Category> listCategoriesByLevel(CategoryPageQuery query); | 29 | List<Category> listCategoriesByLevel(CategoryPageQuery query); |
28 | 30 | ||
29 | List<Category> list(CategoryPageQuery query); | 31 | List<Category> list(CategoryPageQuery query); |
32 | + | ||
33 | + List<CategoryNodeVO> tree(CategoryTreeQuery query); | ||
30 | } | 34 | } |
assistant-product/src/main/java/com/diligrp/assistant/product/domain/CategoryNodeVO.java
0 → 100644
1 | +package com.diligrp.assistant.product.domain; | ||
2 | + | ||
3 | +import java.util.List; | ||
4 | + | ||
5 | +public class CategoryNodeVO { | ||
6 | + // ID | ||
7 | + private Long categoryId; | ||
8 | + // 父品类ID | ||
9 | + private Long parentId; | ||
10 | + // 名称 | ||
11 | + private String categoryName; | ||
12 | + // 级别 | ||
13 | + private Integer level; | ||
14 | + // 路径 | ||
15 | + private String path; | ||
16 | + //子节点 | ||
17 | + private List<CategoryNodeVO> children; | ||
18 | + | ||
19 | + public Long getCategoryId() { | ||
20 | + return categoryId; | ||
21 | + } | ||
22 | + | ||
23 | + public void setCategoryId(Long categoryId) { | ||
24 | + this.categoryId = categoryId; | ||
25 | + } | ||
26 | + | ||
27 | + public Long getParentId() { | ||
28 | + return parentId; | ||
29 | + } | ||
30 | + | ||
31 | + public void setParentId(Long parentId) { | ||
32 | + this.parentId = parentId; | ||
33 | + } | ||
34 | + | ||
35 | + public String getCategoryName() { | ||
36 | + return categoryName; | ||
37 | + } | ||
38 | + | ||
39 | + public void setCategoryName(String categoryName) { | ||
40 | + this.categoryName = categoryName; | ||
41 | + } | ||
42 | + | ||
43 | + public Integer getLevel() { | ||
44 | + return level; | ||
45 | + } | ||
46 | + | ||
47 | + public void setLevel(Integer level) { | ||
48 | + this.level = level; | ||
49 | + } | ||
50 | + | ||
51 | + public String getPath() { | ||
52 | + return path; | ||
53 | + } | ||
54 | + | ||
55 | + public void setPath(String path) { | ||
56 | + this.path = path; | ||
57 | + } | ||
58 | + | ||
59 | + public List<CategoryNodeVO> getChildren() { | ||
60 | + return children; | ||
61 | + } | ||
62 | + | ||
63 | + public void setChildren(List<CategoryNodeVO> children) { | ||
64 | + this.children = children; | ||
65 | + } | ||
66 | +} |
assistant-product/src/main/java/com/diligrp/assistant/product/domain/CategoryPageQuery.java
@@ -8,6 +8,8 @@ public class CategoryPageQuery extends PageQuery { | @@ -8,6 +8,8 @@ public class CategoryPageQuery extends PageQuery { | ||
8 | // 区域级别 | 8 | // 区域级别 |
9 | private Integer level; | 9 | private Integer level; |
10 | 10 | ||
11 | + private String term; | ||
12 | + | ||
11 | public Long getId() { | 13 | public Long getId() { |
12 | return id; | 14 | return id; |
13 | } | 15 | } |
@@ -23,4 +25,12 @@ public class CategoryPageQuery extends PageQuery { | @@ -23,4 +25,12 @@ public class CategoryPageQuery extends PageQuery { | ||
23 | public void setLevel(Integer level) { | 25 | public void setLevel(Integer level) { |
24 | this.level = level; | 26 | this.level = level; |
25 | } | 27 | } |
28 | + | ||
29 | + public String getTerm() { | ||
30 | + return term; | ||
31 | + } | ||
32 | + | ||
33 | + public void setTerm(String term) { | ||
34 | + this.term = term; | ||
35 | + } | ||
26 | } | 36 | } |
assistant-product/src/main/java/com/diligrp/assistant/product/domain/CategoryTreeQuery.java
0 → 100644
1 | +package com.diligrp.assistant.product.domain; | ||
2 | + | ||
3 | +import java.io.Serializable; | ||
4 | + | ||
5 | +public class CategoryTreeQuery implements Serializable { | ||
6 | + | ||
7 | + | ||
8 | + private String term; | ||
9 | + | ||
10 | + | ||
11 | + public String getTerm() { | ||
12 | + return term; | ||
13 | + } | ||
14 | + | ||
15 | + public void setTerm(String term) { | ||
16 | + this.term = term; | ||
17 | + } | ||
18 | +} |
assistant-product/src/main/java/com/diligrp/assistant/product/service/ProductCategoryService.java
1 | package com.diligrp.assistant.product.service; | 1 | package com.diligrp.assistant.product.service; |
2 | 2 | ||
3 | import com.diligrp.assistant.product.domain.CategoryDTO; | 3 | import com.diligrp.assistant.product.domain.CategoryDTO; |
4 | +import com.diligrp.assistant.product.domain.CategoryNodeVO; | ||
4 | import com.diligrp.assistant.product.domain.CategoryPageQuery; | 5 | import com.diligrp.assistant.product.domain.CategoryPageQuery; |
6 | +import com.diligrp.assistant.product.domain.CategoryTreeQuery; | ||
5 | import com.diligrp.assistant.product.model.Category; | 7 | import com.diligrp.assistant.product.model.Category; |
6 | 8 | ||
7 | import java.util.List; | 9 | import java.util.List; |
@@ -77,4 +79,13 @@ public interface ProductCategoryService { | @@ -77,4 +79,13 @@ public interface ProductCategoryService { | ||
77 | * @return 品类 | 79 | * @return 品类 |
78 | */ | 80 | */ |
79 | List<Category> list(CategoryPageQuery query); | 81 | List<Category> list(CategoryPageQuery query); |
82 | + | ||
83 | + | ||
84 | + /** | ||
85 | + * 查询所有品类 | ||
86 | + * | ||
87 | + * @param query 查询条件 | ||
88 | + * @return 品类 | ||
89 | + */ | ||
90 | + List<CategoryNodeVO> tree(CategoryTreeQuery query); | ||
80 | } | 91 | } |
assistant-product/src/main/java/com/diligrp/assistant/product/service/impl/ProductCategoryServiceImpl.java
@@ -2,7 +2,9 @@ package com.diligrp.assistant.product.service.impl; | @@ -2,7 +2,9 @@ package com.diligrp.assistant.product.service.impl; | ||
2 | 2 | ||
3 | import com.diligrp.assistant.product.dao.ProductCategoryDao; | 3 | import com.diligrp.assistant.product.dao.ProductCategoryDao; |
4 | import com.diligrp.assistant.product.domain.CategoryDTO; | 4 | import com.diligrp.assistant.product.domain.CategoryDTO; |
5 | +import com.diligrp.assistant.product.domain.CategoryNodeVO; | ||
5 | import com.diligrp.assistant.product.domain.CategoryPageQuery; | 6 | import com.diligrp.assistant.product.domain.CategoryPageQuery; |
7 | +import com.diligrp.assistant.product.domain.CategoryTreeQuery; | ||
6 | import com.diligrp.assistant.product.exception.CategoryServiceException; | 8 | import com.diligrp.assistant.product.exception.CategoryServiceException; |
7 | import com.diligrp.assistant.product.model.Category; | 9 | import com.diligrp.assistant.product.model.Category; |
8 | import com.diligrp.assistant.product.service.ProductCategoryService; | 10 | import com.diligrp.assistant.product.service.ProductCategoryService; |
@@ -13,6 +15,7 @@ import org.springframework.transaction.annotation.Transactional; | @@ -13,6 +15,7 @@ import org.springframework.transaction.annotation.Transactional; | ||
13 | 15 | ||
14 | import java.time.LocalDateTime; | 16 | import java.time.LocalDateTime; |
15 | import java.util.*; | 17 | import java.util.*; |
18 | +import java.util.stream.Collectors; | ||
16 | 19 | ||
17 | @Service("productCategoryService") | 20 | @Service("productCategoryService") |
18 | public class ProductCategoryServiceImpl implements ProductCategoryService { | 21 | public class ProductCategoryServiceImpl implements ProductCategoryService { |
@@ -26,9 +29,9 @@ public class ProductCategoryServiceImpl implements ProductCategoryService { | @@ -26,9 +29,9 @@ public class ProductCategoryServiceImpl implements ProductCategoryService { | ||
26 | LocalDateTime now = LocalDateTime.now(); | 29 | LocalDateTime now = LocalDateTime.now(); |
27 | Category parent = findCategoryById(request.getParentId()); | 30 | Category parent = findCategoryById(request.getParentId()); |
28 | Category category = Category.builder().parentId(request.getParentId()).name(request.getName()) | 31 | Category category = Category.builder().parentId(request.getParentId()).name(request.getName()) |
29 | - .alias(request.getAlias()).level(parent.getLevel() + 1).pyCode(request.getPyCode()) | ||
30 | - .shortCode(request.getShortCode()).path(parent.getPath()).icon(request.getIcon()).state(1).version(0) | ||
31 | - .createdTime(now).modifiedTime(now).build(); | 32 | + .alias(request.getAlias()).level(parent.getLevel() + 1).pyCode(request.getPyCode()) |
33 | + .shortCode(request.getShortCode()).path(parent.getPath()).icon(request.getIcon()).state(1).version(0) | ||
34 | + .createdTime(now).modifiedTime(now).build(); | ||
32 | productCategoryDao.insertCategory(category); | 35 | productCategoryDao.insertCategory(category); |
33 | 36 | ||
34 | // 更新路径信息, 将自身ID添加到路径信息中 | 37 | // 更新路径信息, 将自身ID添加到路径信息中 |
@@ -45,8 +48,8 @@ public class ProductCategoryServiceImpl implements ProductCategoryService { | @@ -45,8 +48,8 @@ public class ProductCategoryServiceImpl implements ProductCategoryService { | ||
45 | public void updateCategory(CategoryDTO request) { | 48 | public void updateCategory(CategoryDTO request) { |
46 | LocalDateTime now = LocalDateTime.now(); | 49 | LocalDateTime now = LocalDateTime.now(); |
47 | Category category = Category.builder().name(request.getName()) | 50 | Category category = Category.builder().name(request.getName()) |
48 | - .alias(request.getAlias()).pyCode(request.getPyCode()).shortCode(request.getShortCode()) | ||
49 | - .icon(request.getIcon()).modifiedTime(now).build(); | 51 | + .alias(request.getAlias()).pyCode(request.getPyCode()).shortCode(request.getShortCode()) |
52 | + .icon(request.getIcon()).modifiedTime(now).build(); | ||
50 | category.setId(request.getId()); | 53 | category.setId(request.getId()); |
51 | if (productCategoryDao.updateCategory(category) == 0) { | 54 | if (productCategoryDao.updateCategory(category) == 0) { |
52 | throw new CategoryServiceException(ErrorCode.OBJECT_NOT_FOUND, "品类不存在"); | 55 | throw new CategoryServiceException(ErrorCode.OBJECT_NOT_FOUND, "品类不存在"); |
@@ -74,13 +77,13 @@ public class ProductCategoryServiceImpl implements ProductCategoryService { | @@ -74,13 +77,13 @@ public class ProductCategoryServiceImpl implements ProductCategoryService { | ||
74 | @Override | 77 | @Override |
75 | public Category findCategoryById(Long id) { | 78 | public Category findCategoryById(Long id) { |
76 | return productCategoryDao.findCategoryById(id).orElseThrow(() -> | 79 | return productCategoryDao.findCategoryById(id).orElseThrow(() -> |
77 | - new CategoryServiceException(ErrorCode.OBJECT_NOT_FOUND, "品类不存在")); | 80 | + new CategoryServiceException(ErrorCode.OBJECT_NOT_FOUND, "品类不存在")); |
78 | } | 81 | } |
79 | 82 | ||
80 | @Override | 83 | @Override |
81 | public Category findParentCategoryById(Long id) { | 84 | public Category findParentCategoryById(Long id) { |
82 | return productCategoryDao.findParentCategoryById(id).orElseThrow(() -> | 85 | return productCategoryDao.findParentCategoryById(id).orElseThrow(() -> |
83 | - new CategoryServiceException(ErrorCode.OBJECT_NOT_FOUND, "品类不存在")); | 86 | + new CategoryServiceException(ErrorCode.OBJECT_NOT_FOUND, "品类不存在")); |
84 | } | 87 | } |
85 | 88 | ||
86 | @Override | 89 | @Override |
@@ -119,4 +122,23 @@ public class ProductCategoryServiceImpl implements ProductCategoryService { | @@ -119,4 +122,23 @@ public class ProductCategoryServiceImpl implements ProductCategoryService { | ||
119 | public List<Category> list(CategoryPageQuery query) { | 122 | public List<Category> list(CategoryPageQuery query) { |
120 | return productCategoryDao.list(query); | 123 | return productCategoryDao.list(query); |
121 | } | 124 | } |
125 | + | ||
126 | + @Override | ||
127 | + public List<CategoryNodeVO> tree(CategoryTreeQuery query) { | ||
128 | + List<CategoryNodeVO> categoryNodeList = productCategoryDao.tree(query); | ||
129 | + List<CategoryNodeVO> categoryTree = streamToTree(categoryNodeList, 0L); | ||
130 | + return categoryTree; | ||
131 | + } | ||
132 | + | ||
133 | + public static List<CategoryNodeVO> streamToTree(List<CategoryNodeVO> treeList, Long parentId) { | ||
134 | + List<CategoryNodeVO> list = treeList.stream() | ||
135 | + // 过滤父节点 | ||
136 | + .filter(parent -> parent.getParentId().equals(parentId)) | ||
137 | + // 把父节点children递归赋值成为子节点 | ||
138 | + .map(child -> { | ||
139 | + child.setChildren(streamToTree(treeList, child.getCategoryId())); | ||
140 | + return child; | ||
141 | + }).collect(Collectors.toList()); | ||
142 | + return list; | ||
143 | + } | ||
122 | } | 144 | } |
assistant-product/src/main/resources/com/diligrp/assistant/dao/mapper/ProductCategoryDao.xml
@@ -90,4 +90,28 @@ | @@ -90,4 +90,28 @@ | ||
90 | <select id="list" parameterType="com.diligrp.assistant.product.domain.CategoryPageQuery" resultMap="CategoryMap"> | 90 | <select id="list" parameterType="com.diligrp.assistant.product.domain.CategoryPageQuery" resultMap="CategoryMap"> |
91 | SELECT * FROM product_category ORDER BY ID | 91 | SELECT * FROM product_category ORDER BY ID |
92 | </select> | 92 | </select> |
93 | + | ||
94 | + <select id="tree" parameterType="com.diligrp.assistant.product.domain.CategoryPageQuery" resultType="com.diligrp.assistant.product.domain.CategoryNodeVO"> | ||
95 | + | ||
96 | + WITH RECURSIVE CategoryPath AS ( | ||
97 | + SELECT | ||
98 | + id as categoryId,parent_id as parentId,name as categoryName,level as level, path as path | ||
99 | + FROM | ||
100 | + product_category | ||
101 | + <where> | ||
102 | + state > 0 | ||
103 | + <if test="term != null"> | ||
104 | + AND name like CONCAT('%',#{term},'%') | ||
105 | + </if> | ||
106 | + </where> | ||
107 | + | ||
108 | + UNION ALL | ||
109 | + | ||
110 | + SELECT | ||
111 | + c.id as categoryId,c.parent_id as parentId,c.name as categoryName,c.level as level, c.path as path | ||
112 | + FROM product_category c | ||
113 | + INNER JOIN CategoryPath cp ON c.id = cp.parentId | ||
114 | + ) | ||
115 | + SELECT DISTINCT * FROM CategoryPath; | ||
116 | + </select> | ||
93 | </mapper> | 117 | </mapper> |