using _0_Framework.Application; using Company.Domain.EmployeeBankInformationAgg; using CompanyManagment.App.Contracts.EmployeeBankInformation; using System.Collections.Generic; using System.Linq; namespace CompanyManagment.Application { public class EmployeeBankInformationApplication : IEmployeeBankInformationApplication { private readonly IEmployeeBankInformationRepository _employeeBankInformationRepository; public EmployeeBankInformationApplication(IEmployeeBankInformationRepository employeeBankInformationRepository) { _employeeBankInformationRepository = employeeBankInformationRepository; } public OperationResult Create(CreateEmployeeInformation command) { var workshopEmployeeBankInfoList = _employeeBankInformationRepository.GetAllByWorkshopId(command.WorkshopId); OperationResult op = ValidateCreateOperation(workshopEmployeeBankInfoList,command); if (op.IsSuccedded == false) return op; var entity = new EmployeeBankInformation(command.EmployeeId, command.WorkshopId, command.BankId, command.BankAccountNumber, command.CardNumber, command.ShebaNumber); if (HasNoRecordInWorkshop(command.WorkshopId, command.EmployeeId, workshopEmployeeBankInfoList)) entity.SetDefault(); _employeeBankInformationRepository.Create(entity); _employeeBankInformationRepository.SaveChanges(); return op.Succcedded(entity.id); } public OperationResult GroupCreate(long workshopId,List command) { var workshopEmployeeBankInfoList = _employeeBankInformationRepository.GetAllByWorkshopId(workshopId); OperationResult op = new(); //Validations foreach (var newEBI in command) { op = ValidateCreateOperation(workshopEmployeeBankInfoList, newEBI); if (op.IsSuccedded == false) return op; } //ShapeShifting var entities = command.Select(x=> new EmployeeBankInformation(x.EmployeeId, workshopId, x.BankId, x.BankAccountNumber, x.CardNumber, x.ShebaNumber)).ToList(); entities.ForEach(newEBI => { if (HasNoRecordInWorkshop(workshopId, newEBI.EmployeeId, workshopEmployeeBankInfoList)) newEBI.SetDefault(); }); _employeeBankInformationRepository.AddRange(entities); _employeeBankInformationRepository.SaveChanges(); return op.Succcedded(); } public OperationResult SetDefault(long workshopId, long bankInfoId) { OperationResult op = new(); var entity = _employeeBankInformationRepository.Get(bankInfoId); if (entity == null) return op.Failed("خطای سیستمی"); var employeeRecordsInWorkshop = _employeeBankInformationRepository.GetRangeByEmployeeId(workshopId, entity.EmployeeId); if(employeeRecordsInWorkshop.Count > 1) employeeRecordsInWorkshop.ForEach(x => { if (x.id != entity.id) x.SetDefault(false); else x.SetDefault(); }); _employeeBankInformationRepository.SaveChanges(); return op.Succcedded(); } //todo: add CardNumber, BankAccountNumber, etc validations public OperationResult Edit(EditEmployeeInformation command) { OperationResult op = new(); var entity = _employeeBankInformationRepository.Get(command.Id); if (entity == null) return op.Failed(ApplicationMessages.RecordNotFound); if (HasAtLeastOneFormFilled(command) == false) return op.Failed("لطفا حداقل یکی از مشخصات بانکی را کامل کنید"); //if (_employeeBankInformationRepository.Exists(x => // x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && // x.BankId == command.BankId && x.id!= command.Id)) // return op.Failed("این کاربر در بانک انتخاب شده حساب دارد"); if (!string.IsNullOrWhiteSpace(command.BankAccountNumber) && _employeeBankInformationRepository.Exists(x => x.WorkshopId == command.WorkshopId && x.BankAccountNumber == command.BankAccountNumber && x.id != command.Id)) return op.Failed("این شماره حساب قبلا ثبت شده است"); if (!string.IsNullOrWhiteSpace(command.CardNumber) && _employeeBankInformationRepository.Exists(x => x.WorkshopId == command.WorkshopId && x.CardNumber == command.CardNumber && x.id != command.Id)) return op.Failed("این شماره کارت قبلا ثبت شده است"); if (!string.IsNullOrWhiteSpace(command.ShebaNumber) && _employeeBankInformationRepository.Exists(x => x.WorkshopId == command.WorkshopId && x.ShebaNumber == command.ShebaNumber && x.id != command.Id)) return op.Failed("این شماره شبا قبلا ثبت شده است"); if (!string.IsNullOrWhiteSpace(command.ShebaNumber) && _employeeBankInformationRepository.Exists(x => x.WorkshopId == command.WorkshopId && x.ShebaNumber == command.ShebaNumber && x.id != command.Id)) return op.Failed("این شماره شبا قبلا ثبت شده است"); entity.Edit(command.BankId, command.BankAccountNumber, command.CardNumber, command.ShebaNumber); _employeeBankInformationRepository.SaveChanges(); return op.Succcedded(); } public List Search(long workshopId, EmployeeBankInformationSearchModel searchParams) { return _employeeBankInformationRepository.Search(workshopId, searchParams); } public List SearchForExcel(long workshopId, EmployeeBankInformationSearchModel searchParams) { return _employeeBankInformationRepository.SearchForExcel(workshopId, searchParams); } public List GetAllByWorkshopId(long workshopId, EmployeeBankInformationSearchModel searchParams) { return _employeeBankInformationRepository.GetAllByWorkshopId(workshopId); } /// /// دریافت مشخصات بانکی پرسنل /// /// تمامی رکورد های مربوط به پرسنل public GroupedEmployeeBankInformationViewModel GetByEmployeeId(long workshopId, long employeeId) { var entity = _employeeBankInformationRepository.GetByEmployeeId(workshopId, employeeId); if (entity == null) return new(); return entity; } public EmployeeBankInformationViewModel GetDetails(long id) { return _employeeBankInformationRepository.GetDetails(id); } public OperationResult Remove(long id) { OperationResult op = new(); var entity = _employeeBankInformationRepository.Get(id); if (entity == null) return op.Failed(ApplicationMessages.RecordNotFound); _employeeBankInformationRepository.Remove(entity); _employeeBankInformationRepository.SaveChanges(); return op.Succcedded(); } public OperationResult RemoveByEmployeeId(long workshopId, long employeeId) { OperationResult op = new(); var entities = _employeeBankInformationRepository.GetRangeByEmployeeId(workshopId, employeeId); if (entities == null || !entities.Any()) return op.Failed(ApplicationMessages.RecordNotFound); _employeeBankInformationRepository.RemoveRange(entities); _employeeBankInformationRepository.SaveChanges(); return op.Succcedded(); } public List GetAllByWorkshopId(long workshopId) { return _employeeBankInformationRepository.GetAllByWorkshopId(workshopId); } #region Private Methods private OperationResult ValidateCreateOperation(List workshopEmployeeBankInfoList, CreateEmployeeInformation command) { OperationResult op = new(); //if (workshopEmployeeBankInfoList.Exists(x => // x.EmployeeId == command.EmployeeId && x.WorkshopId == workshopId && // x.BankId == command.BankId)) // return op.Failed("این کاربر در بانک انتخاب شده حساب دارد"); if (HasAtLeastOneFormFilled(command) == false) return op.Failed("لطفا حداقل یکی از مشخصات بانکی را کامل کنید"); //Conditions below and multiple accesses to DB can be optimized if needed if (!string.IsNullOrWhiteSpace(command.BankAccountNumber) && workshopEmployeeBankInfoList.Exists(x => x.BankInformation.Exists(y => y.BankAccountNumber == command.BankAccountNumber))) return op.Failed("این شماره حساب قبلا ثبت شده است"); if (!string.IsNullOrWhiteSpace(command.CardNumber) && workshopEmployeeBankInfoList.Exists(x => x.BankInformation.Exists(y => y.CardNumber == command.CardNumber))) return op.Failed("این شماره کارت قبلا ثبت شده است"); if (!string.IsNullOrWhiteSpace(command.ShebaNumber) && workshopEmployeeBankInfoList.Exists(x => x.BankInformation.Exists(y => y.ShebaNumber == command.ShebaNumber))) return op.Failed("این شماره شبا قبلا ثبت شده است"); return op.Succcedded(); } private bool HasAtLeastOneFormFilled(CreateEmployeeInformation inputs) { return !string.IsNullOrWhiteSpace(inputs.BankAccountNumber) || !string.IsNullOrWhiteSpace(inputs.CardNumber) || !string.IsNullOrWhiteSpace(inputs.ShebaNumber); } private bool HasNoRecordInWorkshop(long workshopId, long employeeId,List workshopRecords) { return !workshopRecords.Exists(x => x.WorkshopId == workshopId && x.EmployeeId == employeeId); } #endregion } }