Exemple #1
0
 public async Task <IPagedResult <GetUserNormOutput> > SearchUserGroupUser(QueryUserGroupUserInput query)
 {
     if (query.UserGroupId <= 0)
     {
         throw new BusinessException("必须指定合法的UserGroupId");
     }
     if (!query.Sorting.IsNullOrEmpty() && !AuthConstant.UserGroupSortingFileds.Any(p => p == query.Sorting))
     {
         throw new BusinessException("指定的排序字段无效");
     }
     return(await _userGroupDomainService.SearchUserGroupUser(query));
 }
        public async Task <IPagedResult <GetUserNormOutput> > SearchUserGroupUser(QueryUserGroupUserInput query)
        {
            var querySql  = @"SELECT u.*,u.CreateBy as CreatorUserId, u.CreateTime as CreationTime, u.UpdateBy as LastModifierUserId, u.UpdateTime as LastModificationTime
FROM UserGroup as ug 
INNER JOIN UserUserGroupRelation as uugr on ug.Id=uugr.UserGroupId AND ug.IsDeleted=@IsDeleted AND ug.TenantId=@TenantId
INNER JOIN UserInfo as u on uugr.UserId=u.Id AND u.IsDeleted=@IsDeleted AND u.TenantId=@TenantId
WHERE uugr.UserGroupId=@UserGroupId AND uugr.TenantId=@TenantId";
            var sqlParams = new Dictionary <string, object>();

            sqlParams.Add("UserGroupId", query.UserGroupId);
            sqlParams.Add("IsDeleted", HeroConstants.UnDeletedFlag);
            sqlParams.Add("TenantId", _session.TenantId);
            if (query.OrgId.HasValue && query.OrgId.Value != 0)
            {
                var subOrgIds = await GetService <IOrganizationAppService>().GetSubOrgIds(query.OrgId.Value);

                querySql += " AND u.OrgId in @DataPermissionOrgIds";
                sqlParams.Add("DataPermissionOrgIds", subOrgIds);
            }

            if (query.PositionId.HasValue && query.PositionId.Value != 0)
            {
                querySql += " AND u.PositionId=@PositionId";
                sqlParams.Add("PositionId", query.PositionId.Value);
            }

            if (!query.SearchKey.IsNullOrEmpty())
            {
                querySql +=
                    " AND (u.UserName like @UserName or u.ChineseName like @ChineseName or u.Phone like @Phone or u.Email like @Email)";
                sqlParams.Add("UserName", $"%{query.SearchKey}%");
                sqlParams.Add("ChineseName", $"%{query.SearchKey}%");
                sqlParams.Add("Phone", $"%{query.SearchKey}%");
                sqlParams.Add("Email", $"%{query.SearchKey}%");
            }

            var queryCountSql = string.Format(querySql, "COUNT(u.Id)");

            if (!query.Sorting.IsNullOrEmpty())
            {
                querySql += $" ORDER BY u.{query.Sorting} {query.SortType}";
            }
            else
            {
                querySql += " ORDER BY u.Id DESC";
            }
            querySql += $" LIMIT {(query.PageIndex - 1) * query.PageCount} , {query.PageCount} ";
            querySql  = string.Format(querySql,
                                      "u.*,u.CreateBy as CreatorUserId, u.CreateTime as CreationTime, u.UpdateBy as LastModifierUserId, u.UpdateTime as LastModificationTime");
            await using var conn = Connection;
            var queryResult = await conn.QueryAsync <UserInfo>(querySql, sqlParams);

            var queryCount = await conn.ExecuteScalarAsync <int>(queryCountSql, sqlParams);

            var queryResultOutput = queryResult.MapTo <IEnumerable <GetUserNormOutput> >().GetPagedResult(queryCount);

            foreach (var userOutput in queryResultOutput.Items)
            {
                if (userOutput.OrgId.HasValue)
                {
                    userOutput.DeptName =
                        (await GetService <IDepartmentAppService>().GetByOrgId(userOutput.OrgId.Value)).Name;
                }
                if (userOutput.PositionId.HasValue)
                {
                    userOutput.PositionName =
                        (await GetService <IPositionAppService>().Get(userOutput.PositionId.Value)).Name;
                }
                userOutput.Roles      = (await GetUserRoles(userOutput.Id)).MapTo <IEnumerable <GetDisplayRoleOutput> >();
                userOutput.UserGroups =
                    (await GetUserGroups(userOutput.Id)).MapTo <IEnumerable <GetDisplayUserGroupOutput> >();
                await userOutput.SetAuditInfo();
            }

            return(queryResultOutput);
        }