This commit is contained in:
2025-10-01 17:12:14 +08:00
parent e5961bba23
commit a08dc8b17d
3 changed files with 102 additions and 23 deletions

View File

@@ -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<String> 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<String> history = instructionService.getHistory(type, limit);
return AjaxResult.success(history);
}
}

View File

@@ -10,6 +10,14 @@ public interface IInstructionService {
* @return 执行结果文本列表(可能为单条或多条)
*/
java.util.List<String> execute(String command);
/**
* 获取历史消息记录
* @param type 消息类型request(请求) 或 response(响应)
* @param limit 获取数量默认100条
* @return 历史消息列表
*/
java.util.List<String> getHistory(String type, Integer limit);
}

View File

@@ -43,32 +43,93 @@ public class InstructionServiceImpl implements IInstructionService {
@Override
public List<String> execute(String command) {
// 存储接收的消息到Redis队列
storeMessageToRedis("instruction:request", command);
List<String> result;
if (command == null || command.trim().isEmpty()) {
return Collections.singletonList("请输入指令");
}
result = Collections.singletonList("请输入指令");
} else {
String input = command.trim();
// 一级命令分流:京系(统计/订单)、录单/慢单、转链/礼金…
if (input.startsWith("") || menuKeywords().contains(input)) {
return Collections.singletonList(handleJingFen(input.replaceFirst("^京", "")));
result = Collections.singletonList(handleJingFen(input.replaceFirst("^京", "")));
}
// TF/H/生 生成类指令
if (input.startsWith("TF")) {
return Collections.singletonList(handleTF(input));
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());
}
if (input.startsWith("H")) {
return Collections.singletonList(handleH(input));
}
if (input.startsWith("")) {
return Collections.singletonList(handleSheng(input));
// 存储返回的消息到Redis队列
String response = result != null ? String.join("\n", result) : "";
storeMessageToRedis("instruction:response", response);
return result;
}
if (input.startsWith("录单") || input.startsWith("慢单") || input.startsWith("慢搜") || input.startsWith("慢查") || input.startsWith("")) {
return handleRecordLikeMulti(input);
/**
* 将消息存储到Redis队列最多保留100条
* @param key Redis键
* @param message 消息内容
*/
private void storeMessageToRedis(String key, String message) {
if (stringRedisTemplate == null || message == null) {
return;
}
if (input.startsWith("转链") || input.startsWith("文案") || input.startsWith("")) {
return Collections.singletonList(handleTransferLike(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());
}
}
@Override
public List<String> getHistory(String type, Integer limit) {
if (stringRedisTemplate == null) {
return Collections.emptyList();
}
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<String> 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();
}
return Collections.singletonList(helpText());
}
private Set<String> menuKeywords() {