文章目录
- 销售属性
- 分组规格参数
- 异步编排
- application.yml
- MyThreadConfig.java
- ThreadPoolConfigProperties.java
- SkuInfoServiceImpl.java
销售属性
sku表:tb_sku_info
sku对应销售属性表:tb_sku_sale_attr_value
结果
在详情页系统中,切换属性的时候,需要获取商品唯一属性skuid,为了快速获取设计了如上模式
分组规格参数
分组表:tb_attr_group(一个分类多个分组)
中间表:tb_attr_attrgroup_relation
分组属性表:tb_product_attr_value
异步编排
application.yml
# 线程池相关的配置
cubemall:thread:core-pool-size: 20max-pool-size: 200keep-alive-time: 10
MyThreadConfig.java
package com.xd.cubemall.product.config;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;@Configuration
public class MyThreadConfig {@AutowiredThreadPoolConfigProperties configProperties;@Beanpublic ThreadPoolExecutor getThreadPool(){// 创建线程池对象ThreadPoolExecutor executor = new ThreadPoolExecutor(configProperties.getCorePoolSize(),configProperties.getMaxPoolSize(),configProperties.getKeepAliveTime(),TimeUnit.SECONDS,new LinkedBlockingDeque<>(100),Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());return executor;}}
ThreadPoolConfigProperties.java
package com.xd.cubemall.product.config;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Component
@ConfigurationProperties(prefix = "cubemall.thread")
@Data
public class ThreadPoolConfigProperties {private Integer corePoolSize;private Integer maxPoolSize;private Integer keepAliveTime;
}
SkuInfoServiceImpl.java
@Autowiredprivate SkuImagesService skuImagesService;//注入销售属性组合服务@Autowiredprivate SkuSaleAttrValueService skuSaleAttrValueService;//注入spu描述服务@Autowiredprivate SpuInfoDescService spuInfoDescService;@Autowiredprivate AttrGroupService attrGroupService;@Autowiredprivate ThreadPoolExecutor executor;@Overridepublic SkuItemVo skuItem(Long skuId) {// 新建一个包装类对象SkuItemVo itemVo = new SkuItemVo();/*1.sku基本信息2.sku图片信息(多个图片)3.spu的销售属性4.spu的描述信息5.sku分组规格参数属性值*/// 1.根据skuId 查询 sku基本信息// 开启异步编排实现,提升服务性能CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {SkuInfoEntity skuInfoEntity = this.getById(skuId);itemVo.setInfo(skuInfoEntity);return skuInfoEntity;}, executor);// 2.根据skuId查询sku图片信息(多个图片),skuId是外键CompletableFuture<Void> imagesFuture = CompletableFuture.runAsync(() -> {List<SkuImagesEntity> imageList = skuImagesService.list(new QueryWrapper<SkuImagesEntity>().eq("sku_id", skuId));itemVo.setImages(imageList);},executor);//3.根据spuID获取spu的销售属性CompletableFuture<Void> salesFuture = infoFuture.thenAcceptAsync((res) -> {// 获取sku与之对应的spuIdLong spuId = res.getSpuId();List<SkuItemSaleAttrVo> saleAttrVos = skuSaleAttrValueService.getSaleAttrs(spuId);if (saleAttrVos.size()>0 && saleAttrVos!=null){itemVo.setAttrSales(saleAttrVos);}},executor);//4.根据spuId查询spu的描述信息CompletableFuture<Void> descFuture = infoFuture.thenAcceptAsync((res) -> {// 获取sku与之对应的spuIdLong spuId = res.getSpuId();SpuInfoDescEntity spuInfoDescEntity = spuInfoDescService.getOne(new QueryWrapper<SpuInfoDescEntity>().eq("spu_id",spuId));if(spuInfoDescEntity!=null){itemVo.setDesc(spuInfoDescEntity);}},executor);//5.根据spuID,categoryId查询 sku分组规格参数属性值CompletableFuture<Void> groupFuture = infoFuture.thenAcceptAsync((res) -> {// 获取sku与之对应的spuIdLong spuId = res.getSpuId();// 获取分类idLong categoryId = res.getCategoryId();List<SpuAttrGroupVo> attrGroupVos = attrGroupService.getGroupAttr(spuId,categoryId);if(attrGroupVos.size()>0){itemVo.setAttrGroups(attrGroupVos);}},executor);//等待所有的任务完成后,才能返回结果try {CompletableFuture.allOf(infoFuture,imagesFuture,salesFuture,descFuture,groupFuture).get();} catch (InterruptedException e) {e.printStackTrace();} catch (ExecutionException e) {e.printStackTrace();}return itemVo;}