app-cloudep-permission-server/internal/model/user_role_model_gen.go

156 lines
5.4 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 (
userRoleFieldNames = builder.RawFieldNames(&UserRole{})
userRoleRows = strings.Join(userRoleFieldNames, ",")
userRoleRowsExpectAutoSet = strings.Join(stringx.Remove(userRoleFieldNames, "`id`"), ",")
userRoleRowsWithPlaceHolder = strings.Join(stringx.Remove(userRoleFieldNames, "`id`"), "=?,") + "=?"
cacheUserRoleIdPrefix = "cache:userRole:id:"
cacheUserRoleUidPrefix = "cache:userRole:uid:"
)
type (
userRoleModel interface {
Insert(ctx context.Context, data *UserRole) (sql.Result, error)
FindOne(ctx context.Context, id int64) (*UserRole, error)
FindOneByUid(ctx context.Context, uid string) (*UserRole, error)
Update(ctx context.Context, data *UserRole) error
Delete(ctx context.Context, id int64) error
}
defaultUserRoleModel struct {
sqlc.CachedConn
table string
}
UserRole struct {
Id int64 `db:"id"` // PK
Brand string `db:"brand"`
Uid string `db:"uid"`
RoleId string `db:"role_id"`
Status int64 `db:"status"` // 狀態 1: 啟用, 2: 禁用
CreateTime int64 `db:"create_time"` // 創建時間
UpdateTime int64 `db:"update_time"` // 更新時間
}
)
func newUserRoleModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultUserRoleModel {
return &defaultUserRoleModel{
CachedConn: sqlc.NewConn(conn, c, opts...),
table: "`user_role`",
}
}
func (m *defaultUserRoleModel) withSession(session sqlx.Session) *defaultUserRoleModel {
return &defaultUserRoleModel{
CachedConn: m.CachedConn.WithSession(session),
table: "`user_role`",
}
}
func (m *defaultUserRoleModel) Delete(ctx context.Context, id int64) error {
data, err := m.FindOne(ctx, id)
if err != nil {
return err
}
userRoleIdKey := fmt.Sprintf("%s%v", cacheUserRoleIdPrefix, id)
userRoleUidKey := fmt.Sprintf("%s%v", cacheUserRoleUidPrefix, data.Uid)
_, 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)
}, userRoleIdKey, userRoleUidKey)
return err
}
func (m *defaultUserRoleModel) FindOne(ctx context.Context, id int64) (*UserRole, error) {
userRoleIdKey := fmt.Sprintf("%s%v", cacheUserRoleIdPrefix, id)
var resp UserRole
err := m.QueryRowCtx(ctx, &resp, userRoleIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", userRoleRows, 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 *defaultUserRoleModel) FindOneByUid(ctx context.Context, uid string) (*UserRole, error) {
userRoleUidKey := fmt.Sprintf("%s%v", cacheUserRoleUidPrefix, uid)
var resp UserRole
err := m.QueryRowIndexCtx(ctx, &resp, userRoleUidKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
query := fmt.Sprintf("select %s from %s where `uid` = ? limit 1", userRoleRows, m.table)
if err := conn.QueryRowCtx(ctx, &resp, query, uid); 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 *defaultUserRoleModel) Insert(ctx context.Context, data *UserRole) (sql.Result, error) {
userRoleIdKey := fmt.Sprintf("%s%v", cacheUserRoleIdPrefix, data.Id)
userRoleUidKey := fmt.Sprintf("%s%v", cacheUserRoleUidPrefix, data.Uid)
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, userRoleRowsExpectAutoSet)
return conn.ExecCtx(ctx, query, data.Brand, data.Uid, data.RoleId, data.Status, data.CreateTime, data.UpdateTime)
}, userRoleIdKey, userRoleUidKey)
return ret, err
}
func (m *defaultUserRoleModel) Update(ctx context.Context, newData *UserRole) error {
data, err := m.FindOne(ctx, newData.Id)
if err != nil {
return err
}
userRoleIdKey := fmt.Sprintf("%s%v", cacheUserRoleIdPrefix, data.Id)
userRoleUidKey := fmt.Sprintf("%s%v", cacheUserRoleUidPrefix, data.Uid)
_, 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, userRoleRowsWithPlaceHolder)
return conn.ExecCtx(ctx, query, newData.Brand, newData.Uid, newData.RoleId, newData.Status, newData.CreateTime, newData.UpdateTime, newData.Id)
}, userRoleIdKey, userRoleUidKey)
return err
}
func (m *defaultUserRoleModel) formatPrimary(primary any) string {
return fmt.Sprintf("%s%v", cacheUserRoleIdPrefix, primary)
}
func (m *defaultUserRoleModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error {
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", userRoleRows, m.table)
return conn.QueryRowCtx(ctx, v, query, primary)
}
func (m *defaultUserRoleModel) tableName() string {
return m.table
}