2024-05-30 22:31:40 +04:00

177 lines
6.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityBusinessLogics.OfficePackage.HelperEnums;
using UniversityBusinessLogics.OfficePackage.HelperModels.Excel;
using UniversityContracts.ViewModels;
namespace UniversityBusinessLogics.OfficePackage
{
public abstract class AbstractSaveToExcel
{
private void CreateHeaders(ExcelInfo info)
{
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "A",
RowIndex = 1,
Text = info.Title,
StyleInfo = ExcelStyleInfoType.Title
});
MergeCells(new ExcelMergeParameters
{
CellFromName = "A1",
CellToName = "G1"
});
for (var i = 0; i < info.Headers.Count; i++)
{
InsertCellInWorksheet(new ExcelCellParameters()
{
ColumnName = ((char)('A' + i)).ToString(),
RowIndex = 2,
Text = info.Headers[i],
StyleInfo = ExcelStyleInfoType.Text
});
}
}
/// <summary>
/// Создание отчета
/// </summary>
/// <param name="info"></param>
public void CreateReportPurchase(ExcelInfo info)
{
CreateExcel(info);
CreateHeaders(info);
uint rowIndex = 3;
foreach (var pc in info.ReportObjects)
{
if (pc is not PurchaseViewModel purchase)
{
throw new ArgumentException($"Передан некорректный тип в отчет: " +
$"ожидается PurchaseViewModel; Получен объект типа: {pc.GetType()}", nameof(info));
}
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "B",
RowIndex = rowIndex,
Text = purchase.DatePurchase.ToShortDateString(),
StyleInfo = ExcelStyleInfoType.Text
});
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "A",
RowIndex = rowIndex,
Text = purchase.Id.ToString(),
StyleInfo = ExcelStyleInfoType.Text
});
int i = 0;
foreach (var car in purchase.OperationViewModels)
{
if (info.Ids != null && !info.Ids.Contains(car.Id))
{
continue;
}
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = ((char)('C' + i)).ToString(),
RowIndex = rowIndex,
Text = $"{car.Mark} {car.Model} в количестве {purchase.OperationsModel[car.Id].CountOperations}",
StyleInfo = ExcelStyleInfoType.TextWithBroder
});
if (info.Ids != null)
{
break;
}
i++;
}
rowIndex++;
}
SaveExcel(info);
}
public void CreateReportOperations(ExcelInfo info)
{
CreateExcel(info);
CreateHeaders(info);
uint rowIndex = 3;
foreach (var pc in info.ReportObjects)
{
var car = pc as OperationViewModel;
if (car == null)
{
throw new ArgumentException($"Передан некорректный тип в отчет: " +
$"ожидается OperationViewModel; Получен объект типа: {pc.GetType()}", nameof(info));
}
foreach (var purchase in car.Purchases)
{
if (info.Ids != null && !info.Ids.Contains(purchase.Id))
{
continue;
}
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "A",
RowIndex = rowIndex,
Text = car.Mark,
StyleInfo = ExcelStyleInfoType.Text
});
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "B",
RowIndex = rowIndex,
Text = car.Model,
StyleInfo = ExcelStyleInfoType.Text
});
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "C",
RowIndex = rowIndex,
Text = purchase.OperationsModel[car.Id].CountOperations.ToString(),
StyleInfo = ExcelStyleInfoType.Text
});
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "C",
RowIndex = rowIndex,
Text = purchase.DatePurchase.ToString(),
StyleInfo = ExcelStyleInfoType.Text
});
break;
}
rowIndex++;
}
SaveExcel(info);
}
/// <summary>
/// Создание excel-файла
/// </summary>
/// <param name="info"></param>
protected abstract void CreateExcel(ExcelInfo info);
/// <summary>
/// Добавляем новую ячейку в лист
/// </summary>
/// <param name="cellParameters"></param>
protected abstract void InsertCellInWorksheet(ExcelCellParameters excelParams);
/// <summary>
/// Объединение ячеек
/// </summary>
/// <param name="mergeParameters"></param>
protected abstract void MergeCells(ExcelMergeParameters excelParams);
/// <summary>
/// Сохранение файла
/// </summary>
/// <param name="info"></param>
protected abstract void SaveExcel(ExcelInfo info);
}
}