xiongzhu před 3 roky
rodič
revize
078050ff3c

+ 20 - 4
src/main/java/com/izouma/nineth/service/AssetService.java

@@ -270,7 +270,7 @@ public class AssetService {
         assetRepo.save(asset);
     }
 
-    public synchronized void consignment(Long id, BigDecimal price, String tradeCode) {
+    public synchronized void consignment(Long id, BigDecimal price, String tradeCode, boolean safeFlag) {
         Asset asset = assetRepo.findById(id).orElseThrow(new BusinessException("无记录"));
         if (!asset.getUserId().equals(SecurityUtils.getAuthenticatedUser().getId())) {
             throw new BusinessException("此藏品不属于你");
@@ -298,9 +298,25 @@ public class AssetService {
         if (ChronoUnit.DAYS.between(asset.getCreatedAt(), LocalDateTime.now()) < holdDays) {
             throw new BusinessException("需持有满" + holdDays + "天才能寄售上架");
         }
-        User owner = asset.isSafeFlag() ?
-                userRepo.findById(asset.getOwnerId()).orElseThrow(new BusinessException("用户不存在"))
-                : userRepo.findById(asset.getUserId()).orElseThrow(new BusinessException("用户不存在"));
+        User owner;
+        if (safeFlag && !asset.isSafeFlag()) {
+            owner = createFakeUser();
+            asset.setOwner(owner.getNickname());
+            asset.setOwnerId(owner.getId());
+            asset.setOwnerAvatar(owner.getAvatar());
+            tokenHistoryRepo.findByTokenIdOrderByCreatedAtDesc(asset.getTokenId()).stream()
+                    .filter(t -> t.getToUserId().equals(asset.getUserId())).findFirst()
+                    .ifPresent(tokenHistory -> {
+                        tokenHistory.setToUserId(owner.getId());
+                        tokenHistory.setToUser(owner.getNickname());
+                        tokenHistory.setToAvatar(owner.getAvatar());
+                        tokenHistoryRepo.save(tokenHistory);
+                    });
+        } else {
+            owner = asset.isSafeFlag() ?
+                    userRepo.findById(asset.getOwnerId()).orElseThrow(new BusinessException("用户不存在"))
+                    : userRepo.findById(asset.getUserId()).orElseThrow(new BusinessException("用户不存在"));
+        }
         if (!passwordEncoder.matches(tradeCode, userRepo.findTradeCode(asset.getUserId()))) {
             throw new BusinessException("交易密码错误");
         }

+ 3 - 2
src/main/java/com/izouma/nineth/web/AssetController.java

@@ -115,8 +115,9 @@ public class AssetController extends BaseController {
 
     @PostMapping("/consignment")
     @ApiOperation("寄售")
-    public void consignment(@RequestParam Long id, @RequestParam BigDecimal price, @RequestParam String tradeCode) {
-        assetService.consignment(id, price, tradeCode);
+    public void consignment(@RequestParam Long id, @RequestParam BigDecimal price, @RequestParam String tradeCode,
+                            @RequestParam(defaultValue = "false") boolean safeFlag) {
+        assetService.consignment(id, price, tradeCode, safeFlag);
     }
 
     @PostMapping("/cancelConsignment")