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

152 lines
6.6 KiB
C#

using YLErp.BLL;
using YLErp.CustomizedBizLogic;
using YLErp.DBModels.Consts;
using YLErp.Enums;
using YLErp.Modules.EodModule;
using YLErp.Modules.TradeModule.ExoticOptionModule;
namespace YLErp.Modules.TradeModule.DealModule
{
/// <summary>
/// 二元期权操作
/// 迁移自:trade_binary_optionBLL
/// </summary>
public class TradeSwapService : TradeCashServiceEx
{
public TradeSwapService(YLBaseService baseService) : base(baseService)
{
}
public TradeSwapService(OptUserInfo userInfo) : base(userInfo)
{
}
/// <summary>
///
/// </summary>
public void HandleSwapTradeCashPre(DateTime valueDate, System.Collections.Generic.IEnumerable<int> clienIds)
{
#region #region 新增客户筛选 tw
var tradeCashPres = DbContext.trade_cash_pre.Where(x => x.ValueDate == valueDate && x.ValidState != "InValid" && !x.IsFinished).ToList();
var trades = new List<trade>();
var tradeIds = new List<int>();
if (clienIds != null)
{
trades = DbContext.trade.Where(l => clienIds.Contains(l.ClientId)).ToList();
tradeIds = trades.Select(l => l.id).ToList();
tradeCashPres = tradeCashPres.Where(l => tradeIds.Contains(l.TradeId)).ToList();
}
else
{
tradeIds = tradeCashPres.Select(x => x.TradeId).ToList();
trades = DbContext.trade.Where(x => tradeIds.Contains(x.id)).ToList();
tradeIds = trades.Select(x => x.id).ToList();
tradeCashPres = tradeCashPres.Where(l => tradeIds.Contains(l.TradeId)).ToList();
}
#endregion
tradeCashPres.ForEach(x =>
{
var trade = trades.FirstOrDefault(y => y.id == x.TradeId);
if (trade.ExerciseDate < valueDate)
{
return;//已到期交易不再观察;
}
var client = DataCacheProvider.GetClientDataSource().GetData(trade.ClientId);
var currencyRate = new EodCurrencyRateService(UserInfo).GetCurrencyRate(trade.QuoteCurrency, trade.SettlementCurrency, x.ValueDate);
var currencyRateTradeDate = new EodCurrencyRateService(UserInfo).GetCurrencyRate(trade.QuoteCurrency, trade.SettlementCurrency, trade.TradeDate.Value);
var tradeCashDetails = DbContext.trade_cash_detail.Where(y => y.TradeCashPreId == x.id).ToList();
var costFeeGet = 0.0;
var costTradePriceGet = 0.0;
var coupon = 0.0;
var winloss = 0.0;
tradeCashDetails.ForEach(y =>
{
if (y.TradeCashType == TradeCashTypeEnum.利息.ToString())
{
y.Amount = y.QuoteAmount * (PS.Config.Company == Configuration.CompanyEnum.中金 && client.BoundSide == BoundSideEnum.南向 ? currencyRateTradeDate : currencyRate);
}
else
{
y.Amount = y.QuoteAmount * currencyRate;
}
if (y.TradeCashType == TradeCashTypeEnum.开仓手续费.ToString())
{
costTradePriceGet = y.Amount ?? 0;
}
else if (y.TradeCashType == TradeCashTypeEnum.了结手续费.ToString())
{
costFeeGet = y.Amount ?? 0;
}
else if (y.TradeCashType == TradeCashTypeEnum.利息.ToString())
{
coupon = y.Amount ?? 0;
}
else if (y.TradeCashType == TradeCashTypeEnum.浮动收益.ToString())
{
winloss = y.Amount ?? 0;
}
});
var tradeCashSwap = DbContext.trade_cash_swap.FirstOrDefault(y => y.TradeCashPreId == x.id);
tradeCashSwap.PayInitialAmount = -winloss;
tradeCashSwap.PayAmount = -winloss;
tradeCashSwap.GetExtraAmount = coupon;
tradeCashSwap.GetCostFee = costFeeGet + costTradePriceGet;
tradeCashSwap.GetAmount = costFeeGet + costTradePriceGet + coupon;
var tradeCash = new trade_cash()
{
OptId = x.OptId,
OptName = x.OptName,
OptDate = DateTime.Now,
ExceciseType = x.ExceciseType,
TradeType = x.TradeType,
CallPut = x.CallPut,
Notional = x.Notional,
TradeAmount = x.TradeAmount,
IsLastAction = x.IsLastAction,
TradeId = x.TradeId,
FinalPrice = x.FinalPrice,
UnwindType = x.UnwindType,
UnwindNotional = x.UnwindNotional,
UnwindTradeAmount = x.UnwindTradeAmount,
UnwindPercentRate = x.UnwindPercentRate,
NotionalPercentRate = x.NotionalPercentRate,
Number = x.Number,
Amount = winloss + coupon + costFeeGet + costTradePriceGet,
QuoteAmount = tradeCashDetails.Sum(y => y.QuoteAmount ?? 0),
CurrencyRate = currencyRate,
Action = x.Action,
Status = x.Status,
ValueDate = x.ValueDate,
HappenedDate = x.HappenedDate,
ValidState = "Valid",
ExerciseWay = TradeCashExerciseWayEnum.到期行权
};
DbContext.trade_cash.Add(tradeCash);
x.IsFinished = true;
DbContext.SaveChanges();
if (PS.Config.Company == Configuration.CompanyEnum.招证)
{
new BizLogicZhaoZheng().GenerateZhaoZhengDealNumber(trade, tradeCash);
}
if (PS.Config.Company == Configuration.CompanyEnum.物产中大)
{
new BizLogicWCZD().GenerateWCZDNumber(DbContext, trade, tradeCash.ValueDate, tradeCash.id);
}
tradeCashDetails.ForEach(y => y.TradeCashId = tradeCash.id);
tradeCashSwap.TradeCashId = tradeCash.id;
//增加出入金记录
new ClientCashinCashoutBLL(this).CloseTrade_ClientCashInCashOutSave(trade, tradeCash, tradeCash.ValueDate);
});
//更新数据库
DbContext.SaveChanges();
}
}
}