459 lines
19 KiB
C#
459 lines
19 KiB
C#
using Hg.Core.Domain.Sso;
|
||
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
||
|
||
namespace Hg.Core.Domain
|
||
{
|
||
public class InneruserDomain : IInneruserDomain
|
||
{
|
||
private readonly IConfiguration _configuration;
|
||
private readonly IHttpClient _httpClient;
|
||
private readonly IBaseRepository<ZxdDbContext> _repository;
|
||
private readonly IRedisManager _redisManager;
|
||
|
||
private SsoOrganization SsoOrganization { get; set; }
|
||
|
||
public InneruserDomain(IConfiguration configuration,
|
||
IHttpClient httpClient,
|
||
IBaseRepository<ZxdDbContext> repository,
|
||
IRedisManager redisManager)
|
||
{
|
||
_configuration = configuration;
|
||
_httpClient = httpClient;
|
||
_repository = repository;
|
||
_redisManager = redisManager;
|
||
}
|
||
|
||
public async Task<List<InneruserDto>> GetInnerusers(string eids)
|
||
{
|
||
try
|
||
{
|
||
if(string.IsNullOrEmpty(eids)) return new List<InneruserDto>();
|
||
var organization = SsoOrganization ?? await GetSsoOrganization();
|
||
var ids = eids.Split(',').Select(x => int.Parse(x)).ToList();
|
||
var list = organization.employees
|
||
.Where(x => ids.Contains(x.employee_id))
|
||
.Select(x => new InneruserDto(x.employee_id, x.employee_name,x.status))
|
||
.ToList();
|
||
return list;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Log.Error("查询错误!", ex);
|
||
throw new Exception("请输入正确的eid!");
|
||
}
|
||
}
|
||
|
||
public async Task<List<InneruserDto>> GetInnerusersByName(string eidsOrNames)
|
||
{
|
||
var data = new List<InneruserDto>();
|
||
try
|
||
{
|
||
var organization = SsoOrganization ?? await GetSsoOrganization();
|
||
var eidOrNameList = eidsOrNames.Split(',').ToList();
|
||
|
||
foreach (var eidOrName in eidOrNameList)
|
||
{
|
||
if (int.TryParse(eidOrName, out int eid))
|
||
{
|
||
var item = organization.employees
|
||
.Where(x => eid == x.employee_id)
|
||
.Select(x => new InneruserDto(x.employee_id, x.employee_name,x.status))
|
||
.FirstOrDefault();
|
||
if (item != null)
|
||
data.Add(item);
|
||
}
|
||
else
|
||
{
|
||
data.Add(new InneruserDto(-1, eidOrName,-1));
|
||
}
|
||
}
|
||
return data;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Log.Error("查询错误!", ex);
|
||
throw new Exception("请输入正确的eid!");
|
||
}
|
||
}
|
||
|
||
//public async Task<InnerUserDeptDto> GetInnerUserDept(int eid)
|
||
//{
|
||
// try
|
||
// {
|
||
// var userInfo = new InnerUserDeptDto();
|
||
// var organization = SsoOrganization ?? await GetSsoOrganization();
|
||
// var employee = organization.employees.FirstOrDefault(x => x.employee_id == eid);
|
||
// if (employee != null)
|
||
// {
|
||
// userInfo.EId = employee.employee_id;
|
||
// userInfo.Name = employee.employee_name;
|
||
// userInfo.Status = employee.status;
|
||
|
||
|
||
|
||
// foreach (var department in organization.profession_departments)
|
||
// {
|
||
|
||
// }
|
||
|
||
// foreach (var department in organization.other_departments)
|
||
// {
|
||
|
||
// }
|
||
|
||
// }
|
||
// return userInfo;
|
||
// }
|
||
// catch (Exception ex)
|
||
// {
|
||
// Log.Error("查询错误!", ex);
|
||
// throw new Exception("请输入正确的eid!");
|
||
// }
|
||
//}
|
||
|
||
public async Task<bool> SyncSsoOrganization()
|
||
{
|
||
try
|
||
{
|
||
var key = CacheKeys.SsoOrganization;
|
||
if (await _redisManager.ExistsAsync(key))
|
||
{
|
||
return await _redisManager.RemoveAsync(key);
|
||
}
|
||
return await OrganizationAnalyse(await GetSsoOrganization());
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
private async Task<SsoOrganization> GetSsoOrganization()
|
||
{
|
||
try
|
||
{
|
||
var key = CacheKeys.SsoOrganization;
|
||
if (await _redisManager.ExistsAsync(key))
|
||
{
|
||
return await _redisManager.GetAsync<SsoOrganization>(key);
|
||
}
|
||
else
|
||
{
|
||
var systemConfig = _configuration.GetSection("SystemConfig").Get<SystemConfig>();
|
||
var param = new Dictionary<string, object>();
|
||
param.Add("appid", systemConfig.Appid);
|
||
var response = await _httpClient.PostAsync<SsoResult<SsoOrganization>>($"{systemConfig.SsoUrl}{systemConfig.SsoOrganizationUrl}", param, systemConfig.Appid, systemConfig.AppSecret);
|
||
if (response.Ret != 0)
|
||
{
|
||
Log.Error($"SSO 组织架构获取报错,ex: {response.Msg}");
|
||
throw new Exception($"SSO 组织架构获取报错,ex: {response.Msg}");
|
||
}
|
||
SsoOrganization = response.Data;
|
||
await _redisManager.SetAsync(key, SsoOrganization, TimeSpan.FromHours(1));
|
||
return SsoOrganization;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Log.Error($"SSO 组织架构获取报错!", ex);
|
||
throw;
|
||
}
|
||
}
|
||
|
||
private async Task<bool> OrganizationAnalyse(SsoOrganization ssoOrganization)
|
||
{
|
||
var hasUpdate = false;
|
||
var employeeIds = new List<int>();
|
||
foreach (var employee in ssoOrganization.employees)
|
||
{
|
||
//如果不是数字,跳出
|
||
var employee_id = employee.employee_id;
|
||
var employee_departmentinfo = ssoOrganization.employee_departments.Where(m => m.employee_id == employee.id && m.is_deleted == 0);
|
||
//员工不在关系里面
|
||
if (!employee_departmentinfo.Any())
|
||
continue;
|
||
var departmentId = employee_departmentinfo.Select(n => n.department_id).ToList();
|
||
//员工不在指定的部门里面
|
||
if (ssoOrganization.profession_departments.FirstOrDefault(m => departmentId.Contains(m.id) && m.is_deleted == 0) == null &&
|
||
ssoOrganization.other_departments.FirstOrDefault(m => departmentId.Contains(m.id) && m.is_deleted == 0) == null)
|
||
continue;
|
||
employeeIds.Add(employee_id);
|
||
}
|
||
if (employeeIds != null && employeeIds.Any())
|
||
{
|
||
var list = await _repository.GetRepository<BAS_INNERUSER>().Query().Where(x => employeeIds.Contains(x.EID)).ToListAsync();//在坐席中对应的关系
|
||
using var transaction = await _repository.BeginTransactionAsync();
|
||
try
|
||
{
|
||
foreach (var employeeId in employeeIds)
|
||
{
|
||
var info = list.FirstOrDefault(x => x.EID == employeeId);
|
||
var employee = ssoOrganization.employees.First(x => x.employee_id == employeeId);
|
||
if (info == null)//找不到数据,就新增
|
||
{
|
||
await CreateInneruser(employeeId, employee, ssoOrganization);
|
||
hasUpdate = true;
|
||
}
|
||
else //有数据就修改
|
||
{
|
||
if (info.UNAME != employee.employee_name ||
|
||
info.DISMISSTIME != employee.leave_date ||
|
||
info.ENTRYDATE != employee.entry_date ||
|
||
info.ISHIDE != Convert.ToInt16(employee.is_deleted == 0 ? 0 : 1) ||
|
||
info.ISDISMISS != Convert.ToInt16(employee.status == 2 ? 1 : 0) ||
|
||
info.UNAME != employee.employee_name)
|
||
{
|
||
info.UNAME = employee.employee_name;
|
||
info.DISMISSTIME = employee.leave_date;
|
||
info.ENTRYDATE = employee.entry_date;
|
||
info.ISHIDE = Convert.ToInt16(employee.is_deleted == 0 ? 0 : 1);
|
||
info.ISDISMISS = Convert.ToInt16(employee.status == 2 ? 1 : 0);//是否离职,根据是否含有离职时间来
|
||
info.UNAME = employee.employee_name;
|
||
hasUpdate = true;
|
||
await _repository.GetRepository<BAS_INNERUSER>().UpdateAsync(info);
|
||
}
|
||
}
|
||
}
|
||
|
||
await transaction.CommitAsync();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Log.Error("员工信息更新报错!", ex);
|
||
transaction.Rollback();
|
||
transaction.Dispose();
|
||
return false;
|
||
}
|
||
}
|
||
|
||
if (hasUpdate)
|
||
{
|
||
await ClearCache();
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 清理缓存
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
private async Task ClearCache()
|
||
{
|
||
var systemConfig = _configuration.GetSection("SystemConfig").Get<SystemConfig>();
|
||
try
|
||
{
|
||
foreach (var clearCacheUrl in systemConfig.ClearCacheUrls)
|
||
{
|
||
var httpResponse = await _httpClient.GetAsync<string>(clearCacheUrl);
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Log.Error("清除缓存报错!", ex);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 新增员工数据
|
||
/// </summary>
|
||
/// <param name="employee_id"></param>
|
||
/// <param name="employee"></param>
|
||
/// <param name="ssoOrganization"></param>
|
||
/// <returns></returns>
|
||
private async Task CreateInneruser(int employee_id, SsoEmployee employee, SsoOrganization ssoOrganization)
|
||
{
|
||
try
|
||
{
|
||
var bas_inneruser = new BAS_INNERUSER
|
||
{
|
||
CTIME = DateTime.Now,
|
||
DISMISSTIME = employee.leave_date,
|
||
ENTRYDATE = employee.entry_date,
|
||
EID = employee_id,
|
||
ISHIDE = Convert.ToInt16(employee.is_deleted == 0 ? 0 : 1),
|
||
ISDISMISS = Convert.ToInt16(employee.leave_date.HasValue ? 1 : 0),//是否离职,根据是否含有离职时间来
|
||
UNAME = employee.employee_name
|
||
};
|
||
string password = $"{employee_id}@123";
|
||
string user_salt = Utility.CreateRandomSatl(8);
|
||
bas_inneruser.PASSWORD = Utility.Sha512(password + user_salt);//初始化密码
|
||
var userSaltModel = new BAS_INNERUSERSALT
|
||
{
|
||
INNERUSERID = bas_inneruser.PKID,
|
||
EID = employee_id,
|
||
PWDSALT = user_salt,
|
||
CTIME = DateTime.Now
|
||
};
|
||
|
||
await _repository.GetRepository<BAS_INNERUSERSALT>().InsertAsync(userSaltModel);
|
||
if (employee.sex.HasValue && employee.sex == 1)
|
||
{
|
||
bas_inneruser.GENDER = "m";
|
||
}
|
||
else
|
||
{
|
||
bas_inneruser.GENDER = "f";
|
||
}
|
||
await _repository.GetRepository<BAS_INNERUSER>().InsertAsync(bas_inneruser);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Log.Error($"员工信息{employee_id}新增报错!", ex);
|
||
}
|
||
}
|
||
|
||
public async Task<InneruserDto> GetInneruser(int eid)
|
||
{
|
||
var data = await _repository.GetRepository<BAS_INNERUSER>().Query()
|
||
.Where(x => x.EID == eid)
|
||
.Select(x => new InneruserDto(x.EID, x.UNAME,-1))
|
||
.FirstOrDefaultAsync();
|
||
|
||
return data;
|
||
}
|
||
|
||
public async Task<List<InneruserTreeDto>> GetInneruserTree()
|
||
{
|
||
var key = CacheKeys.SsoOrganization;
|
||
var ssoOrganization = await _redisManager.GetAsync<SsoOrganization>(key);
|
||
|
||
var data = new List<InneruserTreeDto>();
|
||
|
||
foreach (var department in ssoOrganization.profession_departments)
|
||
{
|
||
var item = new InneruserTreeDto
|
||
{
|
||
Value = department.id,
|
||
Name = department.department_name,
|
||
Children = new List<InneruserTreeDto>()
|
||
};
|
||
var employees = ssoOrganization.employee_departments.Where(x => x.department_id == department.id && x.is_deleted == 0).ToList();
|
||
if (employees == null || !employees.Any()) continue;
|
||
foreach(var employeeItem in employees)
|
||
{
|
||
var employee = ssoOrganization.employees.FirstOrDefault(x => x.id == employeeItem.employee_id && x.is_deleted == 0);
|
||
if (employee == null) continue;
|
||
var eid = employee.employee_id;
|
||
item.Children.Add(new InneruserTreeDto
|
||
{
|
||
Value = employee.employee_id,
|
||
Name = employee.employee_name
|
||
});
|
||
}
|
||
if (item.Children == null || !item.Children.Any()) continue;
|
||
data.Add(item);
|
||
}
|
||
|
||
foreach (var department in ssoOrganization.other_departments)
|
||
{
|
||
var item = new InneruserTreeDto
|
||
{
|
||
Value = department.id,
|
||
Name = department.department_name,
|
||
Children = new List<InneruserTreeDto>()
|
||
};
|
||
var employees = ssoOrganization.employee_departments.Where(x => x.department_id == department.id && x.is_deleted == 0).ToList();
|
||
if (employees == null || !employees.Any()) continue;
|
||
foreach (var employeeItem in employees)
|
||
{
|
||
var employee = ssoOrganization.employees.FirstOrDefault(x => x.id == employeeItem.employee_id && x.is_deleted == 0);
|
||
if (employee == null) continue;
|
||
var eid = employee.employee_id;
|
||
item.Children.Add(new InneruserTreeDto
|
||
{
|
||
Value = employee.employee_id,
|
||
Name = employee.employee_name
|
||
});
|
||
}
|
||
if (item.Children == null || !item.Children.Any()) continue;
|
||
data.Add(item);
|
||
}
|
||
|
||
return data;
|
||
}
|
||
|
||
public async Task<List<InneruserTreeDto>> GetInneruserTreeKeyName()
|
||
{
|
||
var key = CacheKeys.SsoOrganization;
|
||
var ssoOrganization = await _redisManager.GetAsync<SsoOrganization>(key);
|
||
|
||
var data = new List<InneruserTreeDto>();
|
||
|
||
foreach (var department in ssoOrganization.profession_departments)
|
||
{
|
||
//var item = new InneruserTreeDto
|
||
//{
|
||
// Value = department.department_name,
|
||
// Name = department.department_name,
|
||
// Children = new List<InneruserTreeDto>()
|
||
//};
|
||
var employees = ssoOrganization.employee_departments.Where(x => x.department_id == department.id && x.is_deleted == 0).ToList();
|
||
if (employees == null || !employees.Any()) continue;
|
||
foreach (var employeeItem in employees)
|
||
{
|
||
var employee = ssoOrganization.employees.FirstOrDefault(x => x.id == employeeItem.employee_id && x.is_deleted == 0);
|
||
if (employee == null) continue;
|
||
var item = new InneruserTreeDto
|
||
{
|
||
Value = employee.employee_name,
|
||
Name = employee.employee_name
|
||
};
|
||
//if (item.Children == null || !item.Children.Any()) continue;
|
||
data.Add(item);
|
||
}
|
||
}
|
||
|
||
foreach (var department in ssoOrganization.other_departments)
|
||
{
|
||
//var item = new InneruserTreeDto
|
||
//{
|
||
// Value = department.department_name,
|
||
// Name = department.department_name,
|
||
// Children = new List<InneruserTreeDto>()
|
||
//};
|
||
var employees = ssoOrganization.employee_departments.Where(x => x.department_id == department.id && x.is_deleted == 0).ToList();
|
||
if (employees == null || !employees.Any()) continue;
|
||
foreach (var employeeItem in employees)
|
||
{
|
||
var employee = ssoOrganization.employees.FirstOrDefault(x => x.id == employeeItem.employee_id && x.is_deleted == 0);
|
||
if (employee == null) continue;
|
||
var item = new InneruserTreeDto
|
||
{
|
||
Value = employee.employee_name,
|
||
Name = employee.employee_name
|
||
};
|
||
//if (item.Children == null || !item.Children.Any()) continue;
|
||
data.Add(item);
|
||
}
|
||
}
|
||
|
||
#region 添加自定义人员
|
||
var meetings = await _repository.GetRepository<Meeting>().Query()
|
||
.Where(x => x.Compere.Contains("\"EId\":-1,"))
|
||
.Select(x => x.Compere)
|
||
.ToListAsync();
|
||
var inners = new List<InneruserDto>();
|
||
meetings.ForEach(x =>
|
||
{
|
||
var list = JsonSerializer.Deserialize<List<InneruserDto>>(x);
|
||
inners.AddRange(list);
|
||
});
|
||
inners = inners.Where(x => x.EId == -1).Distinct().ToList();
|
||
inners.ForEach(x =>
|
||
{
|
||
if (!data.Any(y => y.Name == x.Name))
|
||
{
|
||
data.Add(new InneruserTreeDto
|
||
{
|
||
Value = x.Name,
|
||
Name = x.Name
|
||
});
|
||
}
|
||
});
|
||
#endregion
|
||
|
||
return data;
|
||
}
|
||
}
|
||
} |