商品管理系统——商品新增远程调用保存实现部分

23 篇文章 4 订阅

一 SpuInfoServiceImpl.java

/**
* 功能描述:保存商品信息
*
* @param vo 待保存的商品信息
* @author cakin
* @date 2020/11/8
*/
@Transactional
@Override
public void saveSpuInfo(SpuSaveVo vo) {
    // 1 保存spu基本信息 pms_spu_info
    SpuInfoEntity infoEntity = new SpuInfoEntity();
    BeanUtils.copyProperties(vo, infoEntity);
    infoEntity.setCreateTime(new Date());
    infoEntity.setUpdateTime(new Date());
    this.saveBaseSpuInfo(infoEntity);


    // 2 保存Spu的描述图片 pms_spu_info_desc
    List<String> decript = vo.getDecript();
    SpuInfoDescEntity descEntity = new SpuInfoDescEntity();
    descEntity.setSpuId(infoEntity.getId());
    // 用逗号拼接List中的每一个元素
    descEntity.setDecript(String.join(",", decript));
    spuInfoDescService.saveSpuInfoDesc(descEntity);


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


    // 4 保存spu的规格参数 pms_product_attr_value
    List<BaseAttrs> baseAttrs = vo.getBaseAttrs();
    List<ProductAttrValueEntity> 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的积分信息 gulimall_coupon sms_spu_bounds
    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信息
    List<Skus> skus = vo.getSkus();
    if (skus != null && skus.size() > 0) {
        // 依次遍历每一个sku
        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);
            // 5.1 sku的基本信息 pms_sku_info
            skuInfoService.saveSkuInfo(skuInfoEntity);
            Long skuId = skuInfoEntity.getSkuId();
            List<SkuImagesEntity> 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的图片信息 pms_sku_image
            skuImagesService.saveBatch(imagesEntities);
            // TODO 没有图片路径的无需保存


            List<Attr> attr = item.getAttr();
            List<SkuSaleAttrValueEntity> skuSaleAttrValueEntities = attr.stream().map(a -> {
                SkuSaleAttrValueEntity attrValueEntity = new SkuSaleAttrValueEntity();
                BeanUtils.copyProperties(a, attrValueEntity);
                attrValueEntity.setSkuId(skuId);
                return attrValueEntity;
            }).collect(Collectors.toList());
            // 5.3 sku的销售属性信息 pms_sku_sale_attr_value
            skuSaleAttrValueService.saveBatch(skuSaleAttrValueEntities);


            // 5.4 sku的优惠、满减等信息 gulimall_coupon sms_sku_ladder\sms_sku_full_reduction\sms_member_price
            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("远程保存sku优惠信息失败");
                }
            }
        });
    }
}

二 SkuReductionTo

/**
* @className: SkuReductionTo
* @description: sku的优惠以及满减信息
* @date: 2020/11/8
* @author: cakin
*/
@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;
}

三 MemberPrice

/**
* @className: MemberPrice
* @description: 会员价格
* @date: 2020/11/8
* @author: cakin
*/
@Data
public class MemberPrice {
    private Long id;
    private String name;
    private BigDecimal price;
}

四 SpuBoundTo

/**
* @className: SpuBoundTo
* @description: SPU积分To,用于不同微服务对接使用
* @date: 2020/11/8
* @author: cakin
*/
@Data
public class SpuBoundTo {
    private Long spuId;
    private BigDecimal buyBounds;
    private BigDecimal growBounds;
}

五 CouponFeignService

/**
* @className: CouponFeignService
* @description: 调用优惠卷服务的 Feign 服务
* @date: 2020/11/8
* @author: cakin
*/
// 调用哪个远程服务
@FeignClient("gulimall-coupon")
public interface CouponFeignService {
    /**
     * 功能描述:远程保存积分信息
     *
     * @param spuBoundTo 待保存的积分信息
     * @return R 返回给前端的数据
     * @author cakin
     * @date 2020/11/8
     * @description:
     * 1 @RequestBody将这个对象转为json。
     * 2 注册中心中找到 gulimall-coupon服务,给 /coupon/spubounds/save 发送请求。
     *   将上一步转的json放在请求体位置,发送请求;
     * 3 对方服务收到请求。请求体里有json数据。
     *   (@RequestBody SpuBoundsEntity spuBounds);将请求体的json转为SpuBoundsEntity;
     *
     * 总结:只要json数据模型是兼容的。双方服务无需使用同一个to(传输对象)
     */
    @PostMapping("/coupon/spubounds/save")
    R saveSpuBounds(@RequestBody SpuBoundTo spuBoundTo);


    /**
     * 功能描述:sku的优惠、满减等信息的保存
     *
     * @author cakin
     * @date 2020/11/8
     * @param skuReductionTo sku的优惠、满减等信息
     * @return R 返回给前端的数据
     */
    @PostMapping("/coupon/skufullreduction/saveinfo")
    R saveSkuReduction(@RequestBody SkuReductionTo skuReductionTo);
}

六 SpuBoundsController

 /** 
   * 功能描述:保存积分信息
   *
   * @author cakin
   * @date 2020/11/8
   * @param spuBounds spu积分信息
   */
  @PostMapping("/save")
  public R save(@RequestBody SpuBoundsEntity spuBounds){
spuBoundsService.save(spuBounds);
      return R.ok();
  }

七 SkuFullReductionController

/**
* 功能描述:保存优惠满减信息 
*
* @param reductionTo 优惠满减信息,从商品微服务传递过来的
* @return R 返回给商品微服务的数据
* @author cakin
* @date 2020/11/8
*/
@PostMapping("/saveinfo")
public R saveInfo(@RequestBody SkuReductionTo reductionTo) {
    skuFullReductionService.saveSkuReduction(reductionTo);
    return R.ok();
}

八 SkuFullReductionServiceImpl

/**
* 功能描述:保存满减优惠信息 
*
* @param reductionTo 满减优惠信息
* @author cakin
* @date 2020/11/8
*/
@Override
public void saveSkuReduction(SkuReductionTo reductionTo) {
    //1 sku的优惠、满减等信息 涉及表 sms_sku_ladder sms_sku_full_reduction sms_member_price
    // sms_sku_ladder
    SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
    skuLadderEntity.setSkuId(reductionTo.getSkuId());
    skuLadderEntity.setFullCount(reductionTo.getFullCount());
    skuLadderEntity.setDiscount(reductionTo.getDiscount());
    skuLadderEntity.setAddOther(reductionTo.getCountStatus());
    if (reductionTo.getFullCount() > 0) {
        skuLadderService.save(skuLadderEntity);
    }


    // 2 sms_sku_full_reduction
    SkuFullReductionEntity reductionEntity = new SkuFullReductionEntity();
    BeanUtils.copyProperties(reductionTo, reductionEntity);
    if (reductionEntity.getFullPrice().compareTo(new BigDecimal("0")) == 1) {
        this.save(reductionEntity);
    }


    // 3 sms_member_price
    List<MemberPrice> memberPrice = reductionTo.getMemberPrice();
    List<MemberPriceEntity> collect = memberPrice.stream().map(item -> {
        MemberPriceEntity priceEntity = new MemberPriceEntity();
        priceEntity.setSkuId(reductionTo.getSkuId());
        priceEntity.setMemberLevelId(item.getId());
        priceEntity.setMemberLevelName(item.getName());
        priceEntity.setMemberPrice(item.getPrice());
        priceEntity.setAddOther(1);
        return priceEntity;
    }).filter(item -> {
        return item.getMemberPrice().compareTo(new BigDecimal("0")) == 1;
    }).collect(Collectors.toList());
    memberPriceService.saveBatch(collect);
}

九 GulimallProductApplication

// 开启远程调用功能,以及 feign 在哪个包下 
@EnableFeignClients(basePackages = "com.atguigu.gulimall.product.feign")
// 开启服务注册功能
@EnableDiscoveryClient
@MapperScan("com.atguigu.gulimall.product.dao")
@SpringBootApplication
public class GulimallProductApplication {
    public static void main(String[] args) {
        SpringApplication.run(GulimallProductApplication.class, args);
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值