125 lines
4.6 KiB
Go
125 lines
4.6 KiB
Go
|
// Code generated by goctl. DO NOT EDIT.
|
||
|
// versions:
|
||
|
// goctl version: 1.7.5
|
||
|
|
||
|
package model
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"database/sql"
|
||
|
"fmt"
|
||
|
"strings"
|
||
|
|
||
|
"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 (
|
||
|
appAccountFieldNames = builder.RawFieldNames(&AppAccount{})
|
||
|
appAccountRows = strings.Join(appAccountFieldNames, ",")
|
||
|
appAccountRowsExpectAutoSet = strings.Join(stringx.Remove(appAccountFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
|
||
|
appAccountRowsWithPlaceHolder = strings.Join(stringx.Remove(appAccountFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
|
||
|
|
||
|
cacheEcpmAppAccountIdPrefix = "cache:ecpm:appAccount:id:"
|
||
|
)
|
||
|
|
||
|
type (
|
||
|
defaultAppAccountModel struct {
|
||
|
sqlc.CachedConn
|
||
|
table string
|
||
|
}
|
||
|
|
||
|
AppAccount struct {
|
||
|
Id uint64 `db:"id"`
|
||
|
Type uint64 `db:"type"` // 类型(0:抖音,1:微信)
|
||
|
AppId string `db:"app_id"`
|
||
|
Secret string `db:"secret"`
|
||
|
Remark sql.NullString `db:"remark"` // 备注
|
||
|
DeletedAt sql.NullString `db:"deleted_at"`
|
||
|
}
|
||
|
)
|
||
|
|
||
|
func newAppAccountModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultAppAccountModel {
|
||
|
return &defaultAppAccountModel{
|
||
|
CachedConn: sqlc.NewConn(conn, c, opts...),
|
||
|
table: "`app_account`",
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (m *defaultAppAccountModel) Delete(ctx context.Context, id uint64) error {
|
||
|
ecpmAppAccountIdKey := fmt.Sprintf("%s%v", cacheEcpmAppAccountIdPrefix, id)
|
||
|
_, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||
|
query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
|
||
|
return conn.ExecCtx(ctx, query, id)
|
||
|
}, ecpmAppAccountIdKey)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func (m *defaultAppAccountModel) FindOne(ctx context.Context, id uint64) (*AppAccount, error) {
|
||
|
ecpmAppAccountIdKey := fmt.Sprintf("%s%v", cacheEcpmAppAccountIdPrefix, id)
|
||
|
var resp AppAccount
|
||
|
err := m.QueryRowCtx(ctx, &resp, ecpmAppAccountIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
|
||
|
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", appAccountRows, m.table)
|
||
|
return conn.QueryRowCtx(ctx, v, query, id)
|
||
|
})
|
||
|
switch err {
|
||
|
case nil:
|
||
|
return &resp, nil
|
||
|
case sqlc.ErrNotFound:
|
||
|
return nil, ErrNotFound
|
||
|
default:
|
||
|
return nil, err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (m *defaultAppAccountModel) Insert(ctx context.Context, data *AppAccount) (sql.Result, error) {
|
||
|
ecpmAppAccountIdKey := fmt.Sprintf("%s%v", cacheEcpmAppAccountIdPrefix, data.Id)
|
||
|
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, appAccountRowsExpectAutoSet)
|
||
|
return conn.ExecCtx(ctx, query, data.Type, data.AppId, data.Secret, data.Remark, data.DeletedAt)
|
||
|
}, ecpmAppAccountIdKey)
|
||
|
return ret, err
|
||
|
}
|
||
|
|
||
|
func (m *defaultAppAccountModel) Update(ctx context.Context, data *AppAccount) error {
|
||
|
ecpmAppAccountIdKey := fmt.Sprintf("%s%v", cacheEcpmAppAccountIdPrefix, data.Id)
|
||
|
_, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||
|
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, appAccountRowsWithPlaceHolder)
|
||
|
return conn.ExecCtx(ctx, query, data.Type, data.AppId, data.Secret, data.Remark, data.DeletedAt, data.Id)
|
||
|
}, ecpmAppAccountIdKey)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func (m *defaultAppAccountModel) FindIdByAppId(ctx context.Context, appId string) (uint64, error) {
|
||
|
ecpmAppAccountIdKey := fmt.Sprintf("%s%v", cacheEcpmAppAccountIdPrefix, appId)
|
||
|
var id uint64
|
||
|
err := m.QueryRowCtx(ctx, &id, ecpmAppAccountIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
|
||
|
query := fmt.Sprintf("select id from %s where `app_id` = ? limit 1", m.table)
|
||
|
return conn.QueryRowCtx(ctx, v, query, appId)
|
||
|
})
|
||
|
switch err {
|
||
|
case nil:
|
||
|
return id, nil
|
||
|
case sqlc.ErrNotFound:
|
||
|
return 0, ErrNotFound
|
||
|
default:
|
||
|
return 0, err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (m *defaultAppAccountModel) formatPrimary(primary any) string {
|
||
|
return fmt.Sprintf("%s%v", cacheEcpmAppAccountIdPrefix, primary)
|
||
|
}
|
||
|
|
||
|
func (m *defaultAppAccountModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error {
|
||
|
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", appAccountRows, m.table)
|
||
|
return conn.QueryRowCtx(ctx, v, query, primary)
|
||
|
}
|
||
|
|
||
|
func (m *defaultAppAccountModel) tableName() string {
|
||
|
return m.table
|
||
|
}
|