tconfigmodel.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package model
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "strings"
  7. "time"
  8. "github.com/zeromicro/go-zero/core/stores/sqlx"
  9. )
  10. // TConfig is one dynamic configuration row. Hot-reloadable audit settings
  11. // (MinIO endpoint/keys, log level, archive threshold, HMAC secret) are
  12. // stored here and read back on every request by the audit middleware.
  13. type TConfig struct {
  14. Id int64 `db:"id"` // 主键
  15. ConfigItem string `db:"config_item"` // 配置项
  16. ConfigValue string `db:"config_value"` // 配置值
  17. EditFlag int64 `db:"edit_flag"` // 是否允许编辑,1是不可编辑configItem,2是所有均可编辑
  18. Description sql.NullString `db:"description"` // 描述
  19. UpdatedBy string `db:"updated_by"` // 最后更新人
  20. UpdatedAt time.Time `db:"updated_at"` // 配置更新时间
  21. }
  22. type TConfigModel interface {
  23. // FindAll returns every configuration row ordered by id.
  24. FindAll(ctx context.Context) ([]TConfig, error)
  25. // Config management APIs (ported from sdf-serve applicationConfig).
  26. Insert(ctx context.Context, data *TConfig) (sql.Result, error)
  27. FindOne(ctx context.Context, id int64) (*TConfig, error)
  28. FindOneByConfigItem(ctx context.Context, configItem string) (*TConfig, error)
  29. Update(ctx context.Context, data *TConfig) error
  30. Delete(ctx context.Context, id int64) error
  31. FindPage(ctx context.Context, page, pageSize int64, configItem, description string) ([]TConfig, int64, error)
  32. }
  33. type customTConfigModel struct {
  34. conn sqlx.SqlConn
  35. table string
  36. }
  37. var _ TConfigModel = (*customTConfigModel)(nil)
  38. // NewTConfigModel returns the model for the t_config table.
  39. func NewTConfigModel(conn sqlx.SqlConn) TConfigModel {
  40. return &customTConfigModel{
  41. conn: conn,
  42. table: "`t_config`",
  43. }
  44. }
  45. // tConfigRows is the full column list of t_config.
  46. var tConfigRows = strings.Join([]string{
  47. "`id`", "`config_item`", "`config_value`", "`edit_flag`",
  48. "`description`", "`updated_by`", "`updated_at`",
  49. }, ", ")
  50. func (m *customTConfigModel) FindAll(ctx context.Context) ([]TConfig, error) {
  51. var resp []TConfig
  52. query := fmt.Sprintf("select %s from %s order by `id` asc", tConfigRows, m.table)
  53. if err := m.conn.QueryRowsCtx(ctx, &resp, query); err != nil {
  54. return nil, err
  55. }
  56. return resp, nil
  57. }
  58. // Insert stores one configuration row; updated_at is filled by
  59. // DEFAULT CURRENT_TIMESTAMP and refreshes via ON UPDATE.
  60. func (m *customTConfigModel) Insert(ctx context.Context, data *TConfig) (sql.Result, error) {
  61. query := fmt.Sprintf("insert into %s (`config_item`, `config_value`, `edit_flag`, `description`, `updated_by`) values (?, ?, ?, ?, ?)", m.table)
  62. return m.conn.ExecCtx(ctx, query,
  63. data.ConfigItem, data.ConfigValue, data.EditFlag, data.Description, data.UpdatedBy)
  64. }
  65. // FindOne looks a config row up by its auto-increment id.
  66. func (m *customTConfigModel) FindOne(ctx context.Context, id int64) (*TConfig, error) {
  67. query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", tConfigRows, m.table)
  68. var resp TConfig
  69. switch err := m.conn.QueryRowCtx(ctx, &resp, query, id); err {
  70. case nil:
  71. return &resp, nil
  72. case sqlx.ErrNotFound:
  73. return nil, ErrNotFound
  74. default:
  75. return nil, err
  76. }
  77. }
  78. // FindOneByConfigItem looks a config row up by its unique config_item.
  79. func (m *customTConfigModel) FindOneByConfigItem(ctx context.Context, configItem string) (*TConfig, error) {
  80. query := fmt.Sprintf("select %s from %s where `config_item` = ? limit 1", tConfigRows, m.table)
  81. var resp TConfig
  82. switch err := m.conn.QueryRowCtx(ctx, &resp, query, configItem); err {
  83. case nil:
  84. return &resp, nil
  85. case sqlx.ErrNotFound:
  86. return nil, ErrNotFound
  87. default:
  88. return nil, err
  89. }
  90. }
  91. // Update writes the mutable fields; updated_at refreshes via the table's
  92. // ON UPDATE CURRENT_TIMESTAMP.
  93. func (m *customTConfigModel) Update(ctx context.Context, data *TConfig) error {
  94. query := fmt.Sprintf("update %s set `config_item` = ?, `config_value` = ?, `edit_flag` = ?, `description` = ?, `updated_by` = ? where `id` = ?", m.table)
  95. _, err := m.conn.ExecCtx(ctx, query,
  96. data.ConfigItem, data.ConfigValue, data.EditFlag, data.Description, data.UpdatedBy, data.Id)
  97. return err
  98. }
  99. // Delete removes the configuration row by its id.
  100. func (m *customTConfigModel) Delete(ctx context.Context, id int64) error {
  101. query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
  102. _, err := m.conn.ExecCtx(ctx, query, id)
  103. return err
  104. }
  105. // FindPage returns one page of rows ordered by id with the total count.
  106. // Empty configItem means no item filter; description matches by LIKE.
  107. func (m *customTConfigModel) FindPage(ctx context.Context, page, pageSize int64, configItem, description string) ([]TConfig, int64, error) {
  108. var conds []string
  109. var args []any
  110. if configItem != "" {
  111. conds = append(conds, "`config_item` = ?")
  112. args = append(args, configItem)
  113. }
  114. if keyword, ok := trimKeyword(description); ok {
  115. conds = append(conds, "`description` like ? escape '\\\\'")
  116. args = append(args, "%"+escapeLike(keyword)+"%")
  117. }
  118. where := ""
  119. if len(conds) > 0 {
  120. where = " where " + strings.Join(conds, " and ")
  121. }
  122. var total int64
  123. if err := m.conn.QueryRowCtx(ctx, &total, fmt.Sprintf("select count(*) from %s%s", m.table, where), args...); err != nil {
  124. return nil, 0, err
  125. }
  126. if total == 0 {
  127. return nil, 0, nil
  128. }
  129. offset := (page - 1) * pageSize
  130. args = append(args, pageSize, offset)
  131. query := fmt.Sprintf("select %s from %s%s order by `id` limit ? offset ?", tConfigRows, m.table, where)
  132. var resp []TConfig
  133. if err := m.conn.QueryRowsCtx(ctx, &resp, query, args...); err != nil {
  134. return nil, 0, err
  135. }
  136. return resp, total, nil
  137. }
  138. // trimKeyword reports whether a keyword carries any non-space character.
  139. func trimKeyword(s string) (string, bool) {
  140. trimmed := strings.TrimSpace(s)
  141. return trimmed, trimmed != ""
  142. }