This commit is contained in:
2022-03-28 23:11:09 +00:00
parent 4c6e8102c4
commit 83a3ad689c
3 changed files with 197 additions and 15 deletions

View File

@@ -298,7 +298,7 @@ namespace AyaNova.Biz
var batchResults = await ct.TaskGroup.AsNoTracking().Include(z => z.Items).Where(z => batch.Contains(z.Id)).ToArrayAsync();
//order the results back into original
var orderedList = from id in batch join z in batchResults on id equals z.Id select z;
batchResults=null;
batchResults = null;
foreach (TaskGroup w in orderedList)
{
if (!ReportRenderManager.KeepGoing(jobId)) return null;
@@ -307,7 +307,7 @@ namespace AyaNova.Biz
jo["CustomFields"] = JObject.Parse((string)jo["CustomFields"]);
ReportData.Add(jo);
}
orderedList=null;
orderedList = null;
}
return ReportData;
}
@@ -325,35 +325,107 @@ namespace AyaNova.Biz
return await GetReportData(dataListSelectedRequest, jobId);
}
public async Task<List<string>> ImportData(AyImportData importData)
{
List<string> ImportResult = new List<string>();
string ImportTag = $"imported-{FileUtil.GetSafeDateFileName()}";
List<string> ImportResult = new List<string>();
string ImportTag = ImportUtil.GetImportTag();
//ignore these fields
var jsset = JsonSerializer.CreateDefault(new JsonSerializerSettings { ContractResolver = new AyaNova.Util.JsonUtil.ShouldSerializeContractResolver(new string[] { "Concurrency", "Id", "CustomFields" }) });
foreach (JObject j in importData.Data)
{
var w = j.ToObject<TaskGroup>(jsset);
// if (j["CustomFields"] != null)
// w.CustomFields = j["CustomFields"].ToString();
// w.Tags.Add(ImportTag);//so user can find them all and revert later if necessary
var res = await CreateAsync(w);
if (res == null)
try
{
ImportResult.Add($"* {w.Name} - {this.GetErrorsAsString()}");
this.ClearErrors();
//Compile Items collection if specified
var ImportTaskGroupItems = new List<TaskGroupItem>();
if (j["Items"] != null)
{
//hydrate from collection
foreach (JToken t in j["Items"])
{
ImportTaskGroupItems.Add(new TaskGroupItem() { TaskGroupId = 0, Sequence = (int)t["Sequence"], Task = (string)t["Task"] });
}
j["Items"] = null;//strip it out so it doesn't get automatically converted into parsed object later
}
long existingId = await ct.TaskGroup.AsNoTracking().Where(z => z.Name == (string)j["Name"]).Select(x => x.Id).FirstOrDefaultAsync();
if (existingId == 0)
{
if (importData.DoImport)
{
//import this record
var Target = j.ToObject<TaskGroup>(jsset);
if (Target.Items == null)
Target.Items = new List<TaskGroupItem>();
if (j["Items"] != null)
foreach (TaskGroupItem tgi in ImportTaskGroupItems)
Target.Items.Add(tgi);
var res = await CreateAsync(Target);
if (res == null)
{
ImportResult.Add($"❌ {Target.Name}\r\n{this.GetErrorsAsString()}");
this.ClearErrors();
}
else
{
ImportResult.Add($"✔️ {Target.Name}");
}
}
}
else
{
if (importData.DoUpdate)
{
//update this record with any data provided
//load existing record
var Target = await GetAsync((long)existingId);
var Source = j.ToObject<TaskGroup>(jsset);
var propertiesToUpdate = j.Properties().Select(p => p.Name).ToList();
propertiesToUpdate.Remove("Name");
propertiesToUpdate.Remove("Items");
ImportUtil.Update(Source, Target, propertiesToUpdate);
if (j["Items"] != null)
{
Target.Items.Clear();
foreach (TaskGroupItem tgi in ImportTaskGroupItems)
Target.Items.Add(tgi);
}
var res = await PutAsync(Target);
if (res == null)
{
ImportResult.Add($"❌ {Target.Name} - {this.GetErrorsAsString()}");
this.ClearErrors();
}
else
{
ImportResult.Add($"✔️ {Target.Name}");
}
}
}
}
else
catch (Exception ex)
{
ImportResult.Add($"{w.Name} - ok");
ImportResult.Add($"❌ Exception processing import\n record:{j.ToString()}\nError:{ex.Message}\nSource:{ex.Source}\nStack:{ex.StackTrace.ToString()}");
}
}
return ImportResult;
}
////////////////////////////////////////////////////////////////////////////////////////////////
//JOB / OPERATIONS
//