using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper;
using GxPress.Repository.Interface;
using GxPress.Request.Department;
using GxPress.Result.Department;
using GxPress.Service.Interface;
using GxPress.Service.Interface.Department;
using GxPress.Repository.Interface.DepartmentUser;
using System.Linq;
using Datory;
using GxPress.EnumConst;
namespace GxPress.Service.Implement.Department
{
///
/// 部门
///
public partial class DepartmentService : IDepartmentService
{
private readonly IMapper _mapper;
private readonly IDepartmentRepository _departmentRepository;
private readonly IUserRepository _userRepository;
private readonly IUserService _userService;
private readonly IDepartmentUserRepository departmentUserRepository;
public DepartmentService(IMapper mapper, IDepartmentRepository departmentRepository,
IUserRepository userRepository, IUserService userService, IDepartmentUserRepository departmentUserRepository)
{
_departmentRepository = departmentRepository;
_userRepository = userRepository;
_userService = userService;
_mapper = mapper;
this.departmentUserRepository = departmentUserRepository;
}
///
/// 根据部门ID获取成员以及下级部门
///
///
///
public async Task GetDepartmentUserResultAsync(DepartmentUserRequest request)
{
var result = await _departmentRepository.GetDepartmentUserResultAsync(request);
if (result.Departments != null)
{
foreach (var item in result.Departments)
{
item.TypeId = UserlinkConst.Work.GetHashCode();
//获取部门
var departments = new List();
await _departmentRepository.GetDepartmentById(item.Id, departments);
var userIds = await departmentUserRepository.GetUserIdsByDepartmentIdsAsync(departments.Select(n => n.Id));
item.UserCount = await _userRepository.CountAsync(Q.WhereIn(nameof(Entity.User.Id), userIds));
}
}
return result;
}
public async Task> GetUserInDepartmentAsync(int userId)
{
//获取用户所在部门
var departmentIds = await departmentUserRepository.GetDepartmentIdsAsync(userId);
departmentIds = departmentIds.Distinct();
//
var departmentIdsList = new List();
var result = new DepartmentUserInsResult();
result.Result = new List();
var allDepartment = await _departmentRepository.GetAllAsync(Q.Where(nameof(Entity.Department.Id), ">", 0));
foreach (var departmentId in departmentIds)
{
result.Result.Add(new DepartmentUserInResult
{
DepartmentId = departmentId,
Item = await _departmentRepository.GetDepartmentByPid(departmentId, new List(), allDepartment.ToList())
});
}
if (departmentIds.Count() == 1)
departmentIdsList.AddRange(departmentIds);
for (int i = 0; i < result.Result.Count; i++)
{
for (int k = i + 1; k < result.Result.Count; k++)
{
if (result.Result[i].Item.Any(n => n.Id == result.Result[k].DepartmentId))
{
departmentIdsList.Add(result.Result[i].DepartmentId);
}
else if (result.Result[k].Item.Any(n => n.Id == result.Result[i].DepartmentId))
{
departmentIdsList.Add(result.Result[k].DepartmentId);
result.Result[i].DepartmentId = result.Result[k].DepartmentId;
result.Result[i].Item = result.Result[k].Item;
}
else
{
//departmentIdsList.Add(result.Result[i].DepartmentId);
departmentIdsList.Add(result.Result[k].DepartmentId);
}
}
}
departmentIdsList = await GetDepartmentDistinct(allDepartment.ToList(), departmentIdsList.Distinct().ToList());
return await _departmentRepository.GetAllAsync(Q.WhereIn(nameof(Entity.Department.Id), departmentIdsList));
//
}
public async Task> GetDepartmentDistinct(List allDepartment, List departmentIds)
{
//
var departmentIdsList = new List();
var result = new DepartmentUserInsResult();
result.Result = new List();
foreach (var departmentId in departmentIds)
{
result.Result.Add(new DepartmentUserInResult
{
DepartmentId = departmentId,
Item = await _departmentRepository.GetDepartmentByPid(departmentId, new List(), allDepartment.ToList())
});
}
if (departmentIds.Count() == 1)
departmentIdsList.AddRange(departmentIds);
for (int i = 0; i < result.Result.Count; i++)
{
for (int k = i + 1; k < result.Result.Count; k++)
{
if (result.Result[i].Item.Any(n => n.Id == result.Result[k].DepartmentId))
{
departmentIdsList.Add(result.Result[i].DepartmentId);
}
else if (result.Result[k].Item.Any(n => n.Id == result.Result[i].DepartmentId))
{
departmentIdsList.Add(result.Result[k].DepartmentId);
result.Result[i].DepartmentId = result.Result[k].DepartmentId;
result.Result[i].Item = result.Result[k].Item;
}
else
{
//departmentIdsList.Add(result.Result[i].DepartmentId);
departmentIdsList.Add(result.Result[i].DepartmentId);
}
}
}
return departmentIdsList;
}
}
}