CollectionService.java 17 KB

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