licailing 3 years ago
parent
commit
52e377529c

+ 3 - 0
src/main/java/com/izouma/nineth/domain/Showroom.java

@@ -53,4 +53,7 @@ public class Showroom extends BaseEntity {
 
     @Transient
     private List<ShowCollection> collections;
+
+    @Transient
+    private boolean liked;
 }

+ 3 - 0
src/main/java/com/izouma/nineth/repo/NewsLikeRepo.java

@@ -7,6 +7,7 @@ import org.springframework.data.jpa.repository.Modifying;
 import org.springframework.data.jpa.repository.Query;
 
 import javax.transaction.Transactional;
+import java.util.Collection;
 import java.util.List;
 
 public interface NewsLikeRepo extends JpaRepository<NewsLike, Long>, JpaSpecificationExecutor<NewsLike> {
@@ -18,4 +19,6 @@ public interface NewsLikeRepo extends JpaRepository<NewsLike, Long>, JpaSpecific
     List<NewsLike> findByUserIdAndNewsId(Long userId, Long newsId);
 
     List<NewsLike> findByUserIdAndShowroomId(Long userId, Long showroomId);
+
+    List<NewsLike> findByUserIdAndShowroomIdIn(Long userId, Collection<Long> showroomId);
 }

+ 2 - 0
src/main/java/com/izouma/nineth/repo/ShowCollectionRepo.java

@@ -23,6 +23,8 @@ public interface ShowCollectionRepo extends JpaRepository<ShowCollection, Long>,
 
     List<ShowCollection> findAllByShowroomId(Long showroomId);
 
+    List<ShowCollection> findAllByShowroomIdIn(Collection<Long> showroomId);
+
     @Query("update ShowCollection t set t.del = true where t.showroomId = ?1")
     @Modifying
     @Transactional

+ 40 - 1
src/main/java/com/izouma/nineth/web/ShowroomController.java

@@ -1,20 +1,29 @@
 package com.izouma.nineth.web;
 
+import cn.hutool.core.collection.CollUtil;
+import com.izouma.nineth.domain.NewsLike;
+import com.izouma.nineth.domain.ShowCollection;
 import com.izouma.nineth.domain.Showroom;
+import com.izouma.nineth.domain.User;
 import com.izouma.nineth.dto.PageQuery;
 import com.izouma.nineth.exception.BusinessException;
+import com.izouma.nineth.repo.NewsLikeRepo;
 import com.izouma.nineth.repo.ShowCollectionRepo;
 import com.izouma.nineth.repo.ShowroomRepo;
 import com.izouma.nineth.service.ShowroomService;
 import com.izouma.nineth.utils.SecurityUtils;
 import com.izouma.nineth.utils.excel.ExcelUtils;
 import lombok.AllArgsConstructor;
+import org.apache.commons.lang3.ObjectUtils;
 import org.springframework.data.domain.Page;
 import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
 
 @RestController
 @RequestMapping("/showroom")
@@ -23,6 +32,7 @@ public class ShowroomController extends BaseController {
     private ShowroomService    showroomService;
     private ShowroomRepo       showroomRepo;
     private ShowCollectionRepo showCollectionRepo;
+    private NewsLikeRepo       newsLikeRepo;
 
     @PostMapping("/save")
     public Showroom save(@RequestBody Showroom record) {
@@ -36,13 +46,42 @@ public class ShowroomController extends BaseController {
 
     @PostMapping("/all")
     public Page<Showroom> all(@RequestBody PageQuery pageQuery) {
-        return showroomService.all(pageQuery);
+        Page<Showroom> all = showroomService.all(pageQuery);
+        List<Long> ids = all.map(Showroom::getId).getContent();
+        Map<Long, List<ShowCollection>> collect = showCollectionRepo.findAllByShowroomIdIn(ids)
+                .stream()
+                .collect(Collectors.groupingBy(ShowCollection::getShowroomId));
+        showroomService.all(pageQuery);
+
+        User user = SecurityUtils.getAuthenticatedUser();
+        Map<Long, Long> likesMap = new HashMap<>();
+        if (user != null && !user.isAdmin() && CollUtil.isNotEmpty(ids)) {
+            List<NewsLike> likes = newsLikeRepo.findByUserIdAndShowroomIdIn(user
+                    .getId(), ids);
+            likesMap = likes.stream()
+                    .collect(Collectors.groupingBy(NewsLike::getShowroomId, Collectors.counting()));
+
+        }
+
+        Map<Long, Long> finalLikesMap = likesMap;
+        return all.map(showroom -> {
+            showroom.setCollections(collect.get(showroom.getId()));
+            showroom.setLiked(ObjectUtils.isNotEmpty(finalLikesMap.get(showroom.getId())));
+            return showroom;
+        });
     }
 
     @GetMapping("/get/{id}")
     public Showroom get(@PathVariable Long id) {
         Showroom showroom = showroomRepo.findById(id).orElseThrow(new BusinessException("无记录"));
         showroom.setCollections(showCollectionRepo.findAllByShowroomId(id));
+        User user = SecurityUtils.getAuthenticatedUser();
+
+        if (user != null && !user.isAdmin()) {
+            List<NewsLike> likes = newsLikeRepo.findByUserIdAndShowroomId(user
+                    .getId(), id);
+            showroom.setLiked(CollUtil.isNotEmpty(likes));
+        }
         return showroom;
     }
 

+ 21 - 9
src/test/java/com/izouma/nineth/service/UserServiceTest.java

@@ -187,14 +187,26 @@ public class UserServiceTest extends ApplicationTests {
     }
 
     @Test
-    public void test1() {
-        List<User> users = userRepo.findAll();
-        users.forEach(user -> {
-            if (user.getVipPoint() > 1) {
-                user.setVipPoint(1);
-            } else {
-
-            }
-        });
+    public void removeBank() throws BaseAdaPayException {
+        Long[] arr = {2577842L, 1440523L, 2775597L, 2766468L, 2686690L, 2747709L,
+                2804878L, 1737822L, 1692606L, 1690986L, 12895L, 2768872L,
+                2758319L, 1722623L, 2767026L, 2742354L, 2772909L, 2813187L,
+                2592513L, 1245862L, 2581606L, 2775941L, 1649287L, 2764550L,
+                2660763L, 2609032L, 2777065L, 2589915L, 2810643L, 3114342L,
+                2897490L, 2579233L, 2728905L, 3064530L, 3261900L, 1634473L,
+                2798840L, 3146142L, 2872925L, 3309420L, 3618828L, 2786593L,
+                3337840L, 3253588L, 2745257L, 3454559L, 3474642L, 3468716L,
+                3363882L, 2685228L, 3088476L, 2900428L, 2705763L, 2687527L,
+                2787733L, 2737842L, 3324792L, 3147077L, 3145577L, 3142936L,
+                3385528L, 2888036L, 3366975L, 2842334L, 2864480L, 3277214L,
+                3260809L, 2867983L, 3246561L, 3472246L, 3302523L, 2882982L,
+                3150301L, 2782692L, 3082410L, 3043665L, 3448907L, 3421038L,
+                3371851L, 3225869L, 3087358L, 3463444L, 3478718L, 3434422L,
+                2784575L, 2622956L, 2700225L, 1635188L, 2715660L, 620172L,
+                3418309L, 3476261L, 2597211L, 3293018L, 3261495L, 3443258L
+        };
+        for (Long aLong : arr) {
+            userService.removeBankCard(aLong);
+        }
     }
 }