/// <summary> /// Create/Get new ImportQueueRecordFileAccountDataRecord instance without any link to database /// </summary> /// <returns>ImportQueueRecordFileAccountDataRecord instance</returns> public ImportQueueRecordFileAccountDataRecord ImportQueueRecordFileAccountDataRecordNew(ImportQueueRecordFileInfo importQueueRecordFile = null, AccountDataRecord accountDataRecord = null) { try { var dt = DateTime.UtcNow; var res = new ImportQueueRecordFileAccountDataRecord() { ImportQueueRecordFile = importQueueRecordFile, AccountDataRecord = accountDataRecord, LoadDate = dt, }; if (importQueueRecordFile != null) importQueueRecordFile.LoadedRecords.Add(res); if (accountDataRecord != null) accountDataRecord.LoadedByQueueFiles.Add(res); return res; } catch(Exception ex) { Helpers.Log.Add(ex, string.Format("Repository.ImportQueueRecordFileAccountDataRecordNew()")); throw; } }
/// <summary> /// Вставить записи в основную таблицу данных аккаунта /// </summary> /// <param name="previewRows">Rows to insert</param> /// <param name="importFile">Import queue file</param> /// <param name="progressAction">Действие для отображения прогресса</param> /// <param name="logAction">Действие для отображения лога</param> /// <returns>Возвращает обработанные данные, которые можно экспортировать</returns> public IEnumerable<AccountDataRecord> Import(IEnumerable<DataPreviewRow> previewRows, ImportQueueRecordFileInfo importFile, Action<decimal> progressAction = null, Action<string> logAction = null) { progressAction = progressAction ?? new Action<decimal>((i) => { }); logAction = logAction ?? new Action<string>((i) => { }); using (var logSession = Log.Session($"{this.GetType().Name}.{nameof(Import)}()", VerboseLog)) try { logSession.Output = (strs) => strs.ToList().ForEach(s => logAction(s)); if (previewRows == null) throw new ArgumentNullException(nameof(previewRows)); var progress = new Helpers.PercentageProgress(); var prgPreparation = progress.GetChild(weight: 0.3m); var prgUpdateMarks = progress.GetChild(weight: 0.6m); var prgPrepareExport = progress.GetChild(weight: 0.1m); progress.Change += (s, e) => progressAction(e.Value); logSession.Add($"grouping {previewRows.Count()} rows..."); var dataToImport = previewRows .GroupBy(i => new { i.DataRecord.Phone, i.DataRecord.Street, i.DataRecord.HouseNumber }) .Select(i => i.LastOrDefault()) .LeftOuterJoin(Account.Data, g => new { g.DataRecord.Phone, g.DataRecord.Street, g.DataRecord.HouseNumber }, d => new { d.Phone, d.Street, d.HouseNumber }, (g, d) => new { Existed = d, Insert = g }) .ToList(); if (importFile != null) dataToImport.ForEach(r => Repository.ImportQueueRecordFileAccountDataRecordNew(importFile, r.Existed ?? r.Insert.DataRecord)); prgPreparation.Value = 50; logSession.Add($"grouping rows done. Insert it in account data."); //Repository.AccountDataRecordAdd(dataToImport.Where(i => i.Existed == null).Select(i => i.Insert.DataRecord), Account, false); dataToImport.ForEach(i => { if (i.Existed == null) { Repository.AccountDataRecordAdd(i.Insert.DataRecord, saveAfterInsert: false); } else { i.Insert.DataRecord.CopyObject(i.Existed, new string[] { nameof(i.Insert.DataRecord.DataAdditional), nameof(i.Insert.DataRecord.LoadedByQueueFiles), nameof(i.Insert.DataRecord.AccountDataRecordUID), nameof(i.Insert.DataRecord.Created), nameof(i.Insert.DataRecord.Exported) }); i.Existed.CopyObject(i.Insert.DataRecord, new string[] { nameof(i.Insert.DataRecord.DataAdditional), nameof(i.Insert.DataRecord.LoadedByQueueFiles), nameof(i.Insert.DataRecord.AccountDataRecordUID) }); i.Insert.DataRecordAdditional.CopyObject(i.Existed.DataAdditional, new string[] { nameof(i.Insert.DataRecordAdditional.AccountDataRecordUID) }); } }); prgPreparation.Value = 100; UpdateMarks(dataToImport.Select(i => i.Insert.DataRecord.Phone).Distinct(), (i) => prgUpdateMarks.Value = i, s => logSession.Add($"update marks: {s}")); var exportStart = DateTime.UtcNow - (Account.Settings.IgnoreExportTime ?? new TimeSpan()); logSession.Add($"prepare results to return records older then '{exportStart}'"); var rowsToExport = dataToImport .Where(r => (r.Insert.DataRecord.Exported ?? DateTime.MinValue) <= exportStart) .Select(r => r.Insert.DataRecord); prgPrepareExport.Value = 100; return rowsToExport; } catch (Exception ex) { logSession.Add(ex); logSession.Enabled = true; throw; } }
/// <summary> /// Remove ImportQueueRecordFile from database /// </summary> /// <param name="instance">ImportQueueRecordFile instance</param> /// <param name="saveAfterRemove">Save database after removing</param> /// <param name="waitUntilSaving">Wait until saving</param> public void ImportQueueRecordFileInfoRemove(ImportQueueRecordFileInfo instance, bool saveAfterRemove = true, bool waitUntilSaving = true) { ImportQueueRecordFileInfoRemove(new ImportQueueRecordFileInfo[] { instance }, saveAfterRemove, waitUntilSaving); }
/// <summary> /// Add ImportQueueRecordFile to database /// </summary> /// <param name="instance">ImportQueueRecordFile instance</param> /// <param name="importQueueRecord">ImportQueueRecord instance for instance</param> /// <param name="saveAfterInsert">Save database after insertion</param> /// <param name="waitUntilSaving">Wait until saving</param> public void ImportQueueRecordFileInfoAdd(ImportQueueRecordFileInfo instance, ImportQueueRecord importQueueRecord, bool saveAfterInsert = true, bool waitUntilSaving = true) { ImportQueueRecordFileInfoAdd(new ImportQueueRecordFileInfo[] { instance }, importQueueRecord, saveAfterInsert, waitUntilSaving); }
/// <summary> /// Create/Get new ImportQueueRecordFile instance without any link to database /// </summary> /// <returns>ImportQueueRecordFile instance</returns> public ImportQueueRecordFileInfo ImportQueueRecordFileInfoNew(ImportQueueRecord importQueueRecord = null, object anonymousFiller = null) { try { var dt = DateTime.UtcNow; var res = new ImportQueueRecordFileInfo() { ImportQueueRecordFileInfoUID = Guid.NewGuid(), ImportQueueRecordState = ImportQueueRecordStateGetDefault(), }; if (anonymousFiller != null) res.FillFromAnonymousType(anonymousFiller); importQueueRecord = importQueueRecord ?? res.ImportQueueRecord; if (importQueueRecord != null) importQueueRecord.FileInfoes.Add(res); return res; } catch(Exception ex) { Helpers.Log.Add(ex, string.Format("Repository.ImportQueueRecordFileInfoNew()")); throw; } }
/// <summary> /// Create/Get new ImportQueueRecordFile instance without any link to database /// </summary> /// <returns>ImportQueueRecordFile instance</returns> public ImportQueueRecordFileInfoFile ImportQueueRecordFileInfoFileNew(ImportQueueRecordFileInfoFileType fileType, ImportQueueRecordFileInfo importQueueRecordFileInfo = null, File file = null) { try { var dt = DateTime.UtcNow; var res = new ImportQueueRecordFileInfoFile() { Type = fileType, ImportQueueRecordFileInfo = importQueueRecordFileInfo, File = file }; if (importQueueRecordFileInfo != null) importQueueRecordFileInfo.Files.Add(res); return res; } catch(Exception ex) { Helpers.Log.Add(ex, string.Format("Repository.ImportQueueRecordFileInfoFileNew()")); throw; } }