using System; using System.Collections.Generic; using System.Linq; namespace TProduct.BLL { public partial class WorkBenchMonitorPoint { private readonly DAL.WorkBenchMonitorPoint _dal = new DAL.WorkBenchMonitorPoint(); static List _allWorkBenchMonitorPoint = null; internal static void ClearChache() { _allWorkBenchMonitorPoint = null; } #region Query /// /// 获取所有 /// public List GetAll() { if (_allWorkBenchMonitorPoint != null) return _allWorkBenchMonitorPoint; var all = _dal.GetAll()?.OrderBy(x => x.SortCode)?.ToList(); _allWorkBenchMonitorPoint = Entity2Models(all); if (_allWorkBenchMonitorPoint == null) _allWorkBenchMonitorPoint = new List(); return _allWorkBenchMonitorPoint; } /// /// 通过 ID 获取 /// public Model.WorkBenchMonitorPoint GetByID(long ID) { if (ID < 1) return default; return GetAll().Find(x => x.ID == ID); } /// /// 通过 ID 获取 /// public List GetByBenchID(long BenchID) { if (BenchID < 1) return default; var allMap = new WorkBenchMonitorPointMap().GetByBenchID(BenchID); if(allMap == null || allMap.Count() == 0) return default; var ids = allMap.Select(x=>x.MonitorPointID).ToList(); return GetAll().Where(x => ids.Contains(x.ID))?.ToList(); } /// /// 通过 ID 获取 /// public List Get模拟量ByBenchID(long BenchID) { if (BenchID < 1) return default; return GetByBenchID(BenchID).Where(x=> x.SourceType == Model.eMonitorPointSourceType.模拟量).ToList(); } /// /// 通过 ID 获取 /// public List Get数字量ByBenchID(long BenchID) { if (BenchID < 1) return default; return GetByBenchID(BenchID).Where(x => x.SourceType == Model.eMonitorPointSourceType.数字量).ToList(); } /// /// 通过 ID 获取 /// public List GetByIds(List Ids) { if (Ids == null || Ids.Count() < 1) return default; return GetAll().Where(x => Ids.Contains(x.ID)).ToList(); } /// /// 获取最大排序码 /// public int GetMaxSortCode( ) { var items = GetAll() ; if (items == null || items.Count() == 0) return 1; return items.Select(x => x.SortCode).Max(); } #endregion #region Insert /// /// 添加一条 /// public long Insert(Model.WorkBenchMonitorPoint model) { _allWorkBenchMonitorPoint = null; if (model == null) return default; model.CreateTime = DateTime.Now; model.UpdateTime = DateTime.Now; if (string.IsNullOrEmpty(model.Code)) model.Code = CreateNO(); var entity = Model2Entity(model); var id = _dal.Insert(entity); return id; } /// /// 批量插入 /// public bool Inserts(List list) { _allWorkBenchMonitorPoint = null; if (list == null || list.Count() < 1) return default; var codes = new BLL.WorkBenchMonitorPoint().CreateNO(list.Count()); for (int i = 0; i < list.Count; i++) { var item = list[i]; item.CreateTime = DateTime.Now; item.UpdateTime = DateTime.Now; if (string.IsNullOrEmpty(item.Code)) item.Code = codes[i]; } var entity_list = Model2Entities(list); var ids = _dal.InsertsR(entity_list); if (ids != null && ids.Count > 0) return true; return false; } /// /// 批量插入并返回标记 /// public List InsertsR(List list) { _allWorkBenchMonitorPoint = null; if (list == null || list.Count() < 1) return default; foreach (var item in list) { item.CreateTime = DateTime.Now; item.UpdateTime = DateTime.Now; if (string.IsNullOrEmpty(item.Code)) item.Code = CreateNO(); } var entity_list = Model2Entities(list); return _dal.InsertsR(entity_list); } #endregion #region Update /// /// 更新一条 /// public bool Update(Model.WorkBenchMonitorPoint model) { _allWorkBenchMonitorPoint = null; if (model == null) return default; model.UpdateTime = DateTime.Now; var entity = Model2Entity(model); var bol = _dal.Update(entity); return bol; } /// /// 批量更新 /// public bool Updates(List list) { _allWorkBenchMonitorPoint = null; if (list == null || list.Count() < 1) return default; foreach (var item in list) { item.UpdateTime = DateTime.Now; } var entity_list = Model2Entities(list); var bol = _dal.Updates(entity_list); return bol; } #endregion #region Delete /// /// 通过 ID 删除 /// public bool DeleteByID(long ID) { _allWorkBenchMonitorPoint = null; return _dal.DeleteByID(ID); } /// /// 通过 Ids 批量删除 /// public bool DeleteByIds(List Ids) { _allWorkBenchMonitorPoint = null; return _dal.DeleteByIds(Ids); } #endregion #region Exist /// ///是否存在Code /// public bool IsExistCode(string Code) { if (string.IsNullOrEmpty(Code)) return false; var find = GetAll().Find(x => x.Code.ToLower() == Code.ToLower()); if (find == null) return false; else return true; } /// ///是否存在Name /// public bool IsExistName( string Name) { if (string.IsNullOrEmpty(Name) ) return false; var find = GetAll().Find(x => x.Name.ToLower() == Name.ToLower()); if (find == null) return false; else return true; } #endregion } }