文章目录
- 获取指定分类的属性列表
- AttrController.java
- AttrServiceImpl.java
- 获取属性分组所关联的所有属性
- AttrGroupController
- AttrServiceImpl.java
- 移除
- AttrGroupController.java
- AttrServiceImpl.java
- AttrAttrgroupRelationDao.java
- AttrAttrgroupRelationDao.xml
- 获取属性分组中,当前分类下没有进行关联的所有属性
- AttrGroupController.java
- AttrServiceImpl.java
- 添加属性与属性分组的关联关系
- AttrGroupController.java
- AttrAttrgroupRelationServiceImpl.java
- =================================================
- controller
- AttrAttrgroupRelationController.java
- AttrController.java
- AttrGroupController.java
- dao
- AttrAttrgroupRelationDao.java
- AttrDao.java
- AttrGroupDao.java
- entity
- AttrAttrgroupRelationEntity.java
- AttrEntity.java
- AttrGroupEntity.java
- service
- AttrAttrgroupRelationService
- AttrGroupService.java
- AttrService.java
- impl
- AttrAttrgroupRelationServiceImpl.java
- AttrGroupServiceImpl.java
- AttrServiceImpl.java
- xml
- AttrAttrgroupRelationDao.xml
- 前端
- attr-add-or-update.vue
- attr-group-relation.vue
- attrgroup-add-or-update.vue
- attrgroup.vue
- baseattr.vue
- category.vue
- saleattr.vue
获取指定分类的属性列表
AttrController.java
@RequestMapping("/{attrType}/list/{categoryId}")
public R list(@RequestParam Map<String, Object> params,@PathVariable("categoryId") Long categoryId, @PathVariable("attrType") String attrType){
PageUtils page = attrService.queryBaseAttrPage(params, categoryId, attrType);
return R.ok().put("page", page);
}
AttrServiceImpl.java
@Override
public PageUtils queryBaseAttrPage(Map<String, Object> params, Long categoryId, String attrType) {
QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<>();
if("base".equals(attrType)){
queryWrapper.eq("attr_type",1);
} else if("sale".equals(attrType)){
queryWrapper.eq("attr_type",0);
}
if (categoryId != 0) {
queryWrapper.eq("category_id", categoryId);
}
String key = (String) params.get("key");
if (!StringUtils.isEmpty(key)){
queryWrapper.and(wrapper -> {
wrapper.eq("id", key).or().like("name",key);
});
}
IPage<AttrEntity> page = this.page(
new Query<AttrEntity>().getPage(params),
queryWrapper
);
PageUtils pageUtils = new PageUtils(page);
List<AttrEntity> records = page.getRecords();
List<AttrRespVo> respVos = records.stream().map(attrEntity -> {
AttrRespVo attrRespVo = new AttrRespVo();
BeanUtils.copyProperties(attrEntity,attrRespVo);
AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = attrAttrgroupRelationDao.selectOne(
new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getId())
);
if (attrAttrgroupRelationEntity != null) {
AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrAttrgroupRelationEntity.getAttrGroupId());
if (attrGroupEntity != null) {
attrRespVo.setGroupName(attrGroupEntity.getName());
}
}
CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCategoryId());
if(categoryEntity != null) {
attrRespVo.setCategoryName(categoryEntity.getName());
}
return attrRespVo;
}).collect(Collectors.toList());
pageUtils.setList(respVos);
return pageUtils;
}
获取属性分组所关联的所有属性
AttrGroupController
@GetMapping("/{attrGroupId}/attr/relation")
public R getAttrRelation(@PathVariable("attrGroupId") Long attrGroupId){
List<AttrEntity> entities = attrService.getRelationAttr(attrGroupId);
return R.ok().put("data", entities);
}
AttrServiceImpl.java
@Override
public List<AttrEntity> getRelationAttr(Long attrGroupId) {
List<AttrAttrgroupRelationEntity> entities = attrAttrgroupRelationDao.selectList(
new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrGroupId)
);
List<Long> attrIds = entities.stream().map(attrAttrgroupRelationEntity -> {
return attrAttrgroupRelationEntity.getAttrId();
}).collect(Collectors.toList());
if (attrIds == null || attrIds.size() == 0) {
return null;
}
List<AttrEntity> attrEntities = this.listByIds(attrIds);
return attrEntities;
}
移除
AttrGroupController.java
@PostMapping("/attr/relation/delete")
public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos){
attrService.deleteRelation(vos);
return R.ok();
}
AttrServiceImpl.java
@Override
public void deleteRelation(AttrGroupRelationVo[] vos) {
List<AttrAttrgroupRelationEntity> relationEntities = Arrays.asList(vos).stream().map(attrGroupRelationVo -> {
AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
BeanUtils.copyProperties(attrGroupRelationVo, attrAttrgroupRelationEntity);
return attrAttrgroupRelationEntity;
}).collect(Collectors.toList());
attrAttrgroupRelationDao.deleteBatch(relationEntities);
}
AttrAttrgroupRelationDao.java
package com.xd.cubemall.product.dao;
import com.xd.cubemall.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;
@Mapper
public interface AttrAttrgroupRelationDao extends BaseMapper<AttrAttrgroupRelationEntity> {
void deleteBatch(@Param("entities") List<AttrAttrgroupRelationEntity> relationEntities);
}
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.xd.cubemall.product.dao.AttrAttrgroupRelationDao">
<resultMap type="com.xd.cubemall.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="deleteBatch">
DELETE FROM tb_attr_attrgroup_relation
<where>
<foreach collection="entities" item="item" separator=" or ">
(attr_id=#{item.attrId} and attr_group_id=#{item.attrGroupId})
</foreach>
</where>
</delete>
</mapper>
获取属性分组中,当前分类下没有进行关联的所有属性
AttrGroupController.java
@GetMapping("/{attrGroupId}/noattr/relation")
public R getNoAttrRelation(@RequestParam Map<String, Object> params, @PathVariable("attrGroupId") Long attrGroupId){
PageUtils page = attrService.getNoRelationAttr(params, attrGroupId);
return R.ok().put("page", page);
}
AttrServiceImpl.java
@Override
public PageUtils getNoRelationAttr(Map<String, Object> params, Long attrGroupId) {
AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrGroupId);
Integer categoryId = attrGroupEntity.getCategoryId();
List<AttrGroupEntity> attrGroupEntities = attrGroupDao.selectList(
new QueryWrapper<AttrGroupEntity>().eq("category_id",categoryId)
);
List<Long> groupIds = attrGroupEntities.stream().map(attrGroup -> {
return attrGroup.getId();
}).collect(Collectors.toList());
List<AttrAttrgroupRelationEntity> entities = attrAttrgroupRelationDao.selectList(
new QueryWrapper<AttrAttrgroupRelationEntity>().in("attr_group_id",groupIds)
);
List<Long> attrIds = entities.stream().map(item -> {
return item.getAttrId();
}).collect(Collectors.toList());
QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("category_id",categoryId).eq("attr_type",1);
if (attrIds != null && attrIds.size() > 0) {
queryWrapper.notIn("id",attrIds);
}
String key = (String) params.get("key");
if (StringUtils.isEmpty(key)) {
queryWrapper.and(wrapper -> {
wrapper.eq("id", key).or().like("name",key);
});
}
IPage<AttrEntity> page = this.page(new Query<AttrEntity>().getPage(params), queryWrapper);
return new PageUtils(page);
}
添加属性与属性分组的关联关系
AttrGroupController.java
@PostMapping("/attr/relation")
public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){
attrAttrgroupRelationService.saveBatch(vos);
return R.ok();
}
AttrAttrgroupRelationServiceImpl.java
@Override
public void saveBatch(List<AttrGroupRelationVo> vos) {
List<AttrAttrgroupRelationEntity> collect = vos.stream().map(attrGroupRelationVo -> {
AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
BeanUtils.copyProperties(attrGroupRelationVo, attrAttrgroupRelationEntity);
return attrAttrgroupRelationEntity;
}).collect(Collectors.toList());
this.saveBatch(collect);
}
=================================================
controller
AttrAttrgroupRelationController.java
package com.xd.cubemall.product.controller;
import java.util.Arrays;
import java.util.Map;