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

144 lines
5.5 KiB
C#

using YLErp.DBModels.Consts;
using YLErp.Models;
using YLErp.Modules.TradeModule.DealModule;
using YLErp.Modules.UnderlyingModule;
namespace YLErp.Modules.EodModule.SettlementModule
{
/// <summary>
/// 篮子标的日终结算价格保存服务
/// </summary>
class EodBasketPriceSaveService : EodSettleServiceBase
{
public const string Step = "合成篮子标的价格";
public EodBasketPriceSaveService(EodSettlementContextBase context) : base(context)
{
}
/// <summary>
/// 保存篮子标的日终结算价格
/// </summary>
public void Execute()
{
//取出来未在结算价表中的篮子标的
//如果篮子标的已禁用并且未在交易中使用则可以忽略合成
var settleDate = _context.SettleDate;
var eodStockQuery = DbContext.eod_stock_price.Where(c => c.ValueDate == settleDate);
var eodCommodityQuery = DbContext.eod_commodity_future_price.Where(c => c.ValueDate == settleDate);
var umQuery = from um in DbContext.underlying_manager.AsNoTracking()
where um.CommodityCode == "篮子标的" && um.OpenDate <= settleDate
&& !eodStockQuery.Any(n => n.UnderlyingCode == um.UnderlyingCode)
&& !eodCommodityQuery.Any(n => n.UnderlyingCode == um.UnderlyingCode)
select new
{
um.id,
um.UnderlyingCode,
um.SubData,
um.LaunchState
};
var basketUmList = umQuery.ToArray();
if (!basketUmList.Any())
{
return;
}
#region 增加客户筛选 tw
var edQuery = DbContext.eod_trade_position.Where(d => d.ValueDate == _context.PreSettleDate&& d.TradeType != "自定义交易");
var tdQuery = DbContext.trade.Where(t => t.TradeDate == settleDate && t.ValidState != ConsGlobal.InValid && t.TradeType != "自定义交易");
if (_context.Request.ClientIds != null)
{
edQuery = edQuery.Where(t => _context.Request.ClientIds.Contains(t.ClientId));
tdQuery = tdQuery.Where(t => _context.Request.ClientIds.Contains(t.ClientId));
}
var edCodeQuery = edQuery.Select(n => n.UnderlyingCode);
var tdCodeQuery = tdQuery.Select(n => n.UnderlyingCode);
#endregion
var usedCodes = edCodeQuery.Union(tdCodeQuery).ToHashSet(StringComparer.OrdinalIgnoreCase);
var ids = basketUmList.Select(O => O.id);
var hisDic = new DbRecordChangesService<UnderlyingChanges>(this)
.GetValueOfLatest(ConsInfoChangeType.Dividend, ids, nameof(underlying_manager.SubData), settleDate.AddDays(-1))
.ToDictionary(K => K.RecordId, V => V.FieldValue);
var eodPriceProvider = _context.GetEodPriceProvider();
foreach (var basketUm in basketUmList)
{
if (!usedCodes.Contains(basketUm.UnderlyingCode))
{
continue;
}
if (!hisDic.TryGetValue(basketUm.id, out var subData))
{
subData = basketUm.SubData;
}
var result = BasketUnderlyingHelper.GetBasketPrice(subData, eodPriceProvider, false);
if (result.HasError)
{
_context.RaiseError(Step, $"篮子标的'{basketUm.UnderlyingCode}'合成价格失败,标的价格未找到:{string.Join(",", result.ErrorCodeList)}");
}
var eodPrice = new eod_stock_price
{
ValueDate = settleDate,
UnderlyingCode = basketUm.UnderlyingCode,
UnderlyingStatus = null,
ClosePrice = result.Price,
SettlePrice = result.SettlePrice,
ReferencePrice = null,
LowPrice = null,
HighPrice = null,
OptDate = DateTime.Now,
OptId = 0,
OptName = ConsGlobal.XiTong,
DataSource = ConsGlobal.XiTong
};
DbContext.eod_stock_price.Add(eodPrice);
var eodPrice2 = new EodPrice
{
IsStock = true,
ValueDate = settleDate,
UnderlyingId = basketUm.id,
UnderlyingCode = basketUm.UnderlyingCode,
UnderlyingStatus = null,
ClosePrice = result.Price,
SettlePrice = result.SettlePrice,
ReferencePrice = null,
LowPrice = null,
HighPrice = null
};
eodPriceProvider.SetPrice(eodPrice2);
}
var changes = DbContext.SaveChanges(_context.CancellationToken);
//直接使用MYSQL语句做了昨日收盘价的处理
if (_context.IsCurrentDay)
{
var sql = $@"
update underlying_manager um join eod_stock_price ep on um.UnderlyingCode=ep.StockId
set um.PrevClosePrice=ep.ClosePrice,um.Price=ep.ClosePrice
where um.CommodityCode='篮子标的' and ep.ValueDate='{settleDate:yyyy-MM-dd}'";
DbContext.Database.ExecuteSqlRaw(sql);
}
}
}
}