• 商城项目12_规格参数新增与VO、列表展示、回显数据进行修改、销售属性维护


    ①. 引入VO、PO、DO、TO、DTO

    • ①. VO(value object)值对象
      通常用于业务层之间的数据传递,和PO一样也是仅仅包含数据而已。但应是抽象出的业务对象,可以和表对应,也可以不,这根据业务的需要。用new关键字创建,由GC回收的

    • ②. DO(Domain 0bject)领域对象
      就是从现实世界中推象出来的有形或无形的业务实体

    • ③. TO(Transfer 0bject),数据传输对象传输的对象
      不同的应用程序之间传输的对象。微服务

    • ④. DTO(Data Transfer Obiect)数据传输对象
      这个概念来源于J2EE的设汁模式,原来的目的是为了EJB的分布式应用握供粗粒度的数据实体,以减少分布式调用的次数,从而握分布式调用的性能和降低网络负载,但在这里,泛指用于示层与服务层之间的数据传输对象

    • ⑤. PO持久对象
      PO就是对应数据库中某个表中的一条记录,多个记录可以用PO的集合。PO中应该不包含任何对数据的操作

    • ⑥. POJO简单无规则java对象

    ②. 规格参数 - 新增

    • ①. 新建VO对象
      Request URL: http://localhost:88/api/product/attr/save,现在的情况是,它在保存的时候,只是保存了attr,并没有保存attrgroup,为了解决这个问题,我们新建了一个vo/AttrVo.java,在原Attr基础上增加了attrGroupId字段,使得保存新增数据的时候,也保存了它们之间的关系。
      在这里插入图片描述
    //如在一些Entity中,为了让mybatis-plus知道某个字段不与数据库匹配,那么就加个@TableField(exist=false)
    @TableField(exist=false)
    private Long attrGroupId;
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    @Data
    public class AttrVo implements Serializable {
        private static final long serialVersionUID = 1L;
    
        // 属性id
        private Long attrId;
        // 属性名
        private String attrName;
        // 是否需要检索[0-不需要,1-需要]
        private Integer searchType;
        // 属性图标
        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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • ②. 新增的时候我们需要给pms_attr表和关联表pms_attr_attrgroup_relation表新增记录
      通过"BeanUtils.copyProperties(attr,attrEntity)" 能够实现在两个Bean之间属性对拷
    @Transactional
    @Override
    public void saveAttr(AttrVo attrVo) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attrVo,attrEntity);
        //1. 保存基本数据
        this.save(attrEntity);
        //2. 保存关联关系
        AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
        relationEntity.setAttrGroupId(attrVo.getAttrGroupId());
        relationEntity.setAttrId(attrEntity.getAttrId());
        relationDao.insert(relationEntity);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述

    在这里插入图片描述

    ③. 规格参数 - 列表展示

    • ①. 思路:
      由于返回的字段中有所属分类、所属分组,原来的attrEntity不能满足需求,这里重新抽取返回对象的VO
      所属分类:需要根据分类ID去pms_category表中查询出分类名称
      所属分组:需要根据attr_id去参数分组关联表pms_attr_attrgroup_relation查询出分组attr_group_id,根据分组attr_group_id去pms_attr_group表中查询分组名称
      在这里插入图片描述
    @Data
    public class AttrRespVo implements Serializable {
        // 属性id
        private Long attrId;
        // 属性名
        private String attrName;
        // 是否需要检索[0-不需要,1-需要]
        private Integer searchType;
        // 属性图标
        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;
        // 所属分类名字
        private String catelogName;
        // 所属分组名字
        private String groupName;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • ②. 核心代码展示
        @Override
        public PageUtils queryBaseAttrPage(Map<String, Object> params, Long catelogId) {
            QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
            String key = (String)params.get("key");
            if(!StringUtils.isEmpty(key)){
                wrapper.and(item->item.eq("attr_id",key).or().like("attr_name",key));
            }
            if(catelogId!=0){
                wrapper.eq("catelog_id",catelogId);
            }
    
            IPage<AttrEntity> page = this.page(
                    new Query<AttrEntity>().getPage(params),
                    wrapper);
            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);
                //1. 设置分类的名字
                CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId());
                if (categoryEntity != null) {
                    attrRespVo.setCatelogName(categoryEntity.getName());
                }
                //2. 设置分组的名字
                // 需要根据attr_id去参数分组关联表pms_attr_attrgroup_relation查询出分组attr_group_id,根据分组attr_group_id去pms_attr_group表中查询分组名称
                AttrAttrgroupRelationEntity attrId = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId()));
                if (attrId != null) {
                    AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrId.getAttrGroupId());
                    attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
                }
                return attrRespVo;
            }).collect(Collectors.toList());
    
            pageUtils.setList(respVos);
            return pageUtils;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    ④. 规格参数 - 修改

    • ①. 规格参数修改的第一步,需要回显页面数据。这里需要额外的去回显attrGroupId、catelogPath、使用AttrVO进行接收前台传递过来的数据
      在这里插入图片描述
      在这里插入图片描述
    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
    	AttrRespVo attr = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attr);
    }
    
    /**
     * 查询属性详情
     * @param attrId
     * @return
     */
    @Override
    public AttrRespVo getAttrInfo(Long attrId) {
        AttrEntity attrEntity = this.getById(attrId);
        AttrRespVo attrRespVo = new AttrRespVo();
        BeanUtils.copyProperties(attrEntity,attrRespVo);
        //1. 设置分组信息(获取attrGroupId)
        AttrAttrgroupRelationEntity relationEntity = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id",attrId));
        if(relationEntity!=null){
            attrRespVo.setAttrGroupId(relationEntity.getAttrGroupId());
        }
        //2. 设置分类信息(获取catelogPath)
        Long[] categoryPath = categoryService.findCategoryPath(attrEntity.getCatelogId());
        if(categoryPath!=null){
         attrRespVo.setCatelogPath(categoryPath);
        }
        return attrRespVo;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • ②. 规格参数修改功能:将pms_attr信息进行修改后,还需要对pms_attr_attrgroup_relation关联表进行修改或者新增操作
      在这里插入图片描述
    /**
     * 修改
     * @param attr
     */
    @Transactional
    @Override
    public void updateAttr(AttrVo attr) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attr,attrEntity);
        this.updateById(attrEntity);
        // 级联修改属性分组表
        Integer count = relationDao.selectCount(new QueryWrapper<AttrAttrgroupRelationEntity>());
        if(count>0){
            // 如果有记录就修改
            UpdateWrapper<AttrAttrgroupRelationEntity> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("attr_id",attrEntity.getAttrId());
            updateWrapper.set("attr_group_id",attr.getAttrGroupId());
            relationDao.update(null,updateWrapper);
        }else{
            // 如果没记录就新增
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            relationEntity.setAttrId(attrEntity.getAttrId());
            relationEntity.setAttrGroupId(attr.getAttrGroupId());
            relationDao.insert(relationEntity);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    ⑤. 销售属性 - 查询、新增、修改

    • ①. 销售属性的维护和规格参数一致,我们这里公用一个方法,销售属性不涉及到去新增或者修改关联表pms_attr_attrgroup_relation,我们在销售属性的新增、查询、修改的时候,需要对这些公共的方法进行if判断
      在这里插入图片描述
    • ②. 在数据库表中,使用attr_type来区分是销售属性还是基本属性、由于在后续的代码中我们会用到这个字段1和0的大量判断,这里我们将其抽取为商品的枚举类

    在这里插入图片描述

    public class ProductConstant {
        public enum  AttrEnum{
            ATTR_TYPE_BASE(1,"基本属性"),
            ATTR_TYPE_SALE(0,"销售属性");
            AttrEnum(int code,String msg){
                this.code=code;
                this.msg=msg;
            }
            
            private int code;
            private String msg;
            
            public int getCode() {
                return code;
            }
            public String getMsg() {
                return msg;
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • ③. 查询方法
    //@RequestMapping("/base/list/{catelogId}")
    @RequestMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params,
                          @PathVariable("catelogId")Long catelogId,
                          @PathVariable("attrType")String type){
        PageUtils page = attrService.queryBaseAttrPage(params,catelogId,type);
        return R.ok().put("page", page);
    }
    /**
     * 规则参数列表展示
     * @param params
     * @param catelogId
     * @param type
     * @return
     */
    @Override
    public PageUtils queryBaseAttrPage(Map<String, Object> params, Long catelogId, String type) {
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<AttrEntity>();
        //销售属性和规格参数属性是同一张表,都记录在pms_attr表中
        //如果attr_type=1表示的是规则参数属性(基本属性)、如果attr_type=0表示的是销售属性
        wrapper.eq("attr_type","base".equalsIgnoreCase(type)?
                ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode():
                ProductConstant.AttrEnum.ATTR_TYPE_SALE.getCode());
        String key = (String)params.get("key");
        if(!StringUtils.isEmpty(key)){
            wrapper.and(item->item.eq("attr_id",key).or().like("attr_name",key));
        }
        if(catelogId!=0){
            wrapper.eq("catelog_id",catelogId);
        }
    
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                wrapper);
        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);
            //1. 设置分类的名字
            CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId());
            if (categoryEntity != null) {
                attrRespVo.setCatelogName(categoryEntity.getName());
            }
            //2. 设置分组的名字(只有是规格参数基本属性的时候才去设置分组的名字)
            // 需要根据attr_id去参数分组关联表pms_attr_attrgroup_relation查询出分组attr_group_id,根据分组attr_group_id去pms_attr_group表中查询分组名称
            if("base".equalsIgnoreCase(type)){
                AttrAttrgroupRelationEntity attrId = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId()));
                if (attrId != null) {
                    AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrId.getAttrGroupId());
                    attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
                }
            }
            return attrRespVo;
        }).collect(Collectors.toList());
    
        pageUtils.setList(respVos);
        return pageUtils;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • ④. 新增、修改方法需要对关联表attr_attrgroup_relation表进行判断
       /**
         * 查询属性详情
         * @param attrId
         * @return
         */
        @Override
        public AttrRespVo getAttrInfo(Long attrId) {
            AttrEntity attrEntity = this.getById(attrId);
            AttrRespVo attrRespVo = new AttrRespVo();
            BeanUtils.copyProperties(attrEntity,attrRespVo);
            //1. 设置分组信息(获取attrGroupId)
            if (attrEntity.getAttrType()==ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()){
                AttrAttrgroupRelationEntity relationEntity = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id",attrId));
                if(relationEntity!=null){
                    attrRespVo.setAttrGroupId(relationEntity.getAttrGroupId());
                }
            }
            //2. 设置分类信息(获取catelogPath)
            Long[] categoryPath = categoryService.findCategoryPath(attrEntity.getCatelogId());
            if(categoryPath!=null){
             attrRespVo.setCatelogPath(categoryPath);
            }
            return attrRespVo;
        }
    
        /**
         * 修改
         * @param attr
         */
        @Transactional
        @Override
        public void updateAttr(AttrVo attr) {
            AttrEntity attrEntity = new AttrEntity();
            BeanUtils.copyProperties(attr,attrEntity);
            this.updateById(attrEntity);
            // 级联修改属性分组表
            if(attrEntity.getAttrType()==ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()){
                Integer count = relationDao.selectCount(new QueryWrapper<AttrAttrgroupRelationEntity>());
                if(count>0){
                    // 如果有记录就修改
                    UpdateWrapper<AttrAttrgroupRelationEntity> updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("attr_id",attrEntity.getAttrId());
                    updateWrapper.set("attr_group_id",attr.getAttrGroupId());
                    relationDao.update(null,updateWrapper);
                }else{
                    // 如果没记录就新增
                    AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
                    relationEntity.setAttrId(attrEntity.getAttrId());
                    relationEntity.setAttrGroupId(attr.getAttrGroupId());
                    relationDao.insert(relationEntity);
                }
            }
        }
       /**
         * 查询属性详情
         * @param attrId
         * @return
         */
        @Override
        public AttrRespVo getAttrInfo(Long attrId) {
            AttrEntity attrEntity = this.getById(attrId);
            AttrRespVo attrRespVo = new AttrRespVo();
            BeanUtils.copyProperties(attrEntity,attrRespVo);
            //1. 设置分组信息(获取attrGroupId)
            if (attrEntity.getAttrType()==ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()){
                AttrAttrgroupRelationEntity relationEntity = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id",attrId));
                if(relationEntity!=null){
                    attrRespVo.setAttrGroupId(relationEntity.getAttrGroupId());
                }
            }
            //2. 设置分类信息(获取catelogPath)
            Long[] categoryPath = categoryService.findCategoryPath(attrEntity.getCatelogId());
            if(categoryPath!=null){
             attrRespVo.setCatelogPath(categoryPath);
            }
            return attrRespVo;
        }
    
        /**
         * 修改
         * @param attr
         */
        @Transactional
        @Override
        public void updateAttr(AttrVo attr) {
            AttrEntity attrEntity = new AttrEntity();
            BeanUtils.copyProperties(attr,attrEntity);
            this.updateById(attrEntity);
            // 级联修改属性分组表
            if(attrEntity.getAttrType()==ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()){
                Integer count = relationDao.selectCount(new QueryWrapper<AttrAttrgroupRelationEntity>());
                if(count>0){
                    // 如果有记录就修改
                    UpdateWrapper<AttrAttrgroupRelationEntity> updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("attr_id",attrEntity.getAttrId());
                    updateWrapper.set("attr_group_id",attr.getAttrGroupId());
                    relationDao.update(null,updateWrapper);
                }else{
                    // 如果没记录就新增
                    AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
                    relationEntity.setAttrId(attrEntity.getAttrId());
                    relationEntity.setAttrGroupId(attr.getAttrGroupId());
                    relationDao.insert(relationEntity);
                }
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
  • 相关阅读:
    开源模型应用落地-工具使用篇-SLB(二)
    2022 年-Q2
    QT QTextEdit富文本插入字体-表格-编号-图片-查找-语法高亮功能
    Python高级语法-装饰器(Python语法糖)
    MySQL - count(字段)、count(主键)、count(1)、count(*)的区别
    数据结构复盘——第六章:图
    模型部署——CenterPoint转ONNX(自定义onnx算子)
    Friends or ‘Enemies?‘
    学校上课,是耽误我学习了。。
    [Database] 关系型数据库中的MVCC是什么?怎么理解?原理是什么?MySQL是如何实现的?
  • 原文地址:https://blog.csdn.net/TZ845195485/article/details/126815264