From efbf5917a744beb69dd84f2341d9ef02238213e3 Mon Sep 17 00:00:00 2001 From: XM-GO <93296511+XM-GO@users.noreply.github.com> Date: Thu, 27 Apr 2023 17:17:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E9=9B=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/visual/api/data_source.go | 10 ++ apps/visual/api/visual_data_set_table.go | 94 ++++++++++++++++++- apps/visual/driver/mysql_meta.go | 8 ++ apps/visual/entity/data_set.go | 32 ++++--- apps/visual/router/data_source.go | 6 ++ apps/visual/router/visual_data_set_table.go | 17 ++++ apps/visual/services/visual_data_set_table.go | 6 +- go.mod | 19 ++-- go.sum | 24 +++++ pkg/tool/excel.go | 42 +++++++++ 10 files changed, 231 insertions(+), 27 deletions(-) create mode 100644 pkg/tool/excel.go diff --git a/apps/visual/api/data_source.go b/apps/visual/api/data_source.go index f6734f3..a5b9cbf 100644 --- a/apps/visual/api/data_source.go +++ b/apps/visual/api/data_source.go @@ -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") diff --git a/apps/visual/api/visual_data_set_table.go b/apps/visual/api/visual_data_set_table.go index afdc813..f5ebd2f 100644 --- a/apps/visual/api/visual_data_set_table.go +++ b/apps/visual/api/visual_data_set_table.go @@ -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() { + +} diff --git a/apps/visual/driver/mysql_meta.go b/apps/visual/driver/mysql_meta.go index 52e5922..5005c45 100644 --- a/apps/visual/driver/mysql_meta.go +++ b/apps/visual/driver/mysql_meta.go @@ -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 } diff --git a/apps/visual/entity/data_set.go b/apps/visual/entity/data_set.go index 5341417..5c4e297 100644 --- a/apps/visual/entity/data_set.go +++ b/apps/visual/entity/data_set.go @@ -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"` +} diff --git a/apps/visual/router/data_source.go b/apps/visual/router/data_source.go index bac14bd..88eed49 100644 --- a/apps/visual/router/data_source.go +++ b/apps/visual/router/data_source.go @@ -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) }). diff --git a/apps/visual/router/visual_data_set_table.go b/apps/visual/router/visual_data_set_table.go index 4831c52..7baeb87 100644 --- a/apps/visual/router/visual_data_set_table.go +++ b/apps/visual/router/visual_data_set_table.go @@ -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) } diff --git a/apps/visual/services/visual_data_set_table.go b/apps/visual/services/visual_data_set_table.go index 29d62c1..f515301 100644 --- a/apps/visual/services/visual_data_set_table.go +++ b/apps/visual/services/visual_data_set_table.go @@ -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 } diff --git a/go.mod b/go.mod index 6c470e5..ba627d4 100644 --- a/go.mod +++ b/go.mod @@ -22,7 +22,7 @@ require ( github.com/robfig/cron/v3 v3.0.1 github.com/sirupsen/logrus v1.9.0 github.com/spf13/cobra v1.5.0 - golang.org/x/crypto v0.6.0 + golang.org/x/crypto v0.8.0 google.golang.org/grpc v1.48.0 gorm.io/gorm v1.22.3 ) @@ -91,17 +91,18 @@ require ( github.com/pkg/errors v0.9.1 // indirect github.com/qiniu/go-sdk/v7 v7.11.0 // indirect github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect - github.com/richardlehane/mscfb v1.0.3 // indirect - github.com/richardlehane/msoleps v1.0.1 // indirect + github.com/richardlehane/mscfb v1.0.4 // indirect + github.com/richardlehane/msoleps v1.0.3 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/tencentyun/cos-go-sdk-v5 v0.7.33 // indirect - github.com/xuri/efp v0.0.0-20210322160811-ab561f5b45e3 // indirect - github.com/xuri/excelize/v2 v2.4.1 // indirect - golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb // indirect - golang.org/x/net v0.6.0 // indirect + github.com/xuri/efp v0.0.0-20220603152613-6918739fd470 // indirect + github.com/xuri/excelize/v2 v2.7.1 // indirect + github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22 // indirect + golang.org/x/image v0.5.0 // indirect + golang.org/x/net v0.9.0 // indirect golang.org/x/sync v0.1.0 // indirect - golang.org/x/sys v0.5.0 // indirect - golang.org/x/text v0.7.0 // indirect + golang.org/x/sys v0.7.0 // indirect + golang.org/x/text v0.9.0 // indirect golang.org/x/time v0.3.0 // indirect google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect google.golang.org/protobuf v1.28.0 // indirect diff --git a/go.sum b/go.sum index a1c0cb2..6c906d3 100644 --- a/go.sum +++ b/go.sum @@ -331,8 +331,12 @@ github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5X github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= github.com/richardlehane/mscfb v1.0.3 h1:rD8TBkYWkObWO0oLDFCbwMeZ4KoalxQy+QgniCj3nKI= github.com/richardlehane/mscfb v1.0.3/go.mod h1:YzVpcZg9czvAuhk9T+a3avCpcFPMUWm7gK3DypaEsUk= +github.com/richardlehane/mscfb v1.0.4 h1:WULscsljNPConisD5hR0+OyZjwK46Pfyr6mPu5ZawpM= +github.com/richardlehane/mscfb v1.0.4/go.mod h1:YzVpcZg9czvAuhk9T+a3avCpcFPMUWm7gK3DypaEsUk= github.com/richardlehane/msoleps v1.0.1 h1:RfrALnSNXzmXLbGct/P2b4xkFz4e8Gmj/0Vj9M9xC1o= github.com/richardlehane/msoleps v1.0.1/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg= +github.com/richardlehane/msoleps v1.0.3 h1:aznSZzrwYRl3rLKRT3gUk9am7T/mLNSnJINvN0AQoVM= +github.com/richardlehane/msoleps v1.0.3/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg= github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= @@ -376,8 +380,14 @@ github.com/tencentyun/cos-go-sdk-v5 v0.7.33 h1:5jmJU7U/1nf/7ZPDkrUL8KlF1oDUzTHsd github.com/tencentyun/cos-go-sdk-v5 v0.7.33/go.mod h1:4E4+bQ2gBVJcgEC9Cufwylio4mXOct2iu05WjgEBx1o= github.com/xuri/efp v0.0.0-20210322160811-ab561f5b45e3 h1:EpI0bqf/eX9SdZDwlMmahKM+CDBgNbsXMhsN28XrM8o= github.com/xuri/efp v0.0.0-20210322160811-ab561f5b45e3/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI= +github.com/xuri/efp v0.0.0-20220603152613-6918739fd470 h1:6932x8ltq1w4utjmfMPVj09jdMlkY0aiA6+Skbtl3/c= +github.com/xuri/efp v0.0.0-20220603152613-6918739fd470/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI= github.com/xuri/excelize/v2 v2.4.1 h1:veeeFLAJwsNEBPBlDepzPIYS1eLyBVcXNZUW79exZ1E= github.com/xuri/excelize/v2 v2.4.1/go.mod h1:rSu0C3papjzxQA3sdK8cU544TebhrPUoTOaGPIh0Q1A= +github.com/xuri/excelize/v2 v2.7.1 h1:gm8q0UCAyaTt3MEF5wWMjVdmthm2EHAWesGSKS9tdVI= +github.com/xuri/excelize/v2 v2.7.1/go.mod h1:qc0+2j4TvAUrBw36ATtcTeC1VCM0fFdAXZOmcF4nTpY= +github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22 h1:OAmKAfT06//esDdpi/DZ8Qsdt4+M5+ltca05dA5bG2M= +github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q= go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI= @@ -407,10 +417,14 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.6.0 h1:qfktjS5LUO+fFKeJXZ+ikTRijMmljikvG68fpMMruSc= golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58= +golang.org/x/crypto v0.8.0 h1:pd9TJtTueMTVQXzk8E2XESSMQDj/U7OUu0PqJqPXQjQ= +golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/image v0.0.0-20190501045829-6d32002ffd75/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb h1:fqpd0EBDzlHRCjiphRR5Zo/RSWWQlWv34418dnEixWk= golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= +golang.org/x/image v0.5.0 h1:5JMiNunQeQw++mMOz48/ISeNu3Iweh/JaZU8ZLqHRrI= +golang.org/x/image v0.5.0/go.mod h1:FVC7BI/5Ym8R25iw5OLsgshdUBbT1h5jZTpA+mvAdZ4= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= @@ -418,6 +432,7 @@ golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHl golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -438,6 +453,8 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug golang.org/x/net v0.0.0-20220725212005-46097bf591d3/go.mod h1:AaygXjzTFtRAg2ttMY5RMuhpJ3cNnI0XpyFJD1iQRSM= golang.org/x/net v0.6.0 h1:L4ZwwTvKW9gr0ZMS1yrHD9GZhIuVjOBBnaKH+SPQK0Q= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM= +golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -478,9 +495,13 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= +golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= @@ -490,6 +511,8 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4= golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= @@ -506,6 +529,7 @@ golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20200103221440-774c71fcf114/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/pkg/tool/excel.go b/pkg/tool/excel.go new file mode 100644 index 0000000..de61c02 --- /dev/null +++ b/pkg/tool/excel.go @@ -0,0 +1,42 @@ +package tool + +import ( + "fmt" + "github.com/xuri/excelize/v2" +) + +// 读取数据表 +func ReadExcel(filename string) ([]string, []map[string]interface{}) { + 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] + fmt.Println("第一个工作表", sheet1) + rows, err := f.GetRows(sheet1) + cols := make([]string, 0) + isHead := true + for _, row := range rows { + if isHead { //取得第一行的所有数据---execel表头 + if len(row) == 0 { + continue + } + for _, colCell := range row { + cols = append(cols, colCell) + } + isHead = false + fmt.Println("列信息", cols) + } else { + theRow := map[string]interface{}{} + for j, colCell := range row { + k := cols[j] + theRow[k] = colCell + } + ret = append(ret, theRow) + } + } + return cols, ret +}