153 lines
5.5 KiB
Go
Executable File
153 lines
5.5 KiB
Go
Executable File
// Code generated by goctl. DO NOT EDIT.
|
|
|
|
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 (
|
|
accountToUidFieldNames = builder.RawFieldNames(&AccountToUid{})
|
|
accountToUidRows = strings.Join(accountToUidFieldNames, ",")
|
|
accountToUidRowsExpectAutoSet = strings.Join(stringx.Remove(accountToUidFieldNames, "`id`"), ",")
|
|
accountToUidRowsWithPlaceHolder = strings.Join(stringx.Remove(accountToUidFieldNames, "`id`"), "=?,") + "=?"
|
|
|
|
cacheAccountToUidIdPrefix = "cache:accountToUid:id:"
|
|
cacheAccountToUidAccountPrefix = "cache:accountToUid:account:"
|
|
)
|
|
|
|
type (
|
|
accountToUidModel interface {
|
|
Insert(ctx context.Context, data *AccountToUid) (sql.Result, error)
|
|
FindOne(ctx context.Context, id int64) (*AccountToUid, error)
|
|
FindOneByAccount(ctx context.Context, account string) (*AccountToUid, error)
|
|
Update(ctx context.Context, data *AccountToUid) error
|
|
Delete(ctx context.Context, id int64) error
|
|
}
|
|
|
|
defaultAccountToUidModel struct {
|
|
sqlc.CachedConn
|
|
table string
|
|
}
|
|
|
|
AccountToUid struct {
|
|
Id int64 `db:"id"`
|
|
Account string `db:"account"`
|
|
Uid string `db:"uid"`
|
|
Type int64 `db:"type"` // 1 手機 2 信箱 3 自定義帳號
|
|
}
|
|
)
|
|
|
|
func newAccountToUidModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultAccountToUidModel {
|
|
return &defaultAccountToUidModel{
|
|
CachedConn: sqlc.NewConn(conn, c, opts...),
|
|
table: "`account_to_uid`",
|
|
}
|
|
}
|
|
|
|
func (m *defaultAccountToUidModel) withSession(session sqlx.Session) *defaultAccountToUidModel {
|
|
return &defaultAccountToUidModel{
|
|
CachedConn: m.CachedConn.WithSession(session),
|
|
table: "`account_to_uid`",
|
|
}
|
|
}
|
|
|
|
func (m *defaultAccountToUidModel) Delete(ctx context.Context, id int64) error {
|
|
data, err := m.FindOne(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
accountToUidAccountKey := fmt.Sprintf("%s%v", cacheAccountToUidAccountPrefix, data.Account)
|
|
accountToUidIdKey := fmt.Sprintf("%s%v", cacheAccountToUidIdPrefix, 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)
|
|
}, accountToUidAccountKey, accountToUidIdKey)
|
|
return err
|
|
}
|
|
|
|
func (m *defaultAccountToUidModel) FindOne(ctx context.Context, id int64) (*AccountToUid, error) {
|
|
accountToUidIdKey := fmt.Sprintf("%s%v", cacheAccountToUidIdPrefix, id)
|
|
var resp AccountToUid
|
|
err := m.QueryRowCtx(ctx, &resp, accountToUidIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
|
|
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", accountToUidRows, 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 *defaultAccountToUidModel) FindOneByAccount(ctx context.Context, account string) (*AccountToUid, error) {
|
|
accountToUidAccountKey := fmt.Sprintf("%s%v", cacheAccountToUidAccountPrefix, account)
|
|
var resp AccountToUid
|
|
err := m.QueryRowIndexCtx(ctx, &resp, accountToUidAccountKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
|
|
query := fmt.Sprintf("select %s from %s where `account` = ? limit 1", accountToUidRows, m.table)
|
|
if err := conn.QueryRowCtx(ctx, &resp, query, account); err != nil {
|
|
return nil, err
|
|
}
|
|
return resp.Id, nil
|
|
}, m.queryPrimary)
|
|
switch err {
|
|
case nil:
|
|
return &resp, nil
|
|
case sqlc.ErrNotFound:
|
|
return nil, ErrNotFound
|
|
default:
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
func (m *defaultAccountToUidModel) Insert(ctx context.Context, data *AccountToUid) (sql.Result, error) {
|
|
accountToUidAccountKey := fmt.Sprintf("%s%v", cacheAccountToUidAccountPrefix, data.Account)
|
|
accountToUidIdKey := fmt.Sprintf("%s%v", cacheAccountToUidIdPrefix, 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, accountToUidRowsExpectAutoSet)
|
|
return conn.ExecCtx(ctx, query, data.Account, data.Uid, data.Type)
|
|
}, accountToUidAccountKey, accountToUidIdKey)
|
|
return ret, err
|
|
}
|
|
|
|
func (m *defaultAccountToUidModel) Update(ctx context.Context, newData *AccountToUid) error {
|
|
data, err := m.FindOne(ctx, newData.Id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
accountToUidAccountKey := fmt.Sprintf("%s%v", cacheAccountToUidAccountPrefix, data.Account)
|
|
accountToUidIdKey := fmt.Sprintf("%s%v", cacheAccountToUidIdPrefix, 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, accountToUidRowsWithPlaceHolder)
|
|
return conn.ExecCtx(ctx, query, newData.Account, newData.Uid, newData.Type, newData.Id)
|
|
}, accountToUidAccountKey, accountToUidIdKey)
|
|
return err
|
|
}
|
|
|
|
func (m *defaultAccountToUidModel) formatPrimary(primary any) string {
|
|
return fmt.Sprintf("%s%v", cacheAccountToUidIdPrefix, primary)
|
|
}
|
|
|
|
func (m *defaultAccountToUidModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error {
|
|
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", accountToUidRows, m.table)
|
|
return conn.QueryRowCtx(ctx, v, query, primary)
|
|
}
|
|
|
|
func (m *defaultAccountToUidModel) tableName() string {
|
|
return m.table
|
|
}
|