|
@@ -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;
|
|
|
+ }
|
|
|
+}
|