// Code generated by goctl. DO NOT EDIT. package genModel import ( "context" "database/sql" "fmt" "strings" "time" "github.com/zeromicro/go-zero/core/stores/builder" "github.com/zeromicro/go-zero/core/stores/cache" "github.com/zeromicro/go-zero/core/stores/sqlc" "github.com/zeromicro/go-zero/core/stores/sqlx" "github.com/zeromicro/go-zero/core/stringx" ) var ( serverClientFieldNames = builder.RawFieldNames(&ServerClient{}) serverClientRows = strings.Join(serverClientFieldNames, ",") serverClientRowsExpectAutoSet = strings.Join(stringx.Remove(serverClientFieldNames, "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",") serverClientRowsWithPlaceHolder = strings.Join(stringx.Remove(serverClientFieldNames, "`client_id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?" cacheTransferServerClientClientIdPrefix = "cache:transfer:serverClient:clientId:" cacheTransferServerClientClientIndexPrefix = "cache:transfer:serverClient:clientIndex:" ) type ( serverClientModel interface { Insert(ctx context.Context, data *ServerClient) (sql.Result, error) FindOne(ctx context.Context, clientId int64) (*ServerClient, error) Update(ctx context.Context, data *ServerClient) error Delete(ctx context.Context, clientId int64) error FindOneByIndex(ctx context.Context, index string) (*ServerClient, error) } defaultServerClientModel struct { sqlc.CachedConn table string } ServerClient struct { ClientId int64 `db:"client_id"` ClientName string `db:"client_name"` // 客户名称 ClientIndex string `db:"client_index"` // 客户索引 ZltxMerchantId int64 `db:"zltx_merchant_id"` ZltxSecret string `db:"zltx_secret"` CreateTime time.Time `db:"create_time"` UpdateTime time.Time `db:"update_time"` Status int64 `db:"status"` } ) func newServerClientModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultServerClientModel { return &defaultServerClientModel{ CachedConn: sqlc.NewConn(conn, c, opts...), table: "`server_client`", } } func (m *defaultServerClientModel) Delete(ctx context.Context, clientId int64) error { transferServerClientClientIdKey := fmt.Sprintf("%s%v", cacheTransferServerClientClientIdPrefix, clientId) _, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { query := fmt.Sprintf("delete from %s where `client_id` = ?", m.table) return conn.ExecCtx(ctx, query, clientId) }, transferServerClientClientIdKey) return err } func (m *defaultServerClientModel) FindOne(ctx context.Context, clientId int64) (*ServerClient, error) { transferServerClientClientIdKey := fmt.Sprintf("%s%v", cacheTransferServerClientClientIdPrefix, clientId) var resp ServerClient err := m.QueryRowCtx(ctx, &resp, transferServerClientClientIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error { query := fmt.Sprintf("select %s from %s where `client_id` = ? limit 1", serverClientRows, m.table) return conn.QueryRowCtx(ctx, v, query, clientId) }) switch err { case nil: return &resp, nil case sqlc.ErrNotFound: return nil, ErrNotFound default: return nil, err } } func (m *defaultServerClientModel) FindOneByIndex(ctx context.Context, index string) (*ServerClient, error) { transferServerClientClientIndexKey := fmt.Sprintf("%s%v", cacheTransferServerClientClientIdPrefix, index) var resp ServerClient err := m.QueryRowCtx(ctx, &resp, transferServerClientClientIndexKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error { query := fmt.Sprintf("select %s from %s where `client_id` = ? limit 1", serverClientRows, m.table) return conn.QueryRowCtx(ctx, v, query, index) }) switch err { case nil: return &resp, nil case sqlc.ErrNotFound: return nil, ErrNotFound default: return nil, err } } func (m *defaultServerClientModel) Insert(ctx context.Context, data *ServerClient) (sql.Result, error) { transferServerClientClientIdKey := fmt.Sprintf("%s%v", cacheTransferServerClientClientIdPrefix, data.ClientId) ret, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?)", m.table, serverClientRowsExpectAutoSet) return conn.ExecCtx(ctx, query, data.ClientId, data.ClientName, data.ClientIndex, data.ZltxMerchantId, data.ZltxSecret, data.Status) }, transferServerClientClientIdKey) return ret, err } func (m *defaultServerClientModel) Update(ctx context.Context, data *ServerClient) error { transferServerClientClientIdKey := fmt.Sprintf("%s%v", cacheTransferServerClientClientIdPrefix, data.ClientId) _, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { query := fmt.Sprintf("update %s set %s where `client_id` = ?", m.table, serverClientRowsWithPlaceHolder) return conn.ExecCtx(ctx, query, data.ClientName, data.ClientIndex, data.ZltxMerchantId, data.ZltxSecret, data.Status, data.ClientId) }, transferServerClientClientIdKey) return err } func (m *defaultServerClientModel) formatPrimary(primary any) string { return fmt.Sprintf("%s%v", cacheTransferServerClientClientIdPrefix, primary) } func (m *defaultServerClientModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error { query := fmt.Sprintf("select %s from %s where `client_id` = ? limit 1", serverClientRows, m.table) return conn.QueryRowCtx(ctx, v, query, primary) } func (m *defaultServerClientModel) tableName() string { return m.table }