分组关联属性的新增
在能够正确显示未关联属性之后,添加能够新增关联属性的功能,对应前端页面,可以看到对应的新增请求还是404的返回:
新增功能对应的API文档:
首先来到控制响应层:
...
@Autowired
AttrAttrgroupRelationService relationService;
...
/**
* 处理响应/product/attrgroup/attr/relation
* @param vos 添加管理的列表
* @return R.ok()
*/
@PostMapping("/attr/relation")
public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){
relationService.saveBatch(vos);
return R.ok();
}
之后创建方法实现方法saveBatch(vos):
/**
* 批量保存分组和属性的关联表
* @param vos 新建关联分组ID和属性ID 列表
*/
@Override
public void saveBatch(List<AttrGroupRelationVo> vos) {
//系统有批量保存的saveBatch方法,对象必须是AttrAttrgroupRelationEntity
//进行流式转换,将AttrGroupRelationVo转为AttrAttrgroupRelationEntity
List<AttrAttrgroupRelationEntity> collect = vos.stream().map(item -> {
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
BeanUtils.copyProperties(item, relationEntity);
return relationEntity;
}).collect(Collectors.toList());
//之后调用系统的saveBatch方法
this.saveBatch(collect);
}
之后重启服务来到前端进行测试,点击确认新增之后就能看到属性信息:
同时别的分组无法在关联到该属性:
PS:这里需要修改一个方法钟的非空判断,避免出现检索时的错误
AttrServiceImpl的saveAttr(AttrVo attr)方法:
package com.lastingwar.mall.product.service.impl;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lastingwar.common.constant.ProductConstant;
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.service.CategoryService;
import com.lastingwar.mall.product.vo.AttrGroupRelationVo;
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.Arrays;
import java.util.Collection;
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;
@Autowired
CategoryService categoryService;
@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);
if (attr.getAttrType()== ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode() && attr.getAttrGroupId()!=null){
//保存关联信息
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
relationEntity.setAttrGroupId(attr.getAttrGroupId());
//由于ID是自动生成的,这里使用传入的vo是没有值的
relationEntity.setAttrId(attrEntity.getAttrId());
relationDao.insert(relationEntity);}
}
/**
* 通过分类属性ID查找分类名称和组名称添加到返回中,
* @param params 基础的返回信息
* @param catelogId 分类属性ID
* @param type
* @return AttrRespVo
*/
@Override
public PageUtils queryBaseAttrPage(Map<String, Object> params, Long catelogId, String type) {
//重写QueryWrapper,进行不同的检索
QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<AttrEntity>().eq("attr_type","base".equalsIgnoreCase(type)?ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode():ProductConstant.AttrEnum.ATTR_TYPE_SALE.getCode());
//如果请求的有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获取分组名称
//基础信息才会存在关联信息
if("base".equalsIgnoreCase(type)){
AttrAttrgroupRelationEntity attrId = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId()));
if (attrId != null && attrId.getAttrGroupId()!=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;
}
/**
* 请求属性ID,返回包含有属性基础信息,附加分类,附加分组信息的属性详细信息
* @param attrId 属性ID
* @return AttrRespVo
*/
@Override
public AttrRespVo getAttrInfo(Long attrId) {
AttrRespVo attrRespVo = new AttrRespVo();
//拷贝原有的内容
AttrEntity attrEntity = this.getById(attrId);
BeanUtils.copyProperties(attrEntity,attrRespVo);
//查询到的attrAttrgroupRelation表的全部信息
if(attrEntity.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()){
AttrAttrgroupRelationEntity attrAttrgroupRelation= relationDao.
selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrId));
if (attrAttrgroupRelation!=null) {
//保存分组ID信息
attrRespVo.setAttrGroupId(attrAttrgroupRelation.getAttrGroupId());
//根据分组ID查询分组名称
AttrGroupEntity attrGroupEntity = attrGroupDao.
selectById(attrAttrgroupRelation.getAttrGroupId());
if (attrGroupEntity != null) {
//保存分组名称
attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
}
}
}
Long[] catelogPath = categoryService.findCatelogPath(attrEntity.getCatelogId());
//添加分组路径
attrRespVo.setCatelogPath(catelogPath);
CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId());
//添加分类名称
if (categoryEntity!=null){
attrRespVo.setCatelogName(categoryEntity.getName());
}
return attrRespVo;
}
/**
* 根据请求中的属性详细信息,同时修改信息表和关联表的信息
* @param attrVo 属性详细信息
*/
@Transactional
@Override
public void updateAttr(AttrVo attrVo) {
//首先保存基本信息
AttrEntity attrEntity = new AttrEntity();
BeanUtils.copyProperties(attrVo, attrEntity);
this.updateById(attrEntity);
//修改关联信息
if (attrEntity.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()) {
AttrAttrgroupRelationEntity attrgroupRelation = new AttrAttrgroupRelationEntity();
attrgroupRelation.setAttrGroupId(attrVo.getAttrGroupId());
attrgroupRelation.setAttrId(attrVo.getAttrId());
//判断数据库时候已经存在当前的值
Integer count = relationDao.selectCount(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrgroupRelation.getAttrId()));
if (count < 1) {
relationDao.insert(attrgroupRelation);
}
//使用UpdateWrapper,更新行的匹配条件是attr_id相同
else {
relationDao.update(attrgroupRelation,
new UpdateWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrgroupRelation.getAttrId()));
}
}
}
/**
* 根据分组id查找关联的所有基本属性
* @param attrgroupId 分组id
* @return List<AttrEntity>
*/
@Override
public List<AttrEntity> getRelationAttr(Long attrgroupId) {
List<AttrAttrgroupRelationEntity> entities = relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrgroupId));
//收集ID的集合
List<Long> attrIds = entities.stream().map((attr) -> {
return attr.getAttrId();
}).collect(Collectors.toList());
if(attrIds == null || attrIds.size() == 0){
return null;
}
//根据属性ID查找所以的属性集合
Collection<AttrEntity> attrEntities = this.listByIds(attrIds);
return (List<AttrEntity>) attrEntities;
}
/**
* 根据请求的数组,批量删除商品属性关联关系
* @param vos AttrGroupRelationVo请求的数组
*/
@Override
public void deleteRelation(AttrGroupRelationVo[] vos) {
//relationDao.delete(new QueryWrapper<>().eq("attr_id",1L).eq("attr_group_id",1L));
//需要批量删除多条信息,所以使用自定义的sql语句。
//先把数组转为列表,之后使用流式处理
List<AttrAttrgroupRelationEntity> entities = Arrays.asList(vos).stream().map((item) -> {
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
BeanUtils.copyProperties(item, relationEntity);
return relationEntity;
}).collect(Collectors.toList());
relationDao.deleteBatchRelation(entities);
}
/**
* 获取当前分组没有关联的所有属性
* @param params 分页插件的基本信息
* @param attrgroupId 分类组ID
* @return 列表形式的未关联属性
*/
@Override
public PageUtils getNoRelationAttr(Map<String, Object> params, Long attrgroupId) {
//1、当前分组只能关联自己所属的分类里面的所有属性
AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrgroupId);
//首先查找出分类ID
Long catelogId = attrGroupEntity.getCatelogId();
//2、当前分组只能关联别的分组没有引用的属性
//2.1)、当前分类下的其他分组
List<AttrGroupEntity> group = attrGroupDao.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
//从属性分组实体类的集合通过流转化为组ID的集合
List<Long> collect = group.stream().map(item -> {
return item.getAttrGroupId();
}).collect(Collectors.toList());
//2.2)、这些分组关联的属性
//找到具有关联属性的关系表实例
List<AttrAttrgroupRelationEntity> groupId = relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().in("attr_group_id", collect));
//通过流使关联表的实体类转化为属性ID的集合
List<Long> attrIds = groupId.stream().map(item -> {
return item.getAttrId();
}).collect(Collectors.toList());
//2.3)、从当前分类的所有属性中移除这些属性;
//只能够关联基本属性
QueryWrapper<AttrEntity> wrapper = new QueryWrapper<AttrEntity>()
.eq("catelog_id", catelogId).eq("attr_type",ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode());
//对列表进行非空判断,之后添加过滤条件,不在分类ID中
if(attrIds!=null && attrIds.size()>0){
wrapper.notIn("attr_id", attrIds);
}
//获取搜索关键词,如果关键词不为空添加进wrapper条件中
String key = (String) params.get("key");
if(!org.springframework.util.StringUtils.isEmpty(key)){
wrapper.and((w)->{
w.eq("attr_id",key).or().like("attr_name",key);
});
}
//使用通用的实体类获取当前页面的内容
IPage<AttrEntity> page = this.page(new Query<AttrEntity>().getPage(params), wrapper);
return new PageUtils(page);
}
}
这是在 添加属性关联关系的时候进行一次属性分组的非空判断,非空的时候才会进行数据库的保存,让数据库不再出现如下数据: