一 分组控制器 AttrGroupController
package com.atguigu.gulimall.product.controller;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.AttrGroupRelationVo;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* @className: AttrGroupController
* @description: 属性分组控制器
* @date: 2020/11/1
* @author: cakin
*/
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
@Autowired
private AttrGroupService attrGroupService;
@Autowired
private CategoryService categoryService;
@Autowired
AttrService attrService;
@Autowired
AttrAttrgroupRelationService relationService;
/**
* 功能描述:新增属性分组和属性的关联关系
*
* @param vos 属性分组和属性的关联关系
* @return R 返回给前端的数据
* @author cakin
* @date 2020/11/4
*/
@PostMapping("/attr/relation")
public R addRelation(@RequestBody List<AttrGroupRelationVo> vos) {
relationService.saveBatch(vos);
return R.ok();
}
@GetMapping("/{catelogId}/withattr")
public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId) {
// 1、查出当前分类下的所有属性分组,
// 2、查出每个属性分组的所有属性
List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
return R.ok().put("data", vos);
}
/**
* 功能描述:获取分组对应的基本属性
*
* @param attrgroupId 分组Id
* @return R 返回给前端的数据
* @author cakin
* @date 2020/11/3
*/
@GetMapping("/{attrgroupId}/attr/relation")
public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
List<AttrEntity> entities = attrService.getRelationAttr(attrgroupId);
return R.ok().put("data", entities);
}
/**
* 功能描述:获取属性分组中没有关联的其他属性
*
* @param attrgroupId 属性组Id
* @param params 分页参数
* @return R 返回给前端的分页数据
* @author cakin
* @date 2020/11/3
*/
@GetMapping("/{attrgroupId}/noattr/relation")
public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
@RequestParam Map<String, Object> params) {
PageUtils page = attrService.getNoRelationAttr(params, attrgroupId);
return R.ok().put("page", page);
}
/**
* 功能描述:删除属性分组和属性的对应关系
*
* @param vos 属性分组与属性的列表
* @return R 返回给前端的数据
* @author cakin
* @date 2020/11/3
*/
@PostMapping("/attr/relation/delete")
public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos) {
attrService.deleteRelation(vos);
return R.ok();
}
/**
* 功能描述:商品属性组列表
*
* @param params 过滤条件
* @param catelogId 商品分组Id
* @return R 返回给前端的数据
* @author cakin
* @date 2020/11/1
* @description:
*/
@RequestMapping("/list/{catelogId}")
public R list(@RequestParam Map<String, Object> params,
@PathVariable("catelogId") Long catelogId) {
PageUtils page = attrGroupService.queryPage(params, catelogId);
return R.ok().put("page", page);
}
/**
* 功能描述:属性分组详情
*
* @param attrGroupId 属性分组Id
* @return R 返回给前端的数据
* @author cakin
* @date 2020/11/1
*/
@RequestMapping("/info/{attrGroupId}")
public R info(@PathVariable("attrGroupId") Long attrGroupId) {
AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
Long catelogId = attrGroup.getCatelogId();
// 分类的完整路径
Long[] path = categoryService.findCatelogPath(catelogId);
attrGroup.setCatelogPath(path);
return R.ok().put("attrGroup", attrGroup);
}
/**
* 保存
*/
@RequestMapping("/save")
public R save(@RequestBody AttrGroupEntity attrGroup) {
attrGroupService.save(attrGroup);
return R.ok();
}
/**
* 修改
*/
@RequestMapping("/update")
public R update(@RequestBody AttrGroupEntity attrGroup) {
attrGroupService.updateById(attrGroup);
return R.ok();
}
/**
* 删除
*/
@RequestMapping("/delete")
public R delete(@RequestBody Long[] attrGroupIds) {
attrGroupService.removeByIds(Arrays.asList(attrGroupIds));
return R.ok();
}
}
二 属性服务实现类 AttrServiceImpl
package com.atguigu.gulimall.product.service.impl;
import com.atguigu.common.constant.ProductConstant;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.atguigu.gulimall.product.dao.AttrDao;
import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.dao.CategoryDao;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.AttrGroupRelationVo;
import com.atguigu.gulimall.product.vo.AttrRespVo;
import com.atguigu.gulimall.product.vo.AttrVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@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);
}
/**
* 功能描述:保存属性
*
* @param attr 前端传过来的属性
* @author cakin
* @date 2020/11/1
* @description: 不仅要保存基本数据,还要保存关联关系
*/
@Transactional
@Override
public void saveAttr(AttrVo attr) {
AttrEntity attrEntity = new AttrEntity();
// 将页面传过来的值封装到AttrEntity中
BeanUtils.copyProperties(attr, attrEntity);
// 1 保存基本数据
this.save(attrEntity);
// 2 保存关联关系,销售属性,没有分组的概念,不用保存
if (attr.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode() && attr.getAttrGroupId() != null) {
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
relationEntity.setAttrGroupId(attr.getAttrGroupId());
relationEntity.setAttrId(attrEntity.getAttrId());
relationDao.insert(relationEntity);
}
}
/**
* 功能描述:分页查询基本属性
*
* @param params 过滤条件
* @param catelogId 分类Id
* @param type 属性类别:基本属性或销售数据
* @return PageUtils 分页查询结果
* @author cakin
* @date 2020/11/1
*/
@Override
public PageUtils queryBaseAttrPage(Map<String, Object> params, Long catelogId, String type) {
// 类型作为一个查询条件:是基本属性还是销售属性
QueryWrapper<AttrEntity> queryWrapper =
new QueryWrapper<AttrEntity>().eq("attr_type", "base".equalsIgnoreCase(type) ?
ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode() : ProductConstant.AttrEnum.ATTR_TYPE_SALE.getCode());
// 分类id是一个查询条件
if (catelogId != 0) {
queryWrapper.eq("catelog_id", catelogId);
}
// 关键字是一个查询条件
String key = (String) params.get("key");
if (!StringUtils.isEmpty(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);
List<AttrEntity> records = page.getRecords();
// 遍历查询结果,得到返回给前端的数据
List<AttrRespVo> respVos = records.stream().map((attrEntity) -> {
AttrRespVo attrRespVo = new AttrRespVo();
// 先把基本数据拷贝到attrRespVo
BeanUtils.copyProperties(attrEntity, attrRespVo);
// 1 设置分组的名字,销售属性是不存在分组的
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());
}
}
// 2 设置分类的名字
CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId());
if (categoryEntity != null) {
attrRespVo.setCatelogName(categoryEntity.getName());
}
return attrRespVo;
}).collect(Collectors.toList()); // 收集attrRespVo数据到列表respVos
PageUtils pageUtils = new PageUtils(page);
pageUtils.setList(respVos);
return pageUtils;
}
/**
* 功能描述:查询商品属性的详细信息
*
* @param attrId 属性id
* @return AttrRespVo 返回给前端的数据
* @author cakin
* @date 2020/11/1
*/
@Override
public AttrRespVo getAttrInfo(Long attrId) {
AttrRespVo respVo = new AttrRespVo();
// 获得商品属性基本信息
AttrEntity attrEntity = this.getById(attrId);
BeanUtils.copyProperties(attrEntity, respVo);
// 销售属性没有分组信息
if (attrEntity.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()) {
// 1 设置分组信息
AttrAttrgroupRelationEntity attrgroupRelation = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrId));
if (attrgroupRelation != null) {
// 属性的分组id
respVo.setAttrGroupId(attrgroupRelation.getAttrGroupId());
AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrgroupRelation.getAttrGroupId());
if (attrGroupEntity != null) {
// 属性分组名称
respVo.setGroupName(attrGroupEntity.getAttrGroupName());
}
}
}
// 2 设置分类信息
Long catelogId = attrEntity.getCatelogId();
Long[] catelogPath = categoryService.findCatelogPath(catelogId);
// 设置分类全路径
respVo.setCatelogPath(catelogPath);
CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
if (categoryEntity != null) {
// 设置分类名称
respVo.setCatelogName(categoryEntity.getName());
}
return respVo;
}
/**
* 功能描述:修改属性
*
* @param attr 待修改的属性
* @author cakin
* @date 2020/11/1
*/
@Transactional
@Override
public void updateAttr(AttrVo attr) {
AttrEntity attrEntity = new AttrEntity();
BeanUtils.copyProperties(attr, attrEntity);
// 更新基本属性
this.updateById(attrEntity);
// 销售属性没有分组信息
if (attrEntity.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()) {
//1 修改分组关联
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
relationEntity.setAttrGroupId(attr.getAttrGroupId());
relationEntity.setAttrId(attr.getAttrId());
Integer count = relationDao.selectCount(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attr.getAttrId()));
if (count > 0) {
// 根据属性Id修改分组Id
relationDao.update(relationEntity, new UpdateWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attr.getAttrId()));
} else {
// 如果没有配置,则插入
relationDao.insert(relationEntity);
}
}
}
/**
* 根据分组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());
// attrIds 不能为空
if (attrIds == null || attrIds.size() == 0) {
return null;
}
// 根据属性id的集合获得属性信息列表
Collection<AttrEntity> attrEntities = this.listByIds(attrIds);
return (List<AttrEntity>) attrEntities;
}
/**
* 功能描述:删除关联关系
*
* @param vos 属性分组id和属性id为对象的列表
* @author cakin
* @date 2020/11/3
*/
@Override
public void deleteRelation(AttrGroupRelationVo[] vos) {
// 封装查询条件到entities,以实现批量删除
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));
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));
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());
if (attrIds != null && attrIds.size() > 0) {
wrapper.notIn("attr_id", attrIds);
}
// 封装关键字查询
String key = (String) params.get("key");
if (!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);
PageUtils pageUtils = new PageUtils(page);
return pageUtils;
}
}
三 属性分组管理DAO AttrAttrgroupRelationDao
package com.atguigu.gulimall.product.dao;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* @className: AttrAttrgroupRelationDao
* @description: 属性属性分组关联DAO
* @date: 2020/11/3
* @author: cakin
*/
@Mapper
public interface AttrAttrgroupRelationDao extends BaseMapper<AttrAttrgroupRelationEntity> {
/**
* 功能描述:删除属性属性分组关联
*
* @param entities 封装删除条件
* @author cakin
* @date 2020/11/3
*/
void deleteBatchRelation(@Param("entities") List<AttrAttrgroupRelationEntity> entities);
}
四 属性分组管理Mapper AttrAttrgroupRelationDao.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.atguigu.gulimall.product.dao.AttrAttrgroupRelationDao">
<!-- 可根据自己的需求,是否要使用 -->
<resultMap type="com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity" id="attrAttrgroupRelationMap">
<result property="id" column="id"/>
<result property="attrId" column="attr_id"/>
<result property="attrGroupId" column="attr_group_id"/>
<result property="attrSort" column="attr_sort"/>
</resultMap>
<!-- 批量删除属性属性分组关联-->
<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>
</mapper>
五 测试