This commit is contained in:
@@ -566,121 +566,128 @@ namespace AyaNova.Biz
|
||||
|
||||
foreach (JObject j in importData.Data)
|
||||
{
|
||||
|
||||
//Compile linked objects if specified
|
||||
long? ImportManufacturerId = -1;//default meaning not included / don't set
|
||||
if (j["ManufacturerViz"] != null)
|
||||
try
|
||||
{
|
||||
//something was specified, may be deliberate attempt to null it out so default to that
|
||||
ImportManufacturerId = null;
|
||||
if (!JsonUtil.JTokenIsNullOrEmpty(j["ManufacturerViz"]))
|
||||
|
||||
//Compile linked objects if specified
|
||||
long? ImportManufacturerId = -1;//default meaning not included / don't set
|
||||
if (j["ManufacturerViz"] != null)
|
||||
{
|
||||
//a name was specified so attempt to find it
|
||||
ImportManufacturerId = await ct.Vendor.AsNoTracking().Where(z => z.Name == (string)j["ManufacturerViz"]).Select(x => x.Id).FirstOrDefaultAsync();
|
||||
if (ImportManufacturerId == 0)
|
||||
AddError(ApiErrorCode.NOT_FOUND, "ManufacturerViz", $"'{(string)j["ManufacturerViz"]}'");
|
||||
}
|
||||
}
|
||||
|
||||
long? ImportWholeSalerId = -1;
|
||||
if (j["WholeSalerViz"] != null)
|
||||
{
|
||||
ImportWholeSalerId = null;
|
||||
if (!JsonUtil.JTokenIsNullOrEmpty(j["WholeSalerViz"]))
|
||||
{
|
||||
ImportWholeSalerId = await ct.Vendor.AsNoTracking().Where(z => z.Name == (string)j["WholeSalerViz"]).Select(x => x.Id).FirstOrDefaultAsync();
|
||||
if (ImportWholeSalerId == 0)
|
||||
AddError(ApiErrorCode.NOT_FOUND, "WholeSalerViz", $"'{(string)j["WholeSalerViz"]}'");
|
||||
}
|
||||
}
|
||||
|
||||
long? ImportAlternativeWholeSalerId = -1;
|
||||
if (j["AlternativeWholeSalerViz"] != null)
|
||||
{
|
||||
ImportAlternativeWholeSalerId = null;
|
||||
if (!JsonUtil.JTokenIsNullOrEmpty(j["AlternativeWholeSalerViz"]))
|
||||
{
|
||||
ImportAlternativeWholeSalerId = await ct.Vendor.AsNoTracking().Where(z => z.Name == (string)j["AlternativeWholeSalerViz"]).Select(x => x.Id).FirstOrDefaultAsync();
|
||||
if (ImportAlternativeWholeSalerId == 0)
|
||||
AddError(ApiErrorCode.NOT_FOUND, "AlternativeWholeSalerViz", $"'{(string)j["AlternativeWholeSalerViz"]}'");
|
||||
}
|
||||
}
|
||||
|
||||
List<string> ImportSerials = new List<string>();
|
||||
if (j["PartSerialsViz"] != null)
|
||||
{
|
||||
var raw = (string)j["PartSerialsViz"];
|
||||
if (!string.IsNullOrWhiteSpace(raw))
|
||||
ImportSerials = raw.Split(',').Select(x => x.Trim()).Where(x => !string.IsNullOrWhiteSpace(x)).ToList();
|
||||
}
|
||||
|
||||
|
||||
|
||||
long existingId = await ct.Part.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<Part>(jsset);
|
||||
Target.Tags.Add(ImportTag);
|
||||
|
||||
if (ImportManufacturerId != -1)
|
||||
Target.ManufacturerId = ImportManufacturerId;
|
||||
|
||||
if (ImportWholeSalerId != -1)
|
||||
Target.WholeSalerId = ImportWholeSalerId;
|
||||
|
||||
if (ImportAlternativeWholeSalerId != -1)
|
||||
Target.AlternativeWholeSalerId = ImportAlternativeWholeSalerId;
|
||||
|
||||
var res = await CreateAsync(Target);
|
||||
if (res == null)
|
||||
//something was specified, may be deliberate attempt to null it out so default to that
|
||||
ImportManufacturerId = null;
|
||||
if (!JsonUtil.JTokenIsNullOrEmpty(j["ManufacturerViz"]))
|
||||
{
|
||||
ImportResult.Add($"❌ {Target.Name}\r\n{this.GetErrorsAsString()}");
|
||||
this.ClearErrors();
|
||||
//a name was specified so attempt to find it
|
||||
ImportManufacturerId = await ct.Vendor.AsNoTracking().Where(z => z.Name == (string)j["ManufacturerViz"]).Select(x => x.Id).FirstOrDefaultAsync();
|
||||
if (ImportManufacturerId == 0)
|
||||
AddError(ApiErrorCode.NOT_FOUND, "ManufacturerViz", $"'{(string)j["ManufacturerViz"]}'");
|
||||
}
|
||||
else
|
||||
}
|
||||
|
||||
long? ImportWholeSalerId = -1;
|
||||
if (j["WholeSalerViz"] != null)
|
||||
{
|
||||
ImportWholeSalerId = null;
|
||||
if (!JsonUtil.JTokenIsNullOrEmpty(j["WholeSalerViz"]))
|
||||
{
|
||||
if (j["PartSerialsViz"] != null)
|
||||
await PutSerialsAsync(res.Id, ImportSerials);
|
||||
ImportResult.Add($"✔️ {Target.Name}");
|
||||
ImportWholeSalerId = await ct.Vendor.AsNoTracking().Where(z => z.Name == (string)j["WholeSalerViz"]).Select(x => x.Id).FirstOrDefaultAsync();
|
||||
if (ImportWholeSalerId == 0)
|
||||
AddError(ApiErrorCode.NOT_FOUND, "WholeSalerViz", $"'{(string)j["WholeSalerViz"]}'");
|
||||
}
|
||||
}
|
||||
|
||||
long? ImportAlternativeWholeSalerId = -1;
|
||||
if (j["AlternativeWholeSalerViz"] != null)
|
||||
{
|
||||
ImportAlternativeWholeSalerId = null;
|
||||
if (!JsonUtil.JTokenIsNullOrEmpty(j["AlternativeWholeSalerViz"]))
|
||||
{
|
||||
ImportAlternativeWholeSalerId = await ct.Vendor.AsNoTracking().Where(z => z.Name == (string)j["AlternativeWholeSalerViz"]).Select(x => x.Id).FirstOrDefaultAsync();
|
||||
if (ImportAlternativeWholeSalerId == 0)
|
||||
AddError(ApiErrorCode.NOT_FOUND, "AlternativeWholeSalerViz", $"'{(string)j["AlternativeWholeSalerViz"]}'");
|
||||
}
|
||||
}
|
||||
|
||||
List<string> ImportSerials = new List<string>();
|
||||
if (j["PartSerialsViz"] != null)
|
||||
{
|
||||
var raw = (string)j["PartSerialsViz"];
|
||||
if (!string.IsNullOrWhiteSpace(raw))
|
||||
ImportSerials = raw.Split(',').Select(x => x.Trim()).Where(x => !string.IsNullOrWhiteSpace(x)).ToList();
|
||||
}
|
||||
|
||||
|
||||
|
||||
long existingId = await ct.Part.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<Part>(jsset);
|
||||
Target.Tags.Add(ImportTag);
|
||||
|
||||
if (ImportManufacturerId != -1)
|
||||
Target.ManufacturerId = ImportManufacturerId;
|
||||
|
||||
if (ImportWholeSalerId != -1)
|
||||
Target.WholeSalerId = ImportWholeSalerId;
|
||||
|
||||
if (ImportAlternativeWholeSalerId != -1)
|
||||
Target.AlternativeWholeSalerId = ImportAlternativeWholeSalerId;
|
||||
|
||||
var res = await CreateAsync(Target);
|
||||
if (res == null)
|
||||
{
|
||||
ImportResult.Add($"❌ {Target.Name}\r\n{this.GetErrorsAsString()}");
|
||||
this.ClearErrors();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (j["PartSerialsViz"] != null)
|
||||
await PutSerialsAsync(res.Id, ImportSerials);
|
||||
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");
|
||||
ImportUtil.Update(Source, Target, propertiesToUpdate);
|
||||
if (ImportManufacturerId != -1)
|
||||
Target.ManufacturerId = ImportManufacturerId;
|
||||
if (ImportWholeSalerId != -1)
|
||||
Target.WholeSalerId = ImportWholeSalerId;
|
||||
if (ImportAlternativeWholeSalerId != -1)
|
||||
Target.AlternativeWholeSalerId = ImportAlternativeWholeSalerId;
|
||||
|
||||
var res = await PutAsync(Target);
|
||||
|
||||
if (res == null)
|
||||
{
|
||||
ImportResult.Add($"❌ {Target.Name} - {this.GetErrorsAsString()}");
|
||||
this.ClearErrors();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (j["PartSerialsViz"] != null)
|
||||
await PutSerialsAsync(res.Id, ImportSerials);
|
||||
ImportResult.Add($"✔️ {Target.Name}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
catch (Exception ex)
|
||||
{
|
||||
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");
|
||||
ImportUtil.Update(Source, Target, propertiesToUpdate);
|
||||
if (ImportManufacturerId != -1)
|
||||
Target.ManufacturerId = ImportManufacturerId;
|
||||
if (ImportWholeSalerId != -1)
|
||||
Target.WholeSalerId = ImportWholeSalerId;
|
||||
if (ImportAlternativeWholeSalerId != -1)
|
||||
Target.AlternativeWholeSalerId = ImportAlternativeWholeSalerId;
|
||||
|
||||
var res = await PutAsync(Target);
|
||||
|
||||
if (res == null)
|
||||
{
|
||||
ImportResult.Add($"❌ {Target.Name} - {this.GetErrorsAsString()}");
|
||||
this.ClearErrors();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (j["PartSerialsViz"] != null)
|
||||
await PutSerialsAsync(res.Id, ImportSerials);
|
||||
ImportResult.Add($"✔️ {Target.Name}");
|
||||
}
|
||||
}
|
||||
ImportResult.Add($"❌ Exception processing import\n record:{j.ToString()}\nError:{ex.Message}\nSource:{ex.Source}\nStack:{ex.StackTrace.ToString()}");
|
||||
}
|
||||
}
|
||||
return ImportResult;
|
||||
|
||||
Reference in New Issue
Block a user