ShowroomService.java 15 KB

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