将品牌分类和品牌名称的关系放在pms_category_brand_relation表中
获取该列表品牌所有的关联信息
/**
* 列表
*/
@GetMapping("/catelog/list")
public R list(@RequestParam("brandId") Long brandId){
List<CategoryBrandRelationEntity> data=categoryBrandRelationService.list(
new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
return R.ok().put("data", data);
}
新增
/**
* 保存
*/
@RequestMapping("/save")
public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
//新增时,将名称也新增到数据库中
categoryBrandRelationService.saveDetail(categoryBrandRelation);
return R.ok();
}
创建saveDetail方法
@Override
public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
//先查数据库将品牌name和分类name查询出
BrandEntity brandEntity = brandDao.selectById(categoryBrandRelation.getBrandId());
CategoryEntity categoryEntity = categoryDao.selectById(categoryBrandRelation.getCatelogId());
categoryBrandRelation.setBrandName(brandEntity.getName());
categoryBrandRelation.setCatelogName(categoryEntity.getName());
this.save(categoryBrandRelation);
}
实现效果:
级联更新
当品牌名称修改后,品牌和分类的关联表中的品牌名称也应该修改
在brandController中修改原始的update方法
/**
* 修改
*/
@RequestMapping("/update")
public R update(@Validated({UpdateGroup.class})@RequestBody BrandEntity brand){
// brandService.updateById(brand);
brandService.updateDetail(brand);
return R.ok();
}
创建一个updateDetail方法
//级联更新
@Override
public void updateDetail(BrandEntity brand) {
this.updateById(brand);
Long brandId = brand.getBrandId();
String name = brand.getName();
if (!StringUtils.isEmpty(name)){
//修改关联表中的数据
categoryBrandRelationService.updateBrand(brandId,name);
}
//TODO 修改其他关联表中的数据
}
创建要给updateBrand方法用来更新关联表中的数据
在CategoryBrandRelationServiceImpl中
@Override
public void updateBrand(Long brandId, String name) {
QueryWrapper<CategoryBrandRelationEntity> queryWrapper=new QueryWrapper<>();
queryWrapper.eq("brand_id",brandId);
CategoryBrandRelationEntity categoryBrandRelationEntity=new CategoryBrandRelationEntity();
categoryBrandRelationEntity.setBrandId(brandId);
categoryBrandRelationEntity.setBrandName(name);
this.update(categoryBrandRelationEntity,queryWrapper);
}
实现效果:
同理:
当更新分类名后,关联表中的分类名称也应该修改
这里使用mybatis的方式写sql语句
在CategoryServiceImple中
@Override
public void updateCascade(CategoryEntity category) {
//修改分类表
this.updateById(category);
//修改关联表
categoryBrandRelationService.updateCategory(category.getCatId(),category.getName());
}
创建updateCategory方法
@Override
public void updateCategory(Long catId, String name) {
//实现修改关联表
this.baseMapper.updateCategory(catId,name);
}
使用sql语句的方式
void updateCategory(@Param("catId") Long catId, @Param("name") String name);
<update id="updateCategory">
UPDATE `pms_category_brand_relation` set catelog_name=#{name} where catelog_id=#{catId}
</update>
实现效果:
问题:在属性分组中如果不点击节点,直接查询的话,不能进行模糊查询
修改代码: AttrGroupServiceImpl中
@Override
public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
QueryWrapper<AttrGroupEntity> queryWrapper=new QueryWrapper<>();
String key = (String)params.get("key");
if (catelogId==0){
//如果没有点击节点
//查询全部--并且可以模糊查询
if (!StringUtils.isEmpty(key)){
queryWrapper.and((obj)->{
obj.like("attr_group_id",key).or().like("attr_group_name",key);
});
}
IPage<AttrGroupEntity> page = this.page(
new Query<AttrGroupEntity>().getPage(params),
queryWrapper
);
return new PageUtils(page);
}
else{
//构造查询条件
queryWrapper.eq("catelog_id",catelogId);
if (!StringUtils.isEmpty(key)){
queryWrapper.and((obj)->{
obj.like("attr_group_id",key).or().like("attr_group_name",key);
});
}
//还需要模糊查询
IPage<AttrGroupEntity> page=this.page(
new Query<AttrGroupEntity>().getPage(params),
queryWrapper
);
return new PageUtils(page);
}
}
新增规格参数
将新增的参数添加到attr表中,并且实现关联表attr_attrgroup_relation表的添加
提交的数据
而AttrEntity中没有分组id
之前我们是在AttrEntity中新增一个属性,这样不规范
我们创建一个vo包,在vo包下新建一个实体类AttrVo
package com.atguigu.gulimall.product.vo;
import lombok.Data;
/**
* @ClassName AttrVo
* @Description TODO
* @Author ylh
* @Date 2022/9/12 17:39
* @Description
*/
@Data
public class AttrVo {
/**
* 属性id
*/
private Long attrId;
/**
* 属性名
*/
private String attrName;
/**
* 是否需要检索[0-不需要,1-需要]
*/
private Integer searchType;
/**
* 属性图标
*/
private String icon;
/**
* 可选值列表[用逗号分隔]
*/
private String valueSelect;
/**
* 属性类型[0-销售属性,1-基本属性,2-既是销售属性又是基本属性]
*/
private Integer attrType;
/**
* 启用状态[0 - 禁用,1 - 启用]
*/
private Long enable;
/**
* 所属分类
*/
private Long catelogId;
/**
* 快速展示【是否展示在介绍上;0-否 1-是】,在sku中仍然可以调整
*/
private Integer showDesc;
private Long attrGroup;
}
在attrController中
@RequestMapping("/save")
public R save(@RequestBody AttrVo attrVo){
attrService.saveAttr(attrVo);
return R.ok();
}
编写一个saveAttr方法
@Override
public void saveAttr(AttrVo attrVo) {
//保存attr表中的数据
AttrEntity attr=new AttrEntity();
BeanUtils.copyProperties(attrVo,attr);
this.save(attr);
//对attr_attrgroup_relation表进行关联
AttrAttrgroupRelationEntity relationEntity=new AttrAttrgroupRelationEntity();
relationEntity.setAttrId(attr.getAttrId());
relationEntity.setAttrGroupId(attrVo.getAttrGroupId());
attrAttrgroupRelationDao.insert(relationEntity);
psm_attr表中
attr_attrgroup_relation表中
查询规格参数列表功能
@Override
public PageUtils queryPageDetail(Map<String, Object> params, Long categoryId) {
QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<>();
String key = (String)params.get("key");
if (StringUtils.isEmpty(key)){
queryWrapper.eq("attr_id",key).or().like("attr_name",key);
}
if (categoryId!=0){
//根据id查询
queryWrapper.and((wrapper)->{
wrapper.eq("catelod_id",categoryId);
});
}
IPage<AttrEntity> page= this.page(
new Query<AttrEntity>().getPage(params),
queryWrapper
);
return new PageUtils(page);
}
我们还需要将所属分类和所属分组给展示出来
需要创建一个vo类
@Data
public class AttrRepVo extends AttrEntity {
//所属分类名称
private String catelogName;
//所属分组名称
private String groupName;
}
@Override
public PageUtils queryPageDetail(Map<String, Object> params, Long catelogId) {
QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<>();
String key = (String)params.get("key");
if (catelogId!=0){
//根据id查询
queryWrapper.eq("catelog_id",catelogId);
}
if (StringUtils.isEmpty(key)){
queryWrapper.and((wrapper)->{
wrapper.eq("attr_id",key).or().like("attr_name",key);
});
}
IPage<AttrEntity> page = this.page(
new Query<AttrEntity>().getPage(params),
queryWrapper
);
PageUtils pageUtils = new PageUtils(page);
List<AttrEntity> records = page.getRecords();
List<AttrRepVo> respVos = records.stream().map((attrEntity -> {
AttrRepVo attrRepVo = new AttrRepVo();
BeanUtils.copyProperties(attrEntity, attrRepVo);
//设置分类和分组
//查询分组
AttrAttrgroupRelationEntity attrId = attrAttrgroupRelationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId()));
if (attrId != null) {
AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrId.getAttrGroupId());
attrRepVo.setGroupName(attrGroupEntity.getAttrGroupName());
}
//查询分类
CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId());
if (categoryEntity != null) {
attrRepVo.setCatelogName(categoryEntity.getName());
}
return attrRepVo;
})).collect(Collectors.toList());
pageUtils.setList(respVos);
return pageUtils;
}
}
最终效果
规格修改
一、回显数据
二、修改
销售属性维护
查询分组关联属性
在AttrGroupController中
// http://localhost:88/api/product/attrgroup/1/attr/relation?t=1663076321684
@GetMapping("/{attrgroupId}/attr/relation")
public R attrRelation(@PathVariable("attrgroupId")Long attrgroupId){
//跟据id查询所有分组信息
List<AttrEntity> entities=attrService.getRelationList(attrgroupId);
return R.ok().put("data",entities);
}
创建getRelationList(attrgroupId)方法
@Override
public List<AttrEntity> getRelationList(Long attrgroupId) {
List<AttrAttrgroupRelationEntity> attr_group_id = attrAttrgroupRelationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrgroupId));
List<Long> collect = attr_group_id.stream().map((attr) -> {
Long attrId = attr.getAttrId();
return attrId;
}).collect(Collectors.toList());
Collection<AttrEntity> attrEntities = this.listByIds(collect);
return (List<AttrEntity>) attrEntities;
}
实现效果:
删除分组关联
创建接受对象
@Data
public class AttrDeleteVo {
private Long attrId;
private Long attrGroupId;
}
在AttrGroupController中
///product/attrgroup/attr/relation/delet
@PostMapping ("/attr/relation/delete")
public R deleteRelationGroup(@RequestBody AttrDeleteVo[] attrDeleteVo){
attrAttrgroupRelationService.deleteBatchRelation(attrDeleteVo);
return R.ok();
}
创建方法deleteBatchRelation(attrDeleteVo);
@Override
public void deleteBatchRelation(AttrDeleteVo[] attrDeleteVo) {
//将AttrDeleteVo[]类型的数据转换为AttrAttrgroupRelationEntity 类型的集合
List<AttrDeleteVo> attrDeleteVos = Arrays.asList(attrDeleteVo);
List<AttrAttrgroupRelationEntity> collect = attrDeleteVos.stream().map((item) -> {
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
BeanUtils.copyProperties(item, relationEntity);
return relationEntity;
}).collect(Collectors.toList());
attrAttrgroupRelationDao.deleteBatchRelation(collect);
}
void deleteBatchRelation(@Param("collect") List<AttrAttrgroupRelationEntity> collect);
sql语句
<delete id="deleteBatchRelation">
DELETE FROM `pms_attr_attrgroup_relation` WHERE
<foreach collection="collect" item="item" separator=" OR ">
(attr_id=#{item.attrId} AND attr_group_id=#{item.attrGroupId})
</foreach>
</delete>
新建分组关联属性
一、查询所有可选属性
在attrGroupController中
//Request URL: http://localhost:88/api/product/attrgroup/1/noattr/relation?t=1663139281168&page=1&limit=10&key=
@GetMapping("/{attrgroupId}/noattr/relation")
public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,@RequestParam Map<String,Object> params){
PageUtils page=attrService.getNoRelationAttr(params,attrgroupId);
return R.ok().put("page",page);
}
创建一个getNoRelationAttr(Map<String, Object> params, Long attrgroupId) 方法
@Override
public PageUtils getNoRelationAttr(Map<String, Object> params, Long attrgroupId) {
//获取当前分组还没有关联的属性
//1、要这些属性要和该分组是一个分类下
//2、这些属性没有被其他分组和自己关联
AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrgroupId);
Long catelogId = attrGroupEntity.getCatelogId();
//查询其他分组--得到他们的id
List<AttrGroupEntity> attrGroupEntities = attrGroupDao.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
List<Long> collect = attrGroupEntities.stream().map((item) -> {
return item.getAttrGroupId();
}).collect(Collectors.toList());
//分组关系表中被关联的attr
List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = attrAttrgroupRelationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().in("attr_group_id", collect));
List<Long> collect1 = attrAttrgroupRelationEntities.stream().map((item) -> {
return item.getAttrId();
}).collect(Collectors.toList());
QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper();
//从attr表中剔除这些被关联过的attr
this.list(queryWrapper.eq("catelog_id", catelogId).eq("attr_type",ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()));
if (collect1 != null && collect1.size() != 0) {
queryWrapper.notIn("attr_id", collect1);
}
String key = (String) params.get("key");
if (!StringUtils.isEmpty(key)) {
queryWrapper.and((wapper) -> {
wapper.like("attr_name", key).or().eq("attr_id", "key");
});
}
IPage page = this.page(new Query<AttrEntity>().getPage(params), queryWrapper);
return new PageUtils(page);
}
二、确认新增操作
AttrGroupController中
//Request URL: http://localhost:88/api/product/attrgroup/attr/relation
@PostMapping("/attr/relation")
public R saveAttr(@RequestBody List<AttrRepVo>attrRepVo){
attrAttrgroupRelationService.saveAttrRelation(attrRepVo);
return R.ok();
}
创建saveAttrRelation方法
@Override
public void saveAttrRelation(List<AttrRepVo> attrRepVo) {
//新增
List<AttrAttrgroupRelationEntity> collect = attrRepVo.stream().map((item) -> {
AttrAttrgroupRelationEntity attrAttrgroupRelation = new AttrAttrgroupRelationEntity();
BeanUtils.copyProperties(item, attrAttrgroupRelation);
return attrAttrgroupRelation;
}).collect(Collectors.toList());
this.saveBatch(collect);
}
修改两个bug
当新增规格参数时,所属分组为空点击新增后报错:
在查询基本属性时,添加一个判断
在新增时(saveAttr)也要添加一个判断