123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- package system
- import (
- "errors"
- "strconv"
- "github.com/flipped-aurora/gin-vue-admin/server/global"
- "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
- "github.com/flipped-aurora/gin-vue-admin/server/model/system"
- "gorm.io/gorm"
- )
- type MenuService struct {
- }
- var MenuServiceApp = new(MenuService)
- func (menuService *MenuService) getMenuTreeMap(authorityId string) (err error, treeMap map[string][]system.SysMenu) {
- var allMenus []system.SysMenu
- treeMap = make(map[string][]system.SysMenu)
- err = global.GVA_DB.Where("authority_id = ?", authorityId).Order("sort").Preload("Parameters").Find(&allMenus).Error
- for _, v := range allMenus {
- treeMap[v.ParentId] = append(treeMap[v.ParentId], v)
- }
- return err, treeMap
- }
- func (menuService *MenuService) GetMenuTree(authorityId string) (err error, menus []system.SysMenu) {
- err, menuTree := menuService.getMenuTreeMap(authorityId)
- menus = menuTree["0"]
- for i := 0; i < len(menus); i++ {
- err = menuService.getChildrenList(&menus[i], menuTree)
- }
- return err, menus
- }
- func (menuService *MenuService) getChildrenList(menu *system.SysMenu, treeMap map[string][]system.SysMenu) (err error) {
- menu.Children = treeMap[menu.MenuId]
- for i := 0; i < len(menu.Children); i++ {
- err = menuService.getChildrenList(&menu.Children[i], treeMap)
- }
- return err
- }
- func (menuService *MenuService) GetInfoList() (err error, list interface{}, total int64) {
- var menuList []system.SysBaseMenu
- err, treeMap := menuService.getBaseMenuTreeMap()
- menuList = treeMap["0"]
- for i := 0; i < len(menuList); i++ {
- err = menuService.getBaseChildrenList(&menuList[i], treeMap)
- }
- return err, menuList, total
- }
- func (menuService *MenuService) getBaseChildrenList(menu *system.SysBaseMenu, treeMap map[string][]system.SysBaseMenu) (err error) {
- menu.Children = treeMap[strconv.Itoa(int(menu.ID))]
- for i := 0; i < len(menu.Children); i++ {
- err = menuService.getBaseChildrenList(&menu.Children[i], treeMap)
- }
- return err
- }
- func (menuService *MenuService) AddBaseMenu(menu system.SysBaseMenu) error {
- if !errors.Is(global.GVA_DB.Where("name = ?", menu.Name).First(&system.SysBaseMenu{}).Error, gorm.ErrRecordNotFound) {
- return errors.New("存在重复name,请修改name")
- }
- return global.GVA_DB.Create(&menu).Error
- }
- func (menuService *MenuService) getBaseMenuTreeMap() (err error, treeMap map[string][]system.SysBaseMenu) {
- var allMenus []system.SysBaseMenu
- treeMap = make(map[string][]system.SysBaseMenu)
- err = global.GVA_DB.Order("sort").Preload("Parameters").Find(&allMenus).Error
- for _, v := range allMenus {
- treeMap[v.ParentId] = append(treeMap[v.ParentId], v)
- }
- return err, treeMap
- }
- func (menuService *MenuService) GetBaseMenuTree() (err error, menus []system.SysBaseMenu) {
- err, treeMap := menuService.getBaseMenuTreeMap()
- menus = treeMap["0"]
- for i := 0; i < len(menus); i++ {
- err = menuService.getBaseChildrenList(&menus[i], treeMap)
- }
- return err, menus
- }
- func (menuService *MenuService) AddMenuAuthority(menus []system.SysBaseMenu, authorityId string) (err error) {
- var auth system.SysAuthority
- auth.AuthorityId = authorityId
- auth.SysBaseMenus = menus
- err = AuthorityServiceApp.SetMenuAuthority(&auth)
- return err
- }
- func (menuService *MenuService) GetMenuAuthority(info *request.GetAuthorityId) (err error, menus []system.SysMenu) {
- err = global.GVA_DB.Where("authority_id = ? ", info.AuthorityId).Order("sort").Find(&menus).Error
-
-
- return err, menus
- }
|