由于销售属性和基本属性(规格参数)都是存在pms_attr表里的,所以获取销售属性的方法可以借用之前写好的获取基本属性的方法:
@Override
public PageUtils queryBaseAttrPage(Map<String, Object> params, Long catelogId, String attrType) {
QueryWrapper<AttrEntity> queryWrapper = //attrType是"base",则查"attr_type"字段=1的否则查"attr_type"字段=0的;
new QueryWrapper<AttrEntity>().eq("attr_type", "base".equalsIgnoreCase(attrType) ? 1 : 0);
if (catelogId != 0) {
queryWrapper.eq("catelog_id", catelogId);
}
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);
List<AttrEntity> records = page.getRecords();
List<AttrRespVo> respVoList = records.stream().map((attrEntity) -> {
AttrRespVo attrRespVo = new AttrRespVo();
BeanUtils.copyProperties(attrEntity, attrRespVo);
//设置分类和分组的名字
AttrAttrgroupRelationEntity relationEntity = relationDao.selectOne(
new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId())
);
if (relationEntity != null) {
AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(relationEntity.getAttrGroupId());
attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
}
Í
CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId());
if (categoryEntity != null) {
attrRespVo.setCatelogName(categoryEntity.getName());
}
return attrRespVo;
}).collect(Collectors.toList());
pageUtils.setList(respVoList);
return pageUtils;
}
由于只有基本属性才会有属性分组,销售属性不存在分组,所以在查询的时候执行到
AttrAttrgroupRelationEntity relationEntity = relationDao.selectOne(
new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId())
);
所以要在这里进行判断是基本属性还是销售属性,如果是销售属性就不用查分组名字了
if ("base".equalsIgnoreCase(attrType)) {
AttrAttrgroupRelationEntity relationEntity = relationDao.selectOne(
new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId())
);
if (relationEntity != null) {
AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(relationEntity.getAttrGroupId());
attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
}
}
同理,由于保存属性和修改属性时,销售属性和基本属性都是公用相同的接口,而又由于基本属性有分组会对属性分组那张表有级联操作,所以同样需要进行判断
由于经常需要做这种判断,我们可以在common项目中写一个常量。常量放在common项目的constant包下,和商品有关的常量写在ProductConstant.class中
package xyz.guawaz.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;
}
}
}
然后在需要判断是销售属性还是基本属性事时使用常量