第二十三章第一节:商品服务:保存商品信息
一、保存spu基本信息
1、保存业务
com.applesnt.onlinemall.product.service.impl.SpuInfoServiceImpl
/*注入spu操作service*/
@Autowired
SpuInfoService spuInfoService;
@Transactional/*开启事务*/
@Override
public void saveSpuInfo(SpuSaveVo vo) {
//1、保存spu基本信息 pms_spu_info
SpuInfoEntity spuInfo =new SpuInfoEntity();
/*由于两个vo的基本字段一致,用属性对拷进行字段赋值*/
BeanUtils.copyProperties(vo,spuInfo);
/*实体类多出的两个字段单独进行赋值*/
spuInfo.setCreateTime(new Date());
spuInfo.setUpdateTime(new Date());
//调用保存方法saveBaseSpuInfo
this.spuInfoService.saveBaseSpuInfo(spuInfo);
......
2、定义spu基本信息的保存方法接口
com.applesnt.onlinemall.product.service.SpuInfoService
/*保存spu的基本数据*/
void saveBaseSpuInfo(SpuInfoEntity spuInfoEntity);
3、定义spu基本信息的保存方法接口实现
com.applesnt.onlinemall.product.service.impl.SpuInfoServiceImpl
@Override
public void saveBaseSpuInfo(SpuInfoEntity spuInfoEntity) {
this.baseMapper.insert(spuInfoEntity);
}
二、保存spu的产品介绍图片
1、保存业务
com.applesnt.onlinemall.product.service.impl.SpuInfoServiceImpl
/*注入spu产品图片介绍service*/
@Autowired
SpuInfoDescService spuInfoDescService;
@Transactional/*开启事务*/
@Override
public void saveSpuInfo(SpuSaveVo vo) {
......
//2、保存spu的产品介绍图片 pms_spu_info_desc
/*获取描述图片集合*/
List<String> decript = vo.getDecript();
SpuInfoDescEntity spuInfoDesc = new SpuInfoDescEntity();
spuInfoDesc.setSpuId(spuInfo.getId());
/*处理成字符串 用逗号隔开*/
spuInfoDesc.setDecript(String.join(",",decript));
//调用保存方法saveSpuInfoDesc
this.spuInfoDescService.saveSpuInfoDesc(spuInfoDesc);
......
2、定义spu产品介绍图片的保存方法接口
com.applesnt.onlinemall.product.service.SpuInfoDescService
/*保存spu的描述图片*/
void saveSpuInfoDesc(SpuInfoDescEntity spuInfoDesc);
3、定义spu产品介绍图片的保存方法接口实现
com.applesnt.onlinemall.product.service.impl.SpuInfoDescServiceImpl
@Override
public void saveSpuInfoDesc(SpuInfoDescEntity spuInfoDesc) {
this.baseMapper.insert(spuInfoDesc);
}
三、保存spu的图片集
1、保存业务
com.applesnt.onlinemall.product.service.impl.SpuInfoServiceImpl
/*注入spu图片集service*/
@Autowired
SpuImagesService spuImagesService;
@Transactional/*开启事务*/
@Override
public void saveSpuInfo(SpuSaveVo vo) {
......
//3、保存spu的图片集 pms_spu_images
List<String> images = vo.getImages();
this.spuImagesService.saveImages(spuInfo.getId(),images);
......
2、定义spu图片集的保存方法接口
com.applesnt.onlinemall.product.service.SpuImagesService
//保存spu的图片集
void saveImages(Long id, List<String> images);
3、定义spu图片集的保存方法接口实现
com.applesnt.onlinemall.product.service.impl.SpuImagesServiceImpl
@Override
public void saveImages(Long id, List<String> images) {
if(images !=null && images.size() > 0){
/*遍历images list 重新组合成数据库操作对象进行数据保存*/
List<SpuImagesEntity> collect = images.stream().map((img) -> {
SpuImagesEntity imagesEntity = new SpuImagesEntity();
imagesEntity.setSpuId(id);
imagesEntity.setImgUrl(img);
return imagesEntity;
}).collect(Collectors.toList());
this.saveBatch(collect);
}
}
四、保存spu的规格参数
1、保存业务
com.applesnt.onlinemall.product.service.impl.SpuInfoServiceImpl
/*注入规格参数service*/
@Autowired
AttrService attrService;
/*注入spu的规格参数service*/
@Autowired
ProductAttrValueService productAttrValueService;
@Transactional/*开启事务*/
@Override
public void saveSpuInfo(SpuSaveVo vo) {
......
//4、保存spu的规格参数 pms_product_attr_value
List<BaseAttrs> baseAttrs = vo.getBaseAttrs();
/*遍历baseAttrs,重新组合成数据库操作对象ProductAttrValueEntity,进行保存*/
List<ProductAttrValueEntity> collect = baseAttrs.stream().map((attr) -> {
ProductAttrValueEntity valueEntity = new ProductAttrValueEntity();
valueEntity.setAttrId(attr.getAttrId());
/*通过attrid 获取规格参数实体对象*/
AttrEntity byId = attrService.getById(attr.getAttrId());
if(byId!=null && byId.getAttrName()!=null){
/*从attr实体对象中获取规格参数的名称*/
valueEntity.setAttrName(byId.getAttrName());
}
valueEntity.setAttrValue(attr.getAttrValues());
valueEntity.setQuickShow(attr.getShowDesc());
valueEntity.setSpuId(spuInfo.getId());
return valueEntity;
}).collect(Collectors.toList());
this.productAttrValueService.saveProductAttr(collect);
......
2、定义spu规格参数的保存方法接口
com.applesnt.onlinemall.product.service.ProductAttrValueService
/*保存spu的规格参数*/
void saveProductAttr(List<ProductAttrValueEntity> collect);
3、定义spu规格参数的保存方法接口实现
com.applesnt.onlinemall.product.service.impl.ProductAttrValueServiceImpl
@Override
public void saveProductAttr(List<ProductAttrValueEntity> collect) {
this.saveBatch(collect);
}
五、保存spu的积分信息
需要远程调用保存积分信息:
1:把onlinemall-coupon和onlinemall-product项目注册到注册中心中
2:开启onlinemall-product项目的远程调用服务
3:在onlinemall-product项目中创建远程调用的feign包
4:在onlinemall-product的feign包下创建积分远程调用接口CouponFeignService
5:SpuInfoServiceImpl注入优惠券的远程调用接口
6:远程调用之间传输的叫TO 创建SpuBoundTo 放在onlinemall-common公共项目的to包下
1、创建积分远程调用接口
com.applesnt.onlinemall.product.feign.CouponFeignService
package com.applesnt.onlinemall.product.feign;
import com.applesnt.common.to.SkuReductionTo;
import com.applesnt.common.to.SpuBoundTo;
import com.applesnt.common.utils.R;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
/*调用哪个远程服务*/
@FeignClient("onlinemall-coupon")
public interface CouponFeignService {
/*
* 说明:
* RequestBody会把SpuBoundTo转为json,
* 接收方实体对象的属性名如果和当前实体对象的属性名一致,
* 对方无需用SpuBoundTo接收也可以
* */
@PostMapping("coupon/spubounds/save")
R saveSpuBounds(@RequestBody SpuBoundTo spuBoundTo);
}
2、开启onlinemall-product远程调用服务
com.applesnt.onlinemall.product.OnlinemallProductApplication
package com.applesnt.onlinemall.product;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/*开启远程调用服务 明确扫描哪个包*/
@EnableFeignClients(basePackages = "com.applesnt.onlinemall.product.feign")
@EnableDiscoveryClient
@SpringBootApplication
public class OnlinemallProductApplication {
public static void main(String[] args) {
SpringApplication.run(OnlinemallProductApplication.class, args);
}
}
3、在onlinemall-common项目中创建SpuBoundTo实体对象
com.applesnt.common.to.SpuBoundTo
package com.applesnt.common.to;
import lombok.Data;
import java.math.BigDecimal;
@Data
public class SpuBoundTo {
//spu Id
private Long spuId;
//购物积分
private BigDecimal buyBounds;
//成长积分
private BigDecimal growBounds;
}
4、保存业务
com.applesnt.onlinemall.product.service.impl.SpuInfoServiceImpl
/*注入优惠券的远程调用接口*/
@Autowired
CouponFeignService couponFeignService;
@Transactional/*开启事务*/
@Override
public void saveSpuInfo(SpuSaveVo vo) {
......
//5、保存spu的积分信息 mall_sms-》sms_spu_bounds
Bounds bounds = vo.getBounds();
SpuBoundTo spuBoundTo = new SpuBoundTo();
BeanUtils.copyProperties(bounds,spuBoundTo);
spuBoundTo.setSpuId(spuInfo.getId());
//调用远程保存方法saveSkuInfo
couponFeignService.saveSpuBounds(spuBoundTo);
......
5、onlinemall-coupon远程被调用业务
com.applesnt.onlinemall.coupon.controller.SpuBoundsController
save方法为代码生成器自动生成
@PostMapping("/save")
public R save(@RequestBody SpuBoundsEntity spuBounds){
spuBoundsService.save(spuBounds);
return R.ok();
}
六、保存当前spu对应的sku信息
1、保存业务
com.applesnt.onlinemall.product.service.impl.SpuInfoServiceImpl
/*注入sku的基本信息service*/
@Autowired
SkuInfoService skuInfoService;
/*注入sku 图片service*/
@Autowired
SkuImagesService skuImagesService;
/*注入sku 销售属性service*/
@Autowired
SkuSaleAttrValueService skuSaleAttrValueService;
@Transactional/*开启事务*/
@Override
public void saveSpuInfo(SpuSaveVo vo) {
......
//6、保存当前spu对应的sku信息
//6.1、保存sku的基本信息 pms_sku_info
List<Skus> skus = vo.getSkus();
if(skus!=null&&skus.size()>0){
skus.forEach(item->{
SkuInfoEntity skuInfoEntity = new SkuInfoEntity();
//属性对拷
BeanUtils.copyProperties(item,skuInfoEntity);
//其他值设置
/*品牌id*/
skuInfoEntity.setBrandId(spuInfo.getBrandId());
/*分类id*/
skuInfoEntity.setCatalogId(spuInfo.getCatalogId());
/*销量*/
skuInfoEntity.setSaleCount(0L);
/*spu ID*/
skuInfoEntity.setSpuId(spuInfo.getId());
/*当前sku的默认图片*/
String defaultImg = "";
for (Images image : item.getImages()) {
if(image.getDefaultImg()==1){
defaultImg = image.getImgUrl();
}
}
skuInfoEntity.setSkuDefaultImg(defaultImg);
//调用保存方法saveSkuInfo
skuInfoService.saveSkuInfo(skuInfoEntity);
//sku保存成功后,获取到skuId
Long skuId = skuInfoEntity.getSkuId();
//6.2、保存sku的图片信息 pms_sku_images
List<SkuImagesEntity> collect1 = 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(collect1);
//6.3、保存sku的销售属性信息 pms_sku_sale_attr_value
List<Attr> attrs = item.getAttr();
List<SkuSaleAttrValueEntity> collect2 = attrs.stream().map(attr -> {
SkuSaleAttrValueEntity skuSaleAttrValueEntity = new SkuSaleAttrValueEntity();
BeanUtils.copyProperties(attr, skuSaleAttrValueEntity);
skuSaleAttrValueEntity.setSkuId(skuId);
return skuSaleAttrValueEntity;
}).collect(Collectors.toList());
//调用保存方法
skuSaleAttrValueService.saveBatch(collect2);
//6.4、保存sku的优惠满减信息(远程调用服务)mall_sms-》sms_sku_ladder/sms_sku_full_reduction/sms_member_price
/*
*1:在common中创建TO SkuReductionTo、MemberPrice
*2:编写远程调用的接口saveSkuReduction
*3:编写被调用的saveInfo 满减信息保存业务方法
* */
SkuReductionTo skuReductionTo = new SkuReductionTo();
//数据对拷
BeanUtils.copyProperties(item,skuReductionTo);
//设置skuId
skuReductionTo.setSkuId(skuId);
R r2= couponFeignService.saveSkuReduction(skuReductionTo);
System.out.println("sku的优惠满减信息:"+r2);
});
}
......
2、定义6.1中保存sku的基本信息的接口
com.applesnt.onlinemall.product.service.SkuInfoService
/*sku基本信息保存*/
void saveSkuInfo(SkuInfoEntity skuInfoEntity);
3、定义6.1中保存sku的基本信息的接口实现
com.applesnt.onlinemall.product.service.impl.SkuInfoServiceImpl
@Override
public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
this.baseMapper.insert(skuInfoEntity);
}
4、定义6.4中保存sku的优惠满减信息的to
com.applesnt.common.to.SkuReductionTo
package com.applesnt.common.to;
import lombok.Data;
import java.math.BigDecimal;
import java.util.List;
/*满减信息*/
@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;
}
com.applesnt.common.to.MemberPrice
package com.applesnt.common.to;
import lombok.Data;
import java.math.BigDecimal;
/*会员价格信息*/
@Data
public class MemberPrice {
//修改数据类型
private Long id;
private String name;
private BigDecimal price;
}
5、定义6.4中保存sku的优惠满减信息的远程调用接口
com.applesnt.onlinemall.product.feign.CouponFeignService
@PostMapping("/coupon/skufullreduction/saveinfo")
R saveSkuReduction(@RequestBody SkuReductionTo skuReductionTo);
6、定义6.4中保存sku的优惠满减信息的被调用业务实现(onlinemall-coupon)
com.applesnt.onlinemall.coupon.controller.SkuFullReductionController
/**
* 远程调用保存满减信息
*/
@RequestMapping("/saveinfo")
public R saveInfo(@RequestBody SkuReductionTo skuReductionTo){
skuFullReductionService.saveSkuReduction(skuReductionTo);
return R.ok();
}
7、定义6.4中保存sku的优惠满减信息的被调用业务接口(onlinemall-coupon)
com.applesnt.onlinemall.coupon.service.SkuFullReductionService
void saveSkuReduction(SkuReductionTo skuReductionTo);
8、定义6.4中保存sku的优惠满减信息的被调用业务接口实现(onlinemall-coupon)
com.applesnt.onlinemall.coupon.service.impl.SkuFullReductionServiceImpl
@Autowired
SkuLadderService skuLadderService;
@Autowired
MemberPriceService memberPriceService;
@Override
public void saveSkuReduction(SkuReductionTo skuReductionTo) {
//保存sku的优惠满减信息mall_sms-》sms_sku_ladder/sms_sku_full_reduction/sms_member_price
//保存sms_sku_ladder信息
SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
BeanUtils.copyProperties(skuReductionTo,skuLadderEntity);
skuLadderService.save(skuLadderEntity);
//保存sms_sku_full_reduction信息
SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
BeanUtils.copyProperties(skuReductionTo,skuFullReductionEntity);
this.save(skuFullReductionEntity);
//保存sms_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);
}