package com.smtaiserver.smtaiserver.control; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import org.springframework.web.servlet.ModelAndView; import com.smtaiserver.smtaiserver.core.SMTAIServerApp; import com.smtaiserver.smtaiserver.core.SMTAIServerRequest; import com.smtaiserver.smtaiserver.database.SMTDatabase; import com.smtaiserver.smtaiserver.database.SMTDatabase.DBRecord; import com.smtaiserver.smtaiserver.database.SMTDatabase.DBRecords; import com.smtservlet.util.SMTJsonWriter; import com.smtservlet.util.SMTStatic; public class SMTSystemManagerControl { public ModelAndView getOperateLog(SMTAIServerRequest tranReq) throws Exception { Date startTime = tranReq.convParamToDate("start_time", false); Date endTime = tranReq.convParamToDate("end_time", false); String opType = tranReq.convParamToString("type", false); String opUser = tranReq.convParamToString("user", false); String opFrom = tranReq.convParamToString("from", false); SMTDatabase db = SMTAIServerApp.getApp().allocDatabase(); try { SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null); String sql = "SELECT A.*, B.user_name FROM sys_operate_log A LEFT JOIN sys_user_info B ON A.op_user=B.user_id WHERE 1=1 "; List sqlParams = new ArrayList<>(); if(startTime != null) { sql += " AND op_time >=?"; sqlParams.add(startTime); } if(endTime != null) { sql += " AND op_time <=?"; sqlParams.add(endTime); } if(opType != null) { sql += " AND op_type in (SELECT regexp_split_to_table(?, ','))"; sqlParams.add(opType); } if(opUser != null) { sql += " AND B.user_name LIKE ?"; sqlParams.add("%" + opUser + "%"); } if(opFrom != null) { sql += " AND op_from in (SELECT regexp_split_to_table(?, ','))"; sqlParams.add(opType); } sql += " ORDER BY op_time DESC LIMIT 1000"; DBRecords recs = db.querySQL(sql, sqlParams.toArray(new Object[sqlParams.size()])); jsonWr.beginArray("values"); for(DBRecord rec : recs.getRecords()) { jsonWr.beginMap(null); { jsonWr.addKeyValue("id", rec.getString("op_id")); jsonWr.addKeyValue("type", rec.getString("op_type")); jsonWr.addKeyValue("time", rec.getString("op_time")); jsonWr.addKeyValue("user", rec.getString("user_name")); jsonWr.addKeyValue("note", rec.getString("op_note")); jsonWr.addKeyValue("from", rec.getString("op_from")); jsonWr.addKeyValue("client_ip", rec.getString("client_ip")); } jsonWr.endMap(); } jsonWr.endArray(); return tranReq.returnJson(jsonWr); } finally { db.close(); } } public ModelAndView updateUserRoles(SMTAIServerRequest tranReq) throws Exception { String userId = tranReq.convParamToString("user_id", true); String[] userRoles = tranReq.convParamToStringArray("user_roles", false); SMTDatabase db = SMTAIServerApp.getApp().allocDatabase(); try { DBRecords recs = db.querySQL("SELECT user_name FROM sys_user_info WHERE user_id=?", new Object[] {userId}); if(recs.getRowCount() == 0) return tranReq.returnJsonState(false, "未发现用户", null); String userName = recs.getRecord(0).getString(0); db.executeSQL("BEGIN", null); try { db.executeSQL("DELETE FROM sys_role_user WHERE user_id=?", new Object[] {userId}); if(userRoles != null) { for(String userRole : userRoles) { db.executeSQL("INSERT INTO sys_role_user(user_id, role_id)VALUES(?,?)", new Object[] {userId, userRole}); } } tranReq.appendOperateLog(db, "更新角色", "更新用户[" + userName + "]的角色"); db.executeSQL("COMMIT", null); return tranReq.returnJsonState(true, null, null); } catch(Exception ex) { db.rollbackDB(ex); return null; } } finally { db.close(); } } public ModelAndView getRoleInfoList(SMTAIServerRequest tranReq) throws Exception { SMTDatabase db = SMTAIServerApp.getApp().allocDatabase(); try { SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null); DBRecords recsRoles = db.querySQL("SELECT * FROM sys_role_info", null); jsonWr.beginArray("values"); for(DBRecord recRole : recsRoles.getRecords()) { jsonWr.beginMap(null); { jsonWr.addKeyValue("id", recRole.getString("role_id")); jsonWr.addKeyValue("title", recRole.getString("role_title")); } jsonWr.endMap(); } jsonWr.endArray(); return tranReq.returnJson(jsonWr); } finally { db.close(); } } public ModelAndView addUserInfo(SMTAIServerRequest tranReq) throws Exception { String userName = tranReq.convParamToString("user_name", true); String password = SMTStatic.convStrToMD5(tranReq.convParamToString("password", true)); String userPhone = tranReq.convParamToString("phone", false); String realName = tranReq.convParamToString("real_name", false); String userPart = tranReq.convParamToString("part", false); String userSex = tranReq.convParamToString("sex", false); String userEmail = tranReq.convParamToString("email", false); String userNote = tranReq.convParamToString("note", false); String[] userRoles = tranReq.convParamToStringArray("user_roles", false); SMTDatabase db = SMTAIServerApp.getApp().allocDatabase(); try { String userId = SMTStatic.newUUID(); DBRecords recs = db.querySQL("SELECT 1 FROM sys_user_info WHERE user_name=? AND user_state_code='Y'", new Object[] {userName}); if(recs.getRowCount() != 0) return tranReq.returnJsonState(false, "用户已经存在", null); db.executeSQL("BEGIN", null); try { SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null); db.executeSQL("INSERT INTO sys_user_info(user_id, user_name, user_phone, real_name, user_part, user_sex, user_email, note,user_pass,user_state_code,create_time)VALUES(?,?,?,?,?,?,?,?,?,?,?)", new Object[] { userId, userName, userPhone, realName, userPart, userSex, userEmail, userNote, password, "Y", new Date() }); if(userRoles != null) { for(String userRole : userRoles) { db.executeSQL("INSERT INTO sys_role_user(user_id, role_id)VALUES(?,?)", new Object[] {userId, userRole}); } } tranReq.appendOperateLog(db, "创建用户", "创建用户:" + userName); db.executeSQL("COMMIT", null); jsonWr.addKeyValue("user_id", userId); return tranReq.returnJson(jsonWr); } catch(Exception ex) { db.executeSQL("ROLLBACK", null); throw ex; } } finally { db.close(); } } public ModelAndView deleteUserInfo(SMTAIServerRequest tranReq) throws Exception { String userId = tranReq.convParamToString("user_id", true); SMTDatabase db = SMTAIServerApp.getApp().allocDatabase(); try { SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null); DBRecords recs = db.querySQL("SELECT user_name FROM sys_user_info WHERE user_id=?", new Object[] {userId}); if(recs.getRowCount() == 0) return tranReq.returnJsonState(false, "未发现用户", null); String userName = recs.getRecord(0).getString(0); db.executeSQL("BEGIN", null); try { db.executeSQL("UPDATE sys_user_info SET user_state_code='N' WHERE user_id=?", new Object[] {userId}); db.executeSQL("DELETE FROM sys_role_user WHERE user_id=?", new Object[] {userId}); tranReq.appendOperateLog(db, "删除用户", "删除用户:" + userName); db.executeSQL("COMMIT", null); } catch(Exception ex) { db.rollbackDB(ex); return null; } return tranReq.returnJson(jsonWr); } finally { db.close(); } } public ModelAndView updateUserInfo(SMTAIServerRequest tranReq) throws Exception { String userId = tranReq.convParamToString("user_id", true); String userPhone = tranReq.convParamToString("phone", false); String realName = tranReq.convParamToString("real_name", false); String userPart = tranReq.convParamToString("part", false); String userSex = tranReq.convParamToString("sex", false); String userEmail = tranReq.convParamToString("email", false); String userNote = tranReq.convParamToString("note", false); SMTDatabase db = SMTAIServerApp.getApp().allocDatabase(); try { DBRecords recs = db.querySQL("SELECT user_name FROM sys_user_info WHERE user_id=?", new Object[] {userId}); if(recs.getRowCount() == 0) return tranReq.returnJsonState(false, "未发现用户", null); String userName = recs.getRecord(0).getString(0); SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null); db.executeSQL("UPDATE sys_user_info SET user_phone=?,real_name=?,user_part=?,user_sex=?,user_email=?,note=? WHERE user_id=?", new Object[] { userPhone, realName, userPart, userSex, userEmail, userNote, userId }); tranReq.appendOperateLog(db, "更新用户", "更新用户[" + userName + "]的信息"); return tranReq.returnJson(jsonWr); } finally { db.close(); } } public ModelAndView changeUserPassword(SMTAIServerRequest tranReq) throws Exception { String userId = tranReq.convParamToString("user_id", true); String password = SMTStatic.convStrToMD5(tranReq.convParamToString("password", true)); SMTDatabase db = SMTAIServerApp.getApp().allocDatabase(); try { DBRecords recs = db.querySQL("SELECT user_name FROM sys_user_info WHERE user_id=?", new Object[] {userId}); if(recs.getRowCount() == 0) return tranReq.returnJsonState(false, "未发现用户", null); String userName = recs.getRecord(0).getString(0); SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null); db.executeSQL("UPDATE sys_user_info SET user_pass=? WHERE user_id=?", new Object[] {password, userId}); tranReq.appendOperateLog(db, "修改密码", "修改用户[" + userName + "]的密码"); return tranReq.returnJson(jsonWr); } finally { db.close(); } } public ModelAndView getUserInfoList(SMTAIServerRequest tranReq) throws Exception { SMTDatabase db = SMTAIServerApp.getApp().allocDatabase(); try { SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null); DBRecords recsUser = db.querySQL("SELECT U.*, P.department_name FROM sys_user_info U LEFT JOIN sys_department_info P ON U.user_part=P.department_id WHERE user_state_code='Y'", null); DBRecords recsRoles = db.querySQL("SELECT * FROM sys_role_user", null); Map> mapUserId2RoleSet = new HashMap<>(); for(DBRecord recRole : recsRoles.getRecords()) { String roleId = recRole.getString("role_id"); String userId = recRole.getString("user_id"); Set setRole = mapUserId2RoleSet.get(userId); if(setRole == null) { setRole = new HashSet<>(); mapUserId2RoleSet.put(userId, setRole); } setRole.add(roleId); } jsonWr.beginArray("values"); for(DBRecord recUser : recsUser.getRecords()) { jsonWr.beginMap(null); { String userId = recUser.getString("user_id"); jsonWr.addKeyValue("id", userId); jsonWr.addKeyValue("phone", recUser.getString("user_phone")); jsonWr.addKeyValue("name", recUser.getString("user_name")); jsonWr.addKeyValue("real_name", recUser.getString("real_name")); jsonWr.addKeyValue("part_id", recUser.getString("user_part")); jsonWr.addKeyValue("part_name", recUser.getString("department_name")); jsonWr.addKeyValue("sex", recUser.getString("user_sex")); jsonWr.addKeyValue("email", recUser.getString("user_email")); jsonWr.addKeyValue("note", recUser.getString("note")); jsonWr.addKeyValue("create_time", recUser.getString("create_time")); Set setRole = mapUserId2RoleSet.get(userId); if(setRole != null) { jsonWr.beginArray("roles"); for(String role : setRole) { jsonWr.addKeyValue(null, role); } jsonWr.endArray(); } } jsonWr.endMap(); } jsonWr.endArray(); return tranReq.returnJson(jsonWr); } finally { db.close(); } } public ModelAndView getChatHistoryAnswer(SMTAIServerRequest tranReq) throws Exception { String historyId = tranReq.convParamToString("id", true); SMTDatabase db = SMTAIServerApp.getApp().allocDatabase(); try { SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null); DBRecords recs = db.querySQL("SELECT ask_json, trace_llm FROM chat_history_detail WHERE history_id=?", new Object[] {historyId}); if(recs.getRowCount() == 0) return tranReq.returnJsonState(false, "未找到对话id", null); DBRecord rec = recs.getRecord(0); String sAskJson = rec.getString("ask_json"); if(!SMTStatic.isNullOrEmpty(sAskJson)) jsonWr.addKeyRaw("ask_json", sAskJson); jsonWr.addKeyValue("ask_log", rec.getString("trace_llm")); return tranReq.returnJson(jsonWr); } finally { db.close(); } } public ModelAndView getChatHistoryList(SMTAIServerRequest tranReq) throws Exception { Date startTime = tranReq.convParamToDate("start_time", false); Date endTime = tranReq.convParamToDate("end_time", false); String groupType = tranReq.convParamToString("group_type", false); String opUser = tranReq.convParamToString("user", false); String question = tranReq.convParamToString("question", false); String state = tranReq.convParamToString("state", false); SMTDatabase db = SMTAIServerApp.getApp().allocDatabase(); try { SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null); String sql = " SELECT A.history_id, A.group_id, A.question, A.create_time, A.run_ms, A.chat_state, A.group_type, B.user_name" + " FROM chat_history_detail A" + " INNER JOIN chat_history_group C ON A.group_id=C.group_id" + " LEFT JOIN sys_user_info B ON C.user_id=B.user_id" + " WHERE 1=1 " ; List sqlParams = new ArrayList<>(); if(startTime != null) { sql += " AND A.create_time >=?"; sqlParams.add(startTime); } if(endTime != null) { sql += " AND A.create_time <=?"; sqlParams.add(endTime); } if(groupType != null) { sql += " AND A.group_type in (SELECT regexp_split_to_table(?, ','))"; sqlParams.add(groupType); } if(opUser != null) { sql += " AND B.user_name LIKE ?"; sqlParams.add("%" + opUser + "%"); } if(question != null) { sql += " AND A.question LIKE ?"; sqlParams.add("%" + question + "%"); } if(state != null) { sql += " AND A.chat_state = ?"; sqlParams.add(state); } sql += " ORDER BY create_time DESC LIMIT 1000"; DBRecords recs = db.querySQL(sql, sqlParams.toArray(new Object[sqlParams.size()])); jsonWr.beginArray("values"); for(DBRecord rec : recs.getRecords()) { jsonWr.beginMap(null); { jsonWr.addKeyValue("id", rec.getString("history_id")); jsonWr.addKeyValue("group_id", rec.getString("group_id")); jsonWr.addKeyValue("time", rec.getString("create_time")); jsonWr.addKeyValue("user", rec.getString("user_name")); jsonWr.addKeyValue("question", rec.getString("question")); jsonWr.addKeyValue("run_ms", rec.getString("run_ms")); jsonWr.addKeyValue("state", rec.getString("chat_state")); jsonWr.addKeyValue("group_type", rec.getString("group_type")); } jsonWr.endMap(); } jsonWr.endArray(); return tranReq.returnJson(jsonWr); } finally { db.close(); } } public ModelAndView getDepartmentList(SMTAIServerRequest tranReq) throws Exception { SMTDatabase db = SMTAIServerApp.getApp().allocDatabase(); try { SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null); DBRecords recs = db.querySQL("SELECT D.*, U.user_name FROM sys_department_info D LEFT JOIN sys_user_info U ON U.user_id=D.create_user", null); jsonWr.beginArray("values"); for(DBRecord rec : recs.getRecords()) { jsonWr.beginMap(null); { jsonWr.addKeyValue("id", rec.getString("department_id")); jsonWr.addKeyValue("parent_id", rec.getString("department_p_id")); jsonWr.addKeyValue("name", rec.getString("department_name")); jsonWr.addKeyValue("create_user", rec.getString("user_name")); jsonWr.addKeyValue("create_time", rec.getString("create_time")); } jsonWr.endMap(); } jsonWr.endArray(); return tranReq.returnJson(jsonWr); } finally { db.close(); } } public ModelAndView addDepartmentInfo(SMTAIServerRequest tranReq) throws Exception { String partName = tranReq.convParamToString("name", false); String parentId = tranReq.convParamToString("parent_id", false); SMTDatabase db = SMTAIServerApp.getApp().allocDatabase(); try { SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null); String partId = SMTStatic.newUUID(); db.executeSQL( "INSERT INTO sys_department_info(department_id, department_p_id, department_name, create_user, create_time)VALUES(?,?,?,?,?)", new Object[] { partId, parentId, partName, tranReq.getLoginUserId(), new Date() }); jsonWr.addKeyValue("id", partId); return tranReq.returnJson(jsonWr); } finally { db.close(); } } public ModelAndView deleteDepartment(SMTAIServerRequest tranReq) throws Exception { String partId = tranReq.convParamToString("id", true); SMTDatabase db = SMTAIServerApp.getApp().allocDatabase(); try { DBRecords recs = db.querySQL("SELECT COUNT(*) FROM sys_department_info WHERE department_p_id=?", new Object[] {partId}); if(recs.getRecord(0).getInteger(0) > 0) return tranReq.returnJsonState(false, "当前部门有下属子部门", null); recs = db.querySQL("SELECT COUNT(*) FROM sys_user_info WHERE user_part=?", new Object[] {partId}); if(recs.getRecord(0).getInteger(0) > 0) return tranReq.returnJsonState(false, "当前部门有所属用户", null); db.executeSQL("DELETE FROM sys_department_info WHERE department_id=?", new Object[] {partId}); return tranReq.returnJsonState(true, null, null); } finally { db.close(); } } public ModelAndView updateDepartmentInfo(SMTAIServerRequest tranReq) throws Exception { String partId = tranReq.convParamToString("id", true); String partName = tranReq.convParamToString("name", true); SMTDatabase db = SMTAIServerApp.getApp().allocDatabase(); try { db.executeSQL("UPDATE sys_department_info SET department_name=? WHERE department_id=?", new Object[] {partName, partId}); return tranReq.returnJsonState(true, null, null); } finally { db.close(); } } public ModelAndView getUserSampleList(SMTAIServerRequest tranReq) throws Exception { String userId = tranReq.convParamToString("user_id", true); SMTDatabase db = SMTAIServerApp.getApp().allocDatabase(); try { String sql = "SELECT\r\n" + " group_id, sample_id, sample_question, sample_title\r\n" + "FROM\r\n" + " ai_scene_sample\r\n" + "WHERE\r\n" + " sample_enable='Y' AND \n" + " group_id IN (\r\n" + " SELECT\r\n" + " group_id\r\n" + " FROM\r\n" + " sys_role_group\r\n" + " WHERE\r\n" + " role_id IN (\r\n" + " SELECT\r\n" + " role_id\r\n" + " FROM\r\n" + " sys_role_user\r\n" + " WHERE\r\n" + " user_id=?\r\n" + " )\r\n" + " \r\n" + " )"; DBRecords recs = db.querySQL(sql, new Object[] {userId}); SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null); jsonWr.beginArray("values"); for(DBRecord rec : recs.getRecords()) { jsonWr.beginMap(null); { jsonWr.addKeyValue("sample_id", rec.getString("sample_id")); jsonWr.addKeyValue("group_id", rec.getString("group_id")); jsonWr.addKeyValue("question", rec.getString("sample_question")); jsonWr.addKeyValue("title", rec.getString("sample_title")); } jsonWr.endMap(); } jsonWr.endArray(); return tranReq.returnJson(jsonWr); } finally { db.close(); } } public ModelAndView getLLMInfoList(SMTAIServerRequest tranReq) throws Exception { SMTDatabase db = SMTAIServerApp.getApp().allocDatabase(); try { SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null); DBRecords recsFactory = db.querySQL("SELECT * FROM ai_llm_factory", null); DBRecords recsConnect = db.querySQL("SELECT * FROM ai_llm_connect", null); jsonWr.beginMap("values"); for(DBRecord rec : recsFactory.getRecords()) { jsonWr.beginMap(rec.getString("factory_id")); { jsonWr.addKeyValue("title", rec.getString("factory_title")); String sJsonConfig = rec.getString("factory_args"); if(!SMTStatic.isNullOrEmpty(sJsonConfig)) jsonWr.addKeyRaw("config", sJsonConfig); } jsonWr.endMap(); } for(DBRecord rec : recsConnect.getRecords()) { jsonWr.beginMap(rec.getString("factory_id")); { jsonWr.beginMap("connects"); { jsonWr.beginMap(rec.getString("connect_id")); { jsonWr.addKeyValue("title", rec.getString("connect_title")); jsonWr.addKeyValue("class", rec.getString("class_name")); String sJsonConfig = rec.getString("connect_args"); if(!SMTStatic.isNullOrEmpty(sJsonConfig)) jsonWr.addKeyRaw("config", sJsonConfig); } jsonWr.endMap(); } jsonWr.endMap(); } jsonWr.endMap(); } jsonWr.endMap(); return tranReq.returnJson(jsonWr); } finally { db.close(); } } }