谷粒商城—商品服务—三级分类(增删改查)(45~58)

一:实现目标:

                1)三级分类:

                2)表结构:

                3)

一.编写出 查出所有分类,以及子分类。以 树形结构 组装起来 的接口:

                1)创建用于 数据传输 的 VO 类(CategoryVo):

@Data
public class CategoryVo {
    private static final long serialVersionUID = 1L;

    /**
     * 分类id
     */
    private Long catId;
......
    /**
     * 保存此菜单 所有子分类
     *
     * @JsonInclude(value = JsonInclude.Include.NON_EMPTY) 字段不为空时候才返回
     */
    @JsonInclude(value = JsonInclude.Include.NON_EMPTY)
    private List<CategoryVo> categoryChildrenVoList;

                2)Controller:

    /**
     * 查出所有分类,以及子分类,以 树形结构 组装起来
     */
    @RequestMapping("/listWithTree")
    public R listWithTree(@RequestParam Map<String, Object> params) {
        List<CategoryEntity> listWithTree = categoryService.listWithTree();
        return R.ok().put("data", listWithTree);
    }

                3)service:(1个实现类,一个查找子类方法)

    @Override
    public List<CategoryVo> listWithTree() {
        //1.查出所有分类
        List<CategoryEntity> list = categoryDao.selectList(null);
        List<CategoryVo> categoryVoList = list.stream().map(categoryEntity -> {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(categoryEntity, categoryVo);
            return categoryVo;
        }).collect(Collectors.toList());

        //2.组装成父子的树形解构
        //2.1 查出所有的一级分类
        //2.2 查出子分类
        List<CategoryVo> collect = categoryVoList.stream()
                .filter(categoryVo -> {
                    return categoryVo.getCatLevel() == 1;
                }).map(categoryVo -> {
                    List<CategoryVo> childrensByCaregory = getChildrensByCaregory(categoryVo, categoryVoList);
                    categoryVo.setCategoryChildrenVoList(childrensByCaregory);
                    return categoryVo;
                }).sorted((o1, o2) -> {
                    return (o1.getSort() == null ? 0 : o1.getSort()) - (o2.getSort() == null ? 0 : o2.getSort());
                }).collect(Collectors.toList());

        return collect;
    }

    private List<CategoryVo> getChildrensByCaregory(CategoryVo baseCategoryVo, List<CategoryVo> allList) {

        List<CategoryVo> collect = allList.stream()
                .filter(categoryVo -> {
                    return categoryVo.getParentCid().equals(baseCategoryVo.getCatId());
                }).map(categoryVo -> {
                    //使用方法的 递归,找当前商品的子菜单
                    categoryVo.setCategoryChildrenVoList(getChildrensByCaregory(categoryVo, allList));
                    return categoryVo;
                }).sorted((CategoryVo o1, CategoryVo o2) -> {
                    return (o1.getSort() == null ? 0 : o1.getSort()) - (o2.getSort() == null ? 0 : o2.getSort());
                }).collect(Collectors.toList());

        return collect;

    }

                4)测试:http://127.0.0.1:88/api/product/category/listWithTree



 

二.设置 路由规则:

                1)修改网关项目的 yml:设置跨域:访问(http://127.0.0.1:9527/api/product/category/listWithTree

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 114.215.173.88:8848

    gateway:
      routes:
        - id: product_route
          uri: lb://gulimail-product
          predicates:
            - Path=/api/product/**
          filters:
            - RewritePath=/api/(?<segment>/?.*), /$\{segment}

        - id: admin_route
          uri: lb://renren-fast
          predicates:
            - Path=/api/**
          filters:
            - RewritePath=/api/(?<segment>/?.*), /renren-fast/$\{segment}




 

 四.批量删除商品(逻辑删除):

                1)设置删除为 逻辑删除:
                             a:在 Entity 类,标识 有效/无效 字段,加上注解:@TableLogic:

    /**
     * 是否显示[0-不显示,1显示]
     * 
     *     @TableLogic : 标识 逻辑删除
     */
    @TableLogic
    private Integer showStatus;

                             b:在 yml 文件中,规定 。0:逻辑删除 / 1:逻辑不删除 

mybatis-plus:
  mapper-locations: classpath*:mapper/**/*.xml
  #主键自增
  global-config:
    db-config:
      id-type: auto
      logic-delete-value: 0
      logic-not-delete-value: 1

                2)发送请求:( http://127.0.0.1:88/api/product/category/delete

                 3)Controller:

    /**
     * 删除
     */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public R delete(@RequestBody Long[] catIds) {

        //检查当前的菜单,是否被被的地方引用
        categoryService.removeMenuByIds(Arrays.asList(catIds));

        return R.ok();
    }

                4)Service:

    @Override
    public void removeMenuByIds(List<Long> asList) {
        //TODO: 1.检查当前删除的菜单,是否被别的地方引用
        baseMapper.deleteBatchIds(asList);
    }

                5)Dao:


    /**
     * 删除(根据ID 批量删除)
     *
     * @param idList 主键ID列表(不能为 null 以及 empty)
     */
    int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);

                6)产生的 SQL:

UPDATE pms_category 
SET show_status = 0 
WHERE
	cat_id IN ( 1, 2, 3 ) 
	AND show_status =1
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
gulimall_pms 商品 drop table if exists pms_attr; drop table if exists pms_attr_attrgroup_relation; drop table if exists pms_attr_group; drop table if exists pms_brand; drop table if exists pms_category; drop table if exists pms_category_brand_relation; drop table if exists pms_comment_replay; drop table if exists pms_product_attr_value; drop table if exists pms_sku_images; drop table if exists pms_sku_info; drop table if exists pms_sku_sale_attr_value; drop table if exists pms_spu_comment; drop table if exists pms_spu_images; drop table if exists pms_spu_info; drop table if exists pms_spu_info_desc; /*==============================================================*/ /* Table: pms_attr */ /*==============================================================*/ create table pms_attr ( attr_id bigint not null auto_increment comment '属性id', attr_name char(30) comment '属性名', search_type tinyint comment '是否需要检索[0-不需要,1-需要]', icon varchar(255) comment '属性图标', value_select char(255) comment '可选值列表[用逗号分隔]', attr_type tinyint comment '属性类型[0-销售属性,1-基本属性,2-既是销售属性又是基本属性]', enable bigint comment '启用状态[0 - 禁用,1 - 启用]', catelog_id bigint comment '所属分类', show_desc tinyint comment '快速展示【是否展示在介绍上;0-否 1-是】,在sku中仍然可以调整', primary key (attr_id) ); alter table pms_attr comment '商品属性'; /*==============================================================*/ /* Table: pms_attr_attrgroup_relation */ /*==============================================================*/ create table pms_attr_attrgroup_relation ( id bigint not null auto_increment comment 'id', attr_id bigint comment '属性id', attr_group_id bigint comment '属性分组id', attr_sort int comment '属性组内排序', primary key (id) ); alter table pms_attr_attrgroup_relation comment '属性&

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值