package model import ( "context" "database/sql" "fmt" "strings" "time" "github.com/zeromicro/go-zero/core/stores/sqlx" ) var _ TApplicationCredentialsModel = (*defaultTApplicationCredentialsModel)(nil) type ( TApplicationCredentials struct { Id int64 `db:"id"` // 凭证 ID CredIden string `db:"cred_iden"` // 应用标识 Name string `db:"name"` // 应用名称 ApiKey string `db:"api_key"` // 对外公钥 ApiSecret string `db:"api_secret"` // HMAC 原始密钥(明文存储) HmacAlgo string `db:"hmac_algo"` // 签名算法 Status int64 `db:"status"` // 启用状态:1=启用,2=禁用 Description sql.NullString `db:"description"` // 应用描述 CreatedBy string `db:"created_by"` // 创建人 CreatedAt time.Time `db:"created_at"` // 创建时间 UpdatedBy string `db:"updated_by"` // 更新人 UpdatedAt time.Time `db:"updated_at"` // 更新时间 } TApplicationCredentialsModel interface { Insert(ctx context.Context, data *TApplicationCredentials) (sql.Result, error) FindOne(ctx context.Context, id int64) (*TApplicationCredentials, error) FindByApiKey(ctx context.Context, apiKey string) (string, error) ExistsByName(ctx context.Context, name string) (bool, error) Update(ctx context.Context, data *TApplicationCredentials) error Delete(ctx context.Context, id int64) error FindPage(ctx context.Context, page, pageSize int64, status int64, credIden, name string) ([]TApplicationCredentials, int64, error) } ) // tApplicationCredentialsRows is the full column list. var tApplicationCredentialsRows = strings.Join([]string{ "`id`", "`cred_iden`", "`name`", "`api_key`", "`api_secret`", "`hmac_algo`", "`status`", "`description`", "`created_by`", "`created_at`", "`updated_by`", "`updated_at`", }, ", ") type defaultTApplicationCredentialsModel struct { conn sqlx.SqlConn table string } // NewTApplicationCredentialsModel returns a model for the database table. func NewTApplicationCredentialsModel(conn sqlx.SqlConn) TApplicationCredentialsModel { return &defaultTApplicationCredentialsModel{ conn: conn, table: "`t_application_credentials`", } } // Insert stores one credential row; CreatedAt/UpdatedAt are filled by // DEFAULT CURRENT_TIMESTAMP, updated_at also auto-refreshes via ON UPDATE. func (m *defaultTApplicationCredentialsModel) Insert(ctx context.Context, data *TApplicationCredentials) (sql.Result, error) { query := fmt.Sprintf("insert into %s (`cred_iden`, `name`, `api_key`, `api_secret`, `hmac_algo`, `status`, `description`, `created_by`, `updated_by`) values (?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table) ret, err := m.conn.ExecCtx(ctx, query, data.CredIden, data.Name, data.ApiKey, data.ApiSecret, data.HmacAlgo, data.Status, data.Description, data.CreatedBy, data.UpdatedBy) return ret, err } // FindOne looks a credential up by its auto-increment id. func (m *defaultTApplicationCredentialsModel) FindOne(ctx context.Context, id int64) (*TApplicationCredentials, error) { query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", tApplicationCredentialsRows, m.table) var resp TApplicationCredentials err := m.conn.QueryRowCtx(ctx, &resp, query, id) switch err { case nil: return &resp, nil case sqlx.ErrNotFound: return nil, ErrNotFound default: return nil, err } } func (m *defaultTApplicationCredentialsModel) FindByApiKey(ctx context.Context, apiKey string) (string, error) { query := fmt.Sprintf("select `api_secret` from %s where `api_key` = ? limit 1", m.table) var apiSecret string err := m.conn.QueryRowCtx(ctx, &apiSecret, query, apiKey) switch err { case nil: return apiSecret, nil case sqlx.ErrNotFound: return "", ErrNotFound default: return "", err } } // ExistsByName reports whether a credential with the same name exists. func (m *defaultTApplicationCredentialsModel) ExistsByName(ctx context.Context, name string) (bool, error) { query := fmt.Sprintf("select count(1) from %s where `name` = ?", m.table) var count int64 err := m.conn.QueryRowCtx(ctx, &count, query, name) switch err { case nil: return count > 0, nil case sqlx.ErrNotFound: return false, nil default: return false, err } } // Update writes the mutable fields of the row. created_at is untouched and // updated_at refreshes via the table's ON UPDATE CURRENT_TIMESTAMP. func (m *defaultTApplicationCredentialsModel) Update(ctx context.Context, data *TApplicationCredentials) error { query := fmt.Sprintf("update %s set `name` = ?, `api_key` = ?, `api_secret` = ?, `hmac_algo` = ?, `status` = ?, `description` = ?, `updated_by` = ? where `id` = ?", m.table) _, err := m.conn.ExecCtx(ctx, query, data.Name, data.ApiKey, data.ApiSecret, data.HmacAlgo, data.Status, data.Description, data.UpdatedBy, data.Id) return err } // Delete removes the credential row by its id. func (m *defaultTApplicationCredentialsModel) Delete(ctx context.Context, id int64) error { query := fmt.Sprintf("delete from %s where `id` = ?", m.table) _, err := m.conn.ExecCtx(ctx, query, id) return err } // FindPage returns one page of rows ordered by id with the total count. // status=0 and empty credIden/name mean no filtering; name matches by LIKE. func (m *defaultTApplicationCredentialsModel) FindPage(ctx context.Context, page, pageSize int64, status int64, credIden, name string) ([]TApplicationCredentials, int64, error) { var conds []string var args []any if status != 0 { conds = append(conds, "`status` = ?") args = append(args, status) } if credIden != "" { conds = append(conds, "`cred_iden` = ?") args = append(args, credIden) } if keyword, ok := trimName(name); ok { conds = append(conds, "`name` like ? escape '\\\\'") args = append(args, "%"+escapeLike(keyword)+"%") } where := "" if len(conds) > 0 { where = " where " + strings.Join(conds, " and ") } var total int64 if err := m.conn.QueryRowCtx(ctx, &total, fmt.Sprintf("select count(*) from %s%s", m.table, where), args...); err != nil { return nil, 0, err } if total == 0 { return nil, 0, nil } offset := (page - 1) * pageSize args = append(args, pageSize, offset) query := fmt.Sprintf("select %s from %s%s order by `id` limit ? offset ?", tApplicationCredentialsRows, m.table, where) var resp []TApplicationCredentials if err := m.conn.QueryRowsCtx(ctx, &resp, query, args...); err != nil { return nil, 0, err } return resp, total, nil }