Commit 9a985fcaaa78837dd6cb17467a9ecd944e1f4cb8

Authored by qianbao
1 parent e40a3e40

AI 对于时间的处理

src/main/java/com/xly/agent/DynamicTableNl2SqlAiAgent.java
... ... @@ -33,6 +33,22 @@ public interface DynamicTableNl2SqlAiAgent {
33 33 5.1 严格按用户需求+传入的表结构生成,仅使用指定字段/表,无多余字段、无无效表关联、无冗余过滤条件;
34 34 5.2 用户需求中没有明确的日期条件,默认为全部数据,禁止增加任何日期过滤条件
35 35 6. 关联规则:多表关联时,必须使用外键/业务唯一键关联,禁止无意义关联。
  36 + 7. 当前时间:{{sDataNow}}
  37 + 8. 时间处理规则:
  38 + 8.1 当前系统时间:{{sDataNow}}(格式:yyyy年MM月dd日HH时mm分ss秒)
  39 + 8.2 用户需求中的相对时间概念,必须基于{{sDataNow}}进行转换:
  40 + - "本年" → 当前年份:{{sDataNow}}的年份
  41 + - "本月" → 当前月份:{{sDataNow}}的年份和月份
  42 + - "本季度" → 当前季度:基于{{sDataNow}}计算
  43 + - "本日/今天" → {{sDataNow}}的具体日期
  44 + - "昨天" → {{sDataNow}}减1天
  45 + - "本周" → 基于{{sDataNow}}计算周一到周日
  46 + - "近7天" → {{sDataNow}}减7天到{{sDataNow}}
  47 + 8.3 示例转换:
  48 + 当前时间:2024-03-15
  49 + 用户说"查询本年数据" → 查询条件应为:YEAR(日期字段) = 2024
  50 + 用户说"查询本月数据" → 查询条件应为:YEAR(日期字段) = 2024 AND MONTH(日期字段) = 3
  51 + 8.4 如果用户需求中没有明确的时间条件,禁止增加任何时间过滤条件
36 52 """)
37 53 @UserMessage("""
38 54 【业务场景表结构信息】
... ... @@ -41,7 +57,16 @@ public interface DynamicTableNl2SqlAiAgent {
41 57 当前时间:{{sDataNow}}
42 58 【用户需求】
43 59 {{userInput}}
44   - 请根据上述表结构+通用规则,生成符合要求的MySQL SELECT语句:
  60 + 请根据上述表结构+通用规则,生成符合要求的MySQL SELECT语句;
  61 + 【时间处理要求】
  62 + 1. 如果用户需求包含"本年/本月/本季度/本日/今天/昨天/本周/近X天"等相对时间概念:
  63 + - 必须使用当前系统时间 {{sDataNow}} 进行转换
  64 + - 转换为具体的年份、月份或日期范围
  65 + 2. 转换示例:
  66 + - "本年" → 年份 = {{sDataNow}}的年份部分
  67 + - "本月" → 年份 = {{sDataNow}}的年份, 月份 = {{sDataNow}}的月份
  68 + - "今天" → 日期 = {{sDataNow}}
  69 + 3. 如果没有明确的时间需求,不要添加任何时间过滤条件
45 70 """)
46 71 String generateMysqlSql(@MemoryId String userId,
47 72 @V("tableNames") String tableNames,
... ...
src/main/java/com/xly/service/XlyErpService.java
1 1 package com.xly.service;
2 2  
  3 +import cn.hutool.core.date.DatePattern;
3 4 import cn.hutool.core.date.DateUtil;
4 5 import cn.hutool.core.util.ObjectUtil;
5 6 import cn.hutool.core.util.StrUtil;
... ... @@ -32,7 +33,9 @@ import org.python.antlr.ast.Str;
32 33 import org.springframework.stereotype.Service;
33 34  
34 35 import java.math.BigDecimal;
  36 +import java.text.DateFormat;
35 37 import java.time.LocalDate;
  38 +import java.time.format.DateTimeFormatter;
36 39 import java.util.*;
37 40  
38 41 @Service
... ... @@ -245,7 +248,8 @@ public class XlyErpService {
245 248 String tableNames = session.getCurrentTool().getSInputTabelName();
246 249 // "订单表:viw_salsalesorder,客户信息表:elecustomer,结算方式表:sispayment,产品表(无单价,无金额,无数量):viw_product_sort,销售人员表:viw_sissalesman_depart";
247 250 String tableStruct = session.getCurrentTool().getSStructureMemo();
248   - String sDataNow = DateUtil.now();
  251 + String sDataNow = DateUtil.format(new Date(), DatePattern.CHINESE_DATE_TIME_FORMAT);
  252 + log.info("当前时间:"+sDataNow);
249 253 String rawSql = aiDynamicTableNl2SqlAiAgent.generateMysqlSql(userId,tableNames,tableStruct,sDataNow,userInput);
250 254 if (rawSql == null || rawSql.trim().isEmpty()) {
251 255 throw new SqlGenerateException("AI服务生成SQL失败,返回结果为空");
... ...