66 lines
2.1 KiB
Go
66 lines
2.1 KiB
Go
package model
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"github.com/zeromicro/go-zero/core/stores/cache"
|
|
"github.com/zeromicro/go-zero/core/stores/sqlc"
|
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
|
)
|
|
|
|
var _ AppAccountModel = (*customAppAccountModel)(nil)
|
|
|
|
type (
|
|
// AppAccountModel is an interface to be customized, add more methods here,
|
|
// and implement the added methods in customAppAccountModel.
|
|
AppAccountModel interface {
|
|
appAccountModel
|
|
}
|
|
|
|
customAppAccountModel struct {
|
|
*defaultAppAccountModel
|
|
}
|
|
|
|
appAccountModel interface {
|
|
Insert(ctx context.Context, data *AppAccount) (sql.Result, error)
|
|
FindOne(ctx context.Context, id uint64) (*AppAccount, error)
|
|
Update(ctx context.Context, data *AppAccount) error
|
|
Delete(ctx context.Context, id uint64) error
|
|
FindIdByAppId(ctx context.Context, appId string) (uint64, error)
|
|
FindAll(ctx context.Context) (*[]*GameAppConfig, error)
|
|
}
|
|
)
|
|
|
|
// NewAppAccountModel returns a model for the database table.
|
|
func NewAppAccountModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) AppAccountModel {
|
|
return &customAppAccountModel{
|
|
defaultAppAccountModel: newAppAccountModel(conn, c, opts...),
|
|
}
|
|
}
|
|
|
|
type GameAppConfig struct {
|
|
AppID string `db:"app_id"`
|
|
Secret string `db:"secret"`
|
|
EcpmValue sql.Null[uint32] `db:"ecpm_value"` // 值
|
|
EcpmView sql.Null[uint32] `db:"ecpm_view"` // 浏览次数
|
|
Type uint64 `db:"type"` // 类型(0:抖音,1:微信)
|
|
}
|
|
|
|
func (m *defaultAppAccountModel) FindAll(ctx context.Context) (*[]*GameAppConfig, error) {
|
|
ecpmAppAccountIdKey := fmt.Sprintf("%s%v", cacheEcpmAppAccountIdPrefix, "all")
|
|
var resp []*GameAppConfig
|
|
err := m.QueryRowCtx(ctx, &resp, ecpmAppAccountIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
|
|
query := "select `app_id`,`secret`,`ecpm_value`,`ecpm_view`,`type` from `app_account` left join douyin_ecpm_config on app_account.id = douyin_ecpm_config.app_account_id"
|
|
return conn.QueryRowsCtx(ctx, v, query)
|
|
})
|
|
switch err {
|
|
case nil:
|
|
return &resp, nil
|
|
case sqlc.ErrNotFound:
|
|
return nil, ErrNotFound
|
|
default:
|
|
return nil, err
|
|
}
|
|
}
|