AssetPostService.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package com.izouma.nineth.service;
  2. import com.izouma.nineth.domain.Asset;
  3. import com.izouma.nineth.domain.AssetPost;
  4. import com.izouma.nineth.domain.UserAddress;
  5. import com.izouma.nineth.dto.PageQuery;
  6. import com.izouma.nineth.enums.AssetStatus;
  7. import com.izouma.nineth.enums.PostStatus;
  8. import com.izouma.nineth.exception.BusinessException;
  9. import com.izouma.nineth.repo.AssetPostRepo;
  10. import com.izouma.nineth.repo.AssetRepo;
  11. import com.izouma.nineth.repo.UserAddressRepo;
  12. import com.izouma.nineth.utils.JpaUtils;
  13. import lombok.AllArgsConstructor;
  14. import org.apache.commons.lang3.ObjectUtils;
  15. import org.springframework.data.domain.Page;
  16. import org.springframework.stereotype.Service;
  17. import java.util.List;
  18. import java.util.Optional;
  19. @Service
  20. @AllArgsConstructor
  21. public class AssetPostService {
  22. private AssetPostRepo assetPostRepo;
  23. private AssetRepo assetRepo;
  24. private UserAddressRepo userAddressRepo;
  25. public Page<AssetPost> all(PageQuery pageQuery) {
  26. return assetPostRepo.findAll(JpaUtils.toSpecification(pageQuery, AssetPost.class), JpaUtils.toPageRequest(pageQuery));
  27. }
  28. /*
  29. 衍生品上链
  30. 衍生品有盲盒类型
  31. 衍生品邮寄不支付快递费
  32. 衍生品暂不可以取消邮寄
  33. */
  34. public AssetPost assetPost(Long userId, Long assetId, Long addressId) {
  35. Asset asset = assetRepo.findById(assetId).orElseThrow(new BusinessException("无此衍生品"));
  36. if (!userId.equals(asset.getUserId())) {
  37. throw new BusinessException("此衍生品不属于你");
  38. }
  39. if (asset.isPublicShow()) {
  40. if (asset.isConsignment()) {
  41. throw new BusinessException("请先取消寄售");
  42. }
  43. throw new BusinessException("请先取消公开展示");
  44. }
  45. if (AssetStatus.POSTED.equals(asset.getStatus()) || AssetStatus.POSTING.equals(asset.getStatus())) {
  46. throw new BusinessException("已邮寄,无需再邮寄");
  47. }
  48. if (!AssetStatus.NORMAL.equals(asset.getStatus())) {
  49. throw new BusinessException("当前状态不可邮寄");
  50. }
  51. AssetPost assetPost = assetPostRepo.findFirstByAssetIdAndDelFalseOrderByIdDesc(assetId);
  52. if (ObjectUtils.isNotEmpty(assetPost) && !PostStatus.CANCELLED.equals(assetPost.getStatus())) {
  53. throw new BusinessException("已邮寄,无需再邮寄");
  54. }
  55. UserAddress userAddress = userAddressRepo.findById(addressId).orElseThrow(new BusinessException("地址信息不存在"));
  56. AssetPost build = AssetPost.builder()
  57. .assetId(assetId)
  58. .name(asset.getName())
  59. .number(asset.getNumber())
  60. .pic(asset.getPic())
  61. .userId(userId)
  62. .contactName(userAddress.getName())
  63. .contactPhone(userAddress.getPhone())
  64. .address(userAddress.getProvinceName() + " " + userAddress.getCityName() + " " +
  65. userAddress.getDistrictName() + " " + userAddress.getAddress())
  66. .status(PostStatus.DELIVERY)
  67. .build();
  68. asset.setStatus(AssetStatus.POSTING);
  69. assetRepo.save(asset);
  70. return assetPostRepo.save(build);
  71. }
  72. public void updateStatus(Long id, PostStatus status) {
  73. AssetPost assetPost = assetPostRepo.findById(id).orElseThrow(new BusinessException("无邮寄信息"));
  74. assetPost.setStatus(status);
  75. assetPostRepo.save(assetPost);
  76. }
  77. public void receiveAsset(Long id, String courier, String courierId) {
  78. AssetPost assetPost = assetPostRepo.findById(id).orElseThrow(new BusinessException("无邮寄信息"));
  79. assetPost.setStatus(PostStatus.RECEIVE);
  80. assetPost.setCourier(courier);
  81. assetPost.setCourierId(courierId);
  82. Asset asset = assetRepo.findById(assetPost.getAssetId()).orElseThrow(new BusinessException("无衍生品"));
  83. asset.setStatus(AssetStatus.POSTED);
  84. assetRepo.save(asset);
  85. assetPostRepo.save(assetPost);
  86. }
  87. }