document
API test

获取属性分组详情

GET
http://localhost:88/api/product/attrgroup/info/:attrGroupId

API description

获取属性分组(用于回显)

Request Parameters

parameter
type
description
required
attrGroupId
long
示例:1
required

Response Parameters

parameter
type
description
required
msg
string
示例:success
required
attrGroup
object
数据字典
required
attrGroupId
int
示例:1
required
attrGroupName
string
示例:主体
required
sort
int
示例:0
required
descript
string
示例:主体
required
icon
string
示例:dd
required
catelogId
int
示例:225
required
catelogPath
array
数据列表
required
code
int
示例:0
required

Description or Example

# Error提示 - `toArray(T[])`方法非常的坑, 里面的泛型确实一个数组, 但是这个数组必须要声明长度, 长度是任意的 # 核心代码 ```java @RequestMapping("/info/{attrGroupId}") // @RequiresPermissions("product:attrgroup:info") public R info(@PathVariable("attrGroupId") Long attrGroupId){ AttrGroupEntity attrGroup = attrGroupService.getGroupDetailById(attrGroupId); return R.ok().put("attrGroup", attrGroup); } ``` ```java public AttrGroupRespVO getGroupDetailById(Long attrGroupId) { AttrGroupRespVO groupVO = new AttrGroupRespVO(); // 创建一个集合, 存储分类路径 ArrayDeque<Long> path = new ArrayDeque<>(); // 将属性分组的基本参数查询出来 AttrGroupEntity attrGroupEntity = this.baseMapper.selectById(attrGroupId); // 将分类路径查询出来 categoryService.getGroupCategoryPath(attrGroupEntity.getCatelogId(), path); // 注入视图对象 BeanUtils.copyProperties(attrGroupEntity, groupVO); // 返回结果 return groupVO.setCatelogPath(path.toArray(new Long[0])); } ``` ```java // CategoryServiceImpl.java public void getGroupCategoryPath(Long catelogId, ArrayDeque<Long> path) { // 运用贪心的思想, 如果存在catalogId, 那么一定是路径上的一部分, 可以继续去找父id path.push(catelogId); Long parentCid = this.getById(catelogId).getParentCid(); if (parentCid != 0L) { getGroupCategoryPath(parentCid, path); } } ``` # 核心要点 ## 为什么写了一个递归去获取分类路径? > 因为这样才能让`el-cascader`组件被成功的渲染 ## 如何收集路径? > 收集路径的使用了栈和递归, 因为递归只能从叶子节点开始, 为了从根节点开始, 因此用栈, 通过递归查找父节点 > **这里其实是可以优化的, 没有必要使用递归, 使用循环即可, 这里浪费资源了** ## 特别注意 > 获取路径的方法必须要对应操作的表的service上