谷粒商城基础篇------新增商品

8.1 调试会员等级相关接口(gulimall-member)

①在guimall-gateway的applicaiton.yml文件中配置路由规则:

- id: member_route
  uri: lb://gulimall-member
  predicates:
    - Path=/api/member/**
  filters:
    - RewritePath=/api/(?<segment>.*),/$\{segment}

②将项目提供的前端代码拷贝到src\views\modules文件夹下面,启动项目,找到用户系统,会员等级

添加会员等级

在这里插入图片描述

8.2 发布商品

8.21 获取分类(category)关联的品牌(brand)

在这里插入图片描述
前端:https://easydoc.xyz/s/78237135/ZUqEdvA4/HgVjlzWV

①在vo包下创建BrandVo

@Data
public class BrandVo {
    /**
     * "brandId": 0,
     * "brandName": "string",
     */
    private Long brandId;
    private String  brandName;
}

②在CategoryBrandRelationController中,获取分类关联的所有品牌:

@GetMapping("/brands/list")
public R relationBrandsList(@RequestParam(value = "catId",required = true) Long catId){
   List<BrandEntity> entities =  categoryBrandRelationService.getBrandsByCatId(catId);
    List<BrandVo> collect = entities.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);
}

③对于 pms_category_brand_relation表:我们可以根据catelog_id查询出brand_id ,根据brand_id查询出品牌
在这里插入图片描述

@Resource
CategoryBrandRelationDao relationDao;
@Resource
BrandService brandService;  
@Override
public List<BrandEntity> getBrandsByCatId(Long catId) {
    //根据catelog_id查询出catId(分类id)下的关联表信息(品牌与分类关联表pms_category_brand_relation)
    List<CategoryBrandRelationEntity> catelogId
            = relationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>()
            .eq("catelog_id", catId));
    //在关联表里查询出brand_id,通过brand_id查询出品牌
List<BrandEntity> collect = catelogId.stream().map((item) -> {//因为一个分类id下有一个集合的brandId(品牌id)
        Long brandId = item.getBrandId();//关联表里查询出brand_id
        BrandEntity brandEntity = brandService.getById(brandId);//通过brand_id查询出品牌
        return brandEntity;
    }).collect(Collectors.toList());
    return collect;//返回所有品牌
}

④测试:分类下的品牌仍然不显示
在这里插入图片描述
原因是前端代码的问题:

//安装pubsub-js
npm install --save pubsub-js
//在main.js中引入
import PubSub from 'pubsub-js'
//在main.js中挂载全局
Vue.prototype.PubSub = PubSub

在这里插入图片描述
再次刷新显示品牌
在这里插入图片描述

8.22 获取分类下所有属性分组&其关联属性

前端:https://easydoc.xyz/s/78237135/ZUqEdvA4/6JM6txHf

获取分类下所有分组,以及该分组下关联的所有属性

①在vo包下创建AttrGroupWithAttrsVo

@Data
public class AttrGroupWithAttrsVo {

    /**
     * 分组id
     */
    private Long attrGroupId;
    /**
     * 组名
     */
    private String attrGroupName;
    /**
     * 排序
     */
    private Integer sort;
    /**
     * 描述
     */
    private String descript;
    /**
     * 组图标
     */
    private String icon;
    /**
     * 所属分类id
     */
    private Long catelogId;

    private List<AttrEntity> attrs;
}

②AttrGroupController 添加

///product/attrgroup/{catelogId}/withattr
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId")Long catelogId){

        //1、查出当前分类下的所有属性分组,
        //2、查出每个属性分组的所有属性
        List<AttrGroupWithAttrsVo> vos =  attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data",vos);
    }

③AttrGroupServiceImpl的 getAttrGroupWithAttrsByCatelogId

@Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        //com.atguigu.gulimall.product.vo
        //1、查询当前分类catelogId下的所有属性分组信息
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        //2、查询其所有分组关联的属性
        List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map(group -> {
            AttrGroupWithAttrsVo attrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(group,attrsVo);//将group分组的数据复制到attrsVo
            List<AttrEntity> attrs = attrService.getRelationAttr(attrsVo.getAttrGroupId());
            attrsVo.setAttrs(attrs);
            return attrsVo;
        }).collect(Collectors.toList());

        return collect;
    }

④测试:
比如查看手机分类下的所有属性分组 及其各个属性分组关联的属性
在这里插入图片描述
在展示时,发现属性的值不能多选(下图已经解决)
在这里插入图片描述
发现规格参数的值类型不能单选,原因是pms_attr表缺少了value_type字段

解决步骤:

原因是数据库里少了value_type字段,把数据库字段添上,再去mapper.xml和对应Entity与Vo中添加即可
(1)在数据库的 pms_attr 表加上value_type字段,类型为tinyint就行;
(2)在代码中,AttyEntity.java、AttrVo.java中各添加:private Integer valueType,
(3)在AttrDao.xml中添加:<result property="valueType" column="value_type"/> 

在这里插入图片描述
再次刷新页面,就可以看到能够单选了
在这里插入图片描述

8.3 新增商品

①将JSON工具生成的代码复制到vo包下
在这里插入图片描述
在这里插入图片描述
接口文档:https://easydoc.xyz/s/78237135/ZUqEdvA4/5ULdV3dd

②product–SpuInfoController里写个商品保存方法

/**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody SpuSaveVo vo){
        spuInfoService.saveSpuInfo(vo);
        return R.ok();
    }

③product–SpuInfoService接口

/**
 * spu信息
 *
 * @author leifengyang
 * @email leifengyang@gmail.com
 * @date 2019-10-01 21:08:49
 */
public interface SpuInfoService extends IService<SpuInfoEntity> {
    PageUtils queryPage(Map<String, Object> params);
    void saveSpuInfo(SpuSaveVo vo);
    void saveBaseSpuInfo(SpuInfoEntity infoEntity);
    PageUtils queryPageByCondition(Map<String, Object> params);
}

④product–SpuInfoServiceImpl

这个实现类里既包含了gulimall-product服务的业务方法,也包括了gulimall-coupon服务的业务方法,所以需要product远程调用

coupon服务。下面写新增商品服务的总的业务步骤,在SpuInfoServiceImpl 。

package com.atguigu.gulimall.product.service.impl;
@Service("spuInfoService")
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoDao, SpuInfoEntity> implements SpuInfoService {
    @Resource
    private SpuInfoDescService spuInfoDescService;
    @Resource
    private SpuImagesService imagesService;
    @Resource
    private AttrService attrService;
    @Resource
    private ProductAttrValueService attrValueService;
    @Resource
    private SkuInfoService skuInfoService;
    @Resource
    private SkuImagesService skuImagesService;
    @Resource
    private SkuSaleAttrValueService skuSaleAttrValueService;
    @Resource
    private CouponFeignService couponFeignService;
    /**
     * //TODO 高级部分完善
     * @param vo
     */
    @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());
        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_sms->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){
            skus.forEach(item->{
                String defaultImg = "";
                for (Images image : item.getImages()) {
                    if(image.getDefaultImg() == 1){
                        defaultImg = image.getImgUrl();
                    }
                }
                //    private String skuName;
                //    private BigDecimal price;
                //    private String skuTitle;
                //    private String skuSubtitle;
                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_sms->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优惠信息失败");
                    }
                }
            });
        }
    }

    @Override
    public void saveBaseSpuInfo(SpuInfoEntity infoEntity) {
        this.baseMapper.insert(infoEntity);
    }
}
8.31 增加product的业务方法(gulimall-product)

①保存spu基本信息:pms_spu_info

SpuInfoServiceImpl :saveBaseSpuInfo

//1、保存spu基本信息:pms_spu_info
        SpuInfoEntity spuInfoEntity = new SpuInfoEntity();
        BeanUtils.copyProperties(vo,spuInfoEntity);
        spuInfoEntity.setCreateTime(new Date());
        spuInfoEntity.setUpdateTime(new Date());
        this.saveBaseSpuInfo(spuInfoEntity);//保存spu基本信息

/**
     * 保存spu基本信息
     * @param spuInfoEntity
     * */
    @Override
    public void saveBaseSpuInfo(SpuInfoEntity spuInfoEntity) {
        this.baseMapper.insert(spuInfoEntity);
    }

②保存spu的描述信息:pms_spu_info_desc

SpuInfoServiceImpl :savePurInfoDesc

//2、保存spu的描述信息:pms_spu_info_desc
        List<String> decript = vo.getDecript();
        SpuInfoDescEntity descEntity = new SpuInfoDescEntity();
        descEntity.setSpuId(spuInfoEntity.getId());//从上面保存的spuInfoEntity数据里拿商品id(spu_id)
        descEntity.setDecript(String.join(",",decript));
        spuInfoDescService.savePurInfoDesc(descEntity);

SpuInfoDescService接口及SpuInfoDescServiceImpl实现

//接口
void savePurInfoDesc(SpuInfoDescEntity descEntity);

//实现类
/**
     * 保存spu的描述信息
     * */
    @Override
    public void savePurInfoDesc(SpuInfoDescEntity descEntity) {
        this.baseMapper.insert(descEntity);
    }

③保存spu的图片集: pms_spu_images

SpuInfoServiceImpl :saveImages

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

SpuImagesService接口及SpuImagesServiceImpl实现

//接口
void saveImages(Long id, List<String> images);

//实现
/**
     * 保存spu的图片集
     * */
    @Override
    public void saveImages(Long id, List<String> images) {
        if(images == null || images.size() == 0){

        }else{
            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);
        }
    }

④保存spu的规格参数属性:pms_product_attr_value

SpuInfoServiceImpl :saveProductAttr

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

ProductAttrValueService接口及ProductAttrValueServiceImpl实现

//接口
void saveProductAttr(List<ProductAttrValueEntity> collect);

//实现
/**
     * 保存spu的规格参数
     * */
    @Override
    public void saveProductAttr(List<ProductAttrValueEntity> collect) {
        this.saveBatch(collect);
    }

⑤保存当前spu对应的所有的sku信息

SpuInfoServiceImpl:5.1基本信息、 5.2图片信息、 5.3 销售属性(同上面spu类似),这里总写。

//5、保存当前spu对应的所有的sku信息
        List<Skus> 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();
                    }
                }
                //5.1 保存sku的基本信息:pms_sku_info
                SkuInfoEntity skuInfoEntity = new SkuInfoEntity();
                BeanUtils.copyProperties(item,skuInfoEntity);
                skuInfoEntity.setBrandId(spuInfoEntity.getBrandId());
                skuInfoEntity.setCatalogId(spuInfoEntity.getCatalogId());
                skuInfoEntity.setSaleCount(0L);
                skuInfoEntity.setSpuId(spuInfoEntity.getId());
                skuInfoEntity.setSkuDefaultImg(defaultImg);
                //只有保存了skuInfoEntity,才能获取自增主键,进而保存skuImagesEntity
                skuInfoService.saveSkuInfo(skuInfoEntity);

                //5.2 保存sku的图片信息:pms_sku_images
                Long skuId = skuInfoEntity.getSkuId();//从上面保存的skuInfoEntity数据里拿商品id(sku_id)
                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;
                }).collect(Collectors.toList());
                skuImagesService.saveBatch(imagesEntities);

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

                //5.4 sku的优惠、满减等信息;gulimall_sms->sms_sku_ladder\sms_sku_full_reduction\sms_member_price
            });
        }

接口和实现类

//-------------5.1---------------------
//SkuInfoService
void saveSkuInfo(SkuInfoEntity skuInfoEntity);
//SkuInfoServiceImpl
@Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.baseMapper.insert(skuInfoEntity);
    }
//-----------------5.2和5.3直接在SpuInfoServiceImpl中写方法---------------------------

以上已经完成业务实现都属于商品服务gulimall-product,而对于剩下的未完成的业务是需要远程调用实现的,因为gulimall-product服务已经无法处理,需要交给gulimall-coupon服务来处理,因此使用openFeign来实现远程调用,让gulimall-product服务来调用gulimall-coupon服务。(5.4要远程调用)

8.32 增加coupon的业务方法(gulimall-coupon)

1、 保存spu的积分信息

① 首先保证gulimall-product和gulimall-coupon服务都在注册中心中,并且在主启动类上都开启了服务注册与发现功能。

② 远程调用openfeign,首先需要在gulimall-product服务中引入openfeign坐标依赖:

这个openfeign依赖放在common公共项目下,所以不需要再导入了

③product主启动类上开启远程服务调用功能:

@EnableFeignClients(basePackages ="com.atguigu.gulimall.product.feign" )
@EnableDiscoveryClient
@SpringBootApplication
public class GulimallProductApplication {
    public static void main(String[] args) {
        SpringApplication.run(GulimallProductApplication.class,args);
    }
}

④ 在gulimall-common服务中定义一个TO对象(两个微服务之间传递对象),作为数据传输对象SpuBoundTo,用来将gulimall-product接收的数据传给gulimall-coupon服务:

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

⑤ product要调用的远程服务为:gulimall-common的SpuBoundsController下的 save方法

package com.atguigu.gulimall.coupon.controller;
@RestController
@RequestMapping("coupon/spubounds")
public class SpuBoundsController {
    @Autowired
    private SpuBoundsService spuBoundsService;
    
    @PostMapping("/save")
    public R save(@RequestBody SpuBoundsEntity spuBounds){
		spuBoundsService.save(spuBounds);
        return R.ok();
    }
}

⑥在product的feign包下创建远程调用接口,这个接口的方法要与调用的方法保持一致(方法名,参数等(或者参数的属性一一对应也行))

//指明要调用的远程服务名称
@FeignClient(value = "gulimall-coupon")
public interface CouponFeignService {
    /**
     * 1、couponFeignService.saveSpuBounds(spuBoundTo)找到这个远程服务
     * 2、将saveSpuBounds(@RequestBody SpuBoundTo spuBounds)请求体转换为json
     * 3、找到gulimall-coupon服务,发送/coupon/spubounds/save请求
     * 4、远程服务save(@RequestBody SpuBoundsEntity spuBounds)收到请求
     *    将请求体中的json转换为SpuBoundsEntity
     */
    @PostMapping("/coupon/spubounds/save")
    public R saveSpuBounds(@RequestBody SpuBoundTo spuBounds);
}

⑦SpuInfoServiceImpl:保存spu的积分信息;gulimall_sms->sms_spu_bounds

在业务类中调用openFeign接口:

@Transactional
@Override
public void saveSpuInfo(SpuSaveVo vo) {
    //5、保存spu的积分信息;gulimall_sms->sms_spu_bounds
    Bounds bounds = vo.getBounds();
    SpuBoundTo spuBoundTo = new SpuBoundTo();
    BeanUtils.copyProperties(bounds,spuBoundTo);
    spuBoundTo.setSpuId(spuInfoEntity.getId());
    //调用远程服务接口
    couponFeignService.saveSpuBounds(spuBoundTo);
}

2、5.4 保存spu的优惠满减信息

gulimall_sms-> sms_sku_ladder sms_sku_full_reduction sms_member_price
在这里插入图片描述
① 继续在com.atguigu.common.to包中定义TO对象,接受请求提交的数据,并在两个服务间传送数据:

@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;
}
@Data
public class MemberPrice {
    private Long id;
    private String name;
    private BigDecimal price;
}

② 被调用的远程服务接口方法:gulimall-common的SkuFullReductionController 下的 saveInfo方法

package com.atguigu.gulimall.coupon.controller;
@RestController
@RequestMapping("coupon/skufullreduction")
public class SkuFullReductionController {
    @Autowired
    private SkuFullReductionService skuFullReductionService;
    //被调用的远程服务的接口方法:
    @PostMapping("/saveinfo")
    public R saveInfo(@RequestBody SkuReductionTo reductionTo){
        skuFullReductionService.saveSkuReduction(reductionTo);//调用其实现类SkuFullReductionServiceImpl方法
        return R.ok();
    }
}

gulimall–coupon ----- SkuFullReductionServiceImpl实现类的saveSkuReduction方法

package com.atguigu.gulimall.coupon.service.impl;
@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    private SkuLadderService skuLadderService;

    @Autowired
    private MemberPriceService memberPriceService;

    @Override
    public void saveSkuReduction(SkuReductionTo reductionTo) {
        //1、// //5.4)、sku的优惠、满减等信息;gulimall_sms->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);
    }
}

③ product定义调用远程服务的接口方法:

package com.atguigu.gulimall.product.feign;

//指明要调用的远程服务名称
@FeignClient(value = "gulimall-coupon")
public interface CouponFeignService {
    @PostMapping("/coupon/spubounds/save")
    public R saveSpuBounds(@RequestBody SpuBoundTo spuBounds);

    @PostMapping("/coupon/skufullreduction/saveinfo")
    public R saveSkuReduction(@RequestBody SkuReductionTo skuReductionTo);
}

④ product在业务类中调用openFeign接口:

@Transactional
@Override
public void saveSpuInfo(SpuSaveVo vo) {
    //5、保存当前spu对应的sku信息
    List<Skus> skus = vo.getSkus();
    if(skus!=null && skus.size()>0){
        skus.forEach(item->{
            //5.4 sku的优惠、满减等信息;gulimall_sms->sms_sku_ladder\sms_sku_full_reduction\sms_member_price
            SkuReductionTo skuReductionTo = new SkuReductionTo();
            BeanUtils.copyProperties(item,skuReductionTo);
            skuReductionTo.setSkuId(skuId);
            R r1 = couponFeignService.saveSkuReduction(skuReductionTo);
            if(r1.getCode()!=0){
                log.error("远程保存spu优惠信息失败");
            }
        });
    }
}

⑤测试

需要添加spuId的值,因为创建表时并没有设置spu_id自增,所以会报空值

@TableId(type = IdType.INPUT)
	private Long spuId;

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值