DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/DailyReport/DailyReportBlocks/TimeBalanceBlock.cs

108 lines
3.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
{
/// <summary>
/// Пострение баланса времени
/// </summary>
class TimeBalanceBlock : BlockAbstract
{
/// <summary>
/// Начальная ячейка
/// </summary>
public override CellAddress AddressBlockBegin { get; }
/// <summary>
/// Конечная ячейка
/// </summary>
public override CellAddress AddressBlockEnd { get; }
/// <summary>
/// Ячейка с заголовком
/// </summary>
private CellAddress Title { get { return AddressBlockBegin + (1, 3); } }
/// <summary>
/// Статистика по операциям
/// </summary>
private Dictionary<int, double> OperationsStatistics { get; }
/// <summary>
/// Категории операций
/// </summary>
private IEnumerable<WellOperationCategoryDto> OperationCategories { get; }
/// <summary>
/// количество стобцов в таблице
/// </summary>
private readonly int CountColumns = 3;
/// <summary>
/// количество категорий операций
/// </summary>
private int OperationCategoriesCount { get { return OperationCategories.Count(); } }
public TimeBalanceBlock(CellAddress addressBlockBegin, TimeBalanceDto blockDto, IEnumerable<WellOperationCategoryDto> 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;
}
}
}