public async Task <ActionResult> Import(IFormFile file) { if (UnAuthorization) { return(Forbid()); } if (!ModelState.IsValid) { throw new BindException(ModelState); } AppUserFilter AppUserFilter = new AppUserFilter { Skip = 0, Take = int.MaxValue, Selects = AppUserSelect.ALL }; List <AppUser> AppUsers = await AppUserService.List(AppUserFilter); EntityReferenceFilter EntityReferenceFilter = new EntityReferenceFilter { Skip = 0, Take = int.MaxValue, Selects = EntityReferenceSelect.ALL }; List <EntityReference> EntityReferences = await EntityReferenceService.List(EntityReferenceFilter); CallTypeFilter CallTypeFilter = new CallTypeFilter { Skip = 0, Take = int.MaxValue, Selects = CallTypeSelect.ALL }; List <CallType> CallTypes = await CallTypeService.List(CallTypeFilter); CallEmotionFilter CallEmotionFilter = new CallEmotionFilter { Skip = 0, Take = int.MaxValue, Selects = CallEmotionSelect.ALL }; List <CallEmotion> CallEmotions = await CallEmotionService.List(CallEmotionFilter); List <CallLog> CallLogs = new List <CallLog>(); using (ExcelPackage excelPackage = new ExcelPackage(file.OpenReadStream())) { ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.FirstOrDefault(); if (worksheet == null) { return(Ok(CallLogs)); } int StartColumn = 1; int StartRow = 1; int IdColumn = 0 + StartColumn; int EntityReferenceIdColumn = 1 + StartColumn; int CallTypeIdColumn = 2 + StartColumn; int CallEmotionIdColumn = 3 + StartColumn; int AppUserIdColumn = 4 + StartColumn; int TitleColumn = 5 + StartColumn; int ContentColumn = 6 + StartColumn; int PhoneColumn = 7 + StartColumn; int CallTimeColumn = 8 + StartColumn; int UsedColumn = 12 + StartColumn; for (int i = StartRow; i <= worksheet.Dimension.End.Row; i++) { if (string.IsNullOrEmpty(worksheet.Cells[i + StartRow, StartColumn].Value?.ToString())) { break; } string IdValue = worksheet.Cells[i + StartRow, IdColumn].Value?.ToString(); string EntityReferenceIdValue = worksheet.Cells[i + StartRow, EntityReferenceIdColumn].Value?.ToString(); string CallTypeIdValue = worksheet.Cells[i + StartRow, CallTypeIdColumn].Value?.ToString(); string CallEmotionIdValue = worksheet.Cells[i + StartRow, CallEmotionIdColumn].Value?.ToString(); string AppUserIdValue = worksheet.Cells[i + StartRow, AppUserIdColumn].Value?.ToString(); string TitleValue = worksheet.Cells[i + StartRow, TitleColumn].Value?.ToString(); string ContentValue = worksheet.Cells[i + StartRow, ContentColumn].Value?.ToString(); string PhoneValue = worksheet.Cells[i + StartRow, PhoneColumn].Value?.ToString(); string CallTimeValue = worksheet.Cells[i + StartRow, CallTimeColumn].Value?.ToString(); string UsedValue = worksheet.Cells[i + StartRow, UsedColumn].Value?.ToString(); CallLog CallLog = new CallLog(); CallLog.Title = TitleValue; CallLog.Content = ContentValue; CallLog.Phone = PhoneValue; CallLog.CallTime = DateTime.TryParse(CallTimeValue, out DateTime CallTime) ? CallTime : DateTime.Now; AppUser AppUser = AppUsers.Where(x => x.Id.ToString() == AppUserIdValue).FirstOrDefault(); CallLog.AppUserId = AppUser == null ? 0 : AppUser.Id; CallLog.AppUser = AppUser; EntityReference EntityReference = EntityReferences.Where(x => x.Id.ToString() == EntityReferenceIdValue).FirstOrDefault(); CallLog.EntityReferenceId = EntityReference == null ? 0 : EntityReference.Id; CallLog.EntityReference = EntityReference; CallType CallType = CallTypes.Where(x => x.Id.ToString() == CallTypeIdValue).FirstOrDefault(); CallLog.CallTypeId = CallType == null ? 0 : CallType.Id; CallLog.CallType = CallType; CallEmotion CallEmotion = CallEmotions.Where(x => x.Id.ToString() == CallEmotionIdValue).FirstOrDefault(); CallLog.CallEmotionId = CallEmotion == null ? 0 : CallEmotion.Id; CallLog.CallEmotion = CallEmotion; CallLogs.Add(CallLog); } } CallLogs = await CallLogService.Import(CallLogs); if (CallLogs.All(x => x.IsValidated)) { return(Ok(true)); } else { List <string> Errors = new List <string>(); for (int i = 0; i < CallLogs.Count; i++) { CallLog CallLog = CallLogs[i]; if (!CallLog.IsValidated) { string Error = $"Dòng {i + 2} có lỗi:"; if (CallLog.Errors.ContainsKey(nameof(CallLog.Id))) { Error += CallLog.Errors[nameof(CallLog.Id)]; } if (CallLog.Errors.ContainsKey(nameof(CallLog.EntityReferenceId))) { Error += CallLog.Errors[nameof(CallLog.EntityReferenceId)]; } if (CallLog.Errors.ContainsKey(nameof(CallLog.CallTypeId))) { Error += CallLog.Errors[nameof(CallLog.CallTypeId)]; } if (CallLog.Errors.ContainsKey(nameof(CallLog.CallEmotionId))) { Error += CallLog.Errors[nameof(CallLog.CallEmotionId)]; } if (CallLog.Errors.ContainsKey(nameof(CallLog.AppUserId))) { Error += CallLog.Errors[nameof(CallLog.AppUserId)]; } if (CallLog.Errors.ContainsKey(nameof(CallLog.Title))) { Error += CallLog.Errors[nameof(CallLog.Title)]; } if (CallLog.Errors.ContainsKey(nameof(CallLog.Content))) { Error += CallLog.Errors[nameof(CallLog.Content)]; } if (CallLog.Errors.ContainsKey(nameof(CallLog.Phone))) { Error += CallLog.Errors[nameof(CallLog.Phone)]; } if (CallLog.Errors.ContainsKey(nameof(CallLog.CallTime))) { Error += CallLog.Errors[nameof(CallLog.CallTime)]; } Errors.Add(Error); } } return(BadRequest(Errors)); } }