231 lines
6.6 KiB
C#
231 lines
6.6 KiB
C#
using BaseOUDAL;
|
|
using YLErp.Helpers;
|
|
|
|
namespace YLErp.Modules.ScenarioModule
|
|
{
|
|
/// <summary>
|
|
/// 情景配置数据服务
|
|
/// </summary>
|
|
public class ScenarioConfigService : YLBaseService
|
|
{
|
|
public ScenarioConfigService(OptUserInfo optUser) : base(optUser)
|
|
{
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存数据
|
|
/// </summary>
|
|
public ScenarioConfigV2 SaveData(ScenarioConfigV2 req)
|
|
{
|
|
if (req == null)
|
|
{
|
|
throw new ServiceException("数据不能为空");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(req.ConfigName))
|
|
{
|
|
throw new ServiceException("情景名称 必须填写");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(req.xType))
|
|
{
|
|
throw new ServiceException("横坐标类型 必须填写");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(req.yType))
|
|
{
|
|
throw new ServiceException("纵坐标类型 必须填写");
|
|
}
|
|
|
|
if (req.xType == req.yType)
|
|
{
|
|
throw new ServiceException("横纵坐标类型 不能一致");
|
|
}
|
|
|
|
if (!ScenarioHelper.XyTypes.Contains(req.xType))
|
|
{
|
|
throw new ServiceException("横坐标类型 不支持:" + req.xType);
|
|
}
|
|
|
|
if (!ScenarioHelper.XyTypes.Contains(req.yType))
|
|
{
|
|
throw new ServiceException("纵坐标类型 不支持:" + req.yType);
|
|
}
|
|
|
|
var special = req.ParseSepcial();
|
|
if (special.HasItem())
|
|
{
|
|
if (special.VarityList != null && special.VarityList.Count != special.VarityList.Select(n => n.id).ToHashSet().Count)
|
|
{
|
|
throw new ServiceException("标的品种变动率 有重复的标的品种!");
|
|
}
|
|
|
|
if (special.UnderlyingList != null && special.UnderlyingList.Count != special.UnderlyingList.Select(n => n.id).ToHashSet().Count)
|
|
{
|
|
throw new ServiceException("标的代码变动率 有重复的标的品种!");
|
|
}
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(req.EncryptId))
|
|
{
|
|
req.id = DataProtectHelper.DecryptInt(req.EncryptId);
|
|
}
|
|
|
|
ScenarioConfigV2 dbModel;
|
|
|
|
var isAddNew = req.id == 0;
|
|
|
|
if (isAddNew)
|
|
{
|
|
DbContext.ScenarioConfig.Add(dbModel = req);
|
|
}
|
|
else
|
|
{
|
|
dbModel = DbContext.ScenarioConfig.Find(req.id);
|
|
if (dbModel == null)
|
|
{
|
|
throw new ServiceException("更新失败,数据已不存在!");
|
|
}
|
|
UpdateEntity(dbModel, req);
|
|
}
|
|
|
|
SetDBModelOpt(dbModel);
|
|
|
|
DbContext.SaveChanges();
|
|
|
|
return dbModel;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 移除数据
|
|
/// </summary>
|
|
public int RemoveData(string enid)
|
|
{
|
|
var intid = DataProtectHelper.DecryptInt(enid);
|
|
var dbModel = DbContext.ScenarioConfig.Find(intid);
|
|
if (dbModel == null)
|
|
{
|
|
throw new ServiceException("找不到情景分析配置");
|
|
}
|
|
|
|
DbContext.ScenarioConfig.Remove(dbModel);
|
|
|
|
return DbContext.SaveChanges();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取用户情景配置
|
|
/// </summary>
|
|
public List<ScenarioConfigV2> GetUserScens()
|
|
{
|
|
return DbContext.ScenarioConfig.AsNoTracking().Where(s => s.UserId == UserId).ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询情景配置
|
|
/// </summary>
|
|
public SearchListResult<ScenarioConfigV2> SearchList(ScenarioReq req)
|
|
{
|
|
var query = DbContext.ScenarioConfig.AsNoTracking().AsQueryable();
|
|
|
|
if (!string.IsNullOrEmpty(req.ConfigName))
|
|
{
|
|
query = query.Where(d => d.ConfigName.Contains(req.ConfigName));
|
|
}
|
|
|
|
if (req.UserId != null)
|
|
{
|
|
if (string.IsNullOrEmpty(req.ConfigName))
|
|
{
|
|
query = query.Where(d => d.UserId == req.UserId.Value || d.ConfigName == "对冲风险压力测试");
|
|
}
|
|
else
|
|
{
|
|
query = query.Where(d => d.UserId == req.UserId.Value);
|
|
}
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(req.sidx))
|
|
{
|
|
req.sidx = "id";
|
|
req.sord = "desc";
|
|
}
|
|
|
|
var searchResult = query.ToSearchList(req);
|
|
|
|
foreach (var config in searchResult.rows)
|
|
{
|
|
SetSpecial(config);
|
|
}
|
|
|
|
return searchResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取单条数据
|
|
/// </summary>
|
|
public ScenarioConfigV2 GetData(int id)
|
|
{
|
|
var dbModel = DbContext.ScenarioConfig.AsNoTracking().FirstOrDefault(n => n.id == id);
|
|
|
|
if (dbModel != null)
|
|
{
|
|
SetSpecial(dbModel);
|
|
}
|
|
|
|
return dbModel;
|
|
}
|
|
|
|
private void SetSpecial(ScenarioConfigV2 config)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(config.SpecialConfig))
|
|
{
|
|
return;
|
|
}
|
|
|
|
var special = JsonHelper.ToObject<ScenarioSpecial>(config.SpecialConfig);
|
|
|
|
if (special.VarityList != null)
|
|
{
|
|
foreach (var item in special.VarityList)
|
|
{
|
|
if (string.IsNullOrEmpty(item.Name))
|
|
{
|
|
item.Name = DataCacheProvider.GetVarietyDataSource().GetData(item.id)?.VarietyName;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (special.UnderlyingList != null)
|
|
{
|
|
foreach (var item in special.UnderlyingList)
|
|
{
|
|
if (string.IsNullOrEmpty(item.Name))
|
|
{
|
|
item.Name = DataCacheProvider.GetUnderlyingDataSource().GetData(item.id)?.UnderlyingCode;
|
|
}
|
|
}
|
|
}
|
|
|
|
config.SpecialConfig = special.ToJson();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class ScenarioReq : BaseSearchReq
|
|
{
|
|
/// <summary>
|
|
/// 情景名称
|
|
/// </summary>
|
|
public string ConfigName { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public int? UserId { get; set; }
|
|
}
|
|
}
|