一:保存的基本流程
package com.sysg.gulimail.product.vo;
import lombok.Data;
import java.math.BigDecimal;
import java.util.List;
@Data
public class SpuSaveVo {
private String spuName;
private String spuDescription;
private Long catalogId;
private Long brandId;
private BigDecimal weight;
private int publishStatus;
private List<String> decript;
private List<String> images;
private Bounds bounds;
private List<BaseAttrs> baseAttrs;
private List<Skus> skus;
}
- 1.保存spu基本信息pms_spu_info
private String spuName;
private String spuDescription;
private Long catalogId;
private Long brandId;
private BigDecimal weight;
private int publishStatus;
- 2.保存spu描述信息pms_spu_info_desc
private List<String> decript;
- 3.保存spu图片集pms_spu_info_desc
private List<String> images;
- 4.保存spu规格参数pms_product_attr_value
private List<BaseAttrs> baseAttrs;
- 5.保存spu的积分信息:gulimail_sms->sms_spu_bounds
private Bounds bounds;
- 6.保存当前spu对应的sku信息
private List<Skus> skus;
6.1)sku的基本信息pms_sku_info
6.2)sku的图片信息pmd_sku_images
6.3)sku的销售属性pms_sku_sale_attr_value
6.4)sku的优惠满减等信息:gulimail_sms
二:保存方法实现
- 编辑SpuInfoController的save方法
/**
* 保存
*/
@RequestMapping("/save")
//@RequiresPermissions("product:spuinfo:save")
public R save(@RequestBody SpuSaveVo spuInfo){
spuInfoService.saveSpuInfo(spuInfo);
return R.ok();
}
- 编写SpuInfoServiceImpl的saveSpuInfo方法
@Transactional
@Override
public void saveSpuInfo(SpuSaveVo spuInfo) {
}
1.保存spu基本信息 pms_spu_info
//1.保存spu基本信息pms_spu_info
SpuInfoEntity spuInfoEntity = new SpuInfoEntity();
//将spuInfo的属性值赋值给spuInfoEntity
BeanUtils.copyProperties(spuInfo,spuInfoEntity);
spuInfoEntity.setCreateTime(new Date());
spuInfoEntity.setUpdateTime(new Date());
this.saveBaseSpuInfo(spuInfoEntity);
/**
* 保存spu基本信息pms_spu_info
* @param spuInfoEntity
*/
@Override
public void saveBaseSpuInfo(SpuInfoEntity spuInfoEntity) {
this.baseMapper.insert(spuInfoEntity);
}
2.保存spu描述信息 pms_spu_info_desc
//2.保存spu描述信息pms_spu_info_desc
List<String> decript = spuInfo.getDecript();
SpuInfoDescEntity descEntity = new SpuInfoDescEntity();
descEntity.setSpuId(spuInfoEntity.getId());
descEntity.setDecript(String.join(",",decript));
spuInfoDescService.saveSpuInfoDesc(descEntity);
/**
* 保存spu描述信息pms_spu_info_desc
* @param descEntity
*/
@Override
public void saveSpuInfoDesc(SpuInfoDescEntity descEntity) {
this.baseMapper.insert(descEntity);
}
3.保存spu图片集 pms_spu_info_desc
List<String> images = spuInfo.getImages();
spuImagesService.saveImages(spuInfoEntity.getId(),images);
/**
* 保存spu图片集pms_spu_info_desc
* @param id
* @param images
*/
@Override
public void saveImages(Long id, List<String> images) {
if(!CollectionUtils.isEmpty(images)){
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.保存spu规格参数 pms_product_attr_value
List<BaseAttrs> baseAttrs = spuInfo.getBaseAttrs();
List<ProductAttrValueEntity> collect = baseAttrs.stream().map((attr) -> {
ProductAttrValueEntity entity = new ProductAttrValueEntity();
entity.setAttrId(attr.getAttrId());
AttrEntity attrEntity = attrService.getById(attr.getAttrId());
entity.setAttrName(attrEntity.getAttrName());
entity.setAttrValue(attr.getAttrValues());
entity.setQuickShow(attr.getShowDesc());
entity.setSpuId(spuInfoEntity.getId());
return entity;
}).collect(Collectors.toList());
productAttrValueService.saveProductAttr(collect);
/**
* 保存spu规格参数pms_product_attr_value
* @param collect
*/
@Override
public void saveProductAttr(List<ProductAttrValueEntity> collect) {
this.saveBatch(collect);
}
5.保存当前spu对应的sku信息
//6.1)sku的基本信息pms_sku_info
List<Skus> skus = spuInfo.getSkus();
if( skus != null && skus.size() > 0){
skus.stream().forEach((sku)->{
String defaultImg = "";
for (Images img : sku.getImages()){
if(img.getDefaultImg() == 1){
defaultImg = img.getImgUrl();
}
};
SkuInfoEntity skuInfoEntity = new SkuInfoEntity();
BeanUtils.copyProperties(sku,skuInfoEntity);
skuInfoEntity.setBrandId(spuInfoEntity.getBrandId());
skuInfoEntity.setCatalogId(spuInfoEntity.getCatalogId());
skuInfoEntity.setSaleCount(0L);
skuInfoEntity.setSpuId(spuInfoEntity.getId());
skuInfoEntity.setSkuDefaultImg(defaultImg);
skuInfoService.saveSkuInfo(skuInfoEntity);
Long skuId = skuInfoEntity.getSkuId();
//6.2)sku的图片信息pmd_sku_images
List<SkuImagesEntity> imagesEntities = sku.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);
//6.3)sku的销售属性pms_sku_sale_attr_value
List<Attr> attr = sku.getAttr();
List<SkuSaleAttrValueEntity> skuSaleAttrValueEntities = attr.stream().map(a -> {
SkuSaleAttrValueEntity attrValueEntity = new SkuSaleAttrValueEntity();
BeanUtils.copyProperties(a, attrValueEntity);
attrValueEntity.setSkuId(skuId);
return attrValueEntity;
}).collect(Collectors.toList());
skuSaleAttrValueService.saveBatch(skuSaleAttrValueEntities);
//6.4)sku的优惠满减等信息:gulimail_sms
//todo
});
}
5.1)保存sku的基本信息pms_sku_info
/**
* 保存sku的基本信息pms_sku_info
* @param skuInfoEntity
*/
@Override
public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
this.baseMapper.insert(skuInfoEntity);
}
5.2)保存sku的图片信息pmd_sku_images
/**
* 插入(批量)
*
* @param entityList 实体对象集合
*/
@Transactional(rollbackFor = Exception.class)
default boolean saveBatch(Collection<T> entityList) {
return saveBatch(entityList, 1000);
}
5.3)保存sku的销售属性pms_sku_sale_attr_value
skuSaleAttrValueService.saveBatch(skuSaleAttrValueEntities);
三:远程调用实现保存功能
远程调用的条件:
1).需要调用的微服务必须上线,并且注册到注册中心中。
spring:
datasource:
username: root
password: root
url: jdbc:mysql://127.0.0.1:3306/gulimail_sms
driver-class-name: com.mysql.jdbc.Driver
cloud:
#nacos地址
nacos:
discovery:
server-addr: 127.0.0.1:8848
#服务的名称,用于让nacos知道那个服务正在被调用
application:
name: gulimail-coupon
2).需要调用的微服务必须开启服务的注册和发现功能。
在主启动类添加@EnableDiscoveryClient注解
/**
* @EnableDiscoveryClient
* 启用服务的注册发现功能
*/
@EnableDiscoveryClient
3).声明需要调用的包以及方法名
1.在product的主启动类添加@EnableFeignClients注解
@EnableFeignClients(basePackages = "com.sysg.gulimail.product.feign")
@MapperScan("com.sysg.gulimail.product.dao")
@EnableDiscoveryClient
@SpringBootApplication
public class GulimailProductApplication {
public static void main(String[] args) {
SpringApplication.run(GulimailProductApplication.class, args);
}
}
- @EnableFeignClients:用于发现当前包下添加@FeignClient的包
- (basePackages = “com.sysg.gulimail.product.feign”):当包没有主子关系时,也可以通过basePackages属性自动去添加要找到的包。
2.在product包下新建feign包,CouponFeignService接口
package com.sysg.gulimail.product.feign;
import org.springframework.cloud.openfeign.FeignClient;
@FeignClient("gulimail-coupon")
public interface CouponFeignService {
}
- @FeignClient(“gulimail-coupon”)需要远程调用包的名字
3.编辑SpuInfoServiceImpl,实现远程保存
1)注入远程调用CouponFeignService
/**
* 注入远程调用接口
*/
@Autowired
private CouponFeignService couponFeignService;
2)保存spu的积分信息:gulimail_sms->sms_spu_bounds
Bounds bounds = spuInfo.getBounds();
SpuBoundTo spuBoundTo = new SpuBoundTo();
BeanUtils.copyProperties(bounds,spuBoundTo);
spuBoundTo.setSpuId(spuInfoEntity.getId());
R r = couponFeignService.saveSpuBounds(spuBoundTo);
if( r.getCode() != 0){
log.error("远程保存spu积分信息失败");
}
3)在CouponFeignService调用远程方法
调用思路:
1.当调用CouponFeignService.saveSpuBounds(spuBoundTo)传入对象时。
2.通过@RequestBody将SpuBoundTo 转化为json格式
3.找到gulimail-coupon服务,给/coupon/spubounds/save发送请求。将转化的json放到请求体位置
4.对方服务收到请求体里面的json数据。
5.(@RequestBody SpuBoundsEntity spuBoundsEntity)只要传入的SpuBoundTo 和接受的SpuBoundsEntity 可以一一对应,对象不一样,不影响传递。
@FeignClient("gulimail-coupon")
public interface CouponFeignService {
@PostMapping("/coupon/spubounds/save")
R saveSpuBounds(@RequestBody SpuBoundTo spuBoundTo);
}
- @RequestBody:用于读取Http请求的body部分数据——就是我们的请求数据。比如json或者xml。然后把数据绑定到 controller中方法的参数上。
- @ReponseBody:用法:放在controller层的方法上,将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。
- @PostMapping(“/coupon/spubounds/save”):需要调用controller方法的完整路径
/**
* 保存
*/
@RequestMapping("/save")
//@RequiresPermissions("coupon:spubounds:save")
public R save(@RequestBody SpuBoundsEntity spuBoundsEntity){
spuBoundsService.save(spuBoundsEntity);
return R.ok();
}
四:保存sku的优惠满减等信息
1.编辑SpuInfoServiceImpl实现类
//6.4)sku的优惠满减等信息:gulimail_sms
SkuReductionTo skuReductionTo = new SkuReductionTo();
BeanUtils.copyProperties(sku,skuReductionTo);
skuReductionTo.setSkuId(skuId);
R r1 = couponFeignService.saveSkuReduction(skuReductionTo);
if( r1.getCode() != 0){
log.error("远程保存sku优惠信息失败");
}
2.在CouponFeignService实现类添加远程调用方法
@PostMapping("/coupon/skufullreduction/saveinfo")
R saveSkuReduction(@RequestBody SkuReductionTo skuReductionTo);
3.在SkuFullReductionController添加保存方法
@PostMapping("/saveinfo")
//@RequiresPermissions("coupon:skufullreduction:list")
public R saveInfo(@RequestBody SkuReductionTo skuReductionTo){
skuFullReductionService.saveSkuReduction(skuReductionTo);
return R.ok();
}
4.在SkuFullReductionServiceImpl实现saveSkuReduction方法
@Override
public void saveSkuReduction(SkuReductionTo skuReductionTo) {
//1.保存满减打折,会员价
SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
skuLadderEntity.setSkuId(skuLadderEntity.getSkuId());
skuLadderEntity.setFullCount(skuLadderEntity.getFullCount());
skuLadderEntity.setDiscount(skuReductionTo.getDiscount());
skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());
/*skuLadderEntity.setPrice();*/
skuLadderService.save(skuLadderEntity);
//2.保存满减信息
SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
BeanUtils.copyProperties(skuReductionTo,skuFullReductionEntity);
this.save(skuFullReductionEntity);
//3.会员价格
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);
}