using System.Collections.Generic; using System.Linq; namespace IStation.BLL { /// /// 泵变速曲线 /// public partial class PumpSpeedCurve { private readonly DAL.PumpSpeedCurve _dal = new DAL.PumpSpeedCurve(); #region Cache // 获取缓存 private List GetCache() { return CacheHelper.GetSet(SettingsD.Project.ID, () => { var entities = _dal.GetAll(); var models = Entity2Models(entities); if (models == null) { models = new List(); } return models; }, ConfigHelper.CacheKeepTime, ConfigHelper.CacheRandomTime); } // 更新缓存 private void UpdateCache(long id) { if (id < 1) return; var entities = _dal.GetByID(id); var models = Entity2Model(entities); var all = GetCache(); var model = all.Find(x => x.ID == id); if (model == null) { all.Add(models); } else { model.Reset(models); } } // 更新缓存 private void UpdateCache(List ids) { if (ids == null || ids.Count() < 1) return; var entities = _dal.GetByIds(ids); var models = Entity2Models(entities); var all = GetCache(); all.RemoveAll(x => ids.Contains(x.ID)); if (models != null && models.Count > 0) { all.AddRange(models); } } // 移除缓存 private void RemoveCache(long id) { if (id < 1) return; var all = GetCache(); all.RemoveAll(x => x.ID == id); } // 移除缓存 private void RemoveCache(List ids) { if (ids == null || ids.Count() < 1) return; var all = GetCache(); all.RemoveAll(x => ids.Contains(x.ID)); } #endregion #region Get /// /// 查询全部 /// public List GetAll() { var all = GetCache(); return all.ToList(); } /// /// 根据 ID查询 /// public Model.PumpSpeedCurve GetByID(long id) { var all = GetAll(); return all.Find(x => x.ID == id); } /// /// 根据 Ids 查询 /// public List GetByIds(List ids) { if (ids == null || ids.Count() < 1) return default; var all = GetAll(); return all.Where(x => ids.Contains(x.ID)).ToList(); } /// /// 根据 PumpCurveID 获取 /// public List GetByPumpCurveID(long pumpCurveId) { if (pumpCurveId < 1) return null; var all = GetAll(); return all.Where(x => x.PumpCurveID == pumpCurveId).ToList(); } #endregion #region Insert /// /// 插入 /// public long Insert(Model.PumpSpeedCurve model) { if (model == null) return default; var entity = Model2Entity(model); var ID = _dal.Insert(entity); if (ID > 0) { UpdateCache(ID); } return ID; } /// /// 批量插入 /// public bool Inserts(List models) { if (models == null || models.Count() < 1) return default; var entities = Model2Entities(models.ToList()); var ids = _dal.InsertsR(entities); if (ids != null && ids.Count > 0) { UpdateCache(ids); return true; } return false; } #endregion #region Update /// /// 更新 /// public bool Update(Model.PumpSpeedCurve model) { if (model == null) return default; if (model.ID < 1) return default; var entity = Model2Entity(model); var bol = _dal.Update(entity); if (bol) { UpdateCache(model.ID); } return bol; } /// /// 批量更新 /// public bool Updates(List models) { if (models == null || models.Count() < 1) return default; if (models.ToList().Exists(x => x.ID < 1)) return default; var entities = Model2Entities(models.ToList()); var bol = _dal.Updates(entities); if (bol) { UpdateCache(models.Select(x => x.ID).ToList()); } return bol; } #endregion #region Exist /// /// 根据 ID 判断是否存在 /// public bool IsExistByID(long id) { var all = GetAll(); return all.Exists(x => x.ID == id); } /// /// 根据 PumpCurveID 判断是否存在 /// public bool IsExistByPumpCurveID(long pumpCurveId) { var all = GetAll(); return all.Exists(x => x.PumpCurveID == pumpCurveId); } #endregion #region Delete /// /// 通过ID删除 /// public bool DeleteByID(long id, out string msg) { msg = string.Empty; var bol = _dal.DeleteByID(id); if (bol) { RemoveCache(id); } return bol; } /// /// 通过PumpCurveID删除 /// public bool DeleteByPumpCurveID(long pumpCurveId, out string msg) { msg = string.Empty; var list = GetByPumpCurveID(pumpCurveId); if (list == null || !list.Any()) return default; var ids = list.Select(x => x.ID).ToList(); var bol = _dal.DeleteByIds(ids); if (bol) { RemoveCache(ids); } return bol; } #endregion #region Cover /// /// 批量覆盖 /// public bool Covers(List models) { if (models == null || models.Count() < 1) return default; var entities = Model2Entities(models); var bol = _dal.Covers(entities); if (bol) { CacheHelper.Remove(SettingsD.Project.ID); } return bol; } #endregion } }