@ -1,23 +1,26 @@
using Gremlin_BlazorServer.Data. DB Classes;
using Gremlin_BlazorServer. Data.EntityClass es;
using Gremlin_BlazorServer.Data. Entity Classes;
using Gremlin_BlazorServer. Pag es;
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 ) )
{
List < Account > listOfAccounts = await Task . Run ( ( ) = > ParseListToAccounts ( splitLines ) ) ;
return genericController . Insert ( listOfAccounts ) ;
//foreach (Account account in listOfAccounts)
//{
// _ = genericController.Insert(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 ;
@ -36,9 +39,9 @@ public class GenericImporter
return fileList ;
}
private List < Account > ParseListToAccounts ( IList < string [ ] > lineList )
private void ParseListToAccounts ( IList < string [ ] > lineList ) // ID;Acct Name 1 and 2;Street;City;BP Role;Postal Code;Customer Type;Market Indicator;
{
List < Account > listOf Accounts = new ( ) ;
List < Account > existing Accounts = new ( ) ;
foreach ( string [ ] line in lineList )
{
@ -47,14 +50,7 @@ public class GenericImporter
if ( uint . TryParse ( line [ 0 ] , out uint sapAccountNumber ) & & uint . TryParse ( line [ 5 ] , out uint zip ) ) //HACK: skip lines with wrong uint
{
if ( AccountExists ( sapAccountNumber ) )
{
//TODO: Check for changes
Console . WriteLine ( $"Account with SapAccountNumber {sapAccountNumber} already exists..." ) ;
continue ;
} ;
Account account = new ( )
Account readAccount = new ( )
{
SapAccountNumber = sapAccountNumber ,
AccountName = line [ 1 ] ,
@ -63,29 +59,62 @@ public class GenericImporter
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 ;
account . AccountType . AccountTypeCode = accountType . AccountTypeCode ;
account . AccountType = genericController . Get < AccountType > ( aT = > aT . AccountTypeCode = = account . AccountType . AccountTypeCode ) ;
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 ; }
account . SubMarket . SubMarketCode = subMarket . SubMarketCode ;
account . SubMarket = genericController . Get < SubMarket > ( sM = > sM . SubMarketCode = = account . SubMarket . SubMarketCode ) ;
if ( account ? . SubMarket . SubMarketCode = = null | | account ? . AccountType ? . AccountTypeCode = = null ) continue ; //HACK: skip Accounts with no SubMarket or AccountType
readAccount . SubMarket . SubMarketCode = subMarket . SubMarketCode ;
readAccount . SubMarket = genericController . Get < SubMarket > ( sM = > sM . SubMarketCode = = readAccount . SubMarket . SubMarketCode ) ;
Console . WriteLine ( $"Found new account {account.SapAccountNumber}:{account.AccountName}!" ) ;
listOfAccounts . Add ( account ) ;
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 ) ;
}
}
}
}
return listOfAccounts ;
}
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 )