从山证v2.3.0拷贝
This commit is contained in:
@@ -0,0 +1,196 @@
|
||||
using YLErp.BLL;
|
||||
|
||||
namespace YLErp.Modules.TradeModule
|
||||
{
|
||||
/// <summary>
|
||||
/// 交易元数据服务
|
||||
/// </summary>
|
||||
public class TradeMetaService : YLBaseService
|
||||
{
|
||||
public TradeMetaService(YLBaseService baseService) : base(baseService)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public TradeMetaService(OptUserInfo userInfo) : base(userInfo)
|
||||
{
|
||||
}
|
||||
|
||||
public TradeMetaService(OptUserInfo optUser, YLContext dbContext) : base(optUser, dbContext)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取元数据
|
||||
/// </summary>
|
||||
public Dictionary<string, string> GetTradeMeta(int tradeId)
|
||||
{
|
||||
return DbContext.TradeMeta.AsNoTracking().Where(t => t.TradeId == tradeId)
|
||||
.Select(n => new { n.MetaKey, n.MetaValue }).ToDictionary(n => n.MetaKey, n => n.MetaValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取元数据
|
||||
/// </summary>
|
||||
public string GetTradeMeta(int tradeId, string metaKey)
|
||||
{
|
||||
if (metaKey is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(metaKey));
|
||||
}
|
||||
return DbContext.TradeMeta.AsNoTracking().FirstOrDefault(t => t.TradeId == tradeId && t.MetaKey == metaKey)?.MetaValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取元数据
|
||||
/// </summary>
|
||||
public Dictionary<int, Dictionary<string, string>> GetTradeMetas(IEnumerable<int> tradeIds)
|
||||
{
|
||||
if (tradeIds is null || !tradeIds.Any())
|
||||
{
|
||||
return new Dictionary<int, Dictionary<string, string>>();
|
||||
}
|
||||
|
||||
var set = new HashSet<int>(tradeIds);//应该没有重复的数据但也防止下意外情况的发生
|
||||
var dic = set.ToDictionary(n => n, m => new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase));
|
||||
|
||||
//为前端下载所有数据优化
|
||||
for (var i = 0; i < set.Count; i += 1000)
|
||||
{
|
||||
var ids = set.Skip(i).Take(1000).ToArray();
|
||||
|
||||
var metaDatas = DbContext.TradeMeta.Where(n => ids.Contains(n.TradeId))
|
||||
.Select(n => new { n.TradeId, n.MetaKey, n.MetaValue }).ToArray();
|
||||
|
||||
foreach (var meta in metaDatas)
|
||||
{
|
||||
if (dic.TryGetValue(meta.TradeId, out var metaDic))
|
||||
{
|
||||
metaDic[meta.MetaKey] = meta.MetaValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return dic;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加或更新元数据
|
||||
/// </summary>
|
||||
public void AddTradeMeta(int tradeId, string metaKey, string metaValue, bool saveChanges = true)
|
||||
{
|
||||
if (metaKey is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(metaKey));
|
||||
}
|
||||
|
||||
var dbTradeMeta = DbContext.TradeMeta.FirstOrDefault(t => t.TradeId == tradeId && t.MetaKey == metaKey);
|
||||
|
||||
if (dbTradeMeta == null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(metaValue))
|
||||
{
|
||||
return;
|
||||
}
|
||||
DbContext.TradeMeta.Add(new TradeMeta
|
||||
{
|
||||
TradeId = tradeId,
|
||||
MetaKey = metaKey,
|
||||
MetaValue = metaValue,
|
||||
CreateTime = DateTime.Now
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
dbTradeMeta.MetaValue = metaValue;
|
||||
}
|
||||
|
||||
if (saveChanges)
|
||||
{
|
||||
DbContext.SaveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加或更新元数据
|
||||
/// </summary>
|
||||
public void AddTradeMeta(TradeMeta meta, bool saveChanges = true)
|
||||
{
|
||||
if (meta is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(meta));
|
||||
}
|
||||
|
||||
var dbTradeMeta = DbContext.TradeMeta.FirstOrDefault(t => t.TradeId == meta.TradeId && t.MetaKey == meta.MetaKey);
|
||||
if (dbTradeMeta == null)
|
||||
{
|
||||
meta.CreateTime = DateTime.Now;
|
||||
DbContext.TradeMeta.Add(meta);
|
||||
}
|
||||
else
|
||||
{
|
||||
dbTradeMeta.MetaValue = meta.MetaValue;
|
||||
}
|
||||
|
||||
if (saveChanges)
|
||||
{
|
||||
DbContext.SaveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加或更新元数据
|
||||
/// </summary>
|
||||
public void AddTradeMetas(IEnumerable<TradeMeta> tradeMetas, bool saveChanges = true)
|
||||
{
|
||||
if (tradeMetas == null || !tradeMetas.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var meta in tradeMetas)
|
||||
{
|
||||
var dbTradeMeta = DbContext.TradeMeta.FirstOrDefault(t => t.TradeId == meta.TradeId && t.MetaKey == meta.MetaKey);
|
||||
if (dbTradeMeta == null)
|
||||
{
|
||||
meta.CreateTime = DateTime.Now;
|
||||
DbContext.TradeMeta.Add(meta);
|
||||
}
|
||||
else
|
||||
{
|
||||
dbTradeMeta.MetaValue = meta.MetaValue;
|
||||
}
|
||||
}
|
||||
if (saveChanges)
|
||||
{
|
||||
DbContext.SaveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除元数据
|
||||
/// 不要调用该方法,哪怕交易已经被删除了,也不要删除交易元数据,证券业报送中需要报送交易删除的信息
|
||||
/// </summary>
|
||||
public void DeleteTradeMetas(IEnumerable<TradeMeta> tradeMetas, bool saveChanges = true)
|
||||
{
|
||||
if (tradeMetas == null || !tradeMetas.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var tradeMeta in tradeMetas)
|
||||
{
|
||||
var dbTradeMeta = DbContext.TradeMeta.FirstOrDefault(t => t.TradeId == tradeMeta.TradeId && t.MetaKey == tradeMeta.MetaKey);
|
||||
if (dbTradeMeta != null)
|
||||
{
|
||||
DbContext.TradeMeta.Remove(dbTradeMeta);
|
||||
}
|
||||
}
|
||||
|
||||
if (saveChanges)
|
||||
{
|
||||
DbContext.SaveChanges();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user