CollectionService.java 19 KB

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