fix:添加产品分类树状列表接口

main
HuangHuiKang 1 week ago
parent 2a842d54f4
commit d081c4d0d7

@ -8,6 +8,8 @@ import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.category.ErpProductCategoryListReqVO; import cn.iocoder.yudao.module.erp.controller.admin.product.vo.category.ErpProductCategoryListReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.category.ErpProductCategoryRespVO; import cn.iocoder.yudao.module.erp.controller.admin.product.vo.category.ErpProductCategoryRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.category.ErpProductCategorySaveReqVO; import cn.iocoder.yudao.module.erp.controller.admin.product.vo.category.ErpProductCategorySaveReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.category.ErpProductCategoryTreeRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.category.ErpProductCategoryTypeGroupRespVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.product.ErpProductCategoryDO; import cn.iocoder.yudao.module.erp.dal.dataobject.product.ErpProductCategoryDO;
import cn.iocoder.yudao.module.erp.service.product.ErpProductCategoryService; import cn.iocoder.yudao.module.erp.service.product.ErpProductCategoryService;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
@ -21,7 +23,10 @@ import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid; import javax.validation.Valid;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT; import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
@ -77,10 +82,28 @@ public class ErpProductCategoryController {
return success(BeanUtils.toBean(list, ErpProductCategoryRespVO.class)); return success(BeanUtils.toBean(list, ErpProductCategoryRespVO.class));
} }
@GetMapping("/group-tree")
@Operation(summary = "按类型分组获得产品分类树")
@PreAuthorize("@ss.hasPermission('erp:product-category:query')")
public CommonResult<List<ErpProductCategoryTypeGroupRespVO>> getProductCategoryGroupTree(@Valid ErpProductCategoryListReqVO listReqVO) {
List<ErpProductCategoryDO> list = productCategoryService.getProductCategoryList(listReqVO);
Map<Integer, List<ErpProductCategoryDO>> typeMap = new LinkedHashMap<>();
for (ErpProductCategoryDO category : list) {
typeMap.computeIfAbsent(category.getType(), key -> new ArrayList<>()).add(category);
}
List<ErpProductCategoryTypeGroupRespVO> result = new ArrayList<>();
for (Map.Entry<Integer, List<ErpProductCategoryDO>> entry : typeMap.entrySet()) {
ErpProductCategoryTypeGroupRespVO group = new ErpProductCategoryTypeGroupRespVO();
group.setType(entry.getKey());
group.setChildren(buildCategoryTree(entry.getValue()));
result.add(group);
}
return success(result);
}
@GetMapping("/simple-list") @GetMapping("/simple-list")
@Operation(summary = "获得产品分类精简列表", description = "只包含被开启的分类,主要用于前端的下拉选项") @Operation(summary = "获得产品分类精简列表", description = "只包含被开启的分类,主要用于前端的下拉选项")
public CommonResult<List<ErpProductCategoryRespVO>> getProductCategorySimpleList(@RequestParam("type") Integer type) { public CommonResult<List<ErpProductCategoryRespVO>> getProductCategorySimpleList(@RequestParam("type") Integer type) {
List<ErpProductCategoryDO> list = productCategoryService.getProductCategoryList( List<ErpProductCategoryDO> list = productCategoryService.getProductCategoryList(
new ErpProductCategoryListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus()).setType(type)); new ErpProductCategoryListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus()).setType(type));
return success(convertList(list, category -> new ErpProductCategoryRespVO() return success(convertList(list, category -> new ErpProductCategoryRespVO()
@ -92,11 +115,33 @@ public class ErpProductCategoryController {
@PreAuthorize("@ss.hasPermission('erp:product-category:export')") @PreAuthorize("@ss.hasPermission('erp:product-category:export')")
@ApiAccessLog(operateType = EXPORT) @ApiAccessLog(operateType = EXPORT)
public void exportProductCategoryExcel(@Valid ErpProductCategoryListReqVO listReqVO, public void exportProductCategoryExcel(@Valid ErpProductCategoryListReqVO listReqVO,
HttpServletResponse response) throws IOException { HttpServletResponse response) throws IOException {
List<ErpProductCategoryDO> list = productCategoryService.getProductCategoryList(listReqVO); List<ErpProductCategoryDO> list = productCategoryService.getProductCategoryList(listReqVO);
// 导出 Excel
ExcelUtils.write(response, "产品分类.xls", "数据", ErpProductCategoryRespVO.class, ExcelUtils.write(response, "产品分类.xls", "数据", ErpProductCategoryRespVO.class,
BeanUtils.toBean(list, ErpProductCategoryRespVO.class)); BeanUtils.toBean(list, ErpProductCategoryRespVO.class));
}
private List<ErpProductCategoryTreeRespVO> buildCategoryTree(List<ErpProductCategoryDO> categories) {
Map<Long, ErpProductCategoryTreeRespVO> nodeMap = new LinkedHashMap<>();
for (ErpProductCategoryDO category : categories) {
ErpProductCategoryTreeRespVO node = BeanUtils.toBean(category, ErpProductCategoryTreeRespVO.class);
node.setChildren(new ArrayList<>());
node.setLeaf(true);
nodeMap.put(category.getId(), node);
}
List<ErpProductCategoryTreeRespVO> roots = new ArrayList<>();
for (ErpProductCategoryDO category : categories) {
ErpProductCategoryTreeRespVO node = nodeMap.get(category.getId());
Long parentId = category.getParentId();
ErpProductCategoryTreeRespVO parent = parentId == null ? null : nodeMap.get(parentId);
if (parent == null || ErpProductCategoryDO.PARENT_ID_ROOT.equals(parentId)) {
roots.add(node);
continue;
}
parent.getChildren().add(node);
parent.setLeaf(false);
}
return roots;
} }
} }

@ -10,6 +10,12 @@ public class ErpProductCategoryListReqVO {
@Schema(description = "分类名称", example = "芋艿") @Schema(description = "分类名称", example = "芋艿")
private String name; private String name;
@Schema(description = "分类编码", example = "S110")
private String code;
@Schema(description = "父分类编号", example = "0")
private Long parentId;
@Schema(description = "分类类型", example = "1") @Schema(description = "分类类型", example = "1")
private Integer type; private Integer type;

@ -19,15 +19,17 @@ public interface ErpProductCategoryMapper extends BaseMapperX<ErpProductCategory
default List<ErpProductCategoryDO> selectList(ErpProductCategoryListReqVO reqVO) { default List<ErpProductCategoryDO> selectList(ErpProductCategoryListReqVO reqVO) {
return selectList(new LambdaQueryWrapperX<ErpProductCategoryDO>() return selectList(new LambdaQueryWrapperX<ErpProductCategoryDO>()
.likeIfPresent(ErpProductCategoryDO::getName, reqVO.getName()) .likeIfPresent(ErpProductCategoryDO::getName, reqVO.getName())
.likeIfPresent(ErpProductCategoryDO::getCode, reqVO.getCode())
.eqIfPresent(ErpProductCategoryDO::getParentId, reqVO.getParentId())
.eqIfPresent(ErpProductCategoryDO::getType, reqVO.getType()) .eqIfPresent(ErpProductCategoryDO::getType, reqVO.getType())
.eqIfPresent(ErpProductCategoryDO::getStatus, reqVO.getStatus()) .eqIfPresent(ErpProductCategoryDO::getStatus, reqVO.getStatus())
.orderByDesc(ErpProductCategoryDO::getSort) .orderByDesc(ErpProductCategoryDO::getSort)
.orderByDesc(ErpProductCategoryDO::getId)); .orderByDesc(ErpProductCategoryDO::getId));
} }
default ErpProductCategoryDO selectByParentIdAndName(Long parentId, String name) { default ErpProductCategoryDO selectByParentIdAndName(Long parentId, String name) {
return selectOne(ErpProductCategoryDO::getParentId, parentId, ErpProductCategoryDO::getName, name); return selectOne(ErpProductCategoryDO::getParentId, parentId, ErpProductCategoryDO::getName, name);
} }
default ErpProductCategoryDO selectByName(String name) { default ErpProductCategoryDO selectByName(String name) {
return selectOne(ErpProductCategoryDO::getName, name); return selectOne(ErpProductCategoryDO::getName, name);

@ -40,15 +40,15 @@ server:
springdoc: springdoc:
api-docs: api-docs:
enabled: false enabled: true
path: /v3/api-docs path: /v3/api-docs
swagger-ui: swagger-ui:
enabled: false enabled: true
path: /swagger-ui path: /swagger-ui
default-flat-param-object: true # 参见 https://doc.xiaominfo.com/docs/faq/v4/knife4j-parameterobject-flat-param 文档 default-flat-param-object: true # 参见 https://doc.xiaominfo.com/docs/faq/v4/knife4j-parameterobject-flat-param 文档
knife4j: knife4j:
enable: false enable: true
setting: setting:
language: zh_cn language: zh_cn

Loading…
Cancel
Save