修正项目
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
package com.yovinchen.xlcs;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
/**
|
||||
* ClassName: ServiceHomeApplication
|
||||
* Package: com.yovinchen.xlcs
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2023/9/25 12:06
|
||||
*/
|
||||
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)//取消数据源自动配置
|
||||
@EnableDiscoveryClient
|
||||
@EnableFeignClients
|
||||
public class ServiceHomeApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ServiceHomeApplication.class, args);
|
||||
}
|
||||
}
|
@@ -0,0 +1,26 @@
|
||||
package com.yovinchen.xlcs.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.yovinchen.xlcs.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());
|
||||
}
|
||||
}
|
@@ -0,0 +1,62 @@
|
||||
package com.yovinchen.xlcs.home.controller;
|
||||
|
||||
import com.yovinchen.xlcs.client.product.ProductFeignClient;
|
||||
import com.yovinchen.xlcs.common.auth.AuthContextHolder;
|
||||
import com.yovinchen.xlcs.common.result.Result;
|
||||
import com.yovinchen.xlcs.home.service.HomeService;
|
||||
import com.yovinchen.xlcs.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;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* ClassName: HomeApiController
|
||||
* Package: com.yovinchen.xlcs.home.controller
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2023/9/25 12:10
|
||||
*/
|
||||
|
||||
@Api(tags = "首页接口")
|
||||
@RestController
|
||||
@RequestMapping("api/home")
|
||||
public class HomeApiController {
|
||||
@Autowired
|
||||
private ItemService itemService;
|
||||
@Resource
|
||||
private ProductFeignClient productFeignClient;
|
||||
@Autowired
|
||||
private HomeService homeService;
|
||||
|
||||
@ApiOperation(value = "获取首页数据")
|
||||
@GetMapping("index")
|
||||
public Result index(HttpServletRequest request) {
|
||||
// 获取用户Id
|
||||
Long userId = AuthContextHolder.getUserId();
|
||||
Map<String, Object> map = homeService.homeData(userId);
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,20 @@
|
||||
package com.yovinchen.xlcs.home.service;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* ClassName: HomeService
|
||||
* Package: com.yovinchen.xlcs.home.service
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2023/9/25 12:12
|
||||
*/
|
||||
public interface HomeService {
|
||||
/**
|
||||
* 获取首页数据
|
||||
*
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
Map<String, Object> homeData(Long userId);
|
||||
}
|
@@ -0,0 +1,21 @@
|
||||
package com.yovinchen.xlcs.home.service;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* ClassName: ItemService
|
||||
* Package: com.yovinchen.xlcs.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);
|
||||
}
|
@@ -0,0 +1,72 @@
|
||||
package com.yovinchen.xlcs.home.service.impl;
|
||||
|
||||
import com.yovinchen.xlcs.client.product.ProductFeignClient;
|
||||
import com.yovinchen.xlcs.client.search.SkuFeignClient;
|
||||
import com.yovinchen.xlcs.client.user.UserFeignClient;
|
||||
import com.yovinchen.xlcs.home.service.HomeService;
|
||||
import com.yovinchen.xlcs.model.product.Category;
|
||||
import com.yovinchen.xlcs.model.product.SkuInfo;
|
||||
import com.yovinchen.xlcs.model.search.SkuEs;
|
||||
import com.yovinchen.xlcs.vo.user.LeaderAddressVo;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* ClassName: HomeServiceImpl
|
||||
* Package: com.yovinchen.xlcs.home.service.impl
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2023/9/25 12:12
|
||||
*/
|
||||
@Service
|
||||
public class HomeServiceImpl implements HomeService {
|
||||
|
||||
|
||||
@Autowired
|
||||
private UserFeignClient userFeignClient;
|
||||
|
||||
@Autowired
|
||||
private ProductFeignClient productFeignClient;
|
||||
|
||||
@Autowired
|
||||
private SkuFeignClient skuFeignClient;
|
||||
|
||||
/**
|
||||
* 获取首页数据
|
||||
*
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Map<String, Object> homeData(Long userId) {
|
||||
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
//1 根据userId获取当前登录用户提货地址信息
|
||||
// 远程调用service-user模块接口获取需要数据
|
||||
LeaderAddressVo leaderAddressVo = userFeignClient.getUserAddressByUserId(userId);
|
||||
result.put("leaderAddressVo", leaderAddressVo);
|
||||
|
||||
//2 获取所有分类
|
||||
// 远程调用service-product模块接口
|
||||
List<Category> categoryList = productFeignClient.findAllCategoryList();
|
||||
result.put("categoryList", categoryList);
|
||||
|
||||
//3 获取新人专享商品
|
||||
// 远程调用service-product模块接口
|
||||
List<SkuInfo> newPersonSkuInfoList = productFeignClient.findNewPersonSkuInfoList();
|
||||
result.put("newPersonSkuInfoList", newPersonSkuInfoList);
|
||||
|
||||
//4 获取爆款商品
|
||||
// 远程调用service-search模块接口
|
||||
// hotscore 热门评分降序排序
|
||||
List<SkuEs> hotSkuList = skuFeignClient.findHotSkuList();
|
||||
result.put("hotSkuList", hotSkuList);
|
||||
|
||||
//5 封装获取数据到map集合,返回
|
||||
return result;
|
||||
}
|
||||
}
|
@@ -0,0 +1,75 @@
|
||||
package com.yovinchen.xlcs.home.service.impl;
|
||||
|
||||
import com.yovinchen.xlcs.client.activity.ActivityFeignClient;
|
||||
import com.yovinchen.xlcs.client.product.ProductFeignClient;
|
||||
import com.yovinchen.xlcs.client.search.SkuFeignClient;
|
||||
import com.yovinchen.xlcs.home.service.ItemService;
|
||||
import com.yovinchen.xlcs.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.yovinchen.xlcs.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;
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
server:
|
||||
port: 8207
|
||||
spring:
|
||||
application:
|
||||
name: service-home
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
server-addr: 82.157.68.223:8848
|
||||
username: nacos
|
||||
password: nacos
|
@@ -0,0 +1,21 @@
|
||||
spring:
|
||||
cloud:
|
||||
nacos:
|
||||
config:
|
||||
namespace: dd5265c5-8290-45bc-9d07-395c14c977d3
|
||||
server-addr: 82.157.68.223:8848
|
||||
group: service
|
||||
username: nacos
|
||||
password: nacos
|
||||
enabled: true
|
||||
file-extension: yml
|
||||
extension-configs:
|
||||
- data-id: common.yml
|
||||
group: common
|
||||
refresh: true
|
||||
- data-id: service-redis.yml
|
||||
group: common
|
||||
refresh: true
|
||||
- data-id: service-openfeign.yml
|
||||
group: common
|
||||
refresh: true
|
Reference in New Issue
Block a user