From 16c1ae04a9721998a67975e72eaa17e508094264 Mon Sep 17 00:00:00 2001 From: mahan Date: Tue, 30 Dec 2025 12:44:55 +0330 Subject: [PATCH 1/2] remove some injections --- .../InstitutionContractApplication.cs | 13 +++---------- .../Controllers/institutionContractController.cs | 3 ++- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/CompanyManagment.Application/InstitutionContractApplication.cs b/CompanyManagment.Application/InstitutionContractApplication.cs index 82373502..fdd2e6a7 100644 --- a/CompanyManagment.Application/InstitutionContractApplication.cs +++ b/CompanyManagment.Application/InstitutionContractApplication.cs @@ -49,28 +49,23 @@ public class InstitutionContractApplication : IInstitutionContractApplication private readonly IFinancialStatmentApplication _financialStatmentApplication; private readonly IEmployerRepository _employerRepository; private readonly IWorkshopRepository _workshopRepository; - private readonly ILeftWorkRepository _leftWorkRepository; private readonly IWorkshopApplication _workshopApplication; - private readonly IContractingPartyTempRepository _contractingPartyTempRepository; private readonly IFinancialStatmentRepository _financialStatmentRepository; private readonly IContactInfoApplication _contactInfoApplication; private readonly IAccountApplication _accountApplication; private readonly ISmsService _smsService; - private readonly IUidService _uidService; private readonly IFinancialInvoiceRepository _financialInvoiceRepository; private readonly IPaymentGateway _paymentGateway; private readonly IPaymentTransactionRepository _paymentTransactionRepository; private readonly IRollCallServiceRepository _rollCallServiceRepository; - public InstitutionContractApplication(IInstitutionContractRepository institutionContractRepository, IPersonalContractingPartyRepository contractingPartyRepository, IRepresentativeRepository representativeRepository, IEmployerRepository employerRepository, - IWorkshopRepository workshopRepository, ILeftWorkRepository leftWorkRepository, + IWorkshopRepository workshopRepository, IFinancialStatmentApplication financialStatmentApplication, IWorkshopApplication workshopApplication, - IContractingPartyTempRepository contractingPartyTempRepository, IFinancialStatmentRepository financialStatmentRepository, IContactInfoApplication contactInfoApplication, - IAccountApplication accountApplication, ISmsService smsService, IUidService uidService, + IAccountApplication accountApplication, ISmsService smsService, IFinancialInvoiceRepository financialInvoiceRepository, IHttpClientFactory httpClientFactory, IPaymentTransactionRepository paymentTransactionRepository, IRollCallServiceRepository rollCallServiceRepository) { @@ -79,15 +74,12 @@ public class InstitutionContractApplication : IInstitutionContractApplication _representativeRepository = representativeRepository; _employerRepository = employerRepository; _workshopRepository = workshopRepository; - _leftWorkRepository = leftWorkRepository; _financialStatmentApplication = financialStatmentApplication; _workshopApplication = workshopApplication; - _contractingPartyTempRepository = contractingPartyTempRepository; _financialStatmentRepository = financialStatmentRepository; _contactInfoApplication = contactInfoApplication; _accountApplication = accountApplication; _smsService = smsService; - _uidService = uidService; _financialInvoiceRepository = financialInvoiceRepository; _paymentTransactionRepository = paymentTransactionRepository; _rollCallServiceRepository = rollCallServiceRepository; @@ -831,6 +823,7 @@ public class InstitutionContractApplication : IInstitutionContractApplication { var res = _employerRepository.DeActiveAll(employer.Id); } + } return opration.Succcedded(); diff --git a/ServiceHost/Areas/Admin/Controllers/institutionContractController.cs b/ServiceHost/Areas/Admin/Controllers/institutionContractController.cs index bbcf6af0..06bfab75 100644 --- a/ServiceHost/Areas/Admin/Controllers/institutionContractController.cs +++ b/ServiceHost/Areas/Admin/Controllers/institutionContractController.cs @@ -235,7 +235,8 @@ public class institutionContractController : AdminBaseController if (balance == "0") { result = _institutionContractApplication.DeActive(id); - if (result.IsSuccedded) result = _institutionContractApplication.DeActiveAllConnections(id); + if (result.IsSuccedded) + result = _institutionContractApplication.DeActiveAllConnections(id); } else { From eb9a3e52fe5906e9c82dc783cc3efdacf5ebc3a2 Mon Sep 17 00:00:00 2001 From: mahan Date: Tue, 30 Dec 2025 13:50:09 +0330 Subject: [PATCH 2/2] feat: implement async methods for activating and deactivating personal contracting parties --- .../Account/IAccountApplication.cs | 1 - .../AccountApplication.cs | 1 + .../IPersonalContractingPartyRepository.cs | 10 +- .../InstitutionContractApplication.cs | 47 ++----- .../PersonalContractingPartyRepository.cs | 131 ++++++++++++++++++ 5 files changed, 150 insertions(+), 40 deletions(-) diff --git a/AccountManagement.Application.Contracts/Account/IAccountApplication.cs b/AccountManagement.Application.Contracts/Account/IAccountApplication.cs index b1da0153..4d82946d 100644 --- a/AccountManagement.Application.Contracts/Account/IAccountApplication.cs +++ b/AccountManagement.Application.Contracts/Account/IAccountApplication.cs @@ -75,7 +75,6 @@ public interface IAccountApplication void CameraLogin(CameraLoginRequest request); Task GetPmUserAsync(long accountId); - } public class CameraLoginRequest diff --git a/AccountManagement.Application/AccountApplication.cs b/AccountManagement.Application/AccountApplication.cs index ff2fd102..64e05681 100644 --- a/AccountManagement.Application/AccountApplication.cs +++ b/AccountManagement.Application/AccountApplication.cs @@ -1024,4 +1024,5 @@ public class AccountApplication : IAccountApplication { return await _pmUserQueryService.GetPmUserDataByAccountId(accountId); } + } \ No newline at end of file diff --git a/Company.Domain/ContarctingPartyAgg/IPersonalContractingPartyRepository.cs b/Company.Domain/ContarctingPartyAgg/IPersonalContractingPartyRepository.cs index 068625ad..25178955 100644 --- a/Company.Domain/ContarctingPartyAgg/IPersonalContractingPartyRepository.cs +++ b/Company.Domain/ContarctingPartyAgg/IPersonalContractingPartyRepository.cs @@ -1,4 +1,5 @@ -using CompanyManagment.App.Contracts.PersonalContractingParty; +using System; +using CompanyManagment.App.Contracts.PersonalContractingParty; using System.Collections.Generic; using _0_Framework.Application; using _0_Framework.Domain; @@ -32,7 +33,9 @@ public interface IPersonalContractingPartyRepository :IRepository SearchForMain(PersonalContractingPartySearchModel searchModel2); OperationResult DeletePersonalContractingParties(long id); bool GetHasContract(long id); + [Obsolete("از متدهای async استفاده کنید")] OperationResult DeActiveAll(long id); + [Obsolete("از متدهای async استفاده کنید")] OperationResult ActiveAll(long id); #endregion @@ -76,4 +79,9 @@ public interface IPersonalContractingPartyRepository :IRepository GetByNationalCode(string nationalCode); Task GetByNationalId(string registerId); + + Task DeActiveAllAsync(long id); + Task ActiveAllAsync(long id); + + } \ No newline at end of file diff --git a/CompanyManagment.Application/InstitutionContractApplication.cs b/CompanyManagment.Application/InstitutionContractApplication.cs index fdd2e6a7..0eed6616 100644 --- a/CompanyManagment.Application/InstitutionContractApplication.cs +++ b/CompanyManagment.Application/InstitutionContractApplication.cs @@ -1,40 +1,30 @@ using System; using System.Collections.Generic; -using System.Diagnostics; using System.Linq; using System.Net.Http; -using System.Threading; using System.Threading.Tasks; using _0_Framework.Application; using _0_Framework.Application.Enums; using _0_Framework.Application.PaymentGateway; using _0_Framework.Application.Sms; -using _0_Framework.Application.UID; using _0_Framework.Exceptions; using AccountManagement.Application.Contracts.Account; using Company.Domain.ContarctingPartyAgg; -using Company.Domain.EmployeeAgg; using Company.Domain.empolyerAgg; using Company.Domain.FinancialInvoiceAgg; using Company.Domain.FinancialStatmentAgg; using Company.Domain.FinancialTransactionAgg; using Company.Domain.InstitutionContractAgg; -using Company.Domain.LeftWorkAgg; using Company.Domain.PaymentTransactionAgg; using Company.Domain.RepresentativeAgg; using Company.Domain.RollCallServiceAgg; -using Company.Domain.TemporaryClientRegistrationAgg; using Company.Domain.WorkshopAgg; using CompanyManagment.App.Contracts.FinancialInvoice; using CompanyManagment.App.Contracts.FinancialStatment; using CompanyManagment.App.Contracts.InstitutionContract; using CompanyManagment.App.Contracts.InstitutionContractContactinfo; using CompanyManagment.App.Contracts.PaymentTransaction; -using CompanyManagment.App.Contracts.PersonalContractingParty; using CompanyManagment.App.Contracts.Workshop; -using CompanyManagment.EFCore.Migrations; -using Microsoft.AspNetCore.Mvc; -using OfficeOpenXml.Packaging.Ionic.Zip; using PersianTools.Core; using ConnectedPersonnelViewModel = CompanyManagment.App.Contracts.Workshop.ConnectedPersonnelViewModel; using FinancialStatment = Company.Domain.FinancialStatmentAgg.FinancialStatment; @@ -59,6 +49,7 @@ public class InstitutionContractApplication : IInstitutionContractApplication private readonly IPaymentTransactionRepository _paymentTransactionRepository; private readonly IRollCallServiceRepository _rollCallServiceRepository; + public InstitutionContractApplication(IInstitutionContractRepository institutionContractRepository, IPersonalContractingPartyRepository contractingPartyRepository, IRepresentativeRepository representativeRepository, IEmployerRepository employerRepository, @@ -809,21 +800,11 @@ public class InstitutionContractApplication : IInstitutionContractApplication var contractingParty = _contractingPartyRepository.Get(institutionContract.ContractingPartyId); if (contractingParty != null) { - contractingParty.DeActive(); - _contractingPartyRepository.SaveChanges(); - var employers = - _employerRepository.GetEmployerByContracrtingPartyID(institutionContract.ContractingPartyId); - //var employersIdList = employers.Select(x => x.Id).ToList(); - //var workshops = _workshopApplication.GetWorkshopsByEmployerId(employersIdList); - //foreach (var workshop in workshops) - //{ - // var res = _workshopApplication.DeActive(workshop.Id); - //} - foreach (var employer in employers) - { - var res = _employerRepository.DeActiveAll(employer.Id); - } + var accountsDeActiveRes = _contractingPartyRepository.DeActiveAllAsync(contractingParty.id) + .GetAwaiter().GetResult(); + if (!accountsDeActiveRes.IsSuccedded) + return opration.Failed(accountsDeActiveRes.Message); } return opration.Succcedded(); @@ -840,20 +821,10 @@ public class InstitutionContractApplication : IInstitutionContractApplication var contractingParty = _contractingPartyRepository.Get(institutionContract.ContractingPartyId); if (contractingParty != null) { - contractingParty.Active(); - _contractingPartyRepository.SaveChanges(); - var employers = - _employerRepository.GetEmployerByContracrtingPartyID(institutionContract.ContractingPartyId); - //var employersIdList = employers.Select(x => x.Id).ToList(); - //var workshops = _workshopApplication.GetWorkshopsByEmployerId(employersIdList); - //foreach (var workshop in workshops) - //{ - // var res = _workshopApplication.DeActive(workshop.Id); - //} - foreach (var employer in employers) - { - var res = _employerRepository.ActiveAll(employer.Id); - } + var activeRes = _contractingPartyRepository.ActiveAllAsync(contractingParty.id).GetAwaiter() + .GetResult(); + if (!activeRes.IsSuccedded) + return opration.Failed(activeRes.Message); } return opration.Succcedded(); diff --git a/CompanyManagment.EFCore/Repository/PersonalContractingPartyRepository.cs b/CompanyManagment.EFCore/Repository/PersonalContractingPartyRepository.cs index ba888dc6..e0225194 100644 --- a/CompanyManagment.EFCore/Repository/PersonalContractingPartyRepository.cs +++ b/CompanyManagment.EFCore/Repository/PersonalContractingPartyRepository.cs @@ -773,6 +773,137 @@ public class PersonalContractingPartyRepository : RepositoryBase x.NationalId == nationalId); } + public async Task DeActiveAllAsync(long id) + { + OperationResult result = new OperationResult(); + await using var transaction = await _context.Database.BeginTransactionAsync(); + await using var accountTransaction = await _accountContext.Database.BeginTransactionAsync(); + try + { + var contractingParty = _context.PersonalContractingParties + .FirstOrDefault(x => x.id == id); + + if (contractingParty == null) + return result.Failed("طرف حساب یافت نشد"); + + contractingParty.DeActive(); + + var employers = _context.Employers + .Where(x => x.ContractingPartyId == id).ToList(); + employers.ForEach(x => x.DeActive()); + + var employerIds = employers.Select(x => x.id).ToList(); + var workshopIds = _context.WorkshopEmployers + .Where(x => employerIds.Contains(x.EmployerId)) + .Select(x => x.WorkshopId).ToList(); + + var workshops = _context.Workshops + .Where(x => workshopIds.Contains(x.id)).ToList(); + workshops.ForEach(x => x.DeActive(x.ArchiveCode)); + + var contracts = _context.Contracts + .Where(x => workshopIds.Contains(x.WorkshopIds)).ToList(); + contracts.ForEach(x => x.DeActive()); + + var contractIds = contracts.Select(x => x.id).ToList(); + var checkouts = _context.CheckoutSet + .Where(x => contractIds.Contains(x.ContractId)).ToList(); + checkouts.ForEach(x => x.DeActive()); + + var contractingPartyAccount =await _context.ContractingPartyAccounts + .FirstOrDefaultAsync(x => x.PersonalContractingPartyId == id); + if (contractingPartyAccount != null) + { + var account = await _accountContext.Accounts + .FirstOrDefaultAsync(x => x.id == contractingPartyAccount.AccountId); + + account?.DeActive(); + + var cameraAccount =await _accountContext.CameraAccounts + .FirstOrDefaultAsync(x=>x.AccountId==account.id); + + cameraAccount?.DeActive(); + + await _accountContext.SaveChangesAsync(); + } + + await _context.SaveChangesAsync(); + await transaction.CommitAsync(); + await accountTransaction.CommitAsync(); + result.Succcedded(); + } + catch (Exception) + { + result.Failed("غیرفعال کردن طرف حساب با خطا مواجه شد"); + await transaction.RollbackAsync(); + await accountTransaction.RollbackAsync(); + } + + return result; + } + + public async Task ActiveAllAsync(long id) + { + OperationResult result = new OperationResult(); + await using var transaction =await _context.Database.BeginTransactionAsync(); + await using var accountTransaction = await _accountContext.Database.BeginTransactionAsync(); + try + { + var personel = _context.PersonalContractingParties + .FirstOrDefault(x => x.id == id); + if (personel == null) + return result.Failed("طرف حساب یافت نشد"); + + personel.Active(); + + var employers = _context.Employers.Where(x => x.ContractingPartyId == id).ToList(); + employers.ForEach(x => x.Active()); + + var employerIds = employers.Select(x => x.id).ToList(); + var workshopIds = _context.WorkshopEmployers.Where(x => employerIds.Contains(x.EmployerId)) + .Select(x => x.WorkshopId).ToList(); + var workshops = _context.Workshops.Where(x => workshopIds.Contains(x.id)).ToList(); + workshops.ForEach(x => x.Active(x.ArchiveCode)); + + var contracts = _context.Contracts.Where(x => workshopIds.Contains(x.WorkshopIds)).ToList(); + contracts.ForEach(x => x.Active()); + + var contractIds = contracts.Select(x => x.id).ToList(); + var checkouts = _context.CheckoutSet.Where(x => contractIds.Contains(x.ContractId)).ToList(); + checkouts.ForEach(x => x.Active()); + + var contractingPartyAccount =await _context.ContractingPartyAccounts + .FirstOrDefaultAsync(x => x.PersonalContractingPartyId == id); + if (contractingPartyAccount != null) + { + var account = await _accountContext.Accounts + .FirstOrDefaultAsync(x => x.id == contractingPartyAccount.AccountId); + + account?.Active(); + + var cameraAccount =await _accountContext.CameraAccounts + .FirstOrDefaultAsync(x=>x.AccountId==account.id); + + cameraAccount?.Active(); + + await _accountContext.SaveChangesAsync(); + } + + await _context.SaveChangesAsync(); + await transaction.CommitAsync(); + await accountTransaction.CommitAsync(); + result.Succcedded(); + } + catch (Exception) + { + result.Failed("فعال کردن طرف حساب با خطا مواجه شد"); + await transaction.RollbackAsync(); + await accountTransaction.RollbackAsync(); + } + + return result; + } + #endregion }