using System.Collections.Generic;
using System.Linq;
namespace IStation.BLL
{
///
/// 信号
///
public partial class Signal
{
private readonly DAL.Signal _dal = new DAL.Signal();
#region Cache
//根据 查询缓存
private List GetCache()
{
return SignalCacheHelper.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.Signal 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();
}
///
/// 根据 monitorPointId 查询
///
public List GetByMonitorPointID(long monitorPointId)
{
var all = GetCache();
all = all.Where(x => monitorPointId == x.MonitorPointID).ToList();
return all?.OrderBy(x => x.SortCode).ToList();
}
///
/// 根据 monitorPointId 查询第一条
///
public Model.Signal GetFirstByMonitorPointID(long monitorPointId)
{
var all = GetCache();
var model = all.Where(x => x.MonitorPointID == monitorPointId).OrderBy(x => x.SortCode).FirstOrDefault();
return model;
}
///
/// 根据 monitorPointIds 查询
///
public List GetByMonitorPointIds(IEnumerable monitorPointIds)
{
if (monitorPointIds == null || monitorPointIds.Count() < 1)
return default;
var all = GetCache();
all = all?.Where(x => monitorPointIds.Contains(x.MonitorPointID)).ToList();
return all?.OrderBy(x => x.SortCode).ToList();
}
///
/// 根据 monitorPointId 查询 信号+信号类型
///
public List GetExSignalTypeByMonitorPointID(long monitorPointId)
{
var list = GetByMonitorPointID(monitorPointId);
if (list == null || list.Count < 1)
return default;
var ids = list.Select(x => x.SignalTypeID).Distinct().ToList();
var typeList = new SignalType().GetByIds(ids);
if (typeList == null || typeList.Count < 1)
return default;
var vmList = (from x in list
join y in typeList
on x.SignalTypeID equals y.ID
select new Model.Signal_SignalType(x, y))
.ToList();
return vmList;
}
///
/// 根据 monitorPointId 查询第一条 信号+信号类型
///
public Model.Signal_SignalType GetFirstExSignalTypeByMonitorPointID(long monitorPointId)
{
var first = GetFirstByMonitorPointID(monitorPointId);
if (first == null)
return default;
var signalType = new SignalType().GetByID(first.SignalTypeID);
if (signalType == null)
return default;
return new Model.Signal_SignalType(first, signalType);
}
///
/// 根据 MonitorPointIds 查询 信号+信号类型
///
public List GetExSignalTypeByMonitorPointIds(IEnumerable monitorPointIds)
{
if (monitorPointIds == null || monitorPointIds.Count() < 1)
return default;
var list = GetByMonitorPointIds(monitorPointIds);
if (list == null || list.Count < 1)
return default;
var ids = list.Select(x => x.SignalTypeID).Distinct().ToList();
var typeList = new SignalType().GetByIds(ids);
if (typeList == null || typeList.Count < 1)
return default;
var vmList = (from x in list
join y in typeList
on x.SignalTypeID equals y.ID
select new Model.Signal_SignalType(x, y))
.ToList();
return vmList;
}
#endregion
#region Insert
///
/// 插入
///
public long Insert(Model.Signal 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.Signal 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)
{
SignalCacheHelper.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)
{
SignalCacheHelper.Remove();
}
return models;
}
#endregion
}
}