tapplicationcredentialsmodel.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. FindByApiKey(ctx context.Context, apiKey string) (string, error)
  30. ExistsByName(ctx context.Context, name string) (bool, error)
  31. Update(ctx context.Context, data *TApplicationCredentials) error
  32. Delete(ctx context.Context, id int64) error
  33. FindPage(ctx context.Context, page, pageSize int64, status int64, credIden, name string) ([]TApplicationCredentials, int64, error)
  34. }
  35. )
  36. // tApplicationCredentialsRows is the full column list.
  37. var tApplicationCredentialsRows = strings.Join([]string{
  38. "`id`", "`cred_iden`", "`name`", "`api_key`", "`api_secret`", "`hmac_algo`",
  39. "`status`", "`description`", "`created_by`", "`created_at`",
  40. "`updated_by`", "`updated_at`",
  41. }, ", ")
  42. type defaultTApplicationCredentialsModel struct {
  43. conn sqlx.SqlConn
  44. table string
  45. }
  46. // NewTApplicationCredentialsModel returns a model for the database table.
  47. func NewTApplicationCredentialsModel(conn sqlx.SqlConn) TApplicationCredentialsModel {
  48. return &defaultTApplicationCredentialsModel{
  49. conn: conn,
  50. table: "`t_application_credentials`",
  51. }
  52. }
  53. // Insert stores one credential row; CreatedAt/UpdatedAt are filled by
  54. // DEFAULT CURRENT_TIMESTAMP, updated_at also auto-refreshes via ON UPDATE.
  55. func (m *defaultTApplicationCredentialsModel) Insert(ctx context.Context, data *TApplicationCredentials) (sql.Result, error) {
  56. query := fmt.Sprintf("insert into %s (`cred_iden`, `name`, `api_key`, `api_secret`, `hmac_algo`, `status`, `description`, `created_by`, `updated_by`) values (?, ?, ?, ?, ?, ?, ?, ?, ?)",
  57. m.table)
  58. ret, err := m.conn.ExecCtx(ctx, query,
  59. data.CredIden, data.Name, data.ApiKey, data.ApiSecret, data.HmacAlgo,
  60. data.Status, data.Description, data.CreatedBy, data.UpdatedBy)
  61. return ret, err
  62. }
  63. // FindOne looks a credential up by its auto-increment id.
  64. func (m *defaultTApplicationCredentialsModel) FindOne(ctx context.Context, id int64) (*TApplicationCredentials, error) {
  65. query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", tApplicationCredentialsRows, m.table)
  66. var resp TApplicationCredentials
  67. err := m.conn.QueryRowCtx(ctx, &resp, query, id)
  68. switch err {
  69. case nil:
  70. return &resp, nil
  71. case sqlx.ErrNotFound:
  72. return nil, ErrNotFound
  73. default:
  74. return nil, err
  75. }
  76. }
  77. func (m *defaultTApplicationCredentialsModel) FindByApiKey(ctx context.Context, apiKey string) (string, error) {
  78. query := fmt.Sprintf("select `api_secret` from %s where `api_key` = ? limit 1", m.table)
  79. var apiSecret string
  80. err := m.conn.QueryRowCtx(ctx, &apiSecret, query, apiKey)
  81. switch err {
  82. case nil:
  83. return apiSecret, nil
  84. case sqlx.ErrNotFound:
  85. return "", ErrNotFound
  86. default:
  87. return "", err
  88. }
  89. }
  90. // ExistsByName reports whether a credential with the same name exists.
  91. func (m *defaultTApplicationCredentialsModel) ExistsByName(ctx context.Context, name string) (bool, error) {
  92. query := fmt.Sprintf("select count(1) from %s where `name` = ?", m.table)
  93. var count int64
  94. err := m.conn.QueryRowCtx(ctx, &count, query, name)
  95. switch err {
  96. case nil:
  97. return count > 0, nil
  98. case sqlx.ErrNotFound:
  99. return false, nil
  100. default:
  101. return false, err
  102. }
  103. }
  104. // Update writes the mutable fields of the row. created_at is untouched and
  105. // updated_at refreshes via the table's ON UPDATE CURRENT_TIMESTAMP.
  106. func (m *defaultTApplicationCredentialsModel) Update(ctx context.Context, data *TApplicationCredentials) error {
  107. query := fmt.Sprintf("update %s set `name` = ?, `api_key` = ?, `api_secret` = ?, `hmac_algo` = ?, `status` = ?, `description` = ?, `updated_by` = ? where `id` = ?",
  108. m.table)
  109. _, err := m.conn.ExecCtx(ctx, query,
  110. data.Name, data.ApiKey, data.ApiSecret, data.HmacAlgo,
  111. data.Status, data.Description, data.UpdatedBy, data.Id)
  112. return err
  113. }
  114. // Delete removes the credential row by its id.
  115. func (m *defaultTApplicationCredentialsModel) Delete(ctx context.Context, id int64) error {
  116. query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
  117. _, err := m.conn.ExecCtx(ctx, query, id)
  118. return err
  119. }
  120. // FindPage returns one page of rows ordered by id with the total count.
  121. // status=0 and empty credIden/name mean no filtering; name matches by LIKE.
  122. func (m *defaultTApplicationCredentialsModel) FindPage(ctx context.Context, page, pageSize int64, status int64, credIden, name string) ([]TApplicationCredentials, int64, error) {
  123. var conds []string
  124. var args []any
  125. if status != 0 {
  126. conds = append(conds, "`status` = ?")
  127. args = append(args, status)
  128. }
  129. if credIden != "" {
  130. conds = append(conds, "`cred_iden` = ?")
  131. args = append(args, credIden)
  132. }
  133. if keyword, ok := trimName(name); ok {
  134. conds = append(conds, "`name` like ? escape '\\\\'")
  135. args = append(args, "%"+escapeLike(keyword)+"%")
  136. }
  137. where := ""
  138. if len(conds) > 0 {
  139. where = " where " + strings.Join(conds, " and ")
  140. }
  141. var total int64
  142. if err := m.conn.QueryRowCtx(ctx, &total, fmt.Sprintf("select count(*) from %s%s", m.table, where), args...); err != nil {
  143. return nil, 0, err
  144. }
  145. if total == 0 {
  146. return nil, 0, nil
  147. }
  148. offset := (page - 1) * pageSize
  149. args = append(args, pageSize, offset)
  150. query := fmt.Sprintf("select %s from %s%s order by `id` limit ? offset ?", tApplicationCredentialsRows, m.table, where)
  151. var resp []TApplicationCredentials
  152. if err := m.conn.QueryRowsCtx(ctx, &resp, query, args...); err != nil {
  153. return nil, 0, err
  154. }
  155. return resp, total, nil
  156. }