|
|
package ESSqlService
|
|
|
|
|
|
import (
|
|
|
"dsDataex/MyReport/ESSql/ESSqlDAO"
|
|
|
"dsDataex/MyReport/MySwagger"
|
|
|
"dsDataex/Utils/ES7SqlUtil"
|
|
|
"encoding/json"
|
|
|
"reflect"
|
|
|
"strconv"
|
|
|
)
|
|
|
/**
|
|
|
* @Author zhangjun
|
|
|
* @Description
|
|
|
* @Date 2020-07-13 17:24
|
|
|
* @Param
|
|
|
* @return
|
|
|
**/
|
|
|
func QuerySimple(queryCode string, queryParam []string,queryGroup []string,queryCount []string,queryFormat string) (bool,string,string) {
|
|
|
|
|
|
res,msg,data,_ := ESSqlDAO.GetQuerybyCode(queryCode)
|
|
|
|
|
|
if res {
|
|
|
|
|
|
var essql= data["query_sql"].(string)
|
|
|
|
|
|
switch queryFormat {
|
|
|
case "table":
|
|
|
result:=ES7SqlUtil.SqlQueryTxt(essql,queryParam)
|
|
|
|
|
|
return true,"查询成功",result
|
|
|
|
|
|
break
|
|
|
case "json":
|
|
|
result:=ES7SqlUtil.SqlQueryJson(essql,queryParam)
|
|
|
|
|
|
bytes,_:=json.Marshal(result)
|
|
|
|
|
|
return true,"查询成功",string(bytes)
|
|
|
|
|
|
break
|
|
|
case "echarts":
|
|
|
result:=ES7SqlUtil.SqlQueryObj(essql,queryParam)
|
|
|
|
|
|
echarts := ConvEcharts(result, queryGroup, queryCount)
|
|
|
|
|
|
if len(queryGroup) <= 2 {
|
|
|
|
|
|
bytes, _ := json.Marshal(echarts)
|
|
|
return true, "查询成功,格式转换成功", string(bytes)
|
|
|
} else {
|
|
|
|
|
|
return false, "查询成功,格式转换失败,Echarts格式不支持三级GroupBy", ""
|
|
|
}
|
|
|
|
|
|
break
|
|
|
case "antd":
|
|
|
result := ES7SqlUtil.SqlQueryObj(essql, queryParam)
|
|
|
|
|
|
antd := ConvAntd(result)
|
|
|
|
|
|
bytes, _ := json.Marshal(antd)
|
|
|
|
|
|
return true, "查询成功,格式转换成功", string(bytes)
|
|
|
|
|
|
break
|
|
|
default:
|
|
|
break
|
|
|
}
|
|
|
|
|
|
return false, "查询Format参数错误,不支持此类型的数据格式转换", ""
|
|
|
} else {
|
|
|
return false, msg, ""
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @Author zhangjun
|
|
|
* @Description
|
|
|
* @Date 2020-07-13 17:24
|
|
|
* @Param
|
|
|
* @return
|
|
|
**/
|
|
|
func QueryJoin(queryCode string, queryParam []string,joinCode string,joinParam []string,queryGroup []string,queryCount []string,queryFormat string) (bool,string,string){
|
|
|
res,msg,data,_ := ESSqlDAO.GetQuerybyCode(queryCode)
|
|
|
res2,_,data2,_ := ESSqlDAO.GetQuerybyCode(joinCode)
|
|
|
|
|
|
if res && res2 {
|
|
|
|
|
|
var essql = data["query_sql"].(string)
|
|
|
var essql2 = data2["query_sql"].(string)
|
|
|
var join = data2["join_item"].(string)
|
|
|
|
|
|
result := ES7SqlUtil.SqlQueryObj(essql, queryParam)
|
|
|
result2 := ES7SqlUtil.SqlQueryObj(essql2, joinParam)
|
|
|
|
|
|
switch queryFormat {
|
|
|
case "table":
|
|
|
|
|
|
joinResult := JoinESTable(result, result2, join)
|
|
|
|
|
|
table := ConvTable(joinResult)
|
|
|
|
|
|
if len(joinResult.Rows) > 0 {
|
|
|
|
|
|
return true, "查询成功", table
|
|
|
} else {
|
|
|
return false, "没有查询到相关数据", ""
|
|
|
}
|
|
|
|
|
|
break
|
|
|
case "json":
|
|
|
|
|
|
joinResult := JoinESTable(result, result2, join)
|
|
|
|
|
|
if len(joinResult.Rows) > 0 {
|
|
|
|
|
|
bytes, _ := json.Marshal(joinResult)
|
|
|
|
|
|
return true, "查询成功", string(bytes)
|
|
|
} else {
|
|
|
return false, "没有查询到相关数据", ""
|
|
|
}
|
|
|
|
|
|
break
|
|
|
case "echarts":
|
|
|
joinResult := JoinESTable(result, result2, join)
|
|
|
|
|
|
echarts := ConvEcharts(joinResult, queryGroup, queryCount)
|
|
|
|
|
|
if len(queryGroup) <= 2 {
|
|
|
|
|
|
bytes, _ := json.Marshal(echarts)
|
|
|
return true, "查询成功,格式转换成功", string(bytes)
|
|
|
} else {
|
|
|
|
|
|
return false, "查询成功,格式转换失败,Echarts格式不支持三级GroupBy", ""
|
|
|
}
|
|
|
|
|
|
break
|
|
|
case "antd":
|
|
|
joinResult := JoinESTable(result, result2, join)
|
|
|
|
|
|
antd := ConvAntd(joinResult)
|
|
|
|
|
|
bytes, _ := json.Marshal(antd)
|
|
|
|
|
|
return true, "查询成功,格式转换成功", string(bytes)
|
|
|
|
|
|
break
|
|
|
}
|
|
|
|
|
|
return false, "查询Format参数错误,不支持此类型的数据格式转换", ""
|
|
|
} else {
|
|
|
return false, msg, ""
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @Author zhangjun
|
|
|
* @Description
|
|
|
* @Date 2020-07-13 17:25
|
|
|
* @Param
|
|
|
* @return
|
|
|
**/
|
|
|
func QueryUnion(queryList []MySwagger.Simple,joinList []MySwagger.Join,queryGroup []string,queryCount []string,queryFormat string) (bool,string,string){
|
|
|
|
|
|
var msg=""
|
|
|
var unionResult ES7SqlUtil.ESSqlResult
|
|
|
var joinResult ES7SqlUtil.ESSqlResult
|
|
|
|
|
|
for no:=0; no< len(queryList);no++{
|
|
|
|
|
|
res,_,data,_ := ESSqlDAO.GetQuerybyCode(queryList[no].QueryID)
|
|
|
|
|
|
if res {
|
|
|
|
|
|
var essql = data["query_sql"].(string)
|
|
|
|
|
|
result:=ES7SqlUtil.SqlQueryObj(essql,queryList[no].QueryParam)
|
|
|
|
|
|
if len(unionResult.Columns)==0{
|
|
|
unionResult.Columns=append(unionResult.Columns,result.Columns...)
|
|
|
}
|
|
|
|
|
|
unionResult.Rows=append(unionResult.Rows,result.Rows...)
|
|
|
|
|
|
}else {
|
|
|
msg=msg + "查询QueryID【"+queryList[no].QueryID+"】不存在\n"
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//TODO:目前只支持一级关联,多级关联的性能问题需要评估【代码很容易实现】zhangjun 2020-07-14
|
|
|
if len(joinList)>0{
|
|
|
|
|
|
res,_,data,_ := ESSqlDAO.GetQuerybyCode(joinList[0].JoinID)
|
|
|
|
|
|
if res {
|
|
|
|
|
|
var essql = data["query_sql"].(string)
|
|
|
var join = data["join_item"].(string)
|
|
|
|
|
|
result := ES7SqlUtil.SqlQueryObj(essql, joinList[0].JoinParam)
|
|
|
|
|
|
joinResult = JoinESTable(unionResult, result, join)
|
|
|
|
|
|
}else {
|
|
|
msg=msg + "查询JoinID【"+joinList[0].JoinID+"】不存在\n"
|
|
|
}
|
|
|
}
|
|
|
|
|
|
switch queryFormat {
|
|
|
case "table":
|
|
|
|
|
|
var table string
|
|
|
|
|
|
if len(joinResult.Rows)>0{
|
|
|
table = ConvTable(joinResult)
|
|
|
}else {
|
|
|
table = ConvTable(unionResult)
|
|
|
}
|
|
|
|
|
|
return true, "查询成功", table
|
|
|
|
|
|
break
|
|
|
case "json":
|
|
|
|
|
|
if len(joinResult.Rows)>0{
|
|
|
bytes, _ := json.Marshal(joinResult)
|
|
|
|
|
|
return true, "查询成功", string(bytes)
|
|
|
}else {
|
|
|
bytes, _ := json.Marshal(unionResult)
|
|
|
|
|
|
return true, "查询成功", string(bytes)
|
|
|
}
|
|
|
|
|
|
break
|
|
|
case "echarts":
|
|
|
|
|
|
var echarts ES7SqlUtil.EchartsResult
|
|
|
|
|
|
if len(joinResult.Rows)>0{
|
|
|
echarts = ConvEcharts(joinResult, queryGroup, queryCount)
|
|
|
}else {
|
|
|
echarts = ConvEcharts(unionResult, queryGroup, queryCount)
|
|
|
}
|
|
|
|
|
|
if len(queryGroup) <= 2 {
|
|
|
|
|
|
bytes, _ := json.Marshal(echarts)
|
|
|
return true, "查询成功,格式转换成功", string(bytes)
|
|
|
} else {
|
|
|
|
|
|
return false, "查询成功,格式转换失败,Echarts格式不支持三级GroupBy", ""
|
|
|
}
|
|
|
|
|
|
break
|
|
|
case "antd":
|
|
|
|
|
|
var antd ES7SqlUtil.AntdResult
|
|
|
|
|
|
if len(joinResult.Rows)>0{
|
|
|
antd = ConvAntd(unionResult)
|
|
|
}else {
|
|
|
antd = ConvAntd(unionResult)
|
|
|
}
|
|
|
|
|
|
bytes, _ := json.Marshal(antd)
|
|
|
|
|
|
return true, "查询成功,格式转换成功", string(bytes)
|
|
|
|
|
|
break
|
|
|
}
|
|
|
|
|
|
return false, "查询Format参数错误,不支持此类型的数据格式转换", ""
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @Author zhangjun
|
|
|
* @Description
|
|
|
* @Date 2020-07-13 17:24
|
|
|
* @Param
|
|
|
* @return
|
|
|
**/
|
|
|
func ConvTable(joinResult ES7SqlUtil.ESSqlResult) string {
|
|
|
var table string
|
|
|
var row1 string
|
|
|
var row2 string
|
|
|
var rows string
|
|
|
|
|
|
for no := 0; no < len(joinResult.Columns); no++ {
|
|
|
|
|
|
row1 = row1 + "|" + joinResult.Columns[no].Name
|
|
|
|
|
|
row2 = row2 + "---------------+"
|
|
|
}
|
|
|
|
|
|
for no := 0; no < len(joinResult.Rows); no++ {
|
|
|
for no2 := 0; no2 < len(joinResult.Rows[no]); no2++ {
|
|
|
|
|
|
switch joinResult.Rows[no][no2].(type) {
|
|
|
case int, int32, int64:
|
|
|
|
|
|
if joinResult.Rows[no][no2] == nil {
|
|
|
rows = rows + "0|"
|
|
|
} else {
|
|
|
rows = rows + strconv.Itoa(joinResult.Rows[no][no2].(int)) + "|"
|
|
|
}
|
|
|
case float64:
|
|
|
|
|
|
if joinResult.Rows[no][no2] == nil {
|
|
|
rows = rows + "0|"
|
|
|
} else {
|
|
|
rows = rows + strconv.FormatFloat(joinResult.Rows[no][no2].(float64), 'f', -1, 64) + "|"
|
|
|
}
|
|
|
default:
|
|
|
|
|
|
if joinResult.Rows[no][no2] == nil {
|
|
|
rows = rows + "|"
|
|
|
} else {
|
|
|
rows = rows + joinResult.Rows[no][no2].(string) + "|"
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
rows = rows + "\n"
|
|
|
}
|
|
|
|
|
|
table = row1 + "\n" + row2 + "\n" + rows
|
|
|
return table
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @Author zhangjun
|
|
|
* @Description
|
|
|
* @Date 2020-07-13 17:24
|
|
|
* @Param
|
|
|
* @return
|
|
|
**/
|
|
|
func ConvEcharts(result ES7SqlUtil.ESSqlResult, queryGroup []string, queryCount []string) ES7SqlUtil.EchartsResult {
|
|
|
var echarts ES7SqlUtil.EchartsResult
|
|
|
|
|
|
echarts.Rows = result.Rows
|
|
|
echarts.Columns = result.Columns
|
|
|
|
|
|
if len(queryGroup) == 1 {
|
|
|
|
|
|
var data []string
|
|
|
var data2 []string
|
|
|
var data3 []string
|
|
|
var data4 []string
|
|
|
var data5 []string
|
|
|
var group []string
|
|
|
var groupName = queryGroup[0]
|
|
|
var groupIndex = -1
|
|
|
var totalIndex = -1
|
|
|
var total2Index = -1
|
|
|
var total3Index = -1
|
|
|
var total4Index = -1
|
|
|
var total5Index = -1
|
|
|
|
|
|
if len(queryCount) == 0 {
|
|
|
queryCount = append(queryCount, "total")
|
|
|
}
|
|
|
|
|
|
for no := 0; no < len(result.Columns); no++ {
|
|
|
if result.Columns[no].Name == groupName {
|
|
|
groupIndex = no
|
|
|
}
|
|
|
if result.Columns[no].Name == queryCount[0] {
|
|
|
totalIndex = no
|
|
|
}
|
|
|
if len(queryCount) > 1 && result.Columns[no].Name == queryCount[1] {
|
|
|
total2Index = no
|
|
|
}
|
|
|
if len(queryCount) > 2 && result.Columns[no].Name == queryCount[2] {
|
|
|
total3Index = no
|
|
|
}
|
|
|
if len(queryCount) > 3 && result.Columns[no].Name == queryCount[3] {
|
|
|
total4Index = no
|
|
|
}
|
|
|
if len(queryCount) > 4 && result.Columns[no].Name == queryCount[4] {
|
|
|
total5Index = no
|
|
|
}
|
|
|
}
|
|
|
|
|
|
for no := 0; no < len(result.Rows); no++ {
|
|
|
|
|
|
if groupIndex > -1 {
|
|
|
switch result.Rows[no][groupIndex].(type) {
|
|
|
case int, int32, int64:
|
|
|
group = append(group, strconv.Itoa(result.Rows[no][groupIndex].(int)))
|
|
|
case float64:
|
|
|
group = append(group, strconv.FormatFloat(result.Rows[no][groupIndex].(float64), 'f', -1, 64))
|
|
|
default:
|
|
|
group = append(group, result.Rows[no][groupIndex].(string))
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if totalIndex > -1 {
|
|
|
switch result.Rows[no][totalIndex].(type) {
|
|
|
case int, int32, int64:
|
|
|
data = append(data, strconv.Itoa(result.Rows[no][totalIndex].(int)))
|
|
|
case float64:
|
|
|
data = append(data, strconv.FormatFloat(result.Rows[no][totalIndex].(float64), 'f', -1, 64))
|
|
|
default:
|
|
|
data = append(data, result.Rows[no][totalIndex].(string))
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if total2Index > -1 {
|
|
|
switch result.Rows[no][total2Index].(type) {
|
|
|
case int, int32, int64:
|
|
|
data2 = append(data2, strconv.Itoa(result.Rows[no][total2Index].(int)))
|
|
|
case float64:
|
|
|
data2 = append(data2, strconv.FormatFloat(result.Rows[no][total2Index].(float64), 'f', -1, 64))
|
|
|
default:
|
|
|
data2 = append(data2, result.Rows[no][total2Index].(string))
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if total3Index > -1 {
|
|
|
switch result.Rows[no][total3Index].(type) {
|
|
|
case int, int32, int64:
|
|
|
data3 = append(data3, strconv.Itoa(result.Rows[no][total3Index].(int)))
|
|
|
case float64:
|
|
|
data3 = append(data3, strconv.FormatFloat(result.Rows[no][total3Index].(float64), 'f', -1, 64))
|
|
|
default:
|
|
|
data3 = append(data3, result.Rows[no][total3Index].(string))
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if total4Index > -1 {
|
|
|
switch result.Rows[no][total4Index].(type) {
|
|
|
case int, int32, int64:
|
|
|
data4 = append(data4, strconv.Itoa(result.Rows[no][total4Index].(int)))
|
|
|
case float64:
|
|
|
data4 = append(data4, strconv.FormatFloat(result.Rows[no][total4Index].(float64), 'f', -1, 64))
|
|
|
default:
|
|
|
data4 = append(data4, result.Rows[no][total4Index].(string))
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if total5Index > -1 {
|
|
|
switch result.Rows[no][total5Index].(type) {
|
|
|
case int, int32, int64:
|
|
|
data5 = append(data5, strconv.Itoa(result.Rows[no][total5Index].(int)))
|
|
|
case float64:
|
|
|
data5 = append(data5, strconv.FormatFloat(result.Rows[no][total5Index].(float64), 'f', -1, 64))
|
|
|
default:
|
|
|
data5 = append(data5, result.Rows[no][total5Index].(string))
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
echarts.Groups = append(echarts.Groups, group)
|
|
|
echarts.Datas = append(echarts.Datas, data)
|
|
|
echarts.Datas2 = append(echarts.Datas2, data2)
|
|
|
echarts.Datas3 = append(echarts.Datas3, data3)
|
|
|
echarts.Datas4 = append(echarts.Datas4, data4)
|
|
|
echarts.Datas5 = append(echarts.Datas5, data5)
|
|
|
|
|
|
} else if len(queryGroup) == 2 {
|
|
|
|
|
|
var groupName1 = queryGroup[0]
|
|
|
var groupName2 = queryGroup[1]
|
|
|
var groupIndex1 = -1
|
|
|
var groupIndex2 = -1
|
|
|
var totalIndex = -1
|
|
|
var total2Index = -1
|
|
|
var total3Index = -1
|
|
|
var total4Index = -1
|
|
|
var total5Index = -1
|
|
|
|
|
|
if len(queryCount) == 0 {
|
|
|
queryCount = append(queryCount, "total")
|
|
|
}
|
|
|
|
|
|
for no := 0; no < len(result.Columns); no++ {
|
|
|
if result.Columns[no].Name == groupName1 {
|
|
|
groupIndex1 = no
|
|
|
}
|
|
|
if result.Columns[no].Name == groupName2 {
|
|
|
groupIndex2 = no
|
|
|
}
|
|
|
if result.Columns[no].Name == queryCount[0] {
|
|
|
totalIndex = no
|
|
|
}
|
|
|
if len(queryCount) > 1 && result.Columns[no].Name == queryCount[1] {
|
|
|
total2Index = no
|
|
|
}
|
|
|
if len(queryCount) > 2 && result.Columns[no].Name == queryCount[2] {
|
|
|
total3Index = no
|
|
|
}
|
|
|
if len(queryCount) > 3 && result.Columns[no].Name == queryCount[3] {
|
|
|
total4Index = no
|
|
|
}
|
|
|
if len(queryCount) > 4 && result.Columns[no].Name == queryCount[4] {
|
|
|
total5Index = no
|
|
|
}
|
|
|
}
|
|
|
|
|
|
var group1 []string
|
|
|
var group2 []string
|
|
|
|
|
|
//初始化分组1、2
|
|
|
for no := 0; no < len(result.Rows); no++ {
|
|
|
if groupIndex1 > -1 {
|
|
|
if Contains(group1, result.Rows[no][groupIndex1]) == -1 {
|
|
|
switch result.Rows[no][groupIndex1].(type) {
|
|
|
case int, int32, int64:
|
|
|
group1 = append(group1, strconv.Itoa(result.Rows[no][groupIndex1].(int)))
|
|
|
case float64:
|
|
|
group1 = append(group1, strconv.FormatFloat(result.Rows[no][groupIndex1].(float64), 'f', -1, 64))
|
|
|
default:
|
|
|
group1 = append(group1, result.Rows[no][groupIndex1].(string))
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if groupIndex2 > -1 {
|
|
|
if Contains(group2, result.Rows[no][groupIndex2]) == -1 {
|
|
|
switch result.Rows[no][groupIndex2].(type) {
|
|
|
case int, int32, int64:
|
|
|
group2 = append(group2, strconv.Itoa(result.Rows[no][groupIndex2].(int)))
|
|
|
case float64:
|
|
|
group2 = append(group2, strconv.FormatFloat(result.Rows[no][groupIndex2].(float64), 'f', -1, 64))
|
|
|
default:
|
|
|
group2 = append(group2, result.Rows[no][groupIndex2].(string))
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
echarts.Groups = append(echarts.Groups, group1)
|
|
|
echarts.Groups = append(echarts.Groups, group2)
|
|
|
|
|
|
var datas [][]string = make([][]string, len(group2))
|
|
|
var datas2 [][]string = make([][]string, len(group2))
|
|
|
var datas3 [][]string = make([][]string, len(group2))
|
|
|
var datas4 [][]string = make([][]string, len(group2))
|
|
|
var datas5 [][]string = make([][]string, len(group2))
|
|
|
|
|
|
for no := 0; no < len(group2); no++ {
|
|
|
datas[no] = make([]string, len(group1))
|
|
|
if total2Index > -1 {
|
|
|
datas2[no] = make([]string, len(group1))
|
|
|
}
|
|
|
if total3Index > -1 {
|
|
|
datas3[no] = make([]string, len(group1))
|
|
|
}
|
|
|
if total4Index > -1 {
|
|
|
datas4[no] = make([]string, len(group1))
|
|
|
}
|
|
|
if total5Index > -1 {
|
|
|
datas5[no] = make([]string, len(group1))
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//重组数据
|
|
|
for no := 0; no < len(result.Rows); no++ {
|
|
|
switch result.Rows[no][totalIndex].(type) {
|
|
|
case int, int32, int64:
|
|
|
|
|
|
if result.Rows[no][totalIndex] == nil {
|
|
|
datas[Contains(group2, result.Rows[no][groupIndex2])][Contains(group1, result.Rows[no][groupIndex1])] = "0"
|
|
|
} else {
|
|
|
datas[Contains(group2, result.Rows[no][groupIndex2])][Contains(group1, result.Rows[no][groupIndex1])] = strconv.Itoa(result.Rows[no][totalIndex].(int))
|
|
|
}
|
|
|
case float64:
|
|
|
|
|
|
if result.Rows[no][totalIndex] == nil {
|
|
|
datas[Contains(group2, result.Rows[no][groupIndex2])][Contains(group1, result.Rows[no][groupIndex1])] = "0"
|
|
|
} else {
|
|
|
datas[Contains(group2, result.Rows[no][groupIndex2])][Contains(group1, result.Rows[no][groupIndex1])] = strconv.FormatFloat(result.Rows[no][totalIndex].(float64), 'f', -1, 64)
|
|
|
}
|
|
|
default:
|
|
|
|
|
|
if result.Rows[no][totalIndex] == nil {
|
|
|
datas[Contains(group2, result.Rows[no][groupIndex2])][Contains(group1, result.Rows[no][groupIndex1])] = "0"
|
|
|
} else {
|
|
|
datas[Contains(group2, result.Rows[no][groupIndex2])][Contains(group1, result.Rows[no][groupIndex1])] = result.Rows[no][totalIndex].(string)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if total2Index > -1 {
|
|
|
switch result.Rows[no][total2Index].(type) {
|
|
|
case int, int32, int64:
|
|
|
|
|
|
if result.Rows[no][total2Index] == nil {
|
|
|
datas2[Contains(group2, result.Rows[no][groupIndex2])][Contains(group1, result.Rows[no][groupIndex1])] = "0"
|
|
|
} else {
|
|
|
datas2[Contains(group2, result.Rows[no][groupIndex2])][Contains(group1, result.Rows[no][groupIndex1])] = strconv.Itoa(result.Rows[no][total2Index].(int))
|
|
|
}
|
|
|
case float64:
|
|
|
|
|
|
if result.Rows[no][total2Index] == nil {
|
|
|
datas2[Contains(group2, result.Rows[no][groupIndex2])][Contains(group1, result.Rows[no][groupIndex1])] = "0"
|
|
|
} else {
|
|
|
datas2[Contains(group2, result.Rows[no][groupIndex2])][Contains(group1, result.Rows[no][groupIndex1])] = strconv.FormatFloat(result.Rows[no][total2Index].(float64), 'f', -1, 64)
|
|
|
}
|
|
|
default:
|
|
|
|
|
|
if result.Rows[no][total2Index] == nil {
|
|
|
datas2[Contains(group2, result.Rows[no][groupIndex2])][Contains(group1, result.Rows[no][groupIndex1])] = "0"
|
|
|
} else {
|
|
|
datas2[Contains(group2, result.Rows[no][groupIndex2])][Contains(group1, result.Rows[no][groupIndex1])] = result.Rows[no][total2Index].(string)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if total3Index > -1 {
|
|
|
switch result.Rows[no][total3Index].(type) {
|
|
|
case int, int32, int64:
|
|
|
|
|
|
if result.Rows[no][total3Index] == nil {
|
|
|
datas3[Contains(group2, result.Rows[no][groupIndex2])][Contains(group1, result.Rows[no][groupIndex1])] = "0"
|
|
|
} else {
|
|
|
datas3[Contains(group2, result.Rows[no][groupIndex2])][Contains(group1, result.Rows[no][groupIndex1])] = strconv.Itoa(result.Rows[no][total3Index].(int))
|
|
|
}
|
|
|
case float64:
|
|
|
|
|
|
if result.Rows[no][total3Index] == nil {
|
|
|
datas3[Contains(group2, result.Rows[no][groupIndex2])][Contains(group1, result.Rows[no][groupIndex1])] = "0"
|
|
|
} else {
|
|
|
datas3[Contains(group2, result.Rows[no][groupIndex2])][Contains(group1, result.Rows[no][groupIndex1])] = strconv.FormatFloat(result.Rows[no][total3Index].(float64), 'f', -1, 64)
|
|
|
}
|
|
|
default:
|
|
|
|
|
|
if result.Rows[no][total3Index] == nil {
|
|
|
datas3[Contains(group2, result.Rows[no][groupIndex2])][Contains(group1, result.Rows[no][groupIndex1])] = "0"
|
|
|
} else {
|
|
|
datas3[Contains(group2, result.Rows[no][groupIndex2])][Contains(group1, result.Rows[no][groupIndex1])] = result.Rows[no][total3Index].(string)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if total4Index > -1 {
|
|
|
switch result.Rows[no][total4Index].(type) {
|
|
|
case int, int32, int64:
|
|
|
|
|
|
if result.Rows[no][total4Index] == nil {
|
|
|
datas4[Contains(group2, result.Rows[no][groupIndex2])][Contains(group1, result.Rows[no][groupIndex1])] = "0"
|
|
|
} else {
|
|
|
datas4[Contains(group2, result.Rows[no][groupIndex2])][Contains(group1, result.Rows[no][groupIndex1])] = strconv.Itoa(result.Rows[no][total4Index].(int))
|
|
|
}
|
|
|
case float64:
|
|
|
|
|
|
if result.Rows[no][total4Index] == nil {
|
|
|
datas4[Contains(group2, result.Rows[no][groupIndex2])][Contains(group1, result.Rows[no][groupIndex1])] = "0"
|
|
|
} else {
|
|
|
datas4[Contains(group2, result.Rows[no][groupIndex2])][Contains(group1, result.Rows[no][groupIndex1])] = strconv.FormatFloat(result.Rows[no][total4Index].(float64), 'f', -1, 64)
|
|
|
}
|
|
|
default:
|
|
|
|
|
|
if result.Rows[no][total4Index] == nil {
|
|
|
datas4[Contains(group2, result.Rows[no][groupIndex2])][Contains(group1, result.Rows[no][groupIndex1])] = "0"
|
|
|
} else {
|
|
|
datas4[Contains(group2, result.Rows[no][groupIndex2])][Contains(group1, result.Rows[no][groupIndex1])] = result.Rows[no][total4Index].(string)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if total5Index > -1 {
|
|
|
switch result.Rows[no][total5Index].(type) {
|
|
|
case int, int32, int64:
|
|
|
|
|
|
if result.Rows[no][total5Index] == nil {
|
|
|
datas5[Contains(group2, result.Rows[no][groupIndex2])][Contains(group1, result.Rows[no][groupIndex1])] = "0"
|
|
|
} else {
|
|
|
datas5[Contains(group2, result.Rows[no][groupIndex2])][Contains(group1, result.Rows[no][groupIndex1])] = strconv.Itoa(result.Rows[no][total5Index].(int))
|
|
|
}
|
|
|
case float64:
|
|
|
|
|
|
if result.Rows[no][total5Index] == nil {
|
|
|
datas5[Contains(group2, result.Rows[no][groupIndex2])][Contains(group1, result.Rows[no][groupIndex1])] = "0"
|
|
|
} else {
|
|
|
datas5[Contains(group2, result.Rows[no][groupIndex2])][Contains(group1, result.Rows[no][groupIndex1])] = strconv.FormatFloat(result.Rows[no][total5Index].(float64), 'f', -1, 64)
|
|
|
}
|
|
|
default:
|
|
|
|
|
|
if result.Rows[no][total5Index] == nil {
|
|
|
datas5[Contains(group2, result.Rows[no][groupIndex2])][Contains(group1, result.Rows[no][groupIndex1])] = "0"
|
|
|
} else {
|
|
|
datas5[Contains(group2, result.Rows[no][groupIndex2])][Contains(group1, result.Rows[no][groupIndex1])] = result.Rows[no][total5Index].(string)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
echarts.Datas = datas
|
|
|
if total2Index > -1 {
|
|
|
echarts.Datas2 = datas2
|
|
|
}
|
|
|
if total3Index > -1 {
|
|
|
echarts.Datas3 = datas3
|
|
|
}
|
|
|
if total4Index > -1 {
|
|
|
echarts.Datas4 = datas4
|
|
|
}
|
|
|
if total5Index > -1 {
|
|
|
echarts.Datas5 = datas5
|
|
|
}
|
|
|
|
|
|
//bytes,_:=json.Marshal(echarts)
|
|
|
//return true,"查询成功,格式转换成功",string(bytes)
|
|
|
|
|
|
} else {
|
|
|
//bytes,_:=json.Marshal(result)
|
|
|
//return false,"查询成功,格式转换失败,不支持三级GroupBy","" //string(bytes)
|
|
|
}
|
|
|
return echarts
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @Author zhangjun
|
|
|
* @Description
|
|
|
* @Date 2020-07-13 17:24
|
|
|
* @Param
|
|
|
* @return
|
|
|
**/
|
|
|
func ConvAntd(result ES7SqlUtil.ESSqlResult) ES7SqlUtil.AntdResult {
|
|
|
var antd ES7SqlUtil.AntdResult
|
|
|
|
|
|
antd.Rows = result.Rows
|
|
|
antd.Columns = result.Columns
|
|
|
|
|
|
for no := 0; no < len(result.Rows); no++ {
|
|
|
|
|
|
var jsonRow = make(map[string]interface{})
|
|
|
|
|
|
for no2 := 0; no2 < len(result.Columns); no2++ {
|
|
|
|
|
|
switch result.Rows[no][no2].(type) {
|
|
|
case int, int32, int64:
|
|
|
|
|
|
if result.Rows[no][no2] == nil {
|
|
|
jsonRow[result.Columns[no2].Name] = 0
|
|
|
} else {
|
|
|
jsonRow[result.Columns[no2].Name] = result.Rows[no][no2].(int)
|
|
|
}
|
|
|
case float64:
|
|
|
|
|
|
if result.Rows[no][no2] == nil {
|
|
|
jsonRow[result.Columns[no2].Name] = 0
|
|
|
} else {
|
|
|
jsonRow[result.Columns[no2].Name] = result.Rows[no][no2].(float64)
|
|
|
}
|
|
|
default:
|
|
|
|
|
|
if result.Rows[no][no2] == nil {
|
|
|
jsonRow[result.Columns[no2].Name] = ""
|
|
|
} else {
|
|
|
jsonRow[result.Columns[no2].Name] = result.Rows[no][no2].(string)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
antd.Datas = append(antd.Datas, jsonRow)
|
|
|
}
|
|
|
return antd
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @Author zhangjun
|
|
|
* @Description
|
|
|
* @Date 2020-07-13 17:24
|
|
|
* @Param
|
|
|
* @return
|
|
|
**/
|
|
|
func Contains(array interface{}, val interface{}) (index int) {
|
|
|
index = -1
|
|
|
switch reflect.TypeOf(array).Kind() {
|
|
|
case reflect.Slice: {
|
|
|
s := reflect.ValueOf(array)
|
|
|
for i := 0; i < s.Len(); i++ {
|
|
|
if reflect.DeepEqual(val, s.Index(i).Interface()) {
|
|
|
index = i
|
|
|
return index
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return index
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @Author zhangjun
|
|
|
* @Description
|
|
|
* @Date 2020-07-13 17:25
|
|
|
* @Param
|
|
|
* @return
|
|
|
**/
|
|
|
func JoinESTable(result ES7SqlUtil.ESSqlResult, result2 ES7SqlUtil.ESSqlResult, join string) ES7SqlUtil.ESSqlResult {
|
|
|
var joinResult ES7SqlUtil.ESSqlResult
|
|
|
|
|
|
if len(result.Rows) > 0 && len(result2.Rows) > 0 {
|
|
|
|
|
|
//map[join_key][]join_data
|
|
|
var joinTable = make(map[string][]interface{})
|
|
|
//map[join_key]join_count
|
|
|
var joinKeys = make(map[string]int)
|
|
|
|
|
|
var emptyResult []interface{}
|
|
|
|
|
|
var index = -1
|
|
|
var index2 = -1
|
|
|
|
|
|
//joinResult.Columns=make([]ES7SqlUtil.ESSqlColumn, len(result.Columns)+len(result2.Columns))
|
|
|
|
|
|
for no := 0; no < len(result.Columns); no++ {
|
|
|
var column = result.Columns[no]
|
|
|
joinResult.Columns = append(joinResult.Columns, column)
|
|
|
|
|
|
if result.Columns[no].Name == join {
|
|
|
index = no
|
|
|
}
|
|
|
|
|
|
switch result.Columns[no].Type {
|
|
|
case "float":
|
|
|
emptyResult = append(emptyResult, 0)
|
|
|
break
|
|
|
case "keyword":
|
|
|
emptyResult = append(emptyResult, "")
|
|
|
break
|
|
|
case "date":
|
|
|
emptyResult = append(emptyResult, "")
|
|
|
break
|
|
|
case "boolean":
|
|
|
emptyResult = append(emptyResult, false)
|
|
|
break
|
|
|
case "long":
|
|
|
emptyResult = append(emptyResult, 0)
|
|
|
break
|
|
|
}
|
|
|
}
|
|
|
|
|
|
for no := 0; no < len(result2.Columns); no++ {
|
|
|
var column ES7SqlUtil.ESSqlColumn
|
|
|
|
|
|
column.Name = "data_join." + result2.Columns[no].Name
|
|
|
column.Type = result2.Columns[no].Type
|
|
|
|
|
|
joinResult.Columns = append(joinResult.Columns, column)
|
|
|
|
|
|
if result2.Columns[no].Name == join {
|
|
|
index2 = no
|
|
|
}
|
|
|
}
|
|
|
|
|
|
for no := 0; no < len(result2.Rows); no++ {
|
|
|
|
|
|
joinTable[result2.Rows[no][index2].(string)] = result2.Rows[no]
|
|
|
|
|
|
joinKeys[result2.Rows[no][index2].(string)] = 0
|
|
|
}
|
|
|
|
|
|
for no := 0; no < len(result.Rows); no++ {
|
|
|
|
|
|
_, flag := joinKeys[result.Rows[no][index].(string)]
|
|
|
|
|
|
var rows []interface{}
|
|
|
|
|
|
if flag {
|
|
|
|
|
|
//rows=append(rows,result.Rows[no])
|
|
|
//rows=append(rows,joinTable[result.Rows[no][index].(string)])
|
|
|
|
|
|
rows = append(rows, result.Rows[no]...)
|
|
|
rows = append(rows, joinTable[result.Rows[no][index].(string)]...)
|
|
|
|
|
|
joinResult.Rows = append(joinResult.Rows, rows)
|
|
|
|
|
|
joinKeys[result.Rows[no][index].(string)]++
|
|
|
}
|
|
|
}
|
|
|
|
|
|
for k, v := range joinKeys {
|
|
|
if v == 0 {
|
|
|
var rows []interface{}
|
|
|
|
|
|
rows = append(rows, emptyResult...)
|
|
|
|
|
|
rows = append(rows, joinTable[k]...)
|
|
|
|
|
|
joinResult.Rows = append(joinResult.Rows, rows)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//bytes,_:=json.Marshal(joinResult)
|
|
|
//return true, "查询成功", string(bytes)
|
|
|
}
|
|
|
return joinResult
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @Author zhangjun
|
|
|
* @Description 根据用户ID和身份,获取用户详细信息
|
|
|
* @Date 2020-07-30 08:07
|
|
|
* @Param
|
|
|
* @return
|
|
|
**/
|
|
|
func GetUser4Kafka(userID string,userIdentity string) (bool,[]map[string]interface{}){
|
|
|
|
|
|
//change by zhangjun 2020-08-05
|
|
|
//不按照身份绑定分类,在数据库中自定义查询解耦各个版本基础数据的身份不同问题。
|
|
|
//1 教师,2 学生,3 家长,4 管理员,5 访客--不适用!!!
|
|
|
switch userIdentity {
|
|
|
case "1":
|
|
|
res,_,data,_ := ESSqlDAO.GetQuerybyCode("kafka_user_1")
|
|
|
|
|
|
if res {
|
|
|
|
|
|
var essql = data["query_sql"].(string)
|
|
|
var queryParam=[]string{userID}
|
|
|
|
|
|
result := ES7SqlUtil.SqlQueryObj(essql, queryParam)
|
|
|
|
|
|
antd := ConvAntd(result)
|
|
|
|
|
|
return true,antd.Datas
|
|
|
}
|
|
|
break
|
|
|
case "2":
|
|
|
res,_,data,_ := ESSqlDAO.GetQuerybyCode("kafka_user_2")
|
|
|
|
|
|
if res {
|
|
|
|
|
|
var essql = data["query_sql"].(string)
|
|
|
var queryParam=[]string{userID}
|
|
|
|
|
|
result := ES7SqlUtil.SqlQueryObj(essql, queryParam)
|
|
|
|
|
|
antd := ConvAntd(result)
|
|
|
|
|
|
return true,antd.Datas
|
|
|
}
|
|
|
break
|
|
|
case "3":
|
|
|
res,_,data,_ := ESSqlDAO.GetQuerybyCode("kafka_user_3")
|
|
|
|
|
|
if res {
|
|
|
|
|
|
var essql = data["query_sql"].(string)
|
|
|
var queryParam=[]string{userID}
|
|
|
|
|
|
result := ES7SqlUtil.SqlQueryObj(essql, queryParam)
|
|
|
|
|
|
antd := ConvAntd(result)
|
|
|
|
|
|
return true,antd.Datas
|
|
|
}
|
|
|
break
|
|
|
case "4":
|
|
|
res,_,data,_ := ESSqlDAO.GetQuerybyCode("kafka_user_4")
|
|
|
|
|
|
if res {
|
|
|
|
|
|
var essql = data["query_sql"].(string)
|
|
|
var queryParam=[]string{userID}
|
|
|
|
|
|
result := ES7SqlUtil.SqlQueryObj(essql, queryParam)
|
|
|
|
|
|
antd := ConvAntd(result)
|
|
|
|
|
|
return true,antd.Datas
|
|
|
}
|
|
|
break
|
|
|
case "5":
|
|
|
res,_,data,_ := ESSqlDAO.GetQuerybyCode("kafka_user_5")
|
|
|
|
|
|
if res {
|
|
|
|
|
|
var essql = data["query_sql"].(string)
|
|
|
var queryParam=[]string{userID}
|
|
|
|
|
|
result := ES7SqlUtil.SqlQueryObj(essql, queryParam)
|
|
|
|
|
|
antd := ConvAntd(result)
|
|
|
|
|
|
return true,antd.Datas
|
|
|
}
|
|
|
break
|
|
|
}
|
|
|
|
|
|
return false,nil
|
|
|
}
|