10.商品服务-新增商品

1.配置会员模块网关信息

 网关配置

spring:
  cloud:
    gateway:
      routes:
        #商品的路由
        #localhost:88/api/product/category/list/tree--->localhost:10000/product....
        #优先级比下面的哪个路由要高所以要放在上面,不然会被截断
        - id: product_route
          uri: lb://gulimall-product
          predicates:
            - Path=/api/product/**
          filters:
            - RewritePath=/api/(?<segment>.*),/$\{segment}
      #第三方路由配置
        - id: third_party_route
          uri: lb://gulimall-third-party
          predicates:
            - Path=/api/thirdparty/**
          filters:
            - RewritePath=/api/thirdparty/(?<segment>.*),/$\{segment}
        #会员服务的路由
        - id: member_route
          uri: lb://gulimall-member
          predicates:
            - Path=/api/member/**
          filters:
              #api前缀去掉剩下的全体保留
            - RewritePath=/api/(?<segment>.*),/$\{segment}

        - id: admin_route
          #lb表示负载均衡
          uri: lb://renren-fast
          #规定前端项目必须带有一个api前缀
          #原来验证码的uri ...localhost:88/api/captcha.jpg
          #应该改成的uri ...localhost:88/renrenfast/captcha.jpg
          predicates:
            - Path=/api/**
          filters:
            - RewritePath=/api/(?<segment>.*),/renren-fast/$\{segment}

2.商品维护-发布商品-选择分类之后-显示选择品牌的内容

对应接口

@GetMapping("/brands/list")
    public R relationBrandList(@RequestParam(value="catId",required = true)Long catId){
        List<BrandEntity> list=categoryBrandRelationService.getBrandsByCatId(catId);//提高方法的复用性
        List<BrandVo> collect = list.stream().map((item) -> {
            BrandVo brandVo = new BrandVo();
//            BeanUtils.copyProperties(item, brandVo);由于字段名不一样不可用
            brandVo.setBrandId(item.getBrandId());
            brandVo.setBrandName(item.getName());
            return brandVo;
        }).collect(Collectors.toList());
        return R.ok().put("data",collect);
    }

 @Override
    public List<BrandEntity> getBrandsByCatId(Long catId) {
        List<CategoryBrandRelationEntity> relationEntities = this.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        //前端这一块不需要判断为空,因为是直接点出来的
        List<Long> collect = relationEntities.stream().map((item) -> {
            return item.getBrandId();
        }).collect(Collectors.toList());
        List<BrandEntity> list = brandDao.selectList(new QueryWrapper<BrandEntity>().in("brand_id", collect));
        return list;
    }

3.显示发布商品中的规格参数内容+抽取vo

接口:

 

 相应数据是分类下所有属性的分组+分组下所有的属性

创建product下的vo

public class AttrGroupWithAttrsVo {
    private static final long serialVersionUID = 1L;

    /**
     * 分组id
     */
    @TableId
    private Long attrGroupId;
    /**
     * 组名
     */
    private String attrGroupName;
    /**
     * 排序
     */
    private Integer sort;
    /**
     * 描述
     */
    private String descript;
    /**
     * 组图标
     */
    private String icon;
    /**
     * 所属分类id
     */
    private Long catelogId;
    
    //分组含有的所有属性
    private List<AttrEntity> attrs;
}

 @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttr(@PathVariable("catelogId") Long catelogId){
        //1.查出当前分类下所有的属性分组
        //2.查出每个分组的所有属性
        List<AttrGroupWithAttrsVo> list=attrGroupService.getAttrGroupWithAttrsByCatlogId(catelogId);
        return R.ok().put("data",list);
    }

@Autowired
    AttrService attrService;
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatlogId(Long catelogId) {
        //根据分类id查询分组信息
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        //查询所有属性
        List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map((group) -> {
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(group, attrGroupWithAttrsVo);
            return attrGroupWithAttrsVo;
        }).peek((vo)->{
            List<AttrEntity> relationAttrs = attrService.getRelationAttr(vo.getAttrGroupId());//运用之前写过的方法
            vo.setAttrs(relationAttrs);
        }).collect(Collectors.toList());
        return collect;
    }

抽取vo

 4.保存所有的spu信息

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

4.1)保存基本信息(无远程调用)

@Transactional
    @Override
    public void saveSpuInfoVo(SpuSaveVo vo) {
        //1.保存spu基本的信息 spu_info
        SpuInfoEntity spuInfoEntity = new SpuInfoEntity();
        BeanUtils.copyProperties(vo,spuInfoEntity);
        //设置一下两个名字不一样的值
        spuInfoEntity.setCreateTime(new Date());
        spuInfoEntity.setUpdateTime(new Date());
        this.saveBaseSpuInfo(spuInfoEntity);
        //2.保存spu的描述 spu_info_desc
        List<String> description=vo.getDecript();
        SpuInfoDescEntity spuInfoDescEntity = new SpuInfoDescEntity();
        spuInfoDescEntity.setSpuId(spuInfoEntity.getId());
        spuInfoDescEntity.setDecript(String.join(",",description));//集合转字符串
        spuInfoDescService.saveSpuInfoDesc(spuInfoDescEntity);
        //3.保存spu的图片集 spu_images
        List<String> images=vo.getImages();
        spuImagesService.saveImages(spuInfoEntity.getId(),images);
        //4.保存spu规格参数 product_attr_value
        List<BaseAttrs> baseAttrs = vo.getBaseAttrs();
        List<ProductAttrValueEntity> collect = baseAttrs.stream().map(item -> {
            ProductAttrValueEntity productAttrValueEntity = new ProductAttrValueEntity();
            productAttrValueEntity.setAttrId(item.getAttrId());
            AttrEntity attrEntity = attrService.getById(item.getAttrId());
            productAttrValueEntity.setAttrName(attrEntity.getAttrName());
            productAttrValueEntity.setAttrValue(item.getAttrValues());
            productAttrValueEntity.setQuickShow(item.getShowDesc());
            productAttrValueEntity.setSpuId(spuInfoEntity.getId());
            return productAttrValueEntity;
        }).collect(Collectors.toList());
        productAttrValueService.saveProductAttr(collect);
        //以下是远程保存
        //5.保存spu对应的所有sku信息
        //5.1 sku的基本信息 sku_info
        List<Skus> skus= vo.getSkus();
        if(skus!=null&&skus.size()>0){
            skus.forEach(item->{
                SkuInfoEntity skuInfoEntity = new SkuInfoEntity();
                BeanUtils.copyProperties(item,skuInfoEntity);
                skuInfoEntity.setBrandId(spuInfoEntity.getBrandId());
                skuInfoEntity.setCatalogId(spuInfoEntity.getCatalogId());
                skuInfoEntity.setSaleCount(0L);
                skuInfoEntity.setSpuId(spuInfoEntity.getId());
                //保存默认的图片
                String defaultImg="";
                for(Images images1:item.getImages()){
                    if(images1.getDefaultImg()==1){
                        defaultImg=images1.getImgUrl();
                    }
                }
                skuInfoEntity.setSkuDefaultImg(defaultImg);
                skuInfoService.saveSkuInfo(skuInfoEntity);
                //5.2 sku的图片信息 sku_image
                //处理图片集
                List<SkuImagesEntity> imagesList = item.getImages().stream().map(img -> {
                    SkuImagesEntity skuImagesEntity = new SkuImagesEntity();
                    skuImagesEntity.setSkuId(skuInfoEntity.getSkuId());
                    skuImagesEntity.setImgUrl(img.getImgUrl());
                    skuImagesEntity.setDefaultImg(img.getDefaultImg());
                    return skuImagesEntity;
                }).collect(Collectors.toList());
                skuImagesService.saveBatch(imagesList);//其实早就可以用自己的方法进行保存了...
                //5.3 sku的销售属性值 sku_sale_attr_value
                List<Attr> attr = item.getAttr();
                List<SkuSaleAttrValueEntity> skuSaleAttrValueEntityList = attr.stream().map(a -> {
                    SkuSaleAttrValueEntity skuSaleAttrValueEntity = new SkuSaleAttrValueEntity();
                    BeanUtils.copyProperties(a, skuSaleAttrValueEntity);
                    skuSaleAttrValueEntity.setSkuId(skuInfoEntity.getSkuId());
                    return skuSaleAttrValueEntity;
                }).collect(Collectors.toList());
                skuSaleAttrValueService.saveBatch(skuSaleAttrValueEntityList);
                //以下的服务需要调用远程服务
                //5.4 sku的满减优惠信息 gulimall_sms
                //6 保存spu的积分信息 sms_spu_bounds
            });
        }

    }

里面调用了很多自定义的方法都没有必要,可以用现有的接口,给出一个稍有区别的

 @Override
    public void saveImages(Long id, List<String> images) {
        if(images!=null&&images.size()!=0){
            List<SpuImagesEntity> collect = images.stream().map(img -> {
                SpuImagesEntity spuImagesEntity = new SpuImagesEntity();
                spuImagesEntity.setSpuId(id);
                spuImagesEntity.setImgUrl(img);
                return spuImagesEntity;
            }).collect(Collectors.toList());
            this.saveBatch(collect);
        }
    }

4.2)调用远程服务

1.被调用的远程服务必须在注册中心上线

2.被调用的远程服务一定要开启服务的注册与发现功能

 3.调用者声明一个远程服务

 步骤:

由于有一些数据是需要a服务传给b服务,两个服务都要使用,所以我们把这些数据封装在gulimall-common的to包下,to名为数据传输对象

@Data
public class SpuBoundTo {
    private Long spuId;
    private BigDecimal buyBounds;
    private BigDecimal growBounds;

开启Product远程服务调用

 

 创建feign包下的接口

@FeignClient("gulimall-coupon")
public interface CouponFeignService {
    /**
     * 1.@RequestBody将对象转为json
     * 2.找到coupon服务,给/coupon/spubounds/save发送请求,将上一步转的json对象放在请求体的位置发送请求
     * 3.对方服务收到请求,收到的是请求体里的json数据,对方服务将json转成自己的对象,只要两者对象的属性名一一对应就可以封装
     * 综上所属传递to接受为entity的行为也是可取的
     */
    @PostMapping("/coupon/spubounds/save")
    public R saveSpuBounds(@RequestBody SpuBoundTo spuBoundTo);
    
}

与feign包接口对应的coupon的服务

   /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody SpuBoundsEntity spuBounds){
        spuBoundsService.save(spuBounds);
        return R.ok();
    }

同样的我们创建满减信息的To,复制product服务里vo的skus里相关属性并且加上skuid,这个to需要memberprice我们同样从vo里复制过来

@Data
public class SkuReductionTo {
    private Long skuId;
    private int fullCount;

    private BigDecimal discount;

    private int countStatus;

    /**
     * 满减价格
     */
    private BigDecimal fullPrice;

    private BigDecimal reducePrice;

    private int priceStatus;
    private List<MemberPrice> memberPrice;
}

 

 给出远程接口类代码:

@FeignClient("gulimall-coupon")
public interface CouponFeignService {
    /**
     * 1.@RequestBody将对象转为json
     * 2.找到coupon服务,给/coupon/spubounds/save发送请求,将上一步转的json对象放在请求体的位置发送请求
     * 3.对方服务收到请求,收到的是请求体里的json数据,对方服务将json转成自己的对象,只要两者对象的属性名一一对应就可以封装
     * 综上所属传递to接受为entity的行为也是可取的
     */
    @PostMapping("/coupon/spubounds/save")
    public R saveSpuBounds(@RequestBody SpuBoundTo spuBoundTo);
    @PostMapping("coupon/skufullreduction/saveinfo")
    R saveSkuReduction(@RequestBody SkuReductionTo skuReductionTo);
}

对于第二个方法,coupon里没有所以要我们自己去写

    /**
     * 保存详情远程接口
     */
    @PostMapping("/saveinfo")
    //@RequiresPermissions("coupon:skufullreduction:list")
    public R saveInfo(@RequestBody SkuReductionTo skuReductionTo){
       skuFullReductionService.saveSkuReduction(skuReductionTo);

        return R.ok();
    }

在R里添加getCode方法来判断是否有异常

    public Integer getCode(){
        return Integer.parseInt((String) this.get("code"));
    }

 @Override
    public void saveSkuReduction(SkuReductionTo skuReductionTo) {
        //1.保存优惠,会员价
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setSkuId(skuReductionTo.getSkuId());
        skuLadderEntity.setFullCount(skuReductionTo.getFullCount());
        skuLadderEntity.setDiscount(skuReductionTo.getDiscount());
        skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());
        skuLadderService.save(skuLadderEntity);
        //2.sms_sku_full满减
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuReductionTo,skuFullReductionEntity);
        this.save(skuFullReductionEntity);
        //3.member_price
        List<MemberPrice> memberPrice = skuReductionTo.getMemberPrice();
        List<MemberPriceEntity> collect = memberPrice.stream().map(item -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
            memberPriceEntity.setMemberLevelId(item.getId());
            memberPriceEntity.setMemberLevelName(item.getName());
            memberPriceEntity.setMemberPrice(item.getPrice());
            memberPriceEntity.setAddOther(1);
            return memberPriceEntity;
        }).collect(Collectors.toList());
        memberPriceService.saveBatch(collect);
    }

保存方法完整代码

    @Transactional
    @Override
    public void saveSpuInfoVo(SpuSaveVo vo) {
        //1.保存spu基本的信息 spu_info
        SpuInfoEntity spuInfoEntity = new SpuInfoEntity();
        BeanUtils.copyProperties(vo,spuInfoEntity);
        //设置一下两个名字不一样的值
        spuInfoEntity.setCreateTime(new Date());
        spuInfoEntity.setUpdateTime(new Date());
        this.saveBaseSpuInfo(spuInfoEntity);
        //2.保存spu的描述 spu_info_desc
        List<String> description=vo.getDecript();
        SpuInfoDescEntity spuInfoDescEntity = new SpuInfoDescEntity();
        spuInfoDescEntity.setSpuId(spuInfoEntity.getId());
        spuInfoDescEntity.setDecript(String.join(",",description));//集合转字符串
        spuInfoDescService.saveSpuInfoDesc(spuInfoDescEntity);
        //3.保存spu的图片集 spu_images
        List<String> images=vo.getImages();
        spuImagesService.saveImages(spuInfoEntity.getId(),images);
        //4.保存spu规格参数 product_attr_value
        List<BaseAttrs> baseAttrs = vo.getBaseAttrs();
        List<ProductAttrValueEntity> collect = baseAttrs.stream().map(item -> {
            ProductAttrValueEntity productAttrValueEntity = new ProductAttrValueEntity();
            productAttrValueEntity.setAttrId(item.getAttrId());
            AttrEntity attrEntity = attrService.getById(item.getAttrId());
            productAttrValueEntity.setAttrName(attrEntity.getAttrName());
            productAttrValueEntity.setAttrValue(item.getAttrValues());
            productAttrValueEntity.setQuickShow(item.getShowDesc());
            productAttrValueEntity.setSpuId(spuInfoEntity.getId());
            return productAttrValueEntity;
        }).collect(Collectors.toList());
        productAttrValueService.saveProductAttr(collect);
        //以下是远程保存
        //5.保存spu对应的所有sku信息
        //5.1 sku的基本信息 sku_info
        List<Skus> skus= vo.getSkus();
        if(skus!=null&&skus.size()>0){
            skus.forEach(item->{
                SkuInfoEntity skuInfoEntity = new SkuInfoEntity();
                BeanUtils.copyProperties(item,skuInfoEntity);
                skuInfoEntity.setBrandId(spuInfoEntity.getBrandId());
                skuInfoEntity.setCatalogId(spuInfoEntity.getCatalogId());
                skuInfoEntity.setSaleCount(0L);
                skuInfoEntity.setSpuId(spuInfoEntity.getId());
                //保存默认的图片
                String defaultImg="";
                for(Images images1:item.getImages()){
                    if(images1.getDefaultImg()==1){
                        defaultImg=images1.getImgUrl();
                    }
                }
                skuInfoEntity.setSkuDefaultImg(defaultImg);
                skuInfoService.saveSkuInfo(skuInfoEntity);
                //5.2 sku的图片信息 sku_image
                //处理图片集
                List<SkuImagesEntity> imagesList = item.getImages().stream().map(img -> {
                    SkuImagesEntity skuImagesEntity = new SkuImagesEntity();
                    skuImagesEntity.setSkuId(skuInfoEntity.getSkuId());
                    skuImagesEntity.setImgUrl(img.getImgUrl());
                    skuImagesEntity.setDefaultImg(img.getDefaultImg());
                    return skuImagesEntity;
                }).collect(Collectors.toList());
                skuImagesService.saveBatch(imagesList);//其实早就可以用自己的方法进行保存了...
                //5.3 sku的销售属性值 sku_sale_attr_value
                List<Attr> attr = item.getAttr();
                List<SkuSaleAttrValueEntity> skuSaleAttrValueEntityList = attr.stream().map(a -> {
                    SkuSaleAttrValueEntity skuSaleAttrValueEntity = new SkuSaleAttrValueEntity();
                    BeanUtils.copyProperties(a, skuSaleAttrValueEntity);
                    skuSaleAttrValueEntity.setSkuId(skuInfoEntity.getSkuId());
                    return skuSaleAttrValueEntity;
                }).collect(Collectors.toList());
                skuSaleAttrValueService.saveBatch(skuSaleAttrValueEntityList);
                //以下的服务需要调用远程服务
                //5.4 sku的满减优惠信息 gulimall_sms-》sms_spu_bounds
                SkuReductionTo skuReductionTo=new SkuReductionTo();
                BeanUtils.copyProperties(item,skuReductionTo);
                skuReductionTo.setSkuId(skuInfoEntity.getSkuId());
                R r1 = couponFeignService.saveSkuReduction(skuReductionTo);
                //判断异常
                if(r1.getCode()!=0){
                    log.error("远程保存spu优惠信息失败");
                }
                //6 保存spu的积分信息 sms_spu_bounds
                Bounds bounds = vo.getBounds();
                SpuBoundTo spuBoundTo = new SpuBoundTo();
                BeanUtils.copyProperties(bounds,spuBoundTo);
                spuBoundTo.setSpuId(skuInfoEntity.getSkuId());
                R r = couponFeignService.saveSpuBounds(spuBoundTo);
                //判断异常
                if(r.getCode()!=0){
                    log.error("远程保存spu积分信息失败");
                }
            });
        }
    }

 4.3)测试阶段:

右上角服务,Edit Configurations-》+ -》compound可以便捷整理一组服务的重启

 

 设置内存最大占用

 

 因为我们设置了事务,所以不能逐步查看数据库的信息了,我们修改一下数据库的行为

 

 查看第一个数据库是否正确读入

spu_info没有出问题

 

 继续走发现抛出了异常

 

 我们直接放行完代码

 

 异常抛出的原因是因为,我们在建立pms_spu_info_desc这张表的时候,虽然指定了主键是spu_id,但是我们没有指定他自增,但是mybatis默认当成是自增的

 

 重启product项目使用debug模式发现spu_info_desc保存成功了

 

 继续走图片集也保存成功

 

 可以看到后续product_attr_value也保存成功

 

 然后在这里又抛了一个异常

 

 

 

 修改common里的代码如下

public Integer getCode(){
        return (Integer) this.get("code");
    }

5.商品保存的细节优化

 

 

 

 代码:

 @Override
    public void saveSkuReduction(SkuReductionTo skuReductionTo) {
        //1.保存优惠,会员价
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setSkuId(skuReductionTo.getSkuId());
        skuLadderEntity.setFullCount(skuReductionTo.getFullCount());
        skuLadderEntity.setDiscount(skuReductionTo.getDiscount());
        skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());
        if(skuReductionTo.getFullCount()>0){
            skuLadderService.save(skuLadderEntity);
        }
        //2.sms_sku_full满减
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuReductionTo,skuFullReductionEntity);
        if(skuReductionTo.getFullPrice().compareTo(new BigDecimal("0"))==1){
            this.save(skuFullReductionEntity);
        }
        //3.member_price
        List<MemberPrice> memberPrice = skuReductionTo.getMemberPrice();
        List<MemberPriceEntity> collect = memberPrice.stream().map(item -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
            memberPriceEntity.setMemberLevelId(item.getId());
            memberPriceEntity.setMemberLevelName(item.getName());
            memberPriceEntity.setMemberPrice(item.getPrice());
            memberPriceEntity.setAddOther(1);
            return memberPriceEntity;
        }).filter(item->{
            //接受大于0的值才去保存
            return item.getMemberPrice().compareTo(new BigDecimal("0"))==1;
        }).collect(Collectors.toList());
        memberPriceService.saveBatch(collect);
    }

 @Transactional
    @Override
    public void saveSpuInfoVo(SpuSaveVo vo) {
        //1.保存spu基本的信息 spu_info
        SpuInfoEntity spuInfoEntity = new SpuInfoEntity();
        BeanUtils.copyProperties(vo,spuInfoEntity);
        //设置一下两个名字不一样的值
        spuInfoEntity.setCreateTime(new Date());
        spuInfoEntity.setUpdateTime(new Date());
        this.saveBaseSpuInfo(spuInfoEntity);
        //2.保存spu的描述 spu_info_desc
        List<String> description=vo.getDecript();
        SpuInfoDescEntity spuInfoDescEntity = new SpuInfoDescEntity();
        spuInfoDescEntity.setSpuId(spuInfoEntity.getId());
        spuInfoDescEntity.setDecript(String.join(",",description));//集合转字符串
        spuInfoDescService.saveSpuInfoDesc(spuInfoDescEntity);
        //3.保存spu的图片集 spu_images
        List<String> images=vo.getImages();
        spuImagesService.saveImages(spuInfoEntity.getId(),images);
        //4.保存spu规格参数 product_attr_value
        List<BaseAttrs> baseAttrs = vo.getBaseAttrs();
        List<ProductAttrValueEntity> collect = baseAttrs.stream().map(item -> {
            ProductAttrValueEntity productAttrValueEntity = new ProductAttrValueEntity();
            productAttrValueEntity.setAttrId(item.getAttrId());
            AttrEntity attrEntity = attrService.getById(item.getAttrId());
            productAttrValueEntity.setAttrName(attrEntity.getAttrName());
            productAttrValueEntity.setAttrValue(item.getAttrValues());
            productAttrValueEntity.setQuickShow(item.getShowDesc());
            productAttrValueEntity.setSpuId(spuInfoEntity.getId());
            return productAttrValueEntity;
        }).collect(Collectors.toList());
        productAttrValueService.saveProductAttr(collect);
        //以下是远程保存
        //5.保存spu对应的所有sku信息
        //5.1 sku的基本信息 sku_info
        List<Skus> skus= vo.getSkus();
        if(skus!=null&&skus.size()>0){
            skus.forEach(item->{
                SkuInfoEntity skuInfoEntity = new SkuInfoEntity();
                BeanUtils.copyProperties(item,skuInfoEntity);
                skuInfoEntity.setBrandId(spuInfoEntity.getBrandId());
                skuInfoEntity.setCatalogId(spuInfoEntity.getCatalogId());
                skuInfoEntity.setSaleCount(0L);
                skuInfoEntity.setSpuId(spuInfoEntity.getId());
                //保存默认的图片
                String defaultImg="";
                for(Images images1:item.getImages()){
                    if(images1.getDefaultImg()==1){
                        defaultImg=images1.getImgUrl();
                    }
                }
                skuInfoEntity.setSkuDefaultImg(defaultImg);
                skuInfoService.saveSkuInfo(skuInfoEntity);
                //5.2 sku的图片信息 sku_image
                //处理图片集

                List<SkuImagesEntity> imagesList = item.getImages().stream().map(img -> {
                    SkuImagesEntity skuImagesEntity = new SkuImagesEntity();
                    skuImagesEntity.setSkuId(skuInfoEntity.getSkuId());
                    skuImagesEntity.setImgUrl(img.getImgUrl());
                    skuImagesEntity.setDefaultImg(img.getDefaultImg());
                    return skuImagesEntity;
                    //没有图片的路径不需要保存
                }).filter(entity->{
                    //返回true就是需要,false就是剔除
                    return !StringUtils.isEmpty(entity.getImgUrl());
                }).collect(Collectors.toList());
                skuImagesService.saveBatch(imagesList);//其实早就可以用自己的方法进行保存了...

                //5.3 sku的销售属性值 sku_sale_attr_value
                List<Attr> attr = item.getAttr();
                List<SkuSaleAttrValueEntity> skuSaleAttrValueEntityList = attr.stream().map(a -> {
                    SkuSaleAttrValueEntity skuSaleAttrValueEntity = new SkuSaleAttrValueEntity();
                    BeanUtils.copyProperties(a, skuSaleAttrValueEntity);
                    skuSaleAttrValueEntity.setSkuId(skuInfoEntity.getSkuId());
                    return skuSaleAttrValueEntity;
                }).collect(Collectors.toList());
                skuSaleAttrValueService.saveBatch(skuSaleAttrValueEntityList);
                //以下的服务需要调用远程服务
                //5.4 sku的满减优惠信息 gulimall_sms-》sms_spu_bounds
                SkuReductionTo skuReductionTo=new SkuReductionTo();
                BeanUtils.copyProperties(item,skuReductionTo);
                skuReductionTo.setSkuId(skuInfoEntity.getSkuId());
                //满0减0并且同时没有优惠是没有意义的所以直接剔除 bigDecimal比较大小 比0大就返回1 相等返回0 否则是-1
                if(skuReductionTo.getFullCount()>0||skuReductionTo.getFullPrice().compareTo(new BigDecimal("0"))==1){
                    R r1 = couponFeignService.saveSkuReduction(skuReductionTo);
                    //判断异常
                    if(r1.getCode()!=0){
                        log.error("远程保存spu优惠信息失败");
                    }
                }

                //6 保存spu的积分信息 sms_spu_bounds
                Bounds bounds = vo.getBounds();
                SpuBoundTo spuBoundTo = new SpuBoundTo();
                BeanUtils.copyProperties(bounds,spuBoundTo);
                spuBoundTo.setSpuId(skuInfoEntity.getSkuId());
                R r = couponFeignService.saveSpuBounds(spuBoundTo);
                //判断异常
                if(r.getCode()!=0){
                    log.error("远程保存spu积分信息失败");
                }
            });
        }

    }

 

posted @ 2021-08-10 15:18  一拳超人的逆袭  阅读(105)  评论(0编辑  收藏  举报