Merge remote-tracking branch 'origin/main'

# Conflicts:
#	srs-admin/src/main/java/com/srs/web/controller/routine/NotificationManagementController.java
#	srs-routine/src/main/java/com/srs/routine/service/impl/NotificationManagementServiceImpl.java
This commit is contained in:
MDSMO
2025-08-12 17:19:32 +08:00
11 changed files with 1009 additions and 5 deletions

View File

@@ -0,0 +1,907 @@
package com.srs.web.controller.aitutor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.srs.common.core.controller.BaseController;
// OkHttp 显式导入
import com.srs.common.core.domain.model.LoginUser;
import com.srs.common.exception.ServiceException;
import com.srs.common.utils.SecurityUtils;
import com.srs.teacher.domain.dto.ConversationDTO;
import okhttp3.*;
// Spring 显式导入(不要用 *)
import com.srs.common.core.domain.AjaxResult; // ✅ RuoYi 的返回结果类
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.CompletableFuture;
/**
* Dify聊天控制器
* <p>
* 该控制器用于处理与Dify AI聊天服务的通信提供流式聊天功能。
* Dify是一个LLM应用开发平台此控制器通过调用其API实现与AI模型的交互。
* </p>
*/
@RestController
@RequestMapping("/aitutor/aichat")
public class AiChatController extends BaseController {
/**
* Dify API的URL地址
* 用于发送聊天消息请求到Dify服务
*/
private static final String DIFY_API_URL = "http://47.112.118.149:8100/v1/chat-messages";
//private static final String DIFY_API_URL = "http://localhost:8080/v1/chat-messages";
/**
* Dify反馈API的基础URL
* 用于提交消息反馈(点赞、点踩等)
*/
private static final String DIFY_FEEDBACK_BASE_URL = "http://47.112.118.149:8100/v1/messages";
//private static final String DIFY_FEEDBACK_BASE_URL = "http://localhost:8080/v1/messages";
private static final String DIFY_API_HISTORY_URL = "http://47.112.118.149:8100/v1/messages";
//private static final String DIFY_API_HISTORY_URL = "http://localhost:8080/v1/messages";
/**
* Dify会话API的基础URL
* 用于获取会话列表
*/
private static final String DIFY_CONVERSATIONS_URL = "http://47.112.118.149:8100/v1/conversations";
//private static final String DIFY_CONVERSATIONS_URL = "http://localhost:8080/v1/conversations";
//文件上传
private static final String DIFY_FILES_URL = "http://47.112.118.149:8100/v1/files/upload";
/**
* Dify API的访问密钥
* 用于身份验证授权访问Dify服务
*/
private static final String DIFY_API_KEY = "app-2wjqcYI9n6igHTVHdH8qXlnh";
//private static final String DIFY_API_KEY = "app-";
/**
* HTTP客户端实例
* 配置了5分钟的读取超时时间用于与Dify API进行通信
*/
private final OkHttpClient client = new OkHttpClient.Builder()
.readTimeout(Duration.ofMinutes(5))
.build();
/**
* JSON对象映射器
* 用于处理JSON数据的序列化和反序列化
*/
private final ObjectMapper mapper = new ObjectMapper();
/**
* 处理流式聊天请求的端点
* <p>
* 该方法接收客户端发送的聊天请求并通过SSEServer-Sent Events方式将响应流式传输回客户端。
* 使用异步处理避免阻塞主线程,提高系统并发处理能力。
* </p>
*
* @param requestData 包含聊天请求数据的Map应包含以下字段
* - query: 用户的聊天消息内容(必需)
* - user: 用户标识符(必需)
* - conversation_id: 对话ID用于维持对话上下文可选
* - inputs: 输入参数,用于传递额外的上下文信息(可选)
* @return SseEmitter 用于向客户端流式传输响应的SSE发射器
*/
@PostMapping("/stream")
public SseEmitter stream(@org.springframework.web.bind.annotation.RequestBody Map<String, Object> requestData) {
SseEmitter emitter = new SseEmitter(300_000L);
// 设置超时处理回调
emitter.onTimeout(() -> {
System.out.println("!!! SSE连接超时 !!!");
emitter.complete();
});
// 设置错误处理回调
emitter.onError(ex -> {
System.out.println("!!! SSE连接发生错误 !!!");
ex.printStackTrace();
emitter.completeWithError(ex);
});
// 在主线程中获取当前用户名,避免在异步线程中获取安全上下文
String currentUsername = SecurityUtils.getLoginUser().getUsername();
// 异步执行请求处理,避免阻塞主线程
CompletableFuture.runAsync(() -> {
try {
sendToDifyAndStream(requestData, emitter, currentUsername);
} catch (Exception e) {
e.printStackTrace();
try {
emitter.send(SseEmitter.event().name("error").data("服务器内部错误: " + e.getMessage()));
} catch (IOException ioException) {
ioException.printStackTrace();
}
emitter.completeWithError(e);
}
});
return emitter;
}
/**
* 发送请求到Dify并流式传输响应
* <p>
* 该方法构建请求参数调用Dify API并处理返回的流式响应数据。
* 根据Dify API返回的不同事件类型将数据通过SSE发送给客户端。
* </p>
*
* @param requestData 包含聊天请求数据的Map包含用户消息、用户标识等信息
* @param emitter 用于向客户端发送SSE事件的发射器
* @param currentUsername
* @throws IOException 当网络请求或IO操作失败时抛出
*/
private void sendToDifyAndStream(Map<String, Object> requestData, SseEmitter emitter, String currentUsername) throws IOException {
// 构建请求体参数
Map<String, Object> bodyMap = new HashMap<>();
bodyMap.put("query", requestData.get("query")); // 用户消息内容
bodyMap.put("user", currentUsername); // 用户标识
bodyMap.put("response_mode", "streaming"); // 设置为流式响应模式
// 如果存在对话ID则添加到请求参数中
if (requestData.containsKey("conversation_id")) {
bodyMap.put("conversation_id", requestData.get("conversation_id"));
}
// 添加输入参数默认为空HashMap
Map<String, Object> inputs = (Map<String, Object>) requestData.getOrDefault("inputs", new HashMap<>());
inputs.put("user_id", currentUsername);
Object userName = requestData.get("user_name");
if (userName != null) {
inputs.put("user_name", userName);
}
Object userToken = requestData.get("user_token");
if (userToken != null) {
inputs.put("user_token", userToken);
}
Object userRole = requestData.get("user_role");
if (userRole != null) {
inputs.put("user_role", userRole);
}
bodyMap.put("inputs", inputs);
// 自动为对话生成名称
bodyMap.put("auto_generate_name", false);
// 将请求参数转换为JSON字符串
String jsonBody = mapper.writeValueAsString(bodyMap);
// 创建请求体对象
RequestBody body = RequestBody.create(MediaType.get("application/json; charset=utf-8"), jsonBody);
// 构建HTTP请求
Request httpRequest = new Request.Builder()
.url(DIFY_API_URL) // 设置请求URL
.addHeader("Authorization", "Bearer " + DIFY_API_KEY) // 添加认证头
.addHeader("Content-Type", "application/json") // 设置内容类型
.post(body) // 设置为POST请求
.build();
// 执行HTTP请求
try (Response httpResponse = client.newCall(httpRequest).execute()) {
// 检查响应是否成功
if (!httpResponse.isSuccessful()) {
String errorMsg = "Dify 请求失败: " + httpResponse.code() + " " + httpResponse.message();
System.out.println(errorMsg);
try {
httpResponse.body().string();
} catch (Exception e) {
System.out.println("无法读取响应体: " + e.getMessage());
}
emitter.send(SseEmitter.event().name("error").data(errorMsg));
emitter.complete();
return;
}
// 读取响应流数据
try (BufferedReader reader = new BufferedReader(httpResponse.body().charStream())) {
String line;
while ((line = reader.readLine()) != null) {
if (line.isEmpty() || !line.startsWith("data:")) continue;
String jsonData = line.substring(5).trim();
if ("[DONE]".equals(jsonData)) {
break;
}
// 解析JSON数据
JsonNode node = mapper.readTree(jsonData);
String eventType = node.get("event").asText();
// 根据事件类型处理不同的响应,直接转发原始数据
switch (eventType) {
case "workflow_started":
case "node_started":
case "node_finished":
case "message_start":
case "message":
case "message_end":
case "error":
case "ping":
// 直接转发Dify的原始SSE数据格式
emitter.send(SseEmitter.event().data(jsonData));
break;
default:
break;
}
}
}
// 完成SSE流传输
emitter.complete();
} catch (Exception e) {
// 处理异常情况
e.printStackTrace();
emitter.completeWithError(e);
}
}
/**
* 提交消息反馈(点赞、点踩、撤销、文本反馈)
* <p>
* 该接口代理前端调用 Dify 的反馈 API避免在前端暴露 API Key。
* 支持:'like', 'dislike', 或 null撤销
* </p>
*
* @param feedbackData 包含 feedback 信息的 JSON 对象
* 示例:
* {
* "message_id": "msg-123",
* "user": "user-1",
* "rating": "like", // "like", "dislike", 或 null
* "content": "回答很好"
* }
* @return 统一响应结果
*/
@PostMapping("/feedback")
public AjaxResult submitFeedback(@org.springframework.web.bind.annotation.RequestBody Map<String, Object> feedbackData) {
// 校验必要字段
String messageId = (String) feedbackData.get("message_id");
if (messageId == null || messageId.trim().isEmpty()) {
return AjaxResult.error("message_id 不能为空");
}
// 获取 user必填建议前端传
Object userObj = feedbackData.get("user");
if (userObj == null || userObj.toString().trim().isEmpty()) {
return AjaxResult.error("user 不能为空");
}
String user = userObj.toString();
// 处理 rating支持 "like", "dislike", null撤销
Object ratingObj = feedbackData.get("rating");
String rating = null;
if (ratingObj != null) {
String raw = ratingObj.toString();
if ("like".equals(raw)) {
rating = "like";
} else if ("dislike".equals(raw)) {
rating = "dislike";
} else {
return AjaxResult.error("rating 必须是 'like'、'dislike' 或 null撤销");
}
}
// 如果 ratingObj 为 nullrating 保持 null表示撤销
// 可选 content
String content = (String) feedbackData.get("content");
// 构建请求体(发送给 Dify
Map<String, Object> bodyMap = new HashMap<>();
bodyMap.put("user", user);
bodyMap.put("rating", rating); // 可以是 "like", "dislike", 或 null
bodyMap.put("content", content); // 可选文本反馈
try {
// 序列化为 JSON
String jsonBody = mapper.writeValueAsString(bodyMap);
okhttp3.RequestBody body = okhttp3.RequestBody.create(
MediaType.get("application/json; charset=utf-8"),
jsonBody
);
// 调用 Dify API
Request request = new Request.Builder()
.url(DIFY_FEEDBACK_BASE_URL + "/" + messageId + "/feedbacks")
.addHeader("Authorization", "Bearer " + DIFY_API_KEY)
.addHeader("Content-Type", "application/json")
.post(body)
.build();
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful()) {
return AjaxResult.success("反馈提交成功");
} else {
String errorMsg = "Dify 反馈失败: " + response.code();
try (ResponseBody errorBody = response.body()) {
if (errorBody != null) {
errorMsg += " - " + errorBody.string();
}
} catch (IOException e) {
errorMsg += " (无法读取错误详情)";
}
return AjaxResult.error(errorMsg);
}
}
} catch (Exception e) {
return AjaxResult.error("提交反馈失败: " + e.getMessage());
}
}
/**
* 获取APP的消息点赞和反馈列表
* <p>
* 该接口用于获取整个Dify应用的终端用户反馈、点赞列表类似Dify的日志界面
* </p>
*
* @param page 页码默认值1
* @param limit 每页数量默认值20最大100
* @return 包含点赞、反馈列表的统一响应结果
*/
@GetMapping("/app/feedbacks")
public AjaxResult getAppFeedbacks(
@RequestParam(value = "page", defaultValue = "1") String page,
@RequestParam(value = "limit", defaultValue = "20") String limit) {
try {
// 参数校验和限制
int limitValue = Math.min(Math.max(Integer.parseInt(limit), 1), 100);
// 构建请求URL
String url = "http://47.112.118.149:8100/v1/app/feedbacks?page=" + page + "&limit=" + limitValue;
// 构建请求
Request request = new Request.Builder()
.url(url)
.addHeader("Authorization", "Bearer " + DIFY_API_KEY)
.addHeader("Content-Type", "application/json")
.get()
.build();
// 发送请求
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful()) {
String responseBody = response.body().string();
JsonNode rootNode = mapper.readTree(responseBody);
// 解析数据
JsonNode dataNode = rootNode.get("data");
List<Map<String, Object>> feedbackList = new ArrayList<>();
if (dataNode != null && dataNode.isArray()) {
for (JsonNode feedbackNode : dataNode) {
Map<String, Object> feedbackItem = new HashMap<>();
// 提取反馈信息
feedbackItem.put("id", feedbackNode.has("id") ? feedbackNode.get("id").asText() : null);
feedbackItem.put("message_id", feedbackNode.has("message_id") ? feedbackNode.get("message_id").asText() : null);
feedbackItem.put("rating", feedbackNode.has("rating") ? feedbackNode.get("rating").asText() : null);
feedbackItem.put("content", feedbackNode.has("content") ? feedbackNode.get("content").asText() : null);
feedbackItem.put("created_at", feedbackNode.has("created_at") ? feedbackNode.get("created_at").asLong() : null);
feedbackItem.put("app_id", feedbackNode.has("app_id") ? feedbackNode.get("app_id").asText() : null);
feedbackItem.put("conversation_id", feedbackNode.has("conversation_id") ? feedbackNode.get("conversation_id").asText() : null);
// 提取用户信息
if (feedbackNode.has("from_end_user")) {
JsonNode userNode = feedbackNode.get("from_end_user");
Map<String, Object> userMap = new HashMap<>();
userMap.put("id", userNode.has("id") ? userNode.get("id").asText() : null);
userMap.put("name", userNode.has("name") ? userNode.get("name").asText() : null);
userMap.put("email", userNode.has("email") ? userNode.get("email").asText() : null);
feedbackItem.put("from_end_user", userMap);
}
// 提取消息内容
if (feedbackNode.has("message")) {
JsonNode messageNode = feedbackNode.get("message");
Map<String, Object> messageMap = new HashMap<>();
messageMap.put("id", messageNode.has("id") ? messageNode.get("id").asText() : null);
messageMap.put("query", messageNode.has("query") ? messageNode.get("query").asText() : null);
messageMap.put("answer", messageNode.has("answer") ? messageNode.get("answer").asText() : null);
messageMap.put("created_at", messageNode.has("created_at") ? messageNode.get("created_at").asLong() : null);
feedbackItem.put("message", messageMap);
}
feedbackList.add(feedbackItem);
}
}
// 直接返回数组形式的数据
return AjaxResult.success(feedbackList);
} else {
String errorMsg = "获取反馈列表失败: " + response.code() + " " + response.message();
try (ResponseBody errorBody = response.body()) {
if (errorBody != null) {
String errorBodyString = errorBody.string();
errorMsg += " - 响应体: " + errorBodyString;
// 尝试解析响应体中的错误信息
try {
JsonNode errorNode = mapper.readTree(errorBodyString);
if (errorNode.has("message")) {
errorMsg += " - 错误详情: " + errorNode.get("message").asText();
}
} catch (Exception e) {
// 如果无法解析为JSON就保持原始响应体
}
}
} catch (IOException e) {
errorMsg += " (无法读取错误详情): " + e.getMessage();
}
return AjaxResult.error(errorMsg);
}
}
} catch (Exception e) {
return AjaxResult.error("获取反馈列表时发生异常: " + e.getMessage());
}
}
/* @GetMapping("/history")
public AjaxResult getHistoryMessagesToAdmin(@RequestParam String user,
@RequestParam(required = false) String firstId,
@RequestParam(defaultValue = "20") int limit) {
//调用查询会话
String conversation_id = conversations(user);
}*/
/**
* 获取会话历史消息的端点
* <p>
* 该方法接收客户端发送的请求,获取指定会话的历史消息记录。
* </p>
* <p>
* //@param conversation_id 会话ID
*
* @param firstId 当前页第一条聊天记录的ID默认null
* @param limit 一次请求返回多少条记录默认20条
* @return AjaxResult 返回会话历史消息的结果
*/
@GetMapping("/history")
public AjaxResult getHistoryMessages(
//@RequestParam(required = false) String conversation_id,
@RequestParam(required = false) String firstId,
@RequestParam(defaultValue = "20") int limit) {
try {
/* // 获取当前用户
String currentUsername = SecurityUtils.getLoginUser().getUsername();
// 获取会话列表的id
String conversation_id = conversations(currentUsername);
if(conversation_id == null){
return error("没有会话");
}*/
// 验证conversationId是否为空
/*if (conversation_id == null || conversation_id.trim().isEmpty()) {
return error("会话ID不能为空后端");
}*/
String conversation_id = "8fb04ac4-ac9f-470b-9ac4-fee1ebec6412";
String currentUsername = SecurityUtils.getLoginUser().getUsername();
System.out.println(currentUsername);
System.out.println(conversation_id);
// 构建请求参数
HttpUrl.Builder urlBuilder = HttpUrl.parse(DIFY_API_HISTORY_URL).newBuilder();
urlBuilder.addQueryParameter("conversation_id", conversation_id);
urlBuilder.addQueryParameter("user", currentUsername);
if (firstId != null) {
urlBuilder.addQueryParameter("first_id", firstId);
}
urlBuilder.addQueryParameter("limit", String.valueOf(limit));
System.out.println(currentUsername);
// 构建HTTP请求
Request request = new Request.Builder()
.url(urlBuilder.build())
.addHeader("Authorization", "Bearer " + DIFY_API_KEY)
.get()
.build();
// 执行HTTP请求
try (Response response = client.newCall(request).execute()) {
// 检查响应是否成功
if (!response.isSuccessful()) {
return error("Dify 请求失败: " + response.code() + " " + response.message());
}
// 解析JSON响应
JsonNode rootNode = mapper.readTree(response.body().string());
boolean hasMore = rootNode.path("has_more").asBoolean(false);
List<Map<String, Object>> data = mapper.convertValue(rootNode.path("data"), List.class);
// 构建返回结果
Map<String, Object> result = new HashMap<>();
result.put("limit", limit);
result.put("has_more", hasMore);
result.put("data", data);
return success(result);
}
} catch (IOException e) {
return error("获取会话历史消息失败: " + e.getMessage());
}
}
/**
* 获取会话列表
* <p>
* 该方法用于获取当前用户的会话列表,默认返回最近的 20 条。
* </p>
*
* @param user 用户标识符,由开发者定义规则,需保证用户标识在应用内唯一
* @param lastId 当前页最后一条记录的 ID默认 null
* @param limit 一次请求返回多少条记录,默认 20 条,最大 100 条,最小 1 条
* @param sortBy 排序字段,默认 -updated_at (按更新时间倒序排列)
* @return AjaxResult 返回会话列表的结果
*/
/*@GetMapping("/getConversationsToUser")
public AjaxResult getConversationsToUser(
@RequestParam String user,
@RequestParam(required = false) String lastId,
@RequestParam(defaultValue = "20") int limit,
@RequestParam(defaultValue = "-updated_at") String sortBy) {
try {
// 获取当前用户
String currentUsername = SecurityUtils.getLoginUser().getUsername();
// 获取会话列表的id
String conversation_id = conversations(currentUsername);
return AjaxResult.success(conversation_id);*/
/*// 参数校验
if (user == null || user.trim().isEmpty()) {
return error("用户标识不能为空");
}
// 限制limit的范围在1-100之间
if (limit < 1 || limit > 100) {
return error("limit参数必须在1到100之间");
}
// 构建请求参数
HttpUrl.Builder urlBuilder = HttpUrl.parse(DIFY_CONVERSATIONS_URL).newBuilder();
urlBuilder.addQueryParameter("user", user);
if (lastId != null) {
urlBuilder.addQueryParameter("last_id", lastId);
}
urlBuilder.addQueryParameter("limit", String.valueOf(limit));
urlBuilder.addQueryParameter("sort_by", sortBy);
// 构建HTTP请求
Request request = new Request.Builder()
.url(urlBuilder.build())
.addHeader("Authorization", "Bearer " + DIFY_API_KEY)
.get()
.build();
// 执行HTTP请求
try (Response response = client.newCall(request).execute()) {
// 检查响应是否成功
if (!response.isSuccessful()) {
return error("Dify 请求失败: " + response.code() + " " + response.message());
}
// 解析JSON响应
JsonNode rootNode = mapper.readTree(response.body().string());
int resLimit = rootNode.path("limit").asInt();
boolean hasMore = rootNode.path("has_more").asBoolean(false);
List<Map<String, Object>> data = new ArrayList<>();
// 解析会话列表数据
JsonNode dataArray = rootNode.path("data");
if (dataArray.isArray()) {
for (JsonNode conversationNode : dataArray) {
Map<String, Object> conversation = new HashMap<>();
conversation.put("id", conversationNode.path("id").asText());
conversation.put("name", conversationNode.path("name").asText());
conversation.put("inputs", mapper.convertValue(conversationNode.path("inputs"), Map.class));
conversation.put("status", conversationNode.path("status").asText());
conversation.put("introduction", conversationNode.path("introduction").asText());
conversation.put("created_at", conversationNode.path("created_at").asLong());
conversation.put("updated_at", conversationNode.path("updated_at").asLong());
data.add(conversation);
}
}
// 构建返回结果
Map<String, Object> result = new HashMap<>();
result.put("limit", resLimit);
result.put("has_more", hasMore);
result.put("data", data);
return success(result);
}*/
/* } catch (IOException e) {
return error("获取会话列表失败: " + e.getMessage());
}
}*/
// 权限标识为辅导员
@GetMapping("/getMessagesToAdmin")
public AjaxResult getMessagesToAdmin(@RequestParam String user,
@RequestParam(required = false) String lastId,
@RequestParam(defaultValue = "20") int limit,
@RequestParam(defaultValue = "-updated_at") String sortBy) {
try {
List<ConversationDTO> conversations = getConversations(user);
if (conversations == null || conversations.isEmpty()) {
return AjaxResult.error("暂无会话记录");
}
String conversation_id = conversations.get(0).getId();
System.out.println(conversation_id);
return success(getConversationHistoryMessages(conversation_id, user, null, 20));
} catch (IOException e) {
return AjaxResult.error("网络请求失败,请稍后重试",error());
}
}
// 权限标识为学生
@GetMapping("/getMessagesToUser")
public AjaxResult getMessagesToUser(@RequestParam(required = false) String lastId,
@RequestParam(defaultValue = "20") int limit,
@RequestParam(defaultValue = "-updated_at") String sortBy) {
try {
String user = SecurityUtils.getLoginUser().getUsername();
List<ConversationDTO> conversations = getConversations(user);
if (conversations == null || conversations.isEmpty()) {
return AjaxResult.error("暂无会话记录");
}
String conversation_id = conversations.get(0).getId();
return success(getConversationHistoryMessages(conversation_id, user, null, 20));
} catch (IOException e) {
return AjaxResult.error("网络请求失败,请稍后重试");
}
}
/*
* 获取会话列表
* */
public List<ConversationDTO> getConversations(String user, String lastId, int limit, String sortBy) throws IOException {
// 构建带查询参数的 URL
HttpUrl.Builder urlBuilder = HttpUrl.parse(DIFY_CONVERSATIONS_URL).newBuilder();
urlBuilder.addQueryParameter("user", user);
if (lastId != null && !lastId.trim().isEmpty()) {
urlBuilder.addQueryParameter("last_id", lastId);
}
urlBuilder.addQueryParameter("limit", String.valueOf(limit));
urlBuilder.addQueryParameter("sort_by", sortBy);
// 构建请求
Request request = new Request.Builder()
.url(urlBuilder.build())
.addHeader("Authorization", "Bearer " + DIFY_API_KEY)
.get()
.build();
// 执行请求
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw new IOException("Dify API 请求失败: " + response.code() + " " + response.message());
}
// 读取响应体
String responseBodyString = response.body().string();
JsonNode rootNode = mapper.readTree(responseBodyString);
// 提取 data 数组
JsonNode dataArray = rootNode.path("data");
List<ConversationDTO> conversations = new ArrayList<>();
if (dataArray.isArray()) {
for (JsonNode node : dataArray) {
ConversationDTO dto = new ConversationDTO();
dto.setId(node.path("id").asText(null));
dto.setName(node.path("name").asText(null));
dto.setIntroduction(node.path("introduction").asText(null));
// 处理时间戳(假设接口返回的是 Unix 时间戳,单位:秒)
long createdAt = node.path("created_at").asLong(0L);
long updatedAt = node.path("updated_at").asLong(0L);
dto.setCreated_at(createdAt > 0 ? new Date(createdAt * 1000) : null);
dto.setUpdated_at(updatedAt > 0 ? new Date(updatedAt * 1000) : null);
conversations.add(dto);
}
}
return conversations;
}
}
public List<ConversationDTO> getConversations(String user) throws IOException {
return getConversations(user, null, 0, null);
}
public List<ConversationDTO> getConversations(String user, String lastId) throws IOException {
return getConversations(user, lastId, 0, null);
}
public List<ConversationDTO> getConversations(String user, Integer limit, String sortBy) throws IOException {
return getConversations(user, null, limit, sortBy);
}
//获取历史消息
private Map<String, Object> getConversationHistoryMessages(
String conversationId,
String user,
String firstId,
Integer limit) throws ServiceException {
// 参数校验
if (conversationId == null || conversationId.trim().isEmpty()) {
throw new IllegalArgumentException("conversationId 不能为空");
}
if (user == null || user.trim().isEmpty()) {
throw new IllegalArgumentException("user 不能为空");
}
conversationId = conversationId.trim();
user = user.trim();
int finalLimit = limit != null && limit > 0 ? Math.min(limit, 100) : 20;
// 构建请求 URL
HttpUrl.Builder urlBuilder = HttpUrl.parse(DIFY_API_HISTORY_URL).newBuilder();
urlBuilder.addQueryParameter("conversation_id", conversationId);
urlBuilder.addQueryParameter("user", user);
if (firstId != null && !firstId.trim().isEmpty()) {
urlBuilder.addQueryParameter("first_id", firstId.trim());
}
urlBuilder.addQueryParameter("limit", String.valueOf(finalLimit));
Request request = new Request.Builder()
.url(urlBuilder.build())
.addHeader("Authorization", "Bearer " + DIFY_API_KEY)
.addHeader("Accept", "application/json")
.get()
.build();
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) {
String body = response.body() != null ? response.body().string() : "No body";
String msg = String.format("HTTP %d %s - %s", response.code(), response.message(), body);
throw new ServiceException("请求失败");
}
ResponseBody responseBody = response.body();
if (responseBody == null) {
return emptyResult(finalLimit);
}
String responseBodyString = responseBody.string();
JsonNode rootNode;
try {
rootNode = mapper.readTree(responseBodyString);
} catch (JsonProcessingException e) {
throw new ServiceException("响应数据格式错误");
}
// 提取 data 数组
List<Map<String, Object>> data = new ArrayList<>();
JsonNode dataArray = rootNode.get("data");
if (dataArray != null && dataArray.isArray()) {
data = mapper.convertValue(dataArray, new TypeReference<List<Map<String, Object>>>() {
});
}
// 提取 has_more
boolean hasMore = rootNode.path("has_more").asBoolean(false);
// ✅ 直接构建前端需要的返回结构
Map<String, Object> result = new HashMap<>();
result.put("data", data);
result.put("has_more", hasMore);
result.put("limit", finalLimit);
return result; // 直接返回,调用方直接丢给前端
} catch (IOException e) {
throw new ServiceException("网络请求失败: " + e.getMessage());
}
}
// 辅助方法:返回空结果
private Map<String, Object> emptyResult(int limit) {
Map<String, Object> result = new HashMap<>();
result.put("data", new ArrayList<>());
result.put("has_more", false);
result.put("limit", limit);
return result;
}
/*
*//**
* 文件上传接口
* <p>
* 上传文件并在发送消息时使用,可实现图文多模态理解。支持应用程序所支持的所有格式。
* 上传的文件仅供当前终端用户使用。
* </p>
*
* @param file 要上传的文件
* @param user 用户标识,用于定义终端用户的身份
* @return 包含文件信息的统一响应结果
*//*
@PostMapping("/files/upload")
public AjaxResult uploadFile(@RequestParam("file") MultipartFile file,
@RequestParam("user") String user) {
try {
// 检查文件是否为空
if (file.isEmpty()) {
return AjaxResult.error("文件不能为空");
}
// 创建MultipartBody以上传文件
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("user", user)
.addFormDataPart("file", file.getOriginalFilename(),
RequestBody.create(MediaType.parse("application/octet-stream"),file.getBytes()))
.build();
// 构建请求
Request request = new Request.Builder()
.url(DIFY_FILES_URL)
.addHeader("Authorization", "Bearer " + DIFY_API_KEY)
.post(requestBody)
.build();
// 发送请求
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful()) {
String responseBody = response.body().string();
JsonNode rootNode = mapper.readTree(responseBody);
// 返回成功结果
return AjaxResult.success("文件上传成功", mapper.convertValue(rootNode, Map.class));
} else {
String errorMsg = "文件上传失败: " + response.code() + " " + response.message();
try (ResponseBody errorBody = response.body()) {
if (errorBody != null) {
errorMsg += " - " + errorBody.string();
}
} catch (IOException e) {
errorMsg += " (无法读取错误详情)";
}
return AjaxResult.error(errorMsg);
}
}
} catch (Exception e) {
return AjaxResult.error("文件上传异常: " + e.getMessage());
}
}*/
}

View File

@@ -91,6 +91,8 @@ public class SrsStudent extends BaseEntity
})
@TableField(exist = false)
private SrsMajors srsMajors;
@TableField(exist = false)
private CphStuExtraInfo cphStuExtraInfo;
/** 所属班级ID */
private Long classId;

View File

@@ -32,6 +32,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<association property="srsMajors" column="major_id" javaType="srsMajors" resultMap="SrsMajorsResult" />
<association property="srsClass" column="class_id" javaType="srsClass" resultMap="SrsClassResult" />
<association property="cphTeacher" column="teacher_id" javaType="cphTeacher" resultMap="CphTeacherResult" />
<association property="cphStuExtraInfo" column="stu_no" javaType="cphStuExtraInfo" resultMap="CphStuExtraInfoResult" />
</resultMap>
<resultMap id="CphStuExtraInfoResult" type="CphStuExtraInfo">
<result property="stuNo" column="stu_no"/>
<result property="mz" column="mz"/>
<result property="zzmm" column="zzmm"/>
<result property="jg" column="jg"/>
<result property="xx" column="xx"/>
</resultMap>
<resultMap id="CphTeacherResult" type="CphTeacher">
<result property="teacherId" column="teacher_id"/>
@@ -77,13 +85,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<sql id="selectSrsStudentJoin">
select s.stu_id,s.stu_no, s.name, s.gender, s.birthday,AES_DECRYPT(UNHEX(s.id_card),'zhxg') as id_card , d.dept_id, m.major_id, c.class_id, s.phone, s.address,
s.status, s.status as status2,s.create_by, s.create_time, s.update_by, s.update_time, s.del_flag,s.xhk,s.bank_addr,e.name as cphName,f.grade_name,
d.*,m.*,c.*
d.*,m.*,c.*,g.*
from srs_student s
left JOIN srs_class c ON s.class_id = c.class_id
left JOIN srs_majors m ON c.major_id = m.major_id
left JOIN sys_dept d ON m.college_id = d.dept_id
left join cph_teacher as e on c.teacher_id = e.teacher_id
left join srs_grade as f on f.grade_id=c.grade_id
left join cph_stu_extra_info g on g.stu_no=s.stu_no
</sql>
<select id="selectSrsStudentList" parameterType="SrsStudent" resultMap="SrsStudentResult">
<include refid="selectSrsStudentVo"/>

View File

@@ -81,6 +81,12 @@
<artifactId>aviator</artifactId>
<version>5.3.3</version>
</dependency>
<dependency>
<groupId>com.srs.dormitory</groupId>
<artifactId>dms-dormitory</artifactId>
<version>3.8.5</version>
<scope>compile</scope>
</dependency>
</dependencies>
<properties>
@@ -88,4 +94,4 @@
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
</project>

View File

@@ -3,6 +3,7 @@ package com.srs.flowable.listener.disqualification;
import com.srs.common.utils.DateUtils;
import com.srs.common.utils.SecurityUtils;
import com.srs.common.utils.spring.SpringUtils;
import com.srs.dormitory.domain.SrsDormitoryStudent;
import com.srs.flowable.domain.StuDisqualification;
import com.srs.flowable.domain.Student;
import com.srs.flowable.mapper.DisciplinaryMapper;
@@ -44,6 +45,12 @@ public class DisqualificationStatusListener implements TaskListener {
student.setUpdateTime(DateUtils.getNowDate());
disciplinaryMapper.updateStudentStatus(student);
disciplinaryMapper.updateStudentStatus(student);
// todo 辅导员给予退学1、宿舍
SrsDormitoryStudent dormitoryStudent=new SrsDormitoryStudent();
dormitoryStudent.setStuNo(disqualificationId.getStuNo());
disciplinaryMapper.deleteDormitoryStudent(dormitoryStudent);
//2、综测
//3、班级
}

View File

@@ -3,6 +3,7 @@ package com.srs.flowable.listener.dropOutSchoo;
import com.srs.common.utils.DateUtils;
import com.srs.common.utils.SecurityUtils;
import com.srs.common.utils.spring.SpringUtils;
import com.srs.dormitory.domain.SrsDormitoryStudent;
import com.srs.flowable.domain.StuDropOutSchool;
import com.srs.flowable.domain.StuQuitSchool;
import com.srs.flowable.domain.Student;
@@ -41,6 +42,11 @@ public class DropOutSchoolStatusListener implements TaskListener {
student.setUpdateBy(SecurityUtils.getUsername());
student.setUpdateTime(DateUtils.getNowDate());
disciplinaryMapper.updateStudentStatus(student);
// todo 学生自动退学:
SrsDormitoryStudent dormitoryStudent=new SrsDormitoryStudent();
dormitoryStudent.setStuNo(stuDropOutSchool.getStuNo());
disciplinaryMapper.deleteDormitoryStudent(dormitoryStudent);
}else{
throw new RuntimeException("该退学申请不存在");
}

View File

@@ -3,6 +3,7 @@ package com.srs.flowable.listener.quitSchool;
import com.srs.common.utils.DateUtils;
import com.srs.common.utils.SecurityUtils;
import com.srs.common.utils.spring.SpringUtils;
import com.srs.dormitory.domain.SrsDormitoryStudent;
import com.srs.flowable.domain.StuQuitSchool;
import com.srs.flowable.domain.Student;
import com.srs.flowable.mapper.DisciplinaryMapper;
@@ -39,6 +40,10 @@ public class QuitSchoolStatusListener implements TaskListener {
student.setUpdateBy(SecurityUtils.getUsername());
student.setUpdateTime(DateUtils.getNowDate());
disciplinaryMapper.updateStudentStatus(student);
// todo 学生自动休学:
SrsDormitoryStudent dormitoryStudent=new SrsDormitoryStudent();
dormitoryStudent.setStuNo(quitSchoolId.getStuNo());
disciplinaryMapper.deleteDormitoryStudent(dormitoryStudent);
} else {
throw new RuntimeException("该休学申请不存在");
}

View File

@@ -1,5 +1,6 @@
package com.srs.flowable.mapper;
import com.srs.dormitory.domain.SrsDormitoryStudent;
import com.srs.flowable.domain.StuDisciplinaryApplication;
import com.srs.flowable.domain.Student;
import org.apache.ibatis.annotations.Param;
@@ -39,6 +40,10 @@ public interface DisciplinaryMapper {
* @return
*/
int updateStudentStatus(Student student);
/***
* 清空宿舍
*/
int deleteDormitoryStudent(SrsDormitoryStudent srsDormitoryStudent);
}

View File

@@ -78,6 +78,9 @@
</trim>
where stu_no = #{stuNo}
</update>
<delete id="deleteDormitoryStudent" parameterType="SrsDormitoryStudent">
delete from srs_dormitory_student where stu_no=#{stuNo}
</delete>
<select id="getApprovalByDeptIdAndRoleKey" resultType="java.lang.Long">
SELECT a.user_id FROM sys_user a
@@ -99,4 +102,4 @@
</select>
</mapper>
</mapper>

View File

@@ -1,6 +1,7 @@
package com.srs.routine.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
@@ -15,7 +16,7 @@ import com.srs.routine.service.IRtClassMeetingClassService;
* @date 2025-04-08
*/
@Service
public class RtClassMeetingClassServiceImpl extends ServiceImpl<RtClassMeetingClassMapper,RtClassMeetingClass> implements IRtClassMeetingClassService {
public class RtClassMeetingClassServiceImpl extends ServiceImpl<RtClassMeetingClassMapper, RtClassMeetingClass> implements IRtClassMeetingClassService {
@Autowired
private RtClassMeetingClassMapper rtClassMeetingClassMapper;
@@ -49,7 +50,7 @@ public class RtClassMeetingClassServiceImpl extends ServiceImpl<RtClassMeetingCl
*/
@Override
public int insertRtClassMeetingClass(RtClassMeetingClass rtClassMeetingClass) {
return rtClassMeetingClassMapper.insertRtClassMeetingClass(rtClassMeetingClass);
return rtClassMeetingClassMapper.insertRtClassMeetingClass(rtClassMeetingClass);
}
/**

View File

@@ -0,0 +1,53 @@
package com.srs.teacher.domain.dto;
import java.util.Date;
public class ConversationDTO {
private String id; // 会话 ID
private String name; // 会话名称,默认由大语言模型生成。
private String introduction; // 开场白
private Date created_at; // 创建时间
private Date updated_at; // 更新时间
// Getters and Setters for the properties
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIntroduction() {
return introduction;
}
public void setIntroduction(String introduction) {
this.introduction = introduction;
}
public Date getCreated_at() {
return created_at;
}
public void setCreated_at(Date created_at) {
this.created_at = created_at;
}
public Date getUpdated_at() {
return updated_at;
}
public void setUpdated_at(Date updated_at) {
this.updated_at = updated_at;
}
}