Files
zszq-trs/YLErpDAL/BLL/Logical/SystemEndLogical.cs
T
2024-05-09 14:06:26 +08:00

181 lines
6.8 KiB
C#

using YLErp;
using YLErp.BLL;
using YLErp.DBModels.Consts;
using YLErp.Modules.QuotationModule;
using YLErp.QdpModule;
namespace Job.Logical.Services
{
public class SystemEndLogical
{
public void Execute()
{
LogFactory.GetLogger<SystemEndLogical>().Info("SystemEndLogical Start");
if (DateTime.Now.Hour < 16)
{
LogFactory.GetLogger<SystemEndLogical>().Info("Should Not Run ! SystemEndLogical Start At" + DateTime.Now.ToString("yyyy-MM-dd"));
return;
}
//标的到期
UnderlyingMatured();
//相关性到期
CorrelationMatured();
if (PS.Config.Is宏源)
{
//underlying_main_contract历史备份
SaveMainContractHistory(ConsUserGroup.GroupA);
SaveMainContractHistory(ConsUserGroup.GroupB);
}
else
{ SaveMainContractHistory(""); }
}
/// <summary>
/// 相关性到期
/// </summary>
private static void CorrelationMatured()
{
try
{
using (var db = new YLContext())
{
DateTime valueDate = DateTime.Now.Date;
var live_cors = db.correlation.Where(c => c.ValueDate <= valueDate && c.LiveState == "Live").ToList();
var live_cor_uids = new HashSet<int>();
live_cors.ForEach(c =>
{
if (c.UnderlyingId1 != null)
{
live_cor_uids.Add(c.UnderlyingId1.Value);
}
if (c.UnderlyingId2 != null)
{
live_cor_uids.Add(c.UnderlyingId2.Value);
}
});
//过期标的
var matured_under = db.underlying_manager.Where(u => u.UnderlyingState == "Matured" && live_cor_uids.Contains(u.id))
.Select(n => n.id).ToHashSet();
matured_under.Remove(0);
if (matured_under.Count > 0)
{
live_cors.ForEach(c =>
{
if (matured_under.Contains(c.UnderlyingId1 ?? 0) || matured_under.Contains(c.UnderlyingId2 ?? 0))
{
c.LiveState = "Matured";
}
});
db.SaveChanges();
}
}
}
catch (Exception ex)
{
LogFactory.GetLogger<SystemEndLogical>().Error("Correlation Matured 判断", ex);
}
}
/// <summary>
/// 标的到期日处理
/// </summary>
private static void UnderlyingMatured()
{
DateTime dt99 = new DateTime(2399, 1, 1);
try
{
DateTime nextDay = DateTime.Now.AddDays(1).Date;
using (var db = new YLContext())
{
//---------------------------------------
// 标的到期日如果没有则给个默认值
//---------------------------------------
// 判断underlying_manager表中合约的到期状态
var query = from un in db.underlying_manager
where un.MaturityDate == null
&& un.UnderlyingState == "Live"
&& un.UnderlyingInstrumentType == "CommodityFutures"
select un;
var list = query.ToList();
foreach (var un in list)
{
if (un.UnderlyingInstrumentType == "CommodityFutures")
{
if (!un.MaturityDate.HasValue)
{
try
{
un.MaturityDate = QdpHelper.defaultMaturityDateFromContract(un.UnderlyingCode);
}
catch (Exception ex)
{
LogFactory.GetLogger<SystemEndLogical>().Error("underlying_manager.MaturityDate", ex);
}
}
}
else if (un.UnderlyingInstrumentType == "Stock" || un.UnderlyingInstrumentType == "CommoditySpot")
{
un.MaturityDate = dt99;
}
}
var changes = db.SaveChanges();
var tableName = db.GetTableName<underlying_manager>();
var sql = $"update {tableName} set UnderlyingState='Matured',PrevClosePrice=Price where MaturityDate<'{nextDay:yyyy-MM-dd}' and UnderlyingState<>'Matured'";
changes = db.Database.ExecuteSqlRaw(sql);
}
}
catch (Exception ex)
{
LogFactory.GetLogger<SystemEndLogical>().Error("Matured 判断", ex);
}
}
/// <summary>
/// underlying_main_contract历史备份
/// </summary>
private static void SaveMainContractHistory(string userGroup)
{
try
{
using (var db = new YLContext())
{
var cursystemdate = valuedateBLL.ValueDate.ToString("yyyy-MM-dd");
DateTime dHdate = Convert.ToDateTime(cursystemdate);
bool hasHistory = db.underlying_main_contract_history.Any(c => c.HistoryDate == dHdate);
if (!hasHistory)
{
var tResult = underlying_main_contract_historyBLL.GetVolatilityQuotation(10000
, flat_price_quotationBLL.GetSortFlat_Quotation(null, userGroup), userGroup);
var umch = new underlying_main_contract_history
{
HistoryDate = Convert.ToDateTime(cursystemdate),
OptDate = DateTime.Now,
underlyingjson = JsonHelper.Stringify(tResult)
};
db.underlying_main_contract_history.Add(umch);
}
db.SaveChanges();
}
}
catch (Exception ex)
{
LogFactory.GetLogger<SystemEndLogical>().Error("underlying_main_contract_history 保存", ex);
}
}
}
}