using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using IStation.Untity; namespace IStation.Service { /// /// ¿Í»§ /// public partial class Corpration { #region Cache //»ñÈ¡»º´æ private List GetCache() { var all = CorprationCacheHelper.GetSet(() => { var dal = new DAL.Corpration(); var entity_list = dal.GetAll(); var model_list = Entity2Models(entity_list); if (model_list == null) { model_list = new List(); } return model_list; }, ConfigHelper.CacheKeepTime, ConfigHelper.CacheRandomTime); return all; } //ͨ¹ý ID ¸üлº´æ private void UpdateCache(long ID) { var dal = new DAL.Corpration(); 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 UpdateCache(List Ids) { if (Ids == null || Ids.Count() < 1) return; var dal = new DAL.Corpration(); var entity_list = dal.GetByIds(Ids); var model_list = Entity2Models(entity_list); var all = GetCache(); all.RemoveAll(x => Ids.Contains(x.ID)); if (model_list != null && model_list.Count > 0) { all.AddRange(model_list); } } #endregion #region Query /// /// »ñÈ¡ËùÓÐ /// public List GetAll() { var all = GetCache(); return all.OrderBy(x => x.ParentIds.Count).ThenBy(x => x.SortCode).ToList(); } /// /// ͨ¹ý ID »ñÈ¡ /// public Model.Corpration 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.ParentIds.Count).ThenBy(x => x.SortCode).ToList(); } /// /// ͨ¹ý ID »ñÈ¡×ÓÏî¼°×ÔÉí /// public List GetChildAndSelfByID(long ID) { var all = GetAll(); return all.Where(x => x.ID == ID || x.ParentIds.Contains(ID)).OrderBy(x => x.ParentIds.Count).ThenBy(x => x.SortCode).ToList(); } /// /// ͨ¹ý ID »ñÈ¡×ÓÏî /// public List GetChildrenByID(long ID) { var all = GetAll(); return all.Where(x => x.ParentIds.Contains(ID)).OrderBy(x => x.ParentIds.Count).ThenBy(x => x.SortCode).ToList(); } /// /// ͨ¹ý ID »ñÈ¡×ÓÏî¼°×ÔÉíµÄ IdÁбí /// public List GetChildAndSelfIdsByID(long ID) { var all = GetAll(); var list = all.Where(x => x.ID == ID || x.ParentIds.Contains(ID)).ToList(); return list.Select(x => x.ID).ToList(); } /// /// ͨ¹ý Ids »ñÈ¡×ÓÏî¼°×ÔÉíµÄ IdÁбí /// public List GetChildAndSelfIdsByIds(List Ids) { if (Ids == null || Ids.Count() < 1) return default; var all = GetAll(); var list = Ids.SelectMany(x => all.Where(t => t.ID == x || t.ParentIds.Contains(x))).Select(t => t.ID).Distinct().ToList(); return list; } /// /// ͨ¹ý ID »ñÈ¡×ÓÏîµÄ IdÁбí /// public List GetChildrenIdsByID(long ID) { var all = GetAll(); var list = all.Where(x => x.ParentIds.Contains(ID)).ToList(); return list.Select(x => x.ID).ToList(); } #endregion #region Insert /// /// Ìí¼ÓÒ»Ìõ /// public long Insert(Model.Corpration model) { if (model == null) return default; var entity = Model2Entity(model); var dal = new DAL.Corpration(); var id = dal.Insert(entity); if (id > 0) { UpdateCache(id); } return id; } /// /// ÅúÁ¿²åÈë /// public bool Inserts(List list) { if (list == null || list.Count() < 1) return default; var dal = new DAL.Corpration(); 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.Corpration model) { if (model == null) return default; var entity = Model2Entity(model); var dal = new DAL.Corpration(); var bol = dal.Update(entity); if (bol) { UpdateCache(model.ID); } return bol; } /// /// ÅúÁ¿¸üР/// public bool Updates(List list) { if (list == null || list.Count() < 1) return default; var entity_list = Model2Entities(list.ToList()); var dal = new DAL.Corpration(); var bol = dal.Updates(entity_list); if (bol) { UpdateCache(list.Select(x => x.ID).ToList()); } return bol; } /// /// ¸üÐÂÅÅÐòÂë /// public bool UpdateSortCode(long ID, int SortCode, long UpdateUserID, DateTime UpdateTime) { if (ID < 1) return default; var dal = new DAL.Corpration(); var bol = dal.UpdateSortCode(ID, SortCode, UpdateUserID, UpdateTime); if (bol) { UpdateCache(ID); } return bol; } /// /// ¸üÐÂÅÅÐò /// public bool UpdateSorter(List sorters) { if (sorters == null || sorters.Count() < 1) return default; var dal = new DAL.Corpration(); var bol = dal.UpdateSorter(sorters.ToEntityList()); if (bol) { UpdateCache(sorters.Select(x => x.ID).ToList()); } return bol; } /// /// ¸üÐÂÊ÷ÅÅÐòÂë /// public bool UpdateTreeSortCode(long ID, List ParentIds, int SortCode, long UpdateUserID, DateTime UpdateTime) { if (ID < 1) return default; var dal = new DAL.Corpration(); var bol = dal.UpdateTreeSortCode(ID, TreeParentIdsHelper.ToString(ParentIds), SortCode, UpdateUserID, UpdateTime); if (bol) { UpdateCache(ID); } return bol; } /// /// ¸üÐÂÊ÷ÅÅÐò /// public bool UpdateTreeSorter(List sorters) { if (sorters == null || sorters.Count() < 1) return default; var dal = new DAL.Corpration(); var bol = dal.UpdateTreeSorter(sorters.ToEntityList()); if (bol) { UpdateCache(sorters.Select(x => x.ID).ToList()); } return bol; } /// /// ¸üÐÂʹÓÃ״̬ /// public bool UpdateUseStatus(long ID, Model.eUseStatus UseStatus, long UpdateUserID, DateTime UpdateTime) { if (ID < 1) return default; var dal = new DAL.Corpration(); var bol = dal.UpdateUseStatus(ID, (int)UseStatus, UpdateUserID, UpdateTime); if (bol) { UpdateCache(ID); } return bol; } /// /// ¸üРTagName /// public bool UpdateTagName(long ID, string TagName, long UpdateUserID, DateTime UpdateTime) { var dal = new DAL.Corpration(); var bol = dal.UpdateTagName(ID, TagName, UpdateUserID, UpdateTime); if (bol) { UpdateCache(ID); } return bol; } /// /// ¸üРTerminalId /// public bool UpdateTerminalId(long ID, string TerminalId,long UpdateUserID,DateTime UpdateTime) { var dal = new DAL.Corpration(); var bol = dal.UpdateTerminalId(ID, TerminalId, UpdateUserID,UpdateTime); 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 } }