商品详情SkuItemVo
1. @Data 2. public class SkuItemVo { 3. 4. /*** 1 sku基本信息的获取:如标题*/ 5. SkuInfoEntity info; 6. 7. boolean hasStock = true; 8. 9. /*** 2 sku的图片信息*/ 10. List<SkuImagesEntity> images; 11. 12. /*** 3 获取spu的销售属性组合。每个attrName对应一个value-list*/ 13. List<SkuItemSaleAttrVo> saleAttr; 14. 15. /*** 4 获取spu的介绍*/ 16. SpuInfoDescEntity desc; 17. 18. /*** 5 获取spu的规格参数信息,每个分组的包含list*/ 19. List<SpuItemAttrGroupVo> groupAttrs; 20. 21. 22. }
SkuItemSaleAttrVo
1. @Data 2. public class SkuItemSaleAttrVo{ 3. private Long attrId; 4. private String attrName; 5. private List<AttrValueWithSkuIdVo> attrValues; 6. }
SpuItemAttrGroupVo
1. @Data 2. public class SpuItemAttrGroupVo{ 3. private String groupName; 4. private List<SpuBaseAttrVo> attrs; 5. }
SpuBaseAttrVo
1. @Data 2. public class SpuBaseAttrVo{ 3. private String attrName; 4. private String attrValue; 5. }
控制层Controller
1. @Controller 2. public class itemController { 3. 4. 5. @Autowired 6. SkuInfoService skuInfoService; 7. 8. /** 9. * 展示当前sku的详情 10. * @param skuId 11. * @return 12. */ 13. @GetMapping("/{skuId}.html") 14. public String skuItem(@PathVariable("skuId") Long skuId, Model model){ 15. 16. System.out.println("准备查询"+skuId+"详情"); 17. SkuItemVo vo= skuInfoService.item(skuId); 18. model.addAttribute("item",vo); 19. return "item"; 20. } 21. }
配置类
ThreadPoolConfigProperties
1. @ConfigurationProperties(prefix = "gulimall.thread") 2. @Component 3. @Data 4. public class ThreadPoolConfigProperties { 5. private Integer coreSize; 6. private Integer maxSize; 7. private Integer keepAliveTime; 8. }
MyThreadConfig
1. @Configuration 2. public class MyThreadConfig { 3. 4. @Bean 5. public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties pool){ 6. return new ThreadPoolExecutor(pool.getCoreSize(),pool.getMaxSize(),pool.getKeepAliveTime(), 7. TimeUnit.SECONDS, 8. new LinkedBlockingDeque<>(1000), 9. Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy()); 10. } 11. }
skuInfoService.item实现
1. @Override 2. public SkuItemVo item(Long skuId) { 3. SkuItemVo skuItemVo = new SkuItemVo(); 4. //1.sku基本属性的获取 pms_sku_info 5. CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> { 6. SkuInfoEntity info = getById(skuId); 7. skuItemVo.setInfo(info); 8. return info; 9. }, executor); 10. //2.sku的图片信息 pms_sku_images 11. 12. CompletableFuture<Void> imgeFuture = CompletableFuture.runAsync(() -> { 13. List<SkuImagesEntity> images = imagesService.getImagesBySKuId(skuId); 14. skuItemVo.setImages(images); 15. }, executor); 16. 17. //3.获取spu的销售属性组合 18. CompletableFuture<Void> attrFuture = infoFuture.thenAcceptAsync((res) -> { 19. List<SkuItemSaleAttrVo> saleAttrVos = skuSaleAttrValueService.getSaleAttrsBySpuId(res.getSpuId()); 20. skuItemVo.setSaleAttr(saleAttrVos); 21. }, executor); 22. 23. //4.获取spu的介绍 pms_spu_info_desc 24. CompletableFuture<Void> descFuture = infoFuture.thenAcceptAsync((res) -> { 25. SpuInfoDescEntity spuInfo = spuInfoDescService.getById(res.getSpuId()); 26. skuItemVo.setDesc(spuInfo); 27. }, executor); 28. 29. 30. //5.获取spu的规格参数信息 31. CompletableFuture<Void> spuFuture = infoFuture.thenAcceptAsync((res) -> { 32. List<SpuItemAttrGroupVo> attrGroupVos = attrGroupService.getAttrGroupWithAttrsBySpuId(res.getSpuId(), res.getCatalogId()); 33. skuItemVo.setGroupAttrs(attrGroupVos); 34. }, executor); 35. 36. try { 37. CompletableFuture.allOf(infoFuture,imgeFuture,descFuture,spuFuture,attrFuture).get(); 38. } catch (InterruptedException e) { 39. e.printStackTrace(); 40. } catch (ExecutionException e) { 41. e.printStackTrace(); 42. } 43. return skuItemVo; 44. }
imagesService.getImagesBySKuId
1. @Override 2. public List<SkuImagesEntity> getImagesBySKuId(Long skuId) { 3. SkuImagesDao imagesDao = this.baseMapper; 4. List<SkuImagesEntity> imagesEntities = imagesDao.selectList(new QueryWrapper<SkuImagesEntity>().eq("sku_id", skuId)); 5. 6. return imagesEntities; 7. }
skuSaleAttrValueService.getSaleAttrsBySpuId
1. @Override 2. public List<SkuItemSaleAttrVo> getSaleAttrsBySpuId(Long spuId) { 3. SkuSaleAttrValueDao dao = this.baseMapper; 4. List<SkuItemSaleAttrVo> saleAttrVos= dao.getSaleAttrsBySpuId(spuId); 5. return saleAttrVos; 6. }
dao.getSaleAttrsBySpuId
<resultMap id="SkuItemSaleAttrVo" type="com.atguigu.gulimall.product.vo.SkuItemSaleAttrVo"> <result column="attr_id" property="attrId"></result> <result column="attr_name" property="attrName"></result> <collection property="attrValues" ofType="com.atguigu.gulimall.product.vo.AttrValueWithSkuIdVo"> <result column="attr_value" property="attrValue"></result> <result column="sku_ids" property="skuIds"></result> </collection> </resultMap> <select id="getSaleAttrsBySpuId" resultMap="SkuItemSaleAttrVo"> SELECT ssav.`attr_id`,ssav.`attr_name`,ssav.`attr_value`, GROUP_CONCAT(DISTINCT info.`sku_id`) sku_ids FROM `pms_sku_info` info LEFT JOIN `pms_sku_sale_attr_value` ssav ON ssav.`sku_id` = info.`sku_id` WHERE info.`spu_id` = #{spuId} GROUP BY ssav.`attr_id`,ssav.`attr_name`,ssav.`attr_value` </select>
attrGroupService.getAttrGroupWithAttrsBySpuId
1. @Override 2. public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) { 3. //1.查出当前spu对应的所有属性的分组信息以及当前分组下所有属性对应的值 4. 5. AttrGroupDao baseMapper = this.getBaseMapper(); 6. List<SpuItemAttrGroupVo> vos = baseMapper.getAttrGroupWithAttrsBySpuId(spuId,catalogId); 7. return vos; 8. }
baseMapper.getAttrGroupWithAttrsBySpuId(spuId,catalogId)
pms_product_attr_value
根据spu_id获得spu相关属性pms_attr_attrgroup_relation
根据catelog_id获得属性的分组
<resultMap id="SpuItemAttrGroupVo" type="com.atguigu.gulimall.product.vo.SpuItemAttrGroupVo"> <result column="attr_group_name" property="groupName" javaType="string"></result> <collection property="attrs" ofType="com.atguigu.gulimall.product.vo.SpuBaseAttrVo"> <result column="attr_name" property="attrName" javaType="string"></result> <result column="attr_value" property="attrValue" javaType="string"></result> </collection> </resultMap>
<select id="getAttrGroupWithAttrsBySpuId" resultMap="SpuItemAttrGroupVo"> SELECT pav.`spu_id`, ag.`attr_group_name`, ag.`attr_group_id`, aar.`attr_id`, attr.`attr_name`,pav.`attr_value` FROM `pms_attr_group` ag LEFT JOIN `pms_attr_attrgroup_relation` aar ON aar.`attr_group_id` = ag.`attr_group_id` LEFT JOIN `pms_attr` attr ON attr.`attr_id` = aar.`attr_id` LEFT JOIN `pms_product_attr_value` pav ON pav.`attr_id` = attr.`attr_id` WHERE ag.catelog_id = #{catalogId} AND pav.`spu_id` = #{spuId} </select>
前端js代码
1. $(".sku_attr_value").click(function () { 2. // 1、点击的元素添加上自定义的属性 3. let skus = new Array(); 4. let curr = $(this).attr("skus").split(","); 5. //去掉同一行的所有的check标签 6. $(this).parent().parent().find(".sku_attr_value").removeClass("checked"); 7. $(this).addClass("checked"); 8. changeCheckedStyle(); 9. 10. $("a[class='sku_attr_value checked']").each(function () { 11. skus.push($(this).attr("skus").split(",")); 12. }); 13. //取出交集得到skuId 14. let filterEle = skus[0]; 15. for (let i = 1; i < skus.length; i++) { 16. filterEle = $(filterEle).filter(skus[i])[0]; 17. } 18. 19. location.href = "http://item.gulimall.com/" + filterEle + ".html"; 20. 21. return false; 22. }); 23. $(function () { 24. changeCheckedStyle(); 25. }); 26. 27. function changeCheckedStyle() { 28. $(".sku_attr_value").parent().css({"border": "solid 1px #ccc"}); 29. $("a[class='sku_attr_value checked']").parent().css({"border": "solid 1px red"}); 30. };