CollectionService.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package com.izouma.nineth.service;
  2. import com.izouma.nineth.domain.*;
  3. import com.izouma.nineth.dto.CollectionDTO;
  4. import com.izouma.nineth.dto.CreateBlindBox;
  5. import com.izouma.nineth.dto.PageQuery;
  6. import com.izouma.nineth.dto.UserDTO;
  7. import com.izouma.nineth.enums.CollectionType;
  8. import com.izouma.nineth.exception.BusinessException;
  9. import com.izouma.nineth.repo.*;
  10. import com.izouma.nineth.utils.JpaUtils;
  11. import com.izouma.nineth.utils.SecurityUtils;
  12. import lombok.AllArgsConstructor;
  13. import org.apache.commons.collections.MapUtils;
  14. import org.springframework.beans.BeanUtils;
  15. import org.springframework.data.domain.Page;
  16. import org.springframework.data.domain.PageImpl;
  17. import org.springframework.data.domain.PageRequest;
  18. import org.springframework.data.domain.Sort;
  19. import org.springframework.data.jpa.domain.Specification;
  20. import org.springframework.scheduling.annotation.Scheduled;
  21. import org.springframework.stereotype.Service;
  22. import javax.persistence.criteria.CriteriaBuilder;
  23. import javax.persistence.criteria.CriteriaQuery;
  24. import javax.persistence.criteria.Predicate;
  25. import javax.persistence.criteria.Root;
  26. import javax.transaction.Transactional;
  27. import java.time.LocalDateTime;
  28. import java.util.ArrayList;
  29. import java.util.Arrays;
  30. import java.util.List;
  31. import java.util.stream.Collectors;
  32. @Service
  33. @AllArgsConstructor
  34. public class CollectionService {
  35. private CollectionRepo collectionRepo;
  36. private LikeRepo likeRepo;
  37. private BlindBoxItemRepo blindBoxItemRepo;
  38. private AppointmentRepo appointmentRepo;
  39. private UserRepo userRepo;
  40. public Page<Collection> all(PageQuery pageQuery) {
  41. pageQuery.getQuery().put("del", false);
  42. Specification<Collection> specification = JpaUtils.toSpecification(pageQuery, Collection.class);
  43. PageRequest pageRequest = JpaUtils.toPageRequest(pageQuery);
  44. if (pageRequest.getSort().stream().noneMatch(order -> order.getProperty().equals("createdAt"))) {
  45. pageRequest = PageRequest.of(pageRequest.getPageNumber(), pageQuery.getSize(),
  46. pageRequest.getSort().and(Sort.by("createdAt").descending()));
  47. }
  48. specification = specification.and((Specification<Collection>) (root, criteriaQuery, criteriaBuilder) -> {
  49. List<Predicate> and = new ArrayList<>();
  50. if (!MapUtils.getString(pageQuery.getQuery(), "type", "").equals("BLIND_BOX")) {
  51. and.add(criteriaBuilder.notEqual(root.get("type"), CollectionType.BLIND_BOX));
  52. }
  53. return criteriaBuilder.and(and.toArray(new Predicate[0]));
  54. });
  55. return collectionRepo.findAll(specification, pageRequest);
  56. }
  57. public Collection create(Collection record) {
  58. User minter = userRepo.findById(record.getMinterId()).orElse(SecurityUtils.getAuthenticatedUser());
  59. record.setMinter(minter.getNickname());
  60. record.setMinterId(minter.getId());
  61. record.setMinterAvatar(minter.getAvatar());
  62. record.setOwner(minter.getNickname());
  63. record.setOwnerId(minter.getId());
  64. record.setOwnerAvatar(minter.getAvatar());
  65. record.setStock(record.getTotal());
  66. record.setSale(0);
  67. if (record.isScheduleSale()) {
  68. if (record.getStartTime() == null) {
  69. throw new BusinessException("请填写定时发布时间");
  70. }
  71. record.setOnShelf(record.getStartTime().isBefore(LocalDateTime.now()));
  72. }
  73. return collectionRepo.save(record);
  74. }
  75. public CollectionDTO toDTO(Collection collection) {
  76. return toDTO(collection, true);
  77. }
  78. public CollectionDTO toDTO(Collection collection, boolean join) {
  79. CollectionDTO collectionDTO = new CollectionDTO();
  80. BeanUtils.copyProperties(collection, collectionDTO);
  81. if (join) {
  82. if (SecurityUtils.getAuthenticatedUser() != null) {
  83. List<Like> list = likeRepo.findByUserIdAndCollectionId(SecurityUtils.getAuthenticatedUser().getId(),
  84. collection.getId());
  85. collectionDTO.setLiked(!list.isEmpty());
  86. if (collection.getType() == CollectionType.BLIND_BOX) {
  87. collectionDTO.setAppointment(appointmentRepo.findFirstByBlindBoxId(collection.getId()).isPresent());
  88. }
  89. }
  90. }
  91. return collectionDTO;
  92. }
  93. public List<CollectionDTO> toDTO(List<Collection> collections) {
  94. List<Like> likes = new ArrayList<>();
  95. List<Appointment> appointments = new ArrayList<>();
  96. if (SecurityUtils.getAuthenticatedUser() != null) {
  97. likes.addAll(likeRepo.findByUserId(SecurityUtils.getAuthenticatedUser().getId()));
  98. appointments.addAll(appointmentRepo.findByUserId(SecurityUtils.getAuthenticatedUser().getId()));
  99. }
  100. return collections.stream().parallel().map(collection -> {
  101. CollectionDTO dto = toDTO(collection, false);
  102. if (!likes.isEmpty()) {
  103. dto.setLiked(likes.stream().anyMatch(l -> l.getCollectionId().equals(collection.getId())));
  104. }
  105. if (!appointments.isEmpty()) {
  106. dto.setAppointment(appointments.stream().anyMatch(a -> a.getBlindBoxId().equals(collection.getId())));
  107. }
  108. return dto;
  109. }).collect(Collectors.toList());
  110. }
  111. public Page<CollectionDTO> toDTO(Page<Collection> collections) {
  112. List<CollectionDTO> userDTOS = toDTO(collections.getContent());
  113. return new PageImpl<>(userDTOS, collections.getPageable(), collections.getTotalElements());
  114. }
  115. @Transactional
  116. public Collection createBlindBox(CreateBlindBox createBlindBox) {
  117. Collection blindBox = createBlindBox.getBlindBox();
  118. List<Collection> list =
  119. collectionRepo.findAllById(createBlindBox.getItems().stream().map(BlindBoxItem::getCollectionId)
  120. .collect(Collectors.toSet()));
  121. for (BlindBoxItem item : createBlindBox.getItems()) {
  122. Collection collection = list.stream().filter(i -> i.getId().equals(item.getCollectionId())).findAny()
  123. .orElseThrow(new BusinessException("所选藏品不存在"));
  124. if (item.getTotal() > collection.getStock()) {
  125. throw new BusinessException("所选藏品库存不足:" + collection.getName());
  126. }
  127. }
  128. User user = userRepo.findById(blindBox.getMinterId()).orElse(SecurityUtils.getAuthenticatedUser());
  129. blindBox.setMinter(user.getNickname());
  130. blindBox.setMinterId(user.getId());
  131. blindBox.setMinterAvatar(user.getAvatar());
  132. blindBox.setOwner(user.getNickname());
  133. blindBox.setOwnerId(user.getId());
  134. blindBox.setOwnerAvatar(user.getAvatar());
  135. blindBox.setStock(blindBox.getTotal());
  136. blindBox.setSale(0);
  137. collectionRepo.save(blindBox);
  138. for (BlindBoxItem item : createBlindBox.getItems()) {
  139. Collection collection = list.stream().filter(i -> i.getId().equals(item.getCollectionId())).findAny()
  140. .orElseThrow(new BusinessException("所选藏品不存在"));
  141. collection.setStock(collection.getStock() - item.getTotal());
  142. collectionRepo.save(collection);
  143. BlindBoxItem blindBoxItem = new BlindBoxItem();
  144. BeanUtils.copyProperties(collection, blindBoxItem);
  145. blindBoxItem.setId(null);
  146. blindBoxItem.setCollectionId(item.getCollectionId());
  147. blindBoxItem.setSale(0);
  148. blindBoxItem.setTotal(item.getTotal());
  149. blindBoxItem.setStock(item.getTotal());
  150. blindBoxItem.setRare(item.isRare());
  151. blindBoxItem.setBlindBoxId(blindBox.getId());
  152. blindBoxItemRepo.save(blindBoxItem);
  153. }
  154. return blindBox;
  155. }
  156. public void appointment(Long id, Long userId) {
  157. Collection collection = collectionRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  158. if (collection.getType() != CollectionType.BLIND_BOX) {
  159. throw new BusinessException("非盲盒,无需预约");
  160. }
  161. if (collection.getStartTime().isBefore(LocalDateTime.now())) {
  162. throw new BusinessException("盲盒已开售,无需预约");
  163. }
  164. appointmentRepo.save(Appointment.builder()
  165. .userId(userId)
  166. .blindBoxId(id)
  167. .build());
  168. }
  169. @Scheduled(fixedRate = 60000)
  170. public void scheduleOnShelf() {
  171. List<Collection> collections = collectionRepo.findByScheduleSaleTrueAndOnShelfFalseAndStartTimeBeforeAndDelFalse(LocalDateTime.now());
  172. for (Collection collection : collections) {
  173. collection.setOnShelf(true);
  174. }
  175. collectionRepo.saveAll(collections);
  176. }
  177. }