第二十一章第五节:销售属性维护功能(增删改查)
1:由于销售属性和规格参数的数据都保存在attr表中,字段attrtype为0表示是销售属性,为1表示是规格参数
2:销售属性和规格参数不同的是销售属性不需要保存与属性分组的关联关系
3:查询请求会根据路径类型type来区分查询的是销售属性和规格参数; 前端发送的base表示规则参数,sale表示时销售属性
基于以上几点,我们只需要修改规则参数的增删改查即可实现销售属性的数据管理
1、在公共项目中创建一个枚举类
0:销售属性
1:规格参数
com.applesnt.common.constant.ProductConstant
package com.applesnt.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>修改controller
com.applesnt.onlinemall.product.controller.AttrController
/**
* 列表分页查询
* 由于规格参数和销售属性用的是同一张表,所以curd基本一样
* 以attrType的值来区分是规格参数还是销售属性
* {attrType}:值为base为规格参数,sale为销售属性
*/
/*@RequestMapping("/base/list/{catId}")*/
@RequestMapping("/{attrType}/list/{catId}")
public R baseAttrList(@RequestParam Map<String, Object> params,
@PathVariable("catId") Long catId,
@PathVariable("attrType") String type){
PageUtils page = attrService.queryBaseAttrPage(params,catId,type);
return R.ok().put("page", page);
}
2>修改查询接口
com.applesnt.onlinemall.product.service.AttrService
PageUtils queryBaseAttrPage(Map<String, Object> params, Long catId,String type);
3>修改查询接口实现
com.applesnt.onlinemall.product.service.impl.AttrServiceImpl
@Override
public PageUtils queryBaseAttrPage(Map<String, Object> params, Long catId,String type) {
/*定义一个查询条件对象*/
QueryWrapper<AttrEntity> wrapper = new QueryWrapper<AttrEntity>();
//销售属性修改处
/*拼接查询类型字段,三元运算 数据库中attr_type字段1代表查询规格参数,1代表查询销售属性*/
wrapper.eq("attr_type","base".equalsIgnoreCase(type)?
ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode():
ProductConstant.AttrEnum.ATTR_TYPE_SALE.getCode());
/*获取key查询关键字*/
String key = (String) params.get("key");
/*判断 如果传递的分类id不等于0,就拼接分类id的查询条件*/
if(catId!=0){
wrapper.eq("catelog_id",catId);
}
/*如果key查询关键字不为空,就拼接key的查询条件*/
if(!StringUtils.isEmpty(key)){
wrapper.and((obj)->{
obj.eq("attr_id",key).or().like("attr_name",key);
});
}
/*执行查询 返回AttrEntity集合及分页信息*/
IPage<AttrEntity> page = this.page(
new Query<AttrEntity>().getPage(params), wrapper);
/*获取上一步查询的AttrEntity的列表记录*/
List<AttrEntity> attrEntityList =page.getRecords();
/*流式遍历attrEntityList列表记录,经过数据处理,收集成AttrRespVo集合*/
List<AttrRespVo> attrRespVos = attrEntityList.stream().map((attrEntity)->{
/*定义一个响应数据对象AttrRespVo*/
AttrRespVo respVo = new AttrRespVo();
/*字段值拷贝*/
BeanUtils.copyProperties(attrEntity,respVo);
//销售属性修改处
/*只有时属性分组才会设置分组信息,销售属性不需要*/
if("base".equalsIgnoreCase(type)){
/*因为需要把属性分组的名字放到AttrRespVo,但pms_attr中并没有保存属性分组的相关信息,所有需要通过attrid查询到 pms_attr_attrgroup_relation关联表的属性分组的id*/
AttrAttrgroupRelationEntity relationEntity = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().
eq("attr_id",attrEntity.getAttrId()));
if(relationEntity!=null && relationEntity.getAttrGroupId()!=null){
/*通过上一步得到的属性分组的id 查询整个属性分组对象*/
AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(relationEntity.getAttrGroupId());
/*把属性分组的id和名字放到响应vo中*/
respVo.setAttrGroupId(relationEntity.getAttrGroupId());
respVo.setGroupName(attrGroupEntity.getAttrGroupName());
}
}
/*通过分类id查询pms_category 得到实体对象*/
CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId());
if(categoryEntity!=null){
/*把分类名称放到响应vo中*/
respVo.setCatelogName(categoryEntity.getName());
}
return respVo;
}).collect(Collectors.toList());
PageUtils pageUtils = new PageUtils(page);
/*把查询到的分页对象中的list重新赋值,把AttrEntity集合替换成AttrRespVo集合*/
pageUtils.setList(attrRespVos);
return pageUtils;
}
3、销售属性新增功能(修改规格参数的新增接口实现即可)
com.applesnt.onlinemall.product.service.impl.AttrServiceImpl
@Transactional/*事务注解*/
@Override
public void saveAttr(AttrVo attr) {
/*创建数据库操作实体对象*/
AttrEntity attrEntity = new AttrEntity();
/*对象之间复制属性值*/
BeanUtils.copyProperties(attr,attrEntity);
/*保存*/
this.baseMapper.insert(attrEntity);
//销售属性修改处
/*规格参数和销售属性同样的保存方法,但只有规格参数会保存关联关系,
* arrttype=1是规格属性
* attrtype=0是销售属性
*/
//判断是规则参数时保存关联关系
if(attr.getAttrType()== ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode() && attr.getAttrGroupId()!=null){
/*保存关联关系,同时把属性分组和规格参数的关系保存到pms_attr_attrgroup_relation中*/
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
relationEntity.setAttrGroupId(attr.getAttrGroupId());
relationEntity.setAttrId(attrEntity.getAttrId());
relationDao.insert(relationEntity);
}
}
4、销售属性修改功能(修改规格参数的修改接口实现即可)
com.applesnt.onlinemall.product.service.impl.AttrServiceImpl
@Override
public AttrRespVo getAttrInfo(Long attrId) {
AttrRespVo respVo = new AttrRespVo();
/*根据attrId查询规格参数信息*/
AttrEntity attrEntity = this.baseMapper.selectById(attrId);
/*把基本信息拷贝到响应vo中*/
BeanUtils.copyProperties(attrEntity,respVo);
//销售属性修改处
/*
*销售属性不需要获取分组信息,
* arrttype=1是规格属性
* attrtype=0是销售属性
*/
if(attrEntity.getAttrType()==ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()){
/*设置分组信息*/
/*根据attrid查询规格参数及属性分组关联表*/
AttrAttrgroupRelationEntity relationEntity = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id",attrId));
if(relationEntity!=null){
respVo.setAttrGroupId(relationEntity.getAttrGroupId());
/*根据关联表中的属性分组id查询属性分组信息*/
AttrGroupEntity groupEntity = attrGroupDao.selectById(relationEntity.getAttrGroupId());
if(groupEntity!=null){
respVo.setGroupName(groupEntity.getAttrGroupName());
}
}
}
/*设置分类信息*/
Long[] categoryPath = this.categoryService.findCatelogPath(attrEntity.getCatelogId());
respVo.setCatelogPath(categoryPath);
CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId());
if(categoryEntity!=null){
respVo.setCatelogName(categoryEntity.getName());
}
return respVo;
}
@Transactional/*事务管理*/
@Override
public void updateAttr(AttrVo attr) {
/*规格参数基本信息修改*/
AttrEntity attrEntity = new AttrEntity();
BeanUtils.copyProperties(attr,attrEntity);
this.baseMapper.updateById(attrEntity);
//销售属性修改处
/*
*销售属性不需要修改分组信息,
* arrttype=1是规格属性
* attrtype=0是销售属性
*/
if(attrEntity.getAttrType()==ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()){
/*修改分组关联*/
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
relationEntity.setAttrGroupId(attr.getAttrGroupId());
relationDao.update(relationEntity,new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id",attr.getAttrId()));
}
}

浙公网安备 33010602011771号