017 平台属性[属性分组、规格参数、销售属性]

文章目录

    • 获取指定分类的属性列表
      • 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

基本属性 销售属性
attr-add-or-update
attr-add-or-update

获取指定分类的属性列表

AttrController.java

    /**
     * 列表
     * 请求路径 /product/attr/base/list/{categoryId},分页查询,模糊查询,基本属性
     * 请求路径 /product/attr/sale/list/{categoryId},分页查询,模糊查询,销售属性
     */
    @RequestMapping("/{attrType}/list/{categoryId}")
    //@RequiresPermissions("product:attr:list")
    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

 /**
     * 获取指定分类的属性列表
     *
     * @param params
     * @param categoryId
     * @param attrType
     * @return
     */
    @Override
    public PageUtils queryBaseAttrPage(Map<String, Object> params, Long categoryId, String attrType) {
        QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<>();
        //拼接查询的attrtype条件
        if("base".equals(attrType)){
            queryWrapper.eq("attr_type",1);
        } else if("sale".equals(attrType)){
            queryWrapper.eq("attr_type",0);
        }


        //拼接分类id
        if (categoryId != 0) {
            queryWrapper.eq("category_id", categoryId);
        }
        //拼接关键字查询 key
        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();
            //1.封装基础属性内容
            BeanUtils.copyProperties(attrEntity,attrRespVo);
            //2.封装属性分组名称
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = attrAttrgroupRelationDao.selectOne(
                    new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getId())
            );
            //根据attrAttrgroupRelationEntity 获取attrGroup
            if (attrAttrgroupRelationEntity != null) {
                AttrGroupEntity attrGroupEntity =  attrGroupDao.selectById(attrAttrgroupRelationEntity.getAttrGroupId());

                if (attrGroupEntity != null) {
                    attrRespVo.setGroupName(attrGroupEntity.getName());
                }

            }

            //3.封装分类名称
            CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCategoryId());
            if(categoryEntity != null) {
                attrRespVo.setCategoryName(categoryEntity.getName());
            }



            return attrRespVo;
        }).collect(Collectors.toList());

        pageUtils.setList(respVos);

        return pageUtils;
    }

获取属性分组所关联的所有属性

在这里插入图片描述

AttrGroupController

    /**
     * 获取属性分组所关联的所有属性
     * @param attrGroupId
     * @return
     */
    @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

    /**
     * 获取属性分组所关联的所有属性
     * @param attrGroupId
     * @return
     */
    @Override
    public List<AttrEntity> getRelationAttr(Long attrGroupId) {
        //获取属性分组及属性关联关系
        List<AttrAttrgroupRelationEntity> entities = attrAttrgroupRelationDao.selectList(
                new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrGroupId)
        );

        //获取每个属性关联对象中的属性id
        List<Long> attrIds = entities.stream().map(attrAttrgroupRelationEntity -> {
            return attrAttrgroupRelationEntity.getAttrId();
        }).collect(Collectors.toList());




        //通过属性id 获取对应的 AttrEntity对象
        if (attrIds == null || attrIds.size() == 0) {
            return null;
        }

        List<AttrEntity> attrEntities = this.listByIds(attrIds);
        return attrEntities;
    }

移除

AttrGroupController.java

    /**
     * 请求参数 [{attrId: 19, attrGroupId: 10}]
     * @return
     */

    @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;

/**
 * 属性分组关联表
 * 
 * @author xuedong
 * @email email@gmail.com
 * @date 2024-08-13 01:36:04
 */
@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

    /**
     * 获取属性分组中,当前分类下没有进行关联的所有属性
     * @param attrGroupId
     * @return
     */
    @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

    /**
     * 获取属性分组中,当前分类下没有进行关联的所有属性
     * @param params
     * @param attrGroupId
     * @return
     */
    @Override
    public PageUtils getNoRelationAttr(Map<String, Object> params, Long attrGroupId) {
        //1.获取当前分类下,所有分组已经关联的属性
        AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrGroupId);
        //1.1 获取当前属性分组所对应的 分类
        Integer categoryId = attrGroupEntity.getCategoryId();
        //1.2 获取当前分类下 所关联的所有属性分组
        List<AttrGroupEntity> attrGroupEntities = attrGroupDao.selectList(
                new QueryWrapper<AttrGroupEntity>().eq("category_id",categoryId)
        );
        //获取当前分类下,所有分组的ID
        List<Long> groupIds = attrGroupEntities.stream().map(attrGroup -> {
            return attrGroup.getId();
        }).collect(Collectors.toList());
        //从中间表 attr_attrgroup_relation中 获取 groupIds 所对应的属性
        List<AttrAttrgroupRelationEntity> entities = attrAttrgroupRelationDao.selectList(
                new QueryWrapper<AttrAttrgroupRelationEntity>().in("attr_group_id",groupIds)
        );
        // attrIds 是 当前分类下,所有属性分组已经关联的 属性集合
        List<Long> attrIds = entities.stream().map(item -> {
            return item.getAttrId();
        }).collect(Collectors.toList());




        //2.获取当前分类下所有属性
        QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id",categoryId).eq("attr_type",1);

        //3.移除已经关联的内容,剩下的就是未关联的属性
        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

    /**
     * 添加属性与属性分组的关联关系
     * @param vos
     */
    @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;


上一篇:数据中心运维挑战:性能监控的困境与智能化解决方案的探寻