平台属性
中间表复杂业务
/**
* 获取分类规格参数(模糊查询)
*
* @param params
* @param catelogId
* @param type type="base"时查询基础属性,type="sale"时查询销售属性
* @return
*/
@Override
public PageUtils listByCatelogId(Map<String, Object> params, Long catelogId, String type) {
String key = (String) params.get("key");
QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
// 1.判断参数中是否存在检索关键字key
if(!StringUtils.isEmpty(key)){
wrapper.and((obj) -> {
obj.eq("attr_id", key)
.or().like("attr_name", key)
.or().like("value_select", key);
});
}
// 2.判断参数中是否存在catelogId
// 2.1.查询全部
if(catelogId != 0){
wrapper.eq("catelog_id", catelogId);
}
IPage<AttrEntity> page = this.page(
new Query<AttrEntity>().getPage(params),
wrapper.eq("attr_type", "base".equalsIgnoreCase(type) ? 1 : 0)
);
PageUtils pageUtils = new PageUtils(page);
// 3.封装成AttrRespVO
List<AttrRespVo> respVos = page.getRecords().stream().map((attrEntity) -> {
AttrRespVo attrRespVo = new AttrRespVo();
BeanUtils.copyProperties(attrEntity, attrRespVo);
// 3.1.得到所属分类名
CategoryEntity categoryEntity = categoryService.getById(attrEntity.getCatelogId());
if(categoryEntity != null){
attrRespVo.setCatelogName(categoryEntity.getName());
}
// 3.2.得到所属分组名
if (ATTR_TYPE_BASE.getMsg().equalsIgnoreCase(type)) {
AttrAttrgroupRelationEntity relationEntity = relationService.query().eq("attr_id", attrEntity.getAttrId()).one();
if(relationEntity != null){
AttrGroupEntity attrGroupEntity = attrGroupService.getById(relationEntity.getAttrGroupId());
attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
}
}
return attrRespVo;
}).collect(Collectors.toList());
pageUtils.setList(respVos);
return pageUtils;
}
批量删除
/**
* 删除属性与分组的关联关系
* DELETE FROM pms_attr_attrgroup_relation
* WHERE (( attr_id = 1 and attr_group_id = 1) OR ( attr_id = 2 and attr_group_id = 3))
*/
@Override
public void deleteRelation(List<AttrGroupRelationVo> voList) {
// 1.获取
List<AttrAttrgroupRelationEntity> relationEntities = voList.stream().map(attrGroupRelationVo -> {
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
relationEntity.setAttrId(attrGroupRelationVo.getAttrId());
relationEntity.setAttrGroupId(attrGroupRelationVo.getAttrGroupId());
return relationEntity;
}).collect(Collectors.toList());
// 2.批量删除
relationDao.deleteBatchRelation(relationEntities);
}
<!-- DELETE FROM pms_attr_attrgroup_relation
WHERE (( attr_id = 1 and attr_group_id = 1) OR ( attr_id = 2 and attr_group_id = 3))-->
<delete id="deleteBatchRelation">
DELETE FROM `pms_attr_attrgroup_relation` WHERE
<foreach collection="relationEntities" item="item" separator=" OR ">
( attr_id = #{item.attrId} AND attr_group_id = #{item.attrGroupId})
</foreach>
</delete>
中间表复杂业务
/**
* 获取属性分组没有关联的其他属性
*/
@Override
public PageUtils listNoRelationAttr(Map<String, Object> params, Long attrgroupId) {
// 1.获取分类id
Long catelogId = this.query().eq("attr_group_id", attrgroupId).one().getCatelogId();
// 2.获取当前分类下的其他分组id集合
List<Long> groupIds = this.list(new QueryWrapper<AttrGroupEntity>()
.eq("catelog_id", catelogId))
.stream()
.map(AttrGroupEntity::getAttrGroupId)
.collect(Collectors.toList());;
// 3.获取其他分组关联的属性id集合
List<Long> attrIds = relationService.list(new QueryWrapper<AttrAttrgroupRelationEntity>()
.in("attr_group_id", groupIds))
.stream()
.map(AttrAttrgroupRelationEntity::getAttrId)
.collect(Collectors.toList());
// 4.剔除其他分组关联的属性
QueryWrapper<AttrEntity> wrapper = new QueryWrapper<AttrEntity>()
.eq("catelog_id", catelogId)
.ne("value_type", ATTR_TYPE_BASE.getCode());
if(attrIds != null && attrIds.size() > 0){
wrapper.notIn("attr_id", attrIds);
}
// 5.判断key
String key = (String) params.get("key");
if(!StringUtils.isEmpty(key)){
wrapper.and((obj) -> {
obj.eq("attr_id", key)
.or().like("attr_name", key)
.or().like("value_select", key);
});
}
IPage<AttrEntity> page = attrService.page(new Query<AttrEntity>().getPage(params), wrapper);
return new PageUtils(page);
}