HRM_RabbitMQ_Consumer/JSONParser/Structure/Department.cs

106 lines
3.3 KiB
C#

using Simple.OData.Client;
using System.Text.Json;
namespace JSONParser.Structure
{
public class Department : Entity
{
public string Name { get; set; }
public string ShortName { get; set; }
public BusinessUnit BusinessUnit { get; set; }
public Department HeadOffice { get; set; }
public string Status { get; set; }
public override string ToString()
{
return Name;
}
public override string Serialize()
{
return JsonSerializer.Serialize(this);
}
public override async Task<long> Create(ODataClient client)
{
var newDept = new
{
Name = this.Name,
Status = "Active",
ShortName = this.ShortName,
ExternalId = this.Sid,
HeadOffice = new { Id = this.HeadOffice == null ? 0 : this.HeadOffice.DirectumId },
BusinessUnit = new { Id = this.BusinessUnit?.DirectumId }
};
var created = await client
.For("IDepartment")
.Set(newDept)
.InsertEntryAsync();
this.DirectumId = (long)created["Id"];
return (long)created["Id"];
}
public async override Task<long> Update(ODataClient client, long id)
{
var newDept = new
{
Name = this.Name,
Status = "Active",
ShortName = this.ShortName,
ExternalId = this.Sid,
HeadOffice = new { Id = this.HeadOffice == null ? 0 : this.HeadOffice.DirectumId },
BusinessUnit = new { Id = this.BusinessUnit?.DirectumId }
};
var updated = await client
.For("IDepartment")
.Key(id)
.Set(newDept)
.UpdateEntryAsync();
this.DirectumId = (long)updated["Id"];
return (long)updated["Id"];
}
public async Task<long> GetIdBySid(ODataClient client)
{
long result = 0;
if (!string.IsNullOrEmpty(Sid))
{
var found = await client
.For("IDepartment")
.Filter($"ExternalId eq '{this.Sid}'")
.FindEntryAsync();
if (found != null)
{
DirectumId = (long)found["Id"];
result = (long)found["Id"];
}
}
return result;
}
public bool IsEqual(IDictionary<string, dynamic> dept)
{
var result = false;
long? businessUnitId = null;
long? headOfficeId = null;
if (dept["BusinessUnit"] != null)
{
businessUnitId = dept["BusinessUnit"]["Id"];
}
if (dept["HeadOffice"] != null)
{
headOfficeId = dept["HeadOffice"]["Id"];
}
if (Name == dept["Name"] &&
ShortName == dept["ShortName"] &&
BusinessUnit?.DirectumId == businessUnitId &&
HeadOffice?.DirectumId == headOfficeId)
{
result = true;
}
return result;
}
}
}