Various bug fixs and improvements to stock SKU reconciliation

This commit is contained in:
Bobbie Hodgetts
2020-10-05 22:40:55 +01:00
parent cc2534a3e1
commit ddd2b62743
25 changed files with 1026 additions and 467 deletions

View File

@@ -0,0 +1,103 @@
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace bnhtrade.Core.Data.Database
{
public class SqlWhereBuilder
{
private int parameterCount;
public SqlWhereBuilder()
{
Innit();
}
public string SqlWhereString { get; private set; }
public Dictionary<string, object> ParameterList { get; private set; }
public void Innit()
{
SqlWhereString = null;
ParameterList = new Dictionary<string, object>();
parameterCount = 0;
}
public void AddParametersToSqlCommand(SqlCommand cmd)
{
foreach (var item in ParameterList)
{
cmd.Parameters.AddWithValue(item.Key, item.Value);
}
}
/// <summary>
/// Used to create a string for an SQL where condition 'In' statement
/// </summary>
/// <param name="columnReference">Name of the column to used to for the condition statement</param>
/// <param name="orValueList">List of values to test in condition statement</param>
/// <param name="wherePrefix">Optional string to prefix to the returned result</param>
public void In(string columnReference, List<string> orValueList, string wherePrefix = null)
{
Innit();
if (orValueList == null || !orValueList.Any())
{
return;
}
string sqlWhere = @"
";
if (wherePrefix != null)
{
sqlWhere += wherePrefix;
}
sqlWhere += " " + columnReference + " IN ( ";
var paramters = new Dictionary<string, object>();
int listCount = orValueList.Count();
for (int i = 0; i < listCount; i++, parameterCount++)
{
if (i > 0)
{
sqlWhere += ", ";
}
string param = "@parameter" + parameterCount;
sqlWhere += param;
paramters.Add(param, orValueList[i]);
}
sqlWhere += " )";
SqlWhereString = sqlWhere;
ParameterList = paramters;
}
/// <summary>
/// Used to create a string for an SQL where condition 'In' statement
/// </summary>
/// <param name="columnReference">Name of the column to used to for the condition statement</param>
/// <param name="orValueList">List of values to test in condition statement</param>
/// <param name="wherePrefix">Optional string to prefix to the returned result</param>
public void In(string columnReference, List<int> orValueList, string wherePrefix = null)
{
var stringList = new List<string>();
if (orValueList != null || !orValueList.Any())
{
foreach (int value in orValueList)
{
stringList.Add(value.ToString());
}
}
In(columnReference, stringList, wherePrefix);
}
}
}