153 lines
6.1 KiB
C#
153 lines
6.1 KiB
C#
using System.Linq;
|
|
using System.Linq.Dynamic.Core;
|
|
using YLErp.Modules.DataProviderModule;
|
|
|
|
namespace YLErp.Modules.EodModule.SettlementModule
|
|
{
|
|
/// <summary>
|
|
/// 检查当日结算价,是否全部进系统。
|
|
/// </summary>
|
|
class EodCheckSettlePrice : EodSettleServiceBaseV2
|
|
{
|
|
public const string Step = "检查标的结算价格缺失";
|
|
|
|
public EodCheckSettlePrice(EodSettlementContextV2 context) : base(context)
|
|
{
|
|
ResetDbContext();
|
|
//DbContext.SetDebugLog();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检查收盘价
|
|
/// </summary>
|
|
public void Execute()
|
|
{
|
|
if (!_context.OtcTrades.Any() && !hasSwaptrade())
|
|
{
|
|
return;
|
|
}
|
|
|
|
var request = _context.Request;
|
|
var settleDate = _context.SettleDate;
|
|
var clienIds = _context.Request.ClientIds;
|
|
|
|
IQueryable<string> allQuery = null;
|
|
if (!EodPriceQueryService.CheckFR007Price(_context.SettleDate))
|
|
{
|
|
_context.RaiseError(Step, $"{_context.SettleDate:yyyy年MM月dd日}FR007价格未入库");
|
|
}
|
|
//判断当日结算价是否已经入库
|
|
if (!EodPriceQueryService.CheckDbExists(_context.SettleDate))
|
|
{
|
|
_context.RaiseError(Step, "结算价格未入库");
|
|
}
|
|
|
|
//检查是否所有待结算交易标的价格都存在
|
|
//获取所有现存(抵押状态)抵押品信息 增加抵押品价格检查
|
|
|
|
if (request.IsSettleOtcTrades)
|
|
{
|
|
var tradePredicate = _context.PredicateBuilder.GetOtcTradePredicate()
|
|
.And(t => t.TradeType != "自定义交易" && t.UnderlyingCode != null);
|
|
|
|
//新增客户筛选 tw
|
|
if (clienIds != null)
|
|
{
|
|
tradePredicate = tradePredicate.And(l => clienIds.Contains(l.ClientId));
|
|
}
|
|
|
|
allQuery = DbContext.trade.Where(tradePredicate).Select(n => n.UnderlyingCode).Distinct();
|
|
|
|
if (PS.Config.ErpElement.ForwardTradePriceModel == Configuration.Enums.ForwardTradePriceModel.STANDARD)
|
|
{
|
|
allQuery = allQuery.Union(DbContext.trade.Where(tradePredicate).Where(x => x.BasisUnderlyingCode != null && x.BasisUnderlyingCode != "").Select(n => n.BasisUnderlyingCode).Distinct());
|
|
}
|
|
|
|
var clientProductPredicate = _context.PredicateBuilder.GetClientProductPredicate();
|
|
|
|
//新增客户筛选 tw
|
|
if (clienIds != null)
|
|
{
|
|
clientProductPredicate = clientProductPredicate.And(l => clienIds.Contains(l.ClientId));
|
|
}
|
|
|
|
var clientProductQuery = from t in DbContext.clientcashincashout_product.Where(clientProductPredicate)
|
|
join um in DbContext.underlying_manager on t.UnderlyingId equals um.id
|
|
select um.UnderlyingCode;
|
|
|
|
allQuery = allQuery.Union(clientProductQuery.Distinct());
|
|
}
|
|
|
|
//检查场内标的
|
|
if (request.IsSettleExchangeTrades)
|
|
{
|
|
var exTradePredicate = _context.PredicateBuilder.GetExchangeTradePredicate();
|
|
|
|
var exTradeQuery = DbContext.ExchangeTrade.Where(exTradePredicate).Select(n => n.UnderlyingCode);
|
|
|
|
allQuery = allQuery == null ? exTradeQuery.Distinct() : allQuery.Union(exTradeQuery.Distinct());
|
|
|
|
var preSettleDate = _context.PreSettleDate;
|
|
//最后一个交易日持仓信息
|
|
var futureTypes = ConsGlobal.InstrumentType.GetFutureTypes();
|
|
var positionQuery = from t in DbContext.eod_trade_position
|
|
join um in DbContext.underlying_manager on t.UnderlyingCode equals um.UnderlyingCode
|
|
where t.ValueDate == preSettleDate && t.Amount != 0
|
|
&& ConsTrade.TradeTypesForHedge.Contains(t.TradeType)
|
|
&& (!futureTypes.Contains(um.UnderlyingInstrumentType) || um.MaturityDate >= settleDate)
|
|
select t.UnderlyingCode;
|
|
|
|
allQuery = allQuery.Union(positionQuery.Distinct());
|
|
}
|
|
|
|
|
|
|
|
if (allQuery == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//检查是否所有标的都在结算日有结算价格
|
|
|
|
var umCodeArr = allQuery.ToArray();
|
|
|
|
var eodPriceProvider = _context.GetEodPriceProvider().Initialize(umCodeArr);
|
|
var umCodes = umCodeArr.Where(n => !string.IsNullOrEmpty(n) && !eodPriceProvider.HasValue(n)).ToHashSet(StringComparer.OrdinalIgnoreCase);
|
|
|
|
//排除掉节假日不需要结算的交易
|
|
foreach (var t in _context.HolidayTrades)
|
|
{
|
|
if (!string.IsNullOrEmpty(t.UnderlyingCode))
|
|
{
|
|
umCodes.Remove(t.UnderlyingCode);
|
|
}
|
|
}
|
|
|
|
if (umCodes.Any())
|
|
{
|
|
var codes = string.Join(", ", umCodes);
|
|
|
|
_context.RaiseError(Step, "标的代码:" + codes);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 获取互换标的
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private string[] GetSwapUnderlyingCodes()
|
|
{
|
|
var tradePredicate = _context.PredicateBuilder.GetOtcSwapTradePredicate();
|
|
var query = from t in DbContext.trade.Where(tradePredicate)
|
|
join p in DbContext.swap_position on t.id equals p.SwapTradeId
|
|
where p.IsInitial && !string.IsNullOrEmpty(p.UnderlyingCode) && !p.Invalid
|
|
select p.UnderlyingCode;
|
|
return query.Distinct().ToArray();
|
|
}
|
|
private bool hasSwaptrade()
|
|
{
|
|
var tradePredicate = _context.PredicateBuilder.GetOtcSwapTradePredicate();
|
|
return DbContext.trade.Where(tradePredicate).Any();
|
|
}
|
|
}
|
|
}
|