|
@ -82,7 +82,7 @@ public class VmiAppService : Controller, IApplicationService, IJobService, ITran |
|
|
{ |
|
|
{ |
|
|
using var ms = new MemoryStream(); |
|
|
using var ms = new MemoryStream(); |
|
|
await files.FirstOrDefault().OpenReadStream().CopyToAsync(ms).ConfigureAwait(false); |
|
|
await files.FirstOrDefault().OpenReadStream().CopyToAsync(ms).ConfigureAwait(false); |
|
|
return this.ImportInternal<PUB_ADJ_DETAIL_IMP_DTO>(ms.ToArray()); |
|
|
return this.ImportInternal<PUB_ADJ_DETAIL_IMP_DTO>(ms.ToArray(), out var validationResults); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
@ -367,30 +367,63 @@ public class VmiAppService : Controller, IApplicationService, IJobService, ITran |
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
/// <param name="files"></param>
|
|
|
/// <param name="files"></param>
|
|
|
[HttpPost] |
|
|
[HttpPost] |
|
|
public async Task Import(List<IFormFile> files) |
|
|
public async Task<IActionResult> Import(List<IFormFile> files) |
|
|
|
|
|
{ |
|
|
|
|
|
try |
|
|
{ |
|
|
{ |
|
|
using var ms = new MemoryStream(); |
|
|
using var ms = new MemoryStream(); |
|
|
await files.FirstOrDefault().OpenReadStream().CopyToAsync(ms).ConfigureAwait(false); |
|
|
var file = files.FirstOrDefault(); |
|
|
var list = this.ImportInternal<VmiLog>(ms.ToArray()); |
|
|
await file.OpenReadStream().CopyToAsync(ms).ConfigureAwait(false); |
|
|
foreach (var file in list) |
|
|
var data = ms.ToArray(); |
|
|
|
|
|
var list = this.ImportInternal<VmiLog>(data, out var validationResults); |
|
|
|
|
|
if (validationResults.Any(o => o.Count > 0)) |
|
|
|
|
|
{ |
|
|
|
|
|
using var workbook = new XLWorkbook(new MemoryStream(data)); |
|
|
|
|
|
var ws = workbook.Worksheets.FirstOrDefault(); |
|
|
|
|
|
var header = ws.Row(1); |
|
|
|
|
|
var errorIndex = ws.ColumnsUsed().Count(); |
|
|
|
|
|
header.Cell(errorIndex).Value = "提示信息"; |
|
|
|
|
|
for (int i = 2; i < ws.RowsUsed().Count(); i++) |
|
|
|
|
|
{ |
|
|
|
|
|
ws.Row(2).Cell(errorIndex).Value = string.Join(',', validationResults[i - 2].Select(o => o.ErrorMessage)); |
|
|
|
|
|
} |
|
|
|
|
|
SetStyle(ws); |
|
|
|
|
|
using var stream = new MemoryStream(); |
|
|
|
|
|
workbook.SaveAs(stream); |
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin); |
|
|
|
|
|
var fileName = $"{file.Name}_错误信息.xlsx"; |
|
|
|
|
|
await this._fileContainer.SaveAsync(fileName, stream, true).ConfigureAwait(false); |
|
|
|
|
|
return new JsonResult(new { code = 400, message = "输入异常", fileName }); |
|
|
|
|
|
} |
|
|
|
|
|
foreach (var item in list) |
|
|
|
|
|
{ |
|
|
|
|
|
Adjust(item); |
|
|
|
|
|
} |
|
|
|
|
|
return new JsonResult(new { code = 200, message = "ok" }); |
|
|
|
|
|
} |
|
|
|
|
|
catch (Exception ex) |
|
|
{ |
|
|
{ |
|
|
Adjust(file); |
|
|
Console.WriteLine(ex.ToString()); |
|
|
|
|
|
throw; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private List<T> ImportInternal<T>(byte[] data) |
|
|
private List<T> ImportInternal<T>(byte[] data, out List<List<ValidationResult>> validationResults) |
|
|
{ |
|
|
{ |
|
|
var list = new List<T>(); |
|
|
var list = new List<T>(); |
|
|
|
|
|
validationResults = new List<List<ValidationResult>>(); |
|
|
|
|
|
try |
|
|
|
|
|
{ |
|
|
using var workbook = new XLWorkbook(new MemoryStream(data)); |
|
|
using var workbook = new XLWorkbook(new MemoryStream(data)); |
|
|
var properties = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty) |
|
|
var properties = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty) |
|
|
.Where(o => o.GetAttributes<ImporterHeaderAttribute>().Any()) |
|
|
.Where(o => o.GetAttributes<ImporterHeaderAttribute>().Any()) |
|
|
.ToDictionary(o => o.GetAttribute<ImporterHeaderAttribute>().Name, o => o); |
|
|
.ToDictionary(o => o.GetAttribute<ImporterHeaderAttribute>().Name, o => o); |
|
|
var ws = workbook.Worksheets.FirstOrDefault(); |
|
|
var ws = workbook.Worksheets.FirstOrDefault(); |
|
|
|
|
|
|
|
|
for (int rowIndex = 2; rowIndex <= ws.RowsUsed().Count(); rowIndex++) |
|
|
for (int rowIndex = 2; rowIndex <= ws.RowsUsed().Count(); rowIndex++) |
|
|
{ |
|
|
{ |
|
|
var row = ws.Row(rowIndex); |
|
|
var row = ws.Row(rowIndex); |
|
|
var model = Activator.CreateInstance<T>(); |
|
|
var model = Activator.CreateInstance<T>(); |
|
|
list.Add(model); |
|
|
|
|
|
for (var columnIndex = 1; columnIndex < ws.ColumnsUsed().Count(); columnIndex++) |
|
|
for (var columnIndex = 1; columnIndex < ws.ColumnsUsed().Count(); columnIndex++) |
|
|
{ |
|
|
{ |
|
|
var cell = row.Cell(columnIndex); |
|
|
var cell = row.Cell(columnIndex); |
|
@ -404,9 +437,19 @@ public class VmiAppService : Controller, IApplicationService, IJobService, ITran |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
list.Add(model); |
|
|
|
|
|
var results = new List<ValidationResult>(); |
|
|
|
|
|
Validator.TryValidateObject(model, new ValidationContext(model, null, null), results); |
|
|
|
|
|
validationResults.Add(results); |
|
|
} |
|
|
} |
|
|
return list; |
|
|
return list; |
|
|
} |
|
|
} |
|
|
|
|
|
catch (Exception ex) |
|
|
|
|
|
{ |
|
|
|
|
|
Console.WriteLine(ex.ToString()); |
|
|
|
|
|
throw; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private byte[] GetContent<TExport>(List<TExport> entities, string name = "sheet1") |
|
|
private byte[] GetContent<TExport>(List<TExport> entities, string name = "sheet1") |
|
|
{ |
|
|
{ |
|
@ -435,6 +478,15 @@ public class VmiAppService : Controller, IApplicationService, IJobService, ITran |
|
|
SetCell(item, cell, property); |
|
|
SetCell(item, cell, property); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
SetStyle(ws); |
|
|
|
|
|
using var stream = new MemoryStream(); |
|
|
|
|
|
workbook.SaveAs(stream); |
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin); |
|
|
|
|
|
return stream.ToArray(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static void SetStyle(IXLWorksheet ws) |
|
|
|
|
|
{ |
|
|
ws.RangeUsed().Style.Border.TopBorder = |
|
|
ws.RangeUsed().Style.Border.TopBorder = |
|
|
ws.RangeUsed().Style.Border.RightBorder = |
|
|
ws.RangeUsed().Style.Border.RightBorder = |
|
|
ws.RangeUsed().Style.Border.BottomBorder = |
|
|
ws.RangeUsed().Style.Border.BottomBorder = |
|
@ -446,10 +498,6 @@ public class VmiAppService : Controller, IApplicationService, IJobService, ITran |
|
|
ws.RangeUsed().SetAutoFilter(); |
|
|
ws.RangeUsed().SetAutoFilter(); |
|
|
ws.ColumnsUsed().AdjustToContents(); |
|
|
ws.ColumnsUsed().AdjustToContents(); |
|
|
ws.RowsUsed().AdjustToContents(); |
|
|
ws.RowsUsed().AdjustToContents(); |
|
|
using var stream = new MemoryStream(); |
|
|
|
|
|
workbook.SaveAs(stream); |
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin); |
|
|
|
|
|
return stream.ToArray(); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private static void SetCell<TExportModel>(TExportModel? model, IXLCell cell, PropertyInfo property) |
|
|
private static void SetCell<TExportModel>(TExportModel? model, IXLCell cell, PropertyInfo property) |
|
|