package backend import ( "context" "kpt-tmr-group/model" "kpt-tmr-group/pkg/logger/zaplog" "kpt-tmr-group/pkg/xerr" operationPb "kpt-tmr-group/proto/go/backend/operation" "net/http" "sort" "strings" "sync" "time" "go.uber.org/zap" ) type SystemAllPermissionsList struct { PastureList []*model.SystemGroupPasturePermissions MenuList []*model.SystemMenuPermissions MobileList []*model.SystemMobilePermissions } func (s *StoreEntry) AllPermissionsListToRolePermissions(req *SystemAllPermissionsList) *operationPb.RolePermissionsList { res := &operationPb.RolePermissionsList{ Code: http.StatusOK, Msg: "ok", Data: &operationPb.RolePermissionsData{ MobileList: make([]uint32, 0), MenuList: make([]uint32, 0), PastureList: make([]uint32, 0), }, } wg := sync.WaitGroup{} wg.Add(3) go func() { defer wg.Done() for _, v := range req.MobileList { res.Data.MobileList = append(res.Data.MobileList, uint32(v.MobileId)) } }() go func() { defer wg.Done() for _, v := range req.MenuList { res.Data.MenuList = append(res.Data.MenuList, uint32(v.MenuId)) } }() go func() { defer wg.Done() for _, v := range req.PastureList { res.Data.PastureList = append(res.Data.PastureList, uint32(v.PastureId)) } }() wg.Wait() return res } // SystemUserMenuPermissionsUnDuplicate 角色权限去重 func (s *SystemAllPermissionsList) SystemUserMenuPermissionsUnDuplicate() { newMenuList := make([]*model.SystemMenuPermissions, 0) seen := make(map[int64]bool) for _, menu := range s.MenuList { if _, ok := seen[menu.MenuId]; !ok { newMenuList = append(newMenuList, menu) } } s.MenuList = newMenuList } func (s *StoreEntry) SystemPermissionsFormatPb(pastureList []*model.GroupPasture, mobileList []*model.SystemMobile, menuList []*model.SystemMenu) *operationPb.SystemUserMenuPermissions { systemUserMenuPermissions := &operationPb.SystemUserMenuPermissions{ Code: http.StatusOK, Msg: "ok", Data: &operationPb.SystemUserMenuData{ PastureList: make([]*operationPb.AddPastureRequest, 0), MenuList: make([]*operationPb.AddMenuRequest, 0), MobileList: make([]*operationPb.AddMobileRequest, 0), MenuButtonsPath: make([]*operationPb.MenuButtonsPath, 0), }, } wg := sync.WaitGroup{} wg.Add(3) go func() { for _, v := range pastureList { systemUserMenuPermissions.Data.PastureList = append(systemUserMenuPermissions.Data.PastureList, &operationPb.AddPastureRequest{ Id: int32(v.Id), Name: v.Name, }, ) } wg.Done() }() // TODO 后面优化成递归算法 go func() { level := make(map[int32][]*operationPb.AddMenuRequest, 0) for _, menu := range menuList { if _, ok := level[menu.Level]; !ok { level[menu.Level] = make([]*operationPb.AddMenuRequest, 0) } level[menu.Level] = append(level[menu.Level], &operationPb.AddMenuRequest{ Id: int32(menu.Id), Name: menu.Name, ParentId: int32(menu.ParentId), MenuType: menu.MenuType, Title: menu.Title, Path: menu.Path, IsShow: menu.IsShow, Component: menu.Component, Icon: menu.Icon, Sort: menu.Sort, Redirect: menu.Redirect, CreatedAt: int32(menu.CreatedAt), CreatedAtFormat: time.Unix(menu.CreatedAt, 0).Format(model.LayoutTime), Level: menu.Level, Affix: true, KeepAlive: true, Children: make([]*operationPb.AddMenuRequest, 0), }) if menu.Level == model.Level3 { systemUserMenuPermissions.Data.MenuButtonsPath = append(systemUserMenuPermissions.Data.MenuButtonsPath, &operationPb.MenuButtonsPath{ Path: menu.Path, MenuId: int32(menu.Id), }) } } level2IsShow := make([]int32, 0) for _, leve2Data := range level[model.Level2] { for _, leve3Data := range level[model.Level3] { if leve3Data.ParentId == leve2Data.Id && strings.Contains(leve3Data.Path, ":page") { level2IsShow = append(level2IsShow, leve2Data.Id) } if leve3Data.ParentId == leve2Data.Id { if leve2Data.Children == nil { leve2Data.Children = make([]*operationPb.AddMenuRequest, 0) } leve2Data.Children = append(leve2Data.Children, leve3Data) } } } sort.SliceStable(level[model.Level2], func(i, j int) bool { return level[model.Level2][i].Sort > level[model.Level2][j].Sort }) for _, leve2Data := range level[model.Level2] { var info bool for _, v := range level2IsShow { if v == leve2Data.Id { info = true } } if !info { continue } for _, leve1Data := range level[model.Level1] { if leve2Data.ParentId == leve1Data.Id { if leve1Data.Children == nil { leve1Data.Children = make([]*operationPb.AddMenuRequest, 0) } leve1Data.Children = append(leve1Data.Children, leve2Data) } } } sort.SliceStable(level[model.Level1], func(i, j int) bool { return level[model.Level1][i].Sort > level[model.Level1][j].Sort }) systemUserMenuPermissions.Data.MenuList = level[model.Level1] wg.Done() }() go func() { for _, v := range mobileList { systemUserMenuPermissions.Data.MobileList = append(systemUserMenuPermissions.Data.MobileList, &operationPb.AddMobileRequest{ Id: uint32(v.Id), Name: v.Name, }, ) } wg.Done() }() wg.Wait() return systemUserMenuPermissions } // GetSystemAllPermissionsList 获取用户相关权限 func (s *StoreEntry) GetSystemAllPermissionsList(ctx context.Context, roleId int64) *SystemAllPermissionsList { systemAllPermissionsList := &SystemAllPermissionsList{ PastureList: make([]*model.SystemGroupPasturePermissions, 0), MenuList: make([]*model.SystemMenuPermissions, 0), MobileList: make([]*model.SystemMobilePermissions, 0), } wg := sync.WaitGroup{} wg.Add(3) go func() { pastureList := make([]*model.SystemGroupPasturePermissions, 0) if err := s.DB.Where("role_id = ? and is_show = ?", roleId, operationPb.IsShow_OK).Find(&pastureList).Error; err != nil { zaplog.Error("SystemPasturePermissions", zap.Any("Err", err)) } systemAllPermissionsList.PastureList = pastureList wg.Done() }() go func() { menuList := make([]*model.SystemMenuPermissions, 0) if err := s.DB.Where("role_id = ? and is_show = ?", roleId, operationPb.IsShow_OK).Find(&menuList).Error; err != nil { zaplog.Error("SystemMenuPermissions", zap.Any("Err", err)) } systemAllPermissionsList.MenuList = menuList wg.Done() }() // 获取供应商数据 go func() { mobileList := make([]*model.SystemMobilePermissions, 0) if err := s.DB.Where("role_id = ? and is_show = ?", roleId, operationPb.IsShow_OK).Find(&mobileList).Error; err != nil { zaplog.Error("SystemMobilePermissions", zap.Any("Err", err)) } systemAllPermissionsList.MobileList = mobileList wg.Done() }() wg.Wait() return systemAllPermissionsList } // GetPastureList 获取****** func (s *StoreEntry) GetPastureList(ctx context.Context, req []*model.SystemGroupPasturePermissions) ([]*model.GroupPasture, error) { ids := make([]int64, 0) for _, v := range req { ids = append(ids, v.PastureId) } groupPastureList := make([]*model.GroupPasture, 0) if len(ids) > 0 { if err := s.DB.Where("is_show = ?", operationPb.IsShow_OK).Find(&groupPastureList, ids).Error; err != nil { return nil, xerr.WithStack(err) } } return groupPastureList, nil } // GetMenuList 获取****** func (s *StoreEntry) GetMenuList(ctx context.Context, req []*model.SystemMenuPermissions) ([]*model.SystemMenu, error) { ids := make([]int64, 0) for _, v := range req { ids = append(ids, v.MenuId) } menuList := make([]*model.SystemMenu, 0) if len(ids) > 0 { if err := s.DB.Where("is_show = ?", operationPb.IsShow_OK).Find(&menuList, ids).Error; err != nil { return nil, xerr.WithStack(err) } } return menuList, nil } // GetMobileList 获取****** func (s *StoreEntry) GetMobileList(ctx context.Context, req []*model.SystemMobilePermissions) ([]*model.SystemMobile, error) { ids := make([]int64, 0) for _, v := range req { ids = append(ids, v.MobileId) } mobileList := make([]*model.SystemMobile, 0) if len(ids) > 0 { if err := s.DB.Where("is_show = ?", operationPb.IsShow_OK).Find(&mobileList, ids).Error; err != nil { return nil, xerr.WithStack(err) } } return mobileList, nil }