Nessuna descrizione
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.

get_boot_configs.go 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package bootconfig
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "git.x2erp.com/qdy/go-base/ctx"
  7. "git.x2erp.com/qdy/go-base/logger"
  8. "git.x2erp.com/qdy/go-base/model/response"
  9. "git.x2erp.com/qdy/go-base/util"
  10. "git.x2erp.com/qdy/go-db/factory/database"
  11. )
  12. // BootConfig boot配置项
  13. type BootConfig struct {
  14. ID string `json:"id"`
  15. ConfigName string `json:"config_name"`
  16. YamlName string `json:"yaml_name"`
  17. YamlValue string `json:"yaml_value"`
  18. Creator string `json:"creator,omitempty"`
  19. CreatedAt string `json:"created_at,omitempty"`
  20. }
  21. // GetBootConfigs 获取boot配置项
  22. func GetBootConfigs(ctx context.Context, dbFactory *database.DBFactory, reqCtx *ctx.RequestContext) *response.QueryResult[[]BootConfig] {
  23. logger.Debug("GetBootConfigs-开始获取boot配置项")
  24. // 获取数据库连接
  25. db := dbFactory.GetDB()
  26. // 查询boot相关配置(config_name或yaml_name包含boot)
  27. query := `
  28. SELECT
  29. id,
  30. config_name,
  31. yaml_name,
  32. yaml_value,
  33. creator,
  34. created_at
  35. FROM config_startup
  36. WHERE LOWER(config_name) LIKE '%boot%' OR LOWER(yaml_name) LIKE '%boot%'
  37. ORDER BY config_name, yaml_name
  38. `
  39. rows, err := db.QueryxContext(ctx, query)
  40. if err != nil {
  41. logger.ErrorC(reqCtx, fmt.Sprintf("查询boot配置失败: %v", err))
  42. return util.CreateErrorResult[[]BootConfig](fmt.Sprintf("查询boot配置失败: %v", err), reqCtx)
  43. }
  44. defer rows.Close()
  45. var configs []BootConfig
  46. for rows.Next() {
  47. var config BootConfig
  48. var creator, createdAt interface{}
  49. err := rows.Scan(&config.ID, &config.ConfigName, &config.YamlName, &config.YamlValue, &creator, &createdAt)
  50. if err != nil {
  51. logger.ErrorC(reqCtx, fmt.Sprintf("扫描boot配置数据失败: %v", err))
  52. continue
  53. }
  54. // 处理可能为nil的字段
  55. if creator != nil {
  56. config.Creator = fmt.Sprintf("%v", creator)
  57. }
  58. if createdAt != nil {
  59. config.CreatedAt = fmt.Sprintf("%v", createdAt)
  60. }
  61. configs = append(configs, config)
  62. }
  63. if err := rows.Err(); err != nil {
  64. logger.ErrorC(reqCtx, fmt.Sprintf("遍历boot配置数据失败: %v", err))
  65. return util.CreateErrorResult[[]BootConfig](fmt.Sprintf("遍历boot配置数据失败: %v", err), reqCtx)
  66. }
  67. logger.Debug(fmt.Sprintf("获取到 %d 个boot配置项", len(configs)))
  68. return util.CreateSuccessResultData[[]BootConfig](configs, reqCtx)
  69. }
  70. // SearchBootConfigs 搜索boot配置项
  71. func SearchBootConfigs(ctx context.Context, dbFactory *database.DBFactory, keyword string, reqCtx *ctx.RequestContext) *response.QueryResult[[]BootConfig] {
  72. logger.Debug(fmt.Sprintf("SearchBootConfigs-开始搜索boot配置项: %s", keyword))
  73. // 获取数据库连接
  74. db := dbFactory.GetDB()
  75. // 构建查询条件
  76. query := `
  77. SELECT
  78. id,
  79. config_name,
  80. yaml_name,
  81. yaml_value,
  82. creator,
  83. created_at
  84. FROM config_startup
  85. WHERE (LOWER(config_name) LIKE '%boot%' OR LOWER(yaml_name) LIKE '%boot%')
  86. `
  87. var args []interface{}
  88. if keyword != "" {
  89. query += " AND (LOWER(config_name) LIKE ? OR LOWER(yaml_name) LIKE ? OR LOWER(yaml_value) LIKE ?)"
  90. searchTerm := "%" + strings.ToLower(keyword) + "%"
  91. args = append(args, searchTerm, searchTerm, searchTerm)
  92. }
  93. query += " ORDER BY config_name, yaml_name"
  94. rows, err := db.QueryxContext(ctx, query, args...)
  95. if err != nil {
  96. logger.ErrorC(reqCtx, fmt.Sprintf("搜索boot配置失败: %v", err))
  97. return util.CreateErrorResult[[]BootConfig](fmt.Sprintf("搜索boot配置失败: %v", err), reqCtx)
  98. }
  99. defer rows.Close()
  100. var configs []BootConfig
  101. for rows.Next() {
  102. var config BootConfig
  103. var creator, createdAt interface{}
  104. err := rows.Scan(&config.ID, &config.ConfigName, &config.YamlName, &config.YamlValue, &creator, &createdAt)
  105. if err != nil {
  106. logger.ErrorC(reqCtx, fmt.Sprintf("扫描boot配置数据失败: %v", err))
  107. continue
  108. }
  109. // 处理可能为nil的字段
  110. if creator != nil {
  111. config.Creator = fmt.Sprintf("%v", creator)
  112. }
  113. if createdAt != nil {
  114. config.CreatedAt = fmt.Sprintf("%v", createdAt)
  115. }
  116. configs = append(configs, config)
  117. }
  118. if err := rows.Err(); err != nil {
  119. logger.ErrorC(reqCtx, fmt.Sprintf("遍历boot配置数据失败: %v", err))
  120. return util.CreateErrorResult[[]BootConfig](fmt.Sprintf("遍历boot配置数据失败: %v", err), reqCtx)
  121. }
  122. logger.Debug(fmt.Sprintf("搜索到 %d 个boot配置项", len(configs)))
  123. return util.CreateSuccessResultData[[]BootConfig](configs, reqCtx)
  124. }