licailing 3 years ago
parent
commit
bdfcfa1c95

+ 2 - 0
src/main/java/com/izouma/nineth/domain/Asset.java

@@ -197,6 +197,8 @@ public class Asset extends BaseEntity {
                 .ownerAvatar(user.getAvatar())
                 .type(collection.getType())
                 .holdDays(collection.getHoldDays())
+                .maxCollection(collection.getMaxCollection())
+                .showroomBg(collection.getShowroomBg())
                 .build();
     }
 

+ 6 - 0
src/main/java/com/izouma/nineth/repo/ShowCollectionRepo.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 ShowCollectionRepo extends JpaRepository<ShowCollection, Long>, JpaSpecificationExecutor<ShowCollection> {
@@ -27,4 +28,9 @@ public interface ShowCollectionRepo extends JpaRepository<ShowCollection, Long>,
     @Transactional
     void softDeleteByRoom(Long showroomId);
 
+    @Query("update ShowCollection t set t.del = true where t.showroomId = ?1")
+    @Modifying
+    @Transactional
+    void softDeleteByIdIn(Collection<Long> ids);
+
 }

+ 27 - 16
src/main/java/com/izouma/nineth/service/ShowroomService.java

@@ -22,9 +22,7 @@ import org.springframework.data.domain.Page;
 import org.springframework.stereotype.Service;
 
 import javax.persistence.Id;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 import java.util.stream.Collectors;
 
 @Service
@@ -51,7 +49,7 @@ public class ShowroomService {
         if (!CollectionType.SHOWROOM.equals(asset.getType())) {
             throw new BusinessException("不是展厅藏品");
         }
-        if (AssetStatus.NORMAL.equals(asset.getStatus())) {
+        if (!AssetStatus.NORMAL.equals(asset.getStatus())) {
             throw new BusinessException("该状态不可创建展厅");
         }
         if (asset.isConsignment()) {
@@ -72,6 +70,7 @@ public class ShowroomService {
 
         List<Long> collectionIds = showCollections.stream()
                 .map(ShowCollection::getCollectionId)
+                .distinct()
                 .collect(Collectors.toList());
 
         List<Collection> collections = collectionRepo.findAllByIdIn(collectionIds);
@@ -100,16 +99,22 @@ public class ShowroomService {
     }
 
     public void update(Showroom showroom) {
-        Showroom room = showroomRepo.findById(showroom.getId()).orElseThrow(new BusinessException("无展厅"));
-        if (CollUtil.isNotEmpty(room.getCollections())) {
-            List<ShowCollection> collections = showCollectionRepo.findAllByShowroomId(showroom.getId());
-            Map<Long, ShowCollection> showCollectionMap = collections.stream()
-                    .collect(Collectors.toMap(ShowCollection::getId, coll -> coll));
+        Showroom recordRoom = showroomRepo.findById(showroom.getId()).orElseThrow(new BusinessException("无展厅"));
+        // 上传的藏品
+        List<ShowCollection> showCollections = showroom.getCollections();
+
+        List<ShowCollection> recordShowColls = showCollectionRepo.findAllByShowroomId(showroom.getId());
+        Map<Long, ShowCollection> showCollectionMap = recordShowColls.stream()
+                .collect(Collectors.toMap(ShowCollection::getId, coll -> coll));
+
+        Set<Long> removeRecord = new HashSet<>(showCollectionMap.keySet());
 
-            if (room.getMaxCollection() < collections.size()) {
+        if (CollUtil.isNotEmpty(showCollections)) {
+
+            if (recordRoom.getMaxCollection() < showCollections.size()) {
                 throw new BusinessException("选择的藏品数量大于最多可放置数量");
             }
-            List<Long> collectionIds = room.getCollections()
+            List<Long> collectionIds = showCollections
                     .stream()
                     .filter(show -> ObjectUtils.isEmpty(show.getId()))
                     .map(ShowCollection::getCollectionId)
@@ -119,27 +124,33 @@ public class ShowroomService {
                     .collect(Collectors.toMap(Collection::getId, coll -> coll));
 
 
-            room.getCollections().forEach(coll -> {
+            showCollections.forEach(coll -> {
                 if (coll.getId() != null) {
                     ShowCollection showCollection = showCollectionMap.get(coll.getId());
                     ObjUtils.merge(showCollection, coll);
                     showCollectionRepo.save(showCollection);
+                    // 移除掉
+                    removeRecord.remove(coll.getId());
                 } else {
                     Collection collection = collectionMap.get(coll.getCollectionId());
                     if (ObjectUtils.isNotEmpty(collection)) {
                         coll.setPic(collection.getPic().get(0).getUrl());
-                        coll.setShowroomId(room.getId());
+                        coll.setShowroomId(recordRoom.getId());
                         coll.setAssetId(collection.getAssetId());
                         showCollectionRepo.save(coll);
                     }
                 }
             });
         } else {
-            showCollectionRepo.softDeleteByRoom(room.getId());
+            // 删掉本次没有的
+            showCollectionRepo.softDeleteByRoom(recordRoom.getId());
+        }
+        if (CollUtil.isNotEmpty(removeRecord)) {
+            showCollectionRepo.softDeleteByIdIn(removeRecord);
         }
 
-        ObjUtils.merge(room, showroom);
-        showroomRepo.save(room);
+        ObjUtils.merge(recordRoom, showroom);
+        showroomRepo.save(recordRoom);
     }
 
 }

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

@@ -0,0 +1,93 @@
+package com.izouma.nineth.service;
+
+import com.izouma.nineth.ApplicationTests;
+import com.izouma.nineth.domain.ShowCollection;
+import com.izouma.nineth.domain.Showroom;
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import java.util.ArrayList;
+import java.util.List;
+
+
+public class ShowroomServiceTest extends ApplicationTests {
+
+    @Autowired
+    private ShowroomService showroomService;
+
+    @Test
+    public void save() {
+        ShowCollection collection = ShowCollection.builder()
+                .sort(1)
+                .collectionId(207013L)
+                .build();
+        ShowCollection collection1 = ShowCollection.builder()
+                .sort(1)
+                .collectionId(206067L)
+                .build();
+        ShowCollection collection2 = ShowCollection.builder()
+                .sort(1)
+                .collectionId(207014L)
+                .build();
+        ShowCollection collection3 = ShowCollection.builder()
+                .sort(1)
+                .collectionId(207015L)
+                .build();
+        ShowCollection collection4 = ShowCollection.builder()
+                .sort(1)
+                .collectionId(207016L)
+                .build();
+        ShowCollection collection5 = ShowCollection.builder()
+                .sort(1)
+                .collectionId(207017L)
+                .build();
+        ShowCollection collection6 = ShowCollection.builder()
+                .sort(1)
+                .collectionId(207018L)
+                .build();
+        ShowCollection collection7 = ShowCollection.builder()
+                .sort(1)
+                .collectionId(207019L)
+                .build();
+        ShowCollection collection8 = ShowCollection.builder()
+                .sort(1)
+                .collectionId(207020L)
+                .build();
+        ShowCollection collection9 = ShowCollection.builder()
+                .sort(1)
+                .collectionId(207021L)
+                .build();
+        ShowCollection collection10 = ShowCollection.builder()
+                .sort(1)
+                .collectionId(207022L)
+                .build();
+        ShowCollection collection11 = ShowCollection.builder()
+                .sort(1)
+                .collectionId(207023L)
+                .build();
+        List<ShowCollection> collections = new ArrayList<>();
+        collections.add(collection);
+        collections.add(collection1);
+        collections.add(collection2);
+        collections.add(collection3);
+        collections.add(collection4);
+        collections.add(collection5);
+        collections.add(collection6);
+        collections.add(collection7);
+        collections.add(collection8);
+        collections.add(collection9);
+//        collections.add(collection10);
+//        collections.add(collection11);
+
+        Showroom showroom = Showroom.builder()
+                .assetId(207029L)
+                .pic("https://cdn.raex.vip/image/2022-03-16-11-19-11fAEkRVkS.jpg")
+                .collections(collections)
+                .build();
+        showroomService.save(9972L, showroom);
+    }
+
+    @Test
+    public void update() {
+    }
+}