CollectionService.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. package com.izouma.nineth.service;
  2. import com.izouma.nineth.domain.*;
  3. import com.izouma.nineth.dto.CollectionDTO;
  4. import com.izouma.nineth.dto.CreateBlindBox;
  5. import com.izouma.nineth.dto.PageQuery;
  6. import com.izouma.nineth.enums.CollectionType;
  7. import com.izouma.nineth.exception.BusinessException;
  8. import com.izouma.nineth.repo.*;
  9. import com.izouma.nineth.utils.JpaUtils;
  10. import com.izouma.nineth.utils.SecurityUtils;
  11. import lombok.AllArgsConstructor;
  12. import org.apache.commons.collections.MapUtils;
  13. import org.apache.commons.lang3.RandomUtils;
  14. import org.apache.commons.lang3.Range;
  15. import org.springframework.beans.BeanUtils;
  16. import org.springframework.data.domain.Page;
  17. import org.springframework.data.domain.PageImpl;
  18. import org.springframework.data.domain.PageRequest;
  19. import org.springframework.data.domain.Sort;
  20. import org.springframework.data.jpa.domain.Specification;
  21. import org.springframework.scheduling.annotation.Scheduled;
  22. import org.springframework.stereotype.Service;
  23. import javax.persistence.criteria.Predicate;
  24. import javax.transaction.Transactional;
  25. import java.time.LocalDateTime;
  26. import java.util.ArrayList;
  27. import java.util.HashMap;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.stream.Collectors;
  31. @Service
  32. @AllArgsConstructor
  33. public class CollectionService {
  34. private CollectionRepo collectionRepo;
  35. private LikeRepo likeRepo;
  36. private BlindBoxItemRepo blindBoxItemRepo;
  37. private AppointmentRepo appointmentRepo;
  38. private UserRepo userRepo;
  39. private AssetService assetService;
  40. public Page<Collection> all(PageQuery pageQuery) {
  41. pageQuery.getQuery().put("del", false);
  42. Specification<Collection> specification = JpaUtils.toSpecification(pageQuery, Collection.class);
  43. PageRequest pageRequest = JpaUtils.toPageRequest(pageQuery);
  44. if (pageRequest.getSort().stream().noneMatch(order -> order.getProperty().equals("createdAt"))) {
  45. pageRequest = PageRequest.of(pageRequest.getPageNumber(), pageQuery.getSize(),
  46. pageRequest.getSort().and(Sort.by("createdAt").descending()));
  47. }
  48. specification = specification.and((Specification<Collection>) (root, criteriaQuery, criteriaBuilder) -> {
  49. List<Predicate> and = new ArrayList<>();
  50. if (!MapUtils.getString(pageQuery.getQuery(), "type", "").equals("BLIND_BOX")) {
  51. and.add(criteriaBuilder.notEqual(root.get("type"), CollectionType.BLIND_BOX));
  52. }
  53. return criteriaBuilder.and(and.toArray(new Predicate[0]));
  54. });
  55. return collectionRepo.findAll(specification, pageRequest);
  56. }
  57. public Collection create(Collection record) {
  58. User minter = userRepo.findById(record.getMinterId()).orElse(SecurityUtils.getAuthenticatedUser());
  59. record.setMinter(minter.getNickname());
  60. record.setMinterId(minter.getId());
  61. record.setMinterAvatar(minter.getAvatar());
  62. record.setOwner(minter.getNickname());
  63. record.setOwnerId(minter.getId());
  64. record.setOwnerAvatar(minter.getAvatar());
  65. record.setStock(record.getTotal());
  66. record.setSale(0);
  67. if (record.isScheduleSale()) {
  68. if (record.getStartTime() == null) {
  69. throw new BusinessException("请填写定时发布时间");
  70. }
  71. record.setOnShelf(record.getStartTime().isBefore(LocalDateTime.now()));
  72. }
  73. return collectionRepo.save(record);
  74. }
  75. public CollectionDTO toDTO(Collection collection) {
  76. return toDTO(collection, true);
  77. }
  78. public CollectionDTO toDTO(Collection collection, boolean join) {
  79. CollectionDTO collectionDTO = new CollectionDTO();
  80. BeanUtils.copyProperties(collection, collectionDTO);
  81. if (join) {
  82. if (SecurityUtils.getAuthenticatedUser() != null) {
  83. List<Like> list = likeRepo.findByUserIdAndCollectionId(SecurityUtils.getAuthenticatedUser().getId(),
  84. collection.getId());
  85. collectionDTO.setLiked(!list.isEmpty());
  86. if (collection.getType() == CollectionType.BLIND_BOX) {
  87. collectionDTO.setAppointment(appointmentRepo.findFirstByBlindBoxId(collection.getId()).isPresent());
  88. }
  89. }
  90. }
  91. return collectionDTO;
  92. }
  93. public List<CollectionDTO> toDTO(List<Collection> collections) {
  94. List<Like> likes = new ArrayList<>();
  95. List<Appointment> appointments = new ArrayList<>();
  96. if (SecurityUtils.getAuthenticatedUser() != null) {
  97. likes.addAll(likeRepo.findByUserId(SecurityUtils.getAuthenticatedUser().getId()));
  98. appointments.addAll(appointmentRepo.findByUserId(SecurityUtils.getAuthenticatedUser().getId()));
  99. }
  100. return collections.stream().parallel().map(collection -> {
  101. CollectionDTO dto = toDTO(collection, false);
  102. if (!likes.isEmpty()) {
  103. dto.setLiked(likes.stream().anyMatch(l -> l.getCollectionId().equals(collection.getId())));
  104. }
  105. if (!appointments.isEmpty()) {
  106. dto.setAppointment(appointments.stream().anyMatch(a -> a.getBlindBoxId().equals(collection.getId())));
  107. }
  108. return dto;
  109. }).collect(Collectors.toList());
  110. }
  111. public Page<CollectionDTO> toDTO(Page<Collection> collections) {
  112. List<CollectionDTO> userDTOS = toDTO(collections.getContent());
  113. return new PageImpl<>(userDTOS, collections.getPageable(), collections.getTotalElements());
  114. }
  115. @Transactional
  116. public Collection createBlindBox(CreateBlindBox createBlindBox) {
  117. Collection blindBox = createBlindBox.getBlindBox();
  118. List<Collection> list =
  119. collectionRepo.findAllById(createBlindBox.getItems().stream().map(BlindBoxItem::getCollectionId)
  120. .collect(Collectors.toSet()));
  121. for (BlindBoxItem item : createBlindBox.getItems()) {
  122. Collection collection = list.stream().filter(i -> i.getId().equals(item.getCollectionId())).findAny()
  123. .orElseThrow(new BusinessException("所选藏品不存在"));
  124. if (item.getTotal() > collection.getStock()) {
  125. throw new BusinessException("所选藏品库存不足:" + collection.getName());
  126. }
  127. }
  128. User user = userRepo.findById(blindBox.getMinterId()).orElse(SecurityUtils.getAuthenticatedUser());
  129. blindBox.setMinter(user.getNickname());
  130. blindBox.setMinterId(user.getId());
  131. blindBox.setMinterAvatar(user.getAvatar());
  132. blindBox.setOwner(user.getNickname());
  133. blindBox.setOwnerId(user.getId());
  134. blindBox.setOwnerAvatar(user.getAvatar());
  135. blindBox.setStock(blindBox.getTotal());
  136. blindBox.setSale(0);
  137. collectionRepo.save(blindBox);
  138. for (BlindBoxItem item : createBlindBox.getItems()) {
  139. Collection collection = list.stream().filter(i -> i.getId().equals(item.getCollectionId())).findAny()
  140. .orElseThrow(new BusinessException("所选藏品不存在"));
  141. collection.setStock(collection.getStock() - item.getTotal());
  142. collectionRepo.save(collection);
  143. BlindBoxItem blindBoxItem = new BlindBoxItem();
  144. BeanUtils.copyProperties(collection, blindBoxItem);
  145. blindBoxItem.setId(null);
  146. blindBoxItem.setCollectionId(item.getCollectionId());
  147. blindBoxItem.setSale(0);
  148. blindBoxItem.setTotal(item.getTotal());
  149. blindBoxItem.setStock(item.getTotal());
  150. blindBoxItem.setRare(item.isRare());
  151. blindBoxItem.setBlindBoxId(blindBox.getId());
  152. blindBoxItemRepo.save(blindBoxItem);
  153. }
  154. return blindBox;
  155. }
  156. public void appointment(Long id, Long userId) {
  157. Collection collection = collectionRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  158. if (collection.getType() != CollectionType.BLIND_BOX) {
  159. throw new BusinessException("非盲盒,无需预约");
  160. }
  161. if (collection.getStartTime().isBefore(LocalDateTime.now())) {
  162. throw new BusinessException("盲盒已开售,无需预约");
  163. }
  164. appointmentRepo.save(Appointment.builder()
  165. .userId(userId)
  166. .blindBoxId(id)
  167. .build());
  168. }
  169. @Scheduled(fixedRate = 60000)
  170. public void scheduleOnShelf() {
  171. List<Collection> collections = collectionRepo.findByScheduleSaleTrueAndOnShelfFalseAndStartTimeBeforeAndDelFalse(LocalDateTime.now());
  172. for (Collection collection : collections) {
  173. collection.setOnShelf(true);
  174. }
  175. collectionRepo.saveAll(collections);
  176. }
  177. public BlindBoxItem draw(Long collectionId) {
  178. List<BlindBoxItem> items = blindBoxItemRepo.findByBlindBoxId(collectionId);
  179. Map<BlindBoxItem, Range<Integer>> randomRange = new HashMap<>();
  180. int c = 0, sum = 0;
  181. for (BlindBoxItem item : items) {
  182. randomRange.put(item, Range.between(c, c + item.getStock()));
  183. c += item.getStock();
  184. sum += item.getStock();
  185. }
  186. int retry = 0;
  187. BlindBoxItem winItem = null;
  188. while (winItem == null) {
  189. retry++;
  190. int rand = RandomUtils.nextInt(0, sum + 1);
  191. for (Map.Entry<BlindBoxItem, Range<Integer>> entry : randomRange.entrySet()) {
  192. BlindBoxItem item = entry.getKey();
  193. Range<Integer> range = entry.getValue();
  194. if (rand >= range.getMinimum() && rand < range.getMaximum()) {
  195. int total = items.stream().filter(i -> !i.isRare())
  196. .mapToInt(BlindBoxItem::getTotal).sum();
  197. int stock = items.stream().filter(i -> !i.isRare())
  198. .mapToInt(BlindBoxItem::getStock).sum();
  199. if (item.isRare()) {
  200. double nRate = stock / (double) total;
  201. double rRate = (item.getStock() - 1) / (double) item.getTotal();
  202. if (Math.abs(nRate - rRate) < (1 / (double) item.getTotal()) || retry > 1 || rRate == 0) {
  203. if (!(nRate > 0.1 && item.getStock() == 1)) {
  204. winItem = item;
  205. }
  206. }
  207. } else {
  208. double nRate = (stock - 1) / (double) total;
  209. double rRate = item.getStock() / (double) item.getTotal();
  210. if (Math.abs(nRate - rRate) < 0.2 || retry > 1 || nRate == 0) {
  211. winItem = item;
  212. }
  213. }
  214. }
  215. }
  216. if (retry > 100 && winItem == null) {
  217. throw new BusinessException("盲盒抽卡失败");
  218. }
  219. }
  220. winItem.setStock(winItem.getStock() - 1);
  221. winItem.setSale(winItem.getSale() + 1);
  222. blindBoxItemRepo.save(winItem);
  223. return winItem;
  224. }
  225. public void airDrop(Long collectionId, List<Long> userIds) {
  226. Collection collection = collectionRepo.findById(collectionId).orElseThrow(new BusinessException("藏品不存在"));
  227. for (Long userId : userIds) {
  228. }
  229. }
  230. }