using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using _0_Framework.Application; using Company.Domain.RepresentativeAgg; using CompanyManagment.App.Contracts.PersonalContractingParty; using CompanyManagment.App.Contracts.Representative; namespace CompanyManagment.Application; public class RepresentativeApplication : IRepresentativeApplication { private readonly IRepresentativeRepository _representativeRepository; public bool nationalCodValid = false; public bool nationalCodValidEdit = false; public bool legalNameIsOk = true; public RepresentativeApplication(IRepresentativeRepository representativeRepository) { _representativeRepository = representativeRepository; } public OperationResult CreateReal(CreateRepresentative command) { var nationalCode = string.Empty; var op = new OperationResult(); if (_representativeRepository.Exists(x => x.LName == command.LName && x.Nationalcode == command.Nationalcode)) return op.Failed("امکان ثبت رکورد تکراری وجود ندارد"); if (_representativeRepository.Exists(x => x.LName == command.LName)) { return op.Failed("نام خانوادگی وارد شده تکراری است"); } if (_representativeRepository.Exists(x => x.Nationalcode == command.Nationalcode)) { return op.Failed("کد ملی وارد شده تکراری است"); } if (!string.IsNullOrWhiteSpace(command.Nationalcode)) { if (_representativeRepository.Exists(x => x.Nationalcode == command.Nationalcode)) { return op.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 op.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 op.Failed("کد ملی وارد شده نا معتبر است"); } } catch (Exception) { return op.Failed("لطفا کد ملی 10 رقمی وارد کنید"); } } else { nationalCode = ""; } var createReal = new Representative(command.FName, command.LName,"*", nationalCode, command.IdNumber, "*", "*", "false",command.Phone,command.AgentPhone, command.Address ); _representativeRepository.Create(createReal); _representativeRepository.SaveChanges(); return op.Succcedded(); } public OperationResult EditReal(EditRepresentative command) { var nationalCode = string.Empty; var opration = new OperationResult(); var representativeRealEdit = _representativeRepository.Get(command.Id); if (representativeRealEdit == null) return opration.Failed("رکورد مورد نظر یافت نشد"); if (_representativeRepository.Exists(x => x.LName == command.LName && x.FName == command.FName && x.id != command.Id)) return opration.Failed("امکان ثبت رکورد تکراری وجود ندارد"); if (!string.IsNullOrWhiteSpace(command.Nationalcode)) { if (_representativeRepository.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 = ""; } representativeRealEdit.EditReal(command.FName, command.LName, nationalCode, command.IdNumber, command.Phone, command.AgentPhone, command.Address); _representativeRepository.SaveChanges(); return opration.Succcedded(); } public OperationResult CreateLegal(CreateRepresentative command) { var op = new OperationResult(); if (_representativeRepository.Exists(x => x.LegalName == command.LegalName && x.RegisterId == command.RegisterId)) return op.Failed("امکان ثبت رکورد تکراری وجود ندارد"); if (_representativeRepository.Exists(x => x.LegalName == command.LegalName)) { legalNameIsOk = false; return op.Failed("نام شرکت وارد شده تکراری است"); } if (_representativeRepository.Exists(x => x.RegisterId == command.RegisterId)) { return op.Failed("شماره ثبت وارد شده تکراری است"); } if (_representativeRepository.Exists(x => x.NationalId == command.NationalId)) { return op.Failed("شناسه ملی وارد شده تکراری است"); } var createLegal = new Representative("*", "*", command.LegalName, "*","*", command.RegisterId, command.NationalId, "true", command.Phone, command.AgentPhone, command.Address); _representativeRepository.Create(createLegal); _representativeRepository.SaveChanges(); return op.Succcedded(); } public OperationResult EditLegal(EditRepresentative command) { var opration = new OperationResult(); var representativeLegalEdit = _representativeRepository.Get(command.Id); if (representativeLegalEdit == null) return opration.Failed("رکورد مورد نظر یافت نشد"); if (_representativeRepository.Exists(x => x.LName == command.LName && x.RegisterId == command.RegisterId && x.id != command.Id)) return opration.Failed("امکان ثبت رکورد تکراری وجود ندارد"); representativeLegalEdit.EditLegal(command.LegalName, command.RegisterId, command.NationalId, command.Phone, command.AgentPhone, command.Address); _representativeRepository.SaveChanges(); return opration.Succcedded(); } public List GetRepresentatives() { return _representativeRepository.GetRepresentatives(); } public EditRepresentative GetDetails(long id) { return _representativeRepository.GetDetails(id); } public List Search(RepresentativeSearchModel searchModel) { return _representativeRepository.Search(searchModel); } public List GetContractingParties(long id) { return _representativeRepository.GetContractingParties(id); } #region NewByHeydari public List GetRepresentativeListForSearchText(string searchText) { return _representativeRepository.GetRepresentativeListForSearchText(searchText); } public OperationResult DeleteRepresentative(long id) { var opration = new OperationResult(); bool result = _representativeRepository.DeleteRepresentative(id); if (result) { return opration.Succcedded(-1,"حذف با موفقیت انجام شد."); } else { return opration.Failed("حذف با خطا مواجه شد"); } } public OperationResult Active(long id) { var opration = new OperationResult(); var representative = _representativeRepository.Get(id); if (representative == null) return opration.Failed("رکورد مورد نظر یافت نشد"); representative.Active(); _representativeRepository.SaveChanges(); return opration.Succcedded(); } public OperationResult DeActive(long id) { var opration = new OperationResult(); var representative = _representativeRepository.Get(id); if (representative == null) return opration.Failed("رکورد مورد نظر یافت نشد"); representative.DeActive(); _representativeRepository.SaveChanges(); return opration.Succcedded(); } #endregion #region Api public OperationResult CreateReal(CreateRealRepresentative command) { var nationalCode = string.Empty; var op = new OperationResult(); if (_representativeRepository.Exists(x => x.LName == command.LName && x.Nationalcode == command.Nationalcode)) return op.Failed("امکان ثبت رکورد تکراری وجود ندارد"); if (_representativeRepository.Exists(x => x.LName == command.LName)) { return op.Failed("نام خانوادگی وارد شده تکراری است"); } if (_representativeRepository.Exists(x => x.Nationalcode == command.Nationalcode)) { return op.Failed("کد ملی وارد شده تکراری است"); } if (!string.IsNullOrWhiteSpace(command.Nationalcode)) { if (_representativeRepository.Exists(x => x.Nationalcode == command.Nationalcode)) { return op.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 op.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 op.Failed("کد ملی وارد شده نا معتبر است"); } } catch (Exception) { return op.Failed("لطفا کد ملی 10 رقمی وارد کنید"); } } else { nationalCode = ""; } var createReal = new Representative(command.FName, command.LName, "*", nationalCode, command.IdNumber, "*", "*", "false", command.Phone, command.AgentPhone, command.Address ); _representativeRepository.Create(createReal); _representativeRepository.SaveChanges(); return op.Succcedded(); } public OperationResult CreateLegal(CreateLegalRepresentative command) { var op = new OperationResult(); if (_representativeRepository.Exists(x => x.LegalName == command.LegalName && x.RegisterId == command.RegisterId)) return op.Failed("امکان ثبت رکورد تکراری وجود ندارد"); if (_representativeRepository.Exists(x => x.LegalName == command.LegalName)) { legalNameIsOk = false; return op.Failed("نام شرکت وارد شده تکراری است"); } if (_representativeRepository.Exists(x => x.RegisterId == command.RegisterId)) { return op.Failed("شماره ثبت وارد شده تکراری است"); } if (_representativeRepository.Exists(x => x.NationalId == command.NationalId)) { return op.Failed("شناسه ملی وارد شده تکراری است"); } var createLegal = new Representative("*", "*", command.LegalName, "*", "*", command.RegisterId, command.NationalId, "true", command.Phone, command.AgentPhone, command.Address); _representativeRepository.Create(createLegal); _representativeRepository.SaveChanges(); return op.Succcedded(); } public OperationResult EditReal(EditRealRepresentative command) { var nationalCode = string.Empty; var opration = new OperationResult(); var representativeRealEdit = _representativeRepository.Get(command.Id); if (representativeRealEdit == null) return opration.Failed("رکورد مورد نظر یافت نشد"); if (_representativeRepository.Exists(x => x.LName == command.LName && x.FName == command.FName && x.id != command.Id)) return opration.Failed("امکان ثبت رکورد تکراری وجود ندارد"); if (!string.IsNullOrWhiteSpace(command.Nationalcode)) { if (_representativeRepository.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 = ""; } representativeRealEdit.EditReal(command.FName, command.LName, nationalCode, command.IdNumber, command.Phone, command.AgentPhone, command.Address); _representativeRepository.SaveChanges(); return opration.Succcedded(); } public OperationResult EditLegal(EditLegalRepresentative command) { var opration = new OperationResult(); var representativeLegalEdit = _representativeRepository.Get(command.Id); if (representativeLegalEdit == null) return opration.Failed("رکورد مورد نظر یافت نشد"); if (_representativeRepository.Exists(x => x.LegalName == command.LegalName && x.RegisterId == command.RegisterId && x.id != command.Id)) return opration.Failed("امکان ثبت رکورد تکراری وجود ندارد"); representativeLegalEdit.EditLegal(command.LegalName, command.RegisterId, command.NationalId, command.Phone, command.AgentPhone, command.Address); _representativeRepository.SaveChanges(); return opration.Succcedded(); } public async Task> GetSelectList() { return await _representativeRepository.GetSelectList(); } public async Task> GetList(RepresentativeGetListSearchModel searchModel) { return (await _representativeRepository.GetList(searchModel)).ToList(); } public bool HasAnyContractingParty(long id) { return _representativeRepository.HasAnyContractingParty(id); } #endregion }