using DocumentFormat.OpenXml; using Microsoft.Extensions.Caching.Memory; using YieldChain.Helpers; using YLErp.DBModels.Enums; using YLErp.Events; using YLErp.Model.Enum; using YLErp.Modules.DataCacheModule; using YLErp.Modules.DataProviderModule; using YLErp.Modules.UnderlyingModule; using YLErp.Web.Models.JsModels; namespace YLErp.Web.Models { /// /// JS数据模型 /// public static class JsDataModel { const int CacheSeconds = 2 * 60; static readonly MemoryCache _cache; static JsDataModel() { _cache = new MemoryCache(new MemoryCacheOptions()); EventBus.Subscribe(t => { //两次trim后全部清空 _cache.Compact(1); _cache.Compact(1); }); } /// /// 对冲账户信息 /// public static IEnumerable GetExchangeAccounts() { if (!(_cache.Get(nameof(GetExchangeAccounts)) is IEnumerable datas)) { datas = DataCacheProvider.GetExchangeAccountDataSource().AsQueryable() .Select(n => new ExchangeAccountJsModel { id = n.id, Name = n.AccountName, Code = n.AccountCode }).ToArray(); foreach (var item in datas) { item.PinYin = PingYinHelper.GetFirstPinYin(item.Name)?.ToUpperInvariant(); } _cache.Set(nameof(GetExchangeAccounts), datas, DateTimeOffset.Now.AddSeconds(CacheSeconds)); } return datas; } /// /// 簿记(受权限‘交易管理_查看所有交易’影响) /// public static IEnumerable GetAssetUnits(UserInfo userInfo) { return InnerGetAssetUnits(userInfo, userInfo.交易管理_查看所有交易); } public static IEnumerable GetAllAssetUnits() { return InnerGetAssetUnits(null, true); } private static IEnumerable InnerGetAssetUnits(UserInfo userInfo, bool showAll = false) { var datas = DataCacheManager.GetAssetUnitDataSource().AsQueryable(); if (!showAll && !userInfo.交易管理_新增选择交易员) { var userId = "," + userInfo.UserId + ","; datas = datas.Where(a => ("," + a.TraderIds + ",").Contains(userId)).OrderBy(n => n.Name); } return datas.OrderBy(n => n.Name).Select(n => new AssetBookJsModel { id = n.id, Name = n.Name, TraderIdsStr = n.TraderIds, PinYin = PingYinHelper.GetFirstPinYin(n.Name) }).ToArray(); } public static IEnumerable GetAssetUnitGroups() { return DataCacheManager.GetAssetUnitGroupDataSource().AsQueryable(); } /// /// 客户信息 /// public static IEnumerable GetClients() { if (!(_cache.Get(nameof(GetClients)) is IEnumerable datas)) { using (var db = DbContextFactory.GetClientDbContext(null)) { datas = db.client.AsNoTracking().Where(s => s.ProcessStatus == "已开户") .Select(n => new ClientJsModel { id = n.id, Name = n.Name, ShortName = n.Abbreviation, TwoSideMargin = n.MarginOptionType == 1, CanSell = n.RiskServiceDegree != null && n.RiskServiceDegree >= 5, MarginOptionType = n.MarginOptionType, AccessRule = n.AccessRule, IsCentralClearing = n.IsCentralClearing, CentralClearingPaltform = n.CentralClearingPaltform, TradingPaltform = n.TradingPaltform, BoundSide=n.BoundSide, Number=n.Number, SwapTradeType=n.SwapTradeType??0, MainProtocolCode=n.MainProtocolCode, SupProtocolCode=n.SupProtocolCode, }).ToArray(); foreach (var item in datas) { item.PinYin = PingYinHelper.GetFirstPinYin(item.Name); } _cache.Set(nameof(GetClients), datas, DateTimeOffset.Now.AddSeconds(CacheSeconds)); } } return datas; } /// /// 客户信息 -- 全部客户 /// public static IEnumerable GetAllClients() { if (!(_cache.Get(nameof(GetClients)) is IEnumerable datas)) { using (var db = DbContextFactory.GetClientDbContext(null)) { datas = db.client.AsNoTracking().Where(s => s.ProcessStatus == "已开户" || s.ProcessStatus == "已休眠" || s.ProcessStatus == "已销户") .Select(n => new ClientJsModel { id = n.id, Name = n.Name, ShortName = n.Abbreviation, TwoSideMargin = n.MarginOptionType == 1, CanSell = n.RiskServiceDegree != null && n.RiskServiceDegree >= 5, MarginOptionType = n.MarginOptionType, AccessRule = n.AccessRule, IsCentralClearing = n.IsCentralClearing, CentralClearingPaltform = n.CentralClearingPaltform, TradingPaltform = n.TradingPaltform, }).ToArray(); foreach (var item in datas) { item.PinYin = PingYinHelper.GetFirstPinYin(item.Name); } _cache.Set(nameof(GetClients), datas, DateTimeOffset.Now.AddSeconds(CacheSeconds)); } } return datas; } /// /// 标的信息 /// /// 仅活着的标的 /// 仅权益标的 public static IEnumerable GetUnderlyings(bool onlyLive, bool onlyEquity) { var predicate = PredicateBuilder.True(); if (onlyEquity) { predicate = predicate.And(n => n.UnderlyingInstrumentType == "Stock" || n.UnderlyingInstrumentType == "StockIndex" || n.CommodityCode.StartsWith("IC") || n.CommodityCode.StartsWith("IF") || n.CommodityCode.StartsWith("IH")); if (onlyLive) { predicate = predicate.And(n => n.LaunchState == "1"); } } else if (onlyLive) { predicate = predicate.And(n => n.UnderlyingState != "Matured" && n.LaunchState == "1"); } var useWhiteCode = new StockBlackWhiteService(OptUserInfo.SystemUser).GetStockBlackWhiteList(Configuration.Enums.LimitRangeEnum.Option, out var Codes); var useWhiteCode_Swap = new StockBlackWhiteService(OptUserInfo.SystemUser).GetStockBlackWhiteList(Configuration.Enums.LimitRangeEnum.Swap, out var SwapCodes); var datas = DataCacheProvider.GetUnderlyingDataSource().AsQueryable() .Where(predicate) .Select(n => new { n.id, Code = n.UnderlyingCode, Name = n.IsCombined() ? "" : n.UnderlyingName, InstrumentType = n.UnderlyingInstrumentType, VarietyId = n.UnderlyingTypeId, QuoteUnitString = n.QuoteUnitString, n.CommodityCode, n.Block1, n.Block2, n.Block3, n.Block4, n.Block5, n.PinYinFirst }).ToArray().Select(n => new UnderlyingJsModel { id = n.id, Code = n.Code, Name = n.Name ?? string.Empty, VarietyId = n.VarietyId, QuoteUnitString = n.QuoteUnitString, InstrumentType = n.InstrumentType, Blocks = new int[] { n.Block1, n.Block2, n.Block3, n.Block4, n.Block5 }, PinYin = n.PinYinFirst, BlackWhiteState = useWhiteCode ? 1 : 2, Disallow = ConsGlobal.InstrumentType.IsStock(n.InstrumentType) && Codes != null && useWhiteCode == Codes.Contains(n.Code), BlackWhiteState_Swap = useWhiteCode_Swap ? 1 : 2, Disallow_Swap = ConsGlobal.InstrumentType.IsStock(n.InstrumentType) && SwapCodes != null && useWhiteCode_Swap == SwapCodes.Contains(n.Code), IsCombined = n.CommodityCode == "组合标的" || n.CommodityCode == "篮子标的" }); return datas; } /// /// 标的信息(仅广发商贸使用) /// /// 仅活着的标的 /// 仅权益标的 public static IEnumerable GetGFUnderlyings(bool onlyLive, bool onlyEquity) { var predicate = PredicateBuilder.True(); if (onlyEquity) { predicate = predicate.And(n => n.UnderlyingInstrumentType == "Stock" || n.UnderlyingInstrumentType == "StockIndex" || n.CommodityCode.StartsWith("IC") || n.CommodityCode.StartsWith("IF") || n.CommodityCode.StartsWith("IH")); if (onlyLive) { predicate = predicate.And(n => n.LaunchState == "1"); } } else if (onlyLive) { predicate = predicate.And(n => n.UnderlyingState != "Matured" && n.LaunchState == "1"); } var useWhiteCode = new StockBlackWhiteService(OptUserInfo.SystemUser).GetStockBlackWhiteList(Configuration.Enums.LimitRangeEnum.Option, out var Codes); var useWhiteCode_Swap = new StockBlackWhiteService(OptUserInfo.SystemUser).GetStockBlackWhiteList(Configuration.Enums.LimitRangeEnum.Swap, out var SwapCodes); var datas = DataCacheProvider.GetUnderlyingDataSource().AsQueryable() .Where(predicate) .Select(n => new { n.id, Code = n.UnderlyingCode, Name = n.UnderlyingName, InstrumentType = n.UnderlyingInstrumentType, VarietyId = n.UnderlyingTypeId, QuoteUnitString = n.QuoteUnitString, n.CommodityCode, n.Block1, n.Block2, n.Block3, n.Block4, n.Block5, n.PinYinFirst }).ToArray().Select(n => new UnderlyingJsModel { id = n.id, Code = n.Code, Name = n.Name ?? string.Empty, VarietyId = n.VarietyId, QuoteUnitString = n.QuoteUnitString, InstrumentType = n.InstrumentType, Blocks = new int[] { n.Block1, n.Block2, n.Block3, n.Block4, n.Block5 }, PinYin = n.PinYinFirst, BlackWhiteState = useWhiteCode ? 1 : 2, Disallow = ConsGlobal.InstrumentType.IsStock(n.InstrumentType) && Codes != null && useWhiteCode == Codes.Contains(n.Code), BlackWhiteState_Swap = useWhiteCode_Swap ? 1 : 2, Disallow_Swap = ConsGlobal.InstrumentType.IsStock(n.InstrumentType) && SwapCodes != null && useWhiteCode_Swap == SwapCodes.Contains(n.Code), IsCombined = n.CommodityCode == "组合标的" || n.CommodityCode == "篮子标的" }); return datas; } /// /// 标的品种 /// /// 是否权限过滤 public static IEnumerable GetVarieties(UserInfo userInfo, bool filterByRole = false) { var predicate = PredicateBuilder.True(); if (filterByRole) { var vids = TradeRightProvider.GetUserVarietyIds(userInfo.UserId); if (!vids.Any(n => n > 0)) { return Enumerable.Empty(); } predicate = PredicateBuilder.Create(n => vids.Contains(n.id)); } return DataCacheProvider.GetVarietyDataSource().AsQueryable(predicate).ToArray().Select(n => { var PriceTick = 0.01; if (!string.IsNullOrWhiteSpace(n.MinPriceChange)) { var arr = n.MinPriceChange.SkipWhile(m => char.IsWhiteSpace(m)).TakeWhile(m => m == '.' || (m >= '0' && m <= '9')).ToArray(); double.TryParse(new string(arr), out PriceTick); if (PriceTick < 0.01) { PriceTick = 0.01; } } return new VarietyModel { id = n.id, Code = n.VarietyCode, Name = n.VarietyName, TradeUnit = n.TradeUnit, QuoteUnit = n.QuoteUnit, CountRatio = n.CountRatio, VolAdjust = n.VolatilityAdjust, QuoteCurrency = n.QuoteCurrency, PriceTick = PriceTick, ContractSize = n.TradeUnitValue ?? 0, PinYin = PingYinHelper.GetFirstPinYin(n.VarietyName) }; }).ToArray(); } /// /// 标的品种类型 /// public static IEnumerable GetVarietyTypes() { if (!(_cache.Get(nameof(GetVarietyTypes)) is IEnumerable datas)) { datas = BLL.DictionaryBLL.GetList("品种类型", false); _cache.Set(nameof(GetVarietyTypes), datas, DateTimeOffset.Now.AddSeconds(CacheSeconds)); } return datas; } /// /// 标的板块性质 /// public static IEnumerable GetUnderlyingBlocks() { if (!(_cache.Get(nameof(GetUnderlyingBlocks)) is IEnumerable datas)) { using (var db = DbContextFactory.GetYLDbContext()) { datas = db.UnderlyingBlockConfig.AsNoTracking() .Where(n => n.Group > 0) .Select(n => new UnderlyingBlockJsModel { id = n.id, Name = n.Name, Group = n.Group }).ToArray(); foreach (var item in datas) { item.PinYin = PingYinHelper.GetFirstPinYin(item.Name); } _cache.Set(nameof(GetUnderlyingBlocks), datas, DateTimeOffset.Now.AddSeconds(CacheSeconds)); } } return datas; } /// /// 交易员 /// public static IEnumerable GetTraders(IEnumerable assetunits = null) { var traders = UserBLL.GetUsersByPosition().AsEnumerable(); if (assetunits != null && assetunits.Any()) { var set = assetunits.SelectMany(n => n.TraderIds).ToHashSet(); traders = traders.Where(n => set.Contains(n.Id)).ToArray(); } return traders.Select(n => new TraderJsModel { id = n.Id, Name = n.Name, PinYin = PingYinHelper.GetFirstPinYin(n.Name) }); } /// /// 交易市场信息 /// public static IEnumerable GetMarkets() { var datas = DataCacheManager.GetMarketDataSource().AsQueryable(); return datas.Select(n => new MarketJsModel { id = n.id, Name = n.MarketName, Code = n.ExchangeNo }).ToArray(); } /// /// 交易市场信息 /// public static IEnumerable GetSales() { if (!(_cache.Get(nameof(GetSales)) is IEnumerable datas)) { using (var db = DbContextFactory.GetYLDbContext()) { datas = db.salesmen.AsNoTracking() .Where(n => n.Status == 0) .Select(n => new SalesJsModel { id = n.id, Name = n.Name }).ToArray(); foreach (var item in datas) { item.PinYin = PingYinHelper.GetFirstPinYin(item.Name); } _cache.Set(nameof(GetUnderlyingBlocks), datas, DateTimeOffset.Now.AddSeconds(CacheSeconds)); } } return datas; } public static IEnumerable GetCustomerManager() { if (!(_cache.Get(nameof(GetCustomerManager)) is IEnumerable datas)) { using (var db = DbContextFactory.GetErpBaseContext()) { datas = db.SystemUsers.AsNoTracking() .Where(n => n.State == 0 && (n.AccountPost & (int)AccountPostEnum.客户经理) > 0) .Select(n => new CustomerManagerJsModel { id = n.Id, Name = n.Name }).ToArray(); foreach (var item in datas) { item.PinYin = PingYinHelper.GetFirstPinYin(item.Name); } } } return datas; } public static List GetPushLogType() { var enumValues = System.Enum.GetValues(typeof(SwapPushDataEnum)).Cast(); var viewModelList = enumValues.Select(e => new SelectItem { Value = ((int)e).ToString(), Text = e.ToString() }).ToList(); return viewModelList; } public static List GetPushLogState() { var enumValues = System.Enum.GetValues(typeof(SwapPushDataStateEnum)).Cast(); var viewModelList = enumValues.Select(e => new SelectItem { Value = ((int)e).ToString(), Text = e.ToString() }).ToList(); return viewModelList; } } }