Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

example.go 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // Package configure 使用示例
  2. //
  3. // 以下示例演示如何使用配置中心客户端SDK
  4. package test
  5. import (
  6. "context"
  7. "fmt"
  8. "log"
  9. "time"
  10. "git.x2erp.com/qdy/go-base/model/request/queryreq"
  11. "git.x2erp.com/qdy/go-base/sdk/configure"
  12. )
  13. // ExampleUsage 演示SDK的基本用法
  14. func ExampleUsage() {
  15. // 示例1:创建客户端
  16. exampleCreateClient()
  17. // 示例2:查询列表
  18. exampleListTables()
  19. // 示例3:创建表
  20. exampleSaveTable()
  21. // 示例4:查询详情
  22. exampleGetTable()
  23. // 示例5:删除表
  24. exampleDeleteTable()
  25. // 示例6:别名字典管理
  26. exampleAliasManagement()
  27. }
  28. func exampleCreateClient() {
  29. fmt.Println("=== 示例1:创建客户端 ===")
  30. // 方法1:使用默认配置
  31. client1, err := configure.NewClient()
  32. if err != nil {
  33. log.Printf("Failed to create default client: %v", err)
  34. } else {
  35. fmt.Printf("Default client created: URL=%s\n", client1.GetConfig().BaseURL)
  36. }
  37. // 方法2:使用Basic认证
  38. if _, err := configure.NewBasicAuthClient("http://localhost:8080", "admin", "123"); err != nil {
  39. log.Printf("Failed to create basic auth client: %v", err)
  40. } else {
  41. fmt.Println("Basic auth client created")
  42. }
  43. // 方法3:使用Token认证
  44. if _, err := configure.NewTokenAuthClient("http://localhost:8080", "your-token-here"); err != nil {
  45. log.Printf("Failed to create token auth client: %v", err)
  46. } else {
  47. fmt.Println("Token auth client created")
  48. }
  49. fmt.Println()
  50. }
  51. func exampleListTables() {
  52. fmt.Println("=== 示例2:查询数据库表字典列表 ===")
  53. // 创建客户端
  54. client, err := configure.NewBasicAuthClient("http://localhost:8080", "admin", "123")
  55. if err != nil {
  56. log.Printf("Failed to create client: %v", err)
  57. return
  58. }
  59. // 创建查询请求
  60. query := &configure.DicTableQueryRequest{
  61. QueryRequest: queryreq.QueryRequest{
  62. Page: 0,
  63. PageSize: 10,
  64. },
  65. }
  66. // 执行查询
  67. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  68. defer cancel()
  69. result, err := client.ListTables(ctx, query)
  70. if err != nil {
  71. log.Printf("Error listing tables: %v", err)
  72. return
  73. }
  74. fmt.Printf("Total tables: %d\n", result.TotalCount)
  75. fmt.Printf("Last page: %d\n", result.LastPage)
  76. fmt.Println()
  77. }
  78. func exampleSaveTable() {
  79. fmt.Println("=== 示例3:创建数据库表字典 ===")
  80. // 创建客户端
  81. client, err := configure.NewBasicAuthClient("http://localhost:8080", "admin", "123")
  82. if err != nil {
  83. log.Printf("Failed to create client: %v", err)
  84. return
  85. }
  86. // 创建请求
  87. req := &configure.DicTableRequest{
  88. TableID: "example_table_001",
  89. TableType: "实体表",
  90. Name: "示例表001",
  91. Description: "这是一个示例表",
  92. Fields: []configure.DicTableFieldRequest{
  93. {
  94. FieldID: "example_table_001.id",
  95. TableID: "example_table_001",
  96. FiledType: "实际字段",
  97. DataType: "数值型",
  98. FieldName: "id",
  99. FieldNameCN: "主键ID",
  100. Description: "主键字段",
  101. },
  102. {
  103. FieldID: "example_table_001.name",
  104. TableID: "example_table_001",
  105. FiledType: "实际字段",
  106. DataType: "字符型",
  107. FieldName: "name",
  108. FieldNameCN: "名称",
  109. Description: "名称字段",
  110. },
  111. },
  112. }
  113. // 执行保存
  114. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  115. defer cancel()
  116. detail, err := client.SaveTable(ctx, req)
  117. if err != nil {
  118. log.Printf("Error saving table: %v", err)
  119. return
  120. }
  121. fmt.Printf("Table saved successfully: %s\n", detail.Table.TableID)
  122. fmt.Printf("Table name: %s\n", detail.Table.Name)
  123. fmt.Printf("Number of fields: %d\n", len(detail.Fields))
  124. fmt.Println()
  125. }
  126. func exampleGetTable() {
  127. fmt.Println("=== 示例4:查询数据库表字典详情 ===")
  128. // 创建客户端
  129. client, err := configure.NewBasicAuthClient("http://localhost:8080", "admin", "123")
  130. if err != nil {
  131. log.Printf("Failed to create client: %v", err)
  132. return
  133. }
  134. // 查询表详情
  135. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  136. defer cancel()
  137. tableID := "example_table_001"
  138. detail, err := client.GetTable(ctx, tableID)
  139. if err != nil {
  140. if err == configure.ErrNotFound {
  141. fmt.Printf("Table %s not found\n", tableID)
  142. } else {
  143. log.Printf("Error getting table: %v", err)
  144. }
  145. return
  146. }
  147. fmt.Printf("Table ID: %s\n", detail.Table.TableID)
  148. fmt.Printf("Table Type: %s\n", detail.Table.TableType)
  149. fmt.Printf("Name: %s\n", detail.Table.Name)
  150. fmt.Printf("Description: %s\n", detail.Table.Description)
  151. fmt.Printf("Creator: %s\n", detail.Table.Creator)
  152. fmt.Printf("Fields count: %d\n", len(detail.Fields))
  153. fmt.Println()
  154. }
  155. func exampleDeleteTable() {
  156. fmt.Println("=== 示例5:删除数据库表字典 ===")
  157. // 创建客户端
  158. client, err := configure.NewBasicAuthClient("http://localhost:8080", "admin", "123")
  159. if err != nil {
  160. log.Printf("Failed to create client: %v", err)
  161. return
  162. }
  163. // 删除表
  164. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  165. defer cancel()
  166. tableID := "example_table_001"
  167. if err := client.DeleteTable(ctx, tableID); err != nil {
  168. if err == configure.ErrNotFound {
  169. fmt.Printf("Table %s not found\n", tableID)
  170. } else {
  171. log.Printf("Error deleting table: %v", err)
  172. }
  173. return
  174. }
  175. fmt.Printf("Table %s deleted successfully\n", tableID)
  176. fmt.Println()
  177. }
  178. func exampleAliasManagement() {
  179. fmt.Println("=== 示例6:别名字典管理 ===")
  180. // 创建客户端
  181. client, err := configure.NewBasicAuthClient("http://localhost:8080", "admin", "123")
  182. if err != nil {
  183. log.Printf("Failed to create client: %v", err)
  184. return
  185. }
  186. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  187. defer cancel()
  188. // 1. 创建表别名字典
  189. tableAliasReq := &configure.TableAliasRequest{
  190. TableID: "example_table_001",
  191. TableAlias: "示例表别名",
  192. }
  193. tableAliasDetail, err := client.SaveTableAlias(ctx, tableAliasReq)
  194. if err != nil {
  195. log.Printf("Error saving table alias: %v", err)
  196. return
  197. }
  198. fmt.Printf("Table alias saved: ID=%s, TableID=%s, Alias=%s\n",
  199. tableAliasDetail.TableAlias.ID,
  200. tableAliasDetail.TableAlias.TableID,
  201. tableAliasDetail.TableAlias.TableAlias)
  202. // 2. 查询表别名字典列表
  203. query := &configure.TableAliasQueryRequest{
  204. QueryRequest: queryreq.QueryRequest{
  205. Page: 0,
  206. PageSize: 10,
  207. },
  208. TableID: "example_table_001",
  209. }
  210. tableAliasList, err := client.ListTableAliases(ctx, query)
  211. if err != nil {
  212. log.Printf("Error listing table aliases: %v", err)
  213. return
  214. }
  215. fmt.Printf("Total table aliases: %d\n", tableAliasList.TotalCount)
  216. // 3. 创建字段别名字典
  217. fieldAliasReq := &configure.TableFieldAliasRequest{
  218. FieldID: "example_table_001.id",
  219. TableID: "example_table_001",
  220. FieldName: "id",
  221. FieldAlias: "主键别名",
  222. Description: "主键字段别名",
  223. WhereCondition: "查询条件描述",
  224. }
  225. fieldAliasDetail, err := client.SaveTableFieldAlias(ctx, fieldAliasReq)
  226. if err != nil {
  227. log.Printf("Error saving field alias: %v", err)
  228. return
  229. }
  230. fmt.Printf("Field alias saved: ID=%s, FieldID=%s, Alias=%s\n",
  231. fieldAliasDetail.TableFieldAlias.ID,
  232. fieldAliasDetail.TableFieldAlias.FieldID,
  233. fieldAliasDetail.TableFieldAlias.FieldAlias)
  234. // 4. 查询字段别名字典列表
  235. fieldQuery := &configure.TableFieldAliasQueryRequest{
  236. QueryRequest: queryreq.QueryRequest{
  237. Page: 0,
  238. PageSize: 10,
  239. },
  240. TableID: "example_table_001",
  241. }
  242. fieldAliasList, err := client.ListTableFieldAliases(ctx, fieldQuery)
  243. if err != nil {
  244. log.Printf("Error listing field aliases: %v", err)
  245. return
  246. }
  247. fmt.Printf("Total field aliases: %d\n", fieldAliasList.TotalCount)
  248. fmt.Println()
  249. }