From 7e9ba23b9779c7f226e23f7a281742361eb03a49 Mon Sep 17 00:00:00 2001 From: mahan Date: Sun, 23 Nov 2025 12:52:07 +0330 Subject: [PATCH] add Excel export for workshop roll call data with new exporter and view model --- .../WorkshopRollCallExcelExporter.cs | 47 +++++++++++++++++++ .../WorkshopRollCallExcelViewModel.cs | 11 +++++ .../Pages/Company/AndroidApk/Index.cshtml.cs | 19 +++++++- ServiceHost/appsettings.Development.json | 2 +- 4 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 CompanyManagement.Infrastructure.Excel/WorkshopsRollCall/WorkshopRollCallExcelExporter.cs create mode 100644 CompanyManagement.Infrastructure.Excel/WorkshopsRollCall/WorkshopRollCallExcelViewModel.cs diff --git a/CompanyManagement.Infrastructure.Excel/WorkshopsRollCall/WorkshopRollCallExcelExporter.cs b/CompanyManagement.Infrastructure.Excel/WorkshopsRollCall/WorkshopRollCallExcelExporter.cs new file mode 100644 index 00000000..86676126 --- /dev/null +++ b/CompanyManagement.Infrastructure.Excel/WorkshopsRollCall/WorkshopRollCallExcelExporter.cs @@ -0,0 +1,47 @@ +using System.Drawing; +using CompanyManagment.App.Contracts.Workshop; +using OfficeOpenXml; +using OfficeOpenXml.Style; + +namespace CompanyManagement.Infrastructure.Excel.WorkshopsRollCall; + +public class WorkshopRollCallExcelExporter +{ + public static byte[] Export(List workshops) + { + ExcelPackage.LicenseContext = LicenseContext.NonCommercial; + using (var package = new ExcelPackage()) + { + var ws = package.Workbook.Worksheets.Add("Workshops"); + // Header + ws.Cells[1, 1].Value = "نام کارگاه"; + ws.Cells[1, 2].Value = "فعال/غیرفعال"; + ws.Cells[1, 3].Value = "تعداد پرسنل"; + ws.Cells[1, 4].Value = "نام کارفرما"; + ws.Row(1).Style.Font.Bold = true; + ws.Row(1).Style.Fill.PatternType = ExcelFillStyle.Solid; + ws.Row(1).Style.Fill.BackgroundColor.SetColor(Color.LightGray); + + int row = 2; + foreach (var w in workshops) + { + ws.Cells[row, 1].Value = w.WorkshopName; + ws.Cells[row, 2].Value = w.IsActive ? "فعال" : "غیرفعال"; + ws.Cells[row, 3].Value = w.PersonnelCount; + ws.Cells[row, 4].Value = w.EmployerName; + if (!w.IsActive) + { + using (var range = ws.Cells[row, 1, row, 4]) + { + range.Style.Fill.PatternType = ExcelFillStyle.Solid; + range.Style.Fill.BackgroundColor.SetColor(Color.LightGray); + } + } + row++; + } + ws.Cells[ws.Dimension.Address].AutoFitColumns(); + return package.GetAsByteArray(); + } + } + +} \ No newline at end of file diff --git a/CompanyManagement.Infrastructure.Excel/WorkshopsRollCall/WorkshopRollCallExcelViewModel.cs b/CompanyManagement.Infrastructure.Excel/WorkshopsRollCall/WorkshopRollCallExcelViewModel.cs new file mode 100644 index 00000000..e4da5109 --- /dev/null +++ b/CompanyManagement.Infrastructure.Excel/WorkshopsRollCall/WorkshopRollCallExcelViewModel.cs @@ -0,0 +1,11 @@ +namespace CompanyManagement.Infrastructure.Excel.WorkshopsRollCall +{ + public class WorkshopRollCallExcelViewModel + { + public string WorkshopName { get; set; } + public bool IsActive { get; set; } + public int PersonnelCount { get; set; } + public string EmployerName { get; set; } + } +} + diff --git a/ServiceHost/Areas/AdminNew/Pages/Company/AndroidApk/Index.cshtml.cs b/ServiceHost/Areas/AdminNew/Pages/Company/AndroidApk/Index.cshtml.cs index f4b1e222..eaae0a4e 100644 --- a/ServiceHost/Areas/AdminNew/Pages/Company/AndroidApk/Index.cshtml.cs +++ b/ServiceHost/Areas/AdminNew/Pages/Company/AndroidApk/Index.cshtml.cs @@ -28,6 +28,7 @@ using Parbad; using Parbad.AspNetCore; using Parbad.Gateway.Sepehr; using System.ComponentModel.DataAnnotations; +using CompanyManagement.Infrastructure.Excel.WorkshopsRollCall; using static ServiceHost.Areas.AdminNew.Pages.Company.AndroidApk.IndexModel2; namespace ServiceHost.Areas.AdminNew.Pages.Company.AndroidApk @@ -162,7 +163,23 @@ namespace ServiceHost.Areas.AdminNew.Pages.Company.AndroidApk //await ChangeFridayWorkToWeeklyDayOfWeek(); //await SetPublicId(); - await CreateDadmehrWorkshopFaceEmbedding(); + var now = DateTime.Now; + var data = _context.RollCallServices + .Include(x => x.Workshop) + .ThenInclude(x => x.LeftWorks) + .Include(x => x.Workshop) + .ThenInclude(x => x.WorkshopEmployers) + .ThenInclude(x=>x.Employer) + .Select(x => new WorkshopRollCallExcelViewModel() + { + EmployerName = x.Workshop.WorkshopEmployers.First().Employer.FullName, + IsActive = x.IsActiveString == "true", + PersonnelCount = x.Workshop.LeftWorks.Count(l => l.StartWorkDate <= now && l.LeftWorkDate >= now), + WorkshopName = x.Workshop.WorkshopFullName + }).OrderByDescending(x=>x.IsActive).ToList(); + var dataBytes = WorkshopRollCallExcelExporter.Export(data); + return File(dataBytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + "RollCallServices.xlsx"); ViewData["message"] = "تومام دو"; return Page(); } diff --git a/ServiceHost/appsettings.Development.json b/ServiceHost/appsettings.Development.json index 0f4feeef..4ee562b0 100644 --- a/ServiceHost/appsettings.Development.json +++ b/ServiceHost/appsettings.Development.json @@ -12,7 +12,7 @@ //"MesbahDb": "Data Source=DESKTOP-NUE119G\\MSNEW;Initial Catalog=Mesbah_db;Integrated Security=True" //server - //"MesbahDbServer": "Data Source=171.22.24.15;Initial Catalog=mesbah_db;Persist Security Info=False;User ID=ir_db;Password=R2rNp[170]18[3019]#@ATt;TrustServerCertificate=true;", + "MesbahDbServer": "Data Source=171.22.24.15;Initial Catalog=mesbah_db;Persist Security Info=False;User ID=ir_db;Password=R2rNp[170]18[3019]#@ATt;TrustServerCertificate=true;", //local "MesbahDb": "Data Source=.;Initial Catalog=mesbah_db;Integrated Security=True;TrustServerCertificate=true;",