查询订单
This commit is contained in:
parent
de6f2bdba5
commit
399f88dae5
|
|
@ -143,6 +143,7 @@
|
||||||
<!-- </dependency>-->
|
<!-- </dependency>-->
|
||||||
|
|
||||||
<!-- LangChain4J 依赖 -->
|
<!-- LangChain4J 依赖 -->
|
||||||
|
<!-- Source: https://mvnrepository.com/artifact/dev.langchain4j/langchain4j -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>dev.langchain4j</groupId>
|
<groupId>dev.langchain4j</groupId>
|
||||||
<artifactId>langchain4j</artifactId>
|
<artifactId>langchain4j</artifactId>
|
||||||
|
|
|
||||||
|
|
@ -71,10 +71,17 @@ public class SseController {
|
||||||
try {
|
try {
|
||||||
// 使用AiService处理消息,传递模型参数
|
// 使用AiService处理消息,传递模型参数
|
||||||
String response = aiService.processMessage(message, model);
|
String response = aiService.processMessage(message, model);
|
||||||
|
log.info("==========AI回复:{}",response);
|
||||||
|
// 确保SSE消息格式正确,处理多行消息
|
||||||
|
String[] lines = response.split("\n");
|
||||||
|
StringBuilder formattedResponse = new StringBuilder();
|
||||||
|
for (String line : lines) {
|
||||||
|
formattedResponse.append("data:").append(line).append("\n");
|
||||||
|
}
|
||||||
|
formattedResponse.append("\n"); // 结束消息
|
||||||
|
|
||||||
emitter.send(SseEmitter.event()
|
emitter.send(formattedResponse.toString());
|
||||||
.name("message")
|
log.info("发送给前端的消息: {}", response);
|
||||||
.data(response));
|
|
||||||
|
|
||||||
return "消息发送成功";
|
return "消息发送成功";
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
package cn.qihangerp.erp.service;
|
package cn.qihangerp.erp.service;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
@ -8,6 +10,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||||
/**
|
/**
|
||||||
* 订单服务类,用于查询订单信息
|
* 订单服务类,用于查询订单信息
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
public class OrderService {
|
public class OrderService {
|
||||||
|
|
||||||
// 模拟订单数据
|
// 模拟订单数据
|
||||||
|
|
@ -20,6 +23,8 @@ public class OrderService {
|
||||||
orderMap.put("ORDER003", new Order("ORDER003", "2026-03-03", "王五", 599.99, "已完成"));
|
orderMap.put("ORDER003", new Order("ORDER003", "2026-03-03", "王五", 599.99, "已完成"));
|
||||||
orderMap.put("ORDER004", new Order("ORDER004", "2026-03-04", "赵六", 1999.99, "待发货"));
|
orderMap.put("ORDER004", new Order("ORDER004", "2026-03-04", "赵六", 1999.99, "待发货"));
|
||||||
orderMap.put("ORDER005", new Order("ORDER005", "2026-03-05", "钱七", 399.99, "已发货"));
|
orderMap.put("ORDER005", new Order("ORDER005", "2026-03-05", "钱七", 399.99, "已发货"));
|
||||||
|
orderMap.put("ORDER006", new Order("ORDER006", "2026-03-06", "齐", 399.99, "已发货"));
|
||||||
|
orderMap.put("ORDER007", new Order("ORDER007", "2026-03-07", "钱", 399.99, "未发货"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -62,6 +67,22 @@ public class OrderService {
|
||||||
return getOrdersByStatus("待发货");
|
return getOrdersByStatus("待发货");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据日期查询订单
|
||||||
|
* @param date 订单日期
|
||||||
|
* @return 订单列表
|
||||||
|
*/
|
||||||
|
public List<Order> getOrdersByDate(String date) {
|
||||||
|
log.info("=========根据日期查询订单:{}",date);
|
||||||
|
List<Order> orders = new ArrayList<>();
|
||||||
|
for (Order order : orderMap.values()) {
|
||||||
|
if (order.getOrderDate().equals(date)) {
|
||||||
|
orders.add(order);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return orders;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 订单实体类
|
* 订单实体类
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@ package cn.qihangerp.erp.service;
|
||||||
|
|
||||||
import dev.langchain4j.agent.tool.Tool;
|
import dev.langchain4j.agent.tool.Tool;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 订单工具服务,用于AI查询订单信息
|
* 订单工具服务,用于AI查询订单信息
|
||||||
|
|
@ -82,4 +84,32 @@ public class OrderToolService {
|
||||||
}
|
}
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据日期查询订单
|
||||||
|
* @param date 订单日期,可以是具体日期或"今天"、"昨天"、"明天"等时间表达式
|
||||||
|
* @return 订单信息
|
||||||
|
*/
|
||||||
|
@Tool("根据日期查询订单信息")
|
||||||
|
public String getOrdersByDate(String date) {
|
||||||
|
// 处理时间表达式
|
||||||
|
if ("今天".equals(date)) {
|
||||||
|
date = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
||||||
|
} else if ("昨天".equals(date)) {
|
||||||
|
date = LocalDate.now().minusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
||||||
|
} else if ("明天".equals(date)) {
|
||||||
|
date = LocalDate.now().plusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
||||||
|
}
|
||||||
|
|
||||||
|
List<OrderService.Order> orders = orderService.getOrdersByDate(date);
|
||||||
|
if (orders.isEmpty()) {
|
||||||
|
return "暂无日期为" + date + "的订单";
|
||||||
|
}
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
sb.append("日期为").append(date).append("的订单:<br>");
|
||||||
|
for (OrderService.Order order : orders) {
|
||||||
|
sb.append(order.toString()).append("<br>");
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,8 +1,11 @@
|
||||||
package cn.qihangerp.erp.serviceImpl;
|
package cn.qihangerp.erp.serviceImpl;
|
||||||
|
|
||||||
import dev.langchain4j.model.ollama.OllamaChatModel;
|
import dev.langchain4j.model.ollama.OllamaChatModel;
|
||||||
|
import dev.langchain4j.service.AiServices;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
import cn.qihangerp.erp.service.OrderToolService;
|
import cn.qihangerp.erp.service.OrderToolService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -11,6 +14,13 @@ import cn.qihangerp.erp.service.OrderToolService;
|
||||||
@Service
|
@Service
|
||||||
public class AiService {
|
public class AiService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 定义AI服务接口
|
||||||
|
*/
|
||||||
|
interface OrderAiService {
|
||||||
|
String chat(String message);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理聊天消息
|
* 处理聊天消息
|
||||||
* @param message 用户消息
|
* @param message 用户消息
|
||||||
|
|
@ -19,40 +29,35 @@ public class AiService {
|
||||||
*/
|
*/
|
||||||
public String processMessage(String message, String model) {
|
public String processMessage(String message, String model) {
|
||||||
try {
|
try {
|
||||||
// 检查是否是订单相关查询
|
// 获取当前日期
|
||||||
if (message.contains("订单")) {
|
String today = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
||||||
// 使用订单服务直接处理
|
|
||||||
OrderToolService orderToolService = new OrderToolService();
|
// 替换消息中的"今天"为具体日期
|
||||||
|
message = message.replace("今天", today);
|
||||||
// 简单的意图识别
|
|
||||||
if (message.contains("待发货") || message.contains("未发货")) {
|
// 根据模型名称创建对应的ChatModel
|
||||||
return orderToolService.getPendingOrders();
|
OllamaChatModel modelInstance = OllamaChatModel.builder()
|
||||||
} else if (message.contains("所有")) {
|
.baseUrl("http://localhost:11434") // Ollama默认端口
|
||||||
return orderToolService.getAllOrders();
|
.modelName(model) // 使用指定的模型
|
||||||
} else if (message.contains("状态")) {
|
.temperature(0.7)
|
||||||
// 提取状态
|
.timeout(Duration.ofSeconds(300)) // 超时时间设置为300秒(5分钟)
|
||||||
String status = message.replaceAll(".*状态为([^,。]+).*", "$1");
|
.build();
|
||||||
return orderToolService.getOrdersByStatus(status);
|
|
||||||
} else if (message.contains("订单号") || message.contains("订单ID")) {
|
// 创建订单工具服务
|
||||||
// 提取订单号
|
OrderToolService orderToolService = new OrderToolService();
|
||||||
String orderId = message.replaceAll(".*[订单号|订单ID][::]([^,。]+).*", "$1");
|
|
||||||
return orderToolService.getOrderById(orderId);
|
// 使用AiServices创建AI服务,自动处理工具调用
|
||||||
} else {
|
OrderAiService aiService = AiServices.builder(OrderAiService.class)
|
||||||
// 默认返回所有订单
|
.chatModel(modelInstance)
|
||||||
return orderToolService.getAllOrders();
|
.tools(orderToolService)
|
||||||
}
|
.build();
|
||||||
} else {
|
|
||||||
// 根据模型名称创建对应的ChatModel
|
// 执行AI服务,添加今天的日期信息
|
||||||
OllamaChatModel modelInstance = OllamaChatModel.builder()
|
String enhancedMessage = "今天的日期是:" + today + "\n" + message;
|
||||||
.baseUrl("http://localhost:11434") // Ollama默认端口
|
System.out.println("发送给AI的消息: " + enhancedMessage);
|
||||||
.modelName(model) // 使用指定的模型
|
String result = aiService.chat(enhancedMessage);
|
||||||
.temperature(0.7)
|
System.out.println("AI返回的结果: " + result);
|
||||||
.timeout(Duration.ofSeconds(300)) // 超时时间设置为300秒(5分钟)
|
return result;
|
||||||
.build();
|
|
||||||
|
|
||||||
// 调用Ollama模型获取回复
|
|
||||||
return modelInstance.chat(message);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return "错误: " + e.getMessage();
|
return "错误: " + e.getMessage();
|
||||||
|
|
|
||||||
|
|
@ -209,8 +209,12 @@ export default {
|
||||||
this.isLoading = false;
|
this.isLoading = false;
|
||||||
}
|
}
|
||||||
// 添加实际回复消息
|
// 添加实际回复消息
|
||||||
|
// 确保消息内容是完整的,处理多行消息
|
||||||
|
let messageContent = event.data;
|
||||||
|
// 移除可能的data:前缀
|
||||||
|
messageContent = messageContent.replace(/^data:/g, '');
|
||||||
this.messages.push({
|
this.messages.push({
|
||||||
content: event.data,
|
content: messageContent,
|
||||||
time: this.formatTime(new Date()),
|
time: this.formatTime(new Date()),
|
||||||
isMe: false,
|
isMe: false,
|
||||||
avatar: ''
|
avatar: ''
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue