document
API test

获取分类下未关联的属性

POST

Description or Example

# BUG说明 > 1. 代码中需要创建一个temp临时变量 `HashMap<String, Object> temp = new HashMap<>(params)` 获取所有的属性分组信息, 会将map集合里面的数据修改了, 后面再用到的时候, 里面的数据就不是传进来的数据了, 会发生`ClassCastException`这个异常, 不容易排查 > 2. 存在in的形况 若存在`in`或`notin`条件的, 都需要对集合进行非空判断, 否则会空指针异常 > 3. 为什么获取所有的属性分组, 包括自己 如果排除自己, 自己关联过的属性也会显示在未关联可选项上, 不合理 > 4. 注意: 必须是同一个分类, 没有关联过的属性 # 核心代码 ```java @RequestMapping("/{attrGroupId}/noattr/relation") public R getNoRelationAttrByAttrGroup(@RequestParam Map<String, Object> params, @PathVariable("attrGroupId") Long attrGroupId) { PageUtils page = attrGroupService.getOtherAttrsByAttrGroup(params, attrGroupId); return R.ok().put("page", page); } ``` ```java @Override public PageUtils getOtherAttrsByAttrGroup(Map<String, Object> params, Long attrGroupId) { // 目标: 找到没有关联任何属性分组的属性 // 1. 获取该属性分组对应的分类 AttrGroupEntity attrGroup = this.baseMapper.selectById(attrGroupId); Long catelogId = attrGroup.getCatelogId(); // 获取分类ID List<AttrGroupEntity> attrGroupEntities = getAttrGroupsByCatId(null, catelogId); // 3. 创建一个集合, 存储所有的关联过属性分组的属性ID 获取这些属性分组下关联的属性 List<Long> relayedAttrIds = relationService .getAllAttrIdsByAttrGroups(attrGroupEntities .stream() .map(AttrGroupEntity::getAttrGroupId).toArray(Long[]::new)); return attrService.getAttrsCondition(params, catelogId, relayedAttrIds); } ``` ```java public List<Long> getAllAttrIdsByAttrGroups(Long... attrGroupIds) { LambdaQueryWrapper<AttrAttrgroupRelationEntity> wrapper = new LambdaQueryWrapper<>(); wrapper.in(AttrAttrgroupRelationEntity::getAttrGroupId, Arrays.asList(attrGroupIds)); // 找到所有的属性分组与属性的关系 List<AttrAttrgroupRelationEntity> relationEntities = this.baseMapper.selectList(wrapper); // 返回所有的属性ID return relationEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList()); } ``` ```java @Override public PageUtils getAttrsCondition(Map<String, Object> params, Long catelogId, List<Long> relayedAttrIds) { // 获取检索关键子 String key = (String) params.get("key"); LambdaQueryWrapper<AttrEntity> wrapper = new LambdaQueryWrapper<>(); // 同一分类 wrapper.eq(AttrEntity::getCatelogId, catelogId); // 不在被关联的属性中 if (relayedAttrIds != null && !relayedAttrIds.isEmpty()) // 避免空指针异常 wrapper.notIn(AttrEntity::getAttrId, relayedAttrIds); // 检索查询 if (StringUtils.isNotBlank(key)) { wrapper.and(wp -> wp.eq(AttrEntity::getAttrId, key) .or() .like(AttrEntity::getAttrName, key)); } // 排除销售属性 wrapper.ne(AttrEntity::getAttrType, ProductConstant.Attr.ATTR_SALE.getCode()); IPage<AttrEntity> page = this.page(new Query<AttrEntity>().getPage(params), wrapper); return new PageUtils(page); } ```