mirror of
https://github.com/stokebob/bnhtrade.git
synced 2026-03-21 07:17:15 +00:00
Feature repricing min max (#10)
amazon settlement import/export improvements
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
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.AmazonShipment
|
||||
{
|
||||
public class ReadShipmentPrimaryKey : Connection
|
||||
{
|
||||
private bool enableCache = false;
|
||||
private Dictionary<string, int> shipmentPKByAmazonShipmentIdDic;
|
||||
private Dictionary<int, string> amazonShipmentIdByShipmentPKDic;
|
||||
|
||||
public ReadShipmentPrimaryKey(string sqlConnectionString) : base(sqlConnectionString)
|
||||
{
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user