谷粒商城实战笔记-79-商品服务-API-平台属性-销售属性维护


这一节的主要内容是:

  • 销售属性列表查询接口的开发
  • 将代码中的属性类型字面量用常量枚举代替,涉及列表查询、属性保存、属性修改、属性查询接口

在这里插入图片描述

一,列表接口开发

销售属性的列表接口是/api/product/attr/sale/list,后台暂无这个接口实现,但其和规格参数的接口逻辑高度相似,所以要复用这个接口。
在这里插入图片描述

1,新建常量类

在common类中新建常量类ProductConstants

在这里插入图片描述

package com.atguigu.common.constant;

public class ProductConstant {
    public enum AttrEnum {
        BASE("base", 1), SALES("sales", 0);
        private String type;
        private int code;
        AttrEnum(String type, int code) {
            this.code = code;
            this.type = type;
        }

        public int getCode() {
            return code;
        }

        public String getType() {
            return type;
        }
    }
}

2,路径参数识别规格参数和销售属性

在销售属性列表查询的时候,无需查询分组信息,因为销售属性没有分组信息。

在这里插入图片描述

{
        QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<>();

        if(catelogId != 0){
            queryWrapper.eq("catelog_id",catelogId);
        }

        String key = (String) params.get("key");
        if(StrUtil.isNotEmpty(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);

            if (ProductConstant.AttrEnum.BASE.getType().equals(attrType)) {
                //1、设置分类和分组的名字
                AttrAttrgroupRelationEntity attrId = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId()));
                if (attrId != null && attrId.getAttrGroupId() != null) {
                    AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrId.getAttrGroupId());
                    attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
                }
            }

            CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId());
            if (categoryEntity != null) {
                attrRespVo.setCatelogName(categoryEntity.getName());
            }
            return attrRespVo;
        }).collect(Collectors.toList());

        pageUtils.setList(respVos);
        return pageUtils;

    }

二,其他接口

保存属性的接口。

    public void saveAttr(AttrVo attr) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attr,attrEntity);
        //1、保存基本数据
        this.save(attrEntity);
        if (ProductConstant.AttrEnum.BASE.getCode() == attr.getAttrType()) {
            //2、保存关联关系
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            relationEntity.setAttrGroupId(attr.getAttrGroupId());
            relationEntity.setAttrId(attrEntity.getAttrId());
            relationDao.insert(relationEntity);
        }
    }

更新属性的接口。

    public void updateAttr(AttrVo attr) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attr,attrEntity);
        this.updateById(attrEntity);

        if (ProductConstant.AttrEnum.BASE.getCode() == attrEntity.getAttrType()) {
            //1、修改分组关联
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();

            relationEntity.setAttrGroupId(attr.getAttrGroupId());
            relationEntity.setAttrId(attr.getAttrId());

            Integer count = relationDao.selectCount(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attr.getAttrId()));
            if (count > 0) {
                relationDao.update(relationEntity, new UpdateWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attr.getAttrId()));
            } else {
                relationDao.insert(relationEntity);
            }
        }
    }

查询属性的接口。

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

        if (ProductConstant.AttrEnum.BASE.getCode() == attrEntity.getAttrType()) {
            //1、设置分组信息
            // 要通过中间表得到
            AttrAttrgroupRelationEntity attrgroupRelation = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrId));
            if (attrgroupRelation != null) {
                respVo.setAttrGroupId(attrgroupRelation.getAttrGroupId());
                AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrgroupRelation.getAttrGroupId());
                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;
    }

三,编码经验-使用常量类

为什么要用常量类?

  1. 让代码更整洁

    • 把所有的常量都放在一个地方,就像把所有的调料都放在调料盒里一样,找起来方便,也显得更整洁。
  2. 避免手滑

    • 如果你直接在代码里写一些固定的值,比如 "success" 或者 10,万一打错字或者数字,可能要花好长时间才能找到错在哪。用常量类就不用担心这个问题了。
  3. 容易改错

    • 如果你有一天突然想把 "success" 改成 "ok",你只需要在一个地方改,不用满世界去找。这就像是你只需要改一个地方的地址,而不是给每个人发信通知他们你的新地址。
  4. 编译器帮你把关

    • 如果你用的是常量,编译器会在你写错的时候提醒你。就像是有个严格的老师在旁边盯着,让你不容易犯错。
  5. 让代码更好懂

    • 比如说,你用 "SUCCESS" 或者 "ERROR" 这样的词,别人一看就知道你是啥意思,不像 10 那样让人摸不着头脑。
  6. 防止误操作

    • 在Java里,常量通常是 public static final 的,这意味着一旦设定就不能再改。这就像是给你的钱存进银行,不会被偷走。
  7. 方便国际化

    • 如果你的应用要面向全世界,不同的语言需要用不同的文字。把这些文字都放在常量里,以后要换语言就方便多了。
  8. 可能更快

    • 编译器有时候会把常量直接嵌入到代码里,这样运行起来会更快一点,就像是你提前把菜切好了,做饭的时候就快多了。

示例

public class Constants {
    public static final int MAX_CONNECTIONS = 10; // 最大连接数
    public static final String SUCCESS = "success"; // 成功的标志
    public static final String ERROR = "error"; // 错误的标志
}

public class Example {
    public void doSomething() {
        if (process() == Constants.SUCCESS) { // 如果处理成功
            // 执行后续操作
        }
    }

    private String process() { // 处理逻辑
        return Constants.SUCCESS; // 返回成功标志
    }
}

总的来说,用常量类可以让代码更清晰、更可靠,还能节省不少时间!

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
根据引用中提供的信息,谷粒商城是一个B2C模式的电商平台,主要销售自营商品给客户。B2C模式指的是商对客的模式,即商家直接向消费者销售商品服务谷粒商城使用微服务架构,这是一种将应用程序拆分成独立且可独立开发、部署和扩展的小型服务的架构。引用还提到谷粒商城的技术涵盖微服务架构、分布式、全栈、集群、部署和自动化运维等方面。 因此,从前慢-谷粒商城篇章6主要讨论的可能是谷粒商城的技术方面,包括微服务架构、分布式、全栈、集群、部署和自动化运维等内容。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [从前慢-谷粒商城篇章1](https://blog.csdn.net/m0_67391121/article/details/125383572)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [2020年谷粒商城md笔记](https://download.csdn.net/download/ZHUXIUQINGIT/12543161)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小手追梦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值