Files
carparkvalidation/CarParkValidationAPI/Repository/ConcessionValidationReportRepository.cs
T
2026-06-18 21:39:20 +10:00

140 lines
5.5 KiB
C#

using CarParkValidationAPI.Datas;
using CarParkValidationAPI.Models;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SpreadsheetLight;
using CarParkValidationAPI.Entities;
using System.IO;
namespace CarParkValidationAPI.Repository
{
public class ConcessionValidationReportRepository
{
private readonly DataContext _context;
private readonly IOptions<AppSettings> _appSettings;
const string DateFormat = "dd-MM-yyyy HH:mm";
const string dateFormat = "dd-MM-yyyyy";
public ConcessionValidationReportRepository(
DataContext context, IOptions<AppSettings> appSettings)
{
_context = context;
_appSettings = appSettings;
}
public async Task<ResultModel<FileContent>> GetConcessionValidationReport(DateTime fromDate, DateTime toDate)
{
//excel SLDocument sl = new SLDocument();
int statusCode = 1;
ConcessionValidationExtDto item;
int col = 1;
int row = 1;
SLDocument sl = new SLDocument();
//first sheetName
sl.RenameWorksheet(SLDocument.DefaultFirstSheetName, "ConcessionValidation");
SLStyle style = sl.CreateStyle();
style.SetFontUnderline(DocumentFormat.OpenXml.Spreadsheet.UnderlineValues.Single);
style.SetFontBold(true);
style.SetFont("Arial Black", 16);
sl.SetCellStyle(1, 5, style);
sl.SetRowHeight(1, 1, 25);
sl.SetCellValue(row++, 5, "The Concession Validation Report");
sl.SetCellValue(row, 3, "Date Range: " + fromDate.ToString(dateFormat) + " - " + toDate.ToString(dateFormat) );
sl.SetCellValue(1, 6, " " + DateTime.Now.ToString("ddd dd/MM/yyyy HH:mm"));
int startTitleRow = 3;
row = startTitleRow;
//make the title bold
style = sl.CreateStyle();
style.Font.Bold = true;
sl.SetRowStyle(startTitleRow, style);
//set it height
sl.SetRowHeight(startTitleRow, startTitleRow, 25);
// add the title first
sl.SetCellValue(row, col++, "Date");
sl.SetCellValue(row, col++, "Concession Card Number");
sl.SetCellValue(row, col++, "Concession Holder Name");
sl.SetCellValue(row, col++, "Expiry date checked");
sl.SetCellValue(row, col++, "ConcessionType");
sl.SetCellValue(row, col++, "Validation Piont");
// sl.SetCellValue(row, col++, "StaffLink No");
sl.SetCellValue(row, col++, "Enter by");
// sl.SetCellValue(row, col++, "Staff Last name");
// sl.SetCellValue(row, col++, "Staff Email");
sl.FreezePanes(row, col - 1); //frozen the row.
//wrap text on is concession Expiry
style = sl.CreateStyle();
style.SetWrapText(true);
sl.SetColumnStyle(4, style);
//format date
style = sl.CreateStyle();
style.FormatCode = "dd/MM/yyyy HH:MM";
sl.SetColumnStyle(1, style);
//make column width
sl.SetColumnWidth(1, 20);
sl.SetColumnWidth(2, 18);
sl.SetColumnWidth(3, 25);
sl.SetColumnWidth(4, 20);
sl.SetColumnWidth(5, 50);
sl.SetColumnWidth(6, 40);
sl.SetColumnWidth(7, 40);
// sl.SetColumnWidth(8, 20);
// sl.SetColumnWidth(9, 20);
// sl.SetColumnWidth(10, 20);
int startRowFrom = startTitleRow+1;
var list = await _context.SelectConcessionValidationByDate(true, fromDate.ToLocalTime(), toDate.ToLocalTime());
for (int i = 0; i < list.Count; i++)
{
col = 1;
item = list[i];
row = i + startRowFrom;
sl.SetCellValue(row, col++, item.ValidateDate);
sl.SetCellValue(row, col++, item.ConcessionCard);
sl.SetCellValue(row, col++, item.ConcessionHolder);
sl.SetCellValue(row, col++, item.ConcessionExpiry ? "Yes": "No");
sl.SetCellValue(row, col++, item.ConcessionType);
sl.SetCellValue(row, col++, item.ValidationPoint);
// sl.SetCellValue(row, col++, item.StafflinkNo);
sl.SetCellValue(row, col++, item.StaffFirstName + " " + item.StaffLastName);
// sl.SetCellValue(row, col++, item.StaffLastName);
// sl.SetCellValue(row, col++, item.StaffEmail);
}
byte[] array;
// sl.SaveAs("C:\\Temp\\Report\\ConcessionValidationDate" + DateTime.Now.ToString("yyyyMMddHHmm") + ".xlsx");
using (MemoryStream ws = new MemoryStream())
{
sl.SaveAs(ws);
array = ws.ToArray();
/*
using (FileStream fs = new FileStream("c:\\temp\\Report\\conReport.xlsx",FileMode.Create,FileAccess.Write))
{
ws.WriteTo(fs);
fs.Close();
}
*/
ws.Close();
}
var result = new FileContent
{
Content = array,
FileName = "ConcessionValidation" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm")+ ".xlsx",
};
return new ResultModel<FileContent>
{
Data = result,
StatusCode = statusCode
};
}
}
}