licailing 3 роки тому
батько
коміт
caf746a4c4

+ 22 - 0
src/main/java/com/izouma/nineth/domain/DestroyRecord.java

@@ -0,0 +1,22 @@
+package com.izouma.nineth.domain;
+
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import javax.persistence.Entity;
+
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+@Builder
+@Entity
+public class DestroyRecord extends BaseEntity {
+    private Long userId;
+
+    private Long assetId;
+
+    private int record;
+
+}

+ 4 - 0
src/main/java/com/izouma/nineth/domain/User.java

@@ -178,4 +178,8 @@ public class User extends UserBaseEntity implements Serializable {
     private boolean walletEnabled = false;
 
     private String hcChainAddress;
+
+    @Column(columnDefinition = "int(11) default 0")
+    @ApiModelProperty("销毁积分")
+    private int destroyPoint = 0;
 }

+ 2 - 1
src/main/java/com/izouma/nineth/enums/AssetStatus.java

@@ -9,7 +9,8 @@ public enum AssetStatus {
     MINTING("铸造中"),
     AUCTIONING("拍卖中"),
     AUCTION_TRADING("拍卖中"),
-    AUCTIONED("已拍卖")
+    AUCTIONED("已拍卖"),
+    DESTROYED("已销毁")
     ;
 
     private final String description;

+ 1 - 1
src/main/java/com/izouma/nineth/enums/TransferReason.java

@@ -3,7 +3,7 @@ package com.izouma.nineth.enums;
 public enum TransferReason {
     TRANSFER("转让"),
     GIFT("转赠"),
-
+    DESTROY("销毁")
     ;
 
     TransferReason(String description) {

+ 16 - 0
src/main/java/com/izouma/nineth/repo/DestroyRecordRepo.java

@@ -0,0 +1,16 @@
+package com.izouma.nineth.repo;
+
+import com.izouma.nineth.domain.DestroyRecord;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+import org.springframework.data.jpa.repository.Modifying;
+import org.springframework.data.jpa.repository.Query;
+
+import javax.transaction.Transactional;
+
+public interface DestroyRecordRepo extends JpaRepository<DestroyRecord, Long>, JpaSpecificationExecutor<DestroyRecord> {
+    @Query("update DestroyRecord t set t.del = true where t.id = ?1")
+    @Modifying
+    @Transactional
+    void softDelete(Long id);
+}

+ 5 - 0
src/main/java/com/izouma/nineth/repo/UserRepo.java

@@ -246,4 +246,9 @@ public interface UserRepo extends JpaRepository<User, Long>, JpaSpecificationExe
 
     @Query("select u.id from User u where u.del = false and u.phone in ?1")
     List<Long> findIdByPhones(Collection<String> phones);
+
+    @Transactional
+    @Modifying
+    @Query("update User set destroyPoint = destroyPoint + ?2 where id = ?1")
+    void addDestroyPoint(Long id, int num);
 }

+ 49 - 0
src/main/java/com/izouma/nineth/service/AssetService.java

@@ -59,6 +59,7 @@ public class AssetService {
     private CollectionPrivilegeRepo collectionPrivilegeRepo;
     private PasswordEncoder         passwordEncoder;
     private MintActivityRepo        mintActivityRepo;
+    private DestroyRecordRepo       destroyRecordRepo;
 
     public Page<Asset> all(PageQuery pageQuery) {
         Page<Asset> all = assetRepo
@@ -666,4 +667,52 @@ public class AssetService {
                     "%" + mintActivity.getCollectionName() + "%", pageable);
         }
     }
+
+    public void destroy(Long id, Long userId) {
+        Asset asset = assetRepo.findById(id).orElseThrow(new BusinessException("无记录"));
+        if (!asset.getUserId().equals(userId)) {
+            throw new BusinessException("此藏品不属于你");
+        }
+        if (asset.getStatus() != AssetStatus.NORMAL) {
+            throw new BusinessException("当前状态不可寄售");
+        }
+        if (asset.isPublicShow()) {
+            throw new BusinessException("请先取消公开展示");
+//            cancelPublic(asset);
+        }
+
+        User toUser = userRepo.findById(1435297L).orElseThrow(new BusinessException("无记录"));
+
+        TokenHistory tokenHistory = TokenHistory.builder()
+                .tokenId(asset.getTokenId())
+                .fromUser(asset.getOwner())
+                .fromUserId(asset.getOwnerId())
+                .fromAvatar(asset.getOwnerAvatar())
+                .toUser(toUser.getNickname())
+                .toUserId(toUser.getId())
+                .toAvatar(toUser.getAvatar())
+                .operation(TransferReason.DESTROY.getDescription())
+                .price(null)
+                .build();
+        tokenHistoryRepo.save(tokenHistory);
+
+        asset.setPublicShow(false);
+        asset.setConsignment(false);
+        asset.setPublicCollectionId(null);
+        asset.setStatus(AssetStatus.DESTROYED);
+        asset.setOwner(toUser.getNickname());
+        asset.setOwnerId(toUser.getId());
+        asset.setOwnerAvatar(toUser.getAvatar());
+        assetRepo.save(asset);
+
+        //积分记录
+        destroyRecordRepo.save(DestroyRecord.builder()
+                .userId(userId)
+                .assetId(asset.getId())
+                .record(1)
+                .build());
+
+        //加积分
+        userRepo.addDestroyPoint(userId, 1);
+    }
 }

+ 6 - 0
src/main/java/com/izouma/nineth/web/AssetController.java

@@ -174,6 +174,12 @@ public class AssetController extends BaseController {
     public Page<Asset> byTag(@RequestParam Long tagId, Pageable pageable) {
         return assetRepo.byTag(SecurityUtils.getAuthenticatedUser().getId(), tagId, pageable);
     }
+
+    @ApiOperation("销毁")
+    @PostMapping("destroy")
+    public void destroy(@RequestParam Long assetId) {
+        assetService.destroy(assetId, SecurityUtils.getAuthenticatedUser().getId());
+    }
 }
 
 

+ 5 - 5
src/test/java/com/izouma/nineth/service/AssetServiceTest.java

@@ -18,12 +18,7 @@ import org.apache.commons.lang3.StringUtils;
 import org.apache.rocketmq.spring.core.RocketMQTemplate;
 import org.junit.jupiter.api.Test;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.data.jpa.domain.Specification;
 
-import javax.persistence.criteria.CriteriaBuilder;
-import javax.persistence.criteria.CriteriaQuery;
-import javax.persistence.criteria.Predicate;
-import javax.persistence.criteria.Root;
 import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -262,4 +257,9 @@ class AssetServiceTest extends ApplicationTests {
         assetService.transfer(asset, BigDecimal.ZERO, user, TransferReason.GIFT, null);
     }
 
+    @Test
+    public void destroy() {
+        assetService.destroy(8025352L,9972L);
+    }
+
 }