package com.izouma.nineth.service; import com.izouma.nineth.domain.Asset; import com.izouma.nineth.domain.AssetPost; import com.izouma.nineth.domain.UserAddress; import com.izouma.nineth.dto.PageQuery; import com.izouma.nineth.enums.AssetStatus; import com.izouma.nineth.enums.PostStatus; import com.izouma.nineth.exception.BusinessException; import com.izouma.nineth.repo.AssetPostRepo; import com.izouma.nineth.repo.AssetRepo; import com.izouma.nineth.repo.UserAddressRepo; import com.izouma.nineth.utils.JpaUtils; import lombok.AllArgsConstructor; import org.apache.commons.lang3.ObjectUtils; import org.springframework.data.domain.Page; import org.springframework.stereotype.Service; import java.util.List; import java.util.Optional; @Service @AllArgsConstructor public class AssetPostService { private AssetPostRepo assetPostRepo; private AssetRepo assetRepo; private UserAddressRepo userAddressRepo; public Page all(PageQuery pageQuery) { return assetPostRepo.findAll(JpaUtils.toSpecification(pageQuery, AssetPost.class), JpaUtils.toPageRequest(pageQuery)); } /* 衍生品上链 衍生品有盲盒类型 衍生品邮寄不支付快递费 衍生品暂不可以取消邮寄 */ public AssetPost assetPost(Long userId, Long assetId, Long addressId) { Asset asset = assetRepo.findById(assetId).orElseThrow(new BusinessException("无此衍生品")); if (!userId.equals(asset.getUserId())) { throw new BusinessException("此衍生品不属于你"); } if (asset.isPublicShow()) { if (asset.isConsignment()) { throw new BusinessException("请先取消寄售"); } throw new BusinessException("请先取消公开展示"); } if (AssetStatus.POSTED.equals(asset.getStatus()) || AssetStatus.POSTING.equals(asset.getStatus())) { throw new BusinessException("已邮寄,无需再邮寄"); } if (!AssetStatus.NORMAL.equals(asset.getStatus())) { throw new BusinessException("当前状态不可邮寄"); } AssetPost assetPost = assetPostRepo.findFirstByAssetIdAndDelFalseOrderByIdDesc(assetId); if (ObjectUtils.isNotEmpty(assetPost) && !PostStatus.CANCELLED.equals(assetPost.getStatus())) { throw new BusinessException("已邮寄,无需再邮寄"); } UserAddress userAddress = userAddressRepo.findById(addressId).orElseThrow(new BusinessException("地址信息不存在")); AssetPost build = AssetPost.builder() .assetId(assetId) .name(asset.getName()) .number(asset.getNumber()) .pic(asset.getPic()) .userId(userId) .contactName(userAddress.getName()) .contactPhone(userAddress.getPhone()) .address(userAddress.getProvinceName() + " " + userAddress.getCityName() + " " + userAddress.getDistrictName() + " " + userAddress.getAddress()) .status(PostStatus.DELIVERY) .build(); asset.setStatus(AssetStatus.POSTING); assetRepo.save(asset); return assetPostRepo.save(build); } public void updateStatus(Long id, PostStatus status) { AssetPost assetPost = assetPostRepo.findById(id).orElseThrow(new BusinessException("无邮寄信息")); assetPost.setStatus(status); assetPostRepo.save(assetPost); } public void receiveAsset(Long id, String courier, String courierId) { AssetPost assetPost = assetPostRepo.findById(id).orElseThrow(new BusinessException("无邮寄信息")); assetPost.setStatus(PostStatus.RECEIVE); assetPost.setCourier(courier); assetPost.setCourierId(courierId); Asset asset = assetRepo.findById(assetPost.getAssetId()).orElseThrow(new BusinessException("无衍生品")); asset.setStatus(AssetStatus.POSTED); assetRepo.save(asset); assetPostRepo.save(assetPost); } public AssetPost cancelPost(Long id) { AssetPost assetPost = assetPostRepo.findById(id).orElseThrow(new BusinessException("无邮寄信息")); if (PostStatus.CANCELLED.equals(assetPost.getStatus())) { return assetPost; } if (!PostStatus.DELIVERY.equals(assetPost.getStatus())) { throw new BusinessException("已邮寄无法取消"); } Asset asset = assetRepo.findById(assetPost.getAssetId()).orElseThrow(new BusinessException("无衍生品")); if (AssetStatus.POSTING.equals(asset.getStatus())) { throw new BusinessException("当前状态无法取消"); } assetPost.setStatus(PostStatus.CANCELLED); asset.setStatus(AssetStatus.NORMAL); assetRepo.save(asset); return assetPostRepo.save(assetPost); } }