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