using Microsoft.Extensions.Logging; using PapaCarloContracts.BusinessLogicContracts; using PapaCarloContracts.DataModels; using PapaCarloContracts.Exceptions; using PapaCarloContracts.Extensions; using PapaCarloContracts.StoragesContracts; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PapaCarloBusinessLogic.Implementations; internal class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageContract, ICuttingStorageContract cuttingStorageContract, IPostStorageContract postStorageContract, IWorkerStorageContract workerStorageContract, ILogger logger) : ISalaryBusinessLogicContract { private readonly ILogger _logger = logger; private readonly ISalaryStorageContract _salaryStorageContract = salaryStorageContract; private readonly ICuttingStorageContract _cuttingStorageContract = cuttingStorageContract; private readonly IPostStorageContract _postStorageContract = postStorageContract; private readonly IWorkerStorageContract _workerStorageContract = workerStorageContract; 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 GetAllSalariesByPeriodByWorker(DateTime fromDate, DateTime toDate, string workerId) { if (fromDate.IsDateNotOlder(toDate)) { throw new IncorrectDatesException(fromDate, toDate); } if (workerId.IsEmpty()) { throw new ArgumentNullException(nameof(workerId)); } if (!workerId.IsGuid()) { throw new ValidationException("The value in the field workerId is not a unique identifier."); } _logger.LogInformation("GetAllSalaries params: {fromDate}, {toDate}, {workerId}", fromDate, toDate, workerId); return _salaryStorageContract.GetList(fromDate, toDate, workerId) ?? 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 workers = _workerStorageContract.GetList() ?? throw new NullListException(); foreach (var worker in workers) { var cuttings = _cuttingStorageContract.GetList(startDate, finishDate, workerId: worker.Id)?.Sum(x => x.Products.Count) ?? throw new NullListException(); var post = _postStorageContract.GetElementById(worker.PostId) ?? throw new NullListException(); var salary = cuttings * 100; _logger.LogDebug("The employee {workerId} was paid a salary of {salary}", worker.Id, salary); _salaryStorageContract.AddElement(new SalaryDataModel(worker.Id, finishDate, salary)); } } }