CollectionService.java 33 KB

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