From 816397e6f5cf0e4887d6f515a2a05e91e102862f Mon Sep 17 00:00:00 2001 From: zhangzhiyong Date: Tue, 2 Jul 2024 10:03:28 +0800 Subject: [PATCH] [enhancement] JCommon MongoDB supports pagination queries #851 --- jcommon/ai/bytedance/pom.xml | 7 +++ .../run/mone/ai/bytedance/DoubaoClient.java | 56 +++++++++++++++++++ .../src/main/java/run/mone/auth/AuthAop.java | 5 ++ .../src/main/java/run/mone/bo/User.java | 6 +- .../java/run/mone/service/MongoService.java | 22 ++++++++ .../youpin/docean/test/HttpServerTest.java | 3 +- jcommon/pom.xml | 30 +++++----- 7 files changed, 112 insertions(+), 17 deletions(-) create mode 100644 jcommon/ai/bytedance/src/main/java/run/mone/ai/bytedance/DoubaoClient.java diff --git a/jcommon/ai/bytedance/pom.xml b/jcommon/ai/bytedance/pom.xml index bbadac9dd..b19d7e7e2 100644 --- a/jcommon/ai/bytedance/pom.xml +++ b/jcommon/ai/bytedance/pom.xml @@ -29,6 +29,13 @@ jackson-databind 2.12.4 + + + com.squareup.okhttp3 + okhttp + 4.10.0 + + diff --git a/jcommon/ai/bytedance/src/main/java/run/mone/ai/bytedance/DoubaoClient.java b/jcommon/ai/bytedance/src/main/java/run/mone/ai/bytedance/DoubaoClient.java new file mode 100644 index 000000000..94808fb5f --- /dev/null +++ b/jcommon/ai/bytedance/src/main/java/run/mone/ai/bytedance/DoubaoClient.java @@ -0,0 +1,56 @@ +package run.mone.ai.bytedance; + + +import okhttp3.*; + +/** + * @author goodjava@qq.com + * @date 2024/5/18 17:20 + */ +public class DoubaoClient { + + private static String ARK_API_KEY = ""; + + public static void main(String[] args) { + OkHttpClient client = new OkHttpClient(); + String json = "{\n" + + " \"model\": \"\",\n" + + " \"messages\": [\n" + + " {\n" + + " \"role\": \"system\",\n" + + " \"content\": \"You are a helpful assistant.\"\n" + + " },\n" + + " {\n" + + " \"role\": \"user\",\n" + + " \"content\": \"Hello!\"\n" + + " }\n" + + " ],\n" + + " \"stream\": false\n" + + "}"; + + MediaType JSON = MediaType.get("application/json; charset=utf-8"); + RequestBody body = RequestBody.create(json, JSON); + + Request request = new Request.Builder() + .url("") + .addHeader("Content-Type", "application/json") + .addHeader("Authorization", "Bearer " + ARK_API_KEY) // 确保你已经设置了 ARK_API_KEY 变量 + .post(body) + .build(); + + try (Response response = client.newCall(request).execute()) { + // 处理响应 + if (response.isSuccessful()) { + String responseBody = response.body().string(); + System.out.println(responseBody); + } else { + System.out.println(response); + } + } catch (Exception e) { + e.printStackTrace(); + } + + + } + +} diff --git a/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/run/mone/auth/AuthAop.java b/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/run/mone/auth/AuthAop.java index f9ea28bfa..ad8252e42 100644 --- a/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/run/mone/auth/AuthAop.java +++ b/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/run/mone/auth/AuthAop.java @@ -68,6 +68,11 @@ public void before(ProceedingJoinPoint point) { } } + //没有就当普通用户处理 + if (null == user.getRole()) { + user.setRole("user"); + } + //必须有后台管理权限 if (role.equals("admin")) { if (null == user || !user.getRole().equals("admin")) { diff --git a/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/run/mone/bo/User.java b/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/run/mone/bo/User.java index 01f224d39..0480cfc6c 100644 --- a/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/run/mone/bo/User.java +++ b/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/run/mone/bo/User.java @@ -7,6 +7,8 @@ import lombok.Data; import lombok.NoArgsConstructor; +import java.util.Map; + /** * 用户 * @@ -17,7 +19,7 @@ @AllArgsConstructor @NoArgsConstructor @Entity("user") -public class User implements MongoBo{ +public class User implements MongoBo { @Id private String id; @@ -57,6 +59,8 @@ public class User implements MongoBo{ private String token; + private Map meta; + public User(String username, String password) { this.username = username; this.password = password; diff --git a/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/run/mone/service/MongoService.java b/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/run/mone/service/MongoService.java index 7b0171630..8b5726413 100644 --- a/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/run/mone/service/MongoService.java +++ b/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/run/mone/service/MongoService.java @@ -3,6 +3,7 @@ import com.xiaomi.youpin.docean.anno.Service; import dev.morphia.Datastore; import dev.morphia.UpdateOptions; +import dev.morphia.query.FindOptions; import dev.morphia.query.filters.Filter; import dev.morphia.query.filters.Filters; import dev.morphia.query.updates.UpdateOperator; @@ -65,6 +66,27 @@ public List findAll(Filter filter) { return datastore.find(this.clazz).filter(filter).iterator().toList(); } + public List findAll() { + return datastore.find(this.clazz).iterator().toList(); + } + + //实现分页查询(class) + public List findWithPagination(Filter filter, int page, int size) { + FindOptions options = new FindOptions() + .skip((page - 1) * size) + .limit(size); + return datastore.find(this.clazz) + .filter(filter) + .iterator(options) + .toList(); + } + + //查询总页数(class) + public long getTotalPages(Filter filter, int pageSize) { + long totalRecords = datastore.find(this.clazz).filter(filter).count(); + return (totalRecords + pageSize - 1) / pageSize; + } + public boolean delete(T t) { datastore.delete(t); return true; diff --git a/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/HttpServerTest.java b/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/HttpServerTest.java index bce62cebd..33ede037e 100644 --- a/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/HttpServerTest.java +++ b/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/HttpServerTest.java @@ -116,7 +116,8 @@ public void testWebSocketServer() { @Test public void testWebSocketClient() { OkHttpClient client = new OkHttpClient(); - Request request = new Request.Builder().url("ws://127.0.0.1:8899/ws").build(); +// Request request = new Request.Builder().url("ws://127.0.0.1:8899/ws").build(); + Request request = new Request.Builder().url("").build(); WebSocketListener listener = new WebSocketListener() { @Override public void onOpen(WebSocket webSocket, Response response) { diff --git a/jcommon/pom.xml b/jcommon/pom.xml index bd8a0c84c..66ebff815 100644 --- a/jcommon/pom.xml +++ b/jcommon/pom.xml @@ -232,21 +232,21 @@ - - - - - - - - - - - - - - - + + maven-source-plugin + 2.1 + + + compile + + jar + + + + + true + +