CollectionService.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.BlindBoxItemRepo;
  10. import com.izouma.nineth.repo.CollectionRepo;
  11. import com.izouma.nineth.repo.LikeRepo;
  12. import com.izouma.nineth.utils.JpaUtils;
  13. import com.izouma.nineth.utils.SecurityUtils;
  14. import lombok.AllArgsConstructor;
  15. import org.apache.commons.collections.MapUtils;
  16. import org.springframework.beans.BeanUtils;
  17. import org.springframework.data.domain.Page;
  18. import org.springframework.data.domain.PageImpl;
  19. import org.springframework.data.jpa.domain.Specification;
  20. import org.springframework.stereotype.Service;
  21. import javax.persistence.criteria.CriteriaBuilder;
  22. import javax.persistence.criteria.CriteriaQuery;
  23. import javax.persistence.criteria.Predicate;
  24. import javax.persistence.criteria.Root;
  25. import javax.transaction.Transactional;
  26. import java.util.ArrayList;
  27. import java.util.Arrays;
  28. import java.util.List;
  29. import java.util.stream.Collectors;
  30. @Service
  31. @AllArgsConstructor
  32. public class CollectionService {
  33. private CollectionRepo collectionRepo;
  34. private LikeRepo likeRepo;
  35. private BlindBoxItemRepo blindBoxItemRepo;
  36. public Page<Collection> all(PageQuery pageQuery) {
  37. pageQuery.getQuery().put("del", false);
  38. Specification<Collection> specification = JpaUtils.toSpecification(pageQuery, Collection.class);
  39. specification = specification.and((Specification<Collection>) (root, criteriaQuery, criteriaBuilder) -> {
  40. List<Predicate> and = new ArrayList<>();
  41. if (!MapUtils.getString(pageQuery.getQuery(), "type", "").equals("BLIND_BOX")) {
  42. and.add(criteriaBuilder.notEqual(root.get("type"), CollectionType.BLIND_BOX));
  43. }
  44. return criteriaBuilder.and(and.toArray(new Predicate[0]));
  45. });
  46. return collectionRepo.findAll(specification, JpaUtils.toPageRequest(pageQuery));
  47. }
  48. public Collection create(Collection record) {
  49. User user = SecurityUtils.getAuthenticatedUser();
  50. record.setMinter(user.getNickname());
  51. record.setMinterId(user.getId());
  52. record.setMinterAvatar(user.getAvatar());
  53. record.setStock(record.getTotal());
  54. record.setSale(0);
  55. return collectionRepo.save(record);
  56. }
  57. public CollectionDTO toDTO(Collection collection) {
  58. return toDTO(collection, true);
  59. }
  60. public CollectionDTO toDTO(Collection collection, boolean join) {
  61. CollectionDTO collectionDTO = new CollectionDTO();
  62. BeanUtils.copyProperties(collection, collectionDTO);
  63. if (join) {
  64. if (SecurityUtils.getAuthenticatedUser() != null) {
  65. List<Like> list = likeRepo.findByUserIdAndCollectionId(SecurityUtils.getAuthenticatedUser().getId(),
  66. collection.getId());
  67. collectionDTO.setLiked(!list.isEmpty());
  68. }
  69. }
  70. return collectionDTO;
  71. }
  72. public List<CollectionDTO> toDTO(List<Collection> collections) {
  73. List<Like> likes = new ArrayList<>();
  74. if (SecurityUtils.getAuthenticatedUser() != null) {
  75. likes.addAll(likeRepo.findByUserId(SecurityUtils.getAuthenticatedUser().getId()));
  76. }
  77. return collections.stream().parallel().map(collection -> {
  78. CollectionDTO dto = toDTO(collection, false);
  79. if (!likes.isEmpty()) {
  80. dto.setLiked(likes.stream().anyMatch(l -> l.getCollectionId().equals(collection.getId())));
  81. }
  82. return dto;
  83. }).collect(Collectors.toList());
  84. }
  85. public Page<CollectionDTO> toDTO(Page<Collection> collections) {
  86. List<CollectionDTO> userDTOS = toDTO(collections.getContent());
  87. return new PageImpl<>(userDTOS, collections.getPageable(), collections.getTotalElements());
  88. }
  89. @Transactional
  90. public Collection createBlindBox(CreateBlindBox createBlindBox) {
  91. Collection blindBox = createBlindBox.getBlindBox();
  92. List<Collection> list = new ArrayList<>();
  93. createBlindBox.getItems().stream().parallel().forEach(item -> {
  94. Collection collection = collectionRepo.findById(item.getId()).orElseThrow(new BusinessException("所选藏品不存在"));
  95. list.add(collection);
  96. if (item.getTotal() > collection.getStock()) {
  97. throw new BusinessException("所选藏品库存不足");
  98. }
  99. });
  100. User user = SecurityUtils.getAuthenticatedUser();
  101. blindBox.setMinter(user.getNickname());
  102. blindBox.setMinterId(user.getId());
  103. blindBox.setMinterAvatar(user.getAvatar());
  104. collectionRepo.save(blindBox);
  105. createBlindBox.getItems().stream().parallel().forEach(item -> {
  106. Collection collection = list.stream().filter(i -> i.getId().equals(item.getId())).findAny().get();
  107. collection.setStock(collection.getStock() - item.getTotal());
  108. collectionRepo.save(collection);
  109. BlindBoxItem blindBoxItem = new BlindBoxItem();
  110. BeanUtils.copyProperties(collection, blindBoxItem);
  111. blindBoxItem.setCollectionId(collection.getId());
  112. blindBoxItem.setId(null);
  113. blindBoxItem.setSale(0);
  114. blindBoxItem.setTotal(item.getTotal());
  115. blindBoxItem.setStock(item.getTotal());
  116. blindBoxItem.setRare(item.isRare());
  117. blindBoxItem.setBlindBoxId(blindBox.getId());
  118. blindBoxItemRepo.save(blindBoxItem);
  119. });
  120. return blindBox;
  121. }
  122. }