数据集

This commit is contained in:
XM-GO
2023-05-13 12:59:08 +08:00
parent c7cd18131d
commit c9b8dc0f01
4 changed files with 161 additions and 8 deletions

View File

@@ -146,11 +146,89 @@ func (p *VisualDataSetTableApi) GetDataSetTableFun(rc *restfulx.ReqCtx) {
rc.ResData = p.VisualDataSetTableApp.FindFunList(one.SourceType) rc.ResData = p.VisualDataSetTableApp.FindFunList(one.SourceType)
} }
// GetDataSets 获取图表设置的数据集查询数据 // GetDataSetSeries 获取图表设置的数据集查询数据
func (p *VisualDataSetFieldApi) GetDataSets(rc *restfulx.ReqCtx) { func (p *VisualDataSetTableApi) GetDataSetSeries(rc *restfulx.ReqCtx) {
var data entity.DataSetDataReq var data entity.DataSetDataReq
restfulx.BindQuery(rc, &data) restfulx.BindQuery(rc, &data)
one := p.VisualDataSetTableApp.FindOne(data.TableId)
biz.NotNil(one, "未查到该数据集")
info := make(map[string]string)
biz.ErrIsNil(json.Unmarshal([]byte(one.Info), &info), "解析info失败")
// 数据源查询
source := p.VisualDataSourceApp.FindOne(one.DataSourceId)
// 拼接维度
dataDs := make([]string, 0)
dataQs := make([]string, 0)
sorts := make([]string, 0)
filters := make([]string, 0)
for _, ds := range data.DataDs {
dataDs = append(dataDs, ds.Value)
}
// 拼接指标
for _, qs := range data.DataQs {
q := qs.Value
if qs.Func != "" {
q = fmt.Sprintf("%s(%s) as %s", qs.Func, q, qs.Value)
}
if qs.Decimal > 0 {
q = fmt.Sprintf("ROUND(%s,%d)", q, qs.Decimal)
}
if qs.Sort != "" {
//升序
if qs.Sort == "2" {
sorts = append(sorts, fmt.Sprintf("%s asc", qs.Value))
}
//降序
if qs.Sort == "3" {
sorts = append(sorts, fmt.Sprintf("%s desc", qs.Value))
}
}
if len(qs.Filters) > 0 {
for _, filter := range qs.Filters {
filters = append(filters, fmt.Sprintf("%s %s %d", filter.Name, filter.Rule, filter.Num))
}
}
dataQs = append(dataQs, q)
}
//
if one.TableType == "excel" {
rc.ResData = "占时不支持"
return
}
var sql string
//根据信息拼接sql
if one.TableType == "db" {
sql = fmt.Sprintf(`select %s from %s`,
strings.Join(dataQs, ","),
info["table"],
)
}
if one.TableType == "sql" {
sql = fmt.Sprintf(`select %s from (%s) as table`,
strings.Join(dataQs, ","),
info["sql"],
)
}
if len(filters) > 0 {
sql += fmt.Sprintf(" WHERE %s", strings.Join(filters, " AND "))
}
if len(dataDs) > 0 {
sql += fmt.Sprintf(" GROUP BY %s", strings.Join(dataDs, ","))
}
if len(sorts) > 0 {
sql += fmt.Sprintf(" ORDER BY %s", strings.Join(sorts, ","))
}
if data.ShowNumType == "2" {
sql += fmt.Sprintf(" LIMIT %d", data.ShowNum)
}
// 执行
instance := driver.NewDbInstance(source)
_, series, err := instance.SelectData(sql)
biz.ErrIsNil(err, "查询表信息失败")
rc.ResData = series
} }
func (p *VisualDataSetTableApi) operateTable(opType string, data entity.VisualDataSetTable) { func (p *VisualDataSetTableApi) operateTable(opType string, data entity.VisualDataSetTable) {

View File

@@ -56,15 +56,34 @@ func (VisualDataSetTableFun) TableName() string {
} }
type DataSetDataReq struct { type DataSetDataReq struct {
TableId string `json:"tableId"` //数据集Id TableId string `json:"tableId"` //数据集Id
ShowNumType string `json:"showNumType"` ShowNumType string `json:"showNumType"`
ShowNum int64 `json:"showNum"` ShowNum int64 `json:"showNum"`
DataDs []map[string]interface{} `json:"dataDs"` DataDs []DataSetDataDs `json:"dataDs"`
DataQs []map[string]interface{} `json:"dataQs"` DataQs []DataSetDataQs `json:"dataQs"`
DataLs []map[string]interface{} `json:"dataLs"` DataLs []DataSetDataDs `json:"dataLs"`
} }
type DataSetDataRes struct { type DataSetDataRes struct {
Fields []string `json:"fields"` Fields []string `json:"fields"`
Series []map[string]interface{} `json:"series"` Series []map[string]interface{} `json:"series"`
} }
type DataSetDataDs struct {
Name string `json:"name"`
Value string `json:"value"`
}
type DataSetDataQs struct {
DataSetDataDs
Func string `json:"func"`
Decimal int64 `json:"decimal"`
Sort string `json:"sort"`
Filters []DataSetDataFilters `json:"filters"`
}
type DataSetDataFilters struct {
Name string `json:"name"`
Rule string `json:"rule"`
Num int64 `json:"num"`
}

View File

@@ -97,5 +97,12 @@ func InitVisualDataSetTableRouter(container *restful.Container) {
Param(ws.QueryParameter("sourceId", "数据源Id").Required(true).DataType("string")). Param(ws.QueryParameter("sourceId", "数据源Id").Required(true).DataType("string")).
Metadata(restfulspec.KeyOpenAPITags, tags)) Metadata(restfulspec.KeyOpenAPITags, tags))
ws.Route(ws.POST("/list/series").To(func(request *restful.Request, response *restful.Response) {
restfulx.NewReqCtx(request, response).WithNeedCasbin(false).WithLog("获取数列表").Handle(s.GetDataSetSeries)
}).
Doc("获取数列表").
Reads(entity.DataSetDataReq{}).
Metadata(restfulspec.KeyOpenAPITags, tags))
container.Add(ws) container.Add(ws)
} }

View File

@@ -3,6 +3,7 @@ package tool
import ( import (
"fmt" "fmt"
"github.com/xuri/excelize/v2" "github.com/xuri/excelize/v2"
"pandax/apps/visual/entity"
) )
// 读取数据表 // 读取数据表
@@ -38,3 +39,51 @@ func ReadExcel(filename string) ([]string, []map[string]interface{}) {
} }
return cols, ret return cols, ret
} }
func ReadExcelByFilter(filename string, data entity.DataSetDataReq) ([]string, []map[string]interface{}) {
dataDs := make([]string, 0)
for _, ds := range data.DataDs {
dataDs = append(dataDs, ds.Value)
}
ret := make([]map[string]interface{}, 0)
f, err := excelize.OpenFile(filename)
if err != nil {
fmt.Println("读取excel文件出错", err.Error())
return nil, ret
}
sheets := f.GetSheetMap()
sheet1 := sheets[1]
rows, err := f.GetRows(sheet1)
cols := make([]string, 0)
colsIndex := make([]int, 0)
isHead := true
count := 0
for _, row := range rows {
if data.ShowNumType == "2" {
if count == int(data.ShowNum) {
break
}
}
if isHead { //取得第一行的所有数据---execel表头
if len(row) == 0 {
continue
}
for i, colCell := range row {
cols = append(cols, colCell)
colsIndex = append(colsIndex, i)
}
isHead = false
} else {
theRow := map[string]interface{}{}
for j, colCell := range row {
k := cols[j]
theRow[k] = colCell
}
ret = append(ret, theRow)
}
count++
}
return cols, ret
}