2022-03-01 13:50:13 +08:00
package dao
import (
2023-01-18 15:40:27 +08:00
"cutego/modules/core/api/v1/request"
2023-01-18 17:21:12 +08:00
"cutego/modules/core/dataobject"
2022-03-01 13:50:13 +08:00
"cutego/pkg/common"
2023-01-18 17:09:49 +08:00
"cutego/refs"
2022-03-01 13:50:13 +08:00
"github.com/druidcaesa/gotool"
)
type MenuDao struct {
}
// GetMenuPermission 根据用户ID查询权限
func ( d MenuDao ) GetMenuPermission ( id int64 ) * [ ] string {
var perms [ ] string
2023-01-18 17:09:49 +08:00
session := refs . SqlDB . Table ( [ ] string { "sys_menu" , "m" } )
2022-03-01 13:50:13 +08:00
err := session . Distinct ( "m.perms" ) .
Join ( "LEFT" , [ ] string { "sys_role_menu" , "rm" } , "m.menu_id = rm.menu_id" ) .
Join ( "LEFT" , [ ] string { "sys_user_role" , "ur" } , "rm.role_id = ur.role_id" ) .
Join ( "LEFT" , [ ] string { "sys_role" , "r" } , "r.role_id = ur.role_id" ) .
Where ( "m.status = '0'" ) . And ( "r.status = '0'" ) . And ( "ur.user_id = ?" , id ) . Find ( & perms )
if err != nil {
common . ErrorLog ( err )
return nil
}
return & perms
}
// GetMenuAll 查询所有菜单数据
2023-01-18 17:21:12 +08:00
func ( d MenuDao ) GetMenuAll ( ) * [ ] dataobject . SysMenu {
menus := make ( [ ] dataobject . SysMenu , 0 )
session := refs . SqlDB . Table ( [ ] string { dataobject . SysMenu { } . TableName ( ) , "m" } )
2022-03-01 13:50:13 +08:00
err := session . Distinct ( "m.menu_id" ) . Cols ( "m.parent_id" , "m.menu_name" , "m.path" , "m.component" , "m.visible" , "m.status" , "m.perms" , "m.is_frame" , "m.is_cache" , "m.menu_type" , "m.icon" , "m.order_num" , "m.create_time" ) .
Where ( "m.menu_type in ('M', 'C')" ) . And ( "m.status = 0" ) . OrderBy ( "m.parent_id" ) . OrderBy ( "m.order_num" ) . Find ( & menus )
if err != nil {
common . ErrorLog ( err )
return nil
}
return & menus
}
// GetMenuByUserId 根据用户ID查询菜单
2023-01-18 17:21:12 +08:00
func ( d MenuDao ) GetMenuByUserId ( id int64 ) * [ ] dataobject . SysMenu {
menus := make ( [ ] dataobject . SysMenu , 0 )
session := refs . SqlDB . Table ( [ ] string { dataobject . SysMenu { } . TableName ( ) , "m" } )
2022-03-01 13:50:13 +08:00
err := session . Distinct ( "m.menu_id" ) . Cols ( "m.parent_id" , "m.menu_name" , "m.path" , "m.component" , "m.visible" , "m.status" , "m.perms" , "m.is_frame" , "m.is_cache" , "m.menu_type" , "m.icon" , "m.order_num" , "m.create_time" ) .
Join ( "LEFT" , [ ] string { "sys_role_menu" , "rm" } , "m.menu_id = rm.menu_id" ) .
Join ( "LEFT" , [ ] string { "sys_user_role" , "ur" } , "rm.role_id = ur.role_id" ) .
Join ( "LEFT" , [ ] string { "sys_role" , "ro" } , "ur.role_id = ro.role_id" ) .
Join ( "LEFT" , [ ] string { "sys_user" , "u" } , "ur.user_id = u.user_id" ) . Where ( "u.user_id = ?" , id ) .
And ( "m.menu_type in ('M', 'C')" ) . And ( "m.status = 0" ) . OrderBy ( "m.parent_id" ) . OrderBy ( "m.order_num" ) . Find ( & menus )
if err != nil {
common . ErrorLog ( err )
return nil
}
return & menus
}
// SelectMenuByRoleId 根据角色ID查询菜单树信息
func ( d MenuDao ) SelectMenuByRoleId ( id int64 , strictly bool ) * [ ] int64 {
list := make ( [ ] int64 , 0 )
2023-01-18 17:09:49 +08:00
session := refs . SqlDB . NewSession ( ) . Table ( [ ] string { "sys_menu" , "m" } )
2022-03-01 13:50:13 +08:00
session . Join ( "LEFT" , [ ] string { "sys_role_menu" , "rm" } , "m.menu_id = rm.menu_id" )
session . Where ( "rm.role_id = ?" , id )
if strictly {
session . And ( "m.menu_id not in (select m.parent_id from sys_menu m inner join sys_role_menu rm on m.menu_id = rm.menu_id and rm.role_id = ?)" , id )
}
err := session . OrderBy ( "m.parent_id" ) . OrderBy ( "m.order_num" ) . Cols ( "m.menu_id" ) . Find ( & list )
if err != nil {
common . ErrorLog ( err )
return nil
}
return & list
}
// SelectMenuList 查询系统菜单列表
2023-01-18 17:21:12 +08:00
func ( d MenuDao ) SelectMenuList ( query request . MenuQuery ) * [ ] dataobject . SysMenu {
list := make ( [ ] dataobject . SysMenu , 0 )
2023-01-18 17:09:49 +08:00
session := refs . SqlDB . NewSession ( ) . OrderBy ( "parent_id" ) . OrderBy ( "order_num" )
2022-03-01 13:50:13 +08:00
if gotool . StrUtils . HasNotEmpty ( query . MenuName ) {
session . And ( "menu_name like concat('%', ?, '%')" , query . MenuName )
}
if gotool . StrUtils . HasNotEmpty ( query . Visible ) {
session . And ( "visible = ?" , query . Visible )
}
if gotool . StrUtils . HasNotEmpty ( query . Status ) {
session . And ( "status = ?" , query . Status )
}
err := session . Find ( & list )
if err != nil {
common . ErrorLog ( err )
return nil
}
return & list
}
// SelectMenuListByUserId 根据用户查询系统菜单列表
2023-01-18 17:21:12 +08:00
func ( d MenuDao ) SelectMenuListByUserId ( query request . MenuQuery ) * [ ] dataobject . SysMenu {
2023-01-18 17:09:49 +08:00
session := refs . SqlDB . NewSession ( ) . OrderBy ( "parent_id" ) . OrderBy ( "order_num" )
2023-01-18 17:21:12 +08:00
list := make ( [ ] dataobject . SysMenu , 0 )
2022-03-01 13:50:13 +08:00
session . Distinct ( "m.menu_id" , "m.parent_id" , "m.menu_name" , "m.path" , "m.component" , "m.visible" , "m.status" , "ifnull(m.perms,'') as perms" , "m.is_frame" , "m.is_cache" , "m.menu_type" , "m.icon" , "m.order_num" , "m.create_time" )
session . Join ( "LEFT" , [ ] string { "sys_role_menu" , "rm" } , "m.menu_id = rm.menu_id" ) .
Join ( "LEFT" , [ ] string { "sys_user_role" , "ur" } , "rm.role_id = ur.role_id" ) .
Join ( "LEFT" , [ ] string { "sys_role" , "ro" } , "ur.role_id = ro.role_id" ) .
Where ( "ur.user_id = ?" , query . UserId )
if gotool . StrUtils . HasNotEmpty ( query . MenuName ) {
session . And ( "menu_name like concat('%', ?, '%')" , query . MenuName )
}
if gotool . StrUtils . HasNotEmpty ( query . Visible ) {
session . And ( "visible = ?" , query . Visible )
}
if gotool . StrUtils . HasNotEmpty ( query . Status ) {
session . And ( "status = ?" , query . Status )
}
err := session . Find ( & list )
if err != nil {
common . ErrorLog ( err )
return nil
}
return & list
}
// SelectMenuByMenuId 根据菜单ID查询信息
2023-01-18 17:21:12 +08:00
func ( d MenuDao ) SelectMenuByMenuId ( id int ) * dataobject . SysMenu {
menu := dataobject . SysMenu {
2022-03-01 13:50:13 +08:00
MenuId : id ,
}
2023-01-18 17:09:49 +08:00
_ , err := refs . SqlDB . NewSession ( ) . Where ( "menu_id = ?" , menu . MenuId ) . Get ( & menu )
2022-03-01 13:50:13 +08:00
if err != nil {
common . ErrorLog ( err )
return nil
}
return & menu
}
// Insert 添加菜单数据
2023-01-18 17:21:12 +08:00
func ( d MenuDao ) Insert ( menu dataobject . SysMenu ) int64 {
2023-01-18 17:09:49 +08:00
session := refs . SqlDB . NewSession ( )
2022-03-01 13:50:13 +08:00
session . Begin ( )
insert , err := session . Insert ( & menu )
if err != nil {
common . ErrorLog ( err )
session . Rollback ( )
return 0
}
session . Commit ( )
return insert
}
// Update 修改菜单数据
2023-01-18 17:21:12 +08:00
func ( d MenuDao ) Update ( menu dataobject . SysMenu ) int64 {
2023-01-18 17:09:49 +08:00
session := refs . SqlDB . NewSession ( )
2022-03-01 13:50:13 +08:00
session . Begin ( )
update , err := session . Where ( "menu_id = ?" , menu . MenuId ) . Update ( & menu )
if err != nil {
common . ErrorLog ( err )
session . Rollback ( )
return 0
}
session . Commit ( )
return update
}
// Delete 删除菜单操作
func ( d MenuDao ) Delete ( id int ) int64 {
2023-01-18 17:21:12 +08:00
menu := dataobject . SysMenu {
2022-03-01 13:50:13 +08:00
MenuId : id ,
}
2023-01-18 17:09:49 +08:00
session := refs . SqlDB . NewSession ( )
2022-03-01 13:50:13 +08:00
session . Begin ( )
i , err := session . Delete ( & menu )
if err != nil {
common . ErrorLog ( err )
session . Rollback ( )
return 0
}
session . Commit ( )
return i
}