using IStation.Untity; using System.Collections.Generic; using System.Linq; namespace IStation.BLL { /// /// 泵站 /// public partial class Station { private readonly DAL.Station _dal = new DAL.Station(); #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.Station 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(); } #endregion #region Insert /// /// 插入 /// public long Insert(Model.Station 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.Station 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; } /// /// 更新排序码 /// public bool UpdateSortCode(long id, int sortCode) { if (id < 1) return false; var bol = _dal.UpdateSortCode(id, sortCode); if (bol) { UpdateCache(id); } return bol; } /// /// 更新排序 /// public virtual bool UpdateSorter(List sorters) { if (sorters == null || sorters.Count() < 1) return default; if (sorters.Exists(x => x.ID < 1)) return default; var entities = sorters.Model2Entities(); var bol = _dal.UpdateSorter(entities); if (bol) { var ids = entities.Select(x => x.ID).ToList(); UpdateCache(ids); } return bol; } /// /// 更新 Flags /// public virtual bool UpdateFlags(long id, List flags) { if (id < 1) return false; var bol = _dal.UpdateFlags(id, FlagsHelper.ToString(flags)); if (bol) { UpdateCache(id); } return bol; } /// /// 更新 TagName /// public bool UpdateTagName(long id, string tagName) { if (id < 1) return false; var bol = _dal.UpdateTagName(id, tagName); if (bol) { UpdateCache(id); } return bol; } #endregion #region Exist /// /// 根据 ID 判断是否存在 /// public bool IsExistByID(long id) { var all = GetAll(); return all.Exists(x => x.ID == id); } /// /// 判断TagName是否存在 /// public bool IsExistTagName(string tagName) { if (string.IsNullOrEmpty(tagName)) return default; var all = GetAll(); if (all == null || all.Count < 1) return default; return all.Exists(x => !string.IsNullOrEmpty(x.TagName) && x.TagName.ToUpper() == tagName.ToUpper()); } /// /// 判断TagName是否存在 ExceptID /// public bool IsExistTagNameExceptID(string tagName, long exceptID) { if (string.IsNullOrEmpty(tagName)) return default; var all = GetAll(); if (all == null || all.Count < 1) return default; return all.Exists(x => !string.IsNullOrEmpty(x.TagName) && x.TagName.ToUpper() == tagName.ToUpper() && x.ID != exceptID); } #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; } #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 } }