商品管理系統——商品新增遠程調用保存實現部分

一 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);
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章