Esempio n. 1
0
        public async Task <MessageModel <PageModel <Role> > > Get(int page = 1, int f = 0, string key = "")
        {
            if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
            {
                key = "";
            }

            int intPageSize = 50;

            //var roleList = await _roleServices.QueryPage(a => a.IsDeleted != true && (a.Name != null && a.Name.Contains(key)), page, intPageSize, " Id desc ");
            var userRoleIds = (await _userRoleServices.Query(it => it.UserId == _user.ID && it.IsDeleted == false)).Select(it => it.RoleId).ToList();
            PageModel <Role> roles;

            if (userRoleIds.Contains(1))
            {
                roles = await _roleServices.QueryPage(a => a.IsDeleted != true &&
                                                      a.Pid == f && (key == "" || a.Name != null && a.Name.Contains(key)),
                                                      page, intPageSize, " Id desc ");
            }
            else
            {
                if (f > 0)
                {
                    roles = await _roleServices.QueryPage(a => a.IsDeleted != true &&
                                                          a.Pid == f && (key == "" || a.Name != null && a.Name.Contains(key)),
                                                          page, intPageSize, " Id desc ");
                }
                else
                {
                    roles = await _roleServices.QueryPage(a => a.IsDeleted != true &&
                                                          userRoleIds.Contains(a.Id) && (key == "" || a.Name != null && a.Name.Contains(key)),
                                                          page, intPageSize, " Id desc ");
                }
            }

            foreach (var item in roles.data)
            {
                List <int> pidarr = new List <int> {
                };
                var parent        = await _roleServices.QueryById(item.Pid);

                while (parent != null)
                {
                    pidarr.Add(parent.Id);
                    parent = await _roleServices.QueryById(parent.Pid);
                }

                pidarr.Reverse();
                pidarr.Insert(0, 0);
                item.PidArr      = pidarr;
                item.hasChildren = await _roleServices.ExistsChild(item.Id);
            }

            return(new MessageModel <PageModel <Role> >()
            {
                msg = "获取成功",
                success = roles.dataCount >= 0,
                response = roles
            });
        }