licailing 3 жил өмнө
parent
commit
7f7ebf852c

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

@@ -28,7 +28,7 @@ public interface ShowCollectionRepo extends JpaRepository<ShowCollection, Long>,
     @Transactional
     void softDeleteByRoom(Long showroomId);
 
-    @Query("update ShowCollection t set t.del = true where t.showroomId in ?1")
+    @Query("update ShowCollection t set t.del = true where t.id in ?1")
     @Modifying
     @Transactional
     void softDeleteByIdIn(Collection<Long> ids);

+ 29 - 12
src/main/java/com/izouma/nineth/service/ShowroomService.java

@@ -41,7 +41,7 @@ public class ShowroomService {
     /*
     不做盲盒
      */
-    public void save(Long userId, Showroom showroom) {
+    public Showroom save(Long userId, Showroom showroom) {
         Asset asset = assetRepo.findById(showroom.getAssetId()).orElseThrow(new BusinessException("资产不存在"));
         if (!userId.equals(asset.getUserId())) {
             throw new BusinessException("该资产不属于你");
@@ -96,10 +96,12 @@ public class ShowroomService {
                 showCollectionRepo.save(showCollection);
             }
         });
+        return show;
     }
 
-    public void update(Showroom showroom) {
+    public Showroom update(Long userId, Showroom showroom) {
         Showroom recordRoom = showroomRepo.findById(showroom.getId()).orElseThrow(new BusinessException("无展厅"));
+        showroom.setMaxCollection(recordRoom.getMaxCollection());
         // 上传的藏品
         List<ShowCollection> showCollections = showroom.getCollections();
 
@@ -107,6 +109,9 @@ public class ShowroomService {
         Map<Long, ShowCollection> showCollectionMap = recordShowColls.stream()
                 .collect(Collectors.toMap(ShowCollection::getId, coll -> coll));
 
+        Map<Long, ShowCollection> showCollectionMap1 = recordShowColls.stream()
+                .collect(Collectors.toMap(ShowCollection::getCollectionId, coll -> coll));
+
         Set<Long> removeRecord = new HashSet<>(showCollectionMap.keySet());
 
         if (CollUtil.isNotEmpty(showCollections)) {
@@ -119,10 +124,15 @@ public class ShowroomService {
                     .filter(show -> ObjectUtils.isEmpty(show.getId()))
                     .map(ShowCollection::getCollectionId)
                     .collect(Collectors.toList());
-            Map<Long, Collection> collectionMap = collectionRepo.findAllByIdIn(collectionIds)
-                    .stream()
-                    .collect(Collectors.toMap(Collection::getId, coll -> coll));
 
+            List<Collection> collections = collectionRepo.findAllByIdIn(collectionIds);
+            Map<Long, Collection> collectionMap = new HashMap<>();
+            collections.forEach(collection -> {
+                if (!userId.equals(collection.getOwnerId())) {
+                    throw new BusinessException("该藏品不属于你");
+                }
+                collectionMap.put(collection.getId(), collection);
+            });
 
             showCollections.forEach(coll -> {
                 if (coll.getId() != null) {
@@ -134,12 +144,19 @@ public class ShowroomService {
                         removeRecord.remove(coll.getId());
                     }
                 } else {
-                    Collection collection = collectionMap.get(coll.getCollectionId());
-                    if (ObjectUtils.isNotEmpty(collection)) {
-                        coll.setPic(collection.getPic().get(0).getUrl());
-                        coll.setShowroomId(recordRoom.getId());
-                        coll.setAssetId(collection.getAssetId());
-                        showCollectionRepo.save(coll);
+                    ShowCollection showCollection = showCollectionMap1.get(coll.getCollectionId());
+                    if (ObjectUtils.isNotEmpty(showCollection)) {
+                        ObjUtils.merge(showCollection, coll);
+                        showCollectionRepo.save(showCollection);
+                        removeRecord.remove(showCollection.getId());
+                    } else {
+                        Collection collection = collectionMap.get(coll.getCollectionId());
+                        if (ObjectUtils.isNotEmpty(collection)) {
+                            coll.setPic(collection.getPic().get(0).getUrl());
+                            coll.setShowroomId(recordRoom.getId());
+                            coll.setAssetId(collection.getAssetId());
+                            showCollectionRepo.save(coll);
+                        }
                     }
                 }
             });
@@ -152,7 +169,7 @@ public class ShowroomService {
         }
 
         ObjUtils.merge(recordRoom, showroom);
-        showroomRepo.save(recordRoom);
+        return showroomRepo.save(recordRoom);
     }
 
 }

+ 14 - 10
src/main/java/com/izouma/nineth/web/ShowroomController.java

@@ -1,16 +1,15 @@
 package com.izouma.nineth.web;
 
 import com.izouma.nineth.domain.Showroom;
-import com.izouma.nineth.repo.ShowCollectionRepo;
-import com.izouma.nineth.service.ShowroomService;
 import com.izouma.nineth.dto.PageQuery;
 import com.izouma.nineth.exception.BusinessException;
+import com.izouma.nineth.repo.ShowCollectionRepo;
 import com.izouma.nineth.repo.ShowroomRepo;
-import com.izouma.nineth.utils.ObjUtils;
+import com.izouma.nineth.service.ShowroomService;
+import com.izouma.nineth.utils.SecurityUtils;
 import com.izouma.nineth.utils.excel.ExcelUtils;
 import lombok.AllArgsConstructor;
 import org.springframework.data.domain.Page;
-import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletResponse;
@@ -28,14 +27,19 @@ public class ShowroomController extends BaseController {
     //@PreAuthorize("hasRole('ADMIN')")
     @PostMapping("/save")
     public Showroom save(@RequestBody Showroom record) {
-        if (record.getId() != null) {
-            Showroom orig = showroomRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
-            ObjUtils.merge(orig, record);
-            return showroomRepo.save(orig);
-        }
-        return showroomRepo.save(record);
+//        if (record.getId() != null) {
+//            Showroom orig = showroomRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
+//            ObjUtils.merge(orig, record);
+//            return showroomRepo.save(orig);
+//        }
+//        return showroomRepo.save(record);
+        return showroomService.save(SecurityUtils.getAuthenticatedUser().getId(), record);
     }
 
+    @PostMapping("/update")
+    public Showroom update(@RequestBody Showroom record) {
+        return showroomService.update(SecurityUtils.getAuthenticatedUser().getId(), record);
+    }
 
     //@PreAuthorize("hasRole('ADMIN')")
     @PostMapping("/all")

+ 2 - 2
src/test/java/com/izouma/nineth/service/ShowroomServiceTest.java

@@ -145,7 +145,7 @@ public class ShowroomServiceTest extends ApplicationTests {
 
         ShowCollection collection11 = ShowCollection.builder()
                 .sort(10)
-                .collectionId(207023L)
+                .collectionId(206067L)
                 .build();
 
         List<ShowCollection> collections = new ArrayList<>();
@@ -167,7 +167,7 @@ public class ShowroomServiceTest extends ApplicationTests {
                 .introduction("这个一个展厅测试")
                 .build();
         showroom.setId(207035L);
-        showroomService.update(showroom);
+        showroomService.update(9972L, showroom);
 
     }
 }