86 lines
2.2 KiB
C#
86 lines
2.2 KiB
C#
namespace YLErp.Modules.DataProviderModule
|
|
{
|
|
/// <summary>
|
|
/// 交易资金流水数据提供
|
|
/// </summary>
|
|
public class TradeCashDataProvider : ITradeCashDataProvider
|
|
{
|
|
readonly Dictionary<int, trade_cash[]> _dic;
|
|
|
|
public TradeCashDataProvider()
|
|
{
|
|
_dic = new Dictionary<int, trade_cash[]>();
|
|
}
|
|
|
|
public TradeCashDataProvider Initialize(IEnumerable<int> tradeIds)
|
|
{
|
|
if (tradeIds == null || !tradeIds.Any(n => n > 0))
|
|
{
|
|
return this;
|
|
}
|
|
|
|
var set = new HashSet<int>(110);
|
|
|
|
foreach (var id in tradeIds)
|
|
{
|
|
set.Add(id);
|
|
|
|
if (set.Count > 100)
|
|
{
|
|
QueryTradeCash(set);
|
|
}
|
|
}
|
|
|
|
if (set.Any())
|
|
{
|
|
QueryTradeCash(set);
|
|
}
|
|
|
|
return this;
|
|
}
|
|
|
|
private void QueryTradeCash(HashSet<int> idSet)
|
|
{
|
|
using (var db = DbContextFactory.GetYLDbContext())
|
|
{
|
|
var groups = db.trade_cash.AsNoTracking()
|
|
.Where(t => idSet.Contains(t.TradeId) && t.ValidState != ConsGlobal.InValid && !t.IsDeleted)
|
|
.OrderBy(t => t.TradeId).ThenBy(t => t.ValueDate).ToArray().GroupBy(n => n.TradeId);
|
|
|
|
foreach (var group in groups)
|
|
{
|
|
idSet.Remove(group.Key);
|
|
_dic[group.Key] = group.ToArray();
|
|
}
|
|
|
|
foreach (var id in idSet)
|
|
{
|
|
_dic[id] = Array.Empty<trade_cash>();
|
|
}
|
|
|
|
idSet.Clear();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取交易资金数据(数据已按照ValueDate正序排序)
|
|
/// </summary>
|
|
public IEnumerable<trade_cash> GetTradeCashes(int tradeId)
|
|
{
|
|
if (tradeId < 1)
|
|
{
|
|
return Array.Empty<trade_cash>();
|
|
}
|
|
|
|
if (_dic.TryGetValue(tradeId, out var tcs))
|
|
{
|
|
return tcs;
|
|
}
|
|
|
|
QueryTradeCash(new HashSet<int> { tradeId });
|
|
|
|
return _dic[tradeId];
|
|
}
|
|
}
|
|
}
|