mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-05-06 03:51:25 +08:00
数据集
This commit is contained in:
@@ -26,15 +26,18 @@ import (
|
||||
type VisualDataSetTableApi struct {
|
||||
VisualDataSetTableApp services.VisualDataSetTableModel
|
||||
VisualDataSourceApp services.VisualDataSourceModel
|
||||
VisualDataSetFieldApi services.VisualDataSetFieldModel
|
||||
}
|
||||
|
||||
var ColumnTypeStr = []string{"delete_time", "update_time"}
|
||||
|
||||
// GetVisualDataSetTableList DataSetTable列表数据
|
||||
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")
|
||||
data.Name = restfulx.QueryParam(rc, "name")
|
||||
data.TableType = restfulx.QueryParam(rc, "tableType")
|
||||
list, total := p.VisualDataSetTableApp.FindListPage(pageNum, pageSize, data)
|
||||
|
||||
rc.ResData = model.ResultPage{
|
||||
@@ -58,26 +61,41 @@ func (p *VisualDataSetTableApi) InsertVisualDataSetTable(rc *restfulx.ReqCtx) {
|
||||
data.TableId = kgo.KStr.Uniqid("px")
|
||||
p.VisualDataSetTableApp.Insert(data)
|
||||
// 协程执行添加字段
|
||||
/*go func() {
|
||||
go func() {
|
||||
info := make(map[string]string)
|
||||
err := json.Unmarshal([]byte(data.Info), &info)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
// 保存 excel field
|
||||
if data.TableType == "excel" {
|
||||
filePath := info["filePath"]
|
||||
_, datas := tool.ReadExcel(filePath)
|
||||
field := getDataSetField(datas)
|
||||
if field != nil {
|
||||
field.TableId = data.TableId
|
||||
p.VisualDataSetFieldApi.Insert(*field)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
one := p.VisualDataSourceApp.FindOne(data.DataSourceId)
|
||||
instance := driver.NewDbInstance(one)
|
||||
sql := ""
|
||||
if data.TableType == "db" {
|
||||
instance := driver.NewDbInstance(one)
|
||||
instance.GetMeta().GetColumns(info["table"])
|
||||
sql = fmt.Sprintf(`select * from %s LIMIT 1`, info["table"])
|
||||
}
|
||||
if data.TableType == "sql" {
|
||||
|
||||
sql = info["sql"] + " LIMIT 1"
|
||||
}
|
||||
_, datas, err := instance.SelectData(sql)
|
||||
field := getDataSetField(datas)
|
||||
if field != nil {
|
||||
field.TableId = data.TableId
|
||||
p.VisualDataSetFieldApi.Insert(*field)
|
||||
}
|
||||
}()
|
||||
|
||||
}()*/
|
||||
/*entity.VisualDataSetField{
|
||||
TableId: data.TableId,
|
||||
|
||||
}*/
|
||||
}
|
||||
|
||||
// UpdateVisualDataSetTable 修改DataSetTable
|
||||
@@ -114,12 +132,11 @@ func (p *VisualDataSetTableApi) GetVisualDataSetTableResult(rc *restfulx.ReqCtx)
|
||||
instance := driver.NewDbInstance(one)
|
||||
var sql string
|
||||
if dsr.Type == "db" {
|
||||
sql = fmt.Sprintf(`select * from %s LIMIT 20`, info["table"])
|
||||
sql = fmt.Sprintf(`select * from %s LIMIT 10`, info["table"])
|
||||
}
|
||||
if dsr.Type == "sql" {
|
||||
sql = info["sql"] + " LIMIT 20"
|
||||
sql = info["sql"] + " LIMIT 10"
|
||||
}
|
||||
log.Println(sql)
|
||||
fields, data, err := instance.SelectData(sql)
|
||||
biz.ErrIsNil(err, "查询表信息失败")
|
||||
rc.ResData = entity.VisualDataSetRes{
|
||||
@@ -145,6 +162,51 @@ func (p *VisualDataSetTableApi) GetVisualDataSetTableByExcelResult(rc *restfulx.
|
||||
rc.ResData = dst
|
||||
}
|
||||
|
||||
func GetDataSetTableFun() {
|
||||
|
||||
func (p *VisualDataSetTableApi) GetDataSetTableFun(rc *restfulx.ReqCtx) {
|
||||
sourceId := restfulx.QueryParam(rc, "sourceId")
|
||||
one := p.VisualDataSourceApp.FindOne(sourceId)
|
||||
log.Println(one.SourceType)
|
||||
rc.ResData = p.VisualDataSetTableApp.FindFunList(one.SourceType)
|
||||
}
|
||||
|
||||
func getDataSetField(datas []map[string]interface{}) *entity.VisualDataSetField {
|
||||
field := new(entity.VisualDataSetField)
|
||||
if len(datas) > 0 {
|
||||
for k, v := range datas[0] {
|
||||
if needDelete(k) {
|
||||
continue
|
||||
}
|
||||
field.FieldId = kgo.KStr.Uniqid("px")
|
||||
field.Name = k
|
||||
field.DeName = k
|
||||
switch v.(type) {
|
||||
case int64, float64:
|
||||
field.DeType = "2"
|
||||
field.GroupType = "q"
|
||||
case time.Time:
|
||||
field.DeType = "1"
|
||||
field.GroupType = "d"
|
||||
default:
|
||||
field.DeType = "0"
|
||||
field.GroupType = "d"
|
||||
}
|
||||
}
|
||||
}
|
||||
return field
|
||||
}
|
||||
|
||||
func needDelete(name string) bool {
|
||||
if strings.Contains(name, "id") || strings.Contains(name, "Id") {
|
||||
return true
|
||||
}
|
||||
return isExistInArray(name, ColumnTypeStr)
|
||||
}
|
||||
|
||||
func isExistInArray(value string, array []string) bool {
|
||||
for _, v := range array {
|
||||
if v == value {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -19,16 +19,13 @@ func (VisualDataSetTable) TableName() string {
|
||||
|
||||
type VisualDataSetField struct {
|
||||
model.BaseModelD
|
||||
FieldId string `gorm:"primary_key;fieldId;comment:表id" json:"fieldId"`
|
||||
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:"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"`
|
||||
FieldId string `gorm:"primary_key;fieldId;comment:表id" json:"fieldId"`
|
||||
TableId string `gorm:"tableId;type:varchar(64);comment:表id" json:"tableId"`
|
||||
Name string `gorm:"name;type:varchar(64);comment:字段描述" json:"name"`
|
||||
GroupType string `gorm:"group_type;type:varchar(1);comment:字段分组类型" json:"groupType"` //d 维度 g
|
||||
DeType string `gorm:"de_type;type:varchar(1);comment:数据源字段类型:0-文本,1-时间,2-数值(包括小数)" json:"deType"`
|
||||
DeName string `gorm:"de_name;type:varchar(50);comment:数据源查询名" json:"deName"`
|
||||
ExtField string `gorm:"ext_field;type:varchar(1);comment:是否扩展字段 0否 1是" json:"extField"`
|
||||
}
|
||||
|
||||
func (VisualDataSetField) TableName() string {
|
||||
@@ -45,3 +42,14 @@ type VisualDataSetRequest struct {
|
||||
Info string `json:"info"`
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
type VisualDataSetTableFun struct {
|
||||
Name string `gorm:"name;type:varchar(64);comment:函数名" json:"name"`
|
||||
DbType string `gorm:"db_type;type:varchar(50);comment:数据库类型" json:"dbType"` //d 维度 g
|
||||
Func string `gorm:"func;type:varchar(64);comment:函数" json:"func"`
|
||||
Desc string `gorm:"desc;type:varchar(255);comment:描述" json:"desc"`
|
||||
}
|
||||
|
||||
func (VisualDataSetTableFun) TableName() string {
|
||||
return "visual_data_set_table_function"
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ func InitVisualDataSetTableRouter(container *restful.Container) {
|
||||
s := &api.VisualDataSetTableApi{
|
||||
VisualDataSetTableApp: services.VisualDataSetTableModelDao,
|
||||
VisualDataSourceApp: services.VisualDataSourceModelDao,
|
||||
VisualDataSetFieldApi: services.VisualDataSetFieldModelDao,
|
||||
}
|
||||
|
||||
ws := new(restful.WebService)
|
||||
@@ -83,5 +84,12 @@ func InitVisualDataSetTableRouter(container *restful.Container) {
|
||||
Metadata(restfulspec.KeyOpenAPITags, tags).
|
||||
Returns(200, "OK", entity.VisualDataSetRes{}))
|
||||
|
||||
ws.Route(ws.GET("/list/func").To(func(request *restful.Request, response *restful.Response) {
|
||||
restfulx.NewReqCtx(request, response).WithNeedCasbin(false).WithLog("获取函数列表").Handle(s.GetDataSetTableFun)
|
||||
}).
|
||||
Doc("获取函数列表").
|
||||
Param(ws.QueryParameter("sourceId", "数据源Id").Required(true).DataType("string")).
|
||||
Metadata(restfulspec.KeyOpenAPITags, tags))
|
||||
|
||||
container.Add(ws)
|
||||
}
|
||||
|
||||
@@ -20,6 +20,8 @@ type (
|
||||
FindList(data entity.VisualDataSetTable) *[]entity.VisualDataSetTable
|
||||
Update(data entity.VisualDataSetTable) *entity.VisualDataSetTable
|
||||
Delete(tableIds []string)
|
||||
|
||||
FindFunList(dbType string) *[]entity.VisualDataSetTableFun
|
||||
}
|
||||
|
||||
datasettableModelImpl struct {
|
||||
@@ -85,3 +87,9 @@ func (m *datasettableModelImpl) Update(data entity.VisualDataSetTable) *entity.V
|
||||
func (m *datasettableModelImpl) Delete(tableIds []string) {
|
||||
biz.ErrIsNil(global.Db.Table(m.table).Delete(&entity.VisualDataSetTable{}, "table_id in (?)", tableIds).Error, "删除数据集表失败")
|
||||
}
|
||||
|
||||
func (m *datasettableModelImpl) FindFunList(dbType string) *[]entity.VisualDataSetTableFun {
|
||||
list := make([]entity.VisualDataSetTableFun, 0)
|
||||
biz.ErrIsNil(global.Db.Table("visual_data_set_table_function").Where("db_type = ?", dbType).Find(&list).Error, "查询数据函数列表失败")
|
||||
return &list
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user