Commit 9a985fcaaa78837dd6cb17467a9ecd944e1f4cb8
1 parent
e40a3e40
AI 对于时间的处理
Showing
2 changed files
with
31 additions
and
2 deletions
src/main/java/com/xly/agent/DynamicTableNl2SqlAiAgent.java
| @@ -33,6 +33,22 @@ public interface DynamicTableNl2SqlAiAgent { | @@ -33,6 +33,22 @@ public interface DynamicTableNl2SqlAiAgent { | ||
| 33 | 5.1 严格按用户需求+传入的表结构生成,仅使用指定字段/表,无多余字段、无无效表关联、无冗余过滤条件; | 33 | 5.1 严格按用户需求+传入的表结构生成,仅使用指定字段/表,无多余字段、无无效表关联、无冗余过滤条件; |
| 34 | 5.2 用户需求中没有明确的日期条件,默认为全部数据,禁止增加任何日期过滤条件 | 34 | 5.2 用户需求中没有明确的日期条件,默认为全部数据,禁止增加任何日期过滤条件 |
| 35 | 6. 关联规则:多表关联时,必须使用外键/业务唯一键关联,禁止无意义关联。 | 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 | @UserMessage(""" | 53 | @UserMessage(""" |
| 38 | 【业务场景表结构信息】 | 54 | 【业务场景表结构信息】 |
| @@ -41,7 +57,16 @@ public interface DynamicTableNl2SqlAiAgent { | @@ -41,7 +57,16 @@ public interface DynamicTableNl2SqlAiAgent { | ||
| 41 | 当前时间:{{sDataNow}} | 57 | 当前时间:{{sDataNow}} |
| 42 | 【用户需求】 | 58 | 【用户需求】 |
| 43 | {{userInput}} | 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 | String generateMysqlSql(@MemoryId String userId, | 71 | String generateMysqlSql(@MemoryId String userId, |
| 47 | @V("tableNames") String tableNames, | 72 | @V("tableNames") String tableNames, |
src/main/java/com/xly/service/XlyErpService.java
| 1 | package com.xly.service; | 1 | package com.xly.service; |
| 2 | 2 | ||
| 3 | +import cn.hutool.core.date.DatePattern; | ||
| 3 | import cn.hutool.core.date.DateUtil; | 4 | import cn.hutool.core.date.DateUtil; |
| 4 | import cn.hutool.core.util.ObjectUtil; | 5 | import cn.hutool.core.util.ObjectUtil; |
| 5 | import cn.hutool.core.util.StrUtil; | 6 | import cn.hutool.core.util.StrUtil; |
| @@ -32,7 +33,9 @@ import org.python.antlr.ast.Str; | @@ -32,7 +33,9 @@ import org.python.antlr.ast.Str; | ||
| 32 | import org.springframework.stereotype.Service; | 33 | import org.springframework.stereotype.Service; |
| 33 | 34 | ||
| 34 | import java.math.BigDecimal; | 35 | import java.math.BigDecimal; |
| 36 | +import java.text.DateFormat; | ||
| 35 | import java.time.LocalDate; | 37 | import java.time.LocalDate; |
| 38 | +import java.time.format.DateTimeFormatter; | ||
| 36 | import java.util.*; | 39 | import java.util.*; |
| 37 | 40 | ||
| 38 | @Service | 41 | @Service |
| @@ -245,7 +248,8 @@ public class XlyErpService { | @@ -245,7 +248,8 @@ public class XlyErpService { | ||
| 245 | String tableNames = session.getCurrentTool().getSInputTabelName(); | 248 | String tableNames = session.getCurrentTool().getSInputTabelName(); |
| 246 | // "订单表:viw_salsalesorder,客户信息表:elecustomer,结算方式表:sispayment,产品表(无单价,无金额,无数量):viw_product_sort,销售人员表:viw_sissalesman_depart"; | 249 | // "订单表:viw_salsalesorder,客户信息表:elecustomer,结算方式表:sispayment,产品表(无单价,无金额,无数量):viw_product_sort,销售人员表:viw_sissalesman_depart"; |
| 247 | String tableStruct = session.getCurrentTool().getSStructureMemo(); | 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 | String rawSql = aiDynamicTableNl2SqlAiAgent.generateMysqlSql(userId,tableNames,tableStruct,sDataNow,userInput); | 253 | String rawSql = aiDynamicTableNl2SqlAiAgent.generateMysqlSql(userId,tableNames,tableStruct,sDataNow,userInput); |
| 250 | if (rawSql == null || rawSql.trim().isEmpty()) { | 254 | if (rawSql == null || rawSql.trim().isEmpty()) { |
| 251 | throw new SqlGenerateException("AI服务生成SQL失败,返回结果为空"); | 255 | throw new SqlGenerateException("AI服务生成SQL失败,返回结果为空"); |