第195天学习打卡(项目 谷粒商城 37新增商品 获取分类下所有属性及分组)

### 新增商品 获取分类下所有属性及分组

**gulimall.product.vo**

**AttrGroupWithAttrsVo.java**

```java
package com.doudou.gulimall.product.vo;


import com.doudou.gulimall.product.entity.AttrEntity;
import lombok.Data;

import java.util.List;
@Data
public class AttrGroupWithAttrsVo {


/**
* 分组id
*/

private Long attrGroupId;
/**
* 组名
*/
private String attrGroupName;
/**
* 排序
*/
private Integer sort;
/**
* 描述
*/
private String descript;
/**
* 组图标
*/
private String icon;
/**
* 所属分类id
*/
private Long catelogId;

private List<AttrEntity> attrs;

}

```

![image-20210722220722636](https://gitee.com/doudoutj111/tj/raw/master/img/image-20210722220722636.png)

```java
package com.doudou.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.doudou.gulimall.product.entity.AttrEntity;
import com.doudou.gulimall.product.service.AttrAttrgroupRelationService;
import com.doudou.gulimall.product.service.AttrService;
import com.doudou.gulimall.product.service.CategoryService;
import com.doudou.gulimall.product.vo.AttrGroupRelationVo;
import com.doudou.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.doudou.gulimall.product.entity.AttrGroupEntity;
import com.doudou.gulimall.product.service.AttrGroupService;
import com.doudou.common.utils.PageUtils;
import com.doudou.common.utils.R;

 

/**
* 属性分组
*
* @author doudoutj111
*
* @date 2021-06-19 19:40:52
*/
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
@Autowired
private AttrGroupService attrGroupService;
@Autowired
private CategoryService categoryService;
@Autowired
AttrService attrService;
@Autowired
AttrAttrgroupRelationService relationService;

//product/attrgroup/attr/relation
@PostMapping("/attr/relation")
public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){
relationService.saveBatch(vos);
return R.ok();

}

//product/attrgroup/{catelogId}/withattr
@GetMapping("/{catelogId}/withattr")
public R getAttrGroupWithAttrs(@PathVariable("catelogId")Long catelogId){
//1.查出当前分类下的所有属性分组
//2.查出每个属性分组的所有属性
List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
return R.ok().put("data",vos);

}


//查询分组关联的属性
//product/attrgroup/{attrgroupId}/attr/relation
@GetMapping("/{attrgroupId}/attr/relation")
public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
List<AttrEntity> entities = attrService.getRelationAttr(attrgroupId);
return R.ok().put("data",entities);


}

/**
* 响应product/attrgroup/{attrgroupId}/noattr/relation
* 获取属性分组里面还没有关联的本分类里面的其他基本属性,方便添加新的关联
* @param attrgroupId 属性分组ID
* @param params 分页基本信息
* @return 当前分组未关联属性的分页内容
*/
@GetMapping("/{attrgroupId}/noattr/relation")
public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
@RequestParam Map<String, Object> params){
PageUtils page = attrService.getNoRelationAttr(params,attrgroupId);
return R.ok().put("page",page);
}

/**
* 删除操作
* @param vos
* @return
*/
@PostMapping("/attr/relation/delete")
public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos){
attrService.deleteRelation(vos);
return R.ok();


}


/**
* 列表
*/
@RequestMapping("/list/{catelogId}")
//@RequiresPermissions("product:attrgroup:list")
public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId){
// PageUtils page = attrGroupService.queryPage(params);
PageUtils page = attrGroupService.queryPage(params,catelogId);


return R.ok().put("page", page);
}


/**
* 信息
*/
@RequestMapping("/info/{attrGroupId}")
// @RequiresPermissions("product:attrgroup:info")
public R info(@PathVariable("attrGroupId") Long attrGroupId){
AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
Long catelogId = attrGroup.getCatelogId();
Long[] path = categoryService.findCatelogPath(catelogId);

attrGroup.setCatelogPath(path);

return R.ok().put("attrGroup", attrGroup);
}

/**
* 保存
*/
@RequestMapping("/save")
// @RequiresPermissions("product:attrgroup:save")
public R save(@RequestBody AttrGroupEntity attrGroup){
attrGroupService.save(attrGroup);

return R.ok();
}

/**
* 修改
*/
@RequestMapping("/update")
// @RequiresPermissions("product:attrgroup:update")
public R update(@RequestBody AttrGroupEntity attrGroup){
attrGroupService.updateById(attrGroup);

return R.ok();
}

/**
* 删除
*/
@RequestMapping("/delete")
// @RequiresPermissions("product:attrgroup:delete")
public R delete(@RequestBody Long[] attrGroupIds){
attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

return R.ok();
}

}

```

 

```java
package com.doudou.gulimall.product.service.impl;

import com.doudou.gulimall.product.entity.AttrEntity;
import com.doudou.gulimall.product.service.AttrService;
import com.doudou.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doudou.common.utils.PageUtils;
import com.doudou.common.utils.Query;

import com.doudou.gulimall.product.dao.AttrGroupDao;
import com.doudou.gulimall.product.entity.AttrGroupEntity;
import com.doudou.gulimall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
@Autowired
AttrService attrService;
@Override
public PageUtils queryPage(Map<String, Object> params) {
IPage<AttrGroupEntity> page = this.page(
new Query<AttrGroupEntity>().getPage(params),
new QueryWrapper<AttrGroupEntity>()
);

return new PageUtils(page);
}

@Override
public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
String key = (String) params.get("key");
//select * from pms_attr_group where catelog_id ? and (attr_group_id=key or attr_group_name like %key%)
QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
if(!StringUtils.isEmpty(key)){
wrapper.and((obj)->{
obj.eq("attr_group_id",key).or().like("attr_group_name",key);
});
}
if(catelogId == 0){
IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
return new PageUtils(page);
}else{
wrapper.eq("catelog_id",catelogId);

IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
return new PageUtils(page);
}

}

/**
* 根据分类id查出所有的分组以及这些组里面的属性
*
* @param catelogId
* @return
*/

@Override
public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
//1.查询分组信息
List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
//2.查询所有属性
List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map(group -> {
AttrGroupWithAttrsVo attrsVo = new AttrGroupWithAttrsVo();
BeanUtils.copyProperties(group,attrsVo);
List<AttrEntity> attrs = attrService.getRelationAttr(attrsVo.getAttrGroupId());
attrsVo.setAttrs(attrs);
return attrsVo;
}).collect(Collectors.toList());
return collect;

}

}

```

B站学习网址[全网最强电商教程《谷粒商城》对标阿里P6/P7,40-60万年薪_哔哩哔哩_bilibili](https://www.bilibili.com/video/BV1np4y1C7Yf?p=86&spm_id_from=pageDriver)

上一篇:JAVA 框架 / MYBATIS / MYBATIS系列教材 (一)- 基础 - 入门教程


下一篇:dvwa-javascript