diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/jarvis/InstructionController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/jarvis/InstructionController.java index 550f86f..0f9ece5 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/jarvis/InstructionController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/jarvis/InstructionController.java @@ -3,10 +3,7 @@ package com.ruoyi.web.controller.jarvis; import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.jarvis.service.IInstructionService; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.util.Map; @@ -33,6 +30,19 @@ public class InstructionController extends BaseController { java.util.List result = instructionService.execute(cmd); return AjaxResult.success(result); } + + /** + * 获取历史消息记录 + * @param type 消息类型:request(请求) 或 response(响应) + * @param limit 获取数量,默认100条 + * @return 历史消息列表 + */ + @GetMapping("/history") + public AjaxResult getHistory(@RequestParam(required = false, defaultValue = "request") String type, + @RequestParam(required = false, defaultValue = "100") Integer limit) { + java.util.List history = instructionService.getHistory(type, limit); + return AjaxResult.success(history); + } } diff --git a/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/IInstructionService.java b/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/IInstructionService.java index 35ab32f..42c1102 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/IInstructionService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/IInstructionService.java @@ -10,6 +10,14 @@ public interface IInstructionService { * @return 执行结果文本列表(可能为单条或多条) */ java.util.List execute(String command); + + /** + * 获取历史消息记录 + * @param type 消息类型:request(请求) 或 response(响应) + * @param limit 获取数量,默认100条 + * @return 历史消息列表 + */ + java.util.List getHistory(String type, Integer limit); } diff --git a/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/impl/InstructionServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/impl/InstructionServiceImpl.java index 4af2e4b..05fc697 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/impl/InstructionServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/impl/InstructionServiceImpl.java @@ -43,32 +43,93 @@ public class InstructionServiceImpl implements IInstructionService { @Override public List execute(String command) { + // 存储接收的消息到Redis队列 + storeMessageToRedis("instruction:request", command); + + List result; + if (command == null || command.trim().isEmpty()) { - return Collections.singletonList("请输入指令"); - } - String input = command.trim(); + result = Collections.singletonList("请输入指令"); + } else { + String input = command.trim(); - // 一级命令分流:京系(统计/订单)、录单/慢单、转链/礼金… - if (input.startsWith("京") || menuKeywords().contains(input)) { - return Collections.singletonList(handleJingFen(input.replaceFirst("^京", ""))); + // 一级命令分流:京系(统计/订单)、录单/慢单、转链/礼金… + if (input.startsWith("京") || menuKeywords().contains(input)) { + result = Collections.singletonList(handleJingFen(input.replaceFirst("^京", ""))); + } + // TF/H/生 生成类指令 + else if (input.startsWith("TF")) { + result = Collections.singletonList(handleTF(input)); + } else if (input.startsWith("H")) { + result = Collections.singletonList(handleH(input)); + } else if (input.startsWith("生")) { + result = Collections.singletonList(handleSheng(input)); + } else if (input.startsWith("录单") || input.startsWith("慢单") || input.startsWith("慢搜") || input.startsWith("慢查") || input.startsWith("单")) { + result = handleRecordLikeMulti(input); + } else if (input.startsWith("转链") || input.startsWith("文案") || input.startsWith("转")) { + result = Collections.singletonList(handleTransferLike(input)); + } else { + result = Collections.singletonList(helpText()); + } } - // TF/H/生 生成类指令 - if (input.startsWith("TF")) { - return Collections.singletonList(handleTF(input)); + + // 存储返回的消息到Redis队列 + String response = result != null ? String.join("\n", result) : ""; + storeMessageToRedis("instruction:response", response); + + return result; + } + + /** + * 将消息存储到Redis队列,最多保留100条 + * @param key Redis键 + * @param message 消息内容 + */ + private void storeMessageToRedis(String key, String message) { + if (stringRedisTemplate == null || message == null) { + return; } - if (input.startsWith("H")) { - return Collections.singletonList(handleH(input)); + try { + // 添加时间戳 + String timestampedMessage = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " | " + message; + // 从左边推入消息(新消息在前) + stringRedisTemplate.opsForList().leftPush(key, timestampedMessage); + // 保持列表最多100条,删除最早的消息 + stringRedisTemplate.opsForList().trim(key, 0, 99); + } catch (Exception e) { + // 记录异常但不影响主流程 + System.err.println("存储消息到Redis失败: " + e.getMessage()); } - if (input.startsWith("生")) { - return Collections.singletonList(handleSheng(input)); + } + + @Override + public List getHistory(String type, Integer limit) { + if (stringRedisTemplate == null) { + return Collections.emptyList(); } - if (input.startsWith("录单") || input.startsWith("慢单") || input.startsWith("慢搜") || input.startsWith("慢查") || input.startsWith("单")) { - return handleRecordLikeMulti(input); + + try { + // 确定Redis键 + String key; + if ("request".equalsIgnoreCase(type)) { + key = "instruction:request"; + } else if ("response".equalsIgnoreCase(type)) { + key = "instruction:response"; + } else { + return Collections.emptyList(); + } + + // 确定获取数量,默认100条 + int count = (limit != null && limit > 0) ? Math.min(limit, 100) : 100; + + // 从Redis获取历史消息(索引0到count-1) + List messages = stringRedisTemplate.opsForList().range(key, 0, count - 1); + + return messages != null ? messages : Collections.emptyList(); + } catch (Exception e) { + System.err.println("获取历史消息失败: " + e.getMessage()); + return Collections.emptyList(); } - if (input.startsWith("转链") || input.startsWith("文案") || input.startsWith("转")) { - return Collections.singletonList(handleTransferLike(input)); - } - return Collections.singletonList(helpText()); } private Set menuKeywords() {