using System.Collections.Generic; using System.Linq; namespace IStation.BLL { /// /// ÐźÅÀàÐÍ /// public partial class SignalType { private readonly DAL.SignalType _dal = new DAL.SignalType(); #region Cache //¸ù¾Ý ²éѯ»º´æ private List GetCache() { return SignalTypeCacheHelper.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.OrderBy(x => x.SortCode).ToList(); } /// /// ¸ù¾Ý ID²éѯ /// public Model.SignalType 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(); } /// /// ¸ù¾Ý groupId ²éѯ /// public List GetByGroupID(long groupId) { var all = GetAll(); all = all.Where(x => x.GroupID == groupId).ToList(); return all?.OrderBy(x => x.SortCode).ToList(); } /// /// ¸ù¾Ý identifier ²éѯ /// public Model.SignalType GetByIdentifier(string identifier) { var all = GetAll(); var model = all.Find(x => x.Identifier == identifier); return model; } /// /// ¸ù¾Ý identifiers ²éѯ /// public List GetByIdentifiers(IEnumerable identifiers) { if (identifiers == null || identifiers.Count() < 1) return default; var all = GetAll(); all = all.Where(x => identifiers.Contains(x.Identifier)).ToList(); return all?.OrderBy(x => x.SortCode).ToList(); } #endregion #region Insert /// /// ²åÈë /// public long Insert(Model.SignalType 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.SignalType 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) { SignalTypeCacheHelper.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) { SignalTypeCacheHelper.Remove(); } return models; } #endregion } }