namespace Yw.Service { /// /// 指标评价模型 /// public partial class PumpCurve { #region Cache //获取缓存 private static List GetCache() { var all = PumpCurveCacheHelper.GetSet(() => { var dal = new DAL.PumpCurve(); var entityList = dal.GetAll(); var modelList = Entity2Models(entityList); if (modelList == null) { modelList = new List(); } return modelList; }, ConfigHelper.CacheKeepTime, ConfigHelper.CacheRandomTime); return all; } //通过 ID 更新缓存 internal static void UpdateCache(long ID) { var dal = new DAL.PumpCurve(); var entityDb = dal.GetByID(ID); var modelDb = Entity2Model(entityDb); var all = GetCache(); var model = all.Find(x => x.ID == ID); if (model == null) { all.Add(modelDb); } else { model.Reset(modelDb); } PumpCurveCacheHelper.Trigger(); } //通过 Ids 更新缓存 private static void UpdateCache(List Ids) { if (Ids == null || Ids.Count < 1) return; var dal = new DAL.PumpCurve(); var entityList = dal.GetByIds(Ids); var modelList = Entity2Models(entityList); var all = GetCache(); all.RemoveAll(x => Ids.Contains(x.ID)); if (modelList != null && modelList.Count > 0) { all.AddRange(modelList); } PumpCurveCacheHelper.Trigger(); } //移除缓存 private static void RemoveCache(long ID) { var all = GetCache(); all.RemoveAll(x => x.ID == ID); PumpCurveCacheHelper.Trigger(); } #endregion #region Query /// /// 获取所有 /// public List GetAll() { var all = GetCache(); return all.OrderBy(x => x.CreateTime).ToList(); } /// /// 通过 ID 获取 /// public Model.PumpCurve GetByID(long ID) { var all = GetAll(); return all.Find(x => x.ID == ID); } /// /// 通过 ID 获取 /// public List GetByIds(List Ids) { if (Ids == null || Ids.Count < 1) { return default; } var all = GetAll(); return all.Where(x => Ids.Contains(x.ID)).OrderBy(x => x.CreateTime).ToList(); } #endregion #region Insert /// /// 添加一条 /// public long Insert(Model.PumpCurve model) { if (model == null) { return default; } var entity = Model2Entity(model); var dal = new DAL.PumpCurve(); var id = dal.Insert(entity); if (id > 0) { UpdateCache(id); } return id; } /// /// 批量插入 /// public bool Inserts(List list) { if (list == null || list.Count < 1) { return false; } var dal = new DAL.PumpCurve(); var entity_list = Model2Entities(list); var ids = dal.InsertsR(entity_list); if (ids != null && ids.Count > 0) { UpdateCache(ids); return true; } return false; } #endregion #region Update /// /// 更新一条 /// public bool Update(Model.PumpCurve model) { if (model == null) { return false; } var entity = Model2Entity(model); var dal = new DAL.PumpCurve(); var bol = dal.Update(entity); if (bol) { UpdateCache(model.ID); } return bol; } /// /// 批量更新 /// public bool Updates(List list) { if (list == null || list.Count < 1) { return false; } var entity_list = Model2Entities(list.ToList()); var dal = new DAL.PumpCurve(); var bol = dal.Updates(entity_list); if (bol) { UpdateCache(list.Select(x => x.ID).ToList()); } return bol; } /// /// 更新 CurveInfo /// public bool UpdateCurveInfo(long ID, PumpCurveInfoModel CurveInfo, long UpdateUserID, DateTime UpdateTime) { if (ID < 1) { return false; } var dal = new DAL.PumpCurve(); var bol = dal.UpdateCurveInfo(ID, CurveInfo?.ToJson(), UpdateUserID, UpdateTime); if (bol) { UpdateCache(ID); } return bol; } /// /// 更新 CoordParas /// public bool UpdateCoordParas(long ID, Dictionary CoordParas, long UpdateUserID, DateTime UpdateTime) { if (ID < 1) { return false; } var dal = new DAL.PumpCurve(); var bol = dal.UpdateCoordParas(ID, ParasHelper.ToString(CoordParas), UpdateUserID, UpdateTime); if (bol) { UpdateCache(ID); } return bol; } /// /// 更新 ReliabilityStatus /// public bool UpdateReliabilityStatus(long ID, eReliabilityStatus ReliabilityStatus, long UpdateUserID, DateTime UpdateTime) { if (ID < 1) { return false; } var dal = new DAL.PumpCurve(); var bol = dal.UpdateReliabilityStatus(ID, (int)ReliabilityStatus, UpdateUserID, UpdateTime); if (bol) { UpdateCache(ID); } return bol; } #endregion #region Delete /// /// 通过 ID 删除 /// public bool DeleteByID(long ID, out string Msg) { Msg = string.Empty; if (new PumpCurveMapping().IsExistByCurveID(ID)) { Msg = "存在曲线关联关系"; return false; } var dal = new DAL.PumpCurve(); var bol = dal.DeleteByID(ID); if (bol) { RemoveCache(ID); } return bol; } /// /// 通过 ID 删除 (同时删除曲线映射) /// public bool DeleteExByID(long ID, out string Msg) { Msg = string.Empty; var dal = new DAL.PumpCurve(); var bol = dal.DeleteExByID(ID); if (bol) { RemoveCache(ID); PumpCurveMapping.RemoveCacheByCurveID(ID); } return bol; } #endregion } }