296 lines
11 KiB
C#
296 lines
11 KiB
C#
using NPOI.SS.Formula.Functions;
|
|
using System;
|
|
using System.Data;
|
|
using System.Linq.Expressions;
|
|
using YLErp.DBModels.Enums;
|
|
using YLErp.Model;
|
|
using YLErp.Models;
|
|
|
|
namespace YLErp.Modules.ExchangeTradeModule
|
|
{
|
|
/// <summary>
|
|
/// 场内标的交易查询服务
|
|
/// </summary>
|
|
public class ExchangeTradeQueryService : YLBaseService
|
|
{
|
|
public ExchangeTradeQueryService(YLBaseService baseService) : base(baseService)
|
|
{
|
|
|
|
}
|
|
|
|
public ExchangeTradeQueryService(OptUserInfo userInfo) : base(userInfo)
|
|
{
|
|
|
|
}
|
|
|
|
public IPagedList<ExchangeTradeDto> GetPagedList(ExchangeTradeReq req)
|
|
{
|
|
if (req is null)
|
|
{
|
|
throw new ArgumentNullException(nameof(req));
|
|
}
|
|
|
|
var predicate = req.BuildPredicate();
|
|
|
|
var query = DbContext.ExchangeTrade.Where(predicate)
|
|
.ProjectTo<ExchangeTradeDto>(YLAutoMapper.Config);
|
|
|
|
var list = query.ToPagedList(req);
|
|
|
|
foreach (var item in list)
|
|
{
|
|
var under = DataCacheModule.DataCacheManager.GetUnderlyingDataSource().GetData(item.UnderlyingCode);
|
|
item.ExerciseMode = ConsGlobal.ExerciseMode.GetDesc(item.ExerciseMode);
|
|
item.AssetBookName = DataCacheProvider.GetAssetUnitDataSource().GetData(item.AssetBookId)?.Name;
|
|
item.UnderlyingName = under?.UnderlyingName;
|
|
item.CountRatio = under?.CountRatio ??1;
|
|
}
|
|
return list;
|
|
}
|
|
|
|
public IPagedList<ExchangeTradePositionDto> GetPagedPositionList(ExchangeTradeReq req)
|
|
{
|
|
if (req is null)
|
|
{
|
|
throw new ArgumentNullException(nameof(req));
|
|
}
|
|
|
|
IQueryable<ExchangeTradePositionDto> query = null;
|
|
//var lastValueDate = EodOperationBase.GetLastSettlementDate(DateTime.Now);
|
|
//if (req.TradeDateTo == null || req.TradeDateTo > lastValueDate)
|
|
//{
|
|
var predicate = req.BuildPositionPredicateBase();
|
|
//从 intraday_trade_position获取
|
|
query =
|
|
DbContext.TradePosition.Where(predicate)
|
|
// 组合eod持仓信息
|
|
.Select(o => new ExchangeTradePositionDto()
|
|
{
|
|
id = (int)o.id,
|
|
BookId = o.BookId,
|
|
PositionCount = o.Position,
|
|
OptionCode = o.InstrumentCode,
|
|
//PositionCost = o.PositionCost,
|
|
PositionType = o.PositionType == PositionTypeFlag.Long ? "多头" : "空头",
|
|
UnderlyingCode = o.UnderlyingCode,
|
|
ValueDate = o.UpdateTime,
|
|
TradeType = o.TradeType
|
|
});
|
|
//}
|
|
//else
|
|
//{
|
|
// var predicate = req.BuildPositionPredicate();
|
|
// //从eod_trade_position获取
|
|
// query = DbContext.eod_trade_position.Where(predicate)
|
|
// // 组合eod持仓信息
|
|
// .Select(o => new ExchangeTradePositionDto() {
|
|
// BookId = o.BookId,
|
|
// PositionCount = o.Amount,
|
|
// PositionCost = o.Cost,
|
|
// PositionType = o.PositionType == "long" ? "多头" : "空头",
|
|
// UnderlyingCode = o.UnderlyingCode,
|
|
// ValueDate = o.ValueDate,
|
|
// TradeType = o.TradeType
|
|
// });
|
|
//}
|
|
|
|
var list = query.ToList().AsQueryable().ToPagedList(req);
|
|
var insiteTradeList = list.Where(t => "场内期权".Equals(t.TradeType)).ToList();
|
|
Dictionary<string, double> insiteTradeContractSizeDic = null;
|
|
if (insiteTradeList != null && insiteTradeList.Count > 0)
|
|
{
|
|
var optionCodes = insiteTradeList.Select(p => p.OptionCode).Distinct().ToList();
|
|
insiteTradeContractSizeDic = DbContext.exchange_list_option.AsNoTracking().Where(p => optionCodes.Contains(p.ContractCode)).ToDictionary(p => p.ContractCode, p => p.ContractSize);
|
|
}
|
|
if (insiteTradeContractSizeDic == null)
|
|
{
|
|
insiteTradeContractSizeDic = new Dictionary<string, double>();
|
|
}
|
|
|
|
foreach (var item in list)
|
|
{
|
|
//item.ExerciseMode = ConsGlobal.ExerciseMode.GetDesc(item.ExerciseMode);
|
|
item.AssetBookName = DataCacheProvider.GetAssetUnitDataSource().GetData(item.BookId)?.Name;
|
|
var Underlying = DataCacheModule.DataCacheManager.GetUnderlyingDataSource().GetData(item.UnderlyingCode);
|
|
item.UnderlyingName = Underlying.UnderlyingName;
|
|
item.PositionCount = item.PositionCount / Underlying.CountRatio;
|
|
if ("场内期权".Equals(item.TradeType))
|
|
{
|
|
var contractSize = insiteTradeContractSizeDic.ContainsKey(item.OptionCode) ? insiteTradeContractSizeDic[item.OptionCode] : 1.0;
|
|
item.Position = item.PositionCount / contractSize * Underlying.CountRatio;
|
|
}
|
|
else
|
|
{
|
|
item.Position = item.PositionCount / Underlying.ContractSize * Underlying.CountRatio;
|
|
}
|
|
item.UnderlyingPrice = Underlying.Price.OtcFormat(OtcFormatFlag.marginRate);
|
|
item.PositionCost = item.PositionCount * Underlying.CountRatio;
|
|
item.CountRatio = Underlying.CountRatio;
|
|
}
|
|
|
|
return list;
|
|
}
|
|
|
|
public ExchangeTradeDto GetDetail(int id)
|
|
{
|
|
var dto = DbContext.ExchangeTrade.ProjectTo<ExchangeTradeDto>(YLAutoMapper.Config).Where(n => n.id == id).FirstOrDefault();
|
|
dto.AssetBookName = DataCacheProvider.GetAssetUnitDataSource().GetData(dto.AssetBookId)?.Name;
|
|
dto.CountRatio = DataCacheProvider.GetUnderlyingDataSource().GetData(dto.UnderlyingId)?.CountRatio ?? 1;
|
|
return dto;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 场内交易查询Model
|
|
/// </summary>
|
|
public class ExchangeTradeReq : PagedQueryModel
|
|
{
|
|
public bool IsOption { get; set; }
|
|
|
|
public string TradeType { get; set; }
|
|
|
|
public List<int> AssetBookIds { get; set; }
|
|
|
|
public IEnumerable<int> AssetIdGroupList { get; set; }
|
|
|
|
public IEnumerable<int> UnderlyingIds { get; set; }
|
|
|
|
public IEnumerable<string> UnderlyingCodes { get; set; }
|
|
|
|
public string OptionCode { get; set; }
|
|
|
|
public string TradeSide { get; set; }
|
|
|
|
public DateTime? TradeDateFrom { get; set; }
|
|
|
|
public DateTime? TradeDateTo { get; set; }
|
|
|
|
public List<int> UserAssets { get; set; }
|
|
|
|
|
|
public Expression<Func<ExchangeTrade, bool>> BuildPredicate()
|
|
{
|
|
var predicate = PredicateBuilder.Create<ExchangeTrade>(n => n.IsValid);
|
|
if(UserAssets != null)
|
|
{
|
|
predicate = predicate.And(n => UserAssets.Contains(n.AssetBookId));
|
|
}
|
|
|
|
if (IsOption)
|
|
{
|
|
predicate = predicate.And(n => n.TradeType == "场内期权");
|
|
}
|
|
else
|
|
{
|
|
predicate = predicate.And(n => n.TradeType != "场内期权");
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(TradeType))
|
|
{
|
|
predicate = predicate.And(n => n.TradeType == TradeType);
|
|
}
|
|
|
|
if (UnderlyingIds != null && UnderlyingIds.Any(n => n > 0))
|
|
{
|
|
predicate = predicate.And(n => UnderlyingIds.Contains(n.UnderlyingId));
|
|
}
|
|
|
|
if (UnderlyingCodes != null && UnderlyingCodes.Any(n => !string.IsNullOrEmpty(n)))
|
|
{
|
|
predicate = predicate.And(n => UnderlyingCodes.Contains(n.UnderlyingCode));
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(OptionCode))
|
|
{
|
|
predicate = predicate.And(n => n.OptionCode == OptionCode);
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(TradeSide))
|
|
{
|
|
predicate = predicate.And(n => n.TradeSide == TradeSide);
|
|
}
|
|
|
|
if (TradeDateFrom != null)
|
|
{
|
|
predicate = predicate.And(n => n.TradeDate >= TradeDateFrom.Value);
|
|
}
|
|
|
|
if (TradeDateTo != null)
|
|
{
|
|
predicate = predicate.And(n => n.TradeDate <= TradeDateTo.Value);
|
|
}
|
|
|
|
if (AssetBookIds.Any())
|
|
{
|
|
predicate = predicate.And(n => AssetBookIds.Contains(n.AssetBookId));
|
|
}
|
|
|
|
return predicate;
|
|
}
|
|
|
|
public Expression<Func<TradePosition, bool>> BuildPositionPredicateBase()
|
|
{
|
|
var predicate = PredicateBuilder.Create(PredicateBuilder.True<TradePosition>());
|
|
List<string> tradetypes = new List<string> { "股票", "商品期货", "商品现货", "场内期权", "利率债", "信用债", "其它债券" };
|
|
if (PS.Config.IsGuoJun)
|
|
{
|
|
tradetypes.Remove("场内期权");
|
|
}
|
|
|
|
if(UserAssets != null)
|
|
{
|
|
predicate = predicate.And(n => UserAssets.Contains(n.BookId));
|
|
}
|
|
|
|
predicate = predicate.And(n => tradetypes.Contains(n.TradeType) && n.Position != 0);
|
|
|
|
if (!string.IsNullOrEmpty(TradeType))
|
|
{
|
|
predicate = predicate.And(n => n.TradeType == TradeType);
|
|
}
|
|
|
|
if (UnderlyingIds != null && UnderlyingIds.Any(n => n > 0))
|
|
{
|
|
predicate = predicate.And(n => UnderlyingIds.Contains(n.UnderlyingId));
|
|
}
|
|
|
|
if (UnderlyingCodes != null && UnderlyingCodes.Any(n => !string.IsNullOrEmpty(n)))
|
|
{
|
|
predicate = predicate.And(n => UnderlyingCodes.Contains(n.UnderlyingCode));
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(OptionCode))
|
|
{
|
|
predicate = predicate.And(n => n.InstrumentCode == OptionCode);
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(TradeSide))
|
|
{
|
|
PositionTypeFlag positionType = PositionTypeFlag.Unknown;
|
|
if (TradeSide.Contains("多头"))
|
|
{
|
|
positionType = PositionTypeFlag.Long;
|
|
}
|
|
else
|
|
{
|
|
positionType = PositionTypeFlag.Short;
|
|
}
|
|
predicate = predicate.And(n => n.PositionType == positionType);
|
|
}
|
|
if (AssetBookIds.Any())
|
|
{
|
|
predicate = predicate.And(n => AssetBookIds.Contains(n.BookId));
|
|
}
|
|
return predicate;
|
|
}
|
|
|
|
public int page { get; set; }
|
|
|
|
public int rows { get; set; }
|
|
|
|
public string sidx { get; set; }
|
|
|
|
public string sord { get; set; }
|
|
}
|
|
}
|