gulimall-分布式基础篇-规格参数

文章详细介绍了Gulimall项目中如何处理规格参数的新增、查询分页、详情查询以及更新操作,涉及到VO对象的创建、Controller接口设计、Service实现,以及数据库冗余表的使用来关联属性和属性分组。
摘要由CSDN通过智能技术生成

gulimall-分布式基础篇-规格参数

规则参数用一个冗余表跟属性分组关联

接口里的base是基本属性的意思

在这里插入图片描述

一、规格参数新增

规格参数新增的同时新增规格参数与新增关联关系

Object划分
接口文档

1.1 attrVo

在product模块下创建VO包,再创建attrVo

package com.jyyy.gulimall.product.vo;

import com.jyyy.gulimall.product.entity.AttrEntity;
import lombok.Data;
import lombok.ToString;

@Data
@ToString(callSuper = true)
public class attrVo extends AttrEntity {

    private Long attrGroupId;
    
}

1.2 attrController

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr){
        
		attrService.saveAttr(attr);

        return R.ok();
    }

1.3 attrService

    void saveAttr(AttrVo attr);

1.4 attrServiceImpl


	@Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    /**
     * 保存规格参数同时保存关联关系
     * @param attr
     */
    public void saveAttr(AttrVo attr) {

        AttrEntity attrEntity = new AttrEntity();

        // 保存规格参数
        BeanUtils.copyProperties(attr,attrEntity);
        this.save(attrEntity);

        // 保存关联关系
        AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
        attrAttrgroupRelationEntity.setAttrId(attrEntity .getAttrId());
        attrAttrgroupRelationEntity.setAttrGroupId(attr.getAttrGroupId());
        attrAttrgroupRelationDao.insert(attrAttrgroupRelationEntity);

    }

二、规格参数查询分页

接口文档

2.1 新建AttrRespVo

@Data
public class AttrRespVo extends AttrVo {
    private String catelogName;
    private String groupName;
}

2.2 AttrController

	@RequestMapping("/base/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId){
        PageUtils page = attrService.queryBaseAttrPage(params, catelogId);
        return R.ok().put("page", page);
    }

2.3 AttrServiceImpl

属性分组要是通过冗余表进行关联的,所以要在冗余表查到属性分组id再查名字

 	@Autowired
    private CategoryDao categoryDao;

    @Autowired
    private AttrGroupDao attrGroupDao;
	
	 @Autowired
	 TODO 属性关联表


	@Override
    public PageUtils queryBaseAttrPage(Map<String, Object> params, Long catelogId) {
        QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<>();
        //模糊查询
        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
        );
    	//封装分页数据
        PageUtils pageUtils = new PageUtils(page);

        //查出新增的属性
        List<AttrEntity> records = page.getRecords();
        List<AttrRespVo> respVos = records.stream().map((attrEntity) -> {
            AttrRespVo attrRespVo = new AttrRespVo();
            BeanUtils.copyProperties(attrEntity, attrRespVo);
            CategoryEntity categoryEntity = categoryDao.selectOne(new QueryWrapper<CategoryEntity>().eq("cat_id", attrEntity.getCatelogId()));
            if (categoryEntity != null) {
                attrRespVo.setCatelogName(categoryEntity.getName());
            }

            AttrAttrgroupRelationEntity attrgroupRelationEntity = attrAttrgroupRelationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId()));
            if (attrgroupRelationEntity != null) {
                AttrGroupEntity attrGroupEntity = attrGroupDao.selectOne(new QueryWrapper<AttrGroupEntity>().eq("attr_group_id", attrgroupRelationEntity.getAttrGroupId()));
                attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
            }

            return attrRespVo;
        }).collect(Collectors.toList());
        // 把新的数据传送过去
        pageUtils.setList(respVos);
        return pageUtils;
    }

三、查询属性详情

接口文档

3.1 修改AttrRespVo

所属分类路径回显
在这里插入图片描述

package com.jyyy.gulimall.product.vo;

import lombok.Data;
import lombok.ToString;

@Data
@ToString(callSuper = true)
public class AttrRespVo extends AttrVo{

    private String catelogName;

    private String groupName;

    private Long[]  catelogPath;

}

3.2 AttrController

	@RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
		AttrRespVo attrRespVo = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", attrRespVo);
    }

3.3 AttrServiceImpl

	@Override
    public AttrRespVo getAttrInfo(Long attrId) {
        AttrRespVo respVo = new AttrRespVo();
        AttrEntity attrEntity = this.getById(attrId);
        BeanUtils.copyProperties(attrEntity,respVo);

        //1.设置属性分组信息
        AttrAttrgroupRelationEntity attrAttrgroupRelation = attrAttrgroupRelationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>()
                .eq("attr_id", attrId));
        if(attrAttrgroupRelation != null){
            respVo.setAttrGroupId(attrAttrgroupRelation.getAttrGroupId());
            AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrAttrgroupRelation.getAttrId());
            if(attrGroupEntity!= null){
                respVo.setGroupName(attrGroupEntity.getAttrGroupName());
            }
        }
        //2.设置分类信息
       Long catelogId =  attrEntity.getCatelogId();
        Long[] catelogPath = categoryService.findCatelogPath(catelogId);
        respVo.setCatelogPath(catelogPath);

        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        if(categoryEntity!=null){
            respVo.setCatelogName(categoryEntity.getName());

        }
        return respVo;
    }

四、修改属性

接口文档

4.1 在AttrController中我们进行新增相应的方法

    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateAttr(attr);

        return R.ok();
    }

4.2 AttrServiceImpl

	//保存时,要修改两张表   attr这张表和attrattrgroupRelationEntity这两个表
	@Transactional
    @Override
    public void updateAttr(AttrVo attr) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attr, attrEntity);
        this.updateById(attrEntity);

        AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
        relationEntity.setAttrGroupId(attr.getAttrGroupId());
        relationEntity.setAttrId(attr.getAttrId());

        //判断是新增还是删除
        Integer count = attrAttrgroupRelationDao.selectCount(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attr.getAttrId()));
        if (count > 0){
            attrAttrgroupRelationDao.update(relationEntity, new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attr.getAttrId()));
        }else{
            attrAttrgroupRelationDao.insert(relationEntity);
        }

    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值