mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-23 02:48:34 +08:00
数据集
This commit is contained in:
@@ -40,6 +40,16 @@ func (p *VisualDataSourceApi) GetVisualDataSourceList(rc *restfulx.ReqCtx) {
|
||||
}
|
||||
}
|
||||
|
||||
// GetVisualDataSourceListAll DataSource列表数据
|
||||
func (p *VisualDataSourceApi) GetVisualDataSourceListAll(rc *restfulx.ReqCtx) {
|
||||
data := entity.VisualDataSource{}
|
||||
data.SourceName = restfulx.QueryParam(rc, "sourceName")
|
||||
data.SourceType = restfulx.QueryParam(rc, "sourceType")
|
||||
data.Status = restfulx.QueryParam(rc, "status")
|
||||
list := p.VisualDataSourceApp.FindList(data)
|
||||
rc.ResData = list
|
||||
}
|
||||
|
||||
// GetVisualDataSource 获取DataSource
|
||||
func (p *VisualDataSourceApi) GetVisualDataSource(rc *restfulx.ReqCtx) {
|
||||
sourceId := restfulx.PathParam(rc, "sourceId")
|
||||
|
||||
@@ -6,16 +6,26 @@ package api
|
||||
// 生成人:panda
|
||||
// ==========================================================================
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/XM-GO/PandaKit/biz"
|
||||
filek "github.com/XM-GO/PandaKit/file"
|
||||
"github.com/XM-GO/PandaKit/model"
|
||||
"github.com/XM-GO/PandaKit/restfulx"
|
||||
"strings"
|
||||
|
||||
"github.com/kakuilan/kgo"
|
||||
"log"
|
||||
"pandax/apps/visual/driver"
|
||||
"pandax/apps/visual/entity"
|
||||
"pandax/apps/visual/services"
|
||||
"pandax/pkg/tool"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type VisualDataSetTableApi struct {
|
||||
VisualDataSetTableApp services.VisualDataSetTableModel
|
||||
VisualDataSourceApp services.VisualDataSourceModel
|
||||
}
|
||||
|
||||
// GetVisualDataSetTableList DataSetTable列表数据
|
||||
@@ -23,7 +33,8 @@ func (p *VisualDataSetTableApi) GetVisualDataSetTableList(rc *restfulx.ReqCtx) {
|
||||
data := entity.VisualDataSetTable{}
|
||||
pageNum := restfulx.QueryInt(rc, "pageNum", 1)
|
||||
pageSize := restfulx.QueryInt(rc, "pageSize", 10)
|
||||
|
||||
data.Name = restfulx.QueryParam(rc, "sourceName")
|
||||
data.TableType = restfulx.QueryParam(rc, "sourceType")
|
||||
list, total := p.VisualDataSetTableApp.FindListPage(pageNum, pageSize, data)
|
||||
|
||||
rc.ResData = model.ResultPage{
|
||||
@@ -44,8 +55,29 @@ func (p *VisualDataSetTableApi) GetVisualDataSetTable(rc *restfulx.ReqCtx) {
|
||||
func (p *VisualDataSetTableApi) InsertVisualDataSetTable(rc *restfulx.ReqCtx) {
|
||||
var data entity.VisualDataSetTable
|
||||
restfulx.BindQuery(rc, &data)
|
||||
|
||||
data.TableId = kgo.KStr.Uniqid("px")
|
||||
p.VisualDataSetTableApp.Insert(data)
|
||||
// 协程执行添加字段
|
||||
/*go func() {
|
||||
info := make(map[string]string)
|
||||
err := json.Unmarshal([]byte(data.Info), &info)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
one := p.VisualDataSourceApp.FindOne(data.DataSourceId)
|
||||
if data.TableType == "db" {
|
||||
instance := driver.NewDbInstance(one)
|
||||
instance.GetMeta().GetColumns(info["table"])
|
||||
}
|
||||
if data.TableType == "sql" {
|
||||
|
||||
}
|
||||
|
||||
}()*/
|
||||
/*entity.VisualDataSetField{
|
||||
TableId: data.TableId,
|
||||
|
||||
}*/
|
||||
}
|
||||
|
||||
// UpdateVisualDataSetTable 修改DataSetTable
|
||||
@@ -62,3 +94,57 @@ func (p *VisualDataSetTableApi) DeleteVisualDataSetTable(rc *restfulx.ReqCtx) {
|
||||
tableIds := strings.Split(tableId, ",")
|
||||
p.VisualDataSetTableApp.Delete(tableIds)
|
||||
}
|
||||
|
||||
// GetVisualDataSetTableResult 获取运行结果
|
||||
func (p *VisualDataSetTableApi) GetVisualDataSetTableResult(rc *restfulx.ReqCtx) {
|
||||
dsr := entity.VisualDataSetRequest{}
|
||||
restfulx.BindQuery(rc, &dsr)
|
||||
info := make(map[string]string)
|
||||
biz.ErrIsNil(json.Unmarshal([]byte(dsr.Info), &info), "获取表信息失败")
|
||||
if dsr.Type == "excel" {
|
||||
filePath := info["filePath"]
|
||||
fields, data := tool.ReadExcel(filePath)
|
||||
rc.ResData = entity.VisualDataSetRes{
|
||||
Data: data,
|
||||
Fields: fields,
|
||||
}
|
||||
return
|
||||
}
|
||||
one := p.VisualDataSourceApp.FindOne(dsr.SourceId)
|
||||
instance := driver.NewDbInstance(one)
|
||||
var sql string
|
||||
if dsr.Type == "db" {
|
||||
sql = fmt.Sprintf(`select * from %s LIMIT 20`, info["table"])
|
||||
}
|
||||
if dsr.Type == "sql" {
|
||||
sql = info["sql"] + " LIMIT 20"
|
||||
}
|
||||
log.Println(sql)
|
||||
fields, data, err := instance.SelectData(sql)
|
||||
biz.ErrIsNil(err, "查询表信息失败")
|
||||
rc.ResData = entity.VisualDataSetRes{
|
||||
Data: data,
|
||||
Fields: fields,
|
||||
}
|
||||
}
|
||||
|
||||
const filePath = "uploads/excel"
|
||||
|
||||
// GetVisualDataSetTableByExcelResult 通过excel读取结果
|
||||
func (p *VisualDataSetTableApi) GetVisualDataSetTableByExcelResult(rc *restfulx.ReqCtx) {
|
||||
_, fileHeader, err := rc.Request.Request.FormFile("excelFile")
|
||||
ext := path.Ext(fileHeader.Filename)
|
||||
// 读取文件名并加密
|
||||
name := strings.TrimSuffix(fileHeader.Filename, ext)
|
||||
name = kgo.KStr.Md5(name, 10)
|
||||
// 拼接新文件名
|
||||
filename := name + "_" + time.Now().Format("20060102150405") + ext
|
||||
dst := fmt.Sprintf("%s/%s", filePath, filename)
|
||||
filek.SaveUploadedFile(fileHeader, dst)
|
||||
biz.ErrIsNil(err, "文件上传失败")
|
||||
rc.ResData = dst
|
||||
}
|
||||
|
||||
func GetDataSetTableFun() {
|
||||
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"github.com/XM-GO/PandaKit/biz"
|
||||
"log"
|
||||
"pandax/apps/visual/entity"
|
||||
"strings"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
@@ -45,6 +46,13 @@ const (
|
||||
WHERE table_schema = (SELECT database()) AND table_name in (%s) ORDER BY tableName, ordinal_position`
|
||||
)
|
||||
|
||||
func sqlFix(sql string) string {
|
||||
if strings.LastIndex(sql, ";") == (len(sql) - 1) {
|
||||
sql = sql[0 : len(sql)-1]
|
||||
}
|
||||
return sql
|
||||
}
|
||||
|
||||
type MysqlMetadata struct {
|
||||
di *DbInstance
|
||||
}
|
||||
|
||||
@@ -4,14 +4,13 @@ import "github.com/XM-GO/PandaKit/model"
|
||||
|
||||
type VisualDataSetTable struct {
|
||||
model.BaseModelD
|
||||
TableId string `gorm:"primary_key;tableId;comment:表id" json:"tableId"`
|
||||
DataSourceId string `gorm:"dataSourceId;type:varchar(64);comment:数据源ID" json:"dataSourceId"`
|
||||
Name string `gorm:"name;type:varchar(64);comment:名称" json:"name"`
|
||||
TableType string `gorm:"tableType;type:varchar(64);comment:db,sql,excel,union" json:"tableType"`
|
||||
Info string `gorm:"info;type:TEXT;comment:原始表信息" json:"info"`
|
||||
CreateBy int64 `gorm:"create_by" json:"createBy"` //创建人ID
|
||||
DataSource VisualDataSource `gorm:"foreignKey:DataSourceId;references:SourceId" json:"dataSource"`
|
||||
Fields []VisualDataSetField `gorm:"foreignKey:TableId" json:"fields"` //字段信息
|
||||
TableId string `gorm:"primary_key;tableId;comment:表id" json:"tableId"`
|
||||
DataSourceId string `gorm:"dataSourceId;type:varchar(64);comment:数据源ID" json:"sourceId"`
|
||||
Name string `gorm:"name;type:varchar(64);comment:名称" json:"name"`
|
||||
TableType string `gorm:"tableType;type:varchar(64);comment:db,sql,excel,union" json:"tableType"`
|
||||
Info string `gorm:"info;type:TEXT;comment:原始表信息" json:"info"` //
|
||||
CreateBy int64 `gorm:"create_by" json:"createBy"` //创建人ID
|
||||
DataSource VisualDataSource `gorm:"foreignKey:DataSourceId;references:SourceId" json:"dataSource"`
|
||||
}
|
||||
|
||||
func (VisualDataSetTable) TableName() string {
|
||||
@@ -24,9 +23,9 @@ type VisualDataSetField struct {
|
||||
TableId string `gorm:"tableId;type:varchar(64);comment:表id" json:"tableId"`
|
||||
Comment string `gorm:"comment;type:varchar(64);comment:字段描述" json:"columnComment"` // 列描述
|
||||
Name string `gorm:"name;type:varchar(64);comment:字段名" json:"name"`
|
||||
GroupType string `gorm:"group_type;type:varchar(1);comment:字段分组类型" json:"group_type"` //d 维度 g
|
||||
OriginName string `gorm:"origin_name;type:varchar(50);comment:原始字段名" json:"origin_name"`
|
||||
OriginType string `gorm:"origin_type;type:varchar(50);comment:原始字段类型" json:"origin_type"`
|
||||
GroupType string `gorm:"group_type;type:varchar(1);comment:字段分组类型" json:"groupType"` //d 维度 g
|
||||
OriginName string `gorm:"origin_name;type:varchar(50);comment:原始字段名" json:"originName"`
|
||||
OriginType string `gorm:"origin_type;type:varchar(50);comment:原始字段类型" json:"originType"`
|
||||
DeType int `gorm:"de_type;type:varchar(50);comment:数据源字段类型:0-文本,1-时间,2-数值(包括小数)" json:"deType"`
|
||||
DeName string `gorm:"de_name;type:varchar(50);comment:数据源查询名" json:"deName"`
|
||||
ExtField int `gorm:"ext_field;type:int;comment:是否扩展字段 0否 1是" json:"extField"`
|
||||
@@ -35,3 +34,14 @@ type VisualDataSetField struct {
|
||||
func (VisualDataSetField) TableName() string {
|
||||
return "visual_data_set_field"
|
||||
}
|
||||
|
||||
type VisualDataSetRes struct {
|
||||
Data []map[string]interface{} `json:"data"`
|
||||
Fields []string `json:"fields"`
|
||||
}
|
||||
|
||||
type VisualDataSetRequest struct {
|
||||
SourceId string `json:"sourceId"`
|
||||
Info string `json:"info"`
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
@@ -35,6 +35,12 @@ func InitVisualDataSourceRouter(container *restful.Container) {
|
||||
Writes(model.ResultPage{}).
|
||||
Returns(200, "OK", model.ResultPage{}))
|
||||
|
||||
ws.Route(ws.GET("/list/all").To(func(request *restful.Request, response *restful.Response) {
|
||||
restfulx.NewReqCtx(request, response).WithNeedCasbin(false).WithLog("获取DataSource列表").Handle(s.GetVisualDataSourceListAll)
|
||||
}).
|
||||
Doc("获取DataSource列表").
|
||||
Metadata(restfulspec.KeyOpenAPITags, tags))
|
||||
|
||||
ws.Route(ws.GET("/{sourceId}").To(func(request *restful.Request, response *restful.Response) {
|
||||
restfulx.NewReqCtx(request, response).WithLog("获取DataSource信息").Handle(s.GetVisualDataSource)
|
||||
}).
|
||||
|
||||
@@ -19,6 +19,7 @@ import (
|
||||
func InitVisualDataSetTableRouter(container *restful.Container) {
|
||||
s := &api.VisualDataSetTableApi{
|
||||
VisualDataSetTableApp: services.VisualDataSetTableModelDao,
|
||||
VisualDataSourceApp: services.VisualDataSourceModelDao,
|
||||
}
|
||||
|
||||
ws := new(restful.WebService)
|
||||
@@ -66,5 +67,21 @@ func InitVisualDataSetTableRouter(container *restful.Container) {
|
||||
Metadata(restfulspec.KeyOpenAPITags, tags).
|
||||
Param(ws.PathParameter("tableId", "多id 1,2,3").DataType("string")))
|
||||
|
||||
ws.Route(ws.POST("/resultPreview").To(func(request *restful.Request, response *restful.Response) {
|
||||
restfulx.NewReqCtx(request, response).WithNeedCasbin(false).WithLog("运行结果").Handle(s.GetVisualDataSetTableResult)
|
||||
}).
|
||||
Doc("运行结果").
|
||||
Metadata(restfulspec.KeyOpenAPITags, tags).
|
||||
Reads(entity.VisualDataSetRequest{}).
|
||||
Returns(200, "OK", entity.VisualDataSetRes{}))
|
||||
|
||||
ws.Route(ws.POST("/up/excel").To(func(request *restful.Request, response *restful.Response) {
|
||||
restfulx.NewReqCtx(request, response).WithNeedCasbin(false).WithLog("上传Excel").Handle(s.GetVisualDataSetTableByExcelResult)
|
||||
}).
|
||||
Doc("上传Excel").
|
||||
Param(ws.FormParameter("excelFile", "Excel文件")).
|
||||
Metadata(restfulspec.KeyOpenAPITags, tags).
|
||||
Returns(200, "OK", entity.VisualDataSetRes{}))
|
||||
|
||||
container.Add(ws)
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ func (m *datasettableModelImpl) Insert(data entity.VisualDataSetTable) *entity.V
|
||||
func (m *datasettableModelImpl) FindOne(tableId string) *entity.VisualDataSetTable {
|
||||
resData := new(entity.VisualDataSetTable)
|
||||
db := global.Db.Table(m.table).Where("table_id = ?", tableId)
|
||||
err := db.Preload("DataSource").Preload("Fields").First(resData).Error
|
||||
err := db.Preload("DataSource").First(resData).Error
|
||||
biz.ErrIsNil(err, "查询数据集表失败")
|
||||
return resData
|
||||
}
|
||||
@@ -58,7 +58,7 @@ func (m *datasettableModelImpl) FindListPage(page, pageSize int, data entity.Vis
|
||||
db = db.Where("table_type = ?", data.TableType)
|
||||
}
|
||||
err := db.Count(&total).Error
|
||||
err = db.Order("create_time").Limit(pageSize).Offset(offset).Preload("DataSource").Preload("Fields").Find(&list).Error
|
||||
err = db.Order("create_time").Limit(pageSize).Offset(offset).Preload("DataSource").Find(&list).Error
|
||||
biz.ErrIsNil(err, "查询数据集表分页列表失败")
|
||||
return &list, total
|
||||
}
|
||||
@@ -73,7 +73,7 @@ func (m *datasettableModelImpl) FindList(data entity.VisualDataSetTable) *[]enti
|
||||
if data.TableType != "" {
|
||||
db = db.Where("table_type = ?", data.TableType)
|
||||
}
|
||||
biz.ErrIsNil(db.Order("create_time").Preload("DataSource").Preload("Fields").Find(&list).Error, "查询数据集表列表失败")
|
||||
biz.ErrIsNil(db.Order("create_time").Preload("DataSource").Find(&list).Error, "查询数据集表列表失败")
|
||||
return &list
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user