第二十一章第七节:属性分组-关联功能的新建关联

1、关联属性查询

查询出当前分类下没有被属性分组关联的规格参数数据

1>、定义查询接口

com.applesnt.onlinemall.product.service.AttrService

PageUtils getNoRetaionAttr(Map<String, Object> params, Long attrgroupId);

2>、定义查询接口实现

com.applesnt.onlinemall.product.service.impl.AttrServiceImpl

    /*
    * 整体思路:查询出当前分类下没有被属性分组关联的规格参数数据
    * */
    @Override
    public PageUtils getNoRetaionAttr(Map<String, Object> params, Long attrgroupId) {
        /*先根据传递的属性分组参数,查询当前所属分类的id*/
        AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrgroupId);
        Long catelogId = attrGroupEntity.getCatelogId();
log.info("当前分组所属的分类id是:"+catelogId);

        /*当前分组只能关联所有分组没有被引用的规格参数*/
        //1、查询出当前分类下关联的所有的属性分组
        List<AttrGroupEntity> attrGroupEntities = attrGroupDao.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        //收集属性分组id
        List<Long> collect = attrGroupEntities.stream().map((obj) -> {
            return obj.getAttrGroupId();
        }).collect(Collectors.toList());
log.info("当前分类下所有的分组是:"+collect.toString());

        //2、根据当前属性分组集合查询抽根烟关联的所有规格参数
        List<AttrAttrgroupRelationEntity> relationEntities = relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().in("attr_group_id", collect));
        //收集规格参数id
        List<Long> attrids = relationEntities.stream().map((attr) -> {
            return attr.getAttrId();
        }).collect(Collectors.toList());
log.info("已有属性分组的规格参数包括:"+attrids.toString());
        /*从当前分类中的所有规格参数中移除这些被关联的*/
        //查询当前分类下所有的规格参数
        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);
        //返回分页结果集
        return new PageUtils(page);
    }

3>、controller调用

com.applesnt.onlinemall.product.controller.AttrGroupController

    /*关联属性的查询功能*/
    @RequestMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@RequestParam Map<String, Object> params,
                            @PathVariable("attrgroupId") Long attrgroupId){
        PageUtils page = attrService.getNoRetaionAttr(params,attrgroupId);
        return R.ok().put("page",page);
    }

2、关联属性添加

1>、定义添加接口

com.applesnt.onlinemall.product.service.AttrAttrgroupRelationService

void saveBatch(List<AttrGroupRetaionVo> vos);

2>、定义添加接口实现

com.applesnt.onlinemall.product.service.impl.AttrAttrgroupRelationServiceImpl

    @Override
    public void saveBatch(List<AttrGroupRetaionVo> vos) {
        /*前端接收数据的vo与要操作的实体类进行属性值拷贝*/
        List<AttrAttrgroupRelationEntity> collect = vos.stream().map((retaionVo) -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(retaionVo, relationEntity);
            return relationEntity;
        }).collect(Collectors.toList());
        //批量添加操作
        this.saveBatch(collect);
    }

3>、controller调用

com.applesnt.onlinemall.product.controller.AttrGroupController

    /*关联属性的确认添加功能*/
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRetaionVo> vos){
        relationService.saveBatch(vos);
        return R.ok();
    }
posted @ 2021-06-19 22:03  努力的校长  阅读(80)  评论(0)    收藏  举报