mirror of
https://github.com/stokebob/bnhtrade.git
synced 2026-03-19 22:47:15 +00:00
Export amazon settlement report fix
This commit is contained in:
137
src/bnhtrade.Core/Data/Database/Account/ReadAccountCode.cs
Normal file
137
src/bnhtrade.Core/Data/Database/Account/ReadAccountCode.cs
Normal file
@@ -0,0 +1,137 @@
|
||||
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.Account
|
||||
{
|
||||
public class ReadAccountCode : Connection
|
||||
{
|
||||
private bool allRetrived;
|
||||
private Dictionary<int, Model.Account.AccountCode> cache;
|
||||
protected ReadAccountCode(string sqlConnectionString) : base(sqlConnectionString)
|
||||
{
|
||||
allRetrived = false;
|
||||
cache = new Dictionary<int, Model.Account.AccountCode>();
|
||||
}
|
||||
protected void ClearCache()
|
||||
{
|
||||
allRetrived = false;
|
||||
cache.Clear();
|
||||
}
|
||||
public List<Model.Account.AccountCode> GetAll()
|
||||
{
|
||||
if (allRetrived == false)
|
||||
{
|
||||
UpdateCache(-1);
|
||||
allRetrived = true;
|
||||
}
|
||||
|
||||
var returnList = new List<Model.Account.AccountCode>();
|
||||
foreach (var item in cache)
|
||||
{
|
||||
returnList.Add(item.Value);
|
||||
}
|
||||
|
||||
return returnList;
|
||||
}
|
||||
public Model.Account.AccountCode GetByAccountCode(int accountCode)
|
||||
{
|
||||
if (cache.ContainsKey(accountCode))
|
||||
{
|
||||
return cache[accountCode];
|
||||
}
|
||||
else if (allRetrived)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
UpdateCache(accountCode);
|
||||
if (cache.ContainsKey(accountCode))
|
||||
{
|
||||
return cache[accountCode];
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
protected void UpdateCache(int accountCode = -1)
|
||||
{
|
||||
var dicCache = new Dictionary<int, Model.Account.AccountCode>();
|
||||
|
||||
bool whereClause = false;
|
||||
//build sql query
|
||||
string sqlString = @"
|
||||
SELECT tblAccountChartOf.AccountChartOfID
|
||||
,tblAccountChartOf.AccountCode
|
||||
,tblAccountChartOf.AccountName
|
||||
,tblAccountChartOf.Description
|
||||
,tblAccountChartOfType.AccountChartOfType
|
||||
,tblAccountChartOfType.BasicType
|
||||
FROM tblAccountChartOf
|
||||
INNER JOIN tblAccountChartOfType ON tblAccountChartOf.AccountChartOfTypeID = tblAccountChartOfType.AccountChartOfTypeID";
|
||||
if (accountCode > -1)
|
||||
{
|
||||
whereClause = true;
|
||||
sqlString = sqlString + @"
|
||||
WHERE (((tblAccountChartOf.AccountCode) = @accountCode))";
|
||||
}
|
||||
|
||||
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
using (SqlCommand cmd = new SqlCommand(sqlString, conn))
|
||||
{
|
||||
if (whereClause)
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@accountCode", accountCode);
|
||||
}
|
||||
|
||||
using (SqlDataReader reader = cmd.ExecuteReader())
|
||||
{
|
||||
if (reader.HasRows)
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
var result = new Model.Account.AccountCode();
|
||||
|
||||
int tablePk = reader.GetInt32(0);
|
||||
result.AccountCodeId = reader.GetInt32(1);
|
||||
result.Title = reader.GetString(2);
|
||||
if (!reader.IsDBNull(3)) { result.Description = reader.GetString(3); }
|
||||
result.Type = reader.GetString(4);
|
||||
result.BasicType = reader.GetString(5);
|
||||
|
||||
if (whereClause)
|
||||
{
|
||||
if (cache.ContainsKey(result.AccountCodeId))
|
||||
{
|
||||
cache.Remove(result.AccountCodeId);
|
||||
}
|
||||
cache.Add(result.AccountCodeId, result);
|
||||
}
|
||||
else
|
||||
{
|
||||
dicCache.Add(result.AccountCodeId, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// update cache
|
||||
if (!whereClause)
|
||||
{
|
||||
cache.Clear();
|
||||
allRetrived = true;
|
||||
cache = dicCache;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user