using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using TheButcherShopContracts.BusinessLogicContracts; using TheButcherShopContracts.DataModels; using TheButcherShopContracts.Exceptions; using TheButcherShopContracts.Extensions; using TheButcherShopContracts.StoragesContracts; namespace ButcherShopBusinessLogic.Implementations; internal class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageContract,ISaleStorageContract saleStorageContract, IPostStorageContract postStorageContract, IWorkerStorageContract workerStorageContract, ILogger logger) : ISalaryBusinessLogicContract { private readonly ILogger _logger = logger; private readonly ISalaryStorageContract _salaryStorageContract = salaryStorageContract; private readonly ISaleStorageContract _saleStorageContract = saleStorageContract; 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 sales = _saleStorageContract.GetList(startDate, finishDate, workerId: worker.Id)?.Sum(x => x.Sum) ?? throw new NullListException(); var post = _postStorageContract.GetElementById(worker.PostId) ?? throw new NullListException(); var salary = post.Salary + sales * 0.1; _logger.LogDebug("The employee {workerId} was paid a salary of { salary}", worker.Id, salary); _salaryStorageContract.AddElement(new SalaryDataModel(worker.Id, finishDate, salary)); } } }