谷粒商城第五天(属性分组以及品牌分类关联)

前端页面就不说了 直接使用给的资源  我们先看新增分组

当我们点击新增后

新增分组的实现   分析前端发来的请求以及参数

 后台代码(Controller)

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

将基本的信息展示出来分析前端的代码(当我们只有点击了第三级的时候才会展示对应的分组信息,否则就查询所有的)

 后端代码Controller(创建一个满足需求的分页方法)

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

        PageUtils page = attrGroupService.queryPage(params,catelogId);

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

实现类(当我们分页查询的时候可能会带这查询条件(key)先判断key是否为空  如果不为空我们就在条件加上key的值,然后判断catelogId是否==0 如果为0我们就查询所有,如果不为0 我们就只查询此catelogId的属性分组的数据)

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

修改分组的实现

首先是数据的回显

 分析前端发送的请求以及参数

后端代码的实现

首先在CategoryEntity中添加一个属性

 在Controller中(我们需要查询当前分类id的完整路径)

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

        Long catelogId = attrGroup.getCatelogId();
        //查找当前id的完整路径
        Long[] catelogPath = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

Category的实现类

// 225 25 2
    private List<Long> findParentPath(Long catelogId,List<Long> paths){
        paths.add(catelogId);
        CategoryEntity byId = this.getById(catelogId);
        if(byId.getParentCid() != 0){
            findParentPath(byId.getParentCid(),paths);
        }
        return paths;
    }

    //递归查找所有菜单的子菜单
    private List<CategoryEntity> getChildren(CategoryEntity root,List<CategoryEntity> all){
        List<CategoryEntity> children = all.stream().filter((categoryEntity) -> {
            return categoryEntity.getParentCid() == root.getCatId();
        }).map(categoryEntity -> {
            //1.找到子菜单
            categoryEntity.setChildren(getChildren(categoryEntity, all));
            return categoryEntity;
        }).sorted((menu1, menu2) -> {
            //2.菜单的排序
            return (menu1.getSort() == null ? 0:menu1.getSort()) - (menu2.getSort() == null?0:menu2.getSort());
        }).collect(Collectors.toList());
        return children;
    }

修改分组的实现

 后端Controller的实现

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

        return R.ok();
    }

品牌关联以及级联的更新

当我们点击关联分类的时候会发送一个请求

 后端Controller代码(获取请求参数的值 然后根据brandId进行查询)

/**
     * 获取品牌关联的分类列表
     */
    @GetMapping("/catelog/list")
    //@RequiresPermissions("product:brand:list")
    public R cateloglist(@RequestParam("brandId") Long brandId){
        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("brand_id",brandId);
        List<CategoryBrandRelationEntity> data = categoryBrandRelationService.list(wrapper);
        return R.ok().put("data", data);
    }

新增关联分类

 后端代码Controller实现

 /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.saveDetail(categoryBrandRelation);

        return R.ok();
    }

实现类(我们需要根据品牌id和分类id分别获取到品牌名和分类名保存到数据库中)

@Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        //查询详细的名字
        BrandEntity brandEntity = brandService.getById(brandId);
        String brandName = brandEntity.getName();
        CategoryEntity categoryEntity = categoryService.getById(catelogId);
        String categoryName = categoryEntity.getName();
        categoryBrandRelation.setBrandName(brandName);
        categoryBrandRelation.setCatelogName(categoryName);
        this.save(categoryBrandRelation);
    }

 

然后我们要做的是关联数据要和品牌数据和分类数据保持同步

当品牌修改的时候(如果品牌名称改变了  那么关联分类中的品牌名也要随之改变)

品牌Controller

/**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:brand:update")
    public R update(@Validated(value = {UpdateGroup.class}) @RequestBody BrandEntity brand){
        //进行同步修改
		brandService.updateDetail(brand);
        return R.ok();
    }

品牌的实现类

 //同步修改冗余字段
    @Transactional
    @Override
    public void updateDetail(BrandEntity brand) {
        //保证冗余数据一样
        this.updateById(brand);
        //判断更新字段是否有名字
        if(!StringUtils.isEmpty(brand.getName())){
            categoryBrandRelationService.updateBrand(brand.getBrandId(),brand.getName());
        }
        //TODO 品牌同步级联数据

    }

调用了品牌关联分类的实现类 (只修改品牌id和品牌名称)       

 /**
     * 同步冗余字段名称
     * @param brandId
     * @param name
     */
    @Override
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandId(brandId);
        categoryBrandRelationEntity.setBrandName(name);
        this.update(categoryBrandRelationEntity,new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));

    }

同样的分类也是一样的

controller

 /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity category){
		categoryService.updateCascade(category);
        return R.ok();
    }

 实现类

 /**
     * 同步级联数据
     * @param category
     */
    @Transactional
    @Override
    public void updateCascade(CategoryEntity category) {
        this.updateById(category);
        if(!StringUtils.isEmpty(category.getName())) {
            categoryBrandRelationService.updateCategory(category.getCatId(), category.getName());
        }
        //TODO  分类级联数据同步
    }

在品牌关联分类里同步分类信息(第二种方法写sql语句的)

@Override
    public void updateCategory(Long catId, String name) {
        this.baseMapper.updateCategory(catId,name);
           /* CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
            categoryBrandRelationEntity.setCatelogId(catId);
            categoryBrandRelationEntity.setCatelogName(name);
            this.update(categoryBrandRelationEntity,
                    new UpdateWrapper<CategoryBrandRelationEntity>().eq("catelog_id",catId));*/
    }

 SQL语句

 <update id="updateCategory" parameterType="com.atguigu.gulimall.product.entity.CategoryBrandRelationEntity">
        UPDATE pms_category_brand_relation SET catelog_name =#{name} WHERE catelog_id=#{catId}
    </update>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值