guang
2023-04-23 9e1c8d19bbca288b74464e90b75c9c1fe710ec1e
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
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.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;
 
    @Resource
    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.getContent()))
        {
            throwParamException("请输入查询条件");
        }
 
        QueryRecord queryRecord = new QueryRecord();
        queryRecord.setContent(baseVo.getContent());
 
        //先从模板里匹配
        String sql = sqlQueryTempService.selectByContent(baseVo.getContent());
        if(StringUtils.isEmpty(sql))
        {
            //记录向ai查询时的毫秒数
            queryRecord.setQueryTime(Calendar.getInstance().getTimeInMillis());
 
            //模板里没有,从ai里查询
            JSONObject obj = new JSONObject();
            obj.put("msg", baseVo.getContent());
            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.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);
    }
}