|
|
@@ -7,9 +7,7 @@ import com.izouma.nineth.dto.PageQuery;
|
|
|
import com.izouma.nineth.dto.UserDTO;
|
|
|
import com.izouma.nineth.enums.CollectionType;
|
|
|
import com.izouma.nineth.exception.BusinessException;
|
|
|
-import com.izouma.nineth.repo.BlindBoxItemRepo;
|
|
|
-import com.izouma.nineth.repo.CollectionRepo;
|
|
|
-import com.izouma.nineth.repo.LikeRepo;
|
|
|
+import com.izouma.nineth.repo.*;
|
|
|
import com.izouma.nineth.utils.JpaUtils;
|
|
|
import com.izouma.nineth.utils.SecurityUtils;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
@@ -25,6 +23,7 @@ import javax.persistence.criteria.CriteriaQuery;
|
|
|
import javax.persistence.criteria.Predicate;
|
|
|
import javax.persistence.criteria.Root;
|
|
|
import javax.transaction.Transactional;
|
|
|
+import java.time.LocalDateTime;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
@@ -37,6 +36,8 @@ public class CollectionService {
|
|
|
private CollectionRepo collectionRepo;
|
|
|
private LikeRepo likeRepo;
|
|
|
private BlindBoxItemRepo blindBoxItemRepo;
|
|
|
+ private AppointmentRepo appointmentRepo;
|
|
|
+ private UserRepo userRepo;
|
|
|
|
|
|
public Page<Collection> all(PageQuery pageQuery) {
|
|
|
pageQuery.getQuery().put("del", false);
|
|
|
@@ -53,10 +54,13 @@ public class CollectionService {
|
|
|
}
|
|
|
|
|
|
public Collection create(Collection record) {
|
|
|
- User user = SecurityUtils.getAuthenticatedUser();
|
|
|
- record.setMinter(user.getNickname());
|
|
|
- record.setMinterId(user.getId());
|
|
|
- record.setMinterAvatar(user.getAvatar());
|
|
|
+ User minter = userRepo.findById(record.getMinterId()).orElse(SecurityUtils.getAuthenticatedUser());
|
|
|
+ record.setMinter(minter.getNickname());
|
|
|
+ record.setMinterId(minter.getId());
|
|
|
+ record.setMinterAvatar(minter.getAvatar());
|
|
|
+ record.setOwner(minter.getNickname());
|
|
|
+ record.setOwnerId(minter.getId());
|
|
|
+ record.setOwnerAvatar(minter.getAvatar());
|
|
|
record.setStock(record.getTotal());
|
|
|
record.setSale(0);
|
|
|
return collectionRepo.save(record);
|
|
|
@@ -74,6 +78,9 @@ public class CollectionService {
|
|
|
List<Like> list = likeRepo.findByUserIdAndCollectionId(SecurityUtils.getAuthenticatedUser().getId(),
|
|
|
collection.getId());
|
|
|
collectionDTO.setLiked(!list.isEmpty());
|
|
|
+ if (collection.getType() == CollectionType.BLIND_BOX) {
|
|
|
+ collectionDTO.setAppointment(appointmentRepo.findFirstByBlindBoxId(collection.getId()).isPresent());
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
return collectionDTO;
|
|
|
@@ -81,14 +88,19 @@ public class CollectionService {
|
|
|
|
|
|
public List<CollectionDTO> toDTO(List<Collection> collections) {
|
|
|
List<Like> likes = new ArrayList<>();
|
|
|
+ List<Appointment> appointments = new ArrayList<>();
|
|
|
if (SecurityUtils.getAuthenticatedUser() != null) {
|
|
|
likes.addAll(likeRepo.findByUserId(SecurityUtils.getAuthenticatedUser().getId()));
|
|
|
+ appointments.addAll(appointmentRepo.findByUserId(SecurityUtils.getAuthenticatedUser().getId()));
|
|
|
}
|
|
|
return collections.stream().parallel().map(collection -> {
|
|
|
CollectionDTO dto = toDTO(collection, false);
|
|
|
if (!likes.isEmpty()) {
|
|
|
dto.setLiked(likes.stream().anyMatch(l -> l.getCollectionId().equals(collection.getId())));
|
|
|
}
|
|
|
+ if (!appointments.isEmpty()) {
|
|
|
+ dto.setAppointment(appointments.stream().anyMatch(a -> a.getBlindBoxId().equals(collection.getId())));
|
|
|
+ }
|
|
|
return dto;
|
|
|
}).collect(Collectors.toList());
|
|
|
}
|
|
|
@@ -113,10 +125,13 @@ public class CollectionService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- User user = SecurityUtils.getAuthenticatedUser();
|
|
|
+ User user = userRepo.findById(blindBox.getMinterId()).orElse(SecurityUtils.getAuthenticatedUser());
|
|
|
blindBox.setMinter(user.getNickname());
|
|
|
blindBox.setMinterId(user.getId());
|
|
|
blindBox.setMinterAvatar(user.getAvatar());
|
|
|
+ blindBox.setOwner(user.getNickname());
|
|
|
+ blindBox.setOwnerId(user.getId());
|
|
|
+ blindBox.setOwnerAvatar(user.getAvatar());
|
|
|
blindBox.setStock(blindBox.getTotal());
|
|
|
blindBox.setSale(0);
|
|
|
collectionRepo.save(blindBox);
|
|
|
@@ -139,4 +154,20 @@ public class CollectionService {
|
|
|
|
|
|
return blindBox;
|
|
|
}
|
|
|
+
|
|
|
+ public void appointment(Long id, Long userId) {
|
|
|
+ Collection collection = collectionRepo.findById(id).orElseThrow(new BusinessException("无记录"));
|
|
|
+ if (collection.getType() != CollectionType.BLIND_BOX) {
|
|
|
+ throw new BusinessException("非盲盒,无需预约");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (collection.getStartTime().isBefore(LocalDateTime.now())) {
|
|
|
+ throw new BusinessException("盲盒已开售,无需预约");
|
|
|
+ }
|
|
|
+
|
|
|
+ appointmentRepo.save(Appointment.builder()
|
|
|
+ .userId(userId)
|
|
|
+ .blindBoxId(id)
|
|
|
+ .build());
|
|
|
+ }
|
|
|
}
|