Files
zszq-trs/YLErpDAL/Modules/TradeMsgOutputModule/TradeCashOutputService.cs
T
2024-05-09 14:06:26 +08:00

195 lines
7.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YLErp.DBModels;
using YLErp.Modules.BasicDataModule;
using YLErp.Modules.TradeMsgOutputModule.Dto;
namespace YLErp.Modules.TradeMsgOutputModule
{
/// <summary>
/// 交易资金导出服务
/// </summary>
public class TradeCashOutputService: BaseTradeAfterEodOutputService
{
/// <summary>
/// 获取资金记录
/// </summary>
/// <param name="valueDate"></param>
/// <returns></returns>
public List<CashLogOutputDto> GetCashLog(DateTime valueDate)
{
CheckEodStatus(valueDate);
var list = GetListByDate(valueDate);
var result = new List<CashLogOutputDto>();
if (list == null || list.Count == 0)
{
return result;
}
var tradeIds=list.Select(x => x.TradeId).ToList();
Dictionary<int, TradeDicSimpleDto> tradeDic = null;
using (var db = DbContextFactory.GetYLDbContext())
{
tradeDic = GetEodTradeDicSimpleDic(db, valueDate, tradeIds);
}
if (tradeDic == null)
{
tradeDic = new Dictionary<int, TradeDicSimpleDto>();
}
Dictionary<int, AssetUnitDto> assetUnitDic = null;
var assertIds = tradeDic.Values.Select(p => p.AssetId).Distinct().ToList();
if (assertIds != null && assertIds.Count > 0)
{
assetUnitDic = new AssetUnitDataService(new OptUserInfo(0, "系统", OptUserFrom.System)).GetAssertByAssertIds(assertIds);
}
if (assetUnitDic == null)
{
assetUnitDic = new Dictionary<int, AssetUnitDto>();
}
List<ExchangeAccount> exchangeAccountList = null;
if (assertIds != null && assertIds.Count > 0)
{
exchangeAccountList = new ExchangeAccountService(new OptUserInfo(0, "系统", OptUserFrom.System)).GetExchangeAccountByBookIds(assertIds);
}
if(exchangeAccountList == null)
{
exchangeAccountList = new List<ExchangeAccount>();
}
var tradePriceList = GetTradePrice(tradeIds);
if (tradePriceList == null)
{
tradePriceList = new List<TradePriceDto>();
}
list.ForEach(p =>
{
var model = new CashLogOutputDto
{
ValueDate = p.ValueDate,
HappenedDate = p.HappenedDate,
TradeId = p.TradeId,
CashAction = p.Action,
Amount = p.Amount,
Notional = p.Notional,
};
model.TradePrice = tradePriceList.Where(d => d.TradeId == p.TradeId).Sum(p => p.TradePrice);
if (tradeDic.ContainsKey(p.TradeId))
{
var trade = tradeDic[p.TradeId];
model.TradeType = trade.TradeType;
model.TradeNumber = trade.TradeNumber;
model.TradeDate = trade.TradeDate;
model.ExerciseDate = trade.ExerciseDate;
model.UnderlyingCode = trade.UnderlyingCode;
model.PremiumPayDate = trade.PremiumPayDate;
model.OriginalStockEqvNotional = trade.OriginalStockEqvNotional;
model.StockEqvNotional = trade.StockEqvNotional;
model.OriginalNotional = trade.OriginalNotional;
if (trade.AssetId > 0 && assetUnitDic.ContainsKey(trade.AssetId))
{
model.AssetUnitGroupName = assetUnitDic[trade.AssetId].GroupName;
}
if (trade.AssetId > 0)
{
var exchangeAccount = exchangeAccountList.FirstOrDefault(d => d.DefaultBookId == trade.AssetId);
if(exchangeAccount != null)
{
model.ExChangeAccount = exchangeAccount.AccountCode;
}
}
result.Add(model);
}
});
return result;
}
private List<TradeCashOutputDto> GetByValueDate(DateTime valueDate)
{
using(var db = DbContextFactory.GetYLDbContext())
{
return db.trade_cash.AsNoTracking().Where(p => p.ValueDate == valueDate && (p.ValidState != "InValid" || string.IsNullOrEmpty(p.ValidState))&&!p.IsDeleted).Select(p => new TradeCashOutputDto
{
Id=p.id,
ValueDate=p.ValueDate,
HappenedDate=p.HappenedDate,
Action=p.Action,
Amount=p.Amount,
TradeId=p.TradeId,
Notional=p.Notional
}).ToList();
}
}
private List<TradeCashOutputDto> GetByHappendDate(DateTime valueDate)
{
var endDate = valueDate.AddDays(1);
using (var db = DbContextFactory.GetYLDbContext())
{
var tradeCashIds = db.ClientCashInCashOut.AsNoTracking().Where(p => p.HappenDate >= valueDate && p.HappenDate < endDate && (p.ValidState != "InValid" || string.IsNullOrEmpty(p.ValidState)) && p.TradeCashId > 0).Select(p => p.TradeCashId).Distinct().ToList();
if (tradeCashIds == null || tradeCashIds.Count == 0)
{
return null;
}
return db.trade_cash.AsNoTracking().Where(p => tradeCashIds.Contains(p.id) && (p.ValidState != "InValid" || string.IsNullOrEmpty(p.ValidState)) && !p.IsDeleted).Select(p => new TradeCashOutputDto
{
Id = p.id,
ValueDate = p.ValueDate,
HappenedDate = valueDate,
Action = p.Action,
Amount = p.Amount,
TradeId = p.TradeId,
Notional=p.Notional,
}).ToList();
}
}
private List<TradeCashOutputDto> GetListByDate(DateTime valueDate)
{
var valueList = GetByValueDate(valueDate);
var happendList = GetByHappendDate(valueDate);
var result = new List<TradeCashOutputDto>();
if (happendList != null && happendList.Count > 0)
{
result.AddRange(happendList);
}
if (valueList != null && valueList.Count > 0)
{
valueList = valueList.Where(p => !result.Any(d => d.Id == p.Id)).ToList();
}
if (valueList != null && valueList.Count > 0)
{
result.AddRange(valueList);
}
return result;
}
private List<TradePriceDto> GetTradePrice(List<int> tradeIds)
{
using (var db = DbContextFactory.GetYLDbContext())
{
return db.trade_cash.AsNoTracking().Where(p => tradeIds.Contains(p.TradeId)&& ClientCashInCashOut.系统操作_期权费.Equals(p.Action) && (p.ValidState != "InValid" || string.IsNullOrEmpty(p.ValidState)) && !p.IsDeleted).Select(p => new TradePriceDto
{
TradeId = p.TradeId,
TradePrice=p.Amount
}).ToList();
}
}
}
}