Files
Backend-Api/CompanyManagment.Application/FileEmployeeApplication.cs
2024-07-05 21:36:15 +03:30

273 lines
9.5 KiB
C#

using System;
using System.Collections.Generic;
using _0_Framework.Application;
using Company.Domain.FileEmployeeAgg;
using CompanyManagment.App.Contracts.FileEmployee;
using CompanyManagment.App.Contracts.Representative;
namespace CompanyManagment.Application;
public class FileEmployeeApplication : IFileEmployeeApplication
{
private readonly IFileEmployeeRepository _fileEmployeeRepository;
private readonly IRepresentativeApplication _representativeApplication;
public FileEmployeeApplication(IFileEmployeeRepository fileEmployeeRepository, IRepresentativeApplication representativeApplication)
{
_fileEmployeeRepository = fileEmployeeRepository;
_representativeApplication = representativeApplication;
}
public OperationResult Create(CreateFileEmployee command)
{
var nationalCode = string.Empty;
var opration = new OperationResult();
if (_fileEmployeeRepository.Exists(x =>
x.LName == command.LName && x.FName == command.FName))
return opration.Failed("امکان ثبت رکورد تکراری وجود ندارد");
if (!string.IsNullOrWhiteSpace(command.NationalCode))
{
if (_fileEmployeeRepository.Exists(x => x.NationalCode == command.NationalCode))
{
return opration.Failed("کد ملی وارد شده تکراری است");
}
try
{
char[] chArray = command.NationalCode.ToCharArray();
int[] numArray = new int[chArray.Length];
var cunt = chArray.Length;
for (int i = 0; i < chArray.Length; i++)
{
numArray[i] = (int)char.GetNumericValue(chArray[i]);
}
int num2 = numArray[9];
switch (command.NationalCode)
{
case "0000000000":
case "1111111111":
case "22222222222":
case "33333333333":
case "4444444444":
case "5555555555":
case "6666666666":
case "7777777777":
case "8888888888":
case "9999999999":
return opration.Failed("کد ملی وارد شده صحیح نمی باشد");
}
int num3 =
((((((((numArray[0] * 10) + (numArray[1] * 9)) + (numArray[2] * 8)) + (numArray[3] * 7)) +
(numArray[4] * 6)) + (numArray[5] * 5)) + (numArray[6] * 4)) + (numArray[7] * 3)) +
(numArray[8] * 2);
int num4 = num3 - ((num3 / 11) * 11);
if ((((num4 == 0) && (num2 == num4)) || ((num4 == 1) && (num2 == 1))) ||
((num4 > 1) && (num2 == Math.Abs((int)(num4 - 11)))) && cunt <= 10)
{
nationalCode = command.NationalCode;
}
else
{
return opration.Failed("کد ملی وارد شده نا معتبر است");
}
}
catch (Exception)
{
return opration.Failed("لطفا کد ملی 10 رقمی وارد کنید");
}
}
else
{
nationalCode = "";
}
if (command.RepresentativeId < 1)
return opration.Failed("لطفا معرف را انتخاب کنید");
var representative = _representativeApplication.GetDetails(command.RepresentativeId);
string initial = "1300/10/11";
var dateOfBirth = command.DateOfBirth != null ? command.DateOfBirth.ToGeorgianDateTime() : initial.ToGeorgianDateTime();
var employeeData = new FileEmployee(command.FName, command.LName,command.RepresentativeId,representative.FullName,command.FatherName,
dateOfBirth,nationalCode,command.IdNumber,command.Gender,command.MaritalStatus,command.LevelOfEducation,command.FieldOfStudy,
command.InsuranceCode,command.OfficePhone,command.OfficePhone,command.MclsUserName,command.MclsPassword,command.EserviceUserName,command.EservicePassword,
command.TaxOfficeUserName,command.TaxOfficepassword,command.SanaUserName,command.SanaPassword);
_fileEmployeeRepository.Create(employeeData);
_fileEmployeeRepository.SaveChanges();
return opration.Succcedded();
}
public OperationResult Edit(EditFileEmployee command)
{
var nationalCode = string.Empty;
var opration = new OperationResult();
var employee = _fileEmployeeRepository.Get(command.Id);
if (employee == null)
return opration.Failed("رکورد مورد نظر یافت نشد");
if (_fileEmployeeRepository.Exists(x =>
x.LName == command.LName && x.FName == command.FName && x.id != command.Id))
return opration.Failed("امکان ثبت رکورد تکراری وجود ندارد");
if (!string.IsNullOrWhiteSpace(command.NationalCode))
{
if (_fileEmployeeRepository.Exists(x => x.NationalCode == command.NationalCode && x.id != command.Id))
{
return opration.Failed("کد ملی وارد شده تکراری است");
}
try
{
char[] chArray = command.NationalCode.ToCharArray();
int[] numArray = new int[chArray.Length];
var cunt = chArray.Length;
for (int i = 0; i < chArray.Length; i++)
{
numArray[i] = (int)char.GetNumericValue(chArray[i]);
}
int num2 = numArray[9];
switch (command.NationalCode)
{
case "0000000000":
case "1111111111":
case "22222222222":
case "33333333333":
case "4444444444":
case "5555555555":
case "6666666666":
case "7777777777":
case "8888888888":
case "9999999999":
return opration.Failed("کد ملی وارد شده صحیح نمی باشد");
}
int num3 =
((((((((numArray[0] * 10) + (numArray[1] * 9)) + (numArray[2] * 8)) + (numArray[3] * 7)) +
(numArray[4] * 6)) + (numArray[5] * 5)) + (numArray[6] * 4)) + (numArray[7] * 3)) +
(numArray[8] * 2);
int num4 = num3 - ((num3 / 11) * 11);
if ((((num4 == 0) && (num2 == num4)) || ((num4 == 1) && (num2 == 1))) ||
((num4 > 1) && (num2 == Math.Abs((int)(num4 - 11)))) && cunt <= 10)
{
nationalCode = command.NationalCode;
}
else
{
return opration.Failed("کد ملی وارد شده نا معتبر است");
}
}
catch (Exception)
{
return opration.Failed("لطفا کد ملی 10 رقمی وارد کنید");
}
}
else
{
nationalCode = "";
}
if (command.RepresentativeId < 1)
return opration.Failed("لطفا معرف را انتخاب کنید");
var representative = _representativeApplication.GetDetails(command.RepresentativeId);
string initial = "1300/10/11";
var dateOfBirth = command.DateOfBirth != null ? command.DateOfBirth.ToGeorgianDateTime() : initial.ToGeorgianDateTime();
employee.Edit(command.FName, command.LName, command.RepresentativeId, representative.FullName, command.FatherName,
dateOfBirth, nationalCode, command.IdNumber, command.Gender, command.MaritalStatus, command.LevelOfEducation, command.FieldOfStudy,
command.InsuranceCode, command.OfficePhone, command.OfficePhone, command.MclsUserName, command.MclsPassword, command.EserviceUserName, command.EservicePassword,
command.TaxOfficeUserName, command.TaxOfficepassword, command.SanaUserName, command.SanaPassword);
_fileEmployeeRepository.SaveChanges();
return opration.Succcedded();
}
public EditFileEmployee GetDetails(long id)
{
return _fileEmployeeRepository.GetDetails(id);
}
public OperationResult Active(long id)
{
var opration = new OperationResult();
var employer = _fileEmployeeRepository.Get(id);
if (employer == null)
return opration.Failed("رکورد مورد نظر یافت نشد");
employer.Active();
_fileEmployeeRepository.SaveChanges();
return opration.Succcedded();
}
public OperationResult DeActive(long id)
{
var opration = new OperationResult();
var employer = _fileEmployeeRepository.Get(id);
if (employer == null)
return opration.Failed("رکورد مورد نظر یافت نشد");
employer.DeActive();
_fileEmployeeRepository.SaveChanges();
return opration.Succcedded();
}
public List<FileEmployeeViewModel> GetFileEmployee()
{
return _fileEmployeeRepository.GetFileEmployee();
}
public List<FileEmployeeViewModel> Search(FileEmployeeSearchModel searchModel)
{
return _fileEmployeeRepository.Search(searchModel);
}
}