CollectionService.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. package com.izouma.nineth.service;
  2. import com.alibaba.fastjson.JSON;
  3. import com.izouma.nineth.annotations.Debounce;
  4. import com.izouma.nineth.config.GeneralProperties;
  5. import com.izouma.nineth.config.RedisKeys;
  6. import com.izouma.nineth.domain.Collection;
  7. import com.izouma.nineth.domain.*;
  8. import com.izouma.nineth.dto.*;
  9. import com.izouma.nineth.enums.CollectionSource;
  10. import com.izouma.nineth.enums.CollectionType;
  11. import com.izouma.nineth.enums.OrderStatus;
  12. import com.izouma.nineth.exception.BusinessException;
  13. import com.izouma.nineth.repo.*;
  14. import com.izouma.nineth.utils.JpaUtils;
  15. import com.izouma.nineth.utils.SecurityUtils;
  16. import lombok.AllArgsConstructor;
  17. import lombok.extern.slf4j.Slf4j;
  18. import org.apache.commons.collections.MapUtils;
  19. import org.apache.commons.lang3.RandomUtils;
  20. import org.apache.commons.lang3.Range;
  21. import org.apache.commons.lang3.StringUtils;
  22. import org.apache.rocketmq.spring.core.RocketMQTemplate;
  23. import org.springframework.beans.BeanUtils;
  24. import org.springframework.cache.annotation.Cacheable;
  25. import org.springframework.core.env.Environment;
  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. @Service
  45. @AllArgsConstructor
  46. @Slf4j
  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 RedisTemplate<String, Object> redisTemplate;
  56. private RocketMQTemplate rocketMQTemplate;
  57. private GeneralProperties generalProperties;
  58. private Environment env;
  59. private OrderRepo orderRepo;
  60. private final Map<Long, ScheduledFuture<?>> tasks = new HashMap<>();
  61. @PostConstruct
  62. public void init() {
  63. if (Arrays.asList(env.getActiveProfiles()).contains("dev")) {
  64. return;
  65. }
  66. List<Collection> collections = collectionRepo.findByScheduleSaleTrueAndOnShelfFalseAndStartTimeBeforeAndDelFalse(LocalDateTime.now());
  67. for (Collection collection : collections) {
  68. onShelfTask(collection);
  69. }
  70. for (CollectionStockAndSale collection : collectionRepo.getStockAndSale()) {
  71. if (redisTemplate.opsForValue().get(RedisKeys.COLLECTION_STOCK + collection.getId()) == null) {
  72. redisTemplate.opsForValue().set(RedisKeys.COLLECTION_STOCK + collection.getId(), collection.getStock());
  73. }
  74. if (redisTemplate.opsForValue().get(RedisKeys.COLLECTION_SALE + collection.getId()) == null) {
  75. redisTemplate.opsForValue().set(RedisKeys.COLLECTION_SALE + collection.getId(), collection.getSale());
  76. }
  77. }
  78. }
  79. @Cacheable(value = "collectionList", key = "#pageQuery.hashCode()")
  80. public PageWrapper<Collection> all(PageQuery pageQuery) {
  81. pageQuery.getQuery().put("del", false);
  82. String type = MapUtils.getString(pageQuery.getQuery(), "type", "DEFAULT");
  83. pageQuery.getQuery().remove("type");
  84. Specification<Collection> specification = JpaUtils.toSpecification(pageQuery, Collection.class);
  85. PageRequest pageRequest = JpaUtils.toPageRequest(pageQuery);
  86. if (pageRequest.getSort().stream().noneMatch(order -> order.getProperty().equals("createdAt"))) {
  87. pageRequest = PageRequest.of(pageRequest.getPageNumber(), pageQuery.getSize(),
  88. pageRequest.getSort().and(Sort.by("createdAt").descending()));
  89. }
  90. specification = specification.and((Specification<Collection>) (root, criteriaQuery, criteriaBuilder) -> {
  91. List<Predicate> and = new ArrayList<>();
  92. if (StringUtils.isNotEmpty(type) && !"all".equalsIgnoreCase(type)) {
  93. try {
  94. if (type.contains(",")) {
  95. and.add(root.get("type")
  96. .in(Arrays.stream(type.split(",")).map(s -> Enum.valueOf(CollectionType.class, s))
  97. .collect(Collectors.toList())));
  98. } else {
  99. and.add(criteriaBuilder.equal(root.get("type"), Enum.valueOf(CollectionType.class, type)));
  100. }
  101. } catch (Exception e) {
  102. }
  103. }
  104. return criteriaBuilder.and(and.toArray(new Predicate[0]));
  105. });
  106. Page<Collection> page = collectionRepo.findAll(specification, pageRequest);
  107. return new PageWrapper<>(page.getContent(), page.getPageable().getPageNumber(),
  108. page.getPageable().getPageSize(), page.getTotalElements());
  109. }
  110. public Collection create(Collection record) {
  111. User minter = userRepo.findById(record.getMinterId()).orElse(SecurityUtils.getAuthenticatedUser());
  112. record.setMinter(minter.getNickname());
  113. record.setMinterId(minter.getId());
  114. record.setMinterAvatar(minter.getAvatar());
  115. record.setOwner(minter.getNickname());
  116. record.setOwnerId(minter.getId());
  117. record.setOwnerAvatar(minter.getAvatar());
  118. record.setStock(record.getTotal());
  119. record.setSale(0);
  120. if (record.isScheduleSale()) {
  121. if (record.getStartTime() == null) {
  122. throw new BusinessException("请填写定时发布时间");
  123. }
  124. if (record.getStartTime().isBefore(LocalDateTime.now())) {
  125. record.setOnShelf(true);
  126. record.setSalable(true);
  127. record.setStartTime(null);
  128. }
  129. }
  130. record = collectionRepo.save(record);
  131. onShelfTask(record);
  132. redisTemplate.opsForValue().set(RedisKeys.COLLECTION_STOCK + record.getId(), record.getStock());
  133. redisTemplate.opsForValue().set(RedisKeys.COLLECTION_SALE + record.getId(), record.getSale());
  134. return record;
  135. }
  136. public Collection update(Collection record) {
  137. collectionRepo.update(record.getId(), record.isOnShelf(), record.isSalable(),
  138. record.getStartTime(), record.isScheduleSale(), record.getSort(),
  139. record.getDetail(), JSON.toJSONString(record.getPrivileges()),
  140. JSON.toJSONString(record.getProperties()), JSON.toJSONString(record.getModel3d()),
  141. record.getMaxCount(), record.getCountId(), record.isScanCode(), record.isNoSoldOut(),
  142. record.getAssignment(), record.isCouponPayment(), record.getShareBg(), record.getRegisterBg(),
  143. record.getVipQuota(), record.getTimeDelay(), record.getSaleTime(), record.getHoldDays(),
  144. record.getOpenQuota());
  145. record = collectionRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
  146. onShelfTask(record);
  147. return record;
  148. }
  149. private void onShelfTask(Collection record) {
  150. ScheduledFuture<?> task = tasks.get(record.getId());
  151. if (task != null) {
  152. if (!task.cancel(true)) {
  153. return;
  154. }
  155. }
  156. if (record.isScheduleSale()) {
  157. if (record.getStartTime().minusSeconds(2).isAfter(LocalDateTime.now())) {
  158. Date date = Date.from(record.getStartTime().atZone(ZoneId.systemDefault()).toInstant());
  159. ScheduledFuture<?> future = taskScheduler.schedule(() -> {
  160. collectionRepo.scheduleOnShelf(record.getId(), !record.isScanCode());
  161. tasks.remove(record.getId());
  162. }, date);
  163. tasks.put(record.getId(), future);
  164. } else {
  165. collectionRepo.scheduleOnShelf(record.getId(), !record.isScanCode());
  166. }
  167. }
  168. }
  169. public CollectionDTO toDTO(Collection collection) {
  170. return toDTO(collection, true, false);
  171. }
  172. public CollectionDTO toDTO(Collection collection, boolean join, boolean showVip) {
  173. CollectionDTO collectionDTO = new CollectionDTO();
  174. BeanUtils.copyProperties(collection, collectionDTO);
  175. if (join) {
  176. User user = SecurityUtils.getAuthenticatedUser();
  177. if (user != null) {
  178. List<Like> list = likeRepo.findByUserIdAndCollectionId(user.getId(),
  179. collection.getId());
  180. collectionDTO.setLiked(!list.isEmpty());
  181. if (collection.getType() == CollectionType.BLIND_BOX) {
  182. collectionDTO.setAppointment(appointmentRepo.findFirstByBlindBoxId(collection.getId()).isPresent());
  183. }
  184. if (showVip && collection.getAssignment() > 0 && user.getVipPurchase() > 0) {
  185. int purchase = orderRepo.countByUserIdAndCollectionIdAndVipTrueAndStatusIn(user.getId(), collection.getId(), Arrays.asList(OrderStatus.FINISH, OrderStatus.NOT_PAID, OrderStatus.PROCESSING));
  186. collectionDTO.setVipSurplus(user.getVipPurchase() - purchase);
  187. }
  188. }
  189. }
  190. return collectionDTO;
  191. }
  192. public List<CollectionDTO> toDTO(List<Collection> collections) {
  193. List<Like> likes = new ArrayList<>();
  194. List<Appointment> appointments = new ArrayList<>();
  195. if (SecurityUtils.getAuthenticatedUser() != null) {
  196. likes.addAll(likeRepo.findByUserId(SecurityUtils.getAuthenticatedUser().getId()));
  197. appointments.addAll(appointmentRepo.findByUserId(SecurityUtils.getAuthenticatedUser().getId()));
  198. }
  199. return collections.stream().parallel().map(collection -> {
  200. CollectionDTO dto = toDTO(collection, false, false);
  201. if (!likes.isEmpty()) {
  202. dto.setLiked(likes.stream().anyMatch(l -> l.getCollectionId().equals(collection.getId())));
  203. }
  204. if (!appointments.isEmpty()) {
  205. dto.setAppointment(appointments.stream().anyMatch(a -> a.getBlindBoxId().equals(collection.getId())));
  206. }
  207. return dto;
  208. }).collect(Collectors.toList());
  209. }
  210. public Page<CollectionDTO> toDTO(Page<Collection> collections) {
  211. List<CollectionDTO> userDTOS = toDTO(collections.getContent());
  212. return new PageImpl<>(userDTOS, collections.getPageable(), collections.getTotalElements());
  213. }
  214. @Transactional
  215. public Collection createBlindBox(CreateBlindBox createBlindBox) {
  216. Collection blindBox = createBlindBox.getBlindBox();
  217. if (blindBox.getId() != null) {
  218. throw new BusinessException("无法完成此操作");
  219. }
  220. List<Collection> list =
  221. collectionRepo.findAllById(createBlindBox.getItems().stream().map(BlindBoxItem::getCollectionId)
  222. .collect(Collectors.toSet()));
  223. for (BlindBoxItem item : createBlindBox.getItems()) {
  224. Collection collection = list.stream().filter(i -> i.getId().equals(item.getCollectionId())).findAny()
  225. .orElseThrow(new BusinessException("所选藏品不存在"));
  226. if (item.getTotal() > collection.getStock()) {
  227. throw new BusinessException("所选藏品库存不足:" + collection.getName());
  228. }
  229. }
  230. User user = userRepo.findById(blindBox.getMinterId()).orElse(SecurityUtils.getAuthenticatedUser());
  231. blindBox.setMinter(user.getNickname());
  232. blindBox.setMinterId(user.getId());
  233. blindBox.setMinterAvatar(user.getAvatar());
  234. blindBox.setOwner(user.getNickname());
  235. blindBox.setOwnerId(user.getId());
  236. blindBox.setOwnerAvatar(user.getAvatar());
  237. blindBox.setTotal(createBlindBox.getItems().stream().mapToInt(BlindBoxItem::getTotal).sum());
  238. blindBox.setStock(blindBox.getTotal());
  239. blindBox.setSale(0);
  240. collectionRepo.save(blindBox);
  241. createBlindBox.getItems().stream().parallel().forEach(item -> {
  242. Collection collection = list.stream().filter(i -> i.getId().equals(item.getCollectionId())).findAny()
  243. .orElseThrow(new BusinessException("所选藏品不存在"));
  244. decreaseStock(collection.getId(), item.getTotal());
  245. BlindBoxItem blindBoxItem = new BlindBoxItem();
  246. BeanUtils.copyProperties(collection, blindBoxItem);
  247. blindBoxItem.setId(null);
  248. blindBoxItem.setCollectionId(item.getCollectionId());
  249. blindBoxItem.setSale(0);
  250. blindBoxItem.setTotal(item.getTotal());
  251. blindBoxItem.setStock(item.getTotal());
  252. blindBoxItem.setRare(item.isRare());
  253. blindBoxItem.setBlindBoxId(blindBox.getId());
  254. blindBoxItemRepo.saveAndFlush(blindBoxItem);
  255. log.info("createBlindBoxItemSuccess" + blindBoxItem.getId());
  256. });
  257. return blindBox;
  258. }
  259. public void appointment(Long id, Long userId) {
  260. Collection collection = collectionRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  261. if (collection.getType() != CollectionType.BLIND_BOX) {
  262. throw new BusinessException("非盲盒,无需预约");
  263. }
  264. if (collection.getStartTime().isBefore(LocalDateTime.now())) {
  265. throw new BusinessException("盲盒已开售,无需预约");
  266. }
  267. appointmentRepo.save(Appointment.builder()
  268. .userId(userId)
  269. .blindBoxId(id)
  270. .build());
  271. }
  272. public synchronized BlindBoxItem draw(Long collectionId) {
  273. List<BlindBoxItem> items = blindBoxItemRepo.findByBlindBoxId(collectionId);
  274. Map<BlindBoxItem, Range<Integer>> randomRange = new HashMap<>();
  275. int c = 0, sum = 0;
  276. for (BlindBoxItem item : items) {
  277. randomRange.put(item, Range.between(c, c + item.getStock()));
  278. c += item.getStock();
  279. sum += item.getStock();
  280. }
  281. int retry = 0;
  282. BlindBoxItem winItem = null;
  283. while (winItem == null) {
  284. retry++;
  285. int rand = RandomUtils.nextInt(0, sum + 1);
  286. for (Map.Entry<BlindBoxItem, Range<Integer>> entry : randomRange.entrySet()) {
  287. BlindBoxItem item = entry.getKey();
  288. Range<Integer> range = entry.getValue();
  289. if (rand >= range.getMinimum() && rand < range.getMaximum()) {
  290. int total = items.stream().filter(i -> !i.isRare())
  291. .mapToInt(BlindBoxItem::getTotal).sum();
  292. int stock = items.stream().filter(i -> !i.isRare())
  293. .mapToInt(BlindBoxItem::getStock).sum();
  294. if (item.isRare()) {
  295. double nRate = stock / (double) total;
  296. double rRate = (item.getStock() - 1) / (double) item.getTotal();
  297. if (Math.abs(nRate - rRate) < (1 / (double) item.getTotal()) || retry > 1 || rRate == 0) {
  298. if (!(nRate > 0.1 && item.getStock() == 1)) {
  299. winItem = item;
  300. }
  301. }
  302. } else {
  303. double nRate = (stock - 1) / (double) total;
  304. double rRate = item.getStock() / (double) item.getTotal();
  305. if (Math.abs(nRate - rRate) < 0.2 || retry > 1 || nRate == 0) {
  306. winItem = item;
  307. }
  308. }
  309. }
  310. }
  311. if (retry > 100 && winItem == null) {
  312. throw new BusinessException("盲盒抽卡失败");
  313. }
  314. }
  315. // winItem.setStock(winItem.getStock() - 1);
  316. // winItem.setSale(winItem.getSale() + 1);
  317. // blindBoxItemRepo.saveAndFlush(winItem);
  318. blindBoxItemRepo.decreaseStockAndIncreaseSale(winItem.getId(), 1);
  319. blindBoxItemRepo.flush();
  320. return winItem;
  321. }
  322. public synchronized Integer getNextNumber(Long collectionId) {
  323. collectionRepo.increaseNumber(collectionId, 1);
  324. return collectionRepo.getCurrentNumber(collectionId).orElse(0);
  325. }
  326. public void addStock(Long id, int number) {
  327. Collection collection = collectionRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  328. if (collection.getSource() != CollectionSource.OFFICIAL) {
  329. throw new BusinessException("用户转售无法增发");
  330. }
  331. if (collection.getType() == CollectionType.BLIND_BOX) {
  332. throw new BusinessException("盲盒无法增发");
  333. }
  334. increaseStock(id, number);
  335. collectionRepo.increaseTotal(id, number);
  336. }
  337. public synchronized Long increaseStock(Long id, int number) {
  338. BoundValueOperations<String, Object> ops = redisTemplate.boundValueOps(RedisKeys.COLLECTION_STOCK + id);
  339. if (ops.get() == null) {
  340. Boolean success = ops.setIfAbsent(Optional.ofNullable(collectionRepo.getStock(id))
  341. .orElse(0), 7, TimeUnit.DAYS);
  342. log.info("创建redis库存:{}", success);
  343. }
  344. Long stock = ops.increment(number);
  345. rocketMQTemplate.convertAndSend(generalProperties.getUpdateStockTopic(), id);
  346. return stock;
  347. }
  348. public synchronized Integer getStock(Long id) {
  349. BoundValueOperations<String, Object> ops = redisTemplate.boundValueOps(RedisKeys.COLLECTION_STOCK + id);
  350. Integer stock = (Integer) ops.get();
  351. if (stock == null) {
  352. Boolean success = ops.setIfAbsent(Optional.ofNullable(collectionRepo.getStock(id))
  353. .orElse(0), 7, TimeUnit.DAYS);
  354. log.info("创建redis库存:{}", success);
  355. return (Integer) ops.get();
  356. } else {
  357. return stock;
  358. }
  359. }
  360. public synchronized Long decreaseStock(Long id, int number) {
  361. return increaseStock(id, -number);
  362. }
  363. public synchronized Long increaseSale(Long id, int number) {
  364. BoundValueOperations<String, Object> ops = redisTemplate.boundValueOps(RedisKeys.COLLECTION_SALE + id);
  365. if (ops.get() == null) {
  366. Boolean success = ops.setIfAbsent(Optional.ofNullable(collectionRepo.getSale(id))
  367. .orElse(0), 7, TimeUnit.DAYS);
  368. log.info("创建redis销量:{}", success);
  369. }
  370. Long sale = ops.increment(number);
  371. redisTemplate.opsForHash().increment(RedisKeys.UPDATE_SALE, id, 1);
  372. // rocketMQTemplate.convertAndSend(generalProperties.getUpdateSaleTopic(), id);
  373. return sale;
  374. }
  375. public synchronized Long decreaseSale(Long id, int number) {
  376. return increaseSale(id, -number);
  377. }
  378. @Debounce(key = "#id", delay = 500)
  379. public void syncStock(Long id) {
  380. Integer stock = (Integer) redisTemplate.opsForValue().get(RedisKeys.COLLECTION_STOCK + id);
  381. if (stock != null) {
  382. log.info("同步库存信息{}", id);
  383. collectionRepo.updateStock(id, stock);
  384. cacheService.clearCollection(id);
  385. }
  386. }
  387. // @Debounce(key = "#id", delay = 500)
  388. public void syncSale(Long id) {
  389. Integer sale = (Integer) redisTemplate.opsForValue().get(RedisKeys.COLLECTION_SALE + id);
  390. if (sale != null) {
  391. log.info("同步销量信息{}", id);
  392. collectionRepo.updateSale(id, sale);
  393. cacheService.clearCollection(id);
  394. }
  395. }
  396. @Debounce(key = "#id", delay = 500)
  397. public void syncQuota(Long id) {
  398. Integer quota = (Integer) redisTemplate.opsForValue().get(RedisKeys.COLLECTION_QUOTA + id);
  399. if (quota != null) {
  400. log.info("同步额度信息{}", id);
  401. collectionRepo.updateVipQuota(id, quota);
  402. cacheService.clearCollection(id);
  403. }
  404. }
  405. public synchronized Long decreaseQuota(Long id, int number) {
  406. BoundValueOperations<String, Object> ops = redisTemplate.boundValueOps(RedisKeys.COLLECTION_QUOTA + id);
  407. if (ops.get() == null) {
  408. Boolean success = ops.setIfAbsent(Optional.ofNullable(collectionRepo.getVipQuota(id))
  409. .orElse(0), 7, TimeUnit.DAYS);
  410. log.info("创建redis额度:{}", success);
  411. }
  412. Long stock = ops.increment(-number);
  413. rocketMQTemplate.convertAndSend(generalProperties.getUpdateQuotaTopic(), id);
  414. return stock;
  415. }
  416. }