using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using Dapper;
using DataModel.Models;
namespace DataModel.Process
{
///
/// Profit center tracking functions for LotFinderDB interface
///
public partial class LotFinderDB
{
///
/// Query to find profit centers with matching code or description
///
private const string SQL_SEARCH_PROFIT_CENTERS = @"
SELECT TOP 25
pc.Code,
pc.Description,
pc.LastUpdateDT
FROM dbo.ProfitCenter AS pc
WHERE pc.Code LIKE '%' + @filter + '%' OR
pc.Description LIKE '%' + @filter + '%'
ORDER BY pc.Code";
///
/// Finds profit centers with matching code or description
///
/// Search filter
/// Profit centers with matching code or description
public static List SearchProfitCenters(string filter)
{
List results = new List();
try
{
using (SqlConnection connection = GetConnection())
{
results.AddRange(connection.Query(SQL_SEARCH_PROFIT_CENTERS, new { filter }));
}
}
catch (Exception error)
{
//Log and forward exception
logger.Error("SearchProfitCenters: failed to search for profit centers '{0}': {1}.", filter, error.Message);
throw new Exception($"Failed to search for profit centers '{filter}' from LotFinderDB.", error);
}
return results;
}
///
/// Query to find profit centers with matching profit center codes
///
private const string SQL_LOOKUP_PROFIT_CENTERS = @"
SELECT pc.Code,
pc.Description,
pc.LastUpdateDT
FROM dbo.ProfitCenter AS pc INNER JOIN
@profitCenterCodes AS pc2 ON (pc.Code = pc2.Code)
ORDER BY pc.Code";
///
/// Finds profit centers with matching profit center codes
///
/// Profit center codes to match
/// List of matching profit centers
public static List LookupProfitCenters(List profitCenterCodes)
{
List results = new List();
try
{
//Create search filter parameter
DataTable dataTable = new DataTable();
dataTable.Columns.Add("Code", typeof(string));
foreach (string profitCenter in profitCenterCodes)
{
dataTable.Rows.Add(profitCenter);
}
using (SqlConnection connection = GetConnection())
{
results = connection.Query(SQL_LOOKUP_PROFIT_CENTERS, new { profitCenterCodes = dataTable.AsTableValuedParameter("ProfitCenterFilterParameter") }).ToList();
}
}
catch (Exception error)
{
//Log and forward exception
logger.Error("LookupProfitCenters: failed to find matching profit centers: {0}.", error.Message);
throw new Exception("LookupProfitCenters: failed to find matching profit centers in LotFinderDB.", error);
}
return results;
}
}
}