using AsbCloudApp.Data;
using AsbCloudApp.Data.DailyReport;
using AsbCloudDb.Model;
using ClosedXML.Excel;
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 readonly 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 rowColumn = (CountColumns * 2) + (OperationCategoriesCount % CountColumns == 0 ? 0 : 2);
var cellColumn = OperationCategoriesCount % CountColumns;
AddressBlockEnd = AddressBlockBegin + (rowColumn, cellColumn);
}
public override void Draw(IXLWorksheet sheet)
{
sheet.Range(Title.RowNumber, Title.ColumnNumber, Title.RowNumber, Title.ColumnNumber + 1)
.Merge()
.SetValue("БАЛАНС ВРЕМЕНИ");
var rowColumn = 2;
var cellColumn = 1;
var rowsCountInColumn = OperationCategoriesCount / CountColumns;
if (OperationCategoriesCount % CountColumns != 0)
rowsCountInColumn++;
foreach (var operationCategory in OperationCategories)
{
sheet.Cell(AddressBlockBegin + (rowColumn, cellColumn))
._SetValue(GetCaption(operationCategory.Id), true);
sheet.Cell(AddressBlockBegin + (rowColumn, cellColumn + 1))
._SetValue(GetValue(operationCategory.Id), true);
if (rowColumn <= rowsCountInColumn)
rowColumn++;
else
{
rowColumn = 2;
cellColumn = cellColumn + 2;
}
}
}
private string GetValue(int categoryId)
{
if (OperationsStatistics.TryGetValue(categoryId, out double duration))
return $"{duration}";
return "0";
}
private string GetCaption(int categoryId)
{
var caption = OperationCategories.FirstOrDefault(o => o.Id == categoryId)?.Name ?? string.Empty;
return caption;
}
}
}