电商项目8:平台属性
1、后端
1.1、属性分组模糊查询
需要改造。当前端传0时候。模糊查询功能有点问题
AttrGroupServiceImpl
@Override
public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
// 当前端传了key
String key = (String)params.get("key");
if (!StringUtils.isEmpty(key)){
// select * from pms_attr_group where catelog_id = ? and (attr_gruop_id = ? or attr_group_name like %%)
wrapper.and((obj)->{
obj.eq("attr_group_id",key).or().like("attr_group_name",key);
});
}
// 当前端的catelogId为0,没传的时候,查询所有
if (catelogId == 0){
return new PageUtils(this.page(new Query<AttrGroupEntity>().getPage(params),wrapper));
}
// 当前端cateLogId不为0,则
wrapper.eq("catelog_id",catelogId);
IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),wrapper);
return new PageUtils(page);
}
1.2、商品属性新增功能:保存关联关系
没有插入关联关系,需重写方法
1、创建vo文件夹。
接收页面传递来的数据,封装对象
将业务处理完成的对象,封装成页面要用的数据
AttrVo
package com.ljs.gulimall.product.vo;
import lombok.Data;
@Data
public class AttrVo {
private Long attrId;
/**
* 属性名
*/
private String attrName;
/**
* 是否需要检索[0-不需要,1-需要]
*/
private Integer searchType;
/**
* 值类型[0-为单个值,1-可以选择多个值]
*/
private Integer valueType;
/**
* 属性图标
*/
private String icon;
/**
* 可选值列表[用逗号分隔]
*/
private String valueSelect;
/**
* 属性类型[0-销售属性,1-基本属性,2-既是销售属性又是基本属性]
*/
private Integer attrType;
/**
* 启用状态[0 - 禁用,1 - 启用]
*/
private Long enable;
/**
* 所属分类
*/
private Long catelogId;
/**
* 快速展示【是否展示在介绍上;0-否 1-是】,在sku中仍然可以调整
*/
private Integer showDesc;
/**
* 属性分组id
*/
private Long attrGroupId;
}
AttrController
/**
* 保存
*/
@RequestMapping("/save")
// @RequiresPermissions("product:attr:save")
public R save(@RequestBody AttrVo attr){
attrService.saveDetail(attr);
return R.ok();
}
AttrService
void saveDetail(AttrVo attr);
AttrServiceImpl
##@Transactional生效的前提是
@EnableTransactionManagement注解在配置类上。并且扫描到指定的dao层
@Transactional一般用于一次多个写操作
@Transactional
@Override
public void saveDetail(AttrVo attr) {
// 1、保存分组信息
AttrEntity attrEntity = new AttrEntity();
BeanUtils.copyProperties(attr,attrEntity);
this.save(attrEntity);
// 2、保存关联关系
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
relationEntity.setAttrGroupId(attr.getAttrGroupId());
relationEntity.setAttrId(attrEntity.getAttrId());
relationService.save(relationEntity);
}
1.3、查询规格参数列表
AttrController
@GetMapping("/base/list/{cateLogId}")
public R list(@RequestParam Map<String,Object> params,@PathVariable("cateLogId") Long cateLogId){
PageUtils page = attrService.queryBaseAttrPage(params,cateLogId);
return R.ok().put("page",page);
}
AttrService
PageUtils queryBaseAttrPage(Map<String, Object> params, Long cateLogId);
AttrServiceImpl
@Override
public PageUtils queryBaseAttrPage(Map<String, Object> params, Long cateLogId) {
QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<AttrEntity>();
if (cateLogId != 0){
queryWrapper.eq("catelog_id",cateLogId);
}
String key = (String) params.get("key");
if (!StringUtils.isEmpty(key)){
// attr_id attr_name
queryWrapper.and((wrapper) -> {
wrapper.eq("attr_id",key).or().like("attr_name",key);
});
}
IPage<AttrEntity> page = this.page(
new Query<AttrEntity>().getPage(params),
queryWrapper
);
return new PageUtils(page);
}
这时候所属分类和所属分组没有展示出来
实现类需要改造
@Override
public PageUtils queryBaseAttrPage(Map<String, Object> params, Long cateLogId) {
QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<AttrEntity>();
if (cateLogId != 0){
queryWrapper.eq("catelog_id",cateLogId);
}
String key = (String) params.get("key");
if (!StringUtils.isEmpty(key)){
// attr_id attr_name
queryWrapper.and((wrapper) -> {
wrapper.eq("attr_id",key).or().like("attr_name",key);
});
}
IPage<AttrEntity> page = this.page(
new Query<AttrEntity>().getPage(params),
queryWrapper
);
List<AttrEntity> records = page.getRecords();
List<AttrRespVo> attrRespVos = records.stream().map(attrEntity -> {
AttrRespVo attrRespVo = new AttrRespVo();
BeanUtils.copyProperties(attrEntity,attrRespVo);
// 根据关联关系表获取所属分组信息
AttrAttrgroupRelationEntity relationEntity = relationService.getOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId()));
if (relationEntity != null){
Long attrGroupId = relationEntity.getAttrGroupId();
AttrGroupEntity attrGroupEntity = attrGroupService.getOne(new QueryWrapper<AttrGroupEntity>().eq("attr_group_id", attrGroupId));
if (attrGroupEntity != null){
attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
}
}
// 获取所属分类信息
CategoryEntity categoryEntity = categoryService.getOne(new QueryWrapper<CategoryEntity>().eq("cat_id", attrEntity.getCatelogId()));
if (categoryEntity != null){
attrRespVo.setCatelogName(categoryEntity.getName());
}
return attrRespVo;
}).collect(Collectors.toList());
PageUtils pageUtils = new PageUtils(page);
pageUtils.setList(attrRespVos);
return pageUtils;
}
1.4、编辑规格参数
AttrController
/**
* 信息
*/
@RequestMapping("/info/{attrId}")
// @RequiresPermissions("product:attr:info")
public R info(@PathVariable("attrId") Long attrId){
AttrRespVo attrRespVo = attrService.getInfoById(attrId);
return R.ok().put("attr", attrRespVo);
}
AttrService
AttrRespVo getInfoById(Long attrId);
AttrServiceImpl
@Override
public AttrRespVo getInfoById(Long attrId) {
AttrRespVo attrRespVo = new AttrRespVo();
AttrEntity attrVO = this.getById(attrId);
if (attrVO != null){
BeanUtils.copyProperties(attrVO,attrRespVo);
}
// 1、获取分组信息并设值
AttrAttrgroupRelationEntity relationEntity = relationService.getOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrId));
if (relationEntity != null){
Long groupId = relationEntity.getAttrGroupId();
attrRespVo.setAttrGroupId(groupId);
AttrGroupEntity attrGroupEntity = attrGroupService.getById(groupId);
if (attrGroupEntity != null){
String attrGroupName = attrGroupEntity.getAttrGroupName();
attrRespVo.setGroupName(attrGroupName);
}
}
// 2、获取分类信息并设值
Long catelogId = attrRespVo.getCatelogId();
Map<Integer,Long> categoryMap = new HashMap<>();
Long[] catelogPath = categoryService.findCatelogPath(catelogId,categoryMap);
attrRespVo.setCatelogPath(catelogPath);
return attrRespVo;
}
CategoryServiceImpl
/**
* findCatelogPath
*
* @param catelogId catelogId
* @return Long[]
*/
public Long[] findCatelogPath(Long catelogId,Map<Integer,Long> categoryEntities) {
CategoryEntity categoryEntity = categoryDao.findCatelogPath(catelogId);
if (categoryEntity != null){
Long parentCid = categoryEntity.getParentCid();
Integer catLevel = categoryEntity.getCatLevel();
if (catLevel != null){
categoryEntities.put(catLevel,catelogId);
if (catLevel != 0){
this.findCatelogPath(parentCid,categoryEntities);
}
if (categoryEntities.size() != 0){
List<Long> catelogIdList = new ArrayList<>();
for (int i = 0; i < categoryEntities.size(); i++) {
catelogIdList.add(categoryEntities.get(i+1));
}
return catelogIdList.toArray(new Long[0]);
}
}
}
return new Long[0];
}
CategoryDao
/**
* findCatelogPath
*
* @param catelogId catelogId
* @return CategoryEntity
*/
CategoryEntity findCatelogPath(@Param("catelogId") Long catelogId);
CategoryDao.xml
<select id="findCatelogPath" resultMap="categoryMap">
SELECT parent_cid,cat_level FROM `pms_category`
where cat_id = #{catelogId}
</select>
编辑回显就可以出现三级分类了
但是所属分组这里修改有问题
需要编写修改接口
AttrController
/**
* 修改
*/
@RequestMapping("/update")
// @RequiresPermissions("product:attr:update")
public R update(@RequestBody AttrVo attr){
attrService.updateDetail(attr);
return R.ok();
}
AttrServiceImpl
@Override
public void updateDetail(AttrVo attr) {
// 1、修改分组信息
AttrEntity attrEntity = new AttrEntity();
BeanUtils.copyProperties(attr,attrEntity);
this.updateById(attrEntity);
// 2、修改关联关系
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
relationEntity.setAttrGroupId(attr.getAttrGroupId());
Long attrId = attrEntity.getAttrId();
relationEntity.setAttrId(attrId);
// 3、本身无关联关系。则新增关联关系。如果有关联关系则修改关联关系
int count = relationService.count(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrId));
if (count > 0){
relationService.updateByAttrId(relationEntity);
}else{
relationService.save(relationEntity);
}
}
AttrAttrgroupRelationServiceImpl
/**
* updateByAttrId
*
* @param relationEntity relationEntity
* @return int
*/
public int updateByAttrId(AttrAttrgroupRelationEntity relationEntity) {
return this.baseMapper.updateByAttrId(relationEntity);
}
AttrAttrgroupRelationDao
/**
* updateByAttrId
*
* @param relationEntity relationEntity
* @return int
*/
int updateByAttrId(AttrAttrgroupRelationEntity relationEntity);
AttrAttrgroupRelationDao.xml
<update id="updateByAttrId">
update pms_attr_attrgroup_relation
set attr_group_id = #{attrGroupId}
where attr_id = #{attrId}
</update>
1.5、销售属性维护
1、销售属性分页查询(实现同一个接口,两种路径查询规格规格参数或销售属性)
AttrController
@GetMapping("/{attrType}/list/{cateLogId}")
public R list(@RequestParam Map<String,Object> params,@PathVariable("cateLogId") Long cateLogId,@PathVariable("attrType") String attrType){
PageUtils page = attrService.queryBaseAttrPage(params,cateLogId,attrType);
return R.ok().put("page",page);
}
AttrService
PageUtils queryBaseAttrPage(Map<String, Object> params, Long cateLogId, String attrType);
AttrServiceImpl
queryBaseAttrPage
QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<AttrEntity>().eq("attr_type","base".equalsIgnoreCase(attrType) ? 1 : 0);
2、修改、新增、删除兼容规格参数
ProductEnum.java
package com.ljs.gulimall.common.Enum;
public enum ProductEnum {
SPECIFICATION_PARAMETERS(1,"规格参数"),
SALES_ATTRIBUTES(0,"销售属性");
private final Integer code;
private final String msg;
ProductEnum(Integer code , String msg){
this.code = code;
this.msg = msg;
}
public Integer getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
AttrController.java
/**
* 删除
*/
@RequestMapping("/delete")
// @RequiresPermissions("product:attr:delete")
public R delete(@RequestBody Long[] attrIds){
List<Long> attrIdList = (List<Long>) Arrays.asList(attrIds);
attrService.removeByIds(attrIdList);
// 删除时同时删除关联关系
relationService.removeByAttrIds(attrIdList);
return R.ok();
}
AttrAttrgroupRelationDao.java
/**
* removeByAttrIds
*
* @param attrIdList attrIdList
* @return int
*/
int removeByAttrIds(@Param("list") List<Long> attrIdList);
AttrAttrgroupRelationServiceImpl.java
@Override
public int removeByAttrIds(List<Long> attrIdList) {
return relationDao.removeByAttrIds(attrIdList);
}
AttrServiceImpl
saveDetail
@Transactional
@Override
public void saveDetail(AttrVo attr) {
// 1、保存分组信息
AttrEntity attrEntity = new AttrEntity();
BeanUtils.copyProperties(attr,attrEntity);
this.save(attrEntity);
if (ProductEnum.SPECIFICATION_PARAMETERS.getCode().equals(attrEntity.getAttrType())){
// 2、保存关联关系
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
relationEntity.setAttrGroupId(attr.getAttrGroupId());
relationEntity.setAttrId(attrEntity.getAttrId());
relationService.save(relationEntity);
}
}
updateDetail
@Transactional
@Override
public void updateDetail(AttrVo attr) {
// 1、修改分组信息
AttrEntity attrEntity = new AttrEntity();
BeanUtils.copyProperties(attr,attrEntity);
this.updateById(attrEntity);
if (ProductEnum.SPECIFICATION_PARAMETERS.getCode().equals(attrEntity.getAttrType())){
// 2、修改关联关系
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
relationEntity.setAttrGroupId(attr.getAttrGroupId());
Long attrId = attrEntity.getAttrId();
relationEntity.setAttrId(attrId);
// 3、本身无关联关系。则新增关联关系。如果有关联关系则修改关联关系
int count = relationService.count(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrId));
if (count > 0){
relationService.updateByAttrId(relationEntity);
}else{
relationService.save(relationEntity);
}
}
}
queryBaseAttrPage
@Override
public PageUtils queryBaseAttrPage(Map<String, Object> params, Long cateLogId, String attrType) {
QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<AttrEntity>().eq("attr_type","base".equalsIgnoreCase(attrType) ? 1 : 0);
if (cateLogId != 0){
queryWrapper.eq("catelog_id",cateLogId);
}
String key = (String) params.get("key");
if (!StringUtils.isEmpty(key)){
// attr_id attr_name
queryWrapper.and((wrapper) -> {
wrapper.eq("attr_id",key).or().like("attr_name",key);
});
}
IPage<AttrEntity> page = this.page(
new Query<AttrEntity>().getPage(params),
queryWrapper
);
List<AttrEntity> records = page.getRecords();
List<AttrRespVo> attrRespVos = records.stream().map(attrEntity -> {
AttrRespVo attrRespVo = new AttrRespVo();
BeanUtils.copyProperties(attrEntity,attrRespVo);
if (ProductEnum.SPECIFICATION_PARAMETERS.getCode().equals(attrRespVo.getAttrType())){
// 根据关联关系表获取所属分组信息
AttrAttrgroupRelationEntity relationEntity = relationService.getOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId()));
if (relationEntity != null){
Long attrGroupId = relationEntity.getAttrGroupId();
AttrGroupEntity attrGroupEntity = attrGroupService.getOne(new QueryWrapper<AttrGroupEntity>().eq("attr_group_id", attrGroupId));
if (attrGroupEntity != null){
attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
}
}
}
// 获取所属分类信息
CategoryEntity categoryEntity = categoryService.getOne(new QueryWrapper<CategoryEntity>().eq("cat_id", attrEntity.getCatelogId()));
if (categoryEntity != null){
attrRespVo.setCatelogName(categoryEntity.getName());
}
return attrRespVo;
}).collect(Collectors.toList());
PageUtils pageUtils = new PageUtils(page);
pageUtils.setList(attrRespVos);
return pageUtils;
}
getInfoById
@Override
public AttrRespVo getInfoById(Long attrId) {
AttrRespVo attrRespVo = new AttrRespVo();
AttrEntity attrVO = this.getById(attrId);
if (attrVO != null){
BeanUtils.copyProperties(attrVO,attrRespVo);
}
if (ProductEnum.SPECIFICATION_PARAMETERS.getCode().equals(attrRespVo.getAttrType())){
// 1、获取分组信息并设值
AttrAttrgroupRelationEntity relationEntity = relationService.getOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrId));
if (relationEntity != null){
Long groupId = relationEntity.getAttrGroupId();
attrRespVo.setAttrGroupId(groupId);
AttrGroupEntity attrGroupEntity = attrGroupService.getById(groupId);
if (attrGroupEntity != null){
String attrGroupName = attrGroupEntity.getAttrGroupName();
attrRespVo.setGroupName(attrGroupName);
}
}
}
// 2、获取分类信息并设值
Long catelogId = attrRespVo.getCatelogId();
Map<Integer,Long> categoryMap = new HashMap<>();
Long[] catelogPath = categoryService.findCatelogPath(catelogId,categoryMap);
attrRespVo.setCatelogPath(catelogPath);
return attrRespVo;
}
AttrAttrgroupRelationService.java
int removeByAttrIds(List<Long> attrIdList);
AttrAttrgroupRelationDao
/**
* removeByAttrIds
*
* @param attrIdList attrIdList
* @return int
*/
int removeByAttrIds(@Param("list") List<Long> attrIdList);
AttrAttrgroupRelationDao.xml
<delete id="removeByAttrIds">
delete from
pms_attr_attrgroup_relation
where attr_id
in
<foreach collection="list" item="item" separator="," open="(" close=")">
#{item}
</foreach>
</delete>
1.6、查询分组关联属性
AttrGroupController.java
@Autowired
private AttrService attrService;
// /product/attrgroup/{attrgroupId}/attr/relation
@GetMapping("/{attrgroupId}/attr/relation")
public R getRelation(@PathVariable Long attrgroupId){
List<AttrEntity> list = attrService.getRelationByAttrGroupId(attrgroupId);
return R.ok().put("data",list);
}
AttrServiceImpl.java
@Override
public List<AttrEntity> getRelationByAttrGroupId(Long attrgroupId) {
// 根据attrgroupId获取关联关系表attrId集合
List<AttrAttrgroupRelationEntity> relationEntityList = relationService.list(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrgroupId));
List<Long> attrIdList = relationEntityList.stream().filter(Objects::nonNull).map(
AttrAttrgroupRelationEntity::getAttrId
).collect(Collectors.toList());
// 根据attrId集合查询所有attr信息
Collection<AttrEntity> attrEntities = attrService.listByIds(attrIdList);
if (attrEntities != null && !attrEntities.isEmpty()){
return (List<AttrEntity>) attrEntities;
}
return new ArrayList<AttrEntity>();
}
AttrService.java
List<AttrEntity> getRelationByAttrGroupId(Long attrgroupId);
1.7、属性分组关联关系移除
AttrGroupController
@PostMapping("/attr/relation/delete")
public R deleteRelation(@RequestBody AttrRelationDeleteVo [] vos){
attrGroupService.deleteRelation(vos);
return R.ok();
}
AttrGroupService
void deleteRelation(AttrRelationDeleteVo[] vos);
AttrGroupServiceImpl
@Override
public void deleteRelation(AttrRelationDeleteVo[] vos) {
if (vos == null || vos.length == 0) {
return;
}
List<AttrAttrgroupRelationEntity> relationList = new ArrayList<>();
for (AttrRelationDeleteVo vo : vos) {
AttrAttrgroupRelationEntity entity = new AttrAttrgroupRelationEntity();
entity.setAttrId(vo.getAttrId());
entity.setAttrGroupId(vo.getAttrGroupId());
relationList.add(entity);
}
if (relationList.size() > 0){
attrAttrgroupRelationDao.deleteRelation(relationList);
}
}
AttrAttrgroupRelationDao
/**
* deleteRelation
*
* @param relationList relationList
*/
int deleteRelation(@Param("list") List<AttrAttrgroupRelationEntity> relationList);
AttrAttrgroupRelationDao.xml
<delete id="deleteRelation">
delete from
pms_attr_attrgroup_relation where
<foreach collection="list" item="item" separator=" or ">
(attr_id = #{item.attrId} and attr_group_id = #{item.attrGroupId})
</foreach>
</delete>
1.8、查询分组未关联的属性
查询关联关系和新增关联关系。应该是未关联属性才可以被操作
AttrGroupController
/**
* 获取属性分组没有关联的其他属性
*
* @param attrgroupId attrgroupId
* @param params params
* @return R
*/
// /product/attrgroup/{attrgroupId}/attr/relation
@GetMapping("/{attrgroupId}/noattr/relation")
public R getNoRelation(@PathVariable Long attrgroupId,@RequestParam Map<String, Object> params){
PageUtils page = attrService.getNoRelation(attrgroupId,params);
return R.ok().put("page",page);
}
AttrService
PageUtils getNoRelation(Long attrgroupId, Map<String, Object> params);
AttrServiceImpl
@Override
public PageUtils getNoRelation(Long attrgroupId, Map<String, Object> params) {
// 1、根据分组id查询到所有的分类id
List<AttrGroupEntity> attrGroupEntities = attrGroupService.getBaseMapper().selectList(new QueryWrapper<AttrGroupEntity>().eq("attr_group_id", attrgroupId));
if (attrGroupEntities == null || attrGroupEntities.size() == 0){
return null;
}
List<Long> cateLogIds = attrGroupEntities.stream().map(AttrGroupEntity::getCatelogId).collect(Collectors.toList());
// 2、查询当前分类的所有分组
List<AttrGroupEntity> attrGroupEntityList = attrGroupService.getBaseMapper().selectList(new QueryWrapper<AttrGroupEntity>().in("catelog_id", cateLogIds));
if (attrGroupEntityList == null || attrGroupEntityList.size() == 0){
return null;
}
// 3、查询所有分组的关联属性
List<Long> groupIdList = attrGroupEntityList.stream().map(AttrGroupEntity::getAttrGroupId).collect(Collectors.toList());
if (groupIdList.size() == 0){
return null;
}
List<AttrAttrgroupRelationEntity> relationEntities = relationService.getBaseMapper().selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().in("attr_group_id", groupIdList));
// 4、剔除掉已经存在关联关系的关联属性
if (relationEntities == null || relationEntities.size() == 0){
return null;
}
List<Long> attrIds = relationEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
QueryWrapper<AttrEntity> wrapper = new QueryWrapper<AttrEntity>().in("catelog_id", cateLogIds).notIn("attr_id", attrIds).eq("attr_type",ProductEnum.SPECIFICATION_PARAMETERS.getCode());
String key = (String)params.get("key");
if (!StringUtils.isEmpty(key)){
wrapper.and(item -> {
item.eq("attr_id",key).or().like("attr_name",key);
});
}
IPage<AttrEntity> page = this.page(new Query<AttrEntity>().getPage(params), wrapper);
return new PageUtils(page);
}
1.9、新增分组关联关系
AttrGroupController
///attr/relation
@PostMapping("/attr/relation")
public R addRelation(@RequestBody AttrRelationVo[] vos){
if (vos != null && vos.length != 0){
List<AttrRelationVo> collect = Arrays.stream(vos).collect(Collectors.toList());
List<AttrAttrgroupRelationEntity> entityList = collect.stream().map(
item -> {
AttrAttrgroupRelationEntity entity = new AttrAttrgroupRelationEntity();
BeanUtils.copyProperties(item,entity);
return entity;
}
).collect(Collectors.toList());
relationService.saveBatch(entityList);
return R.ok();
}
return R.error();
}
新增关联关系可以使用了。
至此新增,编辑时需要判断分组id是否存在。再保存关联关系。避免空指针异常