CollectionService.java 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. package com.izouma.nineth.service;
  2. import cn.hutool.core.util.ObjectUtil;
  3. import com.alibaba.fastjson.JSON;
  4. import com.izouma.nineth.TokenHistory;
  5. import com.izouma.nineth.annotations.Debounce;
  6. import com.izouma.nineth.config.GeneralProperties;
  7. import com.izouma.nineth.config.RedisKeys;
  8. import com.izouma.nineth.converter.LongArrayConverter;
  9. import com.izouma.nineth.domain.Collection;
  10. import com.izouma.nineth.domain.*;
  11. import com.izouma.nineth.dto.*;
  12. import com.izouma.nineth.enums.*;
  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.ObjectUtils;
  22. import org.apache.commons.lang3.RandomUtils;
  23. import org.apache.commons.lang3.Range;
  24. import org.apache.commons.lang3.StringUtils;
  25. import org.apache.rocketmq.spring.core.RocketMQTemplate;
  26. import org.springframework.beans.BeanUtils;
  27. import org.springframework.cache.annotation.Cacheable;
  28. import org.springframework.core.env.Environment;
  29. import org.springframework.data.domain.Page;
  30. import org.springframework.data.domain.PageImpl;
  31. import org.springframework.data.domain.PageRequest;
  32. import org.springframework.data.domain.Sort;
  33. import org.springframework.data.jpa.domain.Specification;
  34. import org.springframework.data.redis.core.BoundValueOperations;
  35. import org.springframework.data.redis.core.RedisTemplate;
  36. import org.springframework.scheduling.TaskScheduler;
  37. import org.springframework.stereotype.Service;
  38. import org.springframework.web.bind.annotation.RequestParam;
  39. import javax.annotation.PostConstruct;
  40. import javax.persistence.criteria.Predicate;
  41. import javax.transaction.Transactional;
  42. import java.math.BigDecimal;
  43. import java.time.LocalDate;
  44. import java.time.LocalDateTime;
  45. import java.time.ZoneId;
  46. import java.time.format.DateTimeFormatter;
  47. import java.util.*;
  48. import java.util.concurrent.ScheduledFuture;
  49. import java.util.concurrent.TimeUnit;
  50. import java.util.concurrent.atomic.AtomicInteger;
  51. import java.util.stream.Collectors;
  52. @Service
  53. @AllArgsConstructor
  54. @Slf4j
  55. public class CollectionService {
  56. private CollectionRepo collectionRepo;
  57. private LikeRepo likeRepo;
  58. private BlindBoxItemRepo blindBoxItemRepo;
  59. private AppointmentRepo appointmentRepo;
  60. private UserRepo userRepo;
  61. private TaskScheduler taskScheduler;
  62. private CacheService cacheService;
  63. private RedisTemplate<String, Object> redisTemplate;
  64. private RocketMQTemplate rocketMQTemplate;
  65. private GeneralProperties generalProperties;
  66. private Environment env;
  67. private OrderRepo orderRepo;
  68. private TokenHistoryRepo tokenHistoryRepo;
  69. private PointRecordRepo pointRecordRepo;
  70. private SubscribeRepo subscribeRepo;
  71. private FollowRepo followRepo;
  72. private SubscribeTimeRepo subscribeTimeRepo;
  73. private final Map<Long, ScheduledFuture<?>> tasks = new HashMap<>();
  74. @PostConstruct
  75. public void init() {
  76. if (Arrays.asList(env.getActiveProfiles()).contains("dev")) {
  77. return;
  78. }
  79. List<Collection> collections = collectionRepo
  80. .findByScheduleSaleTrueAndOnShelfFalseAndStartTimeBeforeAndDelFalse(LocalDateTime.now());
  81. for (Collection collection : collections) {
  82. // onShelfTask(collection);
  83. }
  84. }
  85. @Cacheable(value = "collectionList", key = "#pageQuery.hashCode()")
  86. public PageWrapper<Collection> all(PageQuery pageQuery) {
  87. pageQuery.getQuery().put("del", false);
  88. String type = MapUtils.getString(pageQuery.getQuery(), "type", "BLIND_BOX,DEFAULT");
  89. pageQuery.getQuery().remove("type");
  90. Specification<Collection> specification = JpaUtils.toSpecification(pageQuery, Collection.class);
  91. PageRequest pageRequest = JpaUtils.toPageRequest(pageQuery);
  92. if (pageRequest.getSort().stream().noneMatch(order -> order.getProperty().equals("createdAt"))) {
  93. pageRequest = PageRequest.of(pageRequest.getPageNumber(), pageQuery.getSize(),
  94. pageRequest.getSort().and(Sort.by("createdAt").descending()));
  95. }
  96. specification = specification.and((Specification<Collection>) (root, criteriaQuery, criteriaBuilder) -> {
  97. List<Predicate> and = new ArrayList<>();
  98. if (StringUtils.isNotEmpty(type) && !"all".equalsIgnoreCase(type)) {
  99. try {
  100. if (type.contains(",")) {
  101. and.add(root.get("type")
  102. .in(Arrays.stream(type.split(",")).map(s -> Enum.valueOf(CollectionType.class, s))
  103. .collect(Collectors.toList())));
  104. } else {
  105. and.add(criteriaBuilder.equal(root.get("type"), Enum.valueOf(CollectionType.class, type)));
  106. }
  107. } catch (Exception e) {
  108. }
  109. }
  110. return criteriaBuilder.and(and.toArray(new Predicate[0]));
  111. });
  112. Page<Collection> page = collectionRepo.findAll(specification, pageRequest);
  113. return new PageWrapper<>(page.getContent(), page.getPageable().getPageNumber(),
  114. page.getPageable().getPageSize(), page.getTotalElements());
  115. }
  116. public Collection create(Collection record) {
  117. User minter = userRepo.findById(record.getMinterId()).orElse(SecurityUtils.getAuthenticatedUser());
  118. record.setMinter(minter.getNickname());
  119. record.setMinterId(minter.getId());
  120. record.setMinterAvatar(minter.getAvatar());
  121. record.setOwner(minter.getNickname());
  122. record.setOwnerId(minter.getId());
  123. record.setOwnerAvatar(minter.getAvatar());
  124. record.setStock(record.getTotal());
  125. record.setSale(0);
  126. record.setVipQuota(record.getTotalQuota());
  127. record.setSubscribeStatus(SubscribeStatus.NOT_STARTED);
  128. if (record.isHasSubscribe()) {
  129. if (record.getStartTime() == null) {
  130. throw new BusinessException("请填写预约发布时间");
  131. }
  132. if (record.getStartTime().isBefore(LocalDateTime.now())) {
  133. record.setOnShelf(true);
  134. record.setSalable(true);
  135. // record.setStartTime(null);
  136. }
  137. }
  138. record = collectionRepo.save(record);
  139. // onShelfTask(record);
  140. redisTemplate.opsForValue().set(RedisKeys.COLLECTION_STOCK + record.getId(), record.getStock());
  141. redisTemplate.opsForValue().set(RedisKeys.COLLECTION_SALE + record.getId(), record.getSale());
  142. if (record.getSource().equals(CollectionSource.OFFICIAL) & record.isOnShelf()) {
  143. cacheService.clearSubscribeCollectionList(LocalDate.now());
  144. }
  145. return record;
  146. }
  147. public Collection update(Collection record) {
  148. collectionRepo.update(record.getId(), record.isOnShelf(), record.isSalable(),
  149. record.getStartTime(), record.isHasSubscribe(), record.getSort(),
  150. record.getDetail(), JSON.toJSONString(record.getPrivileges()),
  151. JSON.toJSONString(record.getProperties()), JSON.toJSONString(record.getModel3d()),
  152. record.getMaxCount(), record.getCountId(), record.isScanCode(), record.isNoSoldOut(),
  153. record.getAssignment(), record.isCouponPayment(), record.getShareBg(), record.getRegisterBg(),
  154. record.getVipQuota(), record.getTimeDelay(), record.getSaleTime(), record.getHoldDays(),
  155. record.getOpenQuota(), record.getShowroomBg(), record.getMaxCollection(), record.getTotalQuota(), record
  156. .getCollectionCategory(),
  157. record.getCollectionWorks(), record.getIssuer(), record.getPurchaseInstructions(), record
  158. .getEndTime(), record.getPublishTime(), record.getPurchaseTime());
  159. record = collectionRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
  160. // onShelfTask(record);
  161. if (record.getSource().equals(CollectionSource.OFFICIAL) & record.isOnShelf()) {
  162. cacheService.clearSubscribeCollectionList(LocalDate.now());
  163. }
  164. return record;
  165. }
  166. private void onShelfTask(Collection record) {
  167. ScheduledFuture<?> task = tasks.get(record.getId());
  168. if (task != null) {
  169. if (!task.cancel(true)) {
  170. return;
  171. }
  172. }
  173. if (record.isHasSubscribe()) {
  174. if (record.getStartTime().minusSeconds(2).isAfter(LocalDateTime.now())) {
  175. Date date = Date.from(record.getStartTime().atZone(ZoneId.systemDefault()).toInstant());
  176. ScheduledFuture<?> future = taskScheduler.schedule(() -> {
  177. // collectionRepo.scheduleOnShelf(record.getId(), !record.isScanCode());
  178. tasks.remove(record.getId());
  179. }, date);
  180. tasks.put(record.getId(), future);
  181. } else {
  182. // collectionRepo.scheduleOnShelf(record.getId(), !record.isScanCode());
  183. }
  184. }
  185. }
  186. public CollectionDTO toDTO(Collection collection) {
  187. return toDTO(collection, true, false);
  188. }
  189. public CollectionDTO toDTO(Collection collection, boolean join, boolean showVip) {
  190. CollectionDTO collectionDTO = new CollectionDTO();
  191. BeanUtils.copyProperties(collection, collectionDTO);
  192. if (join) {
  193. User user = SecurityUtils.getAuthenticatedUser();
  194. if (user != null) {
  195. List<Like> list = likeRepo.findByUserIdAndCollectionId(user.getId(),
  196. collection.getId());
  197. collectionDTO.setLiked(!list.isEmpty());
  198. if (collection.getType() == CollectionType.BLIND_BOX) {
  199. collectionDTO.setAppointment(appointmentRepo.findFirstByBlindBoxId(collection.getId()).isPresent());
  200. }
  201. if (showVip && collection.getAssignment() > 0 && user.getVipPurchase() > 0) {
  202. int purchase = orderRepo
  203. .countByUserIdAndCollectionIdAndVipTrueAndStatusIn(user.getId(), collection.getId(), Arrays
  204. .asList(OrderStatus.FINISH, OrderStatus.NOT_PAID, OrderStatus.PROCESSING));
  205. collectionDTO.setVipSurplus(user.getVipPurchase() - purchase);
  206. }
  207. Subscribe subscribe = subscribeRepo.findFirstByCollectionIdAndUserId(collection.getId(), user.getId());
  208. if (!ObjectUtil.isEmpty(subscribe)) {
  209. collectionDTO.setSubscribed(true);
  210. if (subscribe.isPurchaseQualifications()) {
  211. // collectionDTO.setPurchaseQualifications(subscribe.isPurchaseQualifications());
  212. collectionDTO.setPurchaseQualifications(true);
  213. }
  214. }
  215. Follow follow = followRepo.findFirstByFollowUserIdAndUserId(collection.getMinterId(), user.getId());
  216. if (!ObjectUtil.isEmpty(follow)) {
  217. collectionDTO.setFollow(true);
  218. }
  219. }
  220. }
  221. return collectionDTO;
  222. }
  223. public List<CollectionDTO> toDTO(List<Collection> collections) {
  224. List<Like> likes = new ArrayList<>();
  225. List<Appointment> appointments = new ArrayList<>();
  226. if (SecurityUtils.getAuthenticatedUser() != null) {
  227. likes.addAll(likeRepo.findByUserId(SecurityUtils.getAuthenticatedUser().getId()));
  228. appointments.addAll(appointmentRepo.findByUserId(SecurityUtils.getAuthenticatedUser().getId()));
  229. }
  230. return collections.stream().parallel().map(collection -> {
  231. CollectionDTO dto = toDTO(collection, false, false);
  232. if (!likes.isEmpty()) {
  233. dto.setLiked(likes.stream().anyMatch(l -> l.getCollectionId().equals(collection.getId())));
  234. }
  235. if (!appointments.isEmpty()) {
  236. dto.setAppointment(appointments.stream().anyMatch(a -> a.getBlindBoxId().equals(collection.getId())));
  237. }
  238. return dto;
  239. }).collect(Collectors.toList());
  240. }
  241. public Page<CollectionDTO> toDTO(Page<Collection> collections) {
  242. List<CollectionDTO> userDTOS = toDTO(collections.getContent());
  243. return new PageImpl<>(userDTOS, collections.getPageable(), collections.getTotalElements());
  244. }
  245. @Transactional
  246. public Collection createBlindBox(CreateBlindBox createBlindBox) {
  247. Collection blindBox = createBlindBox.getBlindBox();
  248. if (blindBox.getId() != null) {
  249. throw new BusinessException("无法完成此操作");
  250. }
  251. List<Collection> list =
  252. collectionRepo.findAllById(createBlindBox.getItems().stream().map(BlindBoxItem::getCollectionId)
  253. .collect(Collectors.toSet()));
  254. for (BlindBoxItem item : createBlindBox.getItems()) {
  255. Collection collection = list.stream().filter(i -> i.getId().equals(item.getCollectionId())).findAny()
  256. .orElseThrow(new BusinessException("所选藏品不存在"));
  257. if (item.getTotal() > collection.getStock()) {
  258. throw new BusinessException("所选藏品库存不足:" + collection.getName());
  259. }
  260. }
  261. User user = userRepo.findById(blindBox.getMinterId()).orElse(SecurityUtils.getAuthenticatedUser());
  262. blindBox.setSubscribeStatus(SubscribeStatus.NOT_STARTED);
  263. if (blindBox.isHasSubscribe()) {
  264. if (blindBox.getStartTime() == null) {
  265. throw new BusinessException("请填写预约发布时间");
  266. }
  267. }
  268. blindBox.setMinter(user.getNickname());
  269. blindBox.setMinterId(user.getId());
  270. blindBox.setMinterAvatar(user.getAvatar());
  271. blindBox.setOwner(user.getNickname());
  272. blindBox.setOwnerId(user.getId());
  273. blindBox.setOwnerAvatar(user.getAvatar());
  274. blindBox.setTotal(createBlindBox.getItems().stream().mapToInt(BlindBoxItem::getTotal).sum());
  275. blindBox.setStock(blindBox.getTotal());
  276. blindBox.setSale(0);
  277. collectionRepo.save(blindBox);
  278. createBlindBox.getItems().stream().parallel().forEach(item -> {
  279. Collection collection = list.stream().filter(i -> i.getId().equals(item.getCollectionId())).findAny()
  280. .orElseThrow(new BusinessException("所选藏品不存在"));
  281. decreaseStock(collection.getId(), item.getTotal());
  282. BlindBoxItem blindBoxItem = new BlindBoxItem();
  283. BeanUtils.copyProperties(collection, blindBoxItem);
  284. blindBoxItem.setId(null);
  285. blindBoxItem.setCollectionId(item.getCollectionId());
  286. blindBoxItem.setSale(0);
  287. blindBoxItem.setTotal(item.getTotal());
  288. blindBoxItem.setStock(item.getTotal());
  289. blindBoxItem.setRare(item.isRare());
  290. blindBoxItem.setBlindBoxId(blindBox.getId());
  291. blindBoxItemRepo.saveAndFlush(blindBoxItem);
  292. log.info("createBlindBoxItemSuccess" + blindBoxItem.getId());
  293. });
  294. return blindBox;
  295. }
  296. public void appointment(Long id, Long userId) {
  297. Collection collection = collectionRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  298. if (collection.getType() != CollectionType.BLIND_BOX) {
  299. throw new BusinessException("非盲盒,无需预约");
  300. }
  301. if (collection.getStartTime().isBefore(LocalDateTime.now())) {
  302. throw new BusinessException("盲盒已开售,无需预约");
  303. }
  304. appointmentRepo.save(Appointment.builder()
  305. .userId(userId)
  306. .blindBoxId(id)
  307. .build());
  308. }
  309. public synchronized BlindBoxItem draw(Long collectionId) {
  310. List<BlindBoxItem> items = blindBoxItemRepo.findByBlindBoxId(collectionId);
  311. Map<BlindBoxItem, Range<Integer>> randomRange = new HashMap<>();
  312. int c = 0, sum = 0;
  313. for (BlindBoxItem item : items) {
  314. randomRange.put(item, Range.between(c, c + item.getStock()));
  315. c += item.getStock();
  316. sum += item.getStock();
  317. }
  318. int retry = 0;
  319. BlindBoxItem winItem = null;
  320. while (winItem == null) {
  321. retry++;
  322. int rand = RandomUtils.nextInt(0, sum + 1);
  323. for (Map.Entry<BlindBoxItem, Range<Integer>> entry : randomRange.entrySet()) {
  324. BlindBoxItem item = entry.getKey();
  325. Range<Integer> range = entry.getValue();
  326. if (rand >= range.getMinimum() && rand < range.getMaximum()) {
  327. int total = items.stream().filter(i -> !i.isRare())
  328. .mapToInt(BlindBoxItem::getTotal).sum();
  329. int stock = items.stream().filter(i -> !i.isRare())
  330. .mapToInt(BlindBoxItem::getStock).sum();
  331. if (item.isRare()) {
  332. double nRate = stock / (double) total;
  333. double rRate = (item.getStock() - 1) / (double) item.getTotal();
  334. if (Math.abs(nRate - rRate) < (1 / (double) item.getTotal()) || retry > 1 || rRate == 0) {
  335. if (!(nRate > 0.1 && item.getStock() == 1)) {
  336. winItem = item;
  337. }
  338. }
  339. } else {
  340. double nRate = (stock - 1) / (double) total;
  341. double rRate = item.getStock() / (double) item.getTotal();
  342. if (Math.abs(nRate - rRate) < 0.2 || retry > 1 || nRate == 0) {
  343. winItem = item;
  344. }
  345. }
  346. }
  347. }
  348. if (retry > 100 && winItem == null) {
  349. throw new BusinessException("盲盒抽卡失败");
  350. }
  351. }
  352. // winItem.setStock(winItem.getStock() - 1);
  353. // winItem.setSale(winItem.getSale() + 1);
  354. // blindBoxItemRepo.saveAndFlush(winItem);
  355. blindBoxItemRepo.decreaseStockAndIncreaseSale(winItem.getId(), 1);
  356. blindBoxItemRepo.flush();
  357. return winItem;
  358. }
  359. public synchronized Integer getNextNumber(Long collectionId) {
  360. collectionRepo.increaseNumber(collectionId, 1);
  361. return collectionRepo.getCurrentNumber(collectionId).orElse(0);
  362. }
  363. public void addStock(Long id, int number) {
  364. Collection collection = collectionRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  365. if (collection.getSource() != CollectionSource.OFFICIAL) {
  366. throw new BusinessException("用户转售无法增发");
  367. }
  368. if (collection.getType() == CollectionType.BLIND_BOX) {
  369. throw new BusinessException("盲盒无法增发");
  370. }
  371. increaseStock(id, number);
  372. collectionRepo.increaseTotal(id, number);
  373. }
  374. public synchronized Long increaseStock(Long id, int number) {
  375. BoundValueOperations<String, Object> ops = redisTemplate.boundValueOps(RedisKeys.COLLECTION_STOCK + id);
  376. if (ops.get() == null) {
  377. Boolean success = ops.setIfAbsent(Optional.ofNullable(collectionRepo.getStock(id))
  378. .orElse(0), 7, TimeUnit.DAYS);
  379. log.info("创建redis库存:{}", success);
  380. }
  381. Long stock = ops.increment(number);
  382. rocketMQTemplate.convertAndSend(generalProperties.getUpdateStockTopic(), id);
  383. return stock;
  384. }
  385. public synchronized Integer getStock(Long id) {
  386. BoundValueOperations<String, Object> ops = redisTemplate.boundValueOps(RedisKeys.COLLECTION_STOCK + id);
  387. Integer stock = (Integer) ops.get();
  388. if (stock == null) {
  389. Boolean success = ops.setIfAbsent(Optional.ofNullable(collectionRepo.getStock(id))
  390. .orElse(0), 7, TimeUnit.DAYS);
  391. log.info("创建redis库存:{}", success);
  392. return (Integer) ops.get();
  393. } else {
  394. return stock;
  395. }
  396. }
  397. public synchronized Long decreaseStock(Long id, int number) {
  398. return increaseStock(id, -number);
  399. }
  400. public synchronized Long increaseSale(Long id, int number) {
  401. BoundValueOperations<String, Object> ops = redisTemplate.boundValueOps(RedisKeys.COLLECTION_SALE + id);
  402. if (ops.get() == null) {
  403. Boolean success = ops.setIfAbsent(Optional.ofNullable(collectionRepo.getSale(id))
  404. .orElse(0), 7, TimeUnit.DAYS);
  405. log.info("创建redis销量:{}", success);
  406. }
  407. Long sale = ops.increment(number);
  408. redisTemplate.opsForHash().increment(RedisKeys.UPDATE_SALE, id.toString(), 1);
  409. // rocketMQTemplate.convertAndSend(generalProperties.getUpdateSaleTopic(), id);
  410. return sale;
  411. }
  412. public synchronized Long decreaseSale(Long id, int number) {
  413. return increaseSale(id, -number);
  414. }
  415. @Debounce(key = "#id", delay = 500)
  416. public void syncStock(Long id) {
  417. Integer stock = (Integer) redisTemplate.opsForValue().get(RedisKeys.COLLECTION_STOCK + id);
  418. if (stock != null) {
  419. log.info("同步库存信息{}", id);
  420. collectionRepo.updateStock(id, stock);
  421. cacheService.clearCollection(id);
  422. }
  423. }
  424. // @Debounce(key = "#id", delay = 500)
  425. public void syncSale(Long id) {
  426. Integer sale = (Integer) redisTemplate.opsForValue().get(RedisKeys.COLLECTION_SALE + id);
  427. if (sale != null) {
  428. log.info("同步销量信息{}", id);
  429. collectionRepo.updateSale(id, sale);
  430. cacheService.clearCollection(id);
  431. }
  432. }
  433. @Debounce(key = "#id", delay = 500)
  434. public void syncQuota(Long id) {
  435. Integer quota = (Integer) redisTemplate.opsForValue().get(RedisKeys.COLLECTION_QUOTA + id);
  436. if (quota != null) {
  437. log.info("同步额度信息{}", id);
  438. collectionRepo.updateVipQuota(id, quota);
  439. cacheService.clearCollection(id);
  440. }
  441. }
  442. public synchronized Long decreaseQuota(Long id, int number) {
  443. BoundValueOperations<String, Object> ops = redisTemplate.boundValueOps(RedisKeys.COLLECTION_QUOTA + id);
  444. if (ops.get() == null) {
  445. Boolean success = ops.setIfAbsent(Optional.ofNullable(collectionRepo.getVipQuota(id))
  446. .orElse(0), 7, TimeUnit.DAYS);
  447. log.info("创建redis额度:{}", success);
  448. }
  449. Long stock = ops.increment(-number);
  450. rocketMQTemplate.convertAndSend(generalProperties.getUpdateQuotaTopic(), id);
  451. return stock;
  452. }
  453. @Cacheable(value = "recommendLegacy", key = "#type")
  454. public List<CollectionDTO> recommendLegacy(@RequestParam String type) {
  455. return collectionRepo.recommend(type).stream().map(rc -> {
  456. if (StringUtils.isNotBlank(rc.getRecommend().getPic())) {
  457. rc.getCollection().setPic(Collections.singletonList(new FileObject(null, rc.getRecommend()
  458. .getPic(), null, null)));
  459. }
  460. CollectionDTO collectionDTO = new CollectionDTO();
  461. BeanUtils.copyProperties(rc.getCollection(), collectionDTO);
  462. return collectionDTO;
  463. }).collect(Collectors.toList());
  464. }
  465. public List<PointDTO> savePoint(Long collectionId, LocalDateTime time) {
  466. Collection collection = collectionRepo.findById(collectionId).orElseThrow(new BusinessException("无藏品"));
  467. //库存
  468. // int stock = collection.getStock();
  469. //是否开启白名单
  470. int assignment = collection.getAssignment();
  471. if (assignment <= 0) {
  472. return null;
  473. }
  474. List<User> users = userRepo.findAllByCollectionId(collectionId);
  475. //邀请者
  476. Map<Long, List<User>> userMap = users.stream()
  477. .filter(user -> ObjectUtils.isNotEmpty(user.getCollectionInvitor()))
  478. .collect(Collectors.groupingBy(User::getCollectionInvitor));
  479. AtomicInteger sum = new AtomicInteger();
  480. AtomicInteger sum1 = new AtomicInteger();
  481. List<PointDTO> dtos = new ArrayList<>();
  482. Map<Long, BigDecimal> historyMap = tokenHistoryRepo.userBuy(userMap.keySet())
  483. .stream()
  484. .collect(Collectors.groupingBy(TokenHistory::getToUserId, Collectors.reducing(BigDecimal.ZERO,
  485. TokenHistory::getPrice,
  486. BigDecimal::add)));
  487. DateTimeFormatter dft = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  488. userMap.forEach((key, value) -> {
  489. //邀请达到数量
  490. if (value.size() >= collection.getAssignment()) {
  491. value.sort(Comparator.comparing(User::getCreatedAt));
  492. BigDecimal buy = historyMap.get(key);
  493. //满足条件的时间
  494. User user = value.get(collection.getAssignment() - 1);
  495. //作弊得已屏蔽
  496. if ((ObjectUtils.isEmpty(buy) || buy.compareTo(BigDecimal.valueOf(500)) < 0) && user.getCreatedAt()
  497. .isBefore(time)) {
  498. sum1.getAndIncrement();
  499. System.out.println(key + "," + dft.format(user.getCreatedAt()) + "," + buy);
  500. } else {
  501. //实名数量
  502. long identitySum = value.stream().filter(u -> AuthStatus.SUCCESS.equals(u.getAuthStatus())).count();
  503. dtos.add(new PointDTO(key, user.getCreatedAt(), value.size(), (int) identitySum, buy));
  504. sum.getAndIncrement();
  505. }
  506. }
  507. });
  508. log.info("完成任务人数:{}", sum);
  509. log.info("作弊任务人数:{}", sum1);
  510. LongArrayConverter longArrayConverter = new LongArrayConverter();
  511. List<Long> collect = dtos.stream()
  512. .filter(dto -> time.isBefore(dto.getCreatedAt()))
  513. .map(PointDTO::getId)
  514. .collect(Collectors.toList());
  515. log.info(dft.format(time) + "前完成任务人数:{}", collect.size());
  516. log.info("sql: update user set vip_point = 1 where id in ({})", longArrayConverter
  517. .convertToDatabaseColumn(collect));
  518. List<PointDTO> collect1 = dtos.stream().filter(dto -> time.isAfter(dto.getCreatedAt()))
  519. .collect(Collectors.toList());
  520. log.info(dft.format(time) + "后完成任务人数:{}", collect1.size());
  521. List<Long> collect2 = dtos.stream().filter(dto -> dto.getIdentitySum() > 0).map(PointDTO::getId)
  522. .collect(Collectors.toList());
  523. log.info("邀请实名认证人量:{}", collect2.size());
  524. log.info("sql: update user set vip_point = 1 where id in ({})", longArrayConverter
  525. .convertToDatabaseColumn(collect2));
  526. //只留库存数量
  527. // List<PointDTO> result = dtos.stream()
  528. // .sorted(Comparator.comparing(PointDTO::getCreatedAt))
  529. // .collect(Collectors.toList());
  530. // List<Long> userIds = result.stream().map(PointDTO::getId).collect(Collectors.toList());
  531. // Map<Long, User> resultMap = userRepo.findAllById(userIds)
  532. // .stream()
  533. // .collect(Collectors.toMap(User::getId, user -> user));
  534. //
  535. // List<PointDTO> result2 = new ArrayList<>();
  536. // List<PointDTO> result3 = new ArrayList<>();
  537. // result.forEach(dto -> {
  538. // if (dto.getIdentitySum() > 0) {
  539. // result2.add(dto);
  540. // } else {
  541. // result3.add(dto);
  542. // }
  543. // });
  544. //
  545. // result2.addAll(result3);
  546. //加积分,存记录
  547. // result.forEach(pointDTO -> {
  548. // User user = resultMap.get(pointDTO.getId());
  549. // if (user.getVipPoint() <= 0) {
  550. // user.setVipPoint(1);
  551. // userRepo.save(user);
  552. // pointRecordRepo.save(PointRecord.builder()
  553. // .collectionId(collectionId)
  554. // .userId(pointDTO.getId())
  555. // .type("VIP_POINT")
  556. // .point(1)
  557. // .build());
  558. // }
  559. //
  560. // });
  561. return dtos;
  562. }
  563. @Cacheable(value = "subscribeCollectionList", key = "#now.hashCode()")
  564. public List<SubscribeListDTO> subscribeAll(LocalDate now) {
  565. List<SubscribeListDTO> subscribeListDTOS = new ArrayList<>();
  566. // Map<String, Object> resultMap = new HashMap<>();
  567. //
  568. // resultMap.put("subList", subscribeListDTOS);
  569. // resultMap.put("notSubscribedIds", dtoPage.getContent().stream().filter(dto -> !dto.isSubscribed())
  570. // .map(CollectionDTO::getId).collect(Collectors
  571. // .toList()));
  572. List<SubscribeTime> subscribeTimes = subscribeTimeRepo
  573. .findAllByStartAfterAndDelOrderBySort(LocalDate.now().atStartOfDay(), false);
  574. subscribeTimes.forEach(subscribeTime -> {
  575. PageQuery pageQuery = new PageQuery();
  576. pageQuery.setPage(0);
  577. pageQuery.setSize(10000);
  578. pageQuery.getQuery().put("del", false);
  579. Specification<Collection> specification = JpaUtils.toSpecification(pageQuery, Collection.class);
  580. PageRequest pageRequest = JpaUtils.toPageRequest(pageQuery);
  581. if (pageRequest.getSort().stream().noneMatch(order -> order.getProperty().equals("startTime"))) {
  582. pageRequest = PageRequest.of(pageRequest.getPageNumber(), pageQuery.getSize(),
  583. pageRequest.getSort().and(Sort.by("startTime").ascending()));
  584. }
  585. specification = specification.and((Specification<Collection>) (root, criteriaQuery, criteriaBuilder) -> {
  586. List<Predicate> and = new ArrayList<>();
  587. List<SubscribeStatus> statuses = new ArrayList<>();
  588. statuses.add(SubscribeStatus.NOT_STARTED);
  589. statuses.add(SubscribeStatus.ONGOING);
  590. and.add(root.get("subscribeStatus").in(statuses));
  591. // and.add(criteriaBuilder.equal(root.get("onShelf"), true));
  592. and.add(criteriaBuilder.equal(root.get("source"), CollectionSource.OFFICIAL));
  593. and.add(criteriaBuilder
  594. .between(root.get("startTime"), subscribeTime.getStart(), subscribeTime.getEnd()));
  595. return criteriaBuilder.and(and.toArray(new Predicate[0]));
  596. });
  597. Page<Collection> page = collectionRepo.findAll(specification, pageRequest);
  598. PageWrapper<Collection> dtoPage = new PageWrapper<>(page.getContent(), page.getPageable().getPageNumber(),
  599. page.getPageable().getPageSize(), page.getTotalElements());
  600. Page<CollectionDTO> pageNew = toDTO(dtoPage.toPage());
  601. // LocalDateTime dateTime;
  602. // if (pageNew.getTotalElements() > 0){
  603. // dateTime = pageNew.getContent().get(0).getStartTime();
  604. // subscribeListDTOS
  605. // .add(SubscribeListDTO.builder().dateTime(dateTime)
  606. // .collectionDTOS(pageNew.getContent())
  607. // .build());
  608. // }
  609. if (CollectionUtils.isNotEmpty(pageNew.getContent())){
  610. subscribeListDTOS
  611. .add(SubscribeListDTO.builder().dateTime(subscribeTime.getStart())
  612. .collectionDTOS(pageNew.getContent())
  613. .build());
  614. }
  615. });
  616. // resultMap.put("notSubscribedIds", );
  617. return subscribeListDTOS;
  618. }
  619. }