using Microsoft.Extensions.Logging; using TheCyclopsContracts.BusinessLogicContracts; using TheCyclopsContracts.DataModels; using TheCyclopsContracts.Exceptions; using TheCyclopsContracts.Extensions; using TheCyclopsContracts.StoragesContracts; namespace TheCyclopsBusinessLogic.Implementations; internal class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageContract, IInstallationStorageContract installationStorageContract, IPostStorageContract postStorageContract, IEmployeeStorageContract employeeStorageContract, ILogger logger) : ISalaryBusinessLogicContract { private readonly ILogger _logger = logger; private readonly ISalaryStorageContract _salaryStorageContract = salaryStorageContract; private readonly IInstallationStorageContract _installationStorageContract = installationStorageContract; private readonly IPostStorageContract _postStorageContract = postStorageContract; private readonly IEmployeeStorageContract _employeeStorageContract = employeeStorageContract; public List GetAllSalariesByPeriod(DateTime fromDate, DateTime toDate) { _logger.LogInformation("GetAllSalaries params: {fromDate}, {toDate}", fromDate, toDate); if (fromDate.IsDateNotOlder(toDate)) { throw new IncorrectDatesException(fromDate, toDate); } return _salaryStorageContract.GetList(fromDate, toDate) ?? throw new NullListException(); } public List GetAllSalariesByPeriodByEmployee(DateTime fromDate, DateTime toDate, string employeeId) { if (fromDate.IsDateNotOlder(toDate)) { throw new IncorrectDatesException(fromDate, toDate); } if (employeeId.IsEmpty()) { throw new ArgumentNullException(nameof(employeeId)); } if (!employeeId.IsGuid()) { throw new ValidationException("The value in the field employeeId is not a unique identifier."); } _logger.LogInformation("GetAllSalaries params: {fromDate}, {toDate}, {employeeId}", fromDate, toDate, employeeId); return _salaryStorageContract.GetList(fromDate, toDate, employeeId) ?? throw new NullListException(); } public void CalculateSalaryByMounth(DateTime date) { _logger.LogInformation("CalculateSalaryByMounth: {date}", date); var startDate = new DateTime(date.Year, date.Month, 1); var finishDate = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month)); var employees = _employeeStorageContract.GetList() ?? throw new NullListException(); foreach (var employee in employees) { var installations = _installationStorageContract.GetList(startDate, finishDate, employeeId: employee.Id)?.Sum(x => x.InstallationPrice) ?? throw new NullListException(); var post = _postStorageContract.GetElementById(employee.PostId) ?? throw new NullListException(); var salary = post.Salary + installations * 0.1; _logger.LogDebug("The employee {employeeId} was paid a salary of {salary}", employee.Id, salary); _salaryStorageContract.AddElement(new SalaryDataModel(employee.Id, finishDate, salary)); } } }