using BaseOUDAL; using YLErp.Helpers; namespace YLErp.Modules.ScenarioModule { /// /// 情景配置数据服务 /// public class ScenarioConfigService : YLBaseService { public ScenarioConfigService(OptUserInfo optUser) : base(optUser) { } /// /// 保存数据 /// 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; } /// /// 移除数据 /// 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(); } /// /// 获取用户情景配置 /// public List GetUserScens() { return DbContext.ScenarioConfig.AsNoTracking().Where(s => s.UserId == UserId).ToList(); } /// /// 查询情景配置 /// public SearchListResult 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; } /// /// 获取单条数据 /// 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(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(); } } /// /// /// public class ScenarioReq : BaseSearchReq { /// /// 情景名称 /// public string ConfigName { get; set; } /// /// /// public int? UserId { get; set; } } }