This commit is contained in:
@@ -379,33 +379,139 @@ 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()}";
|
||||
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<PartAssembly>(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);
|
||||
|
||||
//Compile Items collection if specified
|
||||
var ImportPartAssemblyItems = new List<PartAssemblyItem>();
|
||||
if (j["Items"] != null)
|
||||
{
|
||||
//hydrate from collection
|
||||
foreach (JToken t in j["Items"])
|
||||
{
|
||||
if (!JsonUtil.JTokenIsNullOrEmpty(t["PartNameViz"]))//part name is mandatory, the quantity can be inferred
|
||||
{
|
||||
decimal parsedQuantity = 0;
|
||||
//a name was specified so attempt to find it
|
||||
var parsedPartId = await ct.Part.AsNoTracking().Where(z => z.Name == (string)t["PartNameViz"]).Select(x => x.Id).FirstOrDefaultAsync();
|
||||
if (parsedPartId == 0)
|
||||
AddError(ApiErrorCode.NOT_FOUND, "PartNameViz", $"'{(string)t["PartNameViz"]}'");
|
||||
else
|
||||
{
|
||||
//we have a valid part id now parse out quantity or set to zero if not specified
|
||||
if (!JsonUtil.JTokenIsNullOrEmpty(t["Quantity"]))
|
||||
{
|
||||
parsedQuantity = (decimal)t["Quantity"];
|
||||
}
|
||||
ImportPartAssemblyItems.Add(new PartAssemblyItem() { PartAssemblyId = 0, PartId = parsedPartId, Quantity = parsedQuantity });
|
||||
}
|
||||
}
|
||||
}
|
||||
j["Items"] = null;//strip it out so it doesn't get automatically converted into parsed object later
|
||||
}
|
||||
|
||||
long existingId = await ct.PartAssembly.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<PartAssembly>(jsset);
|
||||
if (Target.Items == null)
|
||||
Target.Items = new List<PartAssemblyItem>();
|
||||
Target.Tags.Add(ImportTag);
|
||||
|
||||
if (j["Items"] != null)
|
||||
foreach (PartAssemblyItem ipi in ImportPartAssemblyItems)
|
||||
Target.Items.Add(ipi);
|
||||
|
||||
var res = await CreateAsync(Target);
|
||||
if (res == null)
|
||||
{
|
||||
ImportResult.Add($"* {w.Name} - {this.GetErrorsAsString()}");
|
||||
ImportResult.Add($"❌ {Target.Name}\r\n{this.GetErrorsAsString()}");
|
||||
this.ClearErrors();
|
||||
}
|
||||
else
|
||||
{
|
||||
ImportResult.Add($"{w.Name} - ok");
|
||||
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<Part>(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)
|
||||
{
|
||||
foreach (PartAssemblyItem ipi in ImportPartAssemblyItems)
|
||||
Target.Items.Add(ipi);
|
||||
}
|
||||
|
||||
var res = await PutAsync(Target);
|
||||
|
||||
if (res == null)
|
||||
{
|
||||
ImportResult.Add($"❌ {Target.Name} - {this.GetErrorsAsString()}");
|
||||
this.ClearErrors();
|
||||
}
|
||||
else
|
||||
{
|
||||
ImportResult.Add($"✔️ {Target.Name}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return ImportResult;
|
||||
}
|
||||
|
||||
|
||||
// public async Task<List<string>> ImportData(AyImportData importData)
|
||||
// {
|
||||
// List<string> ImportResult = new List<string>();
|
||||
// string ImportTag = $"imported-{FileUtil.GetSafeDateFileName()}";
|
||||
|
||||
// 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<PartAssembly>(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)
|
||||
// {
|
||||
// ImportResult.Add($"* {w.Name} - {this.GetErrorsAsString()}");
|
||||
// this.ClearErrors();
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// ImportResult.Add($"{w.Name} - ok");
|
||||
// }
|
||||
// }
|
||||
// return ImportResult;
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Reference in New Issue
Block a user