Gremlin/Gremlin_BlazorServer/Services/GenericImporter.cs

123 lines
4.5 KiB
C#

using Gremlin_BlazorServer.Data.EntityClasses;
namespace Gremlin_BlazorServer.Services;
public class GenericImporter
{
private readonly GenericController genericController = new();
private List<Account> newAccounts = new();
private List<Account> updatedAccounts = new();
public async Task<bool> ImportCsvAsync<T>(string fileContent) where T : class, IMetadata
{
Console.WriteLine($"Importing accounts from csv...");
IList<string[]> splitLines = await Task.Run(() => SplitLines(fileContent));
if (typeof(T) == typeof(Account))
{
ParseListToAccounts(splitLines);
int countNewAccounts = genericController.Insert(newAccounts);
int countUpdatedAccounts = genericController.Update(updatedAccounts);
Console.WriteLine($"Added {countNewAccounts} new Accounts to database and updated {updatedAccounts.Count} Accounts.");
return countNewAccounts > 0 || updatedAccounts.Count > 0;
}
return false;
}
private static IList<string[]> SplitLines(string fileContent)
{
IEnumerable<string> fileLines = fileContent.Split(Environment.NewLine.ToCharArray());
IList<string[]> fileList = (
from clipboardLine in fileLines
where clipboardLine != ""
select clipboardLine.Split(';')
).ToList();
return fileList;
}
private void ParseListToAccounts(IList<string[]> lineList) // ID;Acct Name 1 and 2;Street;City;BP Role;Postal Code;Customer Type;Market Indicator;
{
List<Account> existingAccounts = new();
foreach (string[] line in lineList)
{
if (!line[0].Contains("ID")) //HACK: skip first row if header
{
if (uint.TryParse(line[0], out uint sapAccountNumber) && uint.TryParse(line[5], out uint zip)) //HACK: skip lines with wrong uint
{
Account readAccount = new()
{
SapAccountNumber = sapAccountNumber,
AccountName = line[1],
Street = line[2],
City = line[3].ToUpper().First() + line[3].Substring(1).ToLower(),
Zip = zip,
AccountType = new(),
SubMarket = new(),
PhoneNumber = line[8],
EMail = line[9],
// ParentAccountId = 0,
DataModificationByUser = "Gremlin Generic Importer"
};
AccountType? accountType = genericController.Get<AccountType>(aT => aT.AccountTypeCode == line[6]);
if (accountType == null) { continue; }
readAccount.AccountType.AccountTypeCode = accountType.AccountTypeCode;
readAccount.AccountType = genericController.Get<AccountType>(aT => aT.AccountTypeCode == readAccount.AccountType.AccountTypeCode);
SubMarket? subMarket = genericController.Get<SubMarket>(sM => sM.SubMarketCode == line[7]);
if (subMarket == null) { continue; }
readAccount.SubMarket.SubMarketCode = subMarket.SubMarketCode;
readAccount.SubMarket = genericController.Get<SubMarket>(sM => sM.SubMarketCode == readAccount.SubMarket.SubMarketCode);
if (readAccount?.SubMarket?.SubMarketCode == null || readAccount?.AccountType?.AccountTypeCode == null) continue; //HACK: skip Accounts with no SubMarket or AccountType
if (AccountExists(sapAccountNumber))
{
Account? existingAccount = genericController.Get<Account>(a => a.SapAccountNumber == readAccount.SapAccountNumber);
if (existingAccount == null) continue; //Account does not exist
if (!IsEqualAccount(readAccount, existingAccount))
{
existingAccount.DataModificationDate = DateTime.Now;
existingAccount.DataVersionNumber++;
existingAccount.DataModificationByUser = "Updated by Gremlin Generic Importer";
existingAccount.AccountName = readAccount.AccountName;
existingAccount.City = readAccount.City;
existingAccount.EMail = readAccount.EMail;
existingAccount.PhoneNumber = readAccount.PhoneNumber;
existingAccount.Street = readAccount.Street;
existingAccount.Zip = readAccount.Zip;
//genericController.Update(existingAccount);
Console.WriteLine($"Update in Account {existingAccount.SapAccountNumber}:{existingAccount.AccountName}");
updatedAccounts.Add(existingAccount);
}
}
else
{
newAccounts.Add(readAccount);
}
}
}
}
}
private static bool IsEqualAccount(Account account, Account existingAccount)
{
return account.AccountName == existingAccount.AccountName
&& account.City == existingAccount.City
&& account.EMail == existingAccount.EMail
&& account.PhoneNumber == existingAccount.PhoneNumber
&& account.Street == existingAccount.Street
&& account.Zip == existingAccount.Zip;
}
private bool AccountExists(uint sapAccountNumber)
{
return genericController.IsExisting<Account>(a => a.SapAccountNumber == sapAccountNumber);
}
}