179 lines
6.6 KiB
Go
179 lines
6.6 KiB
Go
|
|
// Code generated by goctl. DO NOT EDIT.
|
||
|
|
// versions:
|
||
|
|
// goctl version: 1.9.2
|
||
|
|
|
||
|
|
package model
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"database/sql"
|
||
|
|
"fmt"
|
||
|
|
"strings"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"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 (
|
||
|
|
sysRoleFieldNames = builder.RawFieldNames(&SysRole{})
|
||
|
|
sysRoleRows = strings.Join(sysRoleFieldNames, ",")
|
||
|
|
sysRoleRowsExpectAutoSet = strings.Join(stringx.Remove(sysRoleFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
|
||
|
|
sysRoleRowsWithPlaceHolder = strings.Join(stringx.Remove(sysRoleFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
|
||
|
|
|
||
|
|
cacheSysRoleIdPrefix = "cache:sysRole:id:"
|
||
|
|
cacheSysRoleRoleIdPrefix = "cache:sysRole:roleId:"
|
||
|
|
cacheSysRoleRoleKeyPrefix = "cache:sysRole:roleKey:"
|
||
|
|
)
|
||
|
|
|
||
|
|
type (
|
||
|
|
sysRoleModel interface {
|
||
|
|
Insert(ctx context.Context, data *SysRole) (sql.Result, error)
|
||
|
|
FindOne(ctx context.Context, id int64) (*SysRole, error)
|
||
|
|
FindOneByRoleId(ctx context.Context, roleId string) (*SysRole, error)
|
||
|
|
FindOneByRoleKey(ctx context.Context, roleKey string) (*SysRole, error)
|
||
|
|
Update(ctx context.Context, data *SysRole) error
|
||
|
|
Delete(ctx context.Context, id int64) error
|
||
|
|
}
|
||
|
|
|
||
|
|
defaultSysRoleModel struct {
|
||
|
|
sqlc.CachedConn
|
||
|
|
table string
|
||
|
|
}
|
||
|
|
|
||
|
|
SysRole struct {
|
||
|
|
Id int64 `db:"id"`
|
||
|
|
RoleId string `db:"role_id"`
|
||
|
|
RoleName string `db:"role_name"`
|
||
|
|
RoleKey string `db:"role_key"`
|
||
|
|
RoleDesc string `db:"role_desc"`
|
||
|
|
SortOrder int64 `db:"sort_order"`
|
||
|
|
Status int64 `db:"status"` // 1:enabled 0:disabled
|
||
|
|
CreatedAt time.Time `db:"created_at"`
|
||
|
|
UpdatedAt time.Time `db:"updated_at"`
|
||
|
|
}
|
||
|
|
)
|
||
|
|
|
||
|
|
func newSysRoleModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultSysRoleModel {
|
||
|
|
return &defaultSysRoleModel{
|
||
|
|
CachedConn: sqlc.NewConn(conn, c, opts...),
|
||
|
|
table: "`sys_role`",
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *defaultSysRoleModel) Delete(ctx context.Context, id int64) error {
|
||
|
|
data, err := m.FindOne(ctx, id)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
sysRoleIdKey := fmt.Sprintf("%s%v", cacheSysRoleIdPrefix, id)
|
||
|
|
sysRoleRoleIdKey := fmt.Sprintf("%s%v", cacheSysRoleRoleIdPrefix, data.RoleId)
|
||
|
|
sysRoleRoleKeyKey := fmt.Sprintf("%s%v", cacheSysRoleRoleKeyPrefix, data.RoleKey)
|
||
|
|
_, 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)
|
||
|
|
}, sysRoleIdKey, sysRoleRoleIdKey, sysRoleRoleKeyKey)
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *defaultSysRoleModel) FindOne(ctx context.Context, id int64) (*SysRole, error) {
|
||
|
|
sysRoleIdKey := fmt.Sprintf("%s%v", cacheSysRoleIdPrefix, id)
|
||
|
|
var resp SysRole
|
||
|
|
err := m.QueryRowCtx(ctx, &resp, sysRoleIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
|
||
|
|
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", sysRoleRows, 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 *defaultSysRoleModel) FindOneByRoleId(ctx context.Context, roleId string) (*SysRole, error) {
|
||
|
|
sysRoleRoleIdKey := fmt.Sprintf("%s%v", cacheSysRoleRoleIdPrefix, roleId)
|
||
|
|
var resp SysRole
|
||
|
|
err := m.QueryRowIndexCtx(ctx, &resp, sysRoleRoleIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
|
||
|
|
query := fmt.Sprintf("select %s from %s where `role_id` = ? limit 1", sysRoleRows, m.table)
|
||
|
|
if err := conn.QueryRowCtx(ctx, &resp, query, 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 *defaultSysRoleModel) FindOneByRoleKey(ctx context.Context, roleKey string) (*SysRole, error) {
|
||
|
|
sysRoleRoleKeyKey := fmt.Sprintf("%s%v", cacheSysRoleRoleKeyPrefix, roleKey)
|
||
|
|
var resp SysRole
|
||
|
|
err := m.QueryRowIndexCtx(ctx, &resp, sysRoleRoleKeyKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
|
||
|
|
query := fmt.Sprintf("select %s from %s where `role_key` = ? limit 1", sysRoleRows, m.table)
|
||
|
|
if err := conn.QueryRowCtx(ctx, &resp, query, roleKey); 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 *defaultSysRoleModel) Insert(ctx context.Context, data *SysRole) (sql.Result, error) {
|
||
|
|
sysRoleIdKey := fmt.Sprintf("%s%v", cacheSysRoleIdPrefix, data.Id)
|
||
|
|
sysRoleRoleIdKey := fmt.Sprintf("%s%v", cacheSysRoleRoleIdPrefix, data.RoleId)
|
||
|
|
sysRoleRoleKeyKey := fmt.Sprintf("%s%v", cacheSysRoleRoleKeyPrefix, data.RoleKey)
|
||
|
|
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, sysRoleRowsExpectAutoSet)
|
||
|
|
return conn.ExecCtx(ctx, query, data.RoleId, data.RoleName, data.RoleKey, data.RoleDesc, data.SortOrder, data.Status)
|
||
|
|
}, sysRoleIdKey, sysRoleRoleIdKey, sysRoleRoleKeyKey)
|
||
|
|
return ret, err
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *defaultSysRoleModel) Update(ctx context.Context, newData *SysRole) error {
|
||
|
|
data, err := m.FindOne(ctx, newData.Id)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
sysRoleIdKey := fmt.Sprintf("%s%v", cacheSysRoleIdPrefix, data.Id)
|
||
|
|
sysRoleRoleIdKey := fmt.Sprintf("%s%v", cacheSysRoleRoleIdPrefix, data.RoleId)
|
||
|
|
sysRoleRoleKeyKey := fmt.Sprintf("%s%v", cacheSysRoleRoleKeyPrefix, data.RoleKey)
|
||
|
|
_, 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, sysRoleRowsWithPlaceHolder)
|
||
|
|
return conn.ExecCtx(ctx, query, newData.RoleId, newData.RoleName, newData.RoleKey, newData.RoleDesc, newData.SortOrder, newData.Status, newData.Id)
|
||
|
|
}, sysRoleIdKey, sysRoleRoleIdKey, sysRoleRoleKeyKey)
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *defaultSysRoleModel) formatPrimary(primary any) string {
|
||
|
|
return fmt.Sprintf("%s%v", cacheSysRoleIdPrefix, primary)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *defaultSysRoleModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error {
|
||
|
|
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", sysRoleRows, m.table)
|
||
|
|
return conn.QueryRowCtx(ctx, v, query, primary)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *defaultSysRoleModel) tableName() string {
|
||
|
|
return m.table
|
||
|
|
}
|