Commit a76f6495 by wyc

用户查询优化

parent 2781c062
......@@ -169,13 +169,11 @@ public List<UserResponse> GetUserList(int userID, int roleType = 1)
if (userConter?.URole.IsViewAllUsers == 2)
{
var userlist = users?.Where(t => t.CreateUser == userID && t.IsDelete == 1 && (t.ParentID == 0 || t.ParentID == null));
var sonUser = users?.Where(t => t.ParentID != 0 && t.ParentID != null);
var userlist = users?.Where(t => t.CreateUser == userID && t.IsDelete == 1 && (t.ParentID == 0 || t.ParentID == null)).ToList();
var sonUser = users?.Where(t => t.ParentID != 0 && t.ParentID != null && t.Department != "").ToList();
if (sonUser != null)
foreach (var user in sonUser)
{
if (user.Department == "")
continue;
var parentUser = userlist.FirstOrDefault(t => t.ID == user.ParentID);
if (parentUser == null) continue;
parentUser.Department = user.Department;
......@@ -184,36 +182,34 @@ public List<UserResponse> GetUserList(int userID, int roleType = 1)
}
else
{
var hospitalIds = userHospitals?.Where(t => t.UserID == userID)?.Select(t => t.HospitalID);
if (hospitalIds == null || !hospitalIds.Any()) return result;
var hospitalIds = userHospitals?.Where(t => t.UserID == userID)?.Select(t => t.HospitalID).ToList();
if (hospitalIds?.Count == 0 || !hospitalIds.Any()) return result;
var userIds = userHospitals?.Where(t => hospitalIds.Contains(t.HospitalID)).Select(t => t.UserID).Distinct().ToList();
var userlist = users?.Where(t => t.ID != userID && userIds.Contains(t.ID) && t.IsDelete == 1 && (t.ParentID == 0 || t.ParentID == null)).ToList();
var sonUser = users?.Where(t => t.ParentID != 0 && t.ParentID != null).Where(w => !string.IsNullOrEmpty(w.Department));
var sonUser = users?.Where(t => t.ParentID != 0 && t.ParentID != null && t.Department != "").ToList();
var filteredUserList = userlist.Where(w => sonUser.Select(s => s.ParentID).Contains(w.ID)).ToList();
if (sonUser != null)
{
foreach (var user in sonUser)
{
var parentUser = userlist.FirstOrDefault(t => t.ID == user.ParentID);
var parentUser = filteredUserList.FirstOrDefault(t => t.ID == user.ParentID);
if (parentUser == null) continue;
parentUser.Department = user?.Department;
}
}
result = _mapper.Map<List<UserResponse>>(userlist);
}
if (result != null && result.Count > 0)
if (result.Count > 0)
{
foreach (var item in result)
{
var hoslist = userHospitals?.Where(p => p.UserID == item.UserID);
if (hoslist != null && hoslist.Count() > 0)
if (hoslist.Count() > 0)
{
var hosids = hoslist.Select(p => p.HospitalID.Value).ToList();
item.Hospital = string.Join(",", hosids);
item.HospitalNameArr = hospitals?.Where(w => hosids.Contains(w.ID)).Select(w => w.HosName).ToArray();
}
List<int> roleId = new List<int>();
var roleId = new List<int>();
var userRole = userRoles?.FirstOrDefault(t => t.UserID == item.UserID);
if (userRole != null)
{
......@@ -221,17 +217,15 @@ public List<UserResponse> GetUserList(int userID, int roleType = 1)
roleId.Add(userRole.RoleID);
}
var diffUserRole = users?.Where(c => c.ParentID == item.UserID);
var diffUserRole = users?.Where(c => c.ParentID == item.UserID).ToList();
if (diffUserRole != null)
{
foreach (var user in diffUserRole)
{
var diffRole = userRoles?.FirstOrDefault(t => t.UserID == user.ID);
roleId.Add(diffRole.RoleID);
}
}
item.RoleArr = roleId?.ToArray();
item.RoleNameArr = roles?.Where(w => roleId.Contains(w.ID)).Select(w => w.RoleName).ToArray();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment