gulimall-分布式基础篇-销售属性
接口文档不能看出规格参数与销售属性仅仅是请求路径上的差异,还有销售属性没有属性分组,所以我们要重写这里的规格参数的函数
一、新建常量数据
在Common中新建类Constant/ProductConstant,用来商品服务中的保存常量
package com.jyyy.gulimall.common.constant;
public class ProductConstant {
public enum AttrEnum{
ATTR_TYPE_BASE(1,"基本属性"),ATTR_TYPE_SALE(0,"销售属性");
private int code;
private String msg;
AttrEnum(int code,String msg){
this.code = code;
this.msg = msg;
}
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
}
二、获取销售属性分页功能
2.1 AttrController
@RequestMapping("/{attrType}/list/{catelogId}")
public R baseList(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId,
@PathVariable("attrType") String attrType){
PageUtils page = attrService.queryBaseAttrPage(params, catelogId, attrType);
return R.ok().put("page", page);
}
2.2 AttrServiceImpl
/**
* 根据查询条件分页
* @param params
* @param catelogId
* @param attrType
* @return
*/
public PageUtils queryBaseAttrPage(Map<String, Object> params, Long catelogId, String attrType) {
// 根据规格参数或者销售属性获取不同的数据
QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<AttrEntity>().eq("attr_type",
"base".equalsIgnoreCase(attrType)?ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode():ProductConstant.AttrEnum.ATTR_TYPE_SALE.getCode());
// id不等于0的情况下
if (catelogId != 0) {
queryWrapper.eq("catelog_id",catelogId);
}
// key不为空的情况下
String key = (String) params.get("key");
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);
// 查询RespVo里面新增的属性
List<AttrEntity> records = page.getRecords();
List<AttrRespVo> respVoList = 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());
}
// 销售属性没有属性分组关联数据
if ("base".equalsIgnoreCase(attrType)) {
// 获取属性分组关联数据
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(respVoList);
return pageUtils;
}
三、销售属性新增功能
3.1 AttrServiceImpl
修改规格属性新增
/**
* 保存规格参数同时保存关联关系
* @param attr
*/
@Transactional
public void saveAttr(AttrVo attr) {
AttrEntity attrEntity = new AttrEntity();
// 保存规格参数
BeanUtils.copyProperties(attr,attrEntity);
this.save(attrEntity);
// 保存关联关系
if (attr.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode() &&
attr.getAttrGroupId() != null) {
AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
attrAttrgroupRelationEntity.setAttrId(attrEntity.getAttrId());
attrAttrgroupRelationEntity.setAttrGroupId(attr.getAttrGroupId());
attrAttrgroupRelationDao.insert(attrAttrgroupRelationEntity);
}
}
四、获取销售属性详细信息
4.1 AttrServiceImpl
/**
* 获取属性详细信息
* @param attrId
* @return
*/
public AttrRespVo getAttrInfo(Long attrId) {
AttrRespVo attrRespVo = new AttrRespVo();
AttrEntity attrEntity = this.getById(attrId);
BeanUtils.copyProperties(attrEntity,attrRespVo);
//1.设置属性分组信息
if (attrEntity.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()) {
AttrAttrgroupRelationEntity attrgroupRelation = attrAttrgroupRelationDao.selectOne(
new QueryWrapper<AttrAttrgroupRelationEntity>()
.eq("attr_id", attrEntity.getAttrId()));
if (attrgroupRelation != null) {
attrRespVo.setAttrGroupId(attrgroupRelation.getAttrGroupId());
AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrgroupRelation.getAttrGroupId());
if (attrGroupEntity != null) {
attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
}
}
}
// 2. 设置分类信息
CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId());
if (categoryEntity != null) {
attrRespVo.setCatelogName(categoryEntity.getName());
}
// 3.设置三级分类路径
Long[] catelogPath = categoryService.findCatelogPath(attrEntity.getCatelogId());
attrRespVo.setCatelogPath(catelogPath);
return attrRespVo;
}
五、更新销售属性
5.1 AttrServiceImpl
/**
* 修改基础属性信息
* @param attr
*/
@Transactional
public void updateAttr(AttrVo attr) {
AttrEntity attrEntity = new AttrEntity();
BeanUtils.copyProperties(attr,attrEntity);
this.updateById(attrEntity);
if(attrEntity.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()){
// 修改属性分组关联表
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
relationEntity.setAttrId(attr.getAttrId());
relationEntity.setAttrGroupId(attr.getAttrGroupId());
// 判断是否存在属性分组关联表
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);
}
}
}