using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using Dapper; using DataModel.Models; namespace DataModel.Process { /// /// Work 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_WORK_CENTERS = @" SELECT TOP 25 wc.Code, wc.Description, wc.LastUpdateDT FROM dbo.WorkCenter AS wc WHERE wc.Code LIKE '%' + @filter + '%' OR wc.Description LIKE '%' + @filter + '%' ORDER BY wc.Code"; /// /// Finds work centers with matching code or description /// /// Search filter /// Work centers with matching code or description public static List SearchWorkCenters(string filter) { List results = new List(); try { using (SqlConnection connection = GetConnection()) { results.AddRange(connection.Query(SQL_SEARCH_WORK_CENTERS, new { filter })); } } catch (Exception error) { //Log and forward exception logger.Error("SearchWorkCenters: failed to search for work centers '{0}': {1}.", filter, error.Message); throw new Exception($"Failed to search for work centers '{filter}' from LotFinderDB.", error); } return results; } /// /// Query to find work orders with matching work order numbers /// private const string SQL_LOOKUP_WORK_CENTERS = @" SELECT wc.Code, wc.Description, wc.LastUpdateDT FROM dbo.WorkCenter AS wc INNER JOIN @workCenterCodes wc2 ON (wc.Code = wc2.Code) ORDER BY wc.Code"; /// /// Finds work centers with matching work center codes /// /// Work center codes to match /// List of matching work centers public static List LookupWorkCenters(List workCenterCodes) { List results = new List(); try { //Create search filter parameter DataTable dataTable = new DataTable(); dataTable.Columns.Add("Code", typeof(string)); foreach (string workCenter in workCenterCodes) { dataTable.Rows.Add(workCenter); } using (SqlConnection connection = GetConnection()) { results = connection.Query(SQL_LOOKUP_WORK_CENTERS, new { workCenterCodes = dataTable.AsTableValuedParameter("WorkCenterFilterParameter") }).ToList(); } } catch (Exception error) { //Log and forward exception logger.Error("LookupWorkCenters: failed to find matching work centers: {0}.", error.Message); throw new Exception("LookupWorkCenters: failed to find matching work centers in LotFinderDB.", error); } return results; } } }