485 lines
20 KiB
C#
485 lines
20 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// JS数据模型
|
|
/// </summary>
|
|
public static class JsDataModel
|
|
{
|
|
const int CacheSeconds = 2 * 60;
|
|
|
|
static readonly MemoryCache _cache;
|
|
|
|
static JsDataModel()
|
|
{
|
|
_cache = new MemoryCache(new MemoryCacheOptions());
|
|
|
|
EventBus.Subscribe<DataCacheUpdateEvent>(t =>
|
|
{
|
|
//两次trim后全部清空
|
|
_cache.Compact(1);
|
|
_cache.Compact(1);
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 对冲账户信息
|
|
/// </summary>
|
|
public static IEnumerable<ExchangeAccountJsModel> GetExchangeAccounts()
|
|
{
|
|
if (!(_cache.Get(nameof(GetExchangeAccounts)) is IEnumerable<ExchangeAccountJsModel> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 簿记(受权限‘交易管理_查看所有交易’影响)
|
|
/// </summary>
|
|
public static IEnumerable<AssetBookJsModel> GetAssetUnits(UserInfo userInfo)
|
|
{
|
|
return InnerGetAssetUnits(userInfo, userInfo.交易管理_查看所有交易);
|
|
}
|
|
|
|
public static IEnumerable<AssetBookJsModel> GetAllAssetUnits()
|
|
{
|
|
return InnerGetAssetUnits(null, true);
|
|
}
|
|
|
|
private static IEnumerable<AssetBookJsModel> 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<AssetUnitGroup> GetAssetUnitGroups()
|
|
{
|
|
return DataCacheManager.GetAssetUnitGroupDataSource().AsQueryable();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 客户信息
|
|
/// </summary>
|
|
public static IEnumerable<ClientJsModel> GetClients()
|
|
{
|
|
if (!(_cache.Get(nameof(GetClients)) is IEnumerable<ClientJsModel> 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;
|
|
}
|
|
/// <summary>
|
|
/// 客户信息 -- 全部客户
|
|
/// </summary>
|
|
public static IEnumerable<ClientJsModel> GetAllClients()
|
|
{
|
|
if (!(_cache.Get(nameof(GetClients)) is IEnumerable<ClientJsModel> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 标的信息
|
|
/// </summary>
|
|
/// <param name="onlyLive">仅活着的标的</param>
|
|
/// <param name="onlyEquity">仅权益标的</param>
|
|
public static IEnumerable<UnderlyingJsModel> GetUnderlyings(bool onlyLive, bool onlyEquity)
|
|
{
|
|
var predicate = PredicateBuilder.True<underlying_manager>();
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 标的信息(仅广发商贸使用)
|
|
/// </summary>
|
|
/// <param name="onlyLive">仅活着的标的</param>
|
|
/// <param name="onlyEquity">仅权益标的</param>
|
|
public static IEnumerable<UnderlyingJsModel> GetGFUnderlyings(bool onlyLive, bool onlyEquity)
|
|
{
|
|
var predicate = PredicateBuilder.True<underlying_manager>();
|
|
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 标的品种
|
|
/// </summary>
|
|
/// <param name="filterByRight">是否权限过滤</param>
|
|
public static IEnumerable<VarietyModel> GetVarieties(UserInfo userInfo, bool filterByRole = false)
|
|
{
|
|
var predicate = PredicateBuilder.True<Variety>();
|
|
|
|
if (filterByRole)
|
|
{
|
|
var vids = TradeRightProvider.GetUserVarietyIds(userInfo.UserId);
|
|
if (!vids.Any(n => n > 0))
|
|
{
|
|
return Enumerable.Empty<VarietyModel>();
|
|
}
|
|
predicate = PredicateBuilder.Create<Variety>(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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 标的品种类型
|
|
/// </summary>
|
|
public static IEnumerable<SelectItem> GetVarietyTypes()
|
|
{
|
|
if (!(_cache.Get(nameof(GetVarietyTypes)) is IEnumerable<SelectItem> datas))
|
|
{
|
|
datas = BLL.DictionaryBLL.GetList("品种类型", false);
|
|
_cache.Set(nameof(GetVarietyTypes), datas, DateTimeOffset.Now.AddSeconds(CacheSeconds));
|
|
}
|
|
return datas;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 标的板块性质
|
|
/// </summary>
|
|
public static IEnumerable<UnderlyingBlockJsModel> GetUnderlyingBlocks()
|
|
{
|
|
if (!(_cache.Get(nameof(GetUnderlyingBlocks)) is IEnumerable<UnderlyingBlockJsModel> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 交易员
|
|
/// </summary>
|
|
public static IEnumerable<TraderJsModel> GetTraders(IEnumerable<AssetBookJsModel> 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)
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 交易市场信息
|
|
/// </summary>
|
|
public static IEnumerable<MarketJsModel> GetMarkets()
|
|
{
|
|
var datas = DataCacheManager.GetMarketDataSource().AsQueryable();
|
|
return datas.Select(n => new MarketJsModel
|
|
{
|
|
id = n.id,
|
|
Name = n.MarketName,
|
|
Code = n.ExchangeNo
|
|
}).ToArray();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 交易市场信息
|
|
/// </summary>
|
|
public static IEnumerable<SalesJsModel> GetSales()
|
|
{
|
|
if (!(_cache.Get(nameof(GetSales)) is IEnumerable<SalesJsModel> 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<CustomerManagerJsModel> GetCustomerManager()
|
|
{
|
|
if (!(_cache.Get(nameof(GetCustomerManager)) is IEnumerable<CustomerManagerJsModel> 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<SelectItem> GetPushLogType()
|
|
{
|
|
var enumValues = System.Enum.GetValues(typeof(SwapPushDataEnum)).Cast<SwapPushDataEnum>();
|
|
var viewModelList = enumValues.Select(e => new SelectItem
|
|
{
|
|
Value = ((int)e).ToString(),
|
|
Text = e.ToString()
|
|
}).ToList();
|
|
return viewModelList;
|
|
}
|
|
public static List<SelectItem> GetPushLogState()
|
|
{
|
|
var enumValues = System.Enum.GetValues(typeof(SwapPushDataStateEnum)).Cast<SwapPushDataStateEnum>();
|
|
var viewModelList = enumValues.Select(e => new SelectItem
|
|
{
|
|
Value = ((int)e).ToString(),
|
|
Text = e.ToString()
|
|
}).ToList();
|
|
return viewModelList;
|
|
}
|
|
}
|
|
} |