From cd5d537aadbe302fc8b62f87edd8aea81021930e Mon Sep 17 00:00:00 2001 From: qfrjava <13402782+qfrjava@user.noreply.gitee.com> Date: 星期日, 27 四月 2025 13:38:22 +0800 Subject: [PATCH] feat(LightRAG): 新增 lightrag 服务管理接口并实现相关功能 --- JAVA/SMTAIServer/src/main/java/com/smtaiserver/smtaiserver/control/SMTLightRAGController.java | 126 ++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 126 insertions(+), 0 deletions(-) diff --git a/JAVA/SMTAIServer/src/main/java/com/smtaiserver/smtaiserver/control/SMTLightRAGController.java b/JAVA/SMTAIServer/src/main/java/com/smtaiserver/smtaiserver/control/SMTLightRAGController.java new file mode 100644 index 0000000..c3e891c --- /dev/null +++ b/JAVA/SMTAIServer/src/main/java/com/smtaiserver/smtaiserver/control/SMTLightRAGController.java @@ -0,0 +1,126 @@ +package com.smtaiserver.smtaiserver.control; + +import com.smtaiserver.smtaiserver.core.SMTAIServerApp; +import com.smtaiserver.smtaiserver.database.SMTDatabase; +import com.smtservlet.core.SMTRequest; +import com.smtservlet.util.SMTJsonWriter; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.springframework.web.servlet.ModelAndView; + +import java.util.HashMap; +import java.util.Map; + +/** lightRag controller */ +public class SMTLightRAGController { + private static Logger _logger = LogManager.getLogger(SMTLightRAGController.class); + + /** + * 鑾峰彇lightrag鏈嶅姟鐨勫惎鍔ㄥ垪琛� + * + * @param tranReq + * @return + * @throws Exception + */ + public ModelAndView getLightragServerList(SMTRequest tranReq) throws Exception { + SMTDatabase db = SMTAIServerApp.getApp().allocDatabase(); + try { + SMTDatabase.DBRecords records = db.querySQL("SELECT * FROM lightrag_server_list", null); + if (records.getRowCount() == 0) return tranReq.returnJsonState(true, null, null); + SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null); + for (int i = 0; i < records.getRowCount(); i++) { + SMTDatabase.DBRecord record = records.getRecord(i); + jsonWr.addKeyValue("server_id", record.getValue("server_id")); + jsonWr.addKeyValue("server_title", record.getValue("server_title")); + jsonWr.addKeyValue("server_port", record.getValue("server_port")); + jsonWr.addKeyValue("is_enable", record.getValue("is_enable")); + } + return tranReq.returnJson(jsonWr); + } catch (Exception e) { + throw new Exception("getLightragServerList error" + e); + } finally { + db.close(); + } + } + + public ModelAndView updateLightragServerEnable(SMTRequest tranReq) throws Exception { + SMTDatabase db = SMTAIServerApp.getApp().allocDatabase(); + try { + // 浠庤姹備腑鑾峰彇鍙傛暟 + String serverId = tranReq.convParamToString("server_id", true); + String isEnable = tranReq.convParamToString("is_enable", true); + + // 鍙傛暟鏍¢獙 + if (serverId == null || isEnable == null) { + return tranReq.returnJsonState(false, "鍙傛暟缂哄け: server_id 鎴� is_enable 涓嶈兘涓虹┖", null); + } + + // 鎵ц鏇存柊 + Map<String, Object> params = new HashMap<>(); + params.put("server_id", serverId); + params.put("is_enable", isEnable); + + int affectedRows = + db.executeSQL( + "UPDATE lightrag_server_list SET is_enable = ? WHERE server_id = ?", + new Object[] {isEnable, serverId}); + + if (affectedRows == 0) { + return tranReq.returnJsonState(false, "鏈壘鍒板搴旂殑 server_id锛屾洿鏂板け璐�", null); + } + + // 鎴愬姛杩斿洖 + return tranReq.returnJsonState(true, null, null); + + } catch (Exception e) { + throw new Exception("updateLightragServerEnable error: " + e); + } finally { + db.close(); + } + } + + public ModelAndView addLightragServer(SMTRequest tranReq) throws Exception { + SMTDatabase db = SMTAIServerApp.getApp().allocDatabase(); + try { + // 浠庤姹備腑鑾峰彇鍙傛暟 + String serverId = tranReq.convParamToString("server_id", true); + String serverTitle = tranReq.convParamToString("server_title", true); + String serverPort = tranReq.convParamToString("server_port", true); + String isEnable = tranReq.convParamToString("is_enable", true); + + // 鍙傛暟鏍¢獙 + if (serverId == null || serverTitle == null || serverPort == null || isEnable == null) { + return tranReq.returnJsonState( + false, "鍙傛暟缂哄け: server_id銆乻erver_title銆乻erver_port銆乮s_enable 涓嶈兘涓虹┖", null); + } + + // 鍏堟煡璇� server_id 鏄惁宸插瓨鍦� + SMTDatabase.DBRecords existingRecords = + db.querySQL( + "SELECT server_id FROM lightrag_server_list WHERE server_id = ?", + new Object[] {serverId}); + + if (existingRecords.getRowCount() > 0) { + return tranReq.returnJsonState(false, "server_id 宸插瓨鍦紝涓嶈兘閲嶅鏂板", null); + } + + // 鎵ц鎻掑叆 + int affectedRows = + db.executeSQL( + "INSERT INTO lightrag_server_list (server_id, server_title, server_port, is_enable) VALUES (?, ?, ?, ?)", + new Object[] {serverId, serverTitle, serverPort, isEnable}); + + if (affectedRows == 0) { + return tranReq.returnJsonState(false, "鏂板澶辫触", null); + } + + // 鎴愬姛杩斿洖 + return tranReq.returnJsonState(true, null, null); + + } catch (Exception e) { + throw new Exception("addLightragServer error: " + e); + } finally { + db.close(); + } + } +} -- Gitblit v1.9.3