using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using Dapper;
using DataModel.Models;
using DataModel.ViewModels;
namespace DataModel.Process
{
///
/// Lot tracking functions for LotFinderDB interface
///
public partial class LotFinderDB
{
///
/// Query to find lots with matching lot numbers
///
private const string SQL_LOOKUP_LOTS = @"
SELECT DISTINCT
l.LotNumber,
l.BranchCode,
l.ShortItemNumber,
l.ItemNumber,
l.SupplierCode,
l.LastUpdateDT
FROM dbo.Lot AS l INNER JOIN
@lotNumbers ln ON (l.LotNumber = ln.ComponentLotNumber AND
((l.ItemNumber IS NULL AND ln.ItemNumber IS NULL) OR l.ItemNumber = ln.ItemNumber))";
///
/// Finds lots with matching lot numbers
///
/// Lot numbers to match
/// List of matching lots
public static List LookupLots(List lots)
{
List results = new List();
try
{
//Create search filter parameter
DataTable dataTable = new DataTable();
dataTable.Columns.Add("ComponentLotNumber", typeof(string));
dataTable.Columns.Add("ItemNumber", typeof(string));
foreach (LotViewModel lot in lots)
{
dataTable.Rows.Add(lot.LotNumber, lot.ItemNumber);
}
using (SqlConnection connection = GetConnection())
{
results = connection.Query(SQL_LOOKUP_LOTS, new { lotNumbers = dataTable.AsTableValuedParameter("ComponentLotFilterParameter") }).ToList();
}
}
catch (Exception error)
{
//Log and forward exception
logger.Error("LookupLots: failed to find matching lots: {0}.", error.Message);
throw new Exception("LookupLots: failed to find matching lots in LotFinderDB.", error);
}
return results;
}
}
}