using AsbCloudApp.Data; using AsbCloudApp.Data.DailyReport; using ClosedXML.Excel; using System; using System.Collections.Generic; using System.Linq; namespace AsbCloudInfrastructure.Services.DailyReport.DailyReportBlocks { /// /// Построение баланса времени /// class TimeBalanceBlock : BlockAbstract { /// /// Начальная ячейка /// public override CellAddress AddressBlockBegin { get; } /// /// Конечная ячейка /// public override CellAddress AddressBlockEnd { get; } /// /// Ячейка с заголовком /// private CellAddress Title { get { return AddressBlockBegin + (1, 3); } } /// /// Статистика по операциям /// private Dictionary OperationsStatistics { get; } /// /// Категории операций /// private IEnumerable OperationCategories { get; } /// /// количество столбцов в таблице /// private const int countColumns = 3; /// /// количество категорий операций /// private int OperationCategoriesCount { get { return OperationCategories.Count(); } } public TimeBalanceBlock(CellAddress addressBlockBegin, TimeBalanceDto blockDto, IEnumerable operationCategories) { AddressBlockBegin = addressBlockBegin.Copy(); OperationsStatistics = blockDto.OperationsStat; OperationCategories = operationCategories; var rowsCount = (int)Math.Ceiling( 1d * OperationCategoriesCount / countColumns); var colsCount = (1 + 1) * countColumns; AddressBlockEnd = Title + (rowsCount, colsCount); } public override void Draw(IXLWorksheet sheet) { sheet.Range(Title.RowNumber, Title.ColumnNumber, Title.RowNumber, Title.ColumnNumber + 1) .Merge() .SetValue("БАЛАНС ВРЕМЕНИ"); var i = 0; foreach (var operationCategory in OperationCategories) { var row = 2 + (int)Math.Floor(1d * i / countColumns); var col = 1 + 2 *(i % countColumns); i++; sheet.Cell(AddressBlockBegin + (row, col)) ._SetValue(operationCategory.Name, true); sheet.Cell(AddressBlockBegin + (row, col + 1)) ._SetValue(GetValue(operationCategory.Id), true); } } private string GetValue(int categoryId) { if (OperationsStatistics.TryGetValue(categoryId, out double duration)) return $"{duration}"; return "0"; } } }