|
@ -10,11 +10,18 @@ using Win_in.Sfs.Basedata.Domain.Shared; |
|
|
|
|
|
|
|
|
namespace Win_in.Sfs.Basedata.Application; |
|
|
namespace Win_in.Sfs.Basedata.Application; |
|
|
|
|
|
|
|
|
|
|
|
using System.Collections.Generic; |
|
|
|
|
|
using System.ComponentModel.DataAnnotations; |
|
|
using System.Linq; |
|
|
using System.Linq; |
|
|
|
|
|
using System.Text; |
|
|
|
|
|
using DocumentFormat.OpenXml.Office2010.ExcelAc; |
|
|
using Microsoft.EntityFrameworkCore; |
|
|
using Microsoft.EntityFrameworkCore; |
|
|
|
|
|
using Microsoft.Extensions.Logging; |
|
|
|
|
|
using Volo.Abp.Domain.Entities; |
|
|
using Volo.Abp.Domain.Repositories; |
|
|
using Volo.Abp.Domain.Repositories; |
|
|
using Win_in.Sfs.Shared; |
|
|
using Win_in.Sfs.Shared; |
|
|
|
|
|
using Win_in.Sfs.Shared.Application.Contracts; |
|
|
|
|
|
using Win_in.Sfs.Shared.Domain; |
|
|
|
|
|
|
|
|
[Authorize] |
|
|
[Authorize] |
|
|
[Route($"{BasedataConsts.RootPath}dict")] |
|
|
[Route($"{BasedataConsts.RootPath}dict")] |
|
@ -81,4 +88,114 @@ public class DictAppService : SfsBaseDataWithCodeAppServiceBase<Dict, DictDTO, S |
|
|
|
|
|
|
|
|
return ObjectMapper.Map<Dict, DictDTO>(entity); |
|
|
return ObjectMapper.Map<Dict, DictDTO>(entity); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 导入数据具体实现,可重写
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
protected override async Task<SfsImportResult> ImportInternalAsync(SfsImportRequestInput requestInput, byte[] inputFileBytes) |
|
|
|
|
|
{ |
|
|
|
|
|
IList<DictImportInput> modelList = null; |
|
|
|
|
|
var modelDict = new Dictionary<DictImportInput, List<ValidationResult>>(); |
|
|
|
|
|
var entityDict = new Dictionary<Dict, EntityState>(); |
|
|
|
|
|
try |
|
|
|
|
|
{ |
|
|
|
|
|
var hasDetails = typeof(Dict).GetInterfaces().Any(o => o.IsGenericType && o.GetGenericTypeDefinition() == typeof(IMasterEntity<>)); |
|
|
|
|
|
modelDict = ExportImportService.ImportHaveValidationResult<DictImportInput>(inputFileBytes); |
|
|
|
|
|
foreach (var modelDictItem in modelDict) |
|
|
|
|
|
{ |
|
|
|
|
|
// DataAnnotations 静态验证
|
|
|
|
|
|
var validationRresults = modelDictItem.Value; |
|
|
|
|
|
var model = modelDictItem.Key; |
|
|
|
|
|
Validator.TryValidateObject(model, new ValidationContext(model, null, null), validationRresults); |
|
|
|
|
|
} |
|
|
|
|
|
modelList = modelDict.Keys.ToList(); |
|
|
|
|
|
// 如果没有验证错误或允许部分导入
|
|
|
|
|
|
if (!modelDict.SelectMany(o => o.Value).Any() || requestInput.IsAllowPartImport) |
|
|
|
|
|
{ |
|
|
|
|
|
var dictCodes = modelList.Select(p => p.Code).Distinct().ToList(); |
|
|
|
|
|
foreach (var detail in dictCodes) |
|
|
|
|
|
{ |
|
|
|
|
|
var dictDetail = modelList.FirstOrDefault(p => p.Code == detail); |
|
|
|
|
|
|
|
|
|
|
|
Dict dictItem = new Dict(); |
|
|
|
|
|
dictItem.SetId(GuidGenerator.Create()); |
|
|
|
|
|
dictItem.Code = dictDetail.Code; |
|
|
|
|
|
dictItem.Name = dictDetail.Name; |
|
|
|
|
|
dictItem.Description = dictDetail.Description; |
|
|
|
|
|
foreach (var item in modelList.Where(p=>p.Code==detail)) |
|
|
|
|
|
{ |
|
|
|
|
|
DictItem dictItemDetail = new DictItem(); |
|
|
|
|
|
dictItemDetail.SetId(GuidGenerator.Create()); |
|
|
|
|
|
dictItemDetail.Code = item.Item_Code; |
|
|
|
|
|
dictItemDetail.Name = item.Item_Name; |
|
|
|
|
|
dictItemDetail.Value = item.Item_Value; |
|
|
|
|
|
dictItemDetail.Description = item.Item_Description; |
|
|
|
|
|
dictItemDetail.Enabled = true; |
|
|
|
|
|
dictItemDetail.MasterId = dictItem.Id; |
|
|
|
|
|
dictItem.Items.Add(dictItemDetail); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
await _repository.InsertAsync(dictItem).ConfigureAwait(false); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 创建导入报告
|
|
|
|
|
|
var reportFile = ExportImportService.GetImportReport(inputFileBytes, modelDict); |
|
|
|
|
|
// 创建返回值
|
|
|
|
|
|
return new SfsImportResult |
|
|
|
|
|
{ |
|
|
|
|
|
TotalNum = modelList.Count, |
|
|
|
|
|
ErrorNum = modelDict.Count(o => o.Value.Any()), |
|
|
|
|
|
FileName = reportFile.FileDownloadName, |
|
|
|
|
|
FileContents = reportFile.FileContents |
|
|
|
|
|
}; |
|
|
|
|
|
} |
|
|
|
|
|
catch (Exception ex) |
|
|
|
|
|
{ |
|
|
|
|
|
Console.WriteLine("---------------------------------"); |
|
|
|
|
|
Console.WriteLine($"####导入验证错误:"); |
|
|
|
|
|
Console.WriteLine($"{ex.Message}"); |
|
|
|
|
|
Console.WriteLine("---------------------------------"); |
|
|
|
|
|
Logger.LogException(ex); |
|
|
|
|
|
if (modelList != null) |
|
|
|
|
|
{ |
|
|
|
|
|
try |
|
|
|
|
|
{ |
|
|
|
|
|
foreach (var item in modelDict) |
|
|
|
|
|
{ |
|
|
|
|
|
var model = item.Key; |
|
|
|
|
|
var validationRresults = item.Value; |
|
|
|
|
|
validationRresults.Add(new ValidationResult($"无法添加,{ex.Message}", new string[] { "异常" })); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 创建导入报告
|
|
|
|
|
|
var reportFile = ExportImportService.GetImportReport(inputFileBytes, modelDict); |
|
|
|
|
|
return new SfsImportResult |
|
|
|
|
|
{ |
|
|
|
|
|
TotalNum = modelList.Count, |
|
|
|
|
|
ErrorNum = modelDict.Count(o => o.Value.Any()), |
|
|
|
|
|
FileName = reportFile.FileDownloadName, |
|
|
|
|
|
FileContents = reportFile.FileContents |
|
|
|
|
|
}; |
|
|
|
|
|
} |
|
|
|
|
|
catch (Exception) |
|
|
|
|
|
{ |
|
|
|
|
|
return new SfsImportResult() |
|
|
|
|
|
{ |
|
|
|
|
|
ExceptionMessage = ex.Message, |
|
|
|
|
|
FileContents = Encoding.Default.GetBytes(ex.Message) |
|
|
|
|
|
}; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
return new SfsImportResult() |
|
|
|
|
|
{ |
|
|
|
|
|
ExceptionMessage = ex.Message, |
|
|
|
|
|
FileContents = Encoding.Default.GetBytes(ex.Message) |
|
|
|
|
|
}; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|