查询规格参数列表
这个功能用于展示平台属性的规格参数,对应的API文档如下:
05、获取分类规格参数
GET /product/attr/base/list/{catelogId}
请求参数
{
page: 1,//当前页码
limit: 10,//每页记录数
sidx: 'id',//排序字段
order: 'asc/desc',//排序方式
key: '华为'//检索关键字
}
分页数据
响应数据
{
"msg": "success",
"code": 0,
"page": {
"totalCount": 0,
"pageSize": 10,
"totalPage": 0,
"currPage": 1,
"list": [{
"attrId": 0, //属性id
"attrName": "string", //属性名
"attrType": 0, //属性类型,0-销售属性,1-基本属性
"catelogName": "手机/数码/手机", //所属分类名字
"groupName": "主体", //所属分组名字
"enable": 0, //是否启用
"icon": "string", //图标
"searchType": 0,//是否需要检索[0-不需要,1-需要]
"showDesc": 0,//是否展示在介绍上;0-否 1-是
"valueSelect": "string",//可选值列表[用逗号分隔]
"valueType": 0//值类型[0-为单个值,1-可以选择多个值]
}]
}
}
首先创建对应GET /product/attr/base/list/{catelogId}请求的响应:
@GetMapping("/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);
}
传入基本的Map作为对象,同时接受catelogId作为参数,之后通过实现方法queryBaseAttrPage,来返回需要的查询内容,之后返回。
快捷键创建方法并实现queryBaseAttrPage。
package com.lastingwar.mall.product.service.impl;
@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {
@Autowired
AttrAttrgroupRelationDao relationDao;
@Transactional
@Override
public void saveAttr(AttrVo attr) {
//拷贝传入的vo数据,保存进实体类
AttrEntity attrEntity = new AttrEntity();
BeanUtils.copyProperties(attr,attrEntity);
//完成保存基础数据的功能
this.save(attrEntity);
//保存关联信息
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
relationEntity.setAttrGroupId(attr.getAttrGroupId());
//由于ID是自动生成的,这里使用传入的vo是没有值的
relationEntity.setAttrId(attrEntity.getAttrId());
relationDao.insert(relationEntity);
}
@Override
public PageUtils queryBaseAttrPage(Map<String, Object> params, Long catelogId) {
//重写QueryWrapper,进行不同的检索
QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<>();
//如果请求的有catelogId,搜索catelogId相同的行
if (catelogId!=0){
queryWrapper.eq("catelog_id",catelogId);
}
//如果有关键字的模糊查询,添加进queryWrapper
String key = (String) params.get("key");
if (StringUtils.isNotEmpty(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
queryWrapper
);
return new PageUtils(page);
}
}
之后重启服务,能够在人人平台上面看到有规格参数的显示:
之后根据API文档添加分类名称和分组名称:
这里可以创建一个AttrVO的子类来添加上这两个参数:
package com.lastingwar.mall.product.vo;
import lombok.Data;
@Data
public class AttrRespVo extends AttrVo {
/**
* "catelogName": "手机/数码/手机", //所属分类名字
* "groupName": "主体", //所属分组名字
*/
private String catelogName;
private String groupName;
}
之后对服务层PageUtils(page)进行内容的添加,修改添加服务层的方法:
package com.lastingwar.mall.product.service.impl;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.lastingwar.common.utils.R;
import com.lastingwar.mall.product.dao.AttrAttrgroupRelationDao;
import com.lastingwar.mall.product.dao.AttrGroupDao;
import com.lastingwar.mall.product.dao.CategoryDao;
import com.lastingwar.mall.product.entity.AttrAttrgroupRelationEntity;
import com.lastingwar.mall.product.entity.AttrGroupEntity;
import com.lastingwar.mall.product.entity.CategoryEntity;
import com.lastingwar.mall.product.vo.AttrRespVo;
import com.lastingwar.mall.product.vo.AttrVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lastingwar.common.utils.PageUtils;
import com.lastingwar.common.utils.Query;
import com.lastingwar.mall.product.dao.AttrDao;
import com.lastingwar.mall.product.entity.AttrEntity;
import com.lastingwar.mall.product.service.AttrService;
import org.springframework.transaction.annotation.Transactional;
@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {
@Autowired
AttrAttrgroupRelationDao relationDao;
@Autowired
AttrGroupDao attrGroupDao;
@Autowired
CategoryDao categoryDao;
@Override
public PageUtils queryPage(Map<String, Object> params) {
IPage<AttrEntity> page = this.page(
new Query<AttrEntity>().getPage(params),
new QueryWrapper<AttrEntity>()
);
return new PageUtils(page);
}
/**
* 将属性分组新增信息保存进属性的表中,同时保存AttrAttrgroupRelation标准对应的ID
* @param attr 属性分组新增信息
*/
@Transactional
@Override
public void saveAttr(AttrVo attr) {
//拷贝传入的vo数据,保存进实体类
AttrEntity attrEntity = new AttrEntity();
BeanUtils.copyProperties(attr,attrEntity);
//完成保存基础数据的功能
this.save(attrEntity);
//保存关联信息
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
relationEntity.setAttrGroupId(attr.getAttrGroupId());
//由于ID是自动生成的,这里使用传入的vo是没有值的
relationEntity.setAttrId(attrEntity.getAttrId());
relationDao.insert(relationEntity);
}
/**
* 通过分类属性ID查找分类名称和组名称添加到返回中,
* @param params 基础的返回信息
* @param catelogId 分类属性ID
* @return AttrRespVo
*/
@Override
public PageUtils queryBaseAttrPage(Map<String, Object> params, Long catelogId) {
//重写QueryWrapper,进行不同的检索
QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<>();
//如果请求的有catelogId,搜索catelogId相同的行
if (catelogId!=0){
queryWrapper.eq("catelog_id",catelogId);
}
//如果有关键字的模糊查询,添加进queryWrapper
String key = (String) params.get("key");
if (StringUtils.isNotEmpty(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
queryWrapper
);
//需要进行内容的添加
PageUtils pageUtils = new PageUtils(page);
//拆开成行信息的形式
List<AttrEntity> records = page.getRecords();
//收集流处理后信息
List<AttrRespVo> respVos = records.stream().map((attrEntity) -> {
//使用流,对每一个对象进行内容的添加,添加成为attrRespVo
AttrRespVo attrRespVo = new AttrRespVo();
//将原来的信息拷贝过来
BeanUtils.copyProperties(attrEntity, attrRespVo);
//查询需要添加的两条信息,并添加进AttrRespVo中
//使用attrId查询关联表获得AttrGroupId,之后根据分组ID获取分组名称
AttrAttrgroupRelationEntity attrId = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>()
.eq("attr_id", attrEntity.getAttrId()));
if (attrId != null) {
AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrId.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(respVos);
return pageUtils;
}
}
之后重启服务,来到前台,能看到正确显示分类名称和分组名称: