谷粒商城 规格参数

需求
在这里插入图片描述

  1. 查询: 根据catId 查询数据,支持根据key进行模糊搜索
  2. 保存:对attr 和 attrAtrrGroup 表进行修改
  3. 修改:首先是数据的回显,回显数据增加

vo

引入vo类,做前端返回数据实体类的中间层。在这里插入代码片

查询和修改查询

vo 继承实体类,并添加前端所需的额外属性

@Data
public class AttrRespVo extends AttrEntity {
    private Long attrGroupId;
    private Long catelogId;
    private Long[] catelogPath;
    private String groupName;
    private String catelogName;
}
  • 查询避免直接联表查询,而是一级一级查询
  • gid -> cid -> cname,path 则单独调用service封装的findpath
  • 每次查到实体类时,应进行非空判断
查询

页面表查询

     @Override
    public PageUtils queryBaseAttrPage(Map<String, Object> params, Long catelogId) {
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        if (catelogId != 0) {
            wrapper.and(i -> i.eq("catelog_id", catelogId));
        }
        if (params.containsKey("key")) { // 除此之外还有cid  cname
            String key = (String) params.get("key");
            wrapper.and(i -> i.eq("attr_id", key).or().like("attr_id", key));
        }
        IPage<AttrEntity> page = this.page(new Query<AttrEntity>().getPage(params), wrapper);
        List<AttrEntity> records = page.getRecords();
        List<AttrRespVo> vos = records.stream().map(entity -> {
            AttrRespVo vo = new AttrRespVo();
            BeanUtils.copyProperties(entity, vo);//
            AttrAttrgroupRelationEntity relationEntity = relationService.getById(entity.getAttrId());
            if (relationEntity != null) {  // 每次查找新表,都要先判空
                AttrGroupEntity groupEntity = groupService.getById(relationEntity.getAttrGroupId());
                if (groupEntity != null) {
                    vo.setGroupName(groupEntity.getAttrGroupName());
                }
            }
            CategoryEntity category = categoryService.getById(entity.getCatelogId());
            if (category != null) {
                vo.setCatelogName(category.getName());
            }
            return vo;
        }).collect(Collectors.toList());
        PageUtils pageUtils = new PageUtils();
        pageUtils.setList(vos);
        return pageUtils;
    }
修改查询
    @Override  // vo :     Long attrGroupId; Long catelogId;Long[] catelogPath;
    public AttrRespVo queryByIdWithPath(Long attrId) {
        AttrRespVo vo = new AttrRespVo();
        AttrEntity entity = this.getById(attrId);
        BeanUtils.copyProperties(entity,vo);
        AttrAttrgroupRelationEntity relationEntity = relationService.getById(attrId);
        if(relationEntity!=null){
            Long attrGroupId = relationEntity.getAttrGroupId();
            vo.setAttrGroupId(attrGroupId);
        }
        CategoryEntity categoryEntity = categoryService.getById(entity.getCatelogId());
        if(categoryEntity!=null){
            Long catelogId = categoryEntity.getCatId();
            Long[] catelogPath = categoryService.findCategoryPath(catelogId);
            vo.setCatelogId(catelogId);
            vo.setCatelogPath(catelogPath);
        }
        return vo;
    }

保存和修改保存

保存
    @Transactional
    @Override
    public void saveAttr(AttrVo attrVo) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attrVo, attrEntity);
        this.save(attrEntity);// 基本数据
        
        AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
        relationEntity.setAttrGroupId(attrVo.getAttrGroupId());
        relationEntity.setAttrId(attrEntity.getAttrId());
        relationService.save(relationEntity);// 关系表数据
    }
修改保存
 @Override
    @Transactional
    public void updateAttr(AttrRespVo vo) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(vo, attrEntity);
        this.updateById(attrEntity);  // 修改基础表

        Long attrId = attrEntity.getAttrId();
        Long attrGroupId = vo.getAttrGroupId();
        AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
        relationEntity.setAttrId(attrId);
        relationEntity.setAttrGroupId(attrGroupId);
        UpdateWrapper<AttrAttrgroupRelationEntity> wrapper = // 修改关系表
                new UpdateWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrId);
        relationService.update(relationEntity, wrapper);
    }

总结

  • 对表结构不熟悉
  • 对需求不熟悉
  • 对方法应该加在哪一层不熟悉
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值