using System.Collections.Generic; using System.Linq; namespace IStation.BLL { /// /// 测点映射 /// public partial class MonitorPointMapping { private readonly DAL.MonitorPointMapping _dal = new DAL.MonitorPointMapping(); #region Cache //根据 查询缓存 private List GetCache() { return MonitorPointMappingCacheHelper.GetSet(() => { var entities = _dal.GetAll(); var models = Entity2Models(entities); if (models == null) { models = new List(); } return models; }, ConfigHelper.CacheKeepTime, ConfigHelper.CacheRandomTime); } //根据 ID 更新缓存 private void UpdateProjectCache(long ID) { var entity_ds = _dal.GetByID(ID); var model_ds = Entity2Model(entity_ds); var all = GetCache(); var model = all.Find(x => x.ID == ID); if (model == null) { all.Add(model_ds); } else { model.Reset(model_ds); } } //根据 Ids 更新缓存 private void UpdateProjectCache(IEnumerable 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); } } //根据 ID 移除缓存 private void RemoveProjectCache(long ID) { var all = GetCache(); all.RemoveAll(x => x.ID == ID); } #endregion #region Get /// /// 查询全部 /// public List GetAll() { var all = GetCache(); return all.ToList(); } /// /// 根据 ID查询 /// public Model.MonitorPointMapping GetByID(long ID) { var all = GetAll(); return all.Find(x => x.ID == ID); } /// /// 根据 Ids 查询 /// public List GetByIds(IEnumerable Ids) { if (Ids == null || Ids.Count() < 1) return default; var all = GetAll(); return all.Where(x => Ids.Contains(x.ID)).ToList(); } /// /// 根据 Ids 查询 /// public List GetByMappingTypeAndMappingID(string mappingType, long mappingId) { var all = GetAll(); return all.Where(x => x.MappingType == mappingType && x.MappingID == mappingId).ToList(); } #endregion #region Insert /// /// 插入 /// public long Insert(Model.MonitorPointMapping model) { if (model == null) return default; var entities = Model2Entity(model); var ID = _dal.Insert(entities); if (ID > 0) { UpdateProjectCache(ID); } return ID; } /// /// 批量插入 /// public bool Inserts(IEnumerable list) { if (list == null || list.Count() < 1) return default; var entities = Model2Entities(list.ToList()); var Ids = _dal.InsertsR(entities); if (Ids != null && Ids.Count > 0) { UpdateProjectCache(Ids); return true; } return false; } #endregion #region Update /// /// 更新 /// public bool Update(Model.MonitorPointMapping model) { if (model == null) return default; if (model.ID < 1) return default; var entities = Model2Entity(model); var bol = _dal.Update(entities); if (bol) { UpdateProjectCache(model.ID); } return bol; } /// /// 批量更新 /// public bool Updates(List list) { if (list == null || list.Count() < 1) return default; if (list.ToList().Exists(x => x.ID < 1)) return default; var entities = Model2Entities(list.ToList()); var bol = _dal.Updates(entities); if (bol) { UpdateProjectCache(list.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); } #endregion #region Delete /// /// 根据 ID 删除 /// public bool DeleteByID(long ID, out string msg) { msg = string.Empty; var bol = _dal.DeleteByID(ID); if (bol) { RemoveProjectCache(ID); } return bol; } #endregion #region Cover /// /// 批量覆盖 /// public bool Covers(IEnumerable list) { if (list == null || list.Count() < 1) return default; var entities = Model2Entities(list.ToList()); var bol = _dal.Covers(entities); if (bol) { MonitorPointCacheHelper.Remove(); } return bol; } /// /// 批量覆盖并返回 /// public List CoversR(IEnumerable list) { if (list == null || list.Count() < 1) return default; var entities = Model2Entities(list.ToList()); var models = _dal.CoversR(entities); if (models != null && models.Count > 0) { MonitorPointCacheHelper.Remove(); } return models; } #endregion } }