tapplicationcredentialsmodel.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. var _ TApplicationCredentialsModel = (*defaultTApplicationCredentialsModel)(nil)
  11. type (
  12. TApplicationCredentials struct {
  13. Id int64 `db:"id"` // 凭证 ID
  14. CredIden string `db:"cred_iden"` // 应用标识
  15. Name string `db:"name"` // 应用名称
  16. ApiKey string `db:"api_key"` // 对外公钥
  17. ApiSecret string `db:"api_secret"` // HMAC 原始密钥(明文存储)
  18. HmacAlgo string `db:"hmac_algo"` // 签名算法
  19. Status int64 `db:"status"` // 启用状态:1=启用,2=禁用
  20. Description sql.NullString `db:"description"` // 应用描述
  21. CreatedBy string `db:"created_by"` // 创建人
  22. CreatedAt time.Time `db:"created_at"` // 创建时间
  23. UpdatedBy string `db:"updated_by"` // 更新人
  24. UpdatedAt time.Time `db:"updated_at"` // 更新时间
  25. }
  26. TApplicationCredentialsModel interface {
  27. Insert(ctx context.Context, data *TApplicationCredentials) (sql.Result, error)
  28. FindOne(ctx context.Context, id int64) (*TApplicationCredentials, error)
  29. ExistsByName(ctx context.Context, name string) (bool, error)
  30. Update(ctx context.Context, data *TApplicationCredentials) error
  31. Delete(ctx context.Context, id int64) error
  32. FindPage(ctx context.Context, page, pageSize int64, status int64, credIden, name string) ([]TApplicationCredentials, int64, error)
  33. }
  34. )
  35. // tApplicationCredentialsRows is the full column list.
  36. var tApplicationCredentialsRows = strings.Join([]string{
  37. "`id`", "`cred_iden`", "`name`", "`api_key`", "`api_secret`", "`hmac_algo`",
  38. "`status`", "`description`", "`created_by`", "`created_at`",
  39. "`updated_by`", "`updated_at`",
  40. }, ", ")
  41. type defaultTApplicationCredentialsModel struct {
  42. conn sqlx.SqlConn
  43. table string
  44. }
  45. // NewTApplicationCredentialsModel returns a model for the database table.
  46. func NewTApplicationCredentialsModel(conn sqlx.SqlConn) TApplicationCredentialsModel {
  47. return &defaultTApplicationCredentialsModel{
  48. conn: conn,
  49. table: "`t_application_credentials`",
  50. }
  51. }
  52. // Insert stores one credential row; CreatedAt/UpdatedAt are filled by
  53. // DEFAULT CURRENT_TIMESTAMP, updated_at also auto-refreshes via ON UPDATE.
  54. func (m *defaultTApplicationCredentialsModel) Insert(ctx context.Context, data *TApplicationCredentials) (sql.Result, error) {
  55. query := fmt.Sprintf("insert into %s (`cred_iden`, `name`, `api_key`, `api_secret`, `hmac_algo`, `status`, `description`, `created_by`, `updated_by`) values (?, ?, ?, ?, ?, ?, ?, ?, ?)",
  56. m.table)
  57. ret, err := m.conn.ExecCtx(ctx, query,
  58. data.CredIden, data.Name, data.ApiKey, data.ApiSecret, data.HmacAlgo,
  59. data.Status, data.Description, data.CreatedBy, data.UpdatedBy)
  60. return ret, err
  61. }
  62. // FindOne looks a credential up by its auto-increment id.
  63. func (m *defaultTApplicationCredentialsModel) FindOne(ctx context.Context, id int64) (*TApplicationCredentials, error) {
  64. query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", tApplicationCredentialsRows, m.table)
  65. var resp TApplicationCredentials
  66. err := m.conn.QueryRowCtx(ctx, &resp, query, id)
  67. switch err {
  68. case nil:
  69. return &resp, nil
  70. case sqlx.ErrNotFound:
  71. return nil, ErrNotFound
  72. default:
  73. return nil, err
  74. }
  75. }
  76. // ExistsByName reports whether a credential with the same name exists.
  77. func (m *defaultTApplicationCredentialsModel) ExistsByName(ctx context.Context, name string) (bool, error) {
  78. query := fmt.Sprintf("select count(1) from %s where `name` = ?", m.table)
  79. var count int64
  80. err := m.conn.QueryRowCtx(ctx, &count, query, name)
  81. switch err {
  82. case nil:
  83. return count > 0, nil
  84. case sqlx.ErrNotFound:
  85. return false, nil
  86. default:
  87. return false, err
  88. }
  89. }
  90. // Update writes the mutable fields of the row. created_at is untouched and
  91. // updated_at refreshes via the table's ON UPDATE CURRENT_TIMESTAMP.
  92. func (m *defaultTApplicationCredentialsModel) Update(ctx context.Context, data *TApplicationCredentials) error {
  93. query := fmt.Sprintf("update %s set `name` = ?, `api_key` = ?, `api_secret` = ?, `hmac_algo` = ?, `status` = ?, `description` = ?, `updated_by` = ? where `id` = ?",
  94. m.table)
  95. _, err := m.conn.ExecCtx(ctx, query,
  96. data.Name, data.ApiKey, data.ApiSecret, data.HmacAlgo,
  97. data.Status, data.Description, data.UpdatedBy, data.Id)
  98. return err
  99. }
  100. // Delete removes the credential row by its id.
  101. func (m *defaultTApplicationCredentialsModel) Delete(ctx context.Context, id int64) error {
  102. query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
  103. _, err := m.conn.ExecCtx(ctx, query, id)
  104. return err
  105. }
  106. // FindPage returns one page of rows ordered by id with the total count.
  107. // status=0 and empty credIden/name mean no filtering; name matches by LIKE.
  108. func (m *defaultTApplicationCredentialsModel) FindPage(ctx context.Context, page, pageSize int64, status int64, credIden, name string) ([]TApplicationCredentials, int64, error) {
  109. var conds []string
  110. var args []any
  111. if status != 0 {
  112. conds = append(conds, "`status` = ?")
  113. args = append(args, status)
  114. }
  115. if credIden != "" {
  116. conds = append(conds, "`cred_iden` = ?")
  117. args = append(args, credIden)
  118. }
  119. if keyword, ok := trimName(name); ok {
  120. conds = append(conds, "`name` like ? escape '\\\\'")
  121. args = append(args, "%"+escapeLike(keyword)+"%")
  122. }
  123. where := ""
  124. if len(conds) > 0 {
  125. where = " where " + strings.Join(conds, " and ")
  126. }
  127. var total int64
  128. if err := m.conn.QueryRowCtx(ctx, &total, fmt.Sprintf("select count(*) from %s%s", m.table, where), args...); err != nil {
  129. return nil, 0, err
  130. }
  131. if total == 0 {
  132. return nil, 0, nil
  133. }
  134. offset := (page - 1) * pageSize
  135. args = append(args, pageSize, offset)
  136. query := fmt.Sprintf("select %s from %s%s order by `id` limit ? offset ?", tApplicationCredentialsRows, m.table, where)
  137. var resp []TApplicationCredentials
  138. if err := m.conn.QueryRowsCtx(ctx, &resp, query, args...); err != nil {
  139. return nil, 0, err
  140. }
  141. return resp, total, nil
  142. }