147 lines
5.5 KiB
Go
147 lines
5.5 KiB
Go
|
|
// 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 (
|
||
|
|
sysRoleMenuFieldNames = builder.RawFieldNames(&SysRoleMenu{})
|
||
|
|
sysRoleMenuRows = strings.Join(sysRoleMenuFieldNames, ",")
|
||
|
|
sysRoleMenuRowsExpectAutoSet = strings.Join(stringx.Remove(sysRoleMenuFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
|
||
|
|
sysRoleMenuRowsWithPlaceHolder = strings.Join(stringx.Remove(sysRoleMenuFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
|
||
|
|
|
||
|
|
cacheSysRoleMenuIdPrefix = "cache:sysRoleMenu:id:"
|
||
|
|
cacheSysRoleMenuRoleIdMenuIdPrefix = "cache:sysRoleMenu:roleId:menuId:"
|
||
|
|
)
|
||
|
|
|
||
|
|
type (
|
||
|
|
sysRoleMenuModel interface {
|
||
|
|
Insert(ctx context.Context, data *SysRoleMenu) (sql.Result, error)
|
||
|
|
FindOne(ctx context.Context, id int64) (*SysRoleMenu, error)
|
||
|
|
FindOneByRoleIdMenuId(ctx context.Context, roleId string, menuId string) (*SysRoleMenu, error)
|
||
|
|
Update(ctx context.Context, data *SysRoleMenu) error
|
||
|
|
Delete(ctx context.Context, id int64) error
|
||
|
|
}
|
||
|
|
|
||
|
|
defaultSysRoleMenuModel struct {
|
||
|
|
sqlc.CachedConn
|
||
|
|
table string
|
||
|
|
}
|
||
|
|
|
||
|
|
SysRoleMenu struct {
|
||
|
|
Id int64 `db:"id"`
|
||
|
|
RoleId string `db:"role_id"`
|
||
|
|
MenuId string `db:"menu_id"`
|
||
|
|
}
|
||
|
|
)
|
||
|
|
|
||
|
|
func newSysRoleMenuModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultSysRoleMenuModel {
|
||
|
|
return &defaultSysRoleMenuModel{
|
||
|
|
CachedConn: sqlc.NewConn(conn, c, opts...),
|
||
|
|
table: "`sys_role_menu`",
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *defaultSysRoleMenuModel) Delete(ctx context.Context, id int64) error {
|
||
|
|
data, err := m.FindOne(ctx, id)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
sysRoleMenuIdKey := fmt.Sprintf("%s%v", cacheSysRoleMenuIdPrefix, id)
|
||
|
|
sysRoleMenuRoleIdMenuIdKey := fmt.Sprintf("%s%v:%v", cacheSysRoleMenuRoleIdMenuIdPrefix, data.RoleId, data.MenuId)
|
||
|
|
_, 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)
|
||
|
|
}, sysRoleMenuIdKey, sysRoleMenuRoleIdMenuIdKey)
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *defaultSysRoleMenuModel) FindOne(ctx context.Context, id int64) (*SysRoleMenu, error) {
|
||
|
|
sysRoleMenuIdKey := fmt.Sprintf("%s%v", cacheSysRoleMenuIdPrefix, id)
|
||
|
|
var resp SysRoleMenu
|
||
|
|
err := m.QueryRowCtx(ctx, &resp, sysRoleMenuIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
|
||
|
|
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", sysRoleMenuRows, 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 *defaultSysRoleMenuModel) FindOneByRoleIdMenuId(ctx context.Context, roleId string, menuId string) (*SysRoleMenu, error) {
|
||
|
|
sysRoleMenuRoleIdMenuIdKey := fmt.Sprintf("%s%v:%v", cacheSysRoleMenuRoleIdMenuIdPrefix, roleId, menuId)
|
||
|
|
var resp SysRoleMenu
|
||
|
|
err := m.QueryRowIndexCtx(ctx, &resp, sysRoleMenuRoleIdMenuIdKey, 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` = ? and `menu_id` = ? limit 1", sysRoleMenuRows, m.table)
|
||
|
|
if err := conn.QueryRowCtx(ctx, &resp, query, roleId, menuId); 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 *defaultSysRoleMenuModel) Insert(ctx context.Context, data *SysRoleMenu) (sql.Result, error) {
|
||
|
|
sysRoleMenuIdKey := fmt.Sprintf("%s%v", cacheSysRoleMenuIdPrefix, data.Id)
|
||
|
|
sysRoleMenuRoleIdMenuIdKey := fmt.Sprintf("%s%v:%v", cacheSysRoleMenuRoleIdMenuIdPrefix, data.RoleId, data.MenuId)
|
||
|
|
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, sysRoleMenuRowsExpectAutoSet)
|
||
|
|
return conn.ExecCtx(ctx, query, data.RoleId, data.MenuId)
|
||
|
|
}, sysRoleMenuIdKey, sysRoleMenuRoleIdMenuIdKey)
|
||
|
|
return ret, err
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *defaultSysRoleMenuModel) Update(ctx context.Context, newData *SysRoleMenu) error {
|
||
|
|
data, err := m.FindOne(ctx, newData.Id)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
sysRoleMenuIdKey := fmt.Sprintf("%s%v", cacheSysRoleMenuIdPrefix, data.Id)
|
||
|
|
sysRoleMenuRoleIdMenuIdKey := fmt.Sprintf("%s%v:%v", cacheSysRoleMenuRoleIdMenuIdPrefix, data.RoleId, data.MenuId)
|
||
|
|
_, 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, sysRoleMenuRowsWithPlaceHolder)
|
||
|
|
return conn.ExecCtx(ctx, query, newData.RoleId, newData.MenuId, newData.Id)
|
||
|
|
}, sysRoleMenuIdKey, sysRoleMenuRoleIdMenuIdKey)
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *defaultSysRoleMenuModel) formatPrimary(primary any) string {
|
||
|
|
return fmt.Sprintf("%s%v", cacheSysRoleMenuIdPrefix, primary)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *defaultSysRoleMenuModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error {
|
||
|
|
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", sysRoleMenuRows, m.table)
|
||
|
|
return conn.QueryRowCtx(ctx, v, query, primary)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *defaultSysRoleMenuModel) tableName() string {
|
||
|
|
return m.table
|
||
|
|
}
|