using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace IStation.BLL { /// /// 泵曲线映射关系 /// public partial class PumpCurveMapping { private readonly IDAL.IPumpCurveMapping _dal = DAL.Factory.DataAccessBase.CreateDAL>(Settings.DataAccess.ProductDLLName, Settings.DataAccess.DALNameSpace); #region Cache //通过 projectId 获取缓存 private List GetProjectCache(long projectId) { return PumpCurveMappingCacheHelper.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.PumpCurveMapping 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.PumpCurveMapping 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.PumpCurveMapping model) { if (projectId < 0) 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 < 0) 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); } #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 } }