携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第18天,点击查看活动详情
开发流程如下: 开发需求,就像在线接口文档如下https://easydoc.net/s/78237135/ZUqEdvA4/OXTgKobR这里一样, 别人告诉你需要什么功能,需要返回什么样的数据,你就通过接口的形式把他们呢实现出来即可!
这一部分都是CRUD相关的代码,所以要好好练好好写!!!
开发接口就是开发Controller、service、dao
以后工作了也是这种形式,主要是开发接口为多,前端其实不用写太多,能看懂即可!!!
下面为显示销售属性详情的接口编写
销售属性详情
显示销售属性
如图http://localhost:88/api/product/attr/sale/list/0?t=1660181297434&page=1&limit=10&key=这个接口有问题!
所以我们就去后端改这个接口即可!
controller
规格参数和销售参数的区别在于type的值,
type为 1是规格参数,type为0是销售参数销售属性和基本属性的区别就在于value_type的类型不同!!!
销售属性为1,基本属性为2
其实知道这两点就可以写好接口
这里采用一个方法当两个来用!
@GetMapping("/{attrType}/list/{catelogId}")
public R baseAttrList(@RequestParam Map<String, Object> params,
@PathVariable("attrType") String type,
@PathVariable("catelogId") Integer catelogId) {
PageUtils page = attrService.queryBaseAttrPage(params, type, catelogId);
return R.ok().put("page", page);
}
service
在原来对规格参数的基础上加了限制条件,如果是规格参数那就是WHERE attr_type = 1,否则就是WHERE attr_type = 0;
下面的逻辑和查询规格参数一致,都要模糊查询
这里为了使代码更通用,1和0的值我们写一个常量来控制,如过后期换值了我们直接更改常量的值即可
ProductConstant
package com.caq.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;
}
}
}
在原来对规格参数的基础上加了限制条件,如果是规格参数那就是WHERE attr_type = 1,否则就是WHERE attr_type = 0;
注意看注释
@Override
public PageUtils queryBaseAttrPage(Map<String, Object> params, String type, Integer catelogId) {
QueryWrapper<AttrEntity> wrapper = new QueryWrapper<AttrEntity>()
.eq("attr_type", "base".equalsIgnoreCase(type) ? ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode() : ProductConstant.AttrEnum.ATTR_TYPE_SALE.getCode());
if (catelogId != 0) {
//如果不是一级分类,那么查询的时候加上where catelog_id = ?
//IgnoreCase忽略大小写
wrapper.eq("catelog_id", catelogId);
}
//多条件模糊查询
//搜索框里的key不但可以对catelog_id进行模糊查询,对attr_name也模糊查询
String key = (String) params.get("key");
if (!StringUtils.isEmpty(key)) {
wrapper.eq("attr_id", key).or().like("attr_name", key);
}
//多条件分页查询
IPage<AttrEntity> page = this.page(
new Query<AttrEntity>().getPage(params),
wrapper);
PageUtils pageUtils = new PageUtils(page);
List<AttrEntity> list = page.getRecords();
// .map()这个方法是对被筛选过后的流进行映射,一般是对属性进行赋值。
List<AttrRespVo> resultList = list.stream().map(item -> {
AttrRespVo attrRespvo = new AttrRespVo();
BeanUtils.copyProperties(item, attrRespvo);
AttrAttrgroupRelationEntity attrgroupRelationEntity = relationService.
getOne(new QueryWrapper<AttrAttrgroupRelationEntity>()
.eq("attr_id", item.getAttrId()));
if (attrgroupRelationEntity != null) {
//attrgroupRelationEntity.getAttrGroupId()也可以,这里可以直接放进去对象
AttrGroupEntity attrGroupEntity = attrGroupService.getById(attrgroupRelationEntity);
attrRespvo.setGroupName(attrGroupEntity.getAttrGroupName());
}
CategoryEntity categoryEntity = categoryService.getById(item.getCatelogId());
if (categoryEntity != null) {
attrRespvo.setCatelogName(categoryEntity.getName());
}
//返回最后的封装结果
return attrRespvo;
}).collect(Collectors.toList());
//返回的结果是一个集合
pageUtils.setList(resultList);
// 返回分页后的集合对象
return pageUtils;
}
销售属性回显
可以看到,销售属性回显是不需要所属分组的
但是
销售属性和规格参数用的是同一个回显方法,我们也进行更改,只有是规格参数的时候才进行分组回显!
在原分组回显的逻辑上加上判断,后面逻辑不变!
if (attrEntity.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()) {
...
}
修改销售属性
销售属性和规格参数用的是同一个修改方法,销售属性进行修改时,会对关联表进行一个级联更新,但销售属性不需要所以也在对关联表级联更新的时候进行判断,只有销售属性修改的时候才进行级联更新!
if (attrEntity.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()) {
...
}
保存销售属性
销售属性和规格参数用的是同一个保存方法,销售属性进行保存时,会对关联表进行一个级联保存,但销售属性不需要所以也在对关联表级联保存的时候进行判断,只有销售属性保存的时候才进行级联保存!
if (attrEntity.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()) {
...
}