147 lines
5.5 KiB
Go
Executable File
147 lines
5.5 KiB
Go
Executable File
// Code generated by goctl. DO NOT EDIT.
|
|
// versions:
|
|
// goctl version: 1.9.2
|
|
|
|
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 (
|
|
sysUserRoleFieldNames = builder.RawFieldNames(&SysUserRole{})
|
|
sysUserRoleRows = strings.Join(sysUserRoleFieldNames, ",")
|
|
sysUserRoleRowsExpectAutoSet = strings.Join(stringx.Remove(sysUserRoleFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
|
|
sysUserRoleRowsWithPlaceHolder = strings.Join(stringx.Remove(sysUserRoleFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
|
|
|
|
cacheSysUserRoleIdPrefix = "cache:sysUserRole:id:"
|
|
cacheSysUserRoleUserIdRoleIdPrefix = "cache:sysUserRole:userId:roleId:"
|
|
)
|
|
|
|
type (
|
|
sysUserRoleModel interface {
|
|
Insert(ctx context.Context, data *SysUserRole) (sql.Result, error)
|
|
FindOne(ctx context.Context, id int64) (*SysUserRole, error)
|
|
FindOneByUserIdRoleId(ctx context.Context, userId string, roleId string) (*SysUserRole, error)
|
|
Update(ctx context.Context, data *SysUserRole) error
|
|
Delete(ctx context.Context, id int64) error
|
|
}
|
|
|
|
defaultSysUserRoleModel struct {
|
|
sqlc.CachedConn
|
|
table string
|
|
}
|
|
|
|
SysUserRole struct {
|
|
Id int64 `db:"id"`
|
|
UserId string `db:"user_id"`
|
|
RoleId string `db:"role_id"`
|
|
}
|
|
)
|
|
|
|
func newSysUserRoleModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultSysUserRoleModel {
|
|
return &defaultSysUserRoleModel{
|
|
CachedConn: sqlc.NewConn(conn, c, opts...),
|
|
table: "`sys_user_role`",
|
|
}
|
|
}
|
|
|
|
func (m *defaultSysUserRoleModel) Delete(ctx context.Context, id int64) error {
|
|
data, err := m.FindOne(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
sysUserRoleIdKey := fmt.Sprintf("%s%v", cacheSysUserRoleIdPrefix, id)
|
|
sysUserRoleUserIdRoleIdKey := fmt.Sprintf("%s%v:%v", cacheSysUserRoleUserIdRoleIdPrefix, data.UserId, data.RoleId)
|
|
_, 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)
|
|
}, sysUserRoleIdKey, sysUserRoleUserIdRoleIdKey)
|
|
return err
|
|
}
|
|
|
|
func (m *defaultSysUserRoleModel) FindOne(ctx context.Context, id int64) (*SysUserRole, error) {
|
|
sysUserRoleIdKey := fmt.Sprintf("%s%v", cacheSysUserRoleIdPrefix, id)
|
|
var resp SysUserRole
|
|
err := m.QueryRowCtx(ctx, &resp, sysUserRoleIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
|
|
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", sysUserRoleRows, 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 *defaultSysUserRoleModel) FindOneByUserIdRoleId(ctx context.Context, userId string, roleId string) (*SysUserRole, error) {
|
|
sysUserRoleUserIdRoleIdKey := fmt.Sprintf("%s%v:%v", cacheSysUserRoleUserIdRoleIdPrefix, userId, roleId)
|
|
var resp SysUserRole
|
|
err := m.QueryRowIndexCtx(ctx, &resp, sysUserRoleUserIdRoleIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
|
|
query := fmt.Sprintf("select %s from %s where `user_id` = ? and `role_id` = ? limit 1", sysUserRoleRows, m.table)
|
|
if err := conn.QueryRowCtx(ctx, &resp, query, userId, roleId); 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 *defaultSysUserRoleModel) Insert(ctx context.Context, data *SysUserRole) (sql.Result, error) {
|
|
sysUserRoleIdKey := fmt.Sprintf("%s%v", cacheSysUserRoleIdPrefix, data.Id)
|
|
sysUserRoleUserIdRoleIdKey := fmt.Sprintf("%s%v:%v", cacheSysUserRoleUserIdRoleIdPrefix, data.UserId, data.RoleId)
|
|
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, sysUserRoleRowsExpectAutoSet)
|
|
return conn.ExecCtx(ctx, query, data.UserId, data.RoleId)
|
|
}, sysUserRoleIdKey, sysUserRoleUserIdRoleIdKey)
|
|
return ret, err
|
|
}
|
|
|
|
func (m *defaultSysUserRoleModel) Update(ctx context.Context, newData *SysUserRole) error {
|
|
data, err := m.FindOne(ctx, newData.Id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
sysUserRoleIdKey := fmt.Sprintf("%s%v", cacheSysUserRoleIdPrefix, data.Id)
|
|
sysUserRoleUserIdRoleIdKey := fmt.Sprintf("%s%v:%v", cacheSysUserRoleUserIdRoleIdPrefix, data.UserId, data.RoleId)
|
|
_, 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, sysUserRoleRowsWithPlaceHolder)
|
|
return conn.ExecCtx(ctx, query, newData.UserId, newData.RoleId, newData.Id)
|
|
}, sysUserRoleIdKey, sysUserRoleUserIdRoleIdKey)
|
|
return err
|
|
}
|
|
|
|
func (m *defaultSysUserRoleModel) formatPrimary(primary any) string {
|
|
return fmt.Sprintf("%s%v", cacheSysUserRoleIdPrefix, primary)
|
|
}
|
|
|
|
func (m *defaultSysUserRoleModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error {
|
|
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", sysUserRoleRows, m.table)
|
|
return conn.QueryRowCtx(ctx, v, query, primary)
|
|
}
|
|
|
|
func (m *defaultSysUserRoleModel) tableName() string {
|
|
return m.table
|
|
}
|