gulimall-分布式基础篇-规格参数
规则参数用一个冗余表跟属性分组关联
接口里的base是基本属性的意思
一、规格参数新增
规格参数新增的同时新增规格参数与新增关联关系
1.1 attrVo
在product模块下创建VO包,再创建attrVo
package com.jyyy.gulimall.product.vo;
import com.jyyy.gulimall.product.entity.AttrEntity;
import lombok.Data;
import lombok.ToString;
@Data
@ToString(callSuper = true)
public class attrVo extends AttrEntity {
private Long attrGroupId;
}
1.2 attrController
/**
* 保存
*/
@RequestMapping("/save")
// @RequiresPermissions("product:attr:save")
public R save(@RequestBody AttrVo attr){
attrService.saveAttr(attr);
return R.ok();
}
1.3 attrService
void saveAttr(AttrVo attr);
1.4 attrServiceImpl
@Autowired
private AttrAttrgroupRelationDao attrAttrgroupRelationDao;
/**
* 保存规格参数同时保存关联关系
* @param attr
*/
public void saveAttr(AttrVo attr) {
AttrEntity attrEntity = new AttrEntity();
// 保存规格参数
BeanUtils.copyProperties(attr,attrEntity);
this.save(attrEntity);
// 保存关联关系
AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
attrAttrgroupRelationEntity.setAttrId(attrEntity .getAttrId());
attrAttrgroupRelationEntity.setAttrGroupId(attr.getAttrGroupId());
attrAttrgroupRelationDao.insert(attrAttrgroupRelationEntity);
}
二、规格参数查询分页
2.1 新建AttrRespVo
@Data
public class AttrRespVo extends AttrVo {
private String catelogName;
private String groupName;
}
2.2 AttrController
@RequestMapping("/base/list/{catelogId}")
public R baseAttrList(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId){
PageUtils page = attrService.queryBaseAttrPage(params, catelogId);
return R.ok().put("page", page);
}
2.3 AttrServiceImpl
属性分组要是通过冗余表进行关联的,所以要在冗余表查到属性分组id再查名字
@Autowired
private CategoryDao categoryDao;
@Autowired
private AttrGroupDao attrGroupDao;
@Autowired
TODO 属性关联表
@Override
public PageUtils queryBaseAttrPage(Map<String, Object> params, Long catelogId) {
QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<>();
//模糊查询
if (catelogId != 0){
queryWrapper.eq("catelog_id", catelogId);
}
String key = (String) params.get("key");
if (!StringUtils.isEmpty(key)){
//attr_id attr_name
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<AttrRespVo> respVos = records.stream().map((attrEntity) -> {
AttrRespVo attrRespVo = new AttrRespVo();
BeanUtils.copyProperties(attrEntity, attrRespVo);
CategoryEntity categoryEntity = categoryDao.selectOne(new QueryWrapper<CategoryEntity>().eq("cat_id", attrEntity.getCatelogId()));
if (categoryEntity != null) {
attrRespVo.setCatelogName(categoryEntity.getName());
}
AttrAttrgroupRelationEntity attrgroupRelationEntity = attrAttrgroupRelationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId()));
if (attrgroupRelationEntity != null) {
AttrGroupEntity attrGroupEntity = attrGroupDao.selectOne(new QueryWrapper<AttrGroupEntity>().eq("attr_group_id", attrgroupRelationEntity.getAttrGroupId()));
attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
}
return attrRespVo;
}).collect(Collectors.toList());
// 把新的数据传送过去
pageUtils.setList(respVos);
return pageUtils;
}
三、查询属性详情
3.1 修改AttrRespVo
所属分类路径回显
package com.jyyy.gulimall.product.vo;
import lombok.Data;
import lombok.ToString;
@Data
@ToString(callSuper = true)
public class AttrRespVo extends AttrVo{
private String catelogName;
private String groupName;
private Long[] catelogPath;
}
3.2 AttrController
@RequestMapping("/info/{attrId}")
public R info(@PathVariable("attrId") Long attrId){
AttrRespVo attrRespVo = attrService.getAttrInfo(attrId);
return R.ok().put("attr", attrRespVo);
}
3.3 AttrServiceImpl
@Override
public AttrRespVo getAttrInfo(Long attrId) {
AttrRespVo respVo = new AttrRespVo();
AttrEntity attrEntity = this.getById(attrId);
BeanUtils.copyProperties(attrEntity,respVo);
//1.设置属性分组信息
AttrAttrgroupRelationEntity attrAttrgroupRelation = attrAttrgroupRelationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>()
.eq("attr_id", attrId));
if(attrAttrgroupRelation != null){
respVo.setAttrGroupId(attrAttrgroupRelation.getAttrGroupId());
AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrAttrgroupRelation.getAttrId());
if(attrGroupEntity!= null){
respVo.setGroupName(attrGroupEntity.getAttrGroupName());
}
}
//2.设置分类信息
Long catelogId = attrEntity.getCatelogId();
Long[] catelogPath = categoryService.findCatelogPath(catelogId);
respVo.setCatelogPath(catelogPath);
CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
if(categoryEntity!=null){
respVo.setCatelogName(categoryEntity.getName());
}
return respVo;
}
四、修改属性
4.1 在AttrController中我们进行新增相应的方法
@RequestMapping("/update")
public R update(@RequestBody AttrVo attr){
attrService.updateAttr(attr);
return R.ok();
}
4.2 AttrServiceImpl
//保存时,要修改两张表 attr这张表和attrattrgroupRelationEntity这两个表
@Transactional
@Override
public void updateAttr(AttrVo attr) {
AttrEntity attrEntity = new AttrEntity();
BeanUtils.copyProperties(attr, attrEntity);
this.updateById(attrEntity);
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
relationEntity.setAttrGroupId(attr.getAttrGroupId());
relationEntity.setAttrId(attr.getAttrId());
//判断是新增还是删除
Integer count = attrAttrgroupRelationDao.selectCount(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attr.getAttrId()));
if (count > 0){
attrAttrgroupRelationDao.update(relationEntity, new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attr.getAttrId()));
}else{
attrAttrgroupRelationDao.insert(relationEntity);
}
}