| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- // Package configure 使用示例
- //
- // 以下示例演示如何使用配置中心客户端SDK
- package test
-
- import (
- "context"
- "fmt"
- "log"
- "time"
-
- "git.x2erp.com/qdy/go-base/model/request/queryreq"
- "git.x2erp.com/qdy/go-base/sdk/configure"
- )
-
- // ExampleUsage 演示SDK的基本用法
- func ExampleUsage() {
- // 示例1:创建客户端
- exampleCreateClient()
-
- // 示例2:查询列表
- exampleListTables()
-
- // 示例3:创建表
- exampleSaveTable()
-
- // 示例4:查询详情
- exampleGetTable()
-
- // 示例5:删除表
- exampleDeleteTable()
-
- // 示例6:别名字典管理
- exampleAliasManagement()
- }
-
- func exampleCreateClient() {
- fmt.Println("=== 示例1:创建客户端 ===")
-
- // 方法1:使用默认配置
- client1, err := configure.NewClient()
- if err != nil {
- log.Printf("Failed to create default client: %v", err)
- } else {
- fmt.Printf("Default client created: URL=%s\n", client1.GetConfig().BaseURL)
- }
-
- // 方法2:使用Basic认证
- if _, err := configure.NewBasicAuthClient("http://localhost:8080", "admin", "123"); err != nil {
- log.Printf("Failed to create basic auth client: %v", err)
- } else {
- fmt.Println("Basic auth client created")
- }
-
- // 方法3:使用Token认证
- if _, err := configure.NewTokenAuthClient("http://localhost:8080", "your-token-here"); err != nil {
- log.Printf("Failed to create token auth client: %v", err)
- } else {
- fmt.Println("Token auth client created")
- }
-
- fmt.Println()
- }
-
- func exampleListTables() {
- fmt.Println("=== 示例2:查询数据库表字典列表 ===")
-
- // 创建客户端
- client, err := configure.NewBasicAuthClient("http://localhost:8080", "admin", "123")
- if err != nil {
- log.Printf("Failed to create client: %v", err)
- return
- }
-
- // 创建查询请求
- query := &configure.DicTableQueryRequest{
- QueryRequest: queryreq.QueryRequest{
- Page: 0,
- PageSize: 10,
- },
- }
-
- // 执行查询
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- defer cancel()
-
- result, err := client.ListTables(ctx, query)
- if err != nil {
- log.Printf("Error listing tables: %v", err)
- return
- }
-
- fmt.Printf("Total tables: %d\n", result.TotalCount)
- fmt.Printf("Last page: %d\n", result.LastPage)
- fmt.Println()
- }
-
- func exampleSaveTable() {
- fmt.Println("=== 示例3:创建数据库表字典 ===")
-
- // 创建客户端
- client, err := configure.NewBasicAuthClient("http://localhost:8080", "admin", "123")
- if err != nil {
- log.Printf("Failed to create client: %v", err)
- return
- }
-
- // 创建请求
- req := &configure.DicTableRequest{
- TableID: "example_table_001",
- TableType: "实体表",
- Name: "示例表001",
- Description: "这是一个示例表",
- Creator: "admin",
- CreatedAt: time.Now(),
- UpdatedAt: time.Now(),
- Fields: []configure.DicTableFieldRequest{
- {
- FieldID: "example_table_001.id",
- TableID: "example_table_001",
- FiledType: "实际字段",
- DataType: "数值型",
- FieldName: "id",
- FieldNameCN: "主键ID",
- Description: "主键字段",
- Creator: "admin",
- CreatedAt: time.Now(),
- UpdatedAt: time.Now(),
- },
- {
- FieldID: "example_table_001.name",
- TableID: "example_table_001",
- FiledType: "实际字段",
- DataType: "字符型",
- FieldName: "name",
- FieldNameCN: "名称",
- Description: "名称字段",
- Creator: "admin",
- CreatedAt: time.Now(),
- UpdatedAt: time.Now(),
- },
- },
- }
-
- // 执行保存
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- defer cancel()
-
- detail, err := client.SaveTable(ctx, req)
- if err != nil {
- log.Printf("Error saving table: %v", err)
- return
- }
-
- fmt.Printf("Table saved successfully: %s\n", detail.Table.TableID)
- fmt.Printf("Table name: %s\n", detail.Table.Name)
- fmt.Printf("Number of fields: %d\n", len(detail.Fields))
- fmt.Println()
- }
-
- func exampleGetTable() {
- fmt.Println("=== 示例4:查询数据库表字典详情 ===")
-
- // 创建客户端
- client, err := configure.NewBasicAuthClient("http://localhost:8080", "admin", "123")
- if err != nil {
- log.Printf("Failed to create client: %v", err)
- return
- }
-
- // 查询表详情
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- defer cancel()
-
- tableID := "example_table_001"
- detail, err := client.GetTable(ctx, tableID)
- if err != nil {
- if err == configure.ErrNotFound {
- fmt.Printf("Table %s not found\n", tableID)
- } else {
- log.Printf("Error getting table: %v", err)
- }
- return
- }
-
- fmt.Printf("Table ID: %s\n", detail.Table.TableID)
- fmt.Printf("Table Type: %s\n", detail.Table.TableType)
- fmt.Printf("Name: %s\n", detail.Table.Name)
- fmt.Printf("Description: %s\n", detail.Table.Description)
- fmt.Printf("Creator: %s\n", detail.Table.Creator)
- fmt.Printf("Fields count: %d\n", len(detail.Fields))
- fmt.Println()
- }
-
- func exampleDeleteTable() {
- fmt.Println("=== 示例5:删除数据库表字典 ===")
-
- // 创建客户端
- client, err := configure.NewBasicAuthClient("http://localhost:8080", "admin", "123")
- if err != nil {
- log.Printf("Failed to create client: %v", err)
- return
- }
-
- // 删除表
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- defer cancel()
-
- tableID := "example_table_001"
- if err := client.DeleteTable(ctx, tableID); err != nil {
- if err == configure.ErrNotFound {
- fmt.Printf("Table %s not found\n", tableID)
- } else {
- log.Printf("Error deleting table: %v", err)
- }
- return
- }
-
- fmt.Printf("Table %s deleted successfully\n", tableID)
- fmt.Println()
- }
-
- func exampleAliasManagement() {
- fmt.Println("=== 示例6:别名字典管理 ===")
-
- // 创建客户端
- client, err := configure.NewBasicAuthClient("http://localhost:8080", "admin", "123")
- if err != nil {
- log.Printf("Failed to create client: %v", err)
- return
- }
-
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- defer cancel()
-
- // 1. 创建表别名字典
- tableAliasReq := &configure.TableAliasRequest{
- TableID: "example_table_001",
- TableAlias: "示例表别名",
- }
-
- tableAliasDetail, err := client.SaveTableAlias(ctx, tableAliasReq)
- if err != nil {
- log.Printf("Error saving table alias: %v", err)
- return
- }
- fmt.Printf("Table alias saved: ID=%s, TableID=%s, Alias=%s\n",
- tableAliasDetail.TableAlias.ID,
- tableAliasDetail.TableAlias.TableID,
- tableAliasDetail.TableAlias.TableAlias)
-
- // 2. 查询表别名字典列表
- query := &configure.TableAliasQueryRequest{
- QueryRequest: queryreq.QueryRequest{
- Page: 0,
- PageSize: 10,
- },
- TableID: "example_table_001",
- }
-
- tableAliasList, err := client.ListTableAliases(ctx, query)
- if err != nil {
- log.Printf("Error listing table aliases: %v", err)
- return
- }
- fmt.Printf("Total table aliases: %d\n", tableAliasList.TotalCount)
-
- // 3. 创建字段别名字典
- fieldAliasReq := &configure.TableFieldAliasRequest{
- FieldID: "example_table_001.id",
- TableID: "example_table_001",
- FieldName: "id",
- FieldAlias: "主键别名",
- Description: "主键字段别名",
- WhereCondition: "查询条件描述",
- }
-
- fieldAliasDetail, err := client.SaveTableFieldAlias(ctx, fieldAliasReq)
- if err != nil {
- log.Printf("Error saving field alias: %v", err)
- return
- }
- fmt.Printf("Field alias saved: ID=%s, FieldID=%s, Alias=%s\n",
- fieldAliasDetail.TableFieldAlias.ID,
- fieldAliasDetail.TableFieldAlias.FieldID,
- fieldAliasDetail.TableFieldAlias.FieldAlias)
-
- // 4. 查询字段别名字典列表
- fieldQuery := &configure.TableFieldAliasQueryRequest{
- QueryRequest: queryreq.QueryRequest{
- Page: 0,
- PageSize: 10,
- },
- TableID: "example_table_001",
- }
-
- fieldAliasList, err := client.ListTableFieldAliases(ctx, fieldQuery)
- if err != nil {
- log.Printf("Error listing field aliases: %v", err)
- return
- }
- fmt.Printf("Total field aliases: %d\n", fieldAliasList.TotalCount)
-
- fmt.Println()
- }
|