Browse Source

add UserRecommend module

guozhaoshun 6 years ago
parent
commit
3f669d5d8d

+ 34 - 0
rankin-user-service/src/main/java/cn/rankin/userservice/controller/UserRecommendController.java

@@ -0,0 +1,34 @@
+package cn.rankin.userservice.controller;
+
+import cn.rankin.data.api.user.entity.UserRecommend;
+import cn.rankin.userservice.service.UserRecommendService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
+
+@RestController
+@RequestMapping(value = "/userRecommend")
+public class UserRecommendController {
+
+    @Autowired
+    private UserRecommendService userRecommendService;
+
+    @RequestMapping(value = "/uid",method = RequestMethod.GET)
+    public List<UserRecommend> get(@RequestParam("uid") String uid) {
+        List<UserRecommend> recommendList = userRecommendService.get(uid);
+        return recommendList;
+    }
+
+    @RequestMapping(value = "/uid",method = RequestMethod.PUT)
+    public List<UserRecommend> put(@RequestParam("uid") String uid, @RequestBody List<String> productIdList) {
+        List<UserRecommend> recommendList = userRecommendService.put(uid, productIdList);
+        return recommendList;
+    }
+
+    @RequestMapping(value = "/courses/uid/{uid}", method = RequestMethod.GET)
+    public List<UserRecommend> getUserRecommendCourses(@RequestParam("uid") String uid) {
+        List<UserRecommend> recommendList = userRecommendService.getUserRecommendCourses(uid);
+        return recommendList;
+    }
+}

+ 17 - 0
rankin-user-service/src/main/java/cn/rankin/userservice/repository/UserRecommendRepository.java

@@ -0,0 +1,17 @@
+package cn.rankin.userservice.repository;
+
+import cn.rankin.common.utils.enums.BaseStatusEnum;
+import cn.rankin.common.utils.jpa.BasicJpaRepository;
+import cn.rankin.data.api.user.entity.UserRecommend;
+import org.springframework.data.jpa.repository.Query;
+import org.springframework.data.repository.query.Param;
+
+import java.util.List;
+
+public interface UserRecommendRepository extends BasicJpaRepository<UserRecommend, String> {
+
+    @Query(value = "select r from UserRecommend r where r.uid = :uid and r.status = :status order by r.sort")
+    List<UserRecommend> findByStatus(@Param("uid") String uid, @Param("status") BaseStatusEnum status);
+
+    List<UserRecommend> findByUid(String uid);
+}

+ 156 - 0
rankin-user-service/src/main/java/cn/rankin/userservice/service/UserRecommendService.java

@@ -0,0 +1,156 @@
+package cn.rankin.userservice.service;
+
+import cn.rankin.common.utils.enums.BaseStatusEnum;
+import cn.rankin.common.utils.util.ListUtil;
+import cn.rankin.data.api.product.entity.Course;
+import cn.rankin.data.api.product.entity.MerchantProduct;
+import cn.rankin.data.api.product.entity.Product;
+import cn.rankin.data.api.product.entity.Recommend;
+import cn.rankin.data.api.user.entity.UserRecommend;
+//import cn.rankin.productservice.repository.CourseRepository;
+//import cn.rankin.productservice.repository.MerchantProductRepository;
+//import cn.rankin.productservice.repository.ProductRepository;
+import cn.rankin.userservice.repository.UserRecommendRepository;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+@Service
+public class UserRecommendService {
+
+//    @Autowired
+//    private MerchantProductRepository merchantProductRepository;
+//
+//    @Autowired
+//    private ProductRepository productRepository;
+//
+//    @Autowired
+//    private CourseRepository courseRepository;
+
+    @Autowired
+    private UserRecommendRepository userRecommendRepository;
+
+    public List<UserRecommend> get(String uid) {
+        List<UserRecommend> recommendList = userRecommendRepository.findByStatus(uid, BaseStatusEnum.NORMAL);
+        if (CollectionUtils.isEmpty(recommendList)) {
+            return new ArrayList<>();
+        }
+
+        return recommendList;
+    }
+
+    /*public List<Recommend> setProductInfo(List<Recommend> recommendList) {
+        if (CollectionUtils.isEmpty(recommendList)) {
+            return new ArrayList<>();
+        }
+
+        List<String> productIdList = new ArrayList<>();
+        recommendList.forEach(recommend -> productIdList.add(recommend.getPid()));
+//        List<MerchantProduct> merchantProductList = merchantProductRepository.findByPidIn(productIdList);
+//        Map<String, MerchantProduct> merchantProductMap = ListUtil.convert(merchantProductList, "pid", MerchantProduct.class);
+        List<Product> productList = productRepository.findByPids(productIdList);
+        Map<String, Product> productMap = ListUtil.convert(productList, "pid", Product.class);
+
+        List<Recommend> result = new ArrayList<>();
+        for (Recommend recommend : recommendList) {
+            Product product = productMap.get(recommend.getPid());
+            if (product == null) {
+                continue;
+            }
+            recommend.setName(product.getName());
+            recommend.setCode(product.getCode());
+            result.add(recommend);
+        }
+        return result;
+    }*/
+
+    /*public List<Recommend> setCourseInfo(List<Recommend> recommendList) {
+        if (CollectionUtils.isEmpty(recommendList)) {
+            return new ArrayList<>();
+        }
+
+        List<String> productIdList = new ArrayList<>();
+        recommendList.forEach(recommend -> productIdList.add(recommend.getPid()));
+        List<Course> courseList = courseRepository.findByIds(productIdList);
+        Map<String, Course> courseMap = ListUtil.convert(courseList, "id", Course.class);
+
+        List<Recommend> result = new ArrayList<>();
+        for (Recommend recommend : recommendList) {
+            Course course = courseMap.get(recommend.getPid());
+            if (course == null) {
+                continue;
+            }
+            recommend.setName(course.getName());
+            recommend.setCode(course.getCode());
+            recommend.setBreadCrumb(course.getBreadCrumb());
+            recommend.setTitle(course.getTitle());
+            recommend.setSubTitle(course.getSubTitle());
+            recommend.setCoverUrl(course.getCoverUrl());
+            result.add(recommend);
+        }
+        return result;
+    }*/
+
+    public List<UserRecommend> put(String uid, List<String> productIdList) {
+        if (CollectionUtils.isEmpty(productIdList)) {
+            return null;
+        }
+
+        List<UserRecommend> recommendList = userRecommendRepository.findByUid(uid);
+        List<String> existRecommendIdList = new ArrayList<>();
+
+        // 更新现存的
+        recommendList.forEach(recommend -> {
+            String productId = recommend.getPid();
+            if (productIdList.contains(productId)) {
+                recommend.setStatus(BaseStatusEnum.NORMAL);
+            }else {
+                recommend.setStatus(BaseStatusEnum.DEL);
+            }
+            existRecommendIdList.add(productId);
+        });
+
+        // 更新新加的
+      /*  List<String> notExistsRecommendIdList = ListUtil.subtract(productIdList, existRecommendIdList);
+        if (notExistsRecommendIdList != null && notExistsRecommendIdList.size() > 0) {
+            List<MerchantProduct> merchantProductList = merchantProductRepository.findByPidIn(notExistsRecommendIdList);
+            Map<String, MerchantProduct> merchantProductMap = ListUtil.convert(merchantProductList, "pid", MerchantProduct.class);
+            for (String productId : notExistsRecommendIdList) {
+                MerchantProduct merchantProduct = merchantProductMap.get(productId);
+                *//*if (merchantProduct == null || !merchantProduct.getMerchantId().equals(merchantId)) {
+                    continue;
+                }*//*
+                UserRecommend recommend = new UserRecommend();
+                recommend.setPid(productId);
+                recommend.setStatus(BaseStatusEnum.NORMAL);
+                recommend.setUid(uid);
+                recommend.setType(merchantProduct.getType());
+                recommendList.add(recommend);
+            }
+        }*/
+
+        // 排序
+        recommendList.forEach( recommend -> {
+            String productId = recommend.getPid();
+            Integer sort = productIdList.indexOf(productId);
+            recommend.setSort(sort);
+        });
+
+        userRecommendRepository.save(recommendList);
+        List<UserRecommend> result = userRecommendRepository.findByUid(uid);
+        return result;
+    }
+
+    public List<UserRecommend> getUserRecommendCourses(String uid) {
+        List<UserRecommend> recommendList = userRecommendRepository.findByStatus(uid, BaseStatusEnum.NORMAL);
+        if (CollectionUtils.isEmpty(recommendList)) {
+            return new ArrayList<>();
+        }
+
+        return recommendList;
+    }
+}