From 3498beb78be0fc0ff0141f4ea2703b5d051eb041 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Mon, 28 Mar 2022 18:42:03 +0000 Subject: [PATCH] --- server/AyaNova/biz/PartAssemblyBiz.cs | 126 ++++++++++++++++++++++++-- 1 file changed, 116 insertions(+), 10 deletions(-) diff --git a/server/AyaNova/biz/PartAssemblyBiz.cs b/server/AyaNova/biz/PartAssemblyBiz.cs index 85ea2d6c..27f124ed 100644 --- a/server/AyaNova/biz/PartAssemblyBiz.cs +++ b/server/AyaNova/biz/PartAssemblyBiz.cs @@ -379,33 +379,139 @@ namespace AyaNova.Biz return await GetReportData(dataListSelectedRequest, jobId); } + + + public async Task> ImportData(AyImportData importData) { List ImportResult = new List(); - 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(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) + + //Compile Items collection if specified + var ImportPartAssemblyItems = new List(); + if (j["Items"] != null) { - ImportResult.Add($"* {w.Name} - {this.GetErrorsAsString()}"); - this.ClearErrors(); + //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(jsset); + if (Target.Items == null) + Target.Items = new List(); + 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($"❌ {Target.Name}\r\n{this.GetErrorsAsString()}"); + this.ClearErrors(); + } + else + { + ImportResult.Add($"✔️ {Target.Name}"); + } + } } else { - ImportResult.Add($"{w.Name} - ok"); + if (importData.DoUpdate) + { + //update this record with any data provided + //load existing record + var Target = await GetAsync((long)existingId); + var Source = j.ToObject(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> ImportData(AyImportData importData) + // { + // List ImportResult = new List(); + // 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(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; + // } + + ////////////////////////////////////////////////////////////////////////////////////////////////