Browse Source

API support UserTag UserRecommend module

guozhaoshun 6 years ago
parent
commit
e25a1087b2

+ 91 - 28
rankin-api-web/src/main/java/cn/rankin/apiweb/controller/RecommendController.java

@@ -4,11 +4,13 @@ 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.apiweb.service.user.UserService;
 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 cn.rankin.data.api.user.entity.UserRecommend;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -27,6 +29,9 @@ public class RecommendController {
     private ProductService productService;
 
     @Autowired
+    private UserService userService;
+
+    @Autowired
     private EventService eventService;
 
     public static final int RECOMMEND_NUM = 5;
@@ -40,58 +45,103 @@ public class RecommendController {
         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){
+        List<String> pids = eventService.getCoursesFromLogs(user.getUid());
+        if(pids != null && pids.size() > 0){
+            log.info("get course ids from logs, courseIds={}", pids);
             //如果有浏览历史,提取浏览历史前五展示    (目前推荐位为5)
-            courseIds.forEach(courseId -> {
+            pids.forEach(pid -> {
                 if(result.size() <= RECOMMEND_NUM){
-                    Course course = productService.getCourse(courseId);
+                    Course course = productService.getCourse(pid);
                     if(course != null){
-                        RecommendVo vo = new RecommendVo();
+                        /*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());
+                        vo.setCoverUrl(course.getCoverUrl());*/
+                        RecommendVo vo = courseToRecommendVo(course);
                         result.add(vo);
+                    }else{
+                        log.info("not found course by pid, pid={}", pid);
                     }
                 }
             });
 
-
             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();
+                //浏览历史少于五,追加用户推荐,补足五  (目前推荐位为5)
+                String uid = user.getUid();
+                List<UserRecommend> userData = userService.getUserRecommendCourses(uid);
+                if(null != userData && userData.size() > 0){
+                    log.info("get user recommend course , size={}", userData.size());
+                    userData.forEach(userRecommend -> {
+                        if(result.size() < RECOMMEND_NUM){
+                            Course course = productService.getCourse(userRecommend.getPid());
+                            if(course != null){
+                                RecommendVo vo = courseToRecommendVo(course);
+                                //如果用户浏览课程不包含推荐课程,追加推荐课程 |(此处为避免推荐位课程重复)
+                                if(!result.contains(vo)){
+                                    result.add(vo);
+                                }
+                            }else{
+                                log.info("not found course by pid, pid={}", userRecommend.getPid());
+                            }
+                        }
+                    });
+                }else{
+                    //浏览历史少于五,没有用户推荐  追加   渠道推荐,补足五  (目前推荐位为5)
+                    String merchantId = user.getMerchantId();
+                    APIResult<List<RecommendVo>> apiResult = productService.getRecommendCourses(merchantId);
+                    if(!apiResult.getSuccess()){
+                        return apiResult;
+                    }
 
-                log.info("get recommend course , size={}", data.size());
+                    List<RecommendVo> data = apiResult.getData();
+                    if(data != null){
+                        log.info("get merchant recommend course , size={}", data.size());
+                        data.forEach(vo -> {
+                            if(result.size() < RECOMMEND_NUM){
+                                //如果用户浏览课程不包含推荐课程,追加推荐课程 |(此处为避免推荐位课程重复)
+                                if(!result.contains(vo)){
+                                    result.add(vo);
+                                }
+                            }
+                        });
+                    }
+                }
+            }
+            return APIResult.ok(result);
+        }else{
 
-                data.forEach(vo -> {
+            //不存在浏览历史,使用用户推荐
+            String uid = user.getUid();
+            List<UserRecommend> userData = userService.getUserRecommendCourses(uid);
+            if(null != userData && userData.size() > 0){
+                log.info("get user recommend course , size={}", userData.size());
+                userData.forEach(userRecommend -> {
                     if(result.size() < RECOMMEND_NUM){
-                        //如果用户浏览课程不包含推荐课程,追加推荐课程 |(此处为避免推荐位课程重复)
-                        if(!result.contains(vo)){
-                            result.add(vo);
+                        Course course = productService.getCourse(userRecommend.getPid());
+                        if(course != null){
+                            RecommendVo vo = courseToRecommendVo(course);
+                            //如果用户浏览课程不包含推荐课程,追加推荐课程 |(此处为避免推荐位课程重复)
+                            if(!result.contains(vo)){
+                                result.add(vo);
+                            }
+                        }else{
+                            log.info("not found course by pid, pid={}", userRecommend.getPid());
                         }
                     }
                 });
-
+                return APIResult.ok(result);
+            }else{
+                //不存在浏览历史,不存在用户推荐;使用渠道推荐
+                String merchantId = user.getMerchantId();
+                return productService.getRecommendCourses(merchantId);
             }
-            return APIResult.ok(result);
-        }else{
-            //不存在浏览历史,直接使用推荐
-            String merchantId = user.getMerchantId();
-            return productService.getRecommendCourses(merchantId);
         }
+    }
 
 
-    }
 
     /**
      * Get recommend posters
@@ -119,4 +169,17 @@ public class RecommendController {
 
         return APIResult.ok(data);
     }
+
+
+
+    private RecommendVo courseToRecommendVo(Course course) {
+        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());
+        return vo;
+    }
 }

+ 44 - 1
rankin-api-web/src/main/java/cn/rankin/apiweb/controller/TagController.java

@@ -1,21 +1,35 @@
 package cn.rankin.apiweb.controller;
 
 import cn.rankin.apiweb.assist.resolver.NeedUser;
+import cn.rankin.apiweb.service.product.ProductService;
 import cn.rankin.apiweb.service.tag.TagService;
+import cn.rankin.apiweb.service.user.UserService;
 import cn.rankin.common.utils.api.model.APIResult;
 import cn.rankin.common.utils.api.page.Page;
 import cn.rankin.data.api.app.vo.*;
+import cn.rankin.data.api.user.entity.UserTag;
+import cn.rankin.data.api.user.entity.UserTagProductRelation;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.util.CollectionUtils;
 import org.springframework.web.bind.annotation.*;
 
+import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
-
+@Slf4j
 @RestController
 public class TagController {
 
     @Autowired
     private TagService tagService;
 
+    @Autowired
+    private UserService userService;
+
+    @Autowired
+    private ProductService productService;
+
     @RequestMapping(value = "/tagGroup/{code}", method = RequestMethod.GET)
     public APIResult<TagGroupVo> getTagGroup(@NeedUser DeviceUserVo user, @PathVariable("code") String code) {
         String merchantId = user.getMerchantId();
@@ -31,7 +45,36 @@ public class TagController {
 
     @RequestMapping(value = "/tagType/{code}", method = RequestMethod.GET)
     public APIResult<List<TagVo>> getTagType(@NeedUser DeviceUserVo user,@PathVariable("code") String code) {
+        List<TagVo> result = new ArrayList<>();
+
+        //先去取用户标签,没有用户标签再使用渠道标签
+        String uid = user.getUid();
         String merchantId = user.getMerchantId();
+
+        //用户标签列表
+        List<UserTag> userTagList = userService.findUserTagByCodeUid(code,uid);
+        if(null != userTagList && userTagList.size() > 0){
+            log.info("get userTag By code and uid, size={}", userTagList.size());
+            //组装数据   用户标签下   课程数据
+            userTagList.forEach(userTag -> {
+                List<UserTagProductRelation> relationList = userTag.getRelationList();
+                log.info("userTag relationList , size={}", relationList);
+                if(!CollectionUtils.isEmpty(relationList)){
+                    List<String> productIdList = new ArrayList<>();
+                    relationList.forEach(relation -> productIdList.add(relation.getPid()) );
+                    List<ItemVo> itemVoList = productService.findItemByPids(productIdList,merchantId);
+
+                    TagVo vo = new TagVo();
+                    vo.setId(userTag.getId());
+                    vo.setName(userTag.getName());
+                    vo.setRecs(itemVoList);
+                    result.add(vo);
+                }
+            });
+            return APIResult.ok(result);
+        }
+
+        //没有用户标签数据,使用渠道标签数据
         return tagService.findTagTypeByCode(code, merchantId);
     }
 

+ 9 - 0
rankin-api-web/src/main/java/cn/rankin/apiweb/service/product/ProductClient.java

@@ -82,6 +82,10 @@ public interface ProductClient {
     @RequestMapping(value = "/app/course/{id}", method = RequestMethod.GET)
     Course getCourse(@PathVariable("id") String id);
 
+    @RequestMapping(value = "/item/pids", method = RequestMethod.POST)
+    List<ItemVo> findItemByPids(@RequestBody List<String> productIdList,
+                                @RequestParam("merchantId") String merchantId);
+
 
     @Component
     class ProductClientHystrix implements ProductClient {
@@ -180,5 +184,10 @@ public interface ProductClient {
             return null;
         }
 
+        @Override
+        public List<ItemVo> findItemByPids(List<String> productIdList, String merchantId) {
+            return null;
+        }
+
     }
 }

+ 5 - 0
rankin-api-web/src/main/java/cn/rankin/apiweb/service/product/ProductService.java

@@ -110,4 +110,9 @@ public class ProductService {
         return productClient.getCourse(id);
     }
 
+
+    public List<ItemVo> findItemByPids(List<String> productIdList, String merchantId) {
+        return productClient.findItemByPids(productIdList,merchantId);
+    }
+
 }

+ 20 - 0
rankin-api-web/src/main/java/cn/rankin/apiweb/service/user/UserClient.java

@@ -3,6 +3,8 @@ package cn.rankin.apiweb.service.user;
 import cn.rankin.apiweb.code.ApiWebCode;
 import cn.rankin.common.utils.api.model.APIResult;
 import cn.rankin.data.api.user.dto.TerminalDeviceDTO;
+import cn.rankin.data.api.user.entity.UserRecommend;
+import cn.rankin.data.api.user.entity.UserTag;
 import cn.rankin.data.api.user.vo.TerminalDeviceVo;
 import cn.rankin.data.api.user.vo.TerminalUserVo;
 import cn.rankin.data.api.user.vo.WhiteUserVo;
@@ -10,6 +12,8 @@ import org.springframework.cloud.netflix.feign.FeignClient;
 import org.springframework.stereotype.Component;
 import org.springframework.web.bind.annotation.*;
 
+import java.util.List;
+
 @FeignClient(name = "${service.user.name}", fallback = UserClient.UserServiceHystrix.class)
 public interface UserClient {
 
@@ -34,6 +38,12 @@ public interface UserClient {
     @RequestMapping(value = "/white/user/{userId}", method = RequestMethod.GET)
     APIResult<WhiteUserVo> findWhiteUserById(@PathVariable("userId") String userId);
 
+    @RequestMapping(value = "/userRecommend/courses/uid/{uid}", method = RequestMethod.GET)
+    List<UserRecommend> getUserRecommendCourses(@RequestParam("uid") String uid);
+
+    @RequestMapping(value = "/userTag/code/{code}", method = RequestMethod.GET)
+    List<UserTag> findUserTagByCodeUid(@PathVariable("code") String code, @RequestParam("uid") String uid);
+
 
     @Component
     class UserServiceHystrix implements UserClient {
@@ -73,5 +83,15 @@ public interface UserClient {
             return APIResult.error(ApiWebCode.SERVER_ERROR);
         }
 
+        @Override
+        public List<UserRecommend> getUserRecommendCourses(String uid) {
+            return null;
+        }
+
+        @Override
+        public List<UserTag> findUserTagByCodeUid(String code, String uid) {
+            return null;
+        }
+
     }
 }

+ 10 - 1
rankin-api-web/src/main/java/cn/rankin/apiweb/service/user/UserService.java

@@ -11,8 +11,9 @@ import cn.rankin.common.utils.service.RedisService;
 import cn.rankin.data.api.app.dto.LoginInfoDTO;
 import cn.rankin.data.api.app.vo.DeviceUserVo;
 import cn.rankin.data.api.app.vo.UserInfoVo;
-import cn.rankin.data.api.product.entity.Product;
 import cn.rankin.data.api.user.dto.TerminalDeviceDTO;
+import cn.rankin.data.api.user.entity.UserRecommend;
+import cn.rankin.data.api.user.entity.UserTag;
 import cn.rankin.data.api.user.vo.TerminalDeviceVo;
 import cn.rankin.data.api.user.vo.TerminalUserVo;
 import com.alibaba.fastjson.JSON;
@@ -261,4 +262,12 @@ public class UserService {
         //TODO: move this method to userCient
         return productClient.getProductValid(uid, 0L, 50, "endTime", Sort.Direction.ASC);
     }
+
+    public List<UserTag> findUserTagByCodeUid(String code, String uid) {
+        return userClient.findUserTagByCodeUid(code, uid);
+    }
+
+    public List<UserRecommend> getUserRecommendCourses(String uid) {
+        return userClient.getUserRecommendCourses(uid);
+    }
 }