✅P81_查询分组未关联的属性
2024/2/13大约 2 分钟
获取属性分组没有关联的其他属性
接口信息
接口地址
GET:/product/attrgroup/{attrgroupId}/noattr/relation
接口描述
未关联属性要属于属性组所属分类下的属性集合中
未关联属性属于该分类其它分组未关联的属性和该属性组已关联属性之外的属性集合中
关联属性都必须是基本属性,销售属性不用关联分组
请求参数
{
   page: 1,//当前页码
   limit: 10,//每页记录数
   sidx: 'id',//排序字段
   order: 'asc/desc',//排序方式
   key: '华为'//检索关键字
}分页数据
响应数据
{
	"msg": "success",
	"code": 0,
	"page": {
		"totalCount": 3,
		"pageSize": 10,
		"totalPage": 1,
		"currPage": 1,
		"list": [{
			"attrId": 1,
			"attrName": "aaa",
			"searchType": 1,
			"valueType": 1,
			"icon": "aa",
			"valueSelect": "aa;ddd;sss;aaa2",
			"attrType": 1,
			"enable": 1,
			"catelogId": 225,
			"showDesc": 1
		}]
	}
}代码实现
cfmall-product/src/main/java/com/gyz/cfmall/product/controller/AttrGroupController.java
@Autowired
private AttrService attrService;
/**
 * 获取属性分组没有关联的其他属性
 */
@GetMapping(value = "/{attrgroupId}/noattr/relation")
public R attrNoattrRelation(@RequestParam Map<String, Object> params,
                            @PathVariable("attrgroupId") Long attrgroupId) {
    PageUtils page = attrService.getNoRelationAttr(params, attrgroupId);
    return R.ok().put("page", page);
}cfmall-product/src/main/java/com/gyz/cfmall/product/service/impl/AttrServiceImpl.java
@Resource
public AttrAttrgroupRelationDao relationDao;
@Autowired
AttrGroupDao attrGroupDao;
/**
 * @param params      :
 * @param attrgroupId :
 * @return com.gyz.common.utils.PageUtils
 * @Description
 */
@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> groupEntities = attrGroupDao.selectList(new QueryWrapper<AttrGroupEntity>()
            .eq("catelog_id", catelogId));
    //获取到所有的attrGroupId
    List<Long> collect = groupEntities.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> queryWrapper = new QueryWrapper<AttrEntity>()
            .eq("catelog_id", catelogId).eq("attr_type", ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode());
    if (attrIds != null && attrIds.size() > 0) {
        queryWrapper.notIn("attr_id", attrIds);
    }
    //判断是否有参数进行模糊查询
    String key = (String) params.get("key");
    if (!StringUtils.isEmpty(key)) {
        queryWrapper.and((w) -> {
            w.eq("attr_id", key).or().like("attr_name", key);
        });
    }
    IPage<AttrEntity> page = this.page(new Query<AttrEntity>().getPage(params), queryWrapper);
    PageUtils pageUtils = new PageUtils(page);
    return pageUtils;
}测试
机身重量所属分组为空,

在属性分组页面查询“分组未关联的属性”,并进行关联


