CollectionService.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. package com.izouma.nineth.service;
  2. import com.alibaba.fastjson.JSON;
  3. import com.izouma.nineth.domain.Collection;
  4. import com.izouma.nineth.domain.*;
  5. import com.izouma.nineth.dto.CollectionDTO;
  6. import com.izouma.nineth.dto.CreateBlindBox;
  7. import com.izouma.nineth.dto.PageQuery;
  8. import com.izouma.nineth.enums.CollectionSource;
  9. import com.izouma.nineth.enums.CollectionType;
  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.SecurityUtils;
  14. import lombok.AllArgsConstructor;
  15. import org.apache.commons.collections.MapUtils;
  16. import org.apache.commons.lang3.RandomUtils;
  17. import org.apache.commons.lang3.Range;
  18. import org.apache.commons.lang3.StringUtils;
  19. import org.springframework.beans.BeanUtils;
  20. import org.springframework.data.domain.Page;
  21. import org.springframework.data.domain.PageImpl;
  22. import org.springframework.data.domain.PageRequest;
  23. import org.springframework.data.domain.Sort;
  24. import org.springframework.data.jpa.domain.Specification;
  25. import org.springframework.scheduling.TaskScheduler;
  26. import org.springframework.stereotype.Service;
  27. import javax.annotation.PostConstruct;
  28. import javax.persistence.criteria.Predicate;
  29. import javax.transaction.Transactional;
  30. import java.time.LocalDateTime;
  31. import java.time.ZoneId;
  32. import java.util.*;
  33. import java.util.concurrent.ScheduledFuture;
  34. import java.util.stream.Collectors;
  35. @Service
  36. @AllArgsConstructor
  37. public class CollectionService {
  38. private CollectionRepo collectionRepo;
  39. private LikeRepo likeRepo;
  40. private BlindBoxItemRepo blindBoxItemRepo;
  41. private AppointmentRepo appointmentRepo;
  42. private UserRepo userRepo;
  43. private AssetService assetService;
  44. private TaskScheduler taskScheduler;
  45. private CacheService cacheService;
  46. private final Map<Long, ScheduledFuture<?>> tasks = new HashMap<>();
  47. @PostConstruct
  48. public void init() {
  49. List<Collection> collections = collectionRepo.findByScheduleSaleTrueAndOnShelfFalseAndStartTimeBeforeAndDelFalse(LocalDateTime.now());
  50. for (Collection collection : collections) {
  51. onShelfTask(collection);
  52. }
  53. }
  54. public Page<Collection> all(PageQuery pageQuery) {
  55. pageQuery.getQuery().put("del", false);
  56. String type = MapUtils.getString(pageQuery.getQuery(), "type", "DEFAULT");
  57. pageQuery.getQuery().remove("type");
  58. Specification<Collection> specification = JpaUtils.toSpecification(pageQuery, Collection.class);
  59. PageRequest pageRequest = JpaUtils.toPageRequest(pageQuery);
  60. if (pageRequest.getSort().stream().noneMatch(order -> order.getProperty().equals("createdAt"))) {
  61. pageRequest = PageRequest.of(pageRequest.getPageNumber(), pageQuery.getSize(),
  62. pageRequest.getSort().and(Sort.by("createdAt").descending()));
  63. }
  64. specification = specification.and((Specification<Collection>) (root, criteriaQuery, criteriaBuilder) -> {
  65. List<Predicate> and = new ArrayList<>();
  66. if (StringUtils.isNotEmpty(type) && !"all".equalsIgnoreCase(type)) {
  67. try {
  68. if (type.contains(",")) {
  69. and.add(root.get("type")
  70. .in(Arrays.stream(type.split(",")).map(s -> Enum.valueOf(CollectionType.class, s))
  71. .collect(Collectors.toList())));
  72. } else {
  73. and.add(criteriaBuilder.equal(root.get("type"), Enum.valueOf(CollectionType.class, type)));
  74. }
  75. } catch (Exception e) {
  76. }
  77. }
  78. return criteriaBuilder.and(and.toArray(new Predicate[0]));
  79. });
  80. return collectionRepo.findAll(specification, pageRequest);
  81. }
  82. public Collection create(Collection record) {
  83. User minter = userRepo.findById(record.getMinterId()).orElse(SecurityUtils.getAuthenticatedUser());
  84. record.setMinter(minter.getNickname());
  85. record.setMinterId(minter.getId());
  86. record.setMinterAvatar(minter.getAvatar());
  87. record.setOwner(minter.getNickname());
  88. record.setOwnerId(minter.getId());
  89. record.setOwnerAvatar(minter.getAvatar());
  90. record.setStock(record.getTotal());
  91. record.setSale(0);
  92. if (record.isScheduleSale()) {
  93. if (record.getStartTime() == null) {
  94. throw new BusinessException("请填写定时发布时间");
  95. }
  96. if (record.getStartTime().isBefore(LocalDateTime.now())) {
  97. record.setOnShelf(true);
  98. record.setSalable(true);
  99. record.setStartTime(null);
  100. }
  101. }
  102. record = collectionRepo.save(record);
  103. onShelfTask(record);
  104. return record;
  105. }
  106. public Collection update(Collection record) {
  107. collectionRepo.update(record.getId(), record.isOnShelf(), record.isSalable(),
  108. record.getStartTime(), record.isScheduleSale(), record.getSort(),
  109. record.getDetail(), JSON.toJSONString(record.getPrivileges()),
  110. JSON.toJSONString(record.getProperties()), JSON.toJSONString(record.getModel3d()));
  111. collectionRepo.update(record.getId(), JSON.toJSONString(record.getPrivileges()));
  112. record = collectionRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
  113. onShelfTask(record);
  114. return record;
  115. }
  116. private void onShelfTask(Collection record) {
  117. ScheduledFuture<?> task = tasks.get(record.getId());
  118. if (task != null) {
  119. if (!task.cancel(true)) {
  120. return;
  121. }
  122. }
  123. if (record.isScheduleSale()) {
  124. if (record.getStartTime().minusSeconds(2).isAfter(LocalDateTime.now())) {
  125. Date date = Date.from(record.getStartTime().atZone(ZoneId.systemDefault()).toInstant());
  126. ScheduledFuture<?> future = taskScheduler.schedule(() -> {
  127. collectionRepo.scheduleOnShelf(record.getId());
  128. tasks.remove(record.getId());
  129. }, date);
  130. tasks.put(record.getId(), future);
  131. } else {
  132. collectionRepo.scheduleOnShelf(record.getId());
  133. }
  134. }
  135. }
  136. public CollectionDTO toDTO(Collection collection) {
  137. return toDTO(collection, true);
  138. }
  139. public CollectionDTO toDTO(Collection collection, boolean join) {
  140. CollectionDTO collectionDTO = new CollectionDTO();
  141. BeanUtils.copyProperties(collection, collectionDTO);
  142. if (join) {
  143. if (SecurityUtils.getAuthenticatedUser() != null) {
  144. List<Like> list = likeRepo.findByUserIdAndCollectionId(SecurityUtils.getAuthenticatedUser().getId(),
  145. collection.getId());
  146. collectionDTO.setLiked(!list.isEmpty());
  147. if (collection.getType() == CollectionType.BLIND_BOX) {
  148. collectionDTO.setAppointment(appointmentRepo.findFirstByBlindBoxId(collection.getId()).isPresent());
  149. }
  150. }
  151. }
  152. return collectionDTO;
  153. }
  154. public List<CollectionDTO> toDTO(List<Collection> collections) {
  155. List<Like> likes = new ArrayList<>();
  156. List<Appointment> appointments = new ArrayList<>();
  157. if (SecurityUtils.getAuthenticatedUser() != null) {
  158. likes.addAll(likeRepo.findByUserId(SecurityUtils.getAuthenticatedUser().getId()));
  159. appointments.addAll(appointmentRepo.findByUserId(SecurityUtils.getAuthenticatedUser().getId()));
  160. }
  161. return collections.stream().parallel().map(collection -> {
  162. CollectionDTO dto = toDTO(collection, false);
  163. if (!likes.isEmpty()) {
  164. dto.setLiked(likes.stream().anyMatch(l -> l.getCollectionId().equals(collection.getId())));
  165. }
  166. if (!appointments.isEmpty()) {
  167. dto.setAppointment(appointments.stream().anyMatch(a -> a.getBlindBoxId().equals(collection.getId())));
  168. }
  169. return dto;
  170. }).collect(Collectors.toList());
  171. }
  172. public Page<CollectionDTO> toDTO(Page<Collection> collections) {
  173. List<CollectionDTO> userDTOS = toDTO(collections.getContent());
  174. return new PageImpl<>(userDTOS, collections.getPageable(), collections.getTotalElements());
  175. }
  176. @Transactional
  177. public Collection createBlindBox(CreateBlindBox createBlindBox) {
  178. Collection blindBox = createBlindBox.getBlindBox();
  179. if (blindBox.getId() != null) {
  180. throw new BusinessException("无法完成此操作");
  181. }
  182. List<Collection> list =
  183. collectionRepo.findAllById(createBlindBox.getItems().stream().map(BlindBoxItem::getCollectionId)
  184. .collect(Collectors.toSet()));
  185. for (BlindBoxItem item : createBlindBox.getItems()) {
  186. Collection collection = list.stream().filter(i -> i.getId().equals(item.getCollectionId())).findAny()
  187. .orElseThrow(new BusinessException("所选藏品不存在"));
  188. if (item.getTotal() > collection.getStock()) {
  189. throw new BusinessException("所选藏品库存不足:" + collection.getName());
  190. }
  191. }
  192. User user = userRepo.findById(blindBox.getMinterId()).orElse(SecurityUtils.getAuthenticatedUser());
  193. blindBox.setMinter(user.getNickname());
  194. blindBox.setMinterId(user.getId());
  195. blindBox.setMinterAvatar(user.getAvatar());
  196. blindBox.setOwner(user.getNickname());
  197. blindBox.setOwnerId(user.getId());
  198. blindBox.setOwnerAvatar(user.getAvatar());
  199. blindBox.setTotal(createBlindBox.getItems().stream().mapToInt(BlindBoxItem::getTotal).sum());
  200. blindBox.setStock(blindBox.getTotal());
  201. blindBox.setSale(0);
  202. collectionRepo.save(blindBox);
  203. createBlindBox.getItems().stream().parallel().forEach(item -> {
  204. Collection collection = list.stream().filter(i -> i.getId().equals(item.getCollectionId())).findAny()
  205. .orElseThrow(new BusinessException("所选藏品不存在"));
  206. collectionRepo.increaseStock(collection.getId(), -item.getTotal());
  207. BlindBoxItem blindBoxItem = new BlindBoxItem();
  208. BeanUtils.copyProperties(collection, blindBoxItem);
  209. blindBoxItem.setId(null);
  210. blindBoxItem.setCollectionId(item.getCollectionId());
  211. blindBoxItem.setSale(0);
  212. blindBoxItem.setTotal(item.getTotal());
  213. blindBoxItem.setStock(item.getTotal());
  214. blindBoxItem.setRare(item.isRare());
  215. blindBoxItem.setBlindBoxId(blindBox.getId());
  216. blindBoxItemRepo.save(blindBoxItem);
  217. });
  218. return blindBox;
  219. }
  220. public void appointment(Long id, Long userId) {
  221. Collection collection = collectionRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  222. if (collection.getType() != CollectionType.BLIND_BOX) {
  223. throw new BusinessException("非盲盒,无需预约");
  224. }
  225. if (collection.getStartTime().isBefore(LocalDateTime.now())) {
  226. throw new BusinessException("盲盒已开售,无需预约");
  227. }
  228. appointmentRepo.save(Appointment.builder()
  229. .userId(userId)
  230. .blindBoxId(id)
  231. .build());
  232. }
  233. public BlindBoxItem draw(Long collectionId) {
  234. List<BlindBoxItem> items = blindBoxItemRepo.findByBlindBoxId(collectionId);
  235. Map<BlindBoxItem, Range<Integer>> randomRange = new HashMap<>();
  236. int c = 0, sum = 0;
  237. for (BlindBoxItem item : items) {
  238. randomRange.put(item, Range.between(c, c + item.getStock()));
  239. c += item.getStock();
  240. sum += item.getStock();
  241. }
  242. int retry = 0;
  243. BlindBoxItem winItem = null;
  244. while (winItem == null) {
  245. retry++;
  246. int rand = RandomUtils.nextInt(0, sum + 1);
  247. for (Map.Entry<BlindBoxItem, Range<Integer>> entry : randomRange.entrySet()) {
  248. BlindBoxItem item = entry.getKey();
  249. Range<Integer> range = entry.getValue();
  250. if (rand >= range.getMinimum() && rand < range.getMaximum()) {
  251. int total = items.stream().filter(i -> !i.isRare())
  252. .mapToInt(BlindBoxItem::getTotal).sum();
  253. int stock = items.stream().filter(i -> !i.isRare())
  254. .mapToInt(BlindBoxItem::getStock).sum();
  255. if (item.isRare()) {
  256. double nRate = stock / (double) total;
  257. double rRate = (item.getStock() - 1) / (double) item.getTotal();
  258. if (Math.abs(nRate - rRate) < (1 / (double) item.getTotal()) || retry > 1 || rRate == 0) {
  259. if (!(nRate > 0.1 && item.getStock() == 1)) {
  260. winItem = item;
  261. }
  262. }
  263. } else {
  264. double nRate = (stock - 1) / (double) total;
  265. double rRate = item.getStock() / (double) item.getTotal();
  266. if (Math.abs(nRate - rRate) < 0.2 || retry > 1 || nRate == 0) {
  267. winItem = item;
  268. }
  269. }
  270. }
  271. }
  272. if (retry > 100 && winItem == null) {
  273. throw new BusinessException("盲盒抽卡失败");
  274. }
  275. }
  276. winItem.setStock(winItem.getStock() - 1);
  277. winItem.setSale(winItem.getSale() + 1);
  278. blindBoxItemRepo.save(winItem);
  279. return winItem;
  280. }
  281. public synchronized Integer getNextNumber(Long collectionId) {
  282. collectionRepo.increaseNumber(collectionId, 1);
  283. return collectionRepo.getCurrentNumber(collectionId).orElse(0);
  284. }
  285. public void addStock(Long id, int number) {
  286. Collection collection = collectionRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  287. if (collection.getSource() != CollectionSource.OFFICIAL) {
  288. throw new BusinessException("用户转售无法增发");
  289. }
  290. if (collection.getType() == CollectionType.BLIND_BOX) {
  291. throw new BusinessException("盲盒无法增发");
  292. }
  293. collectionRepo.increaseStock(id, number);
  294. collectionRepo.increaseTotal(id, number);
  295. }
  296. }