279 lines
11 KiB
C#
279 lines
11 KiB
C#
using MoreLinq;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using YLErp.Modules.CalcPriceShowConfigModule.Dto;
|
|
|
|
namespace YLErp.Modules.CalcPriceShowConfigModule
|
|
{
|
|
/// <summary>
|
|
/// 计算指标显示配置 服务
|
|
/// </summary>
|
|
public class CalcPriceShowConfigService : YLBaseService
|
|
{
|
|
|
|
private const string _configName = "CalcPriceShowConfig";
|
|
|
|
private static List<string> _defaultCalcQuota = new List<string> { "PV", "Delta手数", "Gamma手数", "Vega", "Theta", "Rho" };
|
|
|
|
|
|
|
|
|
|
public CalcPriceShowConfigService(OptUserInfo userInfo) : base(userInfo)
|
|
{
|
|
}
|
|
|
|
|
|
public List<CalcPriceShowCofingDto> GetList()
|
|
{
|
|
var model = DbContext.SysUserConfig.AsNoTracking().FirstOrDefault(p => p.UserId == 0 && p.ConfigType == SysUserConfigType.CalcPriceShowConfig && _configName.Equals(p.ConfigName));
|
|
if (model == null || string.IsNullOrEmpty(model.ConfigData))
|
|
{
|
|
return new List<CalcPriceShowCofingDto>();
|
|
}
|
|
return JsonHelper.Deserialize<List<CalcPriceShowCofingDto>>(model.ConfigData);
|
|
}
|
|
|
|
|
|
public CalcPriceShowCofingDto GetById(int id)
|
|
{
|
|
var list = GetList();
|
|
if (list == null||!list.Any(p=>p.Id==id))
|
|
{
|
|
throw new ServiceException("未找到对应数据");
|
|
}
|
|
return list.First(p => p.Id == id);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 保存数据
|
|
/// </summary>
|
|
/// <param name="dto"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="ServiceException"></exception>
|
|
public bool SaveData(CalcPriceShowCofingDto dto)
|
|
{
|
|
if (string.IsNullOrEmpty(dto.Name))
|
|
{
|
|
throw new ServiceException("模板名称不能为空");
|
|
}
|
|
if (dto.Name.Length > 25)
|
|
{
|
|
throw new ServiceException("模板名称不能超过25个字符");
|
|
}
|
|
if (dto.CalcQuota == null || dto.CalcQuota.Count == 0)
|
|
{
|
|
throw new ServiceException("至少需要选择一个计算指标");
|
|
}
|
|
var calcQuotaNames = GetAllCalcQuota().Select(p => p.Name).ToList();
|
|
if (dto.CalcQuota.Any(p => !calcQuotaNames.Contains(p.Name)))
|
|
{
|
|
throw new ServiceException("存在非法计算指标");
|
|
}
|
|
var model = DbContext.SysUserConfig.FirstOrDefault(p => p.UserId == 0 && p.ConfigType == SysUserConfigType.CalcPriceShowConfig && _configName.Equals(p.ConfigName));
|
|
if (model == null)
|
|
{
|
|
model = new SysUserConfig
|
|
{
|
|
UserId = 0,
|
|
ConfigType = SysUserConfigType.CalcPriceShowConfig,
|
|
ConfigName=_configName,
|
|
CreateTime = DateTime.Now,
|
|
UpdateTime = DateTime.Now,
|
|
};
|
|
DbContext.SysUserConfig.Add(model);
|
|
}
|
|
List<CalcPriceShowCofingDto> configList = null;
|
|
if (!string.IsNullOrEmpty(model.ConfigData))
|
|
{
|
|
configList = JsonHelper.Deserialize<List<CalcPriceShowCofingDto>>(model.ConfigData);
|
|
}
|
|
if (configList == null)
|
|
{
|
|
configList = new List<CalcPriceShowCofingDto>();
|
|
}
|
|
bool isAdd = false;
|
|
if (dto.Id == 0 || !configList.Any(p => p.Id == dto.Id))
|
|
{
|
|
isAdd = true;
|
|
}
|
|
if (dto.IsEnable)
|
|
{
|
|
configList.ForEach(p => { p.IsEnable = false; });
|
|
}
|
|
if (isAdd)
|
|
{
|
|
dto.Id = configList.Count > 0 ? (configList.Max(p => p.Id) + 1) : 1;
|
|
if (configList.Count >= 10)
|
|
{
|
|
throw new ServiceException("计算指标显示模板不能超过10条");
|
|
}
|
|
if (configList.Any(p => p.Name.Equals(dto.Name)))
|
|
{
|
|
throw new ServiceException("模板名称不允许重复");
|
|
}
|
|
configList.Add(dto);
|
|
}
|
|
else
|
|
{
|
|
if (configList.Any(p => p.Name.Equals(dto.Name) && p.Id != dto.Id))
|
|
{
|
|
throw new ServiceException("模板名称不允许重复");
|
|
}
|
|
var configDto = configList.First(p => p.Id == dto.Id);
|
|
configDto.Name = dto.Name;
|
|
configDto.CalcQuota = dto.CalcQuota;
|
|
configDto.IsEnable = dto.IsEnable;
|
|
}
|
|
model.ConfigData = JsonHelper.Serialize(configList);
|
|
model.UpdateTime = DateTime.Now;
|
|
DbContext.SaveChanges();
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 设为应用
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <exception cref="ServiceException"></exception>
|
|
public void SetIsEnable(int id)
|
|
{
|
|
if (id == 0)
|
|
{
|
|
throw new ServiceException("请选择要应用的数据");
|
|
}
|
|
var model = DbContext.SysUserConfig.FirstOrDefault(p => p.UserId == 0 && p.ConfigType == SysUserConfigType.CalcPriceShowConfig && _configName.Equals(p.ConfigName));
|
|
if (model == null || string.IsNullOrEmpty(model.ConfigData))
|
|
{
|
|
throw new ServiceException("未找到模板配置数据");
|
|
}
|
|
|
|
var list= JsonHelper.Deserialize<List<CalcPriceShowCofingDto>>(model.ConfigData);
|
|
if (list == null || !list.Any(p => p.Id == id))
|
|
{
|
|
throw new ServiceException("请选择要应用的数据");
|
|
}
|
|
|
|
list.ForEach(p =>
|
|
{
|
|
p.IsEnable = p.Id == id;
|
|
});
|
|
|
|
model.ConfigData = JsonHelper.Serialize(list);
|
|
model.UpdateTime = DateTime.Now;
|
|
DbContext.SaveChanges();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <exception cref="ServiceException"></exception>
|
|
public void Delete(int id)
|
|
{
|
|
if (id == 0)
|
|
{
|
|
throw new ServiceException("请选择要删除的数据");
|
|
}
|
|
var model = DbContext.SysUserConfig.FirstOrDefault(p => p.UserId == 0 && p.ConfigType == SysUserConfigType.CalcPriceShowConfig && _configName.Equals(p.ConfigName));
|
|
if (model == null || string.IsNullOrEmpty(model.ConfigData))
|
|
{
|
|
throw new ServiceException("未找到模板配置数据");
|
|
}
|
|
|
|
var list = JsonHelper.Deserialize<List<CalcPriceShowCofingDto>>(model.ConfigData);
|
|
if (list == null || !list.Any(p => p.Id == id))
|
|
{
|
|
throw new ServiceException("请选择要删除的数据");
|
|
}
|
|
|
|
var dto = list.First(p => p.Id == id);
|
|
if (dto.IsEnable)
|
|
{
|
|
throw new ServiceException("无法删除正在应用的模板数据");
|
|
}
|
|
|
|
list.Remove(dto);
|
|
model.ConfigData = JsonHelper.Serialize(list);
|
|
model.UpdateTime = DateTime.Now;
|
|
DbContext.SaveChanges();
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取显示定价指标
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static CalcPriceShowCofingDto GetShowCalcQuota()
|
|
{
|
|
using (var db = DbContextFactory.GetYLDbContext())
|
|
{
|
|
var model = db.SysUserConfig.AsNoTracking().FirstOrDefault(p => p.UserId == 0 && p.ConfigType == SysUserConfigType.CalcPriceShowConfig && _configName.Equals(p.ConfigName));
|
|
if (model == null || string.IsNullOrEmpty(model.ConfigData))
|
|
{
|
|
return GetDefaultCalcQuota();
|
|
}
|
|
var list = JsonHelper.Deserialize<List<CalcPriceShowCofingDto>>(model.ConfigData);
|
|
var dto = list.FirstOrDefault(p => p.IsEnable);
|
|
if (dto == null)
|
|
{
|
|
return GetDefaultCalcQuota();
|
|
}
|
|
return dto;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取所有定价指标
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static List<CalcQuotaDto> GetAllCalcQuota()
|
|
{
|
|
return new List<CalcQuotaDto> {
|
|
new CalcQuotaDto{Name="PV",Tip=""},
|
|
new CalcQuotaDto{Name="Delta手数",Tip="每报价单位Delta*手数"},
|
|
new CalcQuotaDto{Name="Gamma手数",Tip="每报价单位Gamma*手数"},
|
|
new CalcQuotaDto{Name="Theta",Tip="每报价单位Theta*交易份额"},
|
|
new CalcQuotaDto{Name="Vega",Tip="每报价单位Vega*交易份额"},
|
|
new CalcQuotaDto{Name="Rho",Tip="每报价单位Rho*交易份额*100"},
|
|
new CalcQuotaDto{Name="Delta",Tip=""},
|
|
new CalcQuotaDto{Name="Gamma",Tip=""},
|
|
new CalcQuotaDto{Name="Deltacash",Tip=""},
|
|
new CalcQuotaDto{Name="Gammacash",Tip=""},
|
|
new CalcQuotaDto{Name="Vegacash",Tip=""},
|
|
new CalcQuotaDto{Name="PV*",Tip="*号标记的字段是所有涉及到敲出的期权结构,若当天为该期权的观察日,当盘中实时价格越过敲出价时,该笔结构的估值只包含Payoff,不需要再包含时间价值,同时该笔交易的Delta手数和Gamma手数都变成0\r\n\r\n系统默认在盘中的时候期权价值=内在价值+时间价值"},
|
|
new CalcQuotaDto{Name="Delta*",Tip="*号标记的字段是所有涉及到敲出的期权结构,若当天为该期权的观察日,当盘中实时价格越过敲出价时,该笔结构的估值只包含Payoff,不需要再包含时间价值,同时该笔交易的Delta手数和Gamma手数都变成0\r\n\r\n系统默认在盘中的时候期权价值=内在价值+时间价值"},
|
|
new CalcQuotaDto{Name="Gamma*",Tip="*号标记的字段是所有涉及到敲出的期权结构,若当天为该期权的观察日,当盘中实时价格越过敲出价时,该笔结构的估值只包含Payoff,不需要再包含时间价值,同时该笔交易的Delta手数和Gamma手数都变成0\r\n\r\n系统默认在盘中的时候期权价值=内在价值+时间价值"}
|
|
};
|
|
}
|
|
|
|
|
|
public static CalcPriceShowCofingDto GetDefaultCalcQuota()
|
|
{
|
|
var result = new CalcPriceShowCofingDto {CalcQuota=new List<CalcQuotaCfg>() };
|
|
|
|
var allQuotas = GetAllCalcQuota();
|
|
allQuotas.ForEach(p =>
|
|
{
|
|
if (_defaultCalcQuota.Contains(p.Name))
|
|
{
|
|
result.CalcQuota.Add(new CalcQuotaCfg { Name = p.Name, IsShow = true });
|
|
}
|
|
else
|
|
{
|
|
result.CalcQuota.Add(new CalcQuotaCfg { Name = p.Name, IsShow = false });
|
|
}
|
|
});
|
|
return result;
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|