|
|
@@ -0,0 +1,127 @@
|
|
|
+package com.izouma.nineth.service;
|
|
|
+
|
|
|
+import com.izouma.nineth.annotations.RedisLock;
|
|
|
+import com.izouma.nineth.config.Constants;
|
|
|
+import com.izouma.nineth.domain.*;
|
|
|
+import com.izouma.nineth.dto.MetaRestResult;
|
|
|
+import com.izouma.nineth.dto.PageQuery;
|
|
|
+import com.izouma.nineth.enums.MetaPropOperationType;
|
|
|
+import com.izouma.nineth.enums.MetaPropUsedType;
|
|
|
+import com.izouma.nineth.enums.MetaStoreCommodityType;
|
|
|
+import com.izouma.nineth.repo.MetaPropRepo;
|
|
|
+import com.izouma.nineth.repo.MetaStorePurchaseRecordRepo;
|
|
|
+import com.izouma.nineth.repo.MetaStoreRepo;
|
|
|
+import com.izouma.nineth.repo.MetaUserPropRepo;
|
|
|
+import com.izouma.nineth.utils.JpaUtils;
|
|
|
+import com.izouma.nineth.utils.SecurityUtils;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import org.apache.commons.collections.CollectionUtils;
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.transaction.Transactional;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+@Service
|
|
|
+@AllArgsConstructor
|
|
|
+public class MetaStoreService {
|
|
|
+
|
|
|
+ private MetaStoreRepo metaStoreRepo;
|
|
|
+
|
|
|
+ private MetaPropRepo metaPropRepo;
|
|
|
+
|
|
|
+ private MetaUserPropRepo metaUserPropRepo;
|
|
|
+
|
|
|
+ private MetaUserGoldService metaUserGoldService;
|
|
|
+
|
|
|
+ private MetaUserPropRecordService metaUserPropRecordService;
|
|
|
+
|
|
|
+ private MetaStorePurchaseRecordRepo metaStorePurchaseRecordRepo;
|
|
|
+
|
|
|
+ public Page<MetaStore> all(PageQuery pageQuery) {
|
|
|
+ return metaStoreRepo.findAll(JpaUtils.toSpecification(pageQuery, MetaStore.class), JpaUtils.toPageRequest(pageQuery));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ public MetaRestResult<Void> purchase(Long id) {
|
|
|
+ MetaStore metaStore = metaStoreRepo.findByIdAndDel(id, false);
|
|
|
+ if (Objects.isNull(metaStore)) {
|
|
|
+ return MetaRestResult.returnError("商品不存在");
|
|
|
+ }
|
|
|
+ if (!metaStore.isOnShelf()) {
|
|
|
+ return MetaRestResult.returnError("商品未上架");
|
|
|
+ }
|
|
|
+ Long userId = SecurityUtils.getAuthenticatedUser().getId();
|
|
|
+ if (MetaStoreCommodityType.META_PROP.equals(metaStore.getCommodityType())) {
|
|
|
+ return purchaseProp(metaStore, userId);
|
|
|
+ }
|
|
|
+ return purchaseNFT(metaStore, userId);
|
|
|
+ }
|
|
|
+
|
|
|
+ @RedisLock("#userId")
|
|
|
+ @Transactional
|
|
|
+ public MetaRestResult<Void> purchaseNFT(MetaStore metaStore, Long userId) {
|
|
|
+ int price = metaStore.getPrice();
|
|
|
+ if (0 >= metaStore.getPrice()) {
|
|
|
+ return MetaRestResult.returnError("购买失败,道具价格不合法");
|
|
|
+ }
|
|
|
+ if (metaStore.getStockNum() <= 0) {
|
|
|
+ return MetaRestResult.returnError("购买失败,库存不足");
|
|
|
+ }
|
|
|
+ // 限购
|
|
|
+ List<MetaStorePurchaseRecord> metaStorePurchaseRecords = metaStorePurchaseRecordRepo.findAllByMetaStoreIdAndUserId(metaStore.getId(), userId);
|
|
|
+ if (CollectionUtils.isNotEmpty(metaStorePurchaseRecords) && metaStorePurchaseRecords.size() >= metaStore.getPurchaseLimitNum()) {
|
|
|
+ return MetaRestResult.returnError(String.format("购买失败,当前商品限购[%S]个", metaStore.getPurchaseLimitNum()));
|
|
|
+ }
|
|
|
+ // 扣减金币
|
|
|
+ MetaRestResult<MetaUserGold> restResult = metaUserGoldService.changeNum(userId, -price, String.format("购买NFT:[%S],消耗金币[%s]", metaStore.getName(), price));
|
|
|
+ if (restResult.getCode() != Constants.MetaRestCode.success) {
|
|
|
+ return MetaRestResult.returnError(restResult.getMessage());
|
|
|
+ }
|
|
|
+ // 保存购买记录
|
|
|
+ metaStorePurchaseRecordRepo.save(new MetaStorePurchaseRecord(metaStore.getId(), userId, LocalDateTime.now()));
|
|
|
+ // 商品库存减1
|
|
|
+ metaStore.setStockNum(metaStore.getStockNum() - 1);
|
|
|
+ metaStoreRepo.save(metaStore);
|
|
|
+ return MetaRestResult.returnSuccess("购买成功!");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ public MetaRestResult<Void> purchaseProp(MetaStore metaStore, Long userId) {
|
|
|
+ int price = metaStore.getPrice();
|
|
|
+ if (0 >= metaStore.getPrice()) {
|
|
|
+ return MetaRestResult.returnError("道具价格不合法");
|
|
|
+ }
|
|
|
+ MetaProp metaProp = metaPropRepo.findByIdAndDel(metaStore.getMetaPropId(), false);
|
|
|
+ if (Objects.isNull(metaProp)) {
|
|
|
+ return MetaRestResult.returnError("道具信息为空");
|
|
|
+ }
|
|
|
+ MetaUserProp dbMetaUserProp = metaUserPropRepo.findByUserIdAndMetaPropIdAndDel(userId, metaProp.getId(), false);
|
|
|
+ if (Objects.isNull(dbMetaUserProp)) {
|
|
|
+ dbMetaUserProp = MetaUserProp.create(userId, metaProp, 1);
|
|
|
+ MetaRestResult<MetaUserGold> restResult = metaUserGoldService.changeNum(userId, -price, String.format("购买道具:[%S],消耗金币[%s]", metaProp.getId(), price));
|
|
|
+ if (restResult.getCode() != Constants.MetaRestCode.success) {
|
|
|
+ return MetaRestResult.returnError(restResult.getMessage());
|
|
|
+ }
|
|
|
+ metaUserPropRepo.save(dbMetaUserProp);
|
|
|
+ metaUserPropRecordService.save(userId, metaProp, MetaPropOperationType.RECEIVE, 1);
|
|
|
+ metaStorePurchaseRecordRepo.save(new MetaStorePurchaseRecord(metaStore.getId(), userId, LocalDateTime.now()));
|
|
|
+ return MetaRestResult.returnSuccess("购买成功!");
|
|
|
+ }
|
|
|
+ if (MetaPropUsedType.PERMANENT.equals(metaProp.getUsedType()) && dbMetaUserProp.getNum() >= 1) {
|
|
|
+ return MetaRestResult.returnError("已拥有永久道具,不可购买");
|
|
|
+ }
|
|
|
+ MetaRestResult<MetaUserGold> restResult = metaUserGoldService.changeNum(userId, -price, String.format("购买道具:[%S],消耗金币[%s]", metaProp.getId(), price));
|
|
|
+ if (restResult.getCode() != Constants.MetaRestCode.success) {
|
|
|
+ return MetaRestResult.returnError(restResult.getMessage());
|
|
|
+ }
|
|
|
+ dbMetaUserProp.setNum(dbMetaUserProp.getNum() + 1);
|
|
|
+ metaUserPropRepo.save(dbMetaUserProp);
|
|
|
+ metaUserPropRecordService.save(userId, metaProp, MetaPropOperationType.RECEIVE, 1);
|
|
|
+ metaStorePurchaseRecordRepo.save(new MetaStorePurchaseRecord(metaStore.getId(), userId, LocalDateTime.now()));
|
|
|
+ return MetaRestResult.returnSuccess("购买成功!");
|
|
|
+
|
|
|
+ }
|
|
|
+}
|