init
This commit is contained in:
@@ -66,4 +66,9 @@ public class File1 : EntityBase
|
||||
Description = description;
|
||||
Status = status;
|
||||
}
|
||||
|
||||
public void ChangeStatus(int status)
|
||||
{
|
||||
Status = status;
|
||||
}
|
||||
}
|
||||
@@ -531,6 +531,11 @@ namespace Company.Domain.RollCallAgg
|
||||
FridayWorkTimeSpan = CalculateFridayWorkDuration(StartDate.Value, EndDate.Value);
|
||||
}
|
||||
|
||||
public void SetStartDate(DateTime start)
|
||||
{
|
||||
StartDate = start;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// جیزه
|
||||
/// </summary>
|
||||
|
||||
@@ -0,0 +1,157 @@
|
||||
@page
|
||||
@model ServiceHost.Areas.AdminNew.Pages.Company.ExcelUpload.IndexModel
|
||||
@{
|
||||
ViewData["Title"] = "بارگذاری فایل اکسل";
|
||||
}
|
||||
|
||||
<div class="container-fluid">
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
<div class="card">
|
||||
<div class="card-header">
|
||||
<h3 class="card-title">بارگذاری و بررسی فایل اکسل</h3>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<form method="post" enctype="multipart/form-data">
|
||||
<div class="form-group">
|
||||
<label for="ExcelFile">انتخاب فایل اکسل (ستون B و C از ردیف 2 به بعد خوانده میشود):</label>
|
||||
<input type="file" class="form-control" id="ExcelFile" name="ExcelFile" accept=".xlsx,.xls" required />
|
||||
</div>
|
||||
<button type="submit" asp-page-handler="UploadExcel" class="btn btn-primary">بارگذاری و پردازش</button>
|
||||
</form>
|
||||
|
||||
@if (!string.IsNullOrEmpty(Model.Message))
|
||||
{
|
||||
<div class="alert alert-info mt-3" role="alert">
|
||||
@Model.Message
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@if (Model.HasProcessed)
|
||||
{
|
||||
<div class="row mt-4">
|
||||
<!-- دسته اول: فایلهایی که در اکسل هستند -->
|
||||
<div class="col-md-6">
|
||||
<div class="card">
|
||||
<div class="card-header bg-success text-white">
|
||||
<h4 class="card-title mb-0">فایلهای موجود در اکسل (@Model.FoundInExcel.Count مورد)</h4>
|
||||
</div>
|
||||
<div class="card-body" style="max-height: 600px; overflow-y: auto;">
|
||||
@if (Model.FoundInExcel.Any())
|
||||
{
|
||||
<table class="table table-striped table-hover">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>ردیف</th>
|
||||
<th>شناسه</th>
|
||||
<th>شماره بایگانی</th>
|
||||
<th>رده پرونده</th>
|
||||
<th>وضعیت</th>
|
||||
<th>عملیات</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@{
|
||||
int index = 1;
|
||||
}
|
||||
@foreach (var item in Model.FoundInExcel)
|
||||
{
|
||||
<tr>
|
||||
<td>@index</td>
|
||||
<td>@item.ArchiveNo</td>
|
||||
<td>@item.FileClass</td>
|
||||
<td>@(item.Status == 2 ? "فعال" : "غیرفعال")</td>
|
||||
<td>
|
||||
<button class="btn btn-sm btn-primary">ویرایش</button>
|
||||
</td>
|
||||
</tr>
|
||||
index++;
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
}
|
||||
else
|
||||
{
|
||||
<p class="text-muted">هیچ موردی یافت نشد.</p>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- دسته دوم: فایلهایی که در اکسل نیستند -->
|
||||
<div class="col-md-6">
|
||||
<div class="card">
|
||||
<div class="card-header bg-warning text-dark">
|
||||
<h4 class="card-title mb-0">فایلهای غیرموجود در اکسل (@Model.NotFoundInExcel.Count مورد)</h4>
|
||||
</div>
|
||||
<div class="card-body" style="max-height: 600px; overflow-y: auto;">
|
||||
@if (Model.NotFoundInExcel.Any())
|
||||
{
|
||||
<table class="table table-striped table-hover">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>ردیف</th>
|
||||
<th>شناسه</th>
|
||||
<th>شماره بایگانی</th>
|
||||
<th>رده پرونده</th>
|
||||
<th>وضعیت</th>
|
||||
<th>عملیات</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@{
|
||||
int index2 = 1;
|
||||
}
|
||||
@foreach (var item in Model.NotFoundInExcel)
|
||||
{
|
||||
<tr>
|
||||
<td>@index2</td>
|
||||
<td>@item.ArchiveNo</td>
|
||||
<td>@item.FileClass</td>
|
||||
<td>@(item.Status == 2 ? "فعال" : "غیرفعال")</td>
|
||||
<td>
|
||||
<button class="btn btn-sm btn-primary">ویرایش</button>
|
||||
</td>
|
||||
</tr>
|
||||
index2++;
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
}
|
||||
else
|
||||
{
|
||||
<p class="text-muted">هیچ موردی یافت نشد.</p>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.card {
|
||||
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
||||
border-radius: 8px;
|
||||
}
|
||||
|
||||
.card-header {
|
||||
border-radius: 8px 8px 0 0;
|
||||
}
|
||||
|
||||
.table {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
.table thead th {
|
||||
position: sticky;
|
||||
top: 0;
|
||||
background-color: #f8f9fa;
|
||||
z-index: 10;
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -0,0 +1,156 @@
|
||||
using Company.Domain.File1;
|
||||
using Company.Domain.RollCallAgg;
|
||||
using Company.Domain.RollCallAgg.DomainService;
|
||||
using CompanyManagment.EFCore;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
using OfficeOpenXml;
|
||||
|
||||
namespace ServiceHost.Areas.AdminNew.Pages.Company.ExcelUpload;
|
||||
|
||||
public class ExcelRowData
|
||||
{
|
||||
public string ColumnB { get; set; }
|
||||
public string ColumnC { get; set; }
|
||||
}
|
||||
|
||||
public class IndexModel : PageModel
|
||||
{
|
||||
private readonly CompanyContext _dbContext;
|
||||
private readonly IRollCallDomainService _rollCallDomainService;
|
||||
|
||||
[BindProperty]
|
||||
public IFormFile ExcelFile { get; set; }
|
||||
|
||||
public string Message { get; set; }
|
||||
public List<File1> FoundInExcel { get; set; } = new List<File1>();
|
||||
public List<File1> NotFoundInExcel { get; set; } = new List<File1>();
|
||||
public List<ExcelRowData> ExcelData { get; set; } = new List<ExcelRowData>();
|
||||
public bool HasProcessed { get; set; }
|
||||
|
||||
public IndexModel(CompanyContext dbContext, IRollCallDomainService rollCallDomainService)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_rollCallDomainService = rollCallDomainService;
|
||||
}
|
||||
|
||||
public void OnGet()
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostUploadExcel()
|
||||
{
|
||||
if (ExcelFile == null || ExcelFile.Length == 0)
|
||||
{
|
||||
Message = "لطفا یک فایل اکسل انتخاب کنید.";
|
||||
return Page();
|
||||
}
|
||||
|
||||
//await NewMethod();
|
||||
|
||||
// var rollcall = _dbContext.RollCalls.Where(x => x.id > 361628 && x.id < 362119).ToList();
|
||||
//
|
||||
// var diffTime = TimeSpan.FromHours(8) + TimeSpan.FromMinutes(30);
|
||||
// var NoneRollCalls = rollcall
|
||||
// .Where(x => x.RollCallModifyType == RollCallModifyType.None && x.EndDate == null).ToList();
|
||||
// foreach (var noneRollCall in NoneRollCalls)
|
||||
// {
|
||||
// noneRollCall.SetStartDate(noneRollCall.StartDate!.Value.Add(diffTime));
|
||||
// }
|
||||
// var rollcallIds = NoneRollCalls.Select(x => x.id).ToList();
|
||||
// var editedRollCall = rollcall.Where(x=>!rollcallIds.Contains(x.id) && x.RollCallModifyType != RollCallModifyType.EditByEmployer).ToList();
|
||||
//
|
||||
// foreach (var rollCall in editedRollCall)
|
||||
// {
|
||||
// rollCall.Edit(rollCall.StartDate.Value.Add(diffTime),
|
||||
// rollCall.EndDate.Value.Add(diffTime),_rollCallDomainService);
|
||||
// }
|
||||
//
|
||||
// await _dbContext.SaveChangesAsync();
|
||||
return Page();
|
||||
}
|
||||
|
||||
private async System.Threading.Tasks.Task NewMethod()
|
||||
{
|
||||
try
|
||||
{
|
||||
ExcelPackage.License.SetNonCommercialOrganization("Gozareshgir Noncommercial organization");
|
||||
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
await ExcelFile.CopyToAsync(stream);
|
||||
stream.Position = 0;
|
||||
|
||||
using (var package = new ExcelPackage(stream))
|
||||
{
|
||||
var worksheet = package.Workbook.Worksheets[0];
|
||||
int rowCount = worksheet.Dimension?.Rows ?? 0;
|
||||
|
||||
// خواندن مقادیر از ستون B و C از ردیف 2 به بعد
|
||||
for (int row = 2; row <= rowCount; row++)
|
||||
{
|
||||
var columnB = worksheet.Cells[row, 2].Value?.ToString()?.Trim();
|
||||
var columnC = worksheet.Cells[row, 3].Value?.ToString()?.Trim();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(columnB) || !string.IsNullOrWhiteSpace(columnC))
|
||||
{
|
||||
ExcelData.Add(new ExcelRowData
|
||||
{
|
||||
ColumnB = columnB ?? "",
|
||||
ColumnC = columnC ?? ""
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ساخت HashSet برای سرچ سریع - هر جفت (B, C) رو با هم نگه داری
|
||||
var excelPairs = new HashSet<(string B, string C)>(
|
||||
ExcelData.Select(x => (
|
||||
B: (x.ColumnB ?? "").Trim(),
|
||||
C: (x.ColumnC ?? "").Trim()
|
||||
)));
|
||||
|
||||
// دریافت تمام دادههای موجود در دیتابیس
|
||||
var allFiles = _dbContext.Files.ToList();
|
||||
|
||||
// دستهبندی: فایلهایی که در اکسل هستند و نیستند
|
||||
foreach (var file in allFiles)
|
||||
{
|
||||
var fileB = file.ArchiveNo.ToString();
|
||||
var fileC = (file.FileClass ?? "").Trim();
|
||||
|
||||
// بررسی آیا جفت (B, C) این فایل در اکسل وجود دارد
|
||||
bool foundInExcel = excelPairs.Contains((fileB, fileC));
|
||||
|
||||
if (foundInExcel)
|
||||
{
|
||||
FoundInExcel.Add(file);
|
||||
}
|
||||
else
|
||||
{
|
||||
NotFoundInExcel.Add(file);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var file1 in FoundInExcel)
|
||||
{
|
||||
file1.ChangeStatus(2);
|
||||
}
|
||||
|
||||
foreach (var file1 in NotFoundInExcel)
|
||||
{
|
||||
file1.ChangeStatus(1);
|
||||
}
|
||||
|
||||
await _dbContext.SaveChangesAsync();
|
||||
HasProcessed = true;
|
||||
Message = $"پردازش با موفقیت انجام شد. تعداد ردیفهای اکسل: {ExcelData.Count} | فایلهای موجود در اکسل: {FoundInExcel.Count} | فایلهای غیرموجود در اکسل: {NotFoundInExcel.Count}";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Message = $"خطا در پردازش فایل: {ex.Message}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user