guang
2023-05-06 56608e48288a97e59fb97340187f57f4a4fcf11e
提交 | 用户 | age
9e1c8d 1 package com.hx.other.service.controller;
G 2
3 import com.alibaba.fastjson.JSONObject;
4 import com.hx.common.BaseController;
5 import com.hx.other.service.api.ai.SqlAiApi;
6 import com.hx.other.service.model.BaseEntity;
7 import com.hx.other.service.model.QueryRecord;
8 import com.hx.other.service.service.QueryRecordService;
9 import com.hx.other.service.service.SqlQueryTempService;
10 import com.hx.other.service.util.AiQueryUtil;
11 import com.hx.other.service.vo.BaseVo;
12 import com.hx.other.service.vo.ai.SqlVo;
13 import com.hx.resultTool.Result;
14 import com.hx.util.StringUtils;
566411 15 import org.springframework.beans.factory.annotation.Autowired;
G 16 import org.springframework.beans.factory.annotation.Qualifier;
9e1c8d 17 import org.springframework.jdbc.core.JdbcTemplate;
G 18 import org.springframework.web.bind.annotation.PostMapping;
19 import org.springframework.web.bind.annotation.RequestBody;
20 import org.springframework.web.bind.annotation.RequestMapping;
21 import org.springframework.web.bind.annotation.RestController;
22
23 import javax.annotation.Resource;
24 import java.util.Calendar;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28
29 /**
30  * ai查询控制器
31  * @Author: cmg
32  * @Date: 2023/4/23 10:29
33  */
34 @RestController
35 @RequestMapping("/ai")
36 public class SqlQueryController extends BaseController {
37
38     @Resource
39     private SqlQueryTempService sqlQueryTempService;
40
566411 41     @Autowired
G 42     @Qualifier("readJdbcTemplate")
9e1c8d 43     private JdbcTemplate jdbcTemplate;
G 44
45     @Resource
46     private SqlAiApi sqlAiApi;
47
48     @Resource
49     private QueryRecordService queryRecordService;
50
51     @PostMapping("/query")
52     public Result query(@RequestBody(required = false) BaseVo baseVo)
53     {
54
6044ac 55         if(baseVo == null || StringUtils.isEmpty(baseVo.getKeyWord()))
9e1c8d 56         {
G 57             throwParamException("请输入查询条件");
58         }
59
60         QueryRecord queryRecord = new QueryRecord();
6044ac 61         queryRecord.setContent(baseVo.getKeyWord());
9e1c8d 62
G 63         //先从模板里匹配
6044ac 64         String sql = sqlQueryTempService.selectByContent(baseVo.getKeyWord());
9e1c8d 65         if(StringUtils.isEmpty(sql))
G 66         {
67             //记录向ai查询时的毫秒数
68             queryRecord.setQueryTime(Calendar.getInstance().getTimeInMillis());
69
70             //模板里没有,从ai里查询
71             JSONObject obj = new JSONObject();
6044ac 72             obj.put("msg", baseVo.getKeyWord());
9e1c8d 73             obj = AiQueryUtil.aiQuery(sqlAiApi, obj);
G 74
75             //记录ai返回的豪秒数
76             queryRecord.setAiResultTime(Calendar.getInstance().getTimeInMillis());
77
78             if(obj != null && 100 == obj.getIntValue("code"))
79             {
80                 //判断返回是否sql
81                 if(obj.getBooleanValue("isSql"))
82                 {
83                     //是sql
84                     sql = obj.getString("now");
85                     //记录ai返回的结果
86                     queryRecord.setAiResult(obj.getString("origin"));
87
88                 }
89
90                 //记录ai查询的content
91                 queryRecord.setAiQueryContent(obj.getString("content"));
92             }
93         }else{
94             queryRecord.setIsFromQuery(BaseEntity.YES);
95         }
96
97         List<Map<String, Object>> list = null;
98         if(!StringUtils.isEmpty(sql))
99         {
100             //记录查询的sql
101             queryRecord.setSqlStr(sql);
102
103             list =  AiQueryUtil.jdbcQuery(jdbcTemplate, sql);
104
105             //记录sql查询返回的豪秒数
106             queryRecord.setSqlTime(Calendar.getInstance().getTimeInMillis());
107         }
108
109         SqlVo sqlVo = new SqlVo();
56608e 110         sqlVo.setNow(sql);
9e1c8d 111         sqlVo.setOrigin(queryRecord.getAiResult());
G 112         sqlVo.setQueryContent(queryRecord.getAiQueryContent());
113         sqlVo.setIsArr(1);
114         sqlVo.setStatus(StringUtils.isEmpty(sql) ? 0 : 1);
115         sqlVo.setMsg(list == null ? "查无信息" : list);
116         sqlVo.setAiQueryTime(queryRecord.getQueryTime());
117         sqlVo.setAiResultTime(queryRecord.getAiResultTime());
118         sqlVo.setSqlResultTime(queryRecord.getSqlTime());
119
120         if(list != null)
121         {
122             //当返回是个统计数值时,直接返回数值
123             if(list.size() == 1)
124             {
125                 Set<String> set = list.get(0).keySet();
126                 if(set.size() == 1) {
127                     for (String key : set) {
128                         if (key.toUpperCase().indexOf("COUNT") != -1 || key.toUpperCase().indexOf("SUM") != -1) {
129                             sqlVo.setIsArr(0);
130                             sqlVo.setMsg(list.get(0).get(key));
131                         }
132                     }
133                 }
134             }
135         }
136
137         queryRecordService.insert(queryRecord);
138
139         return Result.success(sqlVo);
140     }
6044ac 141
G 142     @PostMapping("/query/test")
143     public Result queryTest(@RequestBody(required = false) BaseVo baseVo)
144     {
145
146         if(baseVo == null || StringUtils.isEmpty(baseVo.getKeyWord()))
147         {
148             throwParamException("请输入查询条件");
149         }
150
151         QueryRecord queryRecord = new QueryRecord();
152         queryRecord.setContent(baseVo.getKeyWord());
153
154         //先从模板里匹配
155         String sql = sqlQueryTempService.selectByContent(baseVo.getKeyWord());
156         if(StringUtils.isEmpty(sql))
157         {
158             //记录向ai查询时的毫秒数
159             queryRecord.setQueryTime(Calendar.getInstance().getTimeInMillis());
160
161             //模板里没有,从ai里查询
162             JSONObject obj = new JSONObject();
163             obj.put("msg", baseVo.getKeyWord());
164             obj = AiQueryUtil.aiQuery(sqlAiApi, obj);
165
166             //记录ai返回的豪秒数
167             queryRecord.setAiResultTime(Calendar.getInstance().getTimeInMillis());
168
169             if(obj != null && 100 == obj.getIntValue("code"))
170             {
171                 //判断返回是否sql
172                 if(obj.getBooleanValue("isSql"))
173                 {
174                     //是sql
175                     sql = obj.getString("now");
176                     //记录ai返回的结果
177                     queryRecord.setAiResult(obj.getString("origin"));
178
179                 }
180
181                 //记录ai查询的content
182                 queryRecord.setAiQueryContent(obj.getString("content"));
183             }
184         }else{
185             queryRecord.setIsFromQuery(BaseEntity.YES);
186         }
187
188         List<Map<String, Object>> list = null;
189         if(!StringUtils.isEmpty(sql))
190         {
191             //记录查询的sql
192             queryRecord.setSqlStr(sql);
193
194             list =  AiQueryUtil.jdbcQuery(jdbcTemplate, sql);
195
196             //记录sql查询返回的豪秒数
197             queryRecord.setSqlTime(Calendar.getInstance().getTimeInMillis());
198         }
199
200         SqlVo sqlVo = new SqlVo();
201         sqlVo.setIsArr(1);
202         sqlVo.setStatus(StringUtils.isEmpty(sql) ? 0 : 1);
203         sqlVo.setMsg(list == null ? "查无信息" : list);
204
205         if(list != null)
206         {
207             //当返回是个统计数值时,直接返回数值
208             if(list.size() == 1)
209             {
210                 Set<String> set = list.get(0).keySet();
211                 if(set.size() == 1) {
212                     for (String key : set) {
213                         if (key.toUpperCase().indexOf("COUNT") != -1 || key.toUpperCase().indexOf("SUM") != -1) {
214                             sqlVo.setIsArr(0);
215                             sqlVo.setMsg(list.get(0).get(key));
216                         }
217                     }
218                 }
219             }
220         }
221
222         queryRecordService.insert(queryRecord);
223
224         return Result.success(sqlVo);
225     }
9e1c8d 226 }