mirror of
https://github.com/stokebob/bnhtrade.git
synced 2026-03-19 06:27:15 +00:00
122 lines
4.1 KiB
C#
122 lines
4.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Microsoft.Data.SqlClient;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace bnhtrade.Core.Data.Database.AmazonShipment
|
|
{
|
|
public class ReadShipmentPrimaryKey : Connection
|
|
{
|
|
private bool enableCache = false;
|
|
private Dictionary<string, int> shipmentPKByAmazonShipmentIdDic;
|
|
private Dictionary<int, string> amazonShipmentIdByShipmentPKDic;
|
|
|
|
public ReadShipmentPrimaryKey()
|
|
{
|
|
}
|
|
|
|
public bool CacheEnabled
|
|
{
|
|
get { return enableCache; }
|
|
set
|
|
{
|
|
if (value && enableCache == false)
|
|
{
|
|
shipmentPKByAmazonShipmentIdDic = new Dictionary<string, int>();
|
|
amazonShipmentIdByShipmentPKDic = new Dictionary<int, string>();
|
|
}
|
|
else
|
|
{
|
|
shipmentPKByAmazonShipmentIdDic = null;
|
|
amazonShipmentIdByShipmentPKDic = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ClearCache()
|
|
{
|
|
if (CacheEnabled)
|
|
{
|
|
shipmentPKByAmazonShipmentIdDic.Clear();
|
|
amazonShipmentIdByShipmentPKDic.Clear();
|
|
}
|
|
}
|
|
|
|
private void DeleteCachedShipmentPK(int shipmentPrimaryKey)
|
|
{
|
|
if (CacheEnabled)
|
|
{
|
|
if (amazonShipmentIdByShipmentPKDic.ContainsKey(shipmentPrimaryKey))
|
|
{
|
|
string amazonShipmentId = amazonShipmentIdByShipmentPKDic[shipmentPrimaryKey];
|
|
shipmentPKByAmazonShipmentIdDic.Remove(amazonShipmentId);
|
|
amazonShipmentIdByShipmentPKDic.Remove(shipmentPrimaryKey);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrives table primary key 'AmazonShipmentID' for tblAmazonShipment
|
|
/// </summary>
|
|
/// <param name="amazonShipmentId">Amazon's inbound FBA shipment Id.</param>
|
|
/// <param name="forceDBQuery">Forces a database query when cache is enabled.</param>
|
|
/// <returns>Primary key or -1 if match isn't found.</returns>
|
|
public int ByAmazonShipmentId(string amazonShipmentId, bool forceDBQuery = false)
|
|
{
|
|
if (amazonShipmentId.Length < 5)
|
|
{
|
|
throw new Exception("Incorrect Amazon shipment if supplied '" + amazonShipmentId + "'");
|
|
}
|
|
|
|
// first, query class dictionary before sql call
|
|
if (CacheEnabled
|
|
&& forceDBQuery == false
|
|
&& shipmentPKByAmazonShipmentIdDic.ContainsKey(amazonShipmentId))
|
|
{
|
|
return shipmentPKByAmazonShipmentIdDic[amazonShipmentId];
|
|
}
|
|
|
|
int shipmentPK = -1;
|
|
using (SqlConnection conn = new SqlConnection(SqlConnectionString))
|
|
{
|
|
conn.Open();
|
|
|
|
using (SqlCommand cmd = new SqlCommand(@"
|
|
SELECT AmazonShipmentID
|
|
FROM tblAmazonShipment
|
|
WHERE (((tblAmazonShipment.ShipmentId)=@amazonShipmentId));
|
|
", conn))
|
|
{
|
|
cmd.Parameters.AddWithValue("@amazonShipmentId", amazonShipmentId);
|
|
|
|
object obj = cmd.ExecuteScalar();
|
|
|
|
if (obj == null || obj == DBNull.Value)
|
|
{
|
|
return shipmentPK;
|
|
}
|
|
else
|
|
{
|
|
shipmentPK = (int)obj;
|
|
}
|
|
}
|
|
}
|
|
UpdateCache(shipmentPK, amazonShipmentId);
|
|
return shipmentPK;
|
|
}
|
|
|
|
private void UpdateCache(int shipmentPK, string amazonShipmentId)
|
|
{
|
|
if (CacheEnabled)
|
|
{
|
|
DeleteCachedShipmentPK(shipmentPK);
|
|
|
|
shipmentPKByAmazonShipmentIdDic.Add(amazonShipmentId, shipmentPK);
|
|
amazonShipmentIdByShipmentPKDic.Add(shipmentPK, amazonShipmentId);
|
|
}
|
|
}
|
|
}
|
|
}
|