ludc
2025-01-15 d42f321ca241ab7f8bb3cdc71f11fe5ec4ebc48d
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
package com.vci.web.controller;
 
import com.alibaba.fastjson.JSONObject;
import com.vci.corba.common.PLException;
import com.vci.corba.query.data.KV;
import com.vci.starter.web.exception.VciBaseException;
import com.vci.starter.web.pagemodel.BaseResult;
import com.vci.web.util.PlatformClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * Description:利用sql转化表格的控制器
 *
 * @author: LiHang
 * @date: Created on 2022/1/20
 */
@RestController()
@RequestMapping("/sqlController")
public class Sql2TableController {
    @Autowired
    private PlatformClientUtil platformClientUtil;
 
    @GetMapping("/excute")
    public BaseResult excute(String sql,String oid){
        //返回结果集,分为key和value。key是列名。
        if (sql == null){
            throw  new VciBaseException("查询语句为空,请检查后再试");
        }else if (sql.contains("sql:")){
            sql = sql.replace("sql:","");
        }
 
        if (oid == null && sql.contains("?")){
            throw new VciBaseException("where语句没有赋值,请检查后重试");
        }else if (oid != null && sql.contains("?")){
            sql = sql.replace("?","'" + oid + "'");
        }
        Set<TableDisplay> columns = new HashSet<>();
        List<JSONObject> valueList = new ArrayList<>();
        KV[][] keyValues = new KV[0][0];
        try {
            keyValues =  platformClientUtil.getQueryService().queryBySql(sql);
        } catch ( PLException vciError) {
            vciError.printStackTrace();
            return BaseResult.fail("sql语句有问题,请检查后重试");
        }
        if (keyValues.length > 0){
            //1.第一层数组是该sql的查询结果有多少条
            for (KV kv : keyValues[0]) {
                TableDisplay column = new TableDisplay();
                column.setField(kv.key);
                column.setTitle(kv.key);
                columns.add(column);
            }
            for (KV[] kvs : keyValues) {
                //2.第二层数组是该行数据的具体列名和数据。
                Map<String, Object> dataMap = Arrays.stream(kvs).collect(Collectors.toMap(k -> k.key, v -> v.value));
                JSONObject json = new JSONObject(dataMap);
                valueList.add(json);
            }
        }
        BaseResult baseResult = new BaseResult();
        baseResult.setSuccess(true);
        baseResult.setData(valueList);
        baseResult.setObj(columns);
        return baseResult;
    }
 
    class TableDisplay  {
        private String field;
        private String title;
 
        public String getField() {
            return field;
        }
 
        public void setField(String field) {
            this.field = field;
        }
 
        public String getTitle() {
            return title;
        }
 
        public void setTitle(String title) {
            this.title = title;
        }
    }
}