using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace IStation.BLL { /// /// 业务 /// public partial class LogicArea { private readonly IDAL.ILogicArea _dal = DAL.Factory.DataAccess.CreateDAL>(Settings.DataAccess.LogicDLLName, Settings.DataAccess.DALNameSpace); #region Cache //根据 projectId 查询缓存 private List GetProjectCache(long projectId) { return LogicAreaCacheHelper.GetSet(projectId, () => { var entities = _dal.QueryAll(projectId); var models = Entity2Models(entities); if (models == null) { models = new List(); } return models; }, ConfigHelper.CacheKeepTime, ConfigHelper.CacheRandomTime); } //根据 Id 更新缓存 private void UpdateProjectCache(long projectId, long Id) { var entity_ds = _dal.QueryById(projectId, Id); var model_ds = Entity2Model(entity_ds); var all = GetProjectCache(projectId); var model = all.Find(x => x.Id == Id); if (model == null) { all.Add(model_ds); } else { model.Reset(model_ds); } } //根据 Ids 更新缓存 private void UpdateProjectCache(long projectId, IEnumerable Ids) { if (Ids == null || Ids.Count() < 1) return; var entities = _dal.QueryByIds(projectId, Ids); var models = Entity2Models(entities); var all = GetProjectCache(projectId); all.RemoveAll(x => Ids.Contains(x.Id)); if (models != null && models.Count > 0) { all.AddRange(models); } } //根据 Id 移除缓存 private void RemoveProjectCache(long projectId, long Id) { var all = GetProjectCache(projectId); all.RemoveAll(x => x.Id == Id); } #endregion #region Query /// /// 查询全部 /// public List QueryAll(long projectId) { var all = GetProjectCache(projectId); return all.OrderBy(x => x.SortCode).ToList(); } /// /// 根据 Id查询 /// public Model.LogicArea QueryById(long projectId, long Id) { var all = QueryAll(projectId); return all.Find(x => x.Id == Id); } /// /// 根据 Ids 查询 /// public List QueryByIds(long projectId, IEnumerable Ids) { if (Ids == null || Ids.Count() < 1) return default; var all = QueryAll(projectId); return all.Where(x => Ids.Contains(x.Id)).ToList(); } #endregion #region Insert /// /// 插入 /// public long Insert(long projectId, Model.LogicArea model) { if (projectId < 1) return default; if (model == null) return default; var entities = Model2Entity(model); var Id = _dal.Insert(projectId, entities); if (Id > 0) { UpdateProjectCache(projectId, Id); } return Id; } /// /// 批量插入 /// public bool Inserts(long projectId, IEnumerable list) { if (projectId < 1) return default; if (list == null || list.Count() < 1) return default; var entities = Model2Entities(list.ToList()); var Ids = _dal.InsertsR(projectId, entities); if (Ids != null && Ids.Count > 0) { UpdateProjectCache(projectId, Ids); return true; } return false; } #endregion #region Update /// /// 更新 /// public bool Update(long projectId, Model.LogicArea model) { if (projectId < 1) return default; if (model == null) return default; if (model.Id < 1) return default; var entities = Model2Entity(model); var bol = _dal.Update(projectId, entities); if (bol) { UpdateProjectCache(projectId, model.Id); } return bol; } /// /// 批量更新 /// public bool Updates(long projectId, List list) { if (projectId < 1) return default; 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(projectId, entities); if (bol) { UpdateProjectCache(projectId, list.Select(x => x.Id).ToList()); } return bol; } #endregion #region Exist /// /// 根据 Id 判断是否存在 /// public bool IsExistById(long projectId, long Id) { var all = QueryAll(projectId); return all.Exists(x => x.Id == Id); } /// /// 判断TagName是否存在 /// public bool IsExistTagName(long projectId, string TagName) { if (string.IsNullOrEmpty(TagName)) return default; var all = QueryAll(projectId); 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(long projectId, string TagName, long ExceptID) { if (string.IsNullOrEmpty(TagName)) return default; var all = QueryAll(projectId); 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 projectId, long Id, out string msg) { msg = string.Empty; var bol = _dal.DeleteById(projectId, Id); if (bol) { RemoveProjectCache(projectId, Id); } return bol; } #endregion #region Cover /// /// 批量覆盖 /// public bool Covers(long projectId, IEnumerable list) { if (projectId < 1) return default; if (list == null || list.Count() < 1) return default; var entities = Model2Entities(list.ToList()); var bol = _dal.Covers(projectId, entities); if (bol) { LogicAreaCacheHelper.Remove(projectId); } return bol; } /// /// 批量覆盖并返回 /// public List CoversR(long projectId, IEnumerable list) { if (projectId < 1) return default; if (list == null || list.Count() < 1) return default; var entities = Model2Entities(list.ToList()); var models = _dal.CoversR(projectId, entities); if (models != null && models.Count > 0) { LogicAreaCacheHelper.Remove(projectId); } return models; } #endregion } }