From 4bfbf0949b54ff304552ed7b02271083f3639f73 Mon Sep 17 00:00:00 2001 From: zhouhongjun <565221961@qq.com> Date: Tue, 9 Jul 2024 13:47:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=8F=91=E8=BF=90=E3=80=81ed?= =?UTF-8?q?i=E3=80=81=E7=BB=93=E7=AE=97=E6=AF=94=E5=AF=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SettleAccount.HttpApi.Host/Startup.cs | 4 +- .../Entities/BQ/NopiDataToExcel.cs | 178 ++++++++++++- .../Entities/BQ/VmiAppService.cs | 20 +- .../Jobs/TaskJobService.cs | 9 +- .../Enums/EnumWMSSend.cs | 2 +- .../JisBBACSaSeEdiCompareExportService.cs | 54 ++-- .../JisBBACSeEdiCompareExportService.cs | 18 +- .../JisHBPOSaSeEdiCompareExportService.cs | 22 +- .../JisHBPOSeEdiCompareExportService.cs | 18 +- .../Services/Report/NopiDataToExcel.cs | 235 ++++++++++++++++++ .../Report/SaSeEdiCompareExportBaseService.cs | 108 ++++---- .../Report/SeEdiCompareExportBaseService.cs | 76 ++++-- .../SettleAccount.Job.csproj | 1 + 13 files changed, 623 insertions(+), 122 deletions(-) create mode 100644 code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/NopiDataToExcel.cs diff --git a/code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/Startup.cs b/code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/Startup.cs index 75dce71f..442b4d90 100644 --- a/code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/Startup.cs +++ b/code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/Startup.cs @@ -87,8 +87,8 @@ namespace Win.Sfs.SettleAccount app.UseEndpoints(endpoints => endpoints.MapHub("/api/hub")); app.ApplicationServices.UseScheduler(scheduler => { - scheduler.Schedule().EveryMinute(); - scheduler.Schedule().EveryMinute(); + scheduler.Schedule().EveryThirtyMinutes(); + scheduler.Schedule().EveryThirtyMinutes(); scheduler.Schedule().EveryMinute(); }); var contentTypeProvider = new FileExtensionContentTypeProvider(); diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/NopiDataToExcel.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/NopiDataToExcel.cs index 4bcb5073..4c7d5c2f 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/NopiDataToExcel.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/NopiDataToExcel.cs @@ -1,13 +1,187 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.IO.Compression; +using System.IO; using System.Linq; +using System.Reflection; using System.Text; +using System.Threading; using System.Threading.Tasks; +using Magicodes.ExporterAndImporter.Core; +using Minio; +using NPOI.SS.UserModel; +using NPOI.XSSF.UserModel; +using SettleAccount.Bases; +using System.Diagnostics; +using Magicodes.ExporterAndImporter.Core.Extension; namespace Win.Sfs.SettleAccount.Entities.BQ -{ - public class NopiDataToExcel +{ + public class NopiExtendExcel where T : class, new() { + static ConcurrentBag excelFiles = new ConcurrentBag(); + public static string EndPoint { get; set; } + public static string AccessKey { get; set; } + public static string SecretKey { get; set; } + public static string BucketName { get; set; } + public static bool WithSSL { get; set; } + + /// + /// + /// + /// 页签名称 + /// 数据 + /// 缓存文件行数 + public static async Task WriteDataToExcelInParallel(string sheetName, List data, int chunkSize) + { + Stopwatch sw = Stopwatch.StartNew(); + var count = data.Count / 1000000 + ((data.Count % 1000000) > 0 ? 1 : 0); + for (var i = 1; i <= count; i++) + { + var datas = data.Skip((i - 1) * 1000000).Take(1000000).ToList(); + var chunks = datas.ChunkBy(chunkSize); + Parallel.ForEach(chunks, chunk => + { + var fileName = $"{sheetName}_{i - 1}_{Guid.NewGuid()}.xlsx"; + var fileinfogroup = new FileInfoGroup((fileName), $"{sheetName}_{i - 1}"); + WriteDataToExcel(chunk.ToList(), fileName); + excelFiles.Add(fileinfogroup); + }); + } + string[] filePaths = excelFiles.Select(x => x.FileName).ToArray(); + string zipPath = $"{sheetName}_{Guid.NewGuid()}.zip"; + using (var archive = ZipFile.Open(zipPath, ZipArchiveMode.Create)) + { + foreach (var file in filePaths) + { + archive.CreateEntryFromFile(file, Path.GetFileName(file)); + } + } + + //for (var i = 0; i < count; i++) + //{ + // var filelist = excelFiles.Where(p => p.Grpup == $"{sheetName}_{i}").Select(p => p.FileName).ToList(); + // //MergeExcelFiles(filelist.ToArray(), $"{sheetName}.xlsx",filelist.First()); + // //foreach (var file in filelist) + // //{ + // // File.Delete(file); + // //} + //} + sw.Stop(); + + + using (FileStream fileStream = new FileStream(zipPath, FileMode.Open)) + { + var minioClient = new MinioClient(EndPoint, AccessKey, SecretKey, ""); + try + { + await minioClient.PutObjectAsync(BucketName, zipPath, fileStream, fileStream.Length, "application/zip").ConfigureAwait(false); + } + catch (Exception ex) + { + // Console.WriteLine($"上传文件到 MinIO 时发生错误: {ex.Message}"); + } + + } + Thread.Sleep(10000); + + return zipPath; + //using (FileStream fileStream = new FileStream(zipPath, FileMode.Open)) + //{ + // return fileStream.GetAllBytes(); + //} + + } + + + + + public static void WriteDataToExcel(List data, string fileName) + { + IWorkbook workbook = new XSSFWorkbook(); + ISheet sheet = workbook.CreateSheet("Sheet1"); + var firist = data.FirstOrDefault(); + var properties = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty) + .Where(o => o.GetAttributes().Any() || o.GetAttributes().Any()) + .ToDictionary(o => o.GetAttribute()?.DisplayName ?? o.GetAttribute()?.Name, o => o); + int columnCellIndex = 0; + int rowIndex = 1; + IRow headerRow = sheet.CreateRow(0); + foreach (var property in properties) + { + headerRow.CreateCell(columnCellIndex).SetCellValue(property.Key); + columnCellIndex++; + } + foreach (var item in data) + { + IRow row = sheet.CreateRow(rowIndex++); + int cellIndex = 0; + foreach (var property in item.GetType().GetProperties()) + { + ICell cell = row.CreateCell(cellIndex++); + cell.SetCellValue(property.GetValue(item)?.ToString()); + } + } + using (FileStream fileStream = new FileStream(fileName, FileMode.Create)) + { + workbook.Write(fileStream); + } + } + + public static void MergeExcelFiles(string[] filePaths, string outputFilePath, string sheetName) + { + + IWorkbook workbook = new XSSFWorkbook(); + ISheet sheet = workbook.CreateSheet(sheetName); + + int rowIndex = 0; + foreach (string filePath in filePaths) + { + using (FileStream fileStream = new FileStream(filePath, FileMode.Open)) + { + IWorkbook sourceWorkbook = new XSSFWorkbook(fileStream); + ISheet sourceSheet = sourceWorkbook.GetSheetAt(0); + + int sourceRowCount = sourceSheet.LastRowNum; + for (int i = 0; i <= sourceRowCount; i++) + { + IRow sourceRow = sourceSheet.GetRow(i); + if (sourceRow != null) + { + IRow targetRow = sheet.CreateRow(rowIndex++); + int cellCount = sourceRow.LastCellNum; + for (int j = 0; j < cellCount; j++) + { + ICell sourceCell = sourceRow.GetCell(j); + if (sourceCell != null) + { + ICell targetCell = targetRow.CreateCell(j); + targetCell.SetCellValue(sourceCell.ToString()); + } + } + } + } + } + } + + if (File.Exists(outputFilePath)) + { + using (FileStream fileStream = new FileStream(outputFilePath, FileMode.Open, FileAccess.ReadWrite)) + { + workbook.Write(fileStream); + } + } + else + { + using (FileStream fileStream = new FileStream(outputFilePath, FileMode.Create)) + { + workbook.Write(fileStream); + } + } + } } + } diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAppService.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAppService.cs index 15f0b784..7f50f525 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAppService.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAppService.cs @@ -304,22 +304,22 @@ namespace Win.Sfs.SettleAccount.Entities.BQ var dtos = _maper.Map, List>(entities); dtos = dtos.OrderByDescending(p => p.BillTime).ToList(); var fileName = $"库存余额_{DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss")}.xlsx"; - //ExportImporter _exportImporter = new ExportImporter(); - //var result = await _exportImporter.ExcelExporter(dtos).ConfigureAwait(false); - //result.ShouldNotBeNull(); - //await _excelImportService.SaveBlobAsync(new SaveExcelImportInputDto { Name = fileName, Content = result }).ConfigureAwait(false); + ExportImporter _exportImporter = new ExportImporter(); + var result = await _exportImporter.ExcelExporter(dtos).ConfigureAwait(false); + result.ShouldNotBeNull(); + await _excelImportService.SaveBlobAsync(new SaveExcelImportInputDto { Name = fileName, Content = result }).ConfigureAwait(false); - ExtendExcel.BucketName = _cfg.GetValue("MinIO:BucketName", "default"); + //ExtendExcel.BucketName = _cfg.GetValue("MinIO:BucketName", "default"); - ExtendExcel.AccessKey = _cfg.GetValue("MinIO:AccessKey", "g0GKnhRGEQHI0uiGBYre"); + //ExtendExcel.AccessKey = _cfg.GetValue("MinIO:AccessKey", "g0GKnhRGEQHI0uiGBYre"); - ExtendExcel.SecretKey = _cfg.GetValue("MinIO:SecretKey", "iKGlLz6UBzci3xrERw5Zz1gI77enT5u9agFemHPv"); + //ExtendExcel.SecretKey = _cfg.GetValue("MinIO:SecretKey", "iKGlLz6UBzci3xrERw5Zz1gI77enT5u9agFemHPv"); - ExtendExcel.EndPoint = _cfg.GetValue("MinIO:EndPoint", "localhost:10684/host"); + //ExtendExcel.EndPoint = _cfg.GetValue("MinIO:EndPoint", "localhost:10684/host"); - return await ExtendExcel.WriteDataToExcelInParallel("库存余额", dtos, 500000).ConfigureAwait(false); + //return await ExtendExcel.WriteDataToExcelInParallel("库存余额", dtos, 500000).ConfigureAwait(false); - //return fileName; + return fileName; } /// diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Jobs/TaskJobService.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Jobs/TaskJobService.cs index acfa9238..3c91d6b4 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Jobs/TaskJobService.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Jobs/TaskJobService.cs @@ -215,7 +215,14 @@ namespace Win.Sfs.SettleAccount.Entities.TaskJobs str = string.Format("{0}_{1}.{2}", currentApplicationName, Guid.NewGuid().ToString(), "cvs"); break; case ExportExtentsion.Excel: - str = string.Format("{0}_{1}.{2}", currentApplicationName, Guid.NewGuid().ToString(), "xlsx"); + if(currentApplicationName == "JisBBACEDI与发运数据对比" || currentApplicationName == "JisHBPOEDI与发运数据对比" || currentApplicationName == "JisBBAC结算与发运数据对比" || currentApplicationName == "JisHBPO结算与发运数据对比") + { + str = string.Format("{0}_{1}.{2}", currentApplicationName, Guid.NewGuid().ToString(), "zip"); + } + else + { + str = string.Format("{0}_{1}.{2}", currentApplicationName, Guid.NewGuid().ToString(), "xlsx"); + } break; } var _serviceName = moudle.FullName; diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Domain.Shared/Enums/EnumWMSSend.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Domain.Shared/Enums/EnumWMSSend.cs index 88fc1252..f1838ab6 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Domain.Shared/Enums/EnumWMSSend.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Domain.Shared/Enums/EnumWMSSend.cs @@ -10,7 +10,7 @@ namespace Win.Sfs.SettleAccount.Enums /// /// 未定义 /// - [Display(Name = "未定义")] + [Display(Name = "")] None = 0, /// /// WMS多发 diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/JisBBACSaSeEdiCompareExportService.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/JisBBACSaSeEdiCompareExportService.cs index 9773c3b7..b592a689 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/JisBBACSaSeEdiCompareExportService.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/JisBBACSaSeEdiCompareExportService.cs @@ -3,9 +3,12 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Linq.Dynamic.Core; +using System.Threading; using Magicodes.ExporterAndImporter.Excel; using Microsoft.AspNetCore.SignalR; +using Microsoft.Extensions.Configuration; using Microsoft.OpenApi.Extensions; +using Minio.DataModel; using SettleAccount.Domain.BQ; using SettleAccount.Job.SignalR; using Shouldly; @@ -18,6 +21,7 @@ using Win.Sfs.BaseData.ImportExcelCommon; using Win.Sfs.SettleAccount; using Win.Sfs.SettleAccount.Entities.BQ; using Win.Sfs.SettleAccount.Reports; +using Win.Sfs.Shared.DomainBase; namespace SettleAccount.Job.Services.Report { @@ -49,8 +53,9 @@ namespace SettleAccount.Job.Services.Report public JisBBACSaSeEdiCompareExportService( IHubContext hubContext, IBlobContainer fileContainer, + IConfiguration cfg, IObjectMapper objectMapper, - SettleAccountDbContext settleAccountDbContext) : base(hubContext, settleAccountDbContext) + SettleAccountDbContext settleAccountDbContext) : base(hubContext, settleAccountDbContext, cfg) { _fileContainer = fileContainer; _objectMapper = objectMapper; @@ -115,12 +120,13 @@ namespace SettleAccount.Job.Services.Report var excelExporter = new ExcelExporter(); var items = _objectMapper.Map, List>(saSeEdiCompareDiffs); - excelExporter = BindExcelExporter(items, businessTypeDisplayName); + //excelExporter = BindExcelExporter(items, businessTypeDisplayName); - var result = excelExporter.ExportAppendDataAsByteArray(); - result.ShouldNotBeNull(); - _fileContainer.SaveAsync(filename, result.Result, true); + //var result = excelExporter.ExportAppendDataAsByteArray(); + //result.ShouldNotBeNull(); + //_fileContainer.SaveAsync(filename, result.Result, true); + BindExcelExporter(items, businessTypeDisplayName,filename); //比对完成后更新状态 var pubSaEntityList = _settleAccountDbContext.Set().Where(t => t.Version == version && t.BusinessType == businessType).ToList(); foreach (var item in pubSaEntityList) @@ -130,6 +136,7 @@ namespace SettleAccount.Job.Services.Report _settleAccountDbContext.SaveChanges(); + Thread.Sleep(30000); Notify(); return id.ToString(); } @@ -162,7 +169,7 @@ namespace SettleAccount.Job.Services.Report // Price = groupItem.Max(t => t.Price), // }; var saGroup = from sa in _settleAccountDbContext.Set() - where sa.BusinessType == businessType + where sa.BusinessType == businessType && sa.Version == version group sa by new { sa.PN, sa.CustomerPartCodeNoSpace } into groupItem select new { @@ -193,7 +200,7 @@ namespace SettleAccount.Job.Services.Report // ErpToLoc = groupItem.Max(t => t.ErpToLoc) // }).Where(t => t.Qty != 0M); var seGroup = (from se in _settleAccountDbContext.Set() - where se.BusinessType == businessType + where se.BusinessType == businessType && se.State == 0 && se.IsDeleted == false group se by new { se.PN, se.CustomerPartCodeNoSpace } into groupItem select new { @@ -221,7 +228,7 @@ namespace SettleAccount.Job.Services.Report // LU = groupItem.Max(t => t.LU) // }; var ediGroup = from edi in _settleAccountDbContext.Set() - where edi.IsDeleted == false + where edi.IsDeleted == false && edi.State == 0 group edi by new { edi.PN, edi.CustomerPartCodeNoSpace } into groupItem select new { @@ -310,18 +317,31 @@ namespace SettleAccount.Job.Services.Report }; var saSeCompareFullJoin = saSeCompareLeft.Union(saSeCompareRight).ToList(); - var ediCheck = ediGroup.ToList(); - foreach (var item in saSeCompareFullJoin) + //优化 + + var haveSaNotSe = saSeCompareFullJoin.Where(p => !string.IsNullOrEmpty(p.SaCustomerPartCode) && string.IsNullOrEmpty(p.SeCustomerPartCode)).ToList(); + var ediCheck = ediGroup.ToList().ToDictionary(item=>item.PN+item.LU,item=>item.Qty); + foreach (var item in haveSaNotSe) { - if (!string.IsNullOrEmpty(item.SaCustomerPartCode) && string.IsNullOrEmpty(item.SeCustomerPartCode)) + + //var ediEntity = ediCheck.FirstOrDefault(p => p.PN == item.PN && p.CustomerPartCodeNoSpace == item.SaCustomerPartCode); + //if (ediEntity != null) + //{ + // item.EdiCustomerPartCode = item.SaCustomerPartCode; + // item.EdiQty = ediEntity.Qty; + //} + + if(ediCheck.TryGetValue(item.PN + item.SaCustomerPartCode,out var v)) { - var ediEntity = ediCheck.FirstOrDefault(p => p.PN == item.PN && p.CustomerPartCodeNoSpace == item.SaCustomerPartCode); - if (ediEntity != null) - { - item.EdiCustomerPartCode = item.SaCustomerPartCode; - item.EdiQty = ediEntity.Qty; - } + item.EdiCustomerPartCode = item.SaCustomerPartCode; + item.EdiQty = v; } + //var ediHave = ediCheck.ContainsKey(item.PN + item.SaCustomerPartCode); + //if (ediHave) + //{ + // item.EdiCustomerPartCode = item.SaCustomerPartCode; + // item.EdiQty = ediCheck.FirstOrDefault(p=>p.Key==item.PN + item.SaCustomerPartCode).Value; + //} } diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/JisBBACSeEdiCompareExportService.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/JisBBACSeEdiCompareExportService.cs index 2a08da22..f65fda57 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/JisBBACSeEdiCompareExportService.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/JisBBACSeEdiCompareExportService.cs @@ -2,7 +2,9 @@ using System; using System.Collections.Generic; using System.Linq; using System.Linq.Dynamic.Core; +using System.Threading; using Microsoft.AspNetCore.SignalR; +using Microsoft.Extensions.Configuration; using SettleAccount.Domain.BQ; using SettleAccount.Job.SignalR; using Shouldly; @@ -32,9 +34,9 @@ namespace SettleAccount.Job.Services.Report /// public JisBBACSeEdiCompareExportService( IHubContext hubContext, - IBlobContainer fileContainer, + IBlobContainer fileContainer, IConfiguration cfg, IObjectMapper objectMapper, - SettleAccountDbContext settleAccountDbContext) : base(settleAccountDbContext, hubContext) + SettleAccountDbContext settleAccountDbContext) : base(settleAccountDbContext, hubContext, cfg) { _fileContainer = fileContainer; } @@ -54,10 +56,14 @@ namespace SettleAccount.Job.Services.Report var seEndDateTime = DateTime.Parse(strSeEndDateTime); var filename = exportName.FirstOrDefault(); - var excelExporter = GetSeEdiCompareData(seStartDateTime, seEndDateTime, EnumBusinessType.JisBBAC); - var result = excelExporter.ExportAppendDataAsByteArray(); - result.ShouldNotBeNull(); - _fileContainer.SaveAsync(filename, result.Result, true); + //var excelExporter = GetSeEdiCompareData(seStartDateTime, seEndDateTime, EnumBusinessType.JisBBAC); + //var result = excelExporter.ExportAppendDataAsByteArray(); + //result.ShouldNotBeNull(); + //_fileContainer.SaveAsync(filename, result.Result, true); + + GetSeEdiCompareData(seStartDateTime, seEndDateTime, EnumBusinessType.JisBBAC, filename); + + Thread.Sleep(30000); Notify(); return id.ToString(); diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/JisHBPOSaSeEdiCompareExportService.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/JisHBPOSaSeEdiCompareExportService.cs index 56fd4e77..ca8a43a3 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/JisHBPOSaSeEdiCompareExportService.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/JisHBPOSaSeEdiCompareExportService.cs @@ -3,8 +3,10 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.IO; using System.Linq; +using System.Threading; using Magicodes.ExporterAndImporter.Excel; using Microsoft.AspNetCore.SignalR; +using Microsoft.Extensions.Configuration; using Microsoft.OpenApi.Extensions; using NPOI.HPSF; using NPOI.SS.UserModel; @@ -53,8 +55,9 @@ namespace SettleAccount.Job.Services.Report public JisHBPOSaSeEdiCompareExportService( IHubContext hubContext, IBlobContainer fileContainer, + IConfiguration cfg, IObjectMapper objectMapper, - SettleAccountDbContext settleAccountDbContext) : base(hubContext, settleAccountDbContext) + SettleAccountDbContext settleAccountDbContext) : base(hubContext, settleAccountDbContext,cfg) { _fileContainer = fileContainer; _objectMapper = objectMapper; @@ -114,19 +117,13 @@ namespace SettleAccount.Job.Services.Report var excelExporter = new ExcelExporter(); var items = _objectMapper.Map, List>(saSeEdiCompareDiffs); + //excelExporter = BindExcelExporter(items, businessTypeDisplayName); + //var result = excelExporter.ExportAppendDataAsByteArray(); + //result.ShouldNotBeNull(); + //_fileContainer.SaveAsync(filename, result.Result, true); - - - - - - excelExporter = BindExcelExporter(items, businessTypeDisplayName); - - var result = excelExporter.ExportAppendDataAsByteArray(); - result.ShouldNotBeNull(); - _fileContainer.SaveAsync(filename, result.Result, true); - + BindExcelExporter(items, businessTypeDisplayName,filename); //比对完成后更新状态 var pubSaEntityList = _settleAccountDbContext.Set().Where(t => t.Version == version && t.BusinessType == businessType).ToList(); foreach (var item in pubSaEntityList) @@ -136,6 +133,7 @@ namespace SettleAccount.Job.Services.Report _settleAccountDbContext.SaveChanges(); + Thread.Sleep(30000); Notify(); return id.ToString(); } diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/JisHBPOSeEdiCompareExportService.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/JisHBPOSeEdiCompareExportService.cs index 6e497e20..ac62f65f 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/JisHBPOSeEdiCompareExportService.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/JisHBPOSeEdiCompareExportService.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading; using Microsoft.AspNetCore.SignalR; +using Microsoft.Extensions.Configuration; using SettleAccount.Domain.BQ; using SettleAccount.Job.SignalR; using Shouldly; @@ -30,8 +32,8 @@ namespace SettleAccount.Job.Services.Report /// public JisHBPOSeEdiCompareExportService( IHubContext hubContext, - IBlobContainer fileContainer, - SettleAccountDbContext settleAccountDbContext) : base(settleAccountDbContext, hubContext) + IBlobContainer fileContainer, IConfiguration cfg, + SettleAccountDbContext settleAccountDbContext) : base(settleAccountDbContext, hubContext, cfg) { _fileContainer = fileContainer; } @@ -51,10 +53,14 @@ namespace SettleAccount.Job.Services.Report var seEndDateTime = DateTime.Parse(strSeEndDateTime); var filename = exportName.FirstOrDefault(); - var excelExporter = GetSeEdiCompareData(seStartDateTime, seEndDateTime, EnumBusinessType.JisHBPO); - var result = excelExporter.ExportAppendDataAsByteArray(); - result.ShouldNotBeNull(); - _fileContainer.SaveAsync(filename, result.Result, true); + //var excelExporter = GetSeEdiCompareData(seStartDateTime, seEndDateTime, EnumBusinessType.JisHBPO); + //var result = excelExporter.ExportAppendDataAsByteArray(); + //result.ShouldNotBeNull(); + //_fileContainer.SaveAsync(filename, result.Result, true); + + GetSeEdiCompareData(seStartDateTime, seEndDateTime, EnumBusinessType.JisHBPO, filename); + + Thread.Sleep(30000); Notify(); return id.ToString(); diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/NopiDataToExcel.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/NopiDataToExcel.cs new file mode 100644 index 00000000..19c8b667 --- /dev/null +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/NopiDataToExcel.cs @@ -0,0 +1,235 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics; +using System.IO.Compression; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Magicodes.ExporterAndImporter.Core.Extension; +using Magicodes.ExporterAndImporter.Core; +using NPOI.SS.UserModel; +using NPOI.XSSF.UserModel; +using SettleAccount.Bases; +using Minio; +using Win.Sfs.SettleAccount.Reports; + +namespace SettleAccount.Job.Services.Report +{ + public class NopiExtendExcel where T : class, new() + { + + ConcurrentBag excelFiles = new ConcurrentBag(); + public string EndPoint { get; set; } + public string AccessKey { get; set; } + public string SecretKey { get; set; } + public string BucketName { get; set; } + public bool WithSSL { get; set; } + + /// + /// + /// + /// 页签名称 + /// 数据 + /// 缓存文件行数 + public async Task WriteDataToExcelInParallel(string sheetName, List data, int chunkSize,string zipFileName, List sumdata) + { + var count = data.Count / 1000000 + ((data.Count % 1000000) > 0 ? 1 : 0); + for (var i = 1; i <= count; i++) + { + var datas = data.Skip((i - 1) * 1000000).Take(1000000).ToList(); + var chunks = datas.ChunkBy(chunkSize); + Parallel.ForEach(chunks, chunk => + { + var fileName = $"{sheetName}_{Guid.NewGuid()}.xlsx"; + var fileinfogroup = new FileInfoGroup((fileName), $"{sheetName}_{i}"); + WriteDataToExcel(chunk.ToList(), fileName); + excelFiles.Add(fileinfogroup); + }); + } + if(sumdata!=null) + { + var sumFileName = $"{sheetName}_汇总_{Guid.NewGuid()}.xlsx"; + var fileinfogroup = new FileInfoGroup((sumFileName), $"{sheetName}_汇总"); + WriteDataToExcel(sumdata, sumFileName); + excelFiles.Add(fileinfogroup); + + } + + string[] filePaths = excelFiles.Select(x => x.FileName).ToArray(); + string zipPath = zipFileName; + using (var archive = ZipFile.Open(zipPath, ZipArchiveMode.Create)) + { + foreach (var file in filePaths) + { + archive.CreateEntryFromFile(file, Path.GetFileName(file)); + } + } + + for (var i = 0; i < count; i++) + { + var filelist = excelFiles.Select(p => p.FileName).ToList(); + + foreach (var file in filelist) + { + File.Delete(file); + } + } + + using (FileStream fileStream = new FileStream(zipPath, FileMode.Open)) + { + var minioClient = new MinioClient(EndPoint, AccessKey, SecretKey, ""); + try + { + await minioClient.PutObjectAsync(BucketName, "host/"+zipPath, fileStream, fileStream.Length, "application/zip").ConfigureAwait(false); + + + } + catch (Exception ex) + { + Console.WriteLine($"上传文件到 MinIO 时发生错误: {ex.Message}"); + } + finally + { + if (fileStream != null) + { + fileStream.Close(); + File.Delete(zipPath); + } + + } + + } + //using (FileStream fileStream = new FileStream(zipPath, FileMode.Open)) + //{ + // return fileStream.GetAllBytes(); + //} + return zipFileName; + } + + public void WriteDataToExcel(List data, string fileName) + { + IWorkbook workbook = new XSSFWorkbook(); + ISheet sheet = workbook.CreateSheet("数据"); + var firist = data.FirstOrDefault(); + var properties = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty) + .Where(o => o.GetAttributes().Any() || o.GetAttributes().Any()) + .ToDictionary(o => o.GetAttribute()?.DisplayName ?? o.GetAttribute()?.Name, o => o); + int columnCellIndex = 0; + int rowIndex = 1; + IRow headerRow = sheet.CreateRow(0); + foreach (var property in properties) + { + headerRow.CreateCell(columnCellIndex).SetCellValue(property.Key); + sheet.AutoSizeColumn(columnCellIndex); + columnCellIndex++; + + } + foreach (var item in data) + { + IRow row = sheet.CreateRow(rowIndex++); + int cellIndex = 0; + foreach (var property in item.GetType().GetProperties()) + { + ICell cell = row.CreateCell(cellIndex++); + + var propertyType = property.PropertyType; + if (propertyType == typeof(bool)) + { + cell.SetCellValue((bool)property.GetValue(item)! ? "是" : "否"); + } + else if (propertyType.IsEnum) + { + var value = property.GetValue(item)?.ToString()?.Trim(); + var enumValue = (System.Enum.Parse(propertyType, value) as System.Enum); + var type = enumValue.GetType(); + var field = type.GetField(enumValue.ToString()); + + cell.SetCellValue(field.GetCustomAttribute()?.Name ?? field.Name); + + } + else + { + cell.SetCellValue(property.GetValue(item)?.ToString()); + } + + + } + } + using (FileStream fileStream = new FileStream(fileName, FileMode.Create)) + { + workbook.Write(fileStream); + } + } + + public void MergeExcelFiles(string[] filePaths, string outputFilePath, string sheetName) + { + + IWorkbook workbook = new XSSFWorkbook(); + ISheet sheet = workbook.CreateSheet(sheetName); + + int rowIndex = 0; + foreach (string filePath in filePaths) + { + using (FileStream fileStream = new FileStream(filePath, FileMode.Open)) + { + IWorkbook sourceWorkbook = new XSSFWorkbook(fileStream); + ISheet sourceSheet = sourceWorkbook.GetSheetAt(0); + + int sourceRowCount = sourceSheet.LastRowNum; + for (int i = 0; i <= sourceRowCount; i++) + { + IRow sourceRow = sourceSheet.GetRow(i); + if (sourceRow != null) + { + IRow targetRow = sheet.CreateRow(rowIndex++); + int cellCount = sourceRow.LastCellNum; + for (int j = 0; j < cellCount; j++) + { + ICell sourceCell = sourceRow.GetCell(j); + if (sourceCell != null) + { + ICell targetCell = targetRow.CreateCell(j); + targetCell.SetCellValue(sourceCell.ToString()); + } + } + } + } + } + } + + if (File.Exists(outputFilePath)) + { + using (FileStream fileStream = new FileStream(outputFilePath, FileMode.Open, FileAccess.ReadWrite)) + { + workbook.Write(fileStream); + } + } + else + { + using (FileStream fileStream = new FileStream(outputFilePath, FileMode.Create)) + { + workbook.Write(fileStream); + } + } + } + } + + public class FileInfoGroup + { + public FileInfoGroup(string fileName, string grpup) + { + FileName = fileName; + Grpup = grpup; + } + + public string FileName { get; set; } + public string Grpup { get; set; } + } + + +} diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/SaSeEdiCompareExportBaseService.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/SaSeEdiCompareExportBaseService.cs index c88f3af0..d83e50e5 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/SaSeEdiCompareExportBaseService.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/SaSeEdiCompareExportBaseService.cs @@ -5,6 +5,8 @@ using EFCore.BulkExtensions; using Magicodes.ExporterAndImporter.Core.Extension; using Magicodes.ExporterAndImporter.Excel; using Microsoft.AspNetCore.SignalR; +using Microsoft.Extensions.Configuration; +using NPOI.HPSF; using SettleAccount.Bases; using SettleAccount.Domain.BQ; using SettleAccount.Job.SignalR; @@ -29,10 +31,13 @@ namespace SettleAccount.Job.Services.Report /// private readonly SettleAccountDbContext _settleAccountDbContext; - public SaSeEdiCompareExportBaseService(IHubContext hubContext, SettleAccountDbContext settleAccountDbContext) + private readonly IConfiguration _cfg; + + public SaSeEdiCompareExportBaseService(IHubContext hubContext, SettleAccountDbContext settleAccountDbContext, IConfiguration cfg) { _hubContext = hubContext; _settleAccountDbContext = settleAccountDbContext; + this._cfg = cfg; } /// @@ -170,6 +175,7 @@ namespace SettleAccount.Job.Services.Report { o.ErpLoc = i.ToErpLocCode; o.RealPartCode = i.ReplaceFactoryPartCode; + o.PartCode = i.ReplaceFactoryPartCode; return o; }).ToList(); _settleAccountDbContext.BulkUpdate(canSaDetailsUpdate); @@ -185,6 +191,7 @@ namespace SettleAccount.Job.Services.Report { o.ErpLoc = i.ToErpLocCode; o.RealPartCode = i.ReplaceFactoryPartCode; + o.PartCode = i.ReplaceFactoryPartCode; return o; }).ToList(); _settleAccountDbContext.BulkUpdate(notSaDetailsUpdate); @@ -233,7 +240,7 @@ namespace SettleAccount.Job.Services.Report /// /// 根据单Sheel最大行数配置分Sheel页导出 /// - public ExcelExporter BindExcelExporter(List saSeEdiCompareDetailExports, string businessTypeDisplayName) where T : SaSeEdiCompareDetailReport, new() + public string BindExcelExporter(List saSeEdiCompareDetailExports, string businessTypeDisplayName,string fileName) where T : SaSeEdiCompareDetailReport, new() { //详情Sheet行数 var detailMaxRowNumberOnASheet = AppConst.DefaultRowNumberOnASheet; @@ -299,60 +306,65 @@ namespace SettleAccount.Job.Services.Report EdiQty = p.Sum(t => t.EdiQty) }).ToList(); - var excelExporter = new ExcelExporter(); - //有结算无发运 - excelExporter.Append(haveSaNotHaveSeExport.Take(detailMaxRowNumberOnASheet).ToList(), $"{businessTypeDisplayName}有结算无发运"); - for (var i = 1; i < haveSaNotHaveSeExport.Count / detailMaxRowNumberOnASheet + ((haveSaNotHaveSeExport.Count % detailMaxRowNumberOnASheet) > 0 ? 1 : 0); i++) - { - var sheetDataItems = haveSaNotHaveSeExport.Skip(i * detailMaxRowNumberOnASheet).Take(detailMaxRowNumberOnASheet).ToList(); - excelExporter.Append(sheetDataItems, $"{businessTypeDisplayName}有结算无发运-{i}"); - } - //无结算有发运 - excelExporter.Append(notHaveSaHaveSeExports.Take(detailMaxRowNumberOnASheet).ToList(), $"{businessTypeDisplayName}无结算有发运"); - for (var i = 1; i < notHaveSaHaveSeExports.Count / detailMaxRowNumberOnASheet + ((notHaveSaHaveSeExports.Count % detailMaxRowNumberOnASheet) > 0 ? 1 : 0); i++) - { - var sheetDataItems = notHaveSaHaveSeExports.Skip(i * detailMaxRowNumberOnASheet).Take(detailMaxRowNumberOnASheet).ToList(); - excelExporter.Append(sheetDataItems, $"{businessTypeDisplayName}无结算有发运-{i}"); - } - ////结算核对汇总输出 - //excelExporter.Append(saSeEdiCompareSumExports.Take(sumMaxRowNumberOnASheet).ToList(), $"{businessTypeDisplayName}结算核对汇总输出"); - //for (var i = 1; i < saSeEdiCompareSumExports.Count / sumMaxRowNumberOnASheet + ((saSeEdiCompareSumExports.Count % sumMaxRowNumberOnASheet) > 0 ? 1 : 0); i++) + #region 多线程生成Excel + + haveSaNotHaveSeExport.AddRange(notHaveSaHaveSeExports); + + haveSaNotHaveSeExport.AddRange(haveSaHaveSeExport); + + NopiExtendExcel nopiExcel = new NopiExtendExcel(); + + nopiExcel.BucketName = _cfg.GetValue("MinIO:BucketName", "default"); + + nopiExcel.AccessKey = _cfg.GetValue("MinIO:AccessKey", "g0GKnhRGEQHI0uiGBYre"); + + nopiExcel.SecretKey = _cfg.GetValue("MinIO:SecretKey", "iKGlLz6UBzci3xrERw5Zz1gI77enT5u9agFemHPv"); + + nopiExcel.EndPoint = _cfg.GetValue("MinIO:EndPoint", "localhost:10684"); + + var uploadResult = nopiExcel.WriteDataToExcelInParallel($"{businessTypeDisplayName}结算数据和发货对比", haveSaNotHaveSeExport, 500000, fileName, null).ConfigureAwait(false); + + return fileName; + #endregion + + #region 单线程生成Excel + + //var excelExporter = new ExcelExporter(); + ////有结算无发运 + //excelExporter.Append(haveSaNotHaveSeExport.Take(detailMaxRowNumberOnASheet).ToList(), $"{businessTypeDisplayName}有结算无发运"); + //for (var i = 1; i < haveSaNotHaveSeExport.Count / detailMaxRowNumberOnASheet + ((haveSaNotHaveSeExport.Count % detailMaxRowNumberOnASheet) > 0 ? 1 : 0); i++) //{ - // var sheetDataItems = saSeEdiCompareSumExports.Skip(i * sumMaxRowNumberOnASheet).Take(sumMaxRowNumberOnASheet).ToList(); ; - // excelExporter.Append(sheetDataItems, $"{businessTypeDisplayName}结算核对汇总输出-{i}"); + // var sheetDataItems = haveSaNotHaveSeExport.Skip(i * detailMaxRowNumberOnASheet).Take(detailMaxRowNumberOnASheet).ToList(); + // excelExporter.Append(sheetDataItems, $"{businessTypeDisplayName}有结算无发运-{i}"); + //} + ////无结算有发运 + //excelExporter.Append(notHaveSaHaveSeExports.Take(detailMaxRowNumberOnASheet).ToList(), $"{businessTypeDisplayName}无结算有发运"); + //for (var i = 1; i < notHaveSaHaveSeExports.Count / detailMaxRowNumberOnASheet + ((notHaveSaHaveSeExports.Count % detailMaxRowNumberOnASheet) > 0 ? 1 : 0); i++) + //{ + // var sheetDataItems = notHaveSaHaveSeExports.Skip(i * detailMaxRowNumberOnASheet).Take(detailMaxRowNumberOnASheet).ToList(); + // excelExporter.Append(sheetDataItems, $"{businessTypeDisplayName}无结算有发运-{i}"); //} - //有结算有发货明细输出 - excelExporter.Append(haveSaHaveSeExport.Take(detailMaxRowNumberOnASheet).ToList(), $"{businessTypeDisplayName}有结算有发货明细输出"); - for (var i = 1; i < haveSaHaveSeExport.Count / detailMaxRowNumberOnASheet + ((haveSaHaveSeExport.Count % detailMaxRowNumberOnASheet) > 0 ? 1 : 0); i++) - { - var sheetDataItems = haveSaHaveSeExport.Skip(i * detailMaxRowNumberOnASheet).Take(detailMaxRowNumberOnASheet).ToList(); - excelExporter.Append(sheetDataItems, $"{businessTypeDisplayName}有结算有发货明细输出-{i}"); - } - //有结算有发货汇总输出 - excelExporter.Append(haveSaHaveSeSumExports.Take(sumMaxRowNumberOnASheet).ToList(), $"{businessTypeDisplayName}有结算有发货汇总输出"); - for (var i = 1; i < haveSaHaveSeSumExports.Count / sumMaxRowNumberOnASheet + ((haveSaHaveSeSumExports.Count % sumMaxRowNumberOnASheet) > 0 ? 1 : 0); i++) - { - var sheetDataItems = haveSaHaveSeSumExports.Skip(i * sumMaxRowNumberOnASheet).Take(sumMaxRowNumberOnASheet).ToList(); - excelExporter.Append(sheetDataItems, $"{businessTypeDisplayName}有结算有发货汇总输出-{i}"); - } - - ////有结算无发货明细输出 - //excelExporter.Append(haveSaNotHaveSeExports.Take(detailMaxRowNumberOnASheet).ToList(), $"{businessTypeDisplayName}有结算无发货明细输出"); - //for (var i = 1; i < haveSaNotHaveSeExports.Count / detailMaxRowNumberOnASheet + ((haveSaNotHaveSeExports.Count % detailMaxRowNumberOnASheet) > 0 ? 1 : 0); i++) + ////有结算有发货明细输出 + //excelExporter.Append(haveSaHaveSeExport.Take(detailMaxRowNumberOnASheet).ToList(), $"{businessTypeDisplayName}有结算有发货明细输出"); + //for (var i = 1; i < haveSaHaveSeExport.Count / detailMaxRowNumberOnASheet + ((haveSaHaveSeExport.Count % detailMaxRowNumberOnASheet) > 0 ? 1 : 0); i++) //{ - // var sheetDataItems = haveSaNotHaveSeExports.Skip(i * detailMaxRowNumberOnASheet).Take(detailMaxRowNumberOnASheet).ToList(); - // excelExporter.Append(sheetDataItems, $"{businessTypeDisplayName}有结算无发货明细输出-{i}"); + // var sheetDataItems = haveSaHaveSeExport.Skip(i * detailMaxRowNumberOnASheet).Take(detailMaxRowNumberOnASheet).ToList(); + // excelExporter.Append(sheetDataItems, $"{businessTypeDisplayName}有结算有发货明细输出-{i}"); //} - ////有结算无发货汇总输出 - //excelExporter.Append(haveSaNotHaveSeSumExports.Take(sumMaxRowNumberOnASheet).ToList(), $"{businessTypeDisplayName}有结算无发货汇总输出"); - //for (var i = 1; i < haveSaNotHaveSeSumExports.Count / sumMaxRowNumberOnASheet + ((haveSaNotHaveSeSumExports.Count % sumMaxRowNumberOnASheet) > 0 ? 1 : 0); i++) + ////有结算有发货汇总输出 + //excelExporter.Append(haveSaHaveSeSumExports.Take(sumMaxRowNumberOnASheet).ToList(), $"{businessTypeDisplayName}有结算有发货汇总输出"); + //for (var i = 1; i < haveSaHaveSeSumExports.Count / sumMaxRowNumberOnASheet + ((haveSaHaveSeSumExports.Count % sumMaxRowNumberOnASheet) > 0 ? 1 : 0); i++) //{ - // var sheetDataItems = haveSaNotHaveSeSumExports.Skip(i * sumMaxRowNumberOnASheet).Take(sumMaxRowNumberOnASheet).ToList(); - // excelExporter.Append(sheetDataItems, $"{businessTypeDisplayName}有结算无发货汇总输出-{i}"); + // var sheetDataItems = haveSaHaveSeSumExports.Skip(i * sumMaxRowNumberOnASheet).Take(sumMaxRowNumberOnASheet).ToList(); + // excelExporter.Append(sheetDataItems, $"{businessTypeDisplayName}有结算有发货汇总输出-{i}"); //} - return excelExporter; + //return excelExporter; + + #endregion + + } /// diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/SeEdiCompareExportBaseService.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/SeEdiCompareExportBaseService.cs index 99a27aa1..6b4925c8 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/SeEdiCompareExportBaseService.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/SeEdiCompareExportBaseService.cs @@ -23,6 +23,11 @@ using Win.Sfs.SettleAccount; using Win.Sfs.SettleAccount.Consts; using Win.Sfs.SettleAccount.Entities.BQ.Syncs; using Win.Sfs.SettleAccount.Reports; +using NPOI.POIFS.Properties; +using Shouldly; +using Win.Sfs.BaseData.ImportExcelCommon; +using static Microsoft.Extensions.Logging.EventSource.LoggingEventSource; +using Microsoft.Extensions.Configuration; namespace SettleAccount.Job.Services.Report { @@ -40,10 +45,13 @@ namespace SettleAccount.Job.Services.Report /// private readonly IHubContext _hubContext; - public SeEdiCompareExportBaseService(SettleAccountDbContext settleAccountDbContext, IHubContext hubContext) + private readonly IConfiguration _cfg; + + public SeEdiCompareExportBaseService(SettleAccountDbContext settleAccountDbContext, IHubContext hubContext, IConfiguration cfg) { _settleAccountDbContext = settleAccountDbContext; _hubContext = hubContext; + this._cfg = cfg; } #region 方法 @@ -56,7 +64,7 @@ namespace SettleAccount.Job.Services.Report /// 检索时间段内有Edi有发运的数据 /// 根据导出Excel配置的最大行数分Sheel /// - public ExcelExporter GetSeEdiCompareData(DateTime seStartDateTime, DateTime seEndDateTime, EnumBusinessType businessType) + public string GetSeEdiCompareData(DateTime seStartDateTime, DateTime seEndDateTime, EnumBusinessType businessType,string fileName) where TSe : JisSeBase where TEdi : SE_EDI where TCompareReport : SeEidCompareReport, new() @@ -208,21 +216,45 @@ namespace SettleAccount.Job.Services.Report //生成对比Excel文件 var excelExporter = new ExcelExporter(); - // EDI数据和发货对比 - excelExporter.Append(haveEdiNotHaveSeList.Take(maxRowNumberOnASheet).ToList(), $"{businessTypeDisplayName}EDI数据和发货对比"); - for (var i = 1; i < haveEdiNotHaveSeList.Count / maxRowNumberOnASheet + ((haveEdiNotHaveSeList.Count % maxRowNumberOnASheet) > 0 ? 1 : 0); i++) - { - var sheetDataItems = haveEdiNotHaveSeList.Skip(i * maxRowNumberOnASheet).Take(maxRowNumberOnASheet).ToList(); - excelExporter.Append(sheetDataItems, $"{businessTypeDisplayName}EDI数据和发货对比-{i}"); - } - // 发货和EDI数据对比 - excelExporter.Append(notHaveEdiHaveSeList.Take(maxRowNumberOnASheet).ToList(), $"{businessTypeDisplayName}发货和EDI数据对比"); - for (var i = 1; i < notHaveEdiHaveSeList.Count / maxRowNumberOnASheet + ((haveEdiNotHaveSeList.Count % maxRowNumberOnASheet) > 0 ? 1 : 0); i++) - { - var sheetDataItems = notHaveEdiHaveSeList.Skip(i * maxRowNumberOnASheet).Take(maxRowNumberOnASheet).ToList(); - excelExporter.Append(sheetDataItems, $"{businessTypeDisplayName}发货和EDI数据对比-{i}"); - } - return excelExporter; + + #region 多线程生成Excel + //var pageCount = CalculatePageCount(haveEdiNotHaveSeList.Count, maxRowNumberOnASheet); + + haveEdiNotHaveSeList.AddRange(notHaveEdiHaveSeList); + + NopiExtendExcel nopiExcelHaveEdiNotHaveSe = new NopiExtendExcel(); + + nopiExcelHaveEdiNotHaveSe.BucketName = _cfg.GetValue("MinIO:BucketName", "default"); + + nopiExcelHaveEdiNotHaveSe.AccessKey = _cfg.GetValue("MinIO:AccessKey", "g0GKnhRGEQHI0uiGBYre"); + + nopiExcelHaveEdiNotHaveSe.SecretKey = _cfg.GetValue("MinIO:SecretKey", "iKGlLz6UBzci3xrERw5Zz1gI77enT5u9agFemHPv"); + + nopiExcelHaveEdiNotHaveSe.EndPoint = _cfg.GetValue("MinIO:EndPoint", "localhost:10684"); + + var uploadResult = nopiExcelHaveEdiNotHaveSe.WriteDataToExcelInParallel($"{businessTypeDisplayName}EDI数据和发货对比", haveEdiNotHaveSeList, 500000,fileName,null).ConfigureAwait(false); + + return fileName; + + + #endregion + + //// EDI数据和发货对比 + //excelExporter.Append(haveEdiNotHaveSeList.Take(maxRowNumberOnASheet).ToList(), $"{businessTypeDisplayName}EDI数据和发货对比"); + //for (var i = 1; i < haveEdiNotHaveSeList.Count / maxRowNumberOnASheet + ((haveEdiNotHaveSeList.Count % maxRowNumberOnASheet) > 0 ? 1 : 0); i++) + //{ + // var sheetDataItems = haveEdiNotHaveSeList.Skip(i * maxRowNumberOnASheet).Take(maxRowNumberOnASheet).ToList(); + // excelExporter.Append(sheetDataItems, $"{businessTypeDisplayName}EDI数据和发货对比-{i}"); + //} + //// 发货和EDI数据对比 + //excelExporter.Append(notHaveEdiHaveSeList.Take(maxRowNumberOnASheet).ToList(), $"{businessTypeDisplayName}发货和EDI数据对比"); + //for (var i = 1; i < notHaveEdiHaveSeList.Count / maxRowNumberOnASheet + ((haveEdiNotHaveSeList.Count % maxRowNumberOnASheet) > 0 ? 1 : 0); i++) + //{ + // var sheetDataItems = notHaveEdiHaveSeList.Skip(i * maxRowNumberOnASheet).Take(maxRowNumberOnASheet).ToList(); + // excelExporter.Append(sheetDataItems, $"{businessTypeDisplayName}发货和EDI数据对比-{i}"); + //} + + //return excelExporter; } /// @@ -233,6 +265,16 @@ namespace SettleAccount.Job.Services.Report _hubContext.Clients.All.ServerToClient("SaSeCompare", "refresh", ""); } + private int CalculatePageCount(int totalCount, int pageSize) + { + int pageCount = totalCount / pageSize; + if (totalCount % pageSize != 0) + { + pageCount += 1; + } + return pageCount; + } + /// /// 获取生产码日期 /// diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Job/SettleAccount.Job.csproj b/code/src/Modules/SettleAccount/src/SettleAccount.Job/SettleAccount.Job.csproj index 46106c17..67a931c0 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Job/SettleAccount.Job.csproj +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Job/SettleAccount.Job.csproj @@ -16,6 +16,7 @@ +