ShowroomService.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package com.izouma.nineth.service;
  2. import cn.hutool.core.collection.CollUtil;
  3. import com.izouma.nineth.domain.Asset;
  4. import com.izouma.nineth.domain.Collection;
  5. import com.izouma.nineth.domain.ShowCollection;
  6. import com.izouma.nineth.domain.Showroom;
  7. import com.izouma.nineth.dto.PageQuery;
  8. import com.izouma.nineth.enums.AssetStatus;
  9. import com.izouma.nineth.enums.CollectionType;
  10. import com.izouma.nineth.exception.BusinessException;
  11. import com.izouma.nineth.repo.AssetRepo;
  12. import com.izouma.nineth.repo.CollectionRepo;
  13. import com.izouma.nineth.repo.ShowCollectionRepo;
  14. import com.izouma.nineth.repo.ShowroomRepo;
  15. import com.izouma.nineth.utils.JpaUtils;
  16. import com.izouma.nineth.utils.ObjUtils;
  17. import lombok.AllArgsConstructor;
  18. import org.apache.commons.beanutils.BeanUtils;
  19. import org.apache.commons.lang3.ObjectUtils;
  20. import org.springframework.data.domain.Page;
  21. import org.springframework.stereotype.Service;
  22. import javax.persistence.Id;
  23. import java.util.*;
  24. import java.util.stream.Collectors;
  25. @Service
  26. @AllArgsConstructor
  27. public class ShowroomService {
  28. private ShowroomRepo showroomRepo;
  29. private AssetRepo assetRepo;
  30. private CollectionRepo collectionRepo;
  31. private ShowCollectionRepo showCollectionRepo;
  32. public Page<Showroom> all(PageQuery pageQuery) {
  33. return showroomRepo.findAll(JpaUtils.toSpecification(pageQuery, Showroom.class), JpaUtils.toPageRequest(pageQuery));
  34. }
  35. /*
  36. 不做盲盒
  37. */
  38. public Showroom save(Long userId, Showroom showroom) {
  39. Asset asset = assetRepo.findById(showroom.getAssetId()).orElseThrow(new BusinessException("资产不存在"));
  40. if (!userId.equals(asset.getUserId())) {
  41. throw new BusinessException("该资产不属于你");
  42. }
  43. if (!CollectionType.SHOWROOM.equals(asset.getType())) {
  44. throw new BusinessException("不是展厅藏品");
  45. }
  46. if (!AssetStatus.NORMAL.equals(asset.getStatus())) {
  47. throw new BusinessException("该状态不可创建展厅");
  48. }
  49. if (asset.isConsignment()) {
  50. throw new BusinessException("寄售中不可以创建");
  51. }
  52. if (showroomRepo.findByAssetId(showroom.getAssetId()).isPresent()) {
  53. throw new BusinessException("已创建过展厅");
  54. }
  55. List<ShowCollection> showCollections = showroom.getCollections();
  56. // Collection showColl = collectionRepo.findById(asset.getCollectionId())
  57. // .orElseThrow(new BusinessException("无藏品"));
  58. if (asset.getMaxCollection() < showCollections.size()) {
  59. throw new BusinessException("选择的藏品数量大于最多可放置数量");
  60. }
  61. List<Long> collectionIds = showCollections.stream()
  62. .map(ShowCollection::getCollectionId)
  63. .distinct()
  64. .collect(Collectors.toList());
  65. List<Collection> collections = collectionRepo.findAllByIdIn(collectionIds);
  66. Map<Long, Collection> collectionMap = new HashMap<>();
  67. collections.forEach(collection -> {
  68. if (!userId.equals(collection.getOwnerId())) {
  69. throw new BusinessException("该藏品不属于你");
  70. }
  71. collectionMap.put(collection.getId(), collection);
  72. });
  73. showroom.setUserId(userId);
  74. showroom.setShowroomBg(asset.getShowroomBg());
  75. showroom.setMaxCollection(asset.getMaxCollection());
  76. Showroom show = showroomRepo.save(showroom);
  77. showCollections.forEach(showCollection -> {
  78. Collection collection = collectionMap.get(showCollection.getCollectionId());
  79. if (ObjectUtils.isNotEmpty(collection)) {
  80. showCollection.setPic(collection.getPic().get(0).getUrl());
  81. showCollection.setShowroomId(show.getId());
  82. showCollection.setAssetId(collection.getAssetId());
  83. showCollectionRepo.save(showCollection);
  84. }
  85. });
  86. return show;
  87. }
  88. public Showroom save(Asset asset) {
  89. if (!AssetStatus.NORMAL.equals(asset.getStatus())) {
  90. throw new BusinessException("该状态不可创建展厅");
  91. }
  92. if (asset.isConsignment()) {
  93. throw new BusinessException("寄售中不可以创建");
  94. }
  95. if (showroomRepo.findByAssetId(asset.getId()).isPresent()) {
  96. throw new BusinessException("已创建过展厅");
  97. }
  98. Showroom showroom = Showroom.builder()
  99. .showroomBg(asset.getShowroomBg())
  100. .maxCollection(asset.getMaxCollection())
  101. .publish(false)
  102. .userId(asset.getUserId())
  103. .assetId(asset.getId())
  104. .build();
  105. return showroomRepo.save(showroom);
  106. }
  107. public Showroom update(Long userId, Showroom showroom) {
  108. Showroom recordRoom = showroomRepo.findById(showroom.getId()).orElseThrow(new BusinessException("无展厅"));
  109. showroom.setMaxCollection(recordRoom.getMaxCollection());
  110. // 上传的藏品
  111. List<ShowCollection> showCollections = showroom.getCollections();
  112. List<ShowCollection> recordShowColls = showCollectionRepo.findAllByShowroomId(showroom.getId());
  113. Map<Long, ShowCollection> showCollectionMap = recordShowColls.stream()
  114. .collect(Collectors.toMap(ShowCollection::getId, coll -> coll));
  115. Map<Long, ShowCollection> showCollectionMap1 = recordShowColls.stream()
  116. .collect(Collectors.toMap(ShowCollection::getCollectionId, coll -> coll));
  117. Set<Long> removeRecord = new HashSet<>(showCollectionMap.keySet());
  118. if (CollUtil.isNotEmpty(showCollections)) {
  119. if (recordRoom.getMaxCollection() < showCollections.size()) {
  120. throw new BusinessException("选择的藏品数量大于最多可放置数量");
  121. }
  122. List<Long> collectionIds = showCollections
  123. .stream()
  124. .filter(show -> ObjectUtils.isEmpty(show.getId()))
  125. .map(ShowCollection::getCollectionId)
  126. .collect(Collectors.toList());
  127. List<Collection> collections = collectionRepo.findAllByIdIn(collectionIds);
  128. Map<Long, Collection> collectionMap = new HashMap<>();
  129. collections.forEach(collection -> {
  130. if (!userId.equals(collection.getOwnerId())) {
  131. throw new BusinessException("该藏品不属于你");
  132. }
  133. collectionMap.put(collection.getId(), collection);
  134. });
  135. showCollections.forEach(coll -> {
  136. if (coll.getId() != null) {
  137. ShowCollection showCollection = showCollectionMap.get(coll.getId());
  138. if (ObjectUtils.isNotEmpty(showCollection)) {
  139. ObjUtils.merge(showCollection, coll);
  140. showCollectionRepo.save(showCollection);
  141. // 移除掉
  142. removeRecord.remove(coll.getId());
  143. }
  144. } else {
  145. ShowCollection showCollection = showCollectionMap1.get(coll.getCollectionId());
  146. if (ObjectUtils.isNotEmpty(showCollection)) {
  147. ObjUtils.merge(showCollection, coll);
  148. showCollectionRepo.save(showCollection);
  149. removeRecord.remove(showCollection.getId());
  150. } else {
  151. Collection collection = collectionMap.get(coll.getCollectionId());
  152. if (ObjectUtils.isNotEmpty(collection)) {
  153. coll.setPic(collection.getPic().get(0).getUrl());
  154. coll.setShowroomId(recordRoom.getId());
  155. coll.setAssetId(collection.getAssetId());
  156. showCollectionRepo.save(coll);
  157. }
  158. }
  159. }
  160. });
  161. } else {
  162. // 删掉本次没有的
  163. showCollectionRepo.softDeleteByRoom(recordRoom.getId());
  164. }
  165. if (CollUtil.isNotEmpty(removeRecord)) {
  166. showCollectionRepo.softDeleteByIdIn(removeRecord);
  167. }
  168. ObjUtils.merge(recordRoom, showroom);
  169. return showroomRepo.save(recordRoom);
  170. }
  171. }