HRM_RabbitMQ_Consumer/JSONParser/Structure/Employee.cs

162 lines
5.2 KiB
C#

using Newtonsoft.Json;
using Simple.OData.Client;
namespace JSONParser.Structure
{
public class Employee : Entity
{
public Person Person { get; set; }
public Login Login { get; set; }
public string Email { get; set; }
public Department Department { get; set; }
public JobTitle JobTitle { get; set; }
public string JobTitleName { get; set; }
public bool Status { get; set; }
public override string Serialize()
{
return JsonConvert.SerializeObject(this);
}
public override string ToString()
{
if (Person != null)
{
var fullname = $"{Person.LastName} {Person.FirstName}";
if (!string.IsNullOrEmpty(Person.MiddleName))
{
fullname += $" {Person.MiddleName}";
}
return fullname;
}
return base.ToString();
}
public async Task<long> FindByLogin(ODataClient client)
{
if (Login == null) return 0;
var byLogin = await client
.For("IEmployees")
.Expand("Login")
.Filter($"tolower(Login/LoginName) eq '{Login.LoginName?.ToLower()}'")
.FindEntryAsync();
if (byLogin == null) return 0;
return (long)byLogin["Id"];
}
public async override Task<long> Create(ODataClient client)
{
var newEmployee = new
{
ExternalId = Sid,
Person = new { Id = Person.DirectumId },
Department = new { Id = Department.DirectumId },
JobTitle = new { Id = JobTitle.DirectumId },
Login = new { Id = Login == null ? 0 : Login.DirectumId },
Email = Email,
NeedNotifyExpiredAssignments = false,
NeedNotifyNewAssignments = false,
NeedNotifyAssignmentsSummary = false,
Status = "Active"
};
var created = await client
.For("IEmployees")
.Set(newEmployee)
.InsertEntryAsync();
this.DirectumId = (long)created["Id"];
return (long)created["Id"];
}
public async override Task<long> Update(ODataClient client, long id)
{
var newEmployee = new
{
ExternalId = Sid,
Person = new { Id = Person.DirectumId },
Department = new { Id = Department.DirectumId },
JobTitle = new { Id = JobTitle.DirectumId },
Login = new { Id = Login == null ? 0 : Login.DirectumId },
Email = Email,
NeedNotifyExpiredAssignments = false,
NeedNotifyNewAssignments = false,
NeedNotifyAssignmentsSummary = false,
Status = Status ? "Active" : "Closed"
};
var updated = await client
.For("IEmployees")
.Key(id)
.Set(newEmployee)
.UpdateEntryAsync();
this.DirectumId = (long)updated["Id"];
return (long)updated["Id"];
}
public async Task CloseRecord(ODataClient client, long id)
{
var update = new { Login = new { Id = 0 }, Status = "Closed" };
await client
.For("IEmployees")
.Key(id)
.Set(update)
.UpdateEntryAsync();
}
internal bool IsEqual(IDictionary<string, dynamic>? emp)
{
bool result = false;
long? personId = null;
long? loginId = null;
long? departmentId = null;
long? jobTitleId = null;
bool status = false;
if (emp["Person"] != null)
{
personId = emp["Person"]["Id"];
}
if (emp["Login"] != null)
{
loginId = emp["Login"]["Id"];
}
if (emp["Department"] != null)
{
departmentId = emp["Department"]["Id"];
}
if (emp["JobTitle"] != null)
{
jobTitleId = emp["JobTitle"]["Id"];
}
if (emp["Status"] != null)
{
status = emp["Status"] == "Active" ? true : false;
}
if (Person?.DirectumId == personId &&
Login?.DirectumId == loginId &&
Email == (string)emp["Email"] &&
Department?.DirectumId == departmentId &&
JobTitle?.DirectumId == jobTitleId &&
Status == status)
{
result = true;
}
return result;
}
public async Task<long> TryFindBySid(ODataClient client)
{
long result = 0;
var employee = await client
.For("IEmployees")
.Filter($"ExternalId eq '{Sid}' and Status eq 'Active'")
.FindEntryAsync();
if (employee != null)
{
result = (long)employee["Id"];
}
return result;
}
}
}