CollectionService.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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.jpa.domain.Specification;
  18. import org.springframework.stereotype.Service;
  19. import javax.persistence.criteria.CriteriaBuilder;
  20. import javax.persistence.criteria.CriteriaQuery;
  21. import javax.persistence.criteria.Predicate;
  22. import javax.persistence.criteria.Root;
  23. import javax.transaction.Transactional;
  24. import java.time.LocalDateTime;
  25. import java.util.ArrayList;
  26. import java.util.Arrays;
  27. import java.util.List;
  28. import java.util.stream.Collectors;
  29. @Service
  30. @AllArgsConstructor
  31. public class CollectionService {
  32. private CollectionRepo collectionRepo;
  33. private LikeRepo likeRepo;
  34. private BlindBoxItemRepo blindBoxItemRepo;
  35. private AppointmentRepo appointmentRepo;
  36. private UserRepo userRepo;
  37. public Page<Collection> all(PageQuery pageQuery) {
  38. pageQuery.getQuery().put("del", false);
  39. Specification<Collection> specification = JpaUtils.toSpecification(pageQuery, Collection.class);
  40. specification = specification.and((Specification<Collection>) (root, criteriaQuery, criteriaBuilder) -> {
  41. List<Predicate> and = new ArrayList<>();
  42. if (!MapUtils.getString(pageQuery.getQuery(), "type", "").equals("BLIND_BOX")) {
  43. and.add(criteriaBuilder.notEqual(root.get("type"), CollectionType.BLIND_BOX));
  44. }
  45. return criteriaBuilder.and(and.toArray(new Predicate[0]));
  46. });
  47. return collectionRepo.findAll(specification, JpaUtils.toPageRequest(pageQuery));
  48. }
  49. public Collection create(Collection record) {
  50. User minter = userRepo.findById(record.getMinterId()).orElse(SecurityUtils.getAuthenticatedUser());
  51. record.setMinter(minter.getNickname());
  52. record.setMinterId(minter.getId());
  53. record.setMinterAvatar(minter.getAvatar());
  54. record.setOwner(minter.getNickname());
  55. record.setOwnerId(minter.getId());
  56. record.setOwnerAvatar(minter.getAvatar());
  57. record.setStock(record.getTotal());
  58. record.setSale(0);
  59. return collectionRepo.save(record);
  60. }
  61. public CollectionDTO toDTO(Collection collection) {
  62. return toDTO(collection, true);
  63. }
  64. public CollectionDTO toDTO(Collection collection, boolean join) {
  65. CollectionDTO collectionDTO = new CollectionDTO();
  66. BeanUtils.copyProperties(collection, collectionDTO);
  67. if (join) {
  68. if (SecurityUtils.getAuthenticatedUser() != null) {
  69. List<Like> list = likeRepo.findByUserIdAndCollectionId(SecurityUtils.getAuthenticatedUser().getId(),
  70. collection.getId());
  71. collectionDTO.setLiked(!list.isEmpty());
  72. if (collection.getType() == CollectionType.BLIND_BOX) {
  73. collectionDTO.setAppointment(appointmentRepo.findFirstByBlindBoxId(collection.getId()).isPresent());
  74. }
  75. }
  76. }
  77. return collectionDTO;
  78. }
  79. public List<CollectionDTO> toDTO(List<Collection> collections) {
  80. List<Like> likes = new ArrayList<>();
  81. List<Appointment> appointments = new ArrayList<>();
  82. if (SecurityUtils.getAuthenticatedUser() != null) {
  83. likes.addAll(likeRepo.findByUserId(SecurityUtils.getAuthenticatedUser().getId()));
  84. appointments.addAll(appointmentRepo.findByUserId(SecurityUtils.getAuthenticatedUser().getId()));
  85. }
  86. return collections.stream().parallel().map(collection -> {
  87. CollectionDTO dto = toDTO(collection, false);
  88. if (!likes.isEmpty()) {
  89. dto.setLiked(likes.stream().anyMatch(l -> l.getCollectionId().equals(collection.getId())));
  90. }
  91. if (!appointments.isEmpty()) {
  92. dto.setAppointment(appointments.stream().anyMatch(a -> a.getBlindBoxId().equals(collection.getId())));
  93. }
  94. return dto;
  95. }).collect(Collectors.toList());
  96. }
  97. public Page<CollectionDTO> toDTO(Page<Collection> collections) {
  98. List<CollectionDTO> userDTOS = toDTO(collections.getContent());
  99. return new PageImpl<>(userDTOS, collections.getPageable(), collections.getTotalElements());
  100. }
  101. @Transactional
  102. public Collection createBlindBox(CreateBlindBox createBlindBox) {
  103. Collection blindBox = createBlindBox.getBlindBox();
  104. List<Collection> list =
  105. collectionRepo.findAllById(createBlindBox.getItems().stream().map(BlindBoxItem::getCollectionId)
  106. .collect(Collectors.toSet()));
  107. for (BlindBoxItem item : createBlindBox.getItems()) {
  108. Collection collection = list.stream().filter(i -> i.getId().equals(item.getCollectionId())).findAny()
  109. .orElseThrow(new BusinessException("所选藏品不存在"));
  110. if (item.getTotal() > collection.getStock()) {
  111. throw new BusinessException("所选藏品库存不足:" + collection.getName());
  112. }
  113. }
  114. User user = userRepo.findById(blindBox.getMinterId()).orElse(SecurityUtils.getAuthenticatedUser());
  115. blindBox.setMinter(user.getNickname());
  116. blindBox.setMinterId(user.getId());
  117. blindBox.setMinterAvatar(user.getAvatar());
  118. blindBox.setOwner(user.getNickname());
  119. blindBox.setOwnerId(user.getId());
  120. blindBox.setOwnerAvatar(user.getAvatar());
  121. blindBox.setStock(blindBox.getTotal());
  122. blindBox.setSale(0);
  123. collectionRepo.save(blindBox);
  124. for (BlindBoxItem item : createBlindBox.getItems()) {
  125. Collection collection = list.stream().filter(i -> i.getId().equals(item.getCollectionId())).findAny()
  126. .orElseThrow(new BusinessException("所选藏品不存在"));
  127. collection.setStock(collection.getStock() - item.getTotal());
  128. collectionRepo.save(collection);
  129. BlindBoxItem blindBoxItem = new BlindBoxItem();
  130. BeanUtils.copyProperties(collection, blindBoxItem);
  131. blindBoxItem.setId(null);
  132. blindBoxItem.setCollectionId(item.getCollectionId());
  133. blindBoxItem.setSale(0);
  134. blindBoxItem.setTotal(item.getTotal());
  135. blindBoxItem.setStock(item.getTotal());
  136. blindBoxItem.setRare(item.isRare());
  137. blindBoxItem.setBlindBoxId(blindBox.getId());
  138. blindBoxItemRepo.save(blindBoxItem);
  139. }
  140. return blindBox;
  141. }
  142. public void appointment(Long id, Long userId) {
  143. Collection collection = collectionRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  144. if (collection.getType() != CollectionType.BLIND_BOX) {
  145. throw new BusinessException("非盲盒,无需预约");
  146. }
  147. if (collection.getStartTime().isBefore(LocalDateTime.now())) {
  148. throw new BusinessException("盲盒已开售,无需预约");
  149. }
  150. appointmentRepo.save(Appointment.builder()
  151. .userId(userId)
  152. .blindBoxId(id)
  153. .build());
  154. }
  155. }