Browse Source

websocket服务提供接口

sunkean 3 years ago
parent
commit
7a424404f6

+ 32 - 0
src/main/java/com/izouma/nineth/dto/PurchaseLevelDTO.java

@@ -0,0 +1,32 @@
+package com.izouma.nineth.dto;
+
+import com.izouma.nineth.domain.PurchaseLevel;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+@Builder
+public class PurchaseLevelDTO {
+
+    private int startLevel;
+
+    private int endLevel;
+
+    private String realm;
+
+    private String title;
+
+    public static PurchaseLevelDTO create(PurchaseLevel purchaseLevel) {
+        return PurchaseLevelDTO.builder()
+                .startLevel(purchaseLevel.getStartLevel())
+                .endLevel(purchaseLevel.getEndLevel())
+                .realm(purchaseLevel.getRealm())
+                .title(purchaseLevel.getTitle())
+                .build();
+    }
+
+}

+ 17 - 0
src/main/java/com/izouma/nineth/dto/WebsocketUser.java

@@ -0,0 +1,17 @@
+package com.izouma.nineth.dto;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+public class WebsocketUser {
+
+    private String nickname;
+
+    private String avatar;
+
+    private int level;
+}

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

@@ -1,9 +1,7 @@
 package com.izouma.nineth.repo;
 
 import com.izouma.nineth.domain.User;
-import com.izouma.nineth.dto.CollectionStockAndSale;
 import com.izouma.nineth.dto.InvitedUserDTO;
-import com.izouma.nineth.dto.InvitorDTO;
 import com.izouma.nineth.enums.AuthStatus;
 import com.izouma.nineth.security.Authority;
 import org.springframework.cache.annotation.Cacheable;
@@ -16,6 +14,7 @@ import javax.transaction.Transactional;
 import java.time.LocalDateTime;
 import java.util.Collection;
 import java.util.List;
+import java.util.Map;
 import java.util.Optional;
 
 public interface UserRepo extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {
@@ -263,4 +262,8 @@ public interface UserRepo extends JpaRepository<User, Long>, JpaSpecificationExe
     @Modifying
     @Query("update User set destroyPoint = destroyPoint + ?2 where id = ?1")
     void addDestroyPoint(Long id, int num);
+
+    @Query(nativeQuery = true, value ="SELECT user.avatar avatar, user.nickname nickname, user.level level FROM user WHERE user.id = ?1 AND user.del = false")
+    Map<String, String> websocketQuery(Long userId);
+
 }

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

@@ -2,6 +2,7 @@ package com.izouma.nineth.web;
 
 import com.izouma.nineth.domain.PurchaseLevel;
 import com.izouma.nineth.dto.PageQuery;
+import com.izouma.nineth.dto.PurchaseLevelDTO;
 import com.izouma.nineth.exception.BusinessException;
 import com.izouma.nineth.repo.PurchaseLevelRepo;
 import com.izouma.nineth.service.PurchaseLevelService;
@@ -56,5 +57,10 @@ public class PurchaseLevelController extends BaseController {
         List<PurchaseLevel> data = all(pageQuery).getContent();
         ExcelUtils.export(response, data);
     }
+
+    @GetMapping("/websocket/{level}")
+    public PurchaseLevelDTO websocket(@PathVariable int level) {
+        return PurchaseLevelDTO.create(purchaseLevelService.findPurchaseLevelByLevel(level));
+    }
 }
 

+ 11 - 0
src/main/java/com/izouma/nineth/web/UserController.java

@@ -453,6 +453,17 @@ public class UserController extends BaseController {
         });
         return users;
     }
+
+    @GetMapping("/websocket/{userId}")
+    public WebsocketUser websocket(@PathVariable Long userId) {
+        Map<String, String> map = userRepo.websocketQuery(userId);
+        if (Objects.isNull(map)) {
+            throw new BusinessException("没有玩家信息");
+        }
+        JSONArray jsonArray = new JSONArray();
+        jsonArray.add(map);
+        return jsonArray.toJavaObject(WebsocketUser.class);
+    }
 }