177 lines
6.4 KiB
C#
Raw Normal View History

2024-05-26 00:28:11 +04:00
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;
2024-05-30 22:31:40 +04:00
foreach (var car in purchase.OperationViewModels)
2024-05-26 00:28:11 +04:00
{
if (info.Ids != null && !info.Ids.Contains(car.Id))
{
continue;
}
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = ((char)('C' + i)).ToString(),
RowIndex = rowIndex,
2024-05-30 22:31:40 +04:00
Text = $"{car.Mark} {car.Model} в количестве {purchase.OperationsModel[car.Id].CountOperations}",
2024-05-26 00:28:11 +04:00
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)
{
2024-05-30 22:31:40 +04:00
var car = pc as OperationViewModel;
2024-05-26 00:28:11 +04:00
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,
2024-05-30 22:31:40 +04:00
Text = car.Mark,
2024-05-26 00:28:11 +04:00
StyleInfo = ExcelStyleInfoType.Text
});
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "B",
RowIndex = rowIndex,
2024-05-30 22:31:40 +04:00
Text = car.Model,
2024-05-26 00:28:11 +04:00
StyleInfo = ExcelStyleInfoType.Text
});
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "C",
RowIndex = rowIndex,
2024-05-30 22:31:40 +04:00
Text = purchase.OperationsModel[car.Id].CountOperations.ToString(),
2024-05-26 00:28:11 +04:00
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);
}
}