|
|
@@ -0,0 +1,176 @@
|
|
|
+package com.izouma.nineth.service;
|
|
|
+
|
|
|
+import com.izouma.nineth.domain.Asset;
|
|
|
+import com.izouma.nineth.domain.AssetSuperimposition;
|
|
|
+import com.izouma.nineth.domain.Collection;
|
|
|
+import com.izouma.nineth.dto.PageQuery;
|
|
|
+import com.izouma.nineth.enums.AssetOperationType;
|
|
|
+import com.izouma.nineth.exception.BusinessException;
|
|
|
+import com.izouma.nineth.repo.AssetSuperimpositionRepo;
|
|
|
+import com.izouma.nineth.repo.CollectionRepo;
|
|
|
+import com.izouma.nineth.utils.JpaUtils;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+@Service
|
|
|
+@AllArgsConstructor
|
|
|
+@Slf4j
|
|
|
+public class AssetSuperimpositionService {
|
|
|
+
|
|
|
+ private final int NUM = 1;
|
|
|
+
|
|
|
+ private AssetSuperimpositionRepo assetSuperimpositionRepo;
|
|
|
+
|
|
|
+ private CollectionRepo collectionRepo;
|
|
|
+
|
|
|
+ public Page<AssetSuperimposition> all(PageQuery pageQuery) {
|
|
|
+ return assetSuperimpositionRepo.findAll(JpaUtils.toSpecification(pageQuery, AssetSuperimposition.class), JpaUtils.toPageRequest(pageQuery));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 调整各操作藏品叠加的数量信息
|
|
|
+ *
|
|
|
+ * @param asset 资产信息
|
|
|
+ * @param type 操作类型
|
|
|
+ * @param relatedUserId 相关用户id(买入人,被赠送人)
|
|
|
+ */
|
|
|
+ public void adjustNum(Asset asset, AssetOperationType type, Long relatedUserId) {
|
|
|
+ Collection collection = null;
|
|
|
+ if (Objects.nonNull(asset.getCollectionId())) {
|
|
|
+ collection = collectionRepo.findById(asset.getCollectionId()).orElseThrow(new BusinessException("没有该藏品信息"));
|
|
|
+ }
|
|
|
+ if (Objects.isNull(type)) {
|
|
|
+ throw new BusinessException("操作类型不能为空");
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(asset.getPrefixName())) {
|
|
|
+ throw new BusinessException("prefixName不能为空");
|
|
|
+ }
|
|
|
+ if (Objects.isNull(asset.getUserId())) {
|
|
|
+ throw new BusinessException("用户id不能为空");
|
|
|
+ }
|
|
|
+ AssetSuperimposition assetSuperimposition = assetSuperimpositionRepo.findByUserIdAndPrefixName(asset.getUserId(), asset.getPrefixName()).orElse(null);
|
|
|
+ if (Objects.isNull(assetSuperimposition)) {
|
|
|
+ assetSuperimposition = AssetSuperimposition.create(collection, asset.getUserId());
|
|
|
+ }
|
|
|
+ switch (type) {
|
|
|
+ case OPEN:
|
|
|
+ log.info("调整数量 -> 公开展示");
|
|
|
+ assetSuperimposition.setCloseShowNum(assetSuperimposition.getConsignmentNum() - NUM);
|
|
|
+ assetSuperimposition.setOpenShowNum(assetSuperimposition.getOpenShowNum() + NUM);
|
|
|
+ break;
|
|
|
+ case CLOSE:
|
|
|
+ log.info("调整数量 -> 关闭公开展示");
|
|
|
+ // 关闭公开展示同时取消已经寄售的数量
|
|
|
+ if (asset.isConsignment()) {
|
|
|
+ assetSuperimposition.setConsignmentNum(assetSuperimposition.getConsignmentNum() - NUM);
|
|
|
+ }
|
|
|
+ assetSuperimposition.setCloseShowNum(assetSuperimposition.getConsignmentNum() + NUM);
|
|
|
+ assetSuperimposition.setOpenShowNum(assetSuperimposition.getOpenShowNum() - NUM);
|
|
|
+ break;
|
|
|
+ case CONSIGNMENT:
|
|
|
+ log.info("调整数量 -> 寄售");
|
|
|
+ publicShowOrFalseReduce(assetSuperimposition, asset);
|
|
|
+ assetSuperimposition.setConsignmentNum(assetSuperimposition.getConsignmentNum() + NUM);
|
|
|
+ break;
|
|
|
+ case CANCEL_CONSIGNMENT:
|
|
|
+ log.info("调整数量 -> 取消寄售");
|
|
|
+ publicShowOrFalseAdd(assetSuperimposition, asset);
|
|
|
+ assetSuperimposition.setConsignmentNum(assetSuperimposition.getConsignmentNum() - NUM);
|
|
|
+ break;
|
|
|
+ case CONSIGNMENT_SUCCESS:
|
|
|
+ log.info("调整数量 -> 转让成功");
|
|
|
+ assetSuperimposition.setNum(assetSuperimposition.getNum() - NUM);
|
|
|
+ assetSuperimposition.setConsignmentNum(assetSuperimposition.getConsignmentNum() - NUM);
|
|
|
+ publicShowOrFalseReduce(assetSuperimposition, asset);
|
|
|
+ relatedUserAdjustNum(relatedUserId, asset.getPrefixName(), collection);
|
|
|
+ break;
|
|
|
+ case AUCTIONING:
|
|
|
+ log.info("调整数量 -> 拍卖");
|
|
|
+ assetSuperimposition.setAuctioningNum(assetSuperimposition.getAuctionedNum() + NUM);
|
|
|
+ break;
|
|
|
+ case AUCTIONING_FAIL:
|
|
|
+ log.info("调整数量 -> 拍卖失败/取消");
|
|
|
+ assetSuperimposition.setAuctioningNum(assetSuperimposition.getAuctionedNum() - NUM);
|
|
|
+ break;
|
|
|
+ case AUCTIONING_SUCCESS:
|
|
|
+ log.info("调整数量 -> 拍卖成功");
|
|
|
+ assetSuperimposition.setNum(assetSuperimposition.getNum() - NUM);
|
|
|
+ assetSuperimposition.setAuctioningNum(assetSuperimposition.getAuctionedNum() - NUM);
|
|
|
+ publicShowOrFalseReduce(assetSuperimposition, asset);
|
|
|
+ relatedUserAdjustNum(relatedUserId, asset.getPrefixName(), collection);
|
|
|
+ break;
|
|
|
+ case GIFTING:
|
|
|
+ log.info("调整数量 -> 赠送");
|
|
|
+ assetSuperimposition.setNum(assetSuperimposition.getNum() - NUM);
|
|
|
+ publicShowOrFalseReduce(assetSuperimposition, asset);
|
|
|
+ relatedUserAdjustNum(relatedUserId, asset.getPrefixName(), collection);
|
|
|
+ break;
|
|
|
+ case BUY:
|
|
|
+ log.info("调整数量 -> 购买");
|
|
|
+ assetSuperimposition.setNum(assetSuperimposition.getNum() + NUM);
|
|
|
+ assetSuperimposition.setCloseShowNum(assetSuperimposition.getCloseShowNum() + NUM);
|
|
|
+ break;
|
|
|
+ case DESTORY:
|
|
|
+ log.info("调整数量 -> 销毁");
|
|
|
+ assetSuperimposition.setNum(assetSuperimposition.getNum() - NUM);
|
|
|
+ assetSuperimposition.setCloseShowNum(assetSuperimposition.getCloseShowNum() - NUM);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ assetSuperimpositionRepo.save(assetSuperimposition);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据藏品是否展示判断仅展示或未展示对应数量减去一
|
|
|
+ *
|
|
|
+ * @param assetSuperimposition 资产叠加数量信息
|
|
|
+ * @param asset 资产信息
|
|
|
+ * @return 资产叠加数量信息
|
|
|
+ */
|
|
|
+ private void publicShowOrFalseReduce(AssetSuperimposition assetSuperimposition, Asset asset) {
|
|
|
+ if (asset.isPublicShow()) {
|
|
|
+ assetSuperimposition.setOpenShowNum(assetSuperimposition.getOpenShowNum() - NUM);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ assetSuperimposition.setCloseShowNum(assetSuperimposition.getCloseShowNum() - NUM);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据藏品是否展示判断仅展示或未展示对应数量加一
|
|
|
+ *
|
|
|
+ * @param assetSuperimposition 资产叠加数量信息
|
|
|
+ * @param asset 资产信息
|
|
|
+ * @return 资产叠加数量信息
|
|
|
+ */
|
|
|
+ private void publicShowOrFalseAdd(AssetSuperimposition assetSuperimposition, Asset asset) {
|
|
|
+ if (asset.isPublicShow()) {
|
|
|
+ assetSuperimposition.setOpenShowNum(assetSuperimposition.getOpenShowNum() + NUM);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ assetSuperimposition.setCloseShowNum(assetSuperimposition.getCloseShowNum() + NUM);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 相关用户藏品叠加数量(买入人,被转增人等)
|
|
|
+ *
|
|
|
+ * @param relatedUserId 相关用户id
|
|
|
+ * @param prefixName 叠加分类
|
|
|
+ * @param collection 藏品信息
|
|
|
+ */
|
|
|
+ private void relatedUserAdjustNum(Long relatedUserId, String prefixName, Collection collection) {
|
|
|
+ if (Objects.isNull(relatedUserId)) {
|
|
|
+ throw new BusinessException("涉及相关用户id为空");
|
|
|
+ }
|
|
|
+ AssetSuperimposition assetSuperimposition = assetSuperimpositionRepo.findByUserIdAndPrefixName(relatedUserId, prefixName).orElse(null);
|
|
|
+ if (Objects.isNull(assetSuperimposition)) {
|
|
|
+ assetSuperimposition = AssetSuperimposition.create(collection, relatedUserId);
|
|
|
+ }
|
|
|
+ assetSuperimposition.setNum(assetSuperimposition.getNum() + NUM);
|
|
|
+ assetSuperimposition.setCloseShowNum(assetSuperimposition.getCloseShowNum() + NUM);
|
|
|
+ assetSuperimpositionRepo.save(assetSuperimposition);
|
|
|
+ }
|
|
|
+}
|