CollectionService.java 39 KB

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