mirror of
https://github.com/stokebob/bnhtrade.git
synced 2026-03-19 14:37:16 +00:00
Feature repricing min max (#10)
amazon settlement import/export improvements
This commit is contained in:
148
src/bnhtrade.Core/Data/Database/AmazonFba/ReadShipmentInfo.cs
Normal file
148
src/bnhtrade.Core/Data/Database/AmazonFba/ReadShipmentInfo.cs
Normal file
@@ -0,0 +1,148 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Dapper;
|
||||
|
||||
namespace bnhtrade.Core.Data.Database.AmazonShipment
|
||||
{
|
||||
public class ReadShipmentInfo : Connection
|
||||
{
|
||||
private List<string> shipmentIdList;
|
||||
|
||||
public ReadShipmentInfo(string sqlConnectionString) : base(sqlConnectionString)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return active shipments only. Default is false.
|
||||
/// </summary>
|
||||
public bool ReturnOnlyActiveShipments { get; set; } = false;
|
||||
|
||||
private bool IsSetFbaShipmentIdList
|
||||
{
|
||||
get
|
||||
{
|
||||
if (FbaShipmentIdList == null || !FbaShipmentIdList.Any()) { return false; }
|
||||
else { return true; }
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Filter results by Amazon's shipment Id.
|
||||
/// </summary>
|
||||
private List<string> FbaShipmentIdList
|
||||
{
|
||||
get { return shipmentIdList; }
|
||||
set
|
||||
{
|
||||
if (value != null)
|
||||
{
|
||||
// clean list
|
||||
shipmentIdList = new List<string>();
|
||||
foreach (string item in value)
|
||||
{
|
||||
if (item.Length > 0)
|
||||
{
|
||||
shipmentIdList.Add(item);
|
||||
}
|
||||
}
|
||||
if (!FbaShipmentIdList.Any())
|
||||
{
|
||||
shipmentIdList = null;
|
||||
throw new Exception("Invalid shipment Id set");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Model.AmazonFba.ShipmentInfo HeaderByFbaShipmentId(string fbaShipmentId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(fbaShipmentId))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
var list = new List<string> { fbaShipmentId };
|
||||
var result = HeaderByFbaShipmentId(list);
|
||||
if (result.Any())
|
||||
{
|
||||
return result[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public List<Model.AmazonFba.ShipmentInfo> HeaderByFbaShipmentId(List<string> fbaShipmentIdList)
|
||||
{
|
||||
FbaShipmentIdList = fbaShipmentIdList;
|
||||
|
||||
if (IsSetFbaShipmentIdList)
|
||||
{
|
||||
string sql = @"
|
||||
AND tblAmazonShipment.ShipmentId IN @shipmentId ";
|
||||
|
||||
var parameters = new DynamicParameters();
|
||||
parameters.Add("@shipmentId", FbaShipmentIdList);
|
||||
|
||||
return HeaderInfo(sql, parameters);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new List<Model.AmazonFba.ShipmentInfo>();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrives table primary key 'AmazonShipmentID' for tblAmazonShipment
|
||||
/// </summary>
|
||||
/// <param name="amazonShipmentId">Amazon's inbound FBA shipment Id.</param>
|
||||
/// <returns>Primary key or -1 if match isn't found.</returns>
|
||||
private List<Model.AmazonFba.ShipmentInfo> HeaderInfo(string sqlWhere, DynamicParameters parameters)
|
||||
{
|
||||
// build the sql string
|
||||
string sql = @"
|
||||
SELECT tblAmazonShipment.AmazonShipmentID
|
||||
,tblAmazonShipment.ShipmentName
|
||||
,tblAmazonShipment.ShipmentId AS FbaShipmentId
|
||||
,tblAmazonShipment.CenterId
|
||||
,tblAmazonShipment.ShipmentStatus
|
||||
,tblAmazonShipment.LastUpdated
|
||||
,tblAmazonShipment.IsClosed
|
||||
,tblStockStatus.StockStatusID AS ShipmentStockStatusId
|
||||
,tblStockStatus.StockStatus AS ShipmentStockStatus
|
||||
FROM tblAmazonShipment
|
||||
LEFT OUTER JOIN tblStockStatus ON tblAmazonShipment.ShipmentStockStatusID = tblStockStatus.StockStatusID
|
||||
WHERE 1=1 ";
|
||||
|
||||
sql += sqlWhere;
|
||||
|
||||
if (ReturnOnlyActiveShipments)
|
||||
{
|
||||
sql = sql + @"
|
||||
AND IsClosed = 0";
|
||||
}
|
||||
|
||||
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
var result = conn.Query<Model.AmazonFba.ShipmentInfo>(sql, parameters).ToList();
|
||||
|
||||
// set datetime kind
|
||||
for (int i = 0; i < result.Count; i++)
|
||||
{
|
||||
if (result[i].IsSetLastUpdated())
|
||||
{
|
||||
result[i].LastUpdated = DateTime.SpecifyKind(result[i].LastUpdated, DateTimeKind.Utc);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
252
src/bnhtrade.Core/Data/Database/AmazonFba/SetShipmentInfo.cs
Normal file
252
src/bnhtrade.Core/Data/Database/AmazonFba/SetShipmentInfo.cs
Normal file
@@ -0,0 +1,252 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Transactions;
|
||||
|
||||
namespace bnhtrade.Core.Data.Database.AmazonShipment
|
||||
{
|
||||
public class SetShipmentInfo : Connection
|
||||
{
|
||||
private ReadShipmentPrimaryKey getPK;
|
||||
private Data.Database.Sku.GetSkuId skuIdLoopkup;
|
||||
|
||||
public SetShipmentInfo(string sqlConnectionString) : base(sqlConnectionString)
|
||||
{
|
||||
}
|
||||
|
||||
private ReadShipmentPrimaryKey GetPK
|
||||
{
|
||||
get
|
||||
{
|
||||
if (getPK == null)
|
||||
{
|
||||
getPK = new ReadShipmentPrimaryKey(sqlConnectionString);
|
||||
}
|
||||
return getPK;
|
||||
}
|
||||
set
|
||||
{
|
||||
getPK = value;
|
||||
}
|
||||
}
|
||||
|
||||
private Data.Database.Sku.GetSkuId SkuIdLoopkup
|
||||
{
|
||||
get
|
||||
{
|
||||
if (skuIdLoopkup == null)
|
||||
{
|
||||
skuIdLoopkup = new Sku.GetSkuId(sqlConnectionString);
|
||||
}
|
||||
return skuIdLoopkup;
|
||||
}
|
||||
}
|
||||
|
||||
public void Excecute(Model.AmazonFba.ShipmentInfo info)
|
||||
{
|
||||
using (var scope = new TransactionScope())
|
||||
{
|
||||
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
// input checks
|
||||
if (!info.IsSetAll())
|
||||
{
|
||||
throw new Exception("Insurficent ShipmentInfo parameters.");
|
||||
}
|
||||
|
||||
// get tablePK
|
||||
int shipmentPK = GetPK.ByAmazonShipmentId(info.FbaShipmentId);
|
||||
|
||||
// add or update shipment header info
|
||||
if (shipmentPK == -1)
|
||||
{
|
||||
shipmentPK = InsertShipmentHeaderInfo(info);
|
||||
}
|
||||
else
|
||||
{
|
||||
UpdateShipmentHeaderInfo(info);
|
||||
DeleteShipmentItems(shipmentPK);
|
||||
}
|
||||
|
||||
// insert new shipment item info
|
||||
foreach (var item in info.ShipmentItemInfoList)
|
||||
{
|
||||
int skuId = SkuIdLoopkup.BySKUNumber(item.SKUNumber);
|
||||
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
INSERT INTO tblAmazonShipmentItem (
|
||||
AmazonShipmentID
|
||||
,SkuID
|
||||
,QuantityAllocated
|
||||
,QuantityReceived
|
||||
)
|
||||
VALUES (
|
||||
@amazonShipmentID
|
||||
,@skuID
|
||||
,@quantityAllocated
|
||||
,@quantityReceived
|
||||
)
|
||||
", conn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@amazonShipmentID", shipmentPK);
|
||||
cmd.Parameters.AddWithValue("@skuID", skuId);
|
||||
cmd.Parameters.AddWithValue("@quantityAllocated", item.QuantityAllocated);
|
||||
cmd.Parameters.AddWithValue("@quantityReceived", item.QuantityReceived);
|
||||
|
||||
int effected = (int)cmd.ExecuteNonQuery();
|
||||
|
||||
if (effected == 0)
|
||||
{
|
||||
throw new Exception("Error, no tblAmazonShipment was not updated.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
scope.Complete();
|
||||
}
|
||||
}
|
||||
|
||||
public void ExcecuteByList(List<Model.AmazonFba.ShipmentInfo> infoList)
|
||||
{
|
||||
using (var scope = new TransactionScope())
|
||||
{
|
||||
foreach (var item in infoList)
|
||||
{
|
||||
Excecute(item);
|
||||
}
|
||||
scope.Complete();
|
||||
}
|
||||
}
|
||||
|
||||
private void DeleteShipmentItems(int shipmentId)
|
||||
{
|
||||
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
DELETE FROM
|
||||
tblAmazonShipmentItem
|
||||
WHERE
|
||||
AmazonShipmentId = @shipmentId
|
||||
", conn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@shipmentId", shipmentId);
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private int InsertShipmentHeaderInfo(Model.AmazonFba.ShipmentInfo info)
|
||||
{
|
||||
if (!info.IsSetAll())
|
||||
{
|
||||
throw new Exception("Unsuficent properties set in Shipment Header Info.");
|
||||
}
|
||||
|
||||
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
if (GetPK.ByAmazonShipmentId(info.FbaShipmentId) != -1)
|
||||
{
|
||||
throw new Exception("Shipment insert failed, shipment with same Amazon Id already exists.");
|
||||
}
|
||||
|
||||
// get next shipment Number in sequence and create shipment name
|
||||
int sequenceNumber;
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
SELECT NEXT VALUE FOR dbo.ShipmentCountSequence AS SequenceNumber
|
||||
", conn))
|
||||
{
|
||||
sequenceNumber = (int)cmd.ExecuteScalar();
|
||||
}
|
||||
// info.ShipmentName = "FBA_Shipment_" + sequenceNumber.ToString().PadLeft(4, '0') + "_" + info.DestinationFulfillmentCenterId;
|
||||
|
||||
// make the insert
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
INSERT INTO tblAmazonShipment (
|
||||
AmazonShipmentCount
|
||||
,ShipmentName
|
||||
,ShipmentId
|
||||
,CenterId
|
||||
,ShipmentStatus
|
||||
,LastUpdated
|
||||
,IsClosed
|
||||
)
|
||||
OUTPUT INSERTED.AmazonShipmentID
|
||||
VALUES (
|
||||
@amazonShipmentCount
|
||||
,@shipmentName
|
||||
,@shipmentId
|
||||
,@centerId
|
||||
,@shipmentStatus
|
||||
,@lastUpdated
|
||||
,@isClosed
|
||||
)
|
||||
", conn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@amazonShipmentCount", sequenceNumber);
|
||||
cmd.Parameters.AddWithValue("@shipmentName", info.ShipmentName);
|
||||
cmd.Parameters.AddWithValue("@shipmentId", info.FbaShipmentId);
|
||||
cmd.Parameters.AddWithValue("@centerId", info.DestinationFulfillmentCenterId);
|
||||
cmd.Parameters.AddWithValue("@shipmentStatus", info.ShipmentStatus);
|
||||
cmd.Parameters.AddWithValue("@lastUpdated", info.LastUpdated.ToUniversalTime());
|
||||
cmd.Parameters.AddWithValue("@isClosed", info.ShipmentIsClosed);
|
||||
|
||||
int tablePk = (int)cmd.ExecuteScalar();
|
||||
|
||||
// update cache and return
|
||||
return tablePk;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateShipmentHeaderInfo(Model.AmazonFba.ShipmentInfo info)
|
||||
{
|
||||
int tablePK = GetPK.ByAmazonShipmentId(info.FbaShipmentId);
|
||||
if (tablePK == -1)
|
||||
{
|
||||
throw new Exception("Shipment insert failed, shipment with same Amazon Id already exists.");
|
||||
}
|
||||
|
||||
// make the update
|
||||
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
UPDATE tblAmazonShipment
|
||||
SET
|
||||
ShipmentName = @shipmentName
|
||||
,ShipmentStatus = @shipmentStatus
|
||||
,LastUpdated = @lastUpdated
|
||||
,IsClosed = @isClosed
|
||||
WHERE
|
||||
AmazonShipmentID = @tablePK
|
||||
", conn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@shipmentName", info.ShipmentName);
|
||||
cmd.Parameters.AddWithValue("@shipmentStatus", info.ShipmentStatus);
|
||||
cmd.Parameters.AddWithValue("@lastUpdated", info.LastUpdated.ToUniversalTime());
|
||||
cmd.Parameters.AddWithValue("@isClosed", info.ShipmentIsClosed);
|
||||
cmd.Parameters.AddWithValue("@tablePK", tablePK);
|
||||
|
||||
int count = cmd.ExecuteNonQuery();
|
||||
|
||||
if (count == 0)
|
||||
{
|
||||
throw new Exception("No records updated");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user