278 lines
11 KiB
C#
278 lines
11 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using Zxd.Core.Domain.Dto;
|
||
|
||
namespace Zxd.Domain
|
||
{
|
||
internal class InneruserDomain : IInneruserDomain
|
||
{
|
||
private readonly IConfiguration _configuration;
|
||
private readonly IHttpClient _httpClient;
|
||
private readonly IBaseRepository<ZxdDbContext> _repository;
|
||
|
||
private SsoOrganization SsoOrganization { get; set; }
|
||
|
||
public InneruserDomain(IConfiguration configuration,
|
||
IHttpClient httpClient,
|
||
IBaseRepository<ZxdDbContext> repository)
|
||
{
|
||
_configuration = configuration;
|
||
_httpClient = httpClient;
|
||
_repository = repository;
|
||
}
|
||
|
||
public async Task<bool> SyncSsoOrganization()
|
||
{
|
||
try
|
||
{
|
||
return await OrganizationAnalyse(await GetSsoOrganization());
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Log.Error($"同步SSO报错", ex);
|
||
return false;
|
||
}
|
||
}
|
||
|
||
private async Task<SsoOrganization> GetSsoOrganization()
|
||
{
|
||
try
|
||
{
|
||
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;
|
||
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)
|
||
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<List<SsoOrganizationDto>> GetOrganization()
|
||
{
|
||
var data = new List<SsoOrganizationDto>();
|
||
var ssoOrganization = await GetSsoOrganization();
|
||
if (ssoOrganization == null)
|
||
return data;
|
||
|
||
foreach (var deparment in ssoOrganization.other_departments.Where(x => x.parent_id == null && x.is_deleted == 0))
|
||
{
|
||
data.Add(new SsoOrganizationDto
|
||
{
|
||
Id = deparment.id,
|
||
DepartmentCode = deparment.department_code,
|
||
DepartmentName = deparment.department_name,
|
||
ParentId = deparment.parent_id,
|
||
Status = deparment.status,
|
||
Children = GetChildren(deparment.id, ssoOrganization),
|
||
SsoStaffs = GetSsoStaffs(deparment.id, ssoOrganization)
|
||
});
|
||
}
|
||
return data;
|
||
}
|
||
|
||
private static List<SsoOrganizationDto> GetChildren(int parentId, SsoOrganization ssoOrganization)
|
||
{
|
||
var data = new List<SsoOrganizationDto>();
|
||
foreach (var deparment in ssoOrganization.other_departments.Where(x => x.parent_id == parentId && x.is_deleted == 0))
|
||
{
|
||
data.Add(new SsoOrganizationDto
|
||
{
|
||
Id = deparment.id,
|
||
DepartmentCode = deparment.department_code,
|
||
DepartmentName = deparment.department_name,
|
||
ParentId = deparment.parent_id,
|
||
Status = deparment.status,
|
||
Children = GetChildren(deparment.id, ssoOrganization),
|
||
SsoStaffs = GetSsoStaffs(deparment.id, ssoOrganization)
|
||
});
|
||
}
|
||
return data;
|
||
}
|
||
|
||
private static List<SsoStaffDto> GetSsoStaffs(int departmentId, SsoOrganization ssoOrganization)
|
||
{
|
||
var data = new List<SsoStaffDto>();
|
||
foreach (var employeeDepartments in ssoOrganization.employee_departments.Where(x => x.department_id == departmentId && x.is_deleted == 0))
|
||
{
|
||
var employee = ssoOrganization.employees.FirstOrDefault(x => x.id == employeeDepartments.employee_id);
|
||
if (employee == null) continue;
|
||
data.Add(new SsoStaffDto
|
||
{
|
||
Eid = employee.employee_id,
|
||
Name = employee.employee_name,
|
||
});
|
||
}
|
||
return data;
|
||
}
|
||
|
||
public async Task<InneruserDto> GetInneruser(int eid)
|
||
{
|
||
var data = await _repository.GetRepository<BAS_INNERUSER>().Query()
|
||
.Where(x => x.EID == eid)
|
||
.Select(x => new InneruserDto
|
||
{
|
||
Eid = x.EID,
|
||
Inneruserid = x.PKID,
|
||
Username = x.UNAME
|
||
})
|
||
.FirstOrDefaultAsync();
|
||
|
||
return data;
|
||
}
|
||
}
|
||
}
|