第四章:商品服务——平台属性
一、属性分组
1.前端的引入
1.将前端的代码总结复制进入VScode
2.因为controller的@RequestMapping("/list/tree")封装的数据是date,所以需要将前端的代码的data.data改为data.date
3.说明:
attrgroup为整个页面的表单
category为左侧是树形菜单树
attrgroup-add-or-update为新增或修改分组信息
attr-group-relation为关联
2.后端新增方法
1.添加获取分组属性的方法
1)为AttrGroupController添加获得列表,按id获得分组属性
即点击左侧的三级菜单,右侧就会获得对应属性
/**
* 列表
*/
@RequestMapping("/list/{catelogId}")
public R list(@RequestParam Map<String, Object> params,
@PathVariable("catelogId") Long catelogId){
// PageUtils page = attrGroupService.queryPage(params);
PageUtils page = attrGroupService.queryPage(params, catelogId);
return R.ok().put("page", page);
}
2)添加实现类方法
@Override
public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
if (catelogId == 0) {
IPage<AttrGroupEntity> page = this.page(
new Query<AttrGroupEntity>().getPage(params),
new QueryWrapper<AttrGroupEntity>()
);
return new PageUtils(page);
} else {
String key = (String) params.get("key");
QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId);
if (!StringUtils.isEmpty(key)) {
wrapper.and((obj) -> {
obj.eq("attr_group_id", key)
.or()
.like("attr_group_name", key);
});
}
IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),wrapper);
return new PageUtils(page);
}
}
2.添加分组的新增方法
1)新增所属分类时,要让第三集分类不包含子分类,所以给CategoryEntity,添加一个子分类分组,当他为空时不传json
//子分类
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@TableField(exist = false)
private List<CategoryEntity> children;
2)为AttrGroupController添加传入数据的控制器
@Resource
private CategoryService categoryService;
/**
* 信息
*/
@RequestMapping("/info/{attrGroupId}")
public R info(@PathVariable("attrGroupId") Long attrGroupId){
AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
Long catelogId = attrGroup.getCatelogId();
Long[] path = categoryService.findCategoryPath(catelogId);
attrGroup.setCatelogPath(path);
return R.ok().put("attrGroup", attrGroup);
}
3)为AttrGroupEntity添加仓储路径
@TableField(exist = false)
private Long[] catelogPath;
4)添加实现类
//找到cateLogId的完整路径
@Override
public Long[] findCategoryPath(Long catelogId) {
List<Long> paths = new ArrayList<>();
List<Long> parentPath = findParentPath(catelogId, paths);
Collections.reverse(parentPath);
return parentPath.toArray(new Long[parentPath.size()]);
}
//225,25,2
private List<Long> findParentPath(Long catelogId, List<Long> paths) {
//1、收集当前节点id
paths.add(catelogId);
CategoryEntity byId = this.getById(catelogId);
if (byId.getParentCid() != 0) {
findParentPath(byId.getParentCid(), paths);
}
return paths;
}
3.测试
二、联级更新
1.添加分页
注意:添加事件控制
@Configuration
@EnableTransactionManagement
@MapperScan("con.yangyan.gulimall.product.dao")
public class MyBatisConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(D