谷粒商城【成神路】-【6】——商品维护

目录

1.发布商品

2.获取分类关联品牌 

3.获取分类下所有分组和关联属性 

4.商品保存功能

5.sup检索 

6.sku检索


1.发布商品

获取用户系统等级~,前面生成了后端代码,在因为添加了网关,所以要配置陆游规则

谷粒商城【成神路】-【6】——商品维护_第1张图片

在网管层配置会员服务的路由规则,精确的路由放到上面

        #会员服务
        - id: member_route
          uri: lb://gulimall-member
          predicates:
             - Path=/api/member/**
          filters:
            - RewritePath=/api/(?.*),/$\{segment}

 配置会员服务的配置

1.添加nacos的注册中心地址

2.添加nacos的配置中心地址

3.将配置文件写在bootstrap.yml中

spring:
  #数据源
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://192.168.20.129:3306/gulimall_ums
    username: root
    password: root
  cloud:
    nacos:
      discovery:
        # 这里使用了Ngingx
        server-addr: 192.168.20.50:1111
      config:
        server-addr: 192.168.20.50:1111

  application:
    name: gulimall-member

2.获取分类关联品牌 

2.1controller

controller处理请求,接收和校验数据,接收service处理完的数据,封装页面指定的vo

  @GetMapping("/brands/list")
    public R relationBrandList(@RequestParam(value = "catId", required = true) Long catId) {
        List vos = categoryBrandRelationService.getBrandByCatId(catId);
        List collect = vos.stream().map((item) -> {
            BrandVo brandVo = new BrandVo();
            brandVo.setBrandId(item.getBrandId());
            brandVo.setBrandName(item.getName());
            return brandVo;
        }).collect(Collectors.toList());
        return R.ok().put("data",collect);
    } 
  

2.2service

service来接收controller传来的数据,进行业务处理

@Override
    public List getBrandsByCatId(Long catId) {

        List catelogId = relationDao.selectList(new QueryWrapper().eq("catelog_id", catId));
        List collect = catelogId.stream().map(item -> {
            Long brandId = item.getBrandId();
            BrandEntity byId = brandService.getById(brandId);
            return byId;
        }).collect(Collectors.toList());
        return collect;
    }

 谷粒商城【成神路】-【6】——商品维护_第2张图片

3.获取分类下所有分组和关联属性 

1.controller

/**
     * 获取当前分类下的所有属性分组
     *
     * @return
     */
    @GetMapping("/{catelog_id}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelog_id") Long catelog_id) {
        //1.查出当前分类下的所有属性分组
        //2.查出每个属性分组的所有属性
        List vos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelog_id);
        return R.ok().put("data",vos);

    }

2.service

如果前端爆foreach异常,但后端数据正常,检查一下属性分组里面,必须保证每一个组名至少关联一个属性名,否则为null,后端需要判断

谷粒商城【成神路】-【6】——商品维护_第3张图片

 @Override
    public List getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        //com.atguigu.gulimall.product.vo
        //1、查询分组信息
        List attrGroupEntities = this.list(new QueryWrapper().eq("catelog_id", catelogId));

        //2、查询所有属性
        List collect = attrGroupEntities.stream().map(group -> {
            AtttrGroupWithAttrsVo attrsVo = new AtttrGroupWithAttrsVo();
            BeanUtils.copyProperties(group, attrsVo);
            List attrs = attrService.geRelationAttr(attrsVo.getAttrGroupId());
            attrsVo.setAttrs(attrs);
            return attrsVo;
        }).collect(Collectors.toList());
        return collect;
    }

谷粒商城【成神路】-【6】——商品维护_第4张图片

4.商品保存功能

谷粒商城【成神路】-【6】——商品维护_第5张图片

1.controller

 @RequestMapping("/save")
    //@RequiresPermissions("product:spuinfo:save")
    public R save(@RequestBody SpuSaveVo vo){
//		spuInfoService.save(spuInfo);

        spuInfoService.saveSpuInfo(vo);
        return R.ok();
    }

2.service 

service使用feign调用远程服务,注意feign要调用服务的名称,与具体的请求地址

谷粒商城【成神路】-【6】——商品维护_第6张图片 

 @Transactional
    @Override
    public void saveSpuInfo(SpuSaveVo vo) {
        //1.保存spu基本信息
        SpuInfoEntity infoEntity = new SpuInfoEntity();
        BeanUtils.copyProperties(vo, infoEntity);
        infoEntity.setCreateTime(new Date());
        infoEntity.setUpdateTime(new Date());
        this.saveBaseSpuInfo(infoEntity);

        //2.保存spu的描述图片
        List decript = vo.getDecript();
        SpuInfoDescEntity descEntity = new SpuInfoDescEntity();
        descEntity.setSpuId(infoEntity.getId());
        descEntity.setDecript(String.join(",", decript));
        spuInfoDescService.saveSpuInfoDesc(descEntity);

        //3.保存spu的图片集
        List images = vo.getImages();
        imagesService.saveImages(infoEntity.getId(), images);

        //4.保存sup的规格参数
        List baseAttrs = vo.getBaseAttrs();
        List collect = baseAttrs.stream().map((attr) -> {
            ProductAttrValueEntity valueEntity = new ProductAttrValueEntity();
            valueEntity.setAttrId(attr.getAttrId());
            AttrEntity id = attrService.getById(attr.getAttrId());
            valueEntity.setAttrName(id.getAttrName());
            valueEntity.setAttrValue(attr.getAttrValues());
            valueEntity.setQuickShow(attr.getShowDesc());
            valueEntity.setSpuId(infoEntity.getId());
            return valueEntity;
        }).collect(Collectors.toList());
        attrValueService.saveProductAttr(collect);


        //5.保存spu的积分信息
        Bounds bounds = vo.getBounds();
        SpuBoundTo spuBoundTo = new SpuBoundTo();
        BeanUtils.copyProperties(bounds, spuBoundTo);
        spuBoundTo.setSpuId(infoEntity.getId());

        R r = couponFeignService.saveSpuBounds(spuBoundTo);
        if (r.getCode() != 0) {
            log.error("远程保存spu积分信息失败");
        }


        //5.保存当前spu对应的sku信息
        //5.1、保存sku的基本信息
        List skus = vo.getSkus();
        if (skus != null && skus.size() > 0) {
            skus.forEach(item -> {
                String defaultImg = "";
                for (Images image : item.getImages()) {
                    if (image.getDefaultImg() == 1) {
                        defaultImg = image.getImgUrl();
                    }
                    SkuInfoEntity skuInfoEntity = new SkuInfoEntity();
                    BeanUtils.copyProperties(item, skuInfoEntity);
                    skuInfoEntity.setBrandId(infoEntity.getBrandId());
                    skuInfoEntity.setCatalogId(infoEntity.getCatalogId());
                    skuInfoEntity.setSaleCount(0L);
                    skuInfoEntity.setSpuId(infoEntity.getId());
                    skuInfoEntity.setSkuDefaultImg(defaultImg);
                    skuInfoService.saveSkuInfo(skuInfoEntity);

                    Long skuId = skuInfoEntity.getSkuId();

                    List imagesEntities = item.getImages().stream().map((img) -> {
                        SkuImagesEntity skuImagesEntity = new SkuImagesEntity();
                        skuImagesEntity.setSkuId(skuId);
                        skuImagesEntity.setImgUrl(img.getImgUrl());
                        skuImagesEntity.setDefaultImg(img.getDefaultImg());
                        return skuImagesEntity;
                    }).filter(entity -> {
                        //返回true就是需要,false就是剔除
                        return !StringUtils.isEmpty(entity.getImgUrl());
                    }).collect(Collectors.toList());
                    //5.2、sku的图片信息
                    // TODO 没有图片路径的无需保存
                    skuImagesService.saveBatch(imagesEntities);
                    List attr = item.getAttr();
                    List saleAttrValueEntityList = attr.stream().map(a -> {
                        SkuSaleAttrValueEntity attrValueEntity = new SkuSaleAttrValueEntity();
                        BeanUtils.copyProperties(a, attrValueEntity);
                        attrValueEntity.setSkuId(skuId);
                        return attrValueEntity;
                    }).collect(Collectors.toList());
                    //5.3、sku的销售属性
                    skuSaleAttrValueService.saveBatch(saleAttrValueEntityList);

                    // 5.4、sku的优惠满减信息
                    SkuReductionTo skuReductionTo = new SkuReductionTo();
                    BeanUtils.copyProperties(item, skuReductionTo);
                    skuReductionTo.setSkuId(skuId);
                    if (skuReductionTo.getFullCount() > 0 || skuReductionTo.getFullPrice().compareTo(new BigDecimal("0"))==1) {
                        R r1 = couponFeignService.saveSkuReduction(skuReductionTo);
                        if (r1.getCode() != 0) {
                            log.error("远程保存spu积分优惠失败");
                        }
                    }


                }
            });
        }
    }

5.sup检索 

谷粒商城【成神路】-【6】——商品维护_第7张图片

谷粒商城【成神路】-【6】——商品维护_第8张图片 

1.controller

@RequestMapping("/list")
    //@RequiresPermissions("product:spuinfo:list")
    public R list(@RequestParam Map params){
        PageUtils page = spuInfoService.queryPageByCondition(params);

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

2.service 

  service用PageUtils封装模糊查询拼接

@Override
    public PageUtils queryPageByCondition(Map params) {
        QueryWrapper wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(params.get(key))) {
            wrapper.and((w) -> {
                w.eq("id", key).or().like("spu_name", key);
            });
        }
        String status = (String) params.get("status");
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("publish_status", status);
        }
        String brandId = (String) params.get("brandId");
        if (!StringUtils.isEmpty(brandId)) {
            wrapper.eq("brand_id", brandId);
        }
        String catelogId = (String) params.get("catelogId");
        if (!StringUtils.isEmpty(catelogId)) {
            wrapper.eq("catalog_id", catelogId);
        }

        IPage page = this.page(
                new Query().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

6.sku检索

谷粒商城【成神路】-【6】——商品维护_第9张图片

1.controller

@RequestMapping("/list")
    //@RequiresPermissions("product:skuinfo:list")
    public R list(@RequestParam Map params){
        PageUtils page = skuInfoService.queryPageByCondition(params);

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

2.service

 @Override
    public PageUtils queryPageByCondition(Map params) {
        QueryWrapper wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            wrapper.and((w) -> {
                w.eq("id", key).or().like("spu_name", key);
            });
        }
        String status = (String) params.get("status");
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("publish_status", status);
        }
        String brandId = (String) params.get("brandId");
        if (!StringUtils.isEmpty(brandId) && !"0".equalsIgnoreCase(brandId)) {
            wrapper.eq("brand_id", brandId);
        }
        String catelogId = (String) params.get("catelogId");
        if (!StringUtils.isEmpty(catelogId)&& !"0".equalsIgnoreCase(catelogId)) {
            wrapper.eq("catalog_id", catelogId);
        }

        IPage page = this.page(
                new Query().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

谷粒商城【成神路】-【6】——商品维护_第10张图片

你可能感兴趣的:(谷粒商城【弑神战】,前端,java,后端,spring,cloud,分布式,架构,gateway)