ShowroomService.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. package com.izouma.nineth.service;
  2. import cn.hutool.core.collection.CollUtil;
  3. import com.izouma.nineth.domain.Collection;
  4. import com.izouma.nineth.domain.*;
  5. import com.izouma.nineth.dto.PageQuery;
  6. import com.izouma.nineth.enums.AssetStatus;
  7. import com.izouma.nineth.enums.AuthStatus;
  8. import com.izouma.nineth.enums.CollectionType;
  9. import com.izouma.nineth.enums.ShowroomType;
  10. import com.izouma.nineth.enums.ShowroomType;
  11. import com.izouma.nineth.exception.BusinessException;
  12. import com.izouma.nineth.repo.*;
  13. import com.izouma.nineth.utils.JpaUtils;
  14. import com.izouma.nineth.utils.ObjUtils;
  15. import com.izouma.nineth.utils.SecurityUtils;
  16. import lombok.AllArgsConstructor;
  17. import org.apache.commons.collections.CollectionUtils;
  18. import org.apache.commons.lang3.ObjectUtils;
  19. import org.apache.commons.lang3.StringUtils;
  20. import org.springframework.data.domain.Page;
  21. import org.springframework.stereotype.Service;
  22. import java.time.LocalDateTime;
  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. private CollectionPrivilegeRepo collectionPrivilegeRepo;
  33. private CacheService cacheService;
  34. private UserRepo userRepo;
  35. private SysConfigService sysConfigService;
  36. private NewsLikeRepo newsLikeRepo;
  37. public Page<Showroom> all(PageQuery pageQuery) {
  38. return showroomRepo
  39. .findAll(JpaUtils.toSpecification(pageQuery, Showroom.class), JpaUtils.toPageRequest(pageQuery));
  40. }
  41. /*
  42. 不做盲盒
  43. */
  44. public Showroom save(Long userId, Showroom showroom) {
  45. Asset asset = assetRepo.findById(showroom.getAssetId()).orElseThrow(new BusinessException("资产不存在"));
  46. if (!userId.equals(asset.getUserId())) {
  47. throw new BusinessException("该资产不属于你");
  48. }
  49. if (!CollectionType.SHOWROOM.equals(asset.getType())) {
  50. throw new BusinessException("不是展厅藏品");
  51. }
  52. if (!AssetStatus.NORMAL.equals(asset.getStatus())) {
  53. throw new BusinessException("该状态不可创建展厅");
  54. }
  55. if (asset.isConsignment()) {
  56. throw new BusinessException("寄售中不可以创建");
  57. }
  58. if (showroomRepo.findByAssetId(showroom.getAssetId()).isPresent()) {
  59. throw new BusinessException("已创建过展厅");
  60. }
  61. List<ShowCollection> showCollections = showroom.getCollections();
  62. CollectionPrivilege cp = collectionPrivilegeRepo.findByCollectionId(asset.getCollectionId());
  63. if (cp.getMaxCollection() < showCollections.size()) {
  64. throw new BusinessException("选择的藏品数量大于最多可放置数量");
  65. }
  66. List<Long> collectionIds = showCollections.stream()
  67. .map(ShowCollection::getCollectionId)
  68. .distinct()
  69. .collect(Collectors.toList());
  70. List<Collection> collections = collectionRepo.findAllByIdIn(collectionIds);
  71. Map<Long, Collection> collectionMap = new HashMap<>();
  72. collections.forEach(collection -> {
  73. if (!userId.equals(collection.getOwnerId())) {
  74. throw new BusinessException("该藏品不属于你");
  75. }
  76. collectionMap.put(collection.getId(), collection);
  77. });
  78. showroom.setUserId(userId);
  79. showroom.setHeadBg(cp.getHeadBg());
  80. showroom.setShowroomBg(cp.getShowroomBg());
  81. showroom.setMaxCollection(cp.getMaxCollection());
  82. showroom.setNickname(asset.getOwner());
  83. showroom.setAvatar(asset.getOwnerAvatar());
  84. Showroom show = showroomRepo.save(showroom);
  85. showCollections.forEach(showCollection -> {
  86. Collection collection = collectionMap.get(showCollection.getCollectionId());
  87. if (ObjectUtils.isNotEmpty(collection)) {
  88. FileObject pic = collection.getPic().get(0);
  89. showCollection.setPic(pic.getUrl());
  90. if ("video/mp4".equals(pic.getType())) {
  91. showCollection.setPic(pic.getThumb());
  92. }
  93. showCollection.setShowroomId(show.getId());
  94. showCollection.setAssetId(collection.getAssetId());
  95. showCollection.setStatus(getStatus(collection));
  96. showCollection.setPrice(collection.getPrice());
  97. showCollectionRepo.save(showCollection);
  98. }
  99. });
  100. return show;
  101. }
  102. public Showroom save(Asset asset, String type) {
  103. if (!AssetStatus.NORMAL.equals(asset.getStatus())) {
  104. throw new BusinessException("该状态不可创建展厅");
  105. }
  106. if (asset.isConsignment()) {
  107. throw new BusinessException("寄售中不可以创建");
  108. }
  109. if (showroomRepo.findByAssetId(asset.getId()).isPresent()) {
  110. throw new BusinessException("已创建过展厅");
  111. }
  112. CollectionPrivilege cp = collectionPrivilegeRepo.findByCollectionId(asset.getCollectionId());
  113. Showroom showroom = Showroom.builder()
  114. .headBg(cp.getHeadBg())
  115. .showroomBg(cp.getShowroomBg())
  116. .maxCollection(cp.getMaxCollection())
  117. .publish(false)
  118. .userId(asset.getUserId())
  119. .avatar(asset.getOwnerAvatar())
  120. .assetId(asset.getId())
  121. .nickname(asset.getOwner())
  122. .type(ShowroomType.valueOf(type))
  123. .status(AuthStatus.SUCCESS)
  124. .oasisId(asset.getOasisId())
  125. .build();
  126. showroom = showroomRepo.save(showroom);
  127. return showroom;
  128. }
  129. public Showroom save(Asset asset) {
  130. return this.save(asset, "USER");
  131. }
  132. public Showroom save(Long userId, String type) {
  133. User user = userRepo.findByIdAndDelFalse(userId).orElseThrow(new BusinessException("无用户"));
  134. if (!user.isCompany()) {
  135. throw new BusinessException("无用户权限");
  136. }
  137. int maxCollection = 1;
  138. if ("COMPANY".equals(type)) {
  139. // if (showroomRepo.findByUserIdAndType(user.getId(), "COMPANY").isPresent()) {
  140. // throw new BusinessException("已创建过展厅");
  141. // }
  142. maxCollection = sysConfigService.getInt("max_collection");
  143. }
  144. Showroom showroom = Showroom.builder()
  145. .headBg("")
  146. .showroomBg("")
  147. .maxCollection(maxCollection)
  148. .publish(false)
  149. .userId(user.getId())
  150. .nickname(user.getNickname())
  151. .avatar(user.getAvatar())
  152. .type(ShowroomType.valueOf(type))
  153. .status(AuthStatus.NOT_AUTH)
  154. .build();
  155. showroom = showroomRepo.save(showroom);
  156. return showroom;
  157. }
  158. public Showroom update(Long userId, Showroom showroom) {
  159. Showroom recordRoom = showroomRepo.findById(showroom.getId()).orElseThrow(new BusinessException("无展厅"));
  160. showroom.setMaxCollection(recordRoom.getMaxCollection());
  161. // 上传的藏品
  162. List<ShowCollection> showCollections = showroom.getCollections();
  163. List<ShowCollection> recordShowColls = showCollectionRepo.findAllByShowroomId(showroom.getId());
  164. Map<Long, ShowCollection> showCollectionMap = recordShowColls.stream()
  165. .collect(Collectors.toMap(ShowCollection::getId, coll -> coll));
  166. Map<Long, ShowCollection> showCollectionMap1 = recordShowColls.stream()
  167. .collect(Collectors.toMap(ShowCollection::getCollectionId, coll -> coll));
  168. Set<Long> removeRecord = new HashSet<>(showCollectionMap.keySet());
  169. if (CollUtil.isNotEmpty(showCollections)) {
  170. showCollections = showCollections.stream().distinct().collect(Collectors.toList());
  171. if (recordRoom.getMaxCollection() < showCollections.size()) {
  172. throw new BusinessException("选择的藏品数量大于最多可放置数量");
  173. }
  174. List<Long> collectionIds = showCollections
  175. .stream()
  176. .filter(show -> ObjectUtils.isEmpty(show.getId()))
  177. .map(ShowCollection::getCollectionId)
  178. .collect(Collectors.toList());
  179. List<Collection> collections = collectionRepo.findAllByIdIn(collectionIds);
  180. Map<Long, Collection> collectionMap = new HashMap<>();
  181. collections.forEach(collection -> {
  182. // if (!userId.equals(collection.getOwnerId())) {
  183. // throw new BusinessException("该藏品不属于你");
  184. // }
  185. collectionMap.put(collection.getId(), collection);
  186. });
  187. showCollections.forEach(coll -> {
  188. if (coll.getId() != null) {
  189. ShowCollection showCollection = showCollectionMap.get(coll.getId());
  190. if (ObjectUtils.isNotEmpty(showCollection)) {
  191. ObjUtils.merge(showCollection, coll);
  192. showCollectionRepo.save(showCollection);
  193. // 移除掉
  194. removeRecord.remove(coll.getId());
  195. }
  196. } else {
  197. ShowCollection showCollection = showCollectionMap1.get(coll.getCollectionId());
  198. if (ObjectUtils.isNotEmpty(showCollection)) {
  199. ObjUtils.merge(showCollection, coll);
  200. showCollectionRepo.save(showCollection);
  201. removeRecord.remove(showCollection.getId());
  202. } else {
  203. Collection collection = collectionMap.get(coll.getCollectionId());
  204. if (ObjectUtils.isNotEmpty(collection)) {
  205. if (ShowroomType.COMPANY_BOX.equals(recordRoom.getType())) {
  206. if (!CollectionType.BLIND_BOX.equals(collection.getType())) {
  207. throw new BusinessException("盲盒展厅,只能添加盲盒");
  208. }
  209. }
  210. FileObject pic = collection.getPic().get(0);
  211. coll.setPic(pic.getUrl());
  212. if ("video/mp4".equals(pic.getType())) {
  213. coll.setPic(pic.getThumb());
  214. }
  215. coll.setShowroomId(recordRoom.getId());
  216. // 可能没有
  217. coll.setAssetId(collection.getAssetId());
  218. coll.setStatus(getStatus(collection));
  219. coll.setPrice(collection.getPrice());
  220. showCollectionRepo.save(coll);
  221. }
  222. }
  223. }
  224. });
  225. } else {
  226. // 删掉本次没有的
  227. showCollectionRepo.deleteAllByShowroomId(recordRoom.getId());
  228. }
  229. if (CollUtil.isNotEmpty(removeRecord)) {
  230. showCollectionRepo.deleteAllByIdIn(removeRecord);
  231. }
  232. ObjUtils.merge(recordRoom, showroom);
  233. recordRoom.setPublish(true);
  234. showroom = showroomRepo.save(recordRoom);
  235. cacheService.clearShowroom(showroom.getId());
  236. return showroom;
  237. }
  238. public void audit(Long id, AuthStatus status, String reason) {
  239. Showroom showroom = showroomRepo.findById(id).orElseThrow(new BusinessException("无展厅"));
  240. if (ShowroomType.USER.equals(showroom.getType())) {
  241. // 非企业类型不需要审核
  242. showroom.setStatus(null);
  243. showroomRepo.save(showroom);
  244. return;
  245. }
  246. showroom.setStatus(status);
  247. showroom.setReason(reason);
  248. showroomRepo.save(showroom);
  249. cacheService.clearShowroom(id);
  250. }
  251. public String getStatus(Collection collection) {
  252. boolean salable = collection.isSalable();
  253. if (collection.getSaleTime() != null) {
  254. if (collection.getSaleTime().isAfter(LocalDateTime.now())) {
  255. return "仅展示";
  256. }
  257. }
  258. if (!salable) {
  259. return "仅展示";
  260. }
  261. if (collection.getSoldOut() > 1) {
  262. return "寄售中";
  263. }
  264. if (collection.isNoSoldOut()) {
  265. return "仅展示";
  266. }
  267. if (collection.getSoldOut() != 1 & collection.getStock() == 0) {
  268. return "仅展示";
  269. }
  270. return "寄售中";
  271. }
  272. public String getStatusById(Long collectionId) {
  273. Collection collection = collectionRepo.findById(collectionId).orElseThrow(new BusinessException("暂无"));
  274. return getStatus(collection);
  275. }
  276. public Page<Showroom> show(PageQuery pageQuery) {
  277. if (Objects.equals(pageQuery.getQuery().get("type"), "COMPANY")) {
  278. pageQuery.getQuery().put("type", "COMPANY,COMPANY_BOX");
  279. }
  280. Page<Showroom> all = this.all(pageQuery);
  281. List<Long> ids = all.map(Showroom::getId).getContent();
  282. Map<Long, List<ShowCollection>> collect = showCollectionRepo.findAllByShowroomIdIn(ids)
  283. .stream()
  284. .collect(Collectors.groupingBy(ShowCollection::getShowroomId));
  285. User user = SecurityUtils.getAuthenticatedUser();
  286. Map<Long, Long> likesMap = new HashMap<>();
  287. if (user != null && !user.isAdmin() && CollUtil.isNotEmpty(ids)) {
  288. List<NewsLike> likes = newsLikeRepo.findByUserIdAndShowroomIdIn(user
  289. .getId(), ids);
  290. likesMap = likes.stream()
  291. .collect(Collectors.groupingBy(NewsLike::getShowroomId, Collectors.counting()));
  292. }
  293. Map<Long, Long> finalLikesMap = likesMap;
  294. return all.map(showroom -> {
  295. List<ShowCollection> collections = collect.get(showroom.getId());
  296. if (ShowroomType.COMPANY_BOX.equals(showroom.getType()) && CollectionUtils.isNotEmpty(collections)) {
  297. collections.forEach(orig -> collectionRepo.findById(orig.getCollectionId())
  298. .ifPresent(collection -> {
  299. //展示数量
  300. if (collection.isNoSoldOut()) {
  301. showroom.setNum(collection.getStock());
  302. } else {
  303. String name = StringUtils.isEmpty(collection.getPrefixName()) ?
  304. collection.getName() : collection.getPrefixName();
  305. int num = collectionRepo.countAllByNameLike("%" + name + "%");
  306. showroom.setNum(num);
  307. }
  308. }));
  309. }
  310. if (CollUtil.isNotEmpty(collections)) {
  311. collections.sort(Comparator.comparingInt(ShowCollection::getSort));
  312. showroom.setCollections(collections);
  313. }
  314. showroom.setLiked(ObjectUtils.isNotEmpty(finalLikesMap.get(showroom.getId())));
  315. return showroom;
  316. });
  317. }
  318. }