商品详情展示

This commit is contained in:
2023-10-08 22:07:41 +08:00
parent cfeefe2dc6
commit bf4e5a807d
23 changed files with 390 additions and 18 deletions

View File

@@ -35,6 +35,16 @@
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.atguigu</groupId>
<artifactId>service-activity-client</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,26 @@
package com.atguigu.ssyx.home.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* ClassName: ThreadPoolConfig
* Package: com.atguigu.ssyx.home.config
*
* @author yovinchen
* @Create 2023/10/8 14:47
*/
@Configuration
public class ThreadPoolConfig {
@Bean
public ThreadPoolExecutor threadPoolExecutor() {
return new ThreadPoolExecutor(2, 5, 2, TimeUnit.SECONDS, new ArrayBlockingQueue<>(3), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
}
}

View File

@@ -4,10 +4,12 @@ import com.atguigu.ssyx.client.product.ProductFeignClient;
import com.atguigu.ssyx.common.auth.AuthContextHolder;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.home.service.HomeService;
import com.atguigu.ssyx.home.service.ItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -27,7 +29,8 @@ import java.util.Map;
@RestController
@RequestMapping("api/home")
public class HomeApiController {
@Autowired
private ItemService itemService;
@Resource
private ProductFeignClient productFeignClient;
@Autowired
@@ -42,13 +45,18 @@ public class HomeApiController {
return Result.ok(map);
}
@ApiOperation(value = "获取分类信息")
@GetMapping("category")
public Result category() {
return Result.ok(productFeignClient.findAllCategoryList());
}
@GetMapping("item/{id}")
@ApiOperation("商品详情")
public Result index(@PathVariable Long id) {
Long userId = AuthContextHolder.getUserId();
Map<String, Object> map = itemService.item(id, userId);
return Result.ok(map);
}
}

View File

@@ -0,0 +1,21 @@
package com.atguigu.ssyx.home.service;
import java.util.Map;
/**
* ClassName: ItemService
* Package: com.atguigu.ssyx.home.service
*
* @author yovinchen
* @Create 2023/10/7 15:01
*/
public interface ItemService {
/**
* 商品详情
*
* @param skuId
* @param userId
* @return
*/
Map<String, Object> item(Long skuId, Long userId);
}

View File

@@ -0,0 +1,75 @@
package com.atguigu.ssyx.home.service.impl;
import com.atguigu.ssyx.client.activity.ActivityFeignClient;
import com.atguigu.ssyx.client.product.ProductFeignClient;
import com.atguigu.ssyx.client.search.SkuFeignClient;
import com.atguigu.ssyx.home.service.ItemService;
import com.atguigu.ssyx.vo.product.SkuInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
/**
* ClassName: ItemServiceImpl
* Package: com.atguigu.ssyx.home.service.impl
*
* @author yovinchen
* @Create 2023/10/7 15:02
*/
@Service
public class ItemServiceImpl implements ItemService {
@Autowired
private ProductFeignClient productFeignClient;
@Autowired
private ActivityFeignClient activityFeignClient;
@Autowired
private SkuFeignClient skuFeignClient;
@Resource
private ThreadPoolExecutor threadPoolExecutor;
/**
* 商品详情
*
* @param skuId
* @param userId
* @return
*/
@Override
public Map<String, Object> item(Long skuId, Long userId) {
Map<String, Object> result = new HashMap<>();
//skuId查询
CompletableFuture<SkuInfoVo> skuInfocompletableFuture = CompletableFuture.supplyAsync(() -> {
//远程调用获取sku对应数据
SkuInfoVo skuInfoVo = productFeignClient.getSkuInfoVo(skuId);
result.put("skuInfoVo", skuInfoVo);
return skuInfoVo;
}, threadPoolExecutor);
//sku对应优惠卷信息
CompletableFuture<Void> activityCompletableFuture = CompletableFuture.runAsync(() -> {
//远程调用获取优惠卷
Map<String, Object> activityMap = activityFeignClient.findActivityAndCoupon(skuId, userId);
result.putAll(activityMap);
}, threadPoolExecutor);
//更新商品热度
CompletableFuture<Void> hotCompletableFuture = CompletableFuture.runAsync(() -> {
//远程调用更新热度
skuFeignClient.incrHotScore(skuId);
}, threadPoolExecutor);
//任务组合
CompletableFuture.allOf(skuInfocompletableFuture, activityCompletableFuture, hotCompletableFuture)
.join();
return result;
}
}

View File

@@ -1,5 +1,6 @@
server:
port: 8207
feign:
sentinel:
enabled: true