232 lines
8.4 KiB
C#
232 lines
8.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace AAIntegration.SimmonsBank.API.Entities;
|
|
|
|
public class AutoclassExpression
|
|
{
|
|
public int Id { get; set; }
|
|
public AutoclassTransactionField TransactionField { get; set; }
|
|
public AutoclassCompareOperator CompareOperator { get; set; }
|
|
public string Value { get; set; }
|
|
|
|
public bool Evaluate(Transaction transaction)
|
|
{
|
|
switch (TransactionField)
|
|
{
|
|
case AutoclassTransactionField.DATE:
|
|
return EvaluateDateType(transaction.Date);
|
|
|
|
case AutoclassTransactionField.EXTERNAL_ID:
|
|
return EvaluateStringType(transaction.ExternalId);
|
|
|
|
case AutoclassTransactionField.DESCRIPTION:
|
|
return EvaluateStringType(transaction.Description);
|
|
|
|
case AutoclassTransactionField.AMOUNT:
|
|
return EvaluateDecimalType(transaction.Amount);
|
|
|
|
case AutoclassTransactionField.IS_PENDING:
|
|
return EvaluateBoolType(transaction.IsPending);
|
|
|
|
case AutoclassTransactionField.DEBIT_ACCOUNT:
|
|
return EvaluateAccountType(transaction.DebitAccount);
|
|
|
|
case AutoclassTransactionField.CREDIT_ACCOUNT:
|
|
return EvaluateAccountType(transaction.CreditAccount);
|
|
|
|
case AutoclassTransactionField.DEBIT_ENVELOPE:
|
|
return EvaluateEnvelopeType(transaction.DebitEnvelope);
|
|
|
|
case AutoclassTransactionField.CREDIT_ENVELOPE:
|
|
return EvaluateEnvelopeType(transaction.CreditEnvelope);
|
|
|
|
case AutoclassTransactionField.CURRENCY_TYPE:
|
|
return EvaluateCurrencyType(transaction.CurrencyType);
|
|
|
|
case AutoclassTransactionField.TAGS:
|
|
return EvaluateStringArrayType(transaction.Tags);
|
|
|
|
default:
|
|
throw new Exception($"AutoclassExpression::Evaluate(Transaction) couldn't recognize the field '{TransactionField}'.");
|
|
}
|
|
}
|
|
|
|
private bool EvaluateDateType(DateTime currentValue)
|
|
{
|
|
/*DateTime expValue = Convert.ToDateTime(Value);
|
|
var dt = DateTime.ParseExact(Value , "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);*/
|
|
DateTime expValue = DateTime.Parse(Value, null, System.Globalization.DateTimeStyles.RoundtripKind);
|
|
|
|
switch (CompareOperator)
|
|
{
|
|
case AutoclassCompareOperator.EQUAL:
|
|
return currentValue.Date == expValue.Date;
|
|
case AutoclassCompareOperator.NOT_EQUAL:
|
|
return currentValue.Date != expValue.Date;
|
|
case AutoclassCompareOperator.GREATER_THAN:
|
|
return currentValue.Date > expValue.Date;
|
|
case AutoclassCompareOperator.GREATER_THAN_OR_EQUAL:
|
|
return currentValue.Date >= expValue.Date;
|
|
case AutoclassCompareOperator.LESS_THAN:
|
|
return currentValue.Date < expValue.Date;
|
|
case AutoclassCompareOperator.LESS_THAN_OR_EQUAL:
|
|
return currentValue.Date <= expValue.Date;
|
|
default:
|
|
throw new Exception($"Invalid compare operator '{CompareOperator}' for type 'DateTime'");
|
|
}
|
|
}
|
|
|
|
private bool EvaluateStringType(string currentValue)
|
|
{
|
|
switch (CompareOperator)
|
|
{
|
|
case AutoclassCompareOperator.EQUAL:
|
|
return currentValue == Value;
|
|
case AutoclassCompareOperator.NOT_EQUAL:
|
|
return currentValue != Value;
|
|
case AutoclassCompareOperator.CONTAINS:
|
|
return currentValue.Contains(Value);
|
|
case AutoclassCompareOperator.CONTAINS_INSENSITIVE:
|
|
return currentValue.ToUpper().Contains(Value.ToUpper());
|
|
default:
|
|
throw new Exception($"Invalid compare operator '{CompareOperator}' for type 'string'");
|
|
}
|
|
}
|
|
|
|
private bool EvaluateDecimalType(decimal currentValue)
|
|
{
|
|
decimal expValue = Convert.ToDecimal(Value);
|
|
|
|
switch (CompareOperator)
|
|
{
|
|
case AutoclassCompareOperator.EQUAL:
|
|
return currentValue == expValue;
|
|
case AutoclassCompareOperator.NOT_EQUAL:
|
|
return currentValue != expValue;
|
|
case AutoclassCompareOperator.GREATER_THAN:
|
|
return currentValue > expValue;
|
|
case AutoclassCompareOperator.GREATER_THAN_OR_EQUAL:
|
|
return currentValue >= expValue;
|
|
case AutoclassCompareOperator.LESS_THAN:
|
|
return currentValue < expValue;
|
|
case AutoclassCompareOperator.LESS_THAN_OR_EQUAL:
|
|
return currentValue <= expValue;
|
|
default:
|
|
throw new Exception($"Invalid compare operator '{CompareOperator}' for type 'decimal'");
|
|
}
|
|
}
|
|
|
|
private bool EvaluateBoolType(bool currentValue)
|
|
{
|
|
bool expValue = Convert.ToBoolean(Value);
|
|
|
|
switch (CompareOperator)
|
|
{
|
|
case AutoclassCompareOperator.EQUAL:
|
|
return currentValue == expValue;
|
|
case AutoclassCompareOperator.NOT_EQUAL:
|
|
return currentValue != expValue;
|
|
default:
|
|
throw new Exception($"Invalid compare operator '{CompareOperator}' for type 'boolean'");
|
|
}
|
|
}
|
|
|
|
private bool EvaluateAccountType(Account currentValue)
|
|
{
|
|
int expValue = Convert.ToInt32(Value);
|
|
|
|
switch (CompareOperator)
|
|
{
|
|
case AutoclassCompareOperator.EQUAL:
|
|
if (currentValue == null)
|
|
return expValue == 0;
|
|
else
|
|
return currentValue.Id == expValue;
|
|
case AutoclassCompareOperator.NOT_EQUAL:
|
|
if (currentValue == null)
|
|
return expValue != 0;
|
|
else
|
|
return currentValue.Id != expValue;
|
|
default:
|
|
throw new Exception($"Invalid compare operator '{CompareOperator}' for type 'Account'");
|
|
}
|
|
}
|
|
|
|
//evaluateEnvelopeType
|
|
|
|
private bool EvaluateEnvelopeType(Envelope currentValue)
|
|
{
|
|
int expValue = Convert.ToInt32(Value);
|
|
|
|
switch (CompareOperator)
|
|
{
|
|
case AutoclassCompareOperator.EQUAL:
|
|
if (currentValue == null)
|
|
return expValue == 0;
|
|
else
|
|
return currentValue.Id == expValue;
|
|
case AutoclassCompareOperator.NOT_EQUAL:
|
|
if (currentValue == null)
|
|
return expValue != 0;
|
|
else
|
|
return currentValue.Id != expValue;
|
|
default:
|
|
throw new Exception($"Invalid compare operator '{CompareOperator}' for type 'Envelope'");
|
|
}
|
|
}
|
|
|
|
private bool EvaluateCurrencyType(CurrencyType currentValue)
|
|
{
|
|
int expValue = Convert.ToInt32(Value);
|
|
|
|
switch (CompareOperator)
|
|
{
|
|
case AutoclassCompareOperator.EQUAL:
|
|
if (currentValue == null)
|
|
return expValue == 0;
|
|
else
|
|
return currentValue.Id == expValue;
|
|
case AutoclassCompareOperator.NOT_EQUAL:
|
|
if (currentValue == null)
|
|
return expValue != 0;
|
|
else
|
|
return currentValue.Id != expValue;
|
|
default:
|
|
throw new Exception($"Invalid compare operator '{CompareOperator}' for type 'CurrencyType'");
|
|
}
|
|
}
|
|
|
|
private bool EvaluateStringArrayType(List<string> currentValue)
|
|
{
|
|
switch (CompareOperator)
|
|
{
|
|
case AutoclassCompareOperator.CONTAINS:
|
|
if (currentValue == null)
|
|
return false;
|
|
else
|
|
return currentValue.Any(s => s == Value);
|
|
case AutoclassCompareOperator.CONTAINS_INSENSITIVE:
|
|
if (currentValue == null)
|
|
return false;
|
|
else
|
|
return currentValue.Any(s => s.ToUpper() == Value.ToUpper());
|
|
default:
|
|
throw new Exception($"Invalid compare operator '{CompareOperator}' for type 'string[]'");
|
|
}
|
|
}
|
|
}
|
|
|
|
public enum AutoclassCompareOperator
|
|
{
|
|
EQUAL,
|
|
NOT_EQUAL,
|
|
GREATER_THAN,
|
|
LESS_THAN,
|
|
GREATER_THAN_OR_EQUAL,
|
|
LESS_THAN_OR_EQUAL,
|
|
CONTAINS,
|
|
CONTAINS_INSENSITIVE,
|
|
} |