guang
2023-05-06 56608e48288a97e59fb97340187f57f4a4fcf11e
src/main/java/com/hx/other/service/controller/SqlQueryController.java
@@ -12,6 +12,8 @@
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;
@@ -36,7 +38,8 @@
    @Resource
    private SqlQueryTempService sqlQueryTempService;
    @Resource
    @Autowired
    @Qualifier("readJdbcTemplate")
    private JdbcTemplate jdbcTemplate;
    @Resource
@@ -49,16 +52,16 @@
    public Result query(@RequestBody(required = false) BaseVo baseVo)
    {
        if(baseVo == null || StringUtils.isEmpty(baseVo.getContent()))
        if(baseVo == null || StringUtils.isEmpty(baseVo.getKeyWord()))
        {
            throwParamException("请输入查询条件");
        }
        QueryRecord queryRecord = new QueryRecord();
        queryRecord.setContent(baseVo.getContent());
        queryRecord.setContent(baseVo.getKeyWord());
        //先从模板里匹配
        String sql = sqlQueryTempService.selectByContent(baseVo.getContent());
        String sql = sqlQueryTempService.selectByContent(baseVo.getKeyWord());
        if(StringUtils.isEmpty(sql))
        {
            //记录向ai查询时的毫秒数
@@ -66,7 +69,7 @@
            //模板里没有,从ai里查询
            JSONObject obj = new JSONObject();
            obj.put("msg", baseVo.getContent());
            obj.put("msg", baseVo.getKeyWord());
            obj = AiQueryUtil.aiQuery(sqlAiApi, obj);
            //记录ai返回的豪秒数
@@ -104,6 +107,7 @@
        }
        SqlVo sqlVo = new SqlVo();
        sqlVo.setNow(sql);
        sqlVo.setOrigin(queryRecord.getAiResult());
        sqlVo.setQueryContent(queryRecord.getAiQueryContent());
        sqlVo.setIsArr(1);
@@ -134,4 +138,89 @@
        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);
    }
}