181 lines
6.0 KiB
C#
181 lines
6.0 KiB
C#
using Simple.OData.Client;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace JSONParser.Structure
|
|
{
|
|
public class BusinessUnit : Entity
|
|
{
|
|
public string Name { get; set; }
|
|
public string LegalName { get; set; }
|
|
public Employee NCEO { get; set; }
|
|
public Employee Account { get; set; }
|
|
public BusinessUnit HeadCompany { get; set; }
|
|
public string BINArmadoc { get; set; }
|
|
public string LegalAddress { get; set; }
|
|
public string PostalAddress { get; set; }
|
|
public string Phones { get; set; }
|
|
|
|
public override string ToString()
|
|
{
|
|
return Name;
|
|
}
|
|
|
|
public async override Task<long> Create(ODataClient client)
|
|
{
|
|
var newBusinessUnit = new
|
|
{
|
|
ExternalId = Sid,
|
|
Name = Name,
|
|
LegalName = LegalName,
|
|
LegalAddress = LegalAddress,
|
|
PostalAddress = PostalAddress,
|
|
HeadCompany = new { Id = HeadCompany == null ? 0 : this.HeadCompany.DirectumId },
|
|
BINArmadoc = BINArmadoc,
|
|
Status = "Active"
|
|
};
|
|
var created = await client
|
|
.For("IBusinessUnits")
|
|
.Set(newBusinessUnit)
|
|
.InsertEntryAsync();
|
|
this.DirectumId = (long)created["Id"];
|
|
return (long)created["Id"];
|
|
}
|
|
|
|
public async override Task<long> Update(ODataClient client, long id)
|
|
{
|
|
var newBusinessUnit = new
|
|
{
|
|
ExternalId = Sid,
|
|
Name = Name,
|
|
LegalName = LegalName,
|
|
LegalAddress = LegalAddress,
|
|
PostalAddress = PostalAddress,
|
|
HeadCompany = new { Id = HeadCompany == null ? 0 : HeadCompany.DirectumId },
|
|
BINArmadoc = BINArmadoc,
|
|
Phones = Phones,
|
|
Status = "Active"
|
|
};
|
|
var updated = await client
|
|
.For("IBusinessUnits")
|
|
.Key(id)
|
|
.Set(newBusinessUnit)
|
|
.UpdateEntryAsync();
|
|
this.DirectumId = (long)updated["Id"];
|
|
return (long)updated["Id"];
|
|
}
|
|
|
|
public bool IsEqual(IDictionary<string, dynamic> businessUnit)
|
|
{
|
|
var result = false;
|
|
long? headComapnyId = null;
|
|
if (businessUnit["HeadCompany"] != null)
|
|
{
|
|
headComapnyId = businessUnit["HeadCompany"]["Id"];
|
|
}
|
|
dynamic headCompany = businessUnit["HeadCompany"];
|
|
if (headCompany != null)
|
|
{
|
|
headComapnyId = (long)headCompany["Id"];
|
|
}
|
|
if (Name == (string)businessUnit["Name"] &&
|
|
LegalName == (string)businessUnit["LegalName"] &&
|
|
HeadCompany?.DirectumId == headComapnyId &&
|
|
BINArmadoc == (string)businessUnit["BINArmadoc"] &&
|
|
LegalAddress == (string)businessUnit["LegalAddress"] &&
|
|
PostalAddress == (string)businessUnit["PostalAddress"] &&
|
|
Phones == (string)businessUnit["Phones"])
|
|
result = true;
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<long> CreateNCEO(ODataClient client)
|
|
{
|
|
long result = 0;
|
|
if (NCEO != null)
|
|
{
|
|
dynamic emp = await client
|
|
.For("IEmployees")
|
|
.Filter($"ExternalId eq '{NCEO.Sid}' and Status eq 'Active'")
|
|
.FindEntryAsync();
|
|
|
|
dynamic bu = await client
|
|
.For("IBusinessUnits")
|
|
.Filter($"ExternalId eq '{Sid}'")
|
|
.FindEntryAsync();
|
|
|
|
|
|
if (emp != null && bu != null)
|
|
{
|
|
var newBusinessUnit = new
|
|
{
|
|
CEO = new { Id = (long)emp["Id"] }
|
|
};
|
|
var updated = await client
|
|
.For("IBusinessUnits")
|
|
.Key((long)bu["Id"])
|
|
.Set(newBusinessUnit)
|
|
.UpdateEntryAsync();
|
|
result = (long)updated["Id"];
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public async Task<long> CreateAccount(ODataClient client)
|
|
{
|
|
long result = 0;
|
|
if (Account != null)
|
|
{
|
|
dynamic emp = await client
|
|
.For("IEmployees")
|
|
.Filter($"ExternalId eq '{Account.Sid}' and Status eq 'Active'")
|
|
.FindEntryAsync();
|
|
|
|
dynamic bu = await client
|
|
.For("IBusinessUnits")
|
|
.Filter($"ExternalId eq '{Sid}'")
|
|
.FindEntryAsync();
|
|
|
|
|
|
if (emp != null && bu != null)
|
|
{
|
|
var newBusinessUnit = new
|
|
{
|
|
CAO = new { Id = (long)emp["Id"] }
|
|
};
|
|
var updated = await client
|
|
.For("IBusinessUnits")
|
|
.Key((long)bu["Id"])
|
|
.Set(newBusinessUnit)
|
|
.UpdateEntryAsync();
|
|
result = (long)updated["Id"];
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public async Task<long> GetIdBySid(ODataClient client)
|
|
{
|
|
long result = 0;
|
|
if (!string.IsNullOrEmpty(Sid))
|
|
{
|
|
var found = await client
|
|
.For("IBusinessUnits")
|
|
.Filter($"ExternalId eq '{this.Sid}'")
|
|
.FindEntryAsync();
|
|
if (found != null)
|
|
{
|
|
DirectumId = (long)found["Id"];
|
|
result = (long)found["Id"];
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
}
|