|
@@ -2,11 +2,14 @@ package cn.rankin.apiweb.controller;
|
|
|
|
|
|
import cn.rankin.apiweb.assist.resolver.NeedUser;
|
|
|
import cn.rankin.apiweb.code.ApiWebCode;
|
|
|
+import cn.rankin.apiweb.service.event.EventService;
|
|
|
import cn.rankin.apiweb.service.product.ProductService;
|
|
|
import cn.rankin.common.utils.api.model.APIResult;
|
|
|
import cn.rankin.data.api.app.vo.DeviceUserVo;
|
|
|
import cn.rankin.data.api.app.vo.RecommendVo;
|
|
|
+import cn.rankin.data.api.product.entity.Course;
|
|
|
import cn.rankin.data.api.product.entity.Poster;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
|
@@ -15,7 +18,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
-
|
|
|
+@Slf4j
|
|
|
@RestController
|
|
|
@RequestMapping(value = "/recommend")
|
|
|
public class RecommendController {
|
|
@@ -23,14 +26,65 @@ public class RecommendController {
|
|
|
@Autowired
|
|
|
private ProductService productService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private EventService eventService;
|
|
|
+
|
|
|
+ public static final int RECOMMEND_NUM = 5;
|
|
|
+
|
|
|
@RequestMapping(value = "/courses", method = RequestMethod.GET)
|
|
|
public APIResult<List<RecommendVo>> getRecommend(@NeedUser DeviceUserVo user) {
|
|
|
if (user == null) {
|
|
|
return APIResult.error(ApiWebCode.NOT_EXISTS);
|
|
|
}
|
|
|
|
|
|
- String merchantId = user.getMerchantId();
|
|
|
- return productService.getRecommendCourses(merchantId);
|
|
|
+ List<RecommendVo> result = new ArrayList<>();
|
|
|
+
|
|
|
+ //查询浏览历史课程ID,已去重
|
|
|
+ List<String> courseIds = eventService.getCoursesFromLogs(user.getUid());
|
|
|
+// log.info("get course ids from logs, courseIds={}", courseIds);
|
|
|
+ if(courseIds != null && courseIds.size() > 0){
|
|
|
+ //如果有浏览历史,提取浏览历史前五展示 (目前推荐位为5)
|
|
|
+ courseIds.forEach(courseId -> {
|
|
|
+ if(result.size() <= RECOMMEND_NUM){
|
|
|
+ Course course = productService.getCourse(courseId);
|
|
|
+ if(course != null){
|
|
|
+ RecommendVo vo = new RecommendVo();
|
|
|
+ vo.setId(course.getId());
|
|
|
+ vo.setCode(course.getCode());
|
|
|
+ vo.setTitle(course.getTitle());
|
|
|
+ vo.setSubTitle(course.getSubTitle());
|
|
|
+ vo.setBreadCrumb(course.getBreadCrumb());
|
|
|
+ vo.setCoverUrl(course.getCoverUrl());
|
|
|
+ result.add(vo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
+ if(result.size() < RECOMMEND_NUM){
|
|
|
+ //浏览历史少于五,追加推荐,补足五 (目前推荐位为5)
|
|
|
+ String merchantId = user.getMerchantId();
|
|
|
+ APIResult<List<RecommendVo>> apiResult = productService.getRecommendCourses(merchantId);
|
|
|
+ if(!apiResult.getSuccess()){
|
|
|
+ return apiResult;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<RecommendVo> data = apiResult.getData();
|
|
|
+
|
|
|
+ log.info("get recommend course , size={}", data.size());
|
|
|
+ int num = RECOMMEND_NUM - result.size();
|
|
|
+ for (int i = 0; i < num; i++) {
|
|
|
+ result.add(data.get(i));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return APIResult.ok(result);
|
|
|
+ }else{
|
|
|
+ //不存在浏览历史,直接使用推荐
|
|
|
+ String merchantId = user.getMerchantId();
|
|
|
+ return productService.getRecommendCourses(merchantId);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
|
|
|
/**
|