携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第19天,点击查看活动详情
开发流程如下: 开发需求,就像在线接口文档如下https://easydoc.net/s/78237135/ZUqEdvA4/OXTgKobR这里一样, 别人告诉你需要什么功能,需要返回什么样的数据,你就通过接口的形式把他们呢实现出来即可!
这一部分都是CRUD相关的代码,所以要好好练好好写!!!
开发接口就是开发Controller、service、dao
以后工作了也是这种形式,主要是开发接口为多,前端其实不用写太多,能看懂即可!!!
下面为分组与属性关联的接口编写
分组与属性关联
显示属性
这里其实就是一个分布查询,流程如下:
- 点击分组属性的时候获取到分组id,
- 拿分组id去关联表查分组id对应的attr_id
- 拿
attr_id去pms_attr表中获取属性
controller
/**
* 3.获取属性分组的关联的所有属性
*/
@RequestMapping("/{attrgroupId}/attr/relation")
public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
List<AttrEntity> entities = attrService.getRelationAttr(attrgroupId);
return R.ok().put("data", entities);
}
service
@Override
public List<AttrEntity> getRelationAttr(Long attrgroupId) {
//分布查询,第一步去关联表中查出所有的组和属性id
List<AttrAttrgroupRelationEntity> entities = relationService.list(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id",attrgroupId));
//第二收集属性id
List<Long> attrIds = entities.stream().map((attr) -> {
return attr.getAttrId();
}).collect(Collectors.toList());
List<AttrEntity> list = this.listByIds(attrIds);
return list;
}
测试
属性显示成功
移除属性
这里为了方便,我们直接写一个批量删除的接口
controller
- /product/attrgroup/attr/relation/delete
- post请求会带来json数据,要封装成自定义对象vos需要@RequestBody注解
- 意思就是将请求体中的数据封装成vos
/**
* 4.移除属性分组和属性的关系
*/
@PostMapping("/attr/relation/delete")
public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos) {
attrService.deleteRelation(vos);
return R.ok();
}
service
@Override
public void deleteRelation(AttrGroupRelationVo[] vos) {
List<AttrAttrgroupRelationEntity> entities = Arrays.asList(vos).stream().map((item) -> {
AttrAttrgroupRelationEntity entity = new AttrAttrgroupRelationEntity();
BeanUtils.copyProperties(item, entity);
return entity;
}).collect(Collectors.toList());
relation.deleteBatchRelation(entities);
}
mapper
void deleteBatchRelation(@Param("entities") List<AttrAttrgroupRelationEntity> entities);
<delete id="deleteBatchRelation">
DELETE FROM `pms_attr_attrgroup_relation` where
<foreach collection="entities" item="item" separator="OR">
(attr_id = #{item.attrId} AND attr_group_id = #{item.attrGroupId})
</foreach>
</delete>
查询分组未关联的属性
逻辑分析
controller
/**
* 5.获取属性分组没有关联的所有属性
* /product/attrgroup/{attrgroupId}/noattr/relation
*/
@RequestMapping("/{attrgroupId}/noattr/relation")
public R attrNoRelation(@RequestParam Map<String, Object> params,
@PathVariable("attrgroupId") Long attrgroupId) {
PageUtils page = attrService.getNoRelationAttr(params,attrgroupId);
return R.ok().put("page", page);
}
service
认真看注释,认真理解,还是很绕的
查询分组未关联的数据三步!
- 获得当前分类下的所有分组
- 获得这些分组下所有已添加的属性
- 添加新属性时移除这些已添加的属性
@Override
public PageUtils getNoRelationAttr(Map<String, Object> params, Long attrgroupId) {
/**
* 1.当前分组只能关联自己所属的分类里面的所有属性
*/
AttrGroupEntity attrGroupEntity = attrGroupService.getById(attrgroupId);
Long catelogId = attrGroupEntity.getCatelogId();
/**
* 2 .当前分组只能引用别的分组没有引用的属性
* 2.1 当前分类下的所有分组
* 2.2 这些分组关联的属性
* 2.3 从当前分类的所有属性中移除这些属性
*/
/**
* 2.1 当前分类下的所有分组。收集到他们的组id
*/
List<AttrGroupEntity> group = attrGroupService.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
List<Long> collectGroupIds = group.stream().map((item) -> {
return item.getAttrGroupId();
}).collect(Collectors.toList());
/**
* 2.2 收集到分组的所有属性
* (1)拿着上一步收集到的组id到关系表中查找关系表实体类对象,
* (2)通过关系表实体类对象获得所有分组下的所有属性id
*/
List<AttrAttrgroupRelationEntity> groupId = relationService.list(new QueryWrapper<AttrAttrgroupRelationEntity>().in("attr_group_id", collectGroupIds));
List<Long> attrIds = groupId.stream().map((item) -> {
return item.getAttrId();
}).collect(Collectors.toList());
/**
* 2.3 从当前分类的所有属性中移除这些属性并筛选出基本属性(where attr_type = 1)
*/
QueryWrapper<AttrEntity> wrapper = new QueryWrapper<AttrEntity>().eq("catelog_id", catelogId).eq("attr_type",ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode());
//如果其他分组也没关联属性,那么就不加这个条件
if (attrIds != null && attrIds.size() > 0){
wrapper.notIn("attr_id", attrIds);
}
/**
* 分页多条件查询
* where (`attr_id` = ? or `attr_name` like ?)
*/
String key = (String) params.get("key");
if (!StringUtils.isEmpty(key)) {
wrapper.and((w) -> {
w.eq("attr_id", key).or().like("attr_name", key);
});
}
/**
* page方法需要两个参数
* 1.IPage对象(通过工具类Query获取并通过.getPage(params)封装页面传来分页参数)
* 2.wrapper(自己生成)
*/
IPage<AttrEntity> page = this.page(new Query<AttrEntity>().getPage(params), wrapper);
PageUtils pageUtils = new PageUtils(page);
return pageUtils;
}
tips:
注意非空判断
测试
给销售属性绑定分组,把9号属性绑定给1号分组
查询分组未关联的属性
添加属性关联
常规的调用,注意点是saveBatch传的参数是数据对应的实体类
我们想传其他
vo时,需要对这个方法进行一个重写最后也是通过把vo的值赋给对应实体类,在调用相应批量保存
controller
/**
* 6.添加属性与分组关联关系
* /product/attrgroup/attr/relation
*/
@PostMapping("/attr/relation")
public R addRelation(@RequestBody List<AttrGroupRelationVo> vos) {
relationService.saveBatch(vos);
return R.ok();
}
service
@Override
public void saveBatch(List<AttrGroupRelationVo> vos) {
List<AttrAttrgroupRelationEntity> collect = vos.stream().map((item) -> {
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
BeanUtils.copyProperties(item, relationEntity);
return relationEntity;
}).collect(Collectors.toList());
this.saveBatch(collect);
}
\