HRM_RabbitMQ_Consumer/JSONParser/JSONStructure.cs

224 lines
8.7 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using JSONParser.Structure;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace JSONParser
{
public class JSONStructure
{
//private JArray _array;
private List<JToken> _orgs;
private List<JToken> _depts;
private List<JToken> _jobs;
private List<JToken> _emps;
public List<BusinessUnit> BusinessUnits { get; set; }
public List<Department> Departments { get; set; }
public List<JobTitle> JobTitles { get; set; }
public List<Employee> Employees { get; set; }
public List<Person> Persons { get; set; }
public List<Login> Logins { get; set; }
public List<string> NotFoundDepartments { get; set; } = new List<string>();
public List<string> NotFoundEmployees { get; set; } = new List<string>();
public JSONStructure(JArray array)
{
//var filePath = @"C:\Users\anboevdd\Desktop\ОШС_2024-10-30.json";
//var json = File.ReadAllText(filePath);
//var array = JArray.Parse(json);
Persons = array.Where(x => (string)x["Type"] == "ФизическиеЛица").Select(x => new Person
{
Type = (string)x["Type"],
Sid = (string)x["Sid"],
FirstName = (string)x["FirstName"],
LastName = (string)x["LastName"],
MiddleName = (string)x["MiddleName"]
}).ToList();
Logins = array.Where(x => (string)x["Type"] == "УчетныеЗаписи").Select(x => new Login
{
Type = (string)x["Type"],
Sid = (string)x["Sid"],
//LoginName = (string)x["LoginName"] == null ? null : $"POLY\\{(string)x["LoginName"]}",
LoginName = (string)x["LoginName"] == null ? null : $"{(string)x["LoginName"]}",
TypeAuthentication = (string)x["TypeAuthentication"],
Status = (bool)x["Status"]
}).ToList();
_orgs = array.Where(x => (string)x["Type"] == "Организации").ToList();
_depts = array.Where(x => (string)x["Type"] == "ПодразделенияОрганизаций").ToList();
_jobs = array.Where(x => (string)x["Type"] == "Должности").ToList();
_emps = array.Where(x => (string)x["Type"] == "Сотрудники").ToList();
BusinessUnits = GetBusinessUnits();
Departments = GetDepartments();
JobTitles = GetJobTitles();
Employees = GetEmployees();
BusinessUnits.ForEach(x => x.NCEO = CreateEmployee(x.NCEO.Sid));
BusinessUnits.ForEach(x => x.Account = CreateEmployee(x.Account.Sid));
}
private List<Employee> GetEmployees()
{
var result = new List<Employee>();
foreach (var item in _emps)
{
result.Add(CreateEmployee((string)item["Sid"]));
}
return result;
}
private List<JobTitle> GetJobTitles()
{
var result = new List<JobTitle>();
foreach (var item in _jobs)
{
result.Add(CreateJobTitle((string)item["Sid"]));
}
return result;
}
private List<Department> GetDepartments()
{
var result = new List<Department>();
foreach (var item in _depts)
{
result.Add(CreateDepartment((string)item["Sid"]));
}
return result;
}
private Department CreateDepartment(string sid)
{
if (Guid.Parse(sid) == Guid.Empty) return null;
var department = new Department();
var dept = _depts.FirstOrDefault(x => (string)x["Sid"] == sid);
if (dept != null)
{
department.Type = (string)dept["Type"];
department.Sid = (string)dept["Sid"];
department.Name = (string)dept["Name"];
department.ShortName = (string)dept["ShortName"];
//department.BusinessUnit = BusinessUnits.FirstOrDefault(x => x.Sid == (string)dept["BusinessUnit"]);
department.BusinessUnit = CreateBusinessUnit((string)dept["BusinessUnit"]);
if (string.IsNullOrEmpty((string)dept["HeadOffice"]))
{
department.HeadOffice = null;
}
else
{
department.HeadOffice = CreateDepartment((string)dept["HeadOffice"]);
}
department.Status = (string)dept["Status"];
}
else
{
department.Sid = sid;
NotFoundDepartments.Add(sid);
//Console.WriteLine($"Department {sid}");
}
return department;
}
private List<BusinessUnit> GetBusinessUnits()
{
var result = new List<BusinessUnit>();
foreach (var item in _orgs)
{
result.Add(CreateBusinessUnit((string)item["Sid"]));
}
return result;
}
private BusinessUnit CreateBusinessUnit(string sid)
{
var org = _orgs.FirstOrDefault(x => (string)x["Sid"] == sid);
var bu = new BusinessUnit();
if (org != null)
{
bu.Type = (string)org["Type"];
bu.Sid = (string)org["Sid"];
bu.Name = (string)org["Name"];
bu.LegalName = (string)org["LegalName"];
bu.NCEO = new Employee { Sid = (string)org["NCEO"] };
bu.Account = new Employee { Sid = (string)org["Account"] };
if ((string)org["Sid"] == (string)org["HeadCompany"] || Guid.Parse((string)org["Sid"]) == Guid.Empty)
{
bu.HeadCompany = null;
}
else
{
bu.HeadCompany = CreateBusinessUnit((string)org["HeadCompany"]);
}
bu.BINArmadoc = (string)org["BINArmadoc"];
bu.LegalAddress = (string)org["LegalAddress"];
bu.PostalAddress = (string)org["PostalAddress"];
bu.Phones = (string)org["Phones"];
}
else
{
bu.Sid = sid;
//Console.WriteLine($"BusinessUnit - {sid}");
}
return bu;
}
private Employee CreateEmployee(string? sid)
{
if (Guid.Parse(sid) == Guid.Empty) return null;
var employee = new Employee();
var emp = _emps.FirstOrDefault(x => (string)x["Sid"] == sid);
if (emp != null)
{
employee.Type = (string)emp["Type"];
employee.Sid = (string)emp["Sid"];
employee.Person = Persons.FirstOrDefault(x => x.Sid == (string)emp["Person"]);
employee.Login = Logins.FirstOrDefault(x => x.Sid == (string)emp["Login"]);
employee.Email = (string)emp["Email"];
employee.Status = (bool)emp["Status"];
//employee.Department = Departments.FirstOrDefault(x => x.Sid == (string)emp["Department"]);
employee.Department = CreateDepartment((string)emp["Department"]);
employee.JobTitleName = (string)emp["JobTitleName"];
employee.JobTitle = new JobTitle
{
Type = "Должности",
Sid = Guid.NewGuid().ToString(),
Name = employee.JobTitleName,
Department = employee.Department
};
//employee.JobTitle = CreateJobTitle((string)emp["JobTitle"]);
}
else
{
employee.Sid = sid;
NotFoundEmployees.Add(sid);
//Console.WriteLine($"Employee - {sid}");
}
return employee;
}
private JobTitle CreateJobTitle(string sid)
{
var jt = _jobs.FirstOrDefault(x => (string)x["Sid"] == sid);
var jobTitle = new JobTitle();
if (jt != null)
{
jobTitle.Type = (string)jt["Type"];
jobTitle.Sid = (string)jt["Sid"];
jobTitle.Name = (string)jt["Name"];
jobTitle.Department = CreateDepartment((string)jt["Department"]);
}
else
{
jobTitle.Sid = sid;
//Console.WriteLine($"JobTitle - {sid}");
}
return jobTitle;
}
}
}