using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace IStation.BLL
|
{
|
/// <summary>
|
/// 信号
|
/// </summary>
|
public partial class Signal
|
{
|
private readonly IDAL.ISignal<Entity.Signal> _dal
|
= DAL.Factory.DataAccess.CreateDAL<IDAL.ISignal<Entity.Signal>>(Settings.DataAccess.MonitorDLLName, Settings.DataAccess.DALNameSpace);
|
|
#region Cache
|
|
//根据 projectId 查询缓存
|
private List<Model.Signal> GetProjectCache(long projectId)
|
{
|
return SignalCacheHelper.GetSet(projectId, () =>
|
{
|
var entities = _dal.GetAll(projectId);
|
var models = Entity2Models(entities);
|
if (models == null)
|
{
|
models = new List<Model.Signal>();
|
}
|
return models;
|
}, ConfigHelper.CacheKeepTime, ConfigHelper.CacheRandomTime);
|
}
|
|
//根据 Id 更新缓存
|
private void UpdateProjectCache(long projectId, long Id)
|
{
|
var entity_ds = _dal.GetById(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<long> Ids)
|
{
|
if (Ids == null || Ids.Count() < 1)
|
return;
|
var entities = _dal.GetByIds(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 Get
|
|
/// <summary>
|
/// 查询全部
|
/// </summary>
|
public List<Model.Signal> GetAll(long projectId)
|
{
|
var all = GetProjectCache(projectId);
|
return all.OrderBy(x => x.SortCode).ToList();
|
}
|
|
/// <summary>
|
/// 根据 Id查询
|
/// </summary>
|
public Model.Signal GetById(long projectId, long Id)
|
{
|
var all = GetAll(projectId);
|
return all.Find(x => x.Id == Id);
|
}
|
|
/// <summary>
|
/// 根据 Ids 查询
|
/// </summary>
|
public List<Model.Signal> GetByIds(long projectId, IEnumerable<long> Ids)
|
{
|
if (Ids == null || Ids.Count() < 1)
|
return default;
|
var all = GetAll(projectId);
|
return all.Where(x => Ids.Contains(x.Id)).ToList();
|
}
|
|
/// <summary>
|
/// 根据 monitorPointId 查询
|
/// </summary>
|
public List<Model.Signal> GetByMonitorPointId(long projectId, long monitorPointId)
|
{
|
var all = GetProjectCache(projectId);
|
all = all.Where(x => monitorPointId == x.MonitorPointId).ToList();
|
return all?.OrderBy(x => x.SortCode).ToList();
|
}
|
|
/// <summary>
|
/// 根据 monitorPointId 查询第一条
|
/// </summary>
|
public Model.Signal GetFirstByMonitorPointId(long projectId, long monitorPointId)
|
{
|
var all = GetProjectCache(projectId);
|
var model = all.Where(x => x.MonitorPointId == monitorPointId).OrderBy(x => x.SortCode).FirstOrDefault();
|
return model;
|
}
|
|
/// <summary>
|
/// 根据 monitorPointIds 查询
|
/// </summary>
|
public List<Model.Signal> GetByMonitorPointIds(long projectId, IEnumerable<long> monitorPointIds)
|
{
|
if (monitorPointIds == null || monitorPointIds.Count() < 1)
|
return default;
|
var all = GetProjectCache(projectId);
|
all = all?.Where(x => monitorPointIds.Contains(x.MonitorPointId)).ToList();
|
return all?.OrderBy(x => x.SortCode).ToList();
|
}
|
|
/// <summary>
|
/// 根据 monitorPointId 查询 信号+信号类型
|
/// </summary>
|
public List<Model.Signal_SignalType> GetExSignalTypeByMonitorPointId(long projectId, long monitorPointId)
|
{
|
var list = GetByMonitorPointId(projectId, monitorPointId);
|
if (list == null || list.Count < 1)
|
return default;
|
var identifiers = list.Select(x => x.SignalType).Distinct().ToList();
|
var typeList = new SignalType().GetByIdentifiers(projectId,identifiers);
|
if (typeList == null || typeList.Count < 1)
|
return default;
|
var vmList = (from x in list
|
join y in typeList
|
on x.SignalType equals y.Identifier
|
select new Model.Signal_SignalType(x, y))
|
.ToList();
|
return vmList;
|
}
|
|
/// <summary>
|
/// 根据 monitorPointId 查询第一条 信号+信号类型
|
/// </summary>
|
public Model.Signal_SignalType GetFirstExSignalTypeByMonitorPointId(long projectId, long monitorPointId)
|
{
|
var first = GetFirstByMonitorPointId(projectId, monitorPointId);
|
if (first == null)
|
return default;
|
var signalType = new SignalType().GetByIdentifier(projectId,first.SignalType);
|
if (signalType == null)
|
return default;
|
return new Model.Signal_SignalType(first, signalType);
|
}
|
|
/// <summary>
|
/// 根据 MonitorPointIds 查询 信号+信号类型
|
/// </summary>
|
public List<Model.Signal_SignalType> GetExSignalTypeByMonitorPointIds(long projectId, IEnumerable<long> monitorPointIds)
|
{
|
if (monitorPointIds == null || monitorPointIds.Count() < 1)
|
return default;
|
var list = GetByMonitorPointIds(projectId, monitorPointIds);
|
if (list == null || list.Count < 1)
|
return default;
|
var identifiers = list.Select(x => x.SignalType).Distinct().ToList();
|
var typeList = new SignalType().GetByIdentifiers(projectId, identifiers);
|
if (typeList == null || typeList.Count < 1)
|
return default;
|
var vmList = (from x in list
|
join y in typeList
|
on x.SignalType equals y.Identifier
|
select new Model.Signal_SignalType(x, y))
|
.ToList();
|
return vmList;
|
}
|
#endregion
|
|
#region Insert
|
|
/// <summary>
|
/// 插入
|
/// </summary>
|
public long Insert(long projectId, Model.Signal 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;
|
}
|
|
/// <summary>
|
/// 批量插入
|
/// </summary>
|
public bool Inserts(long projectId, IEnumerable<Model.Signal> 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
|
|
/// <summary>
|
/// 更新
|
/// </summary>
|
public bool Update(long projectId, Model.Signal 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;
|
}
|
|
/// <summary>
|
/// 批量更新
|
/// </summary>
|
public bool Updates(long projectId, List<Model.Signal> 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
|
|
/// <summary>
|
/// 根据 Id 判断是否存在
|
/// </summary>
|
public bool IsExistById(long projectId, long Id)
|
{
|
var all = GetAll(projectId);
|
return all.Exists(x => x.Id == Id);
|
}
|
#endregion
|
|
#region Delete
|
|
/// <summary>
|
/// 根据 Id 删除
|
/// </summary>
|
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
|
|
/// <summary>
|
/// 批量覆盖
|
/// </summary>
|
public bool Covers(long projectId, IEnumerable<Model.Signal> 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)
|
{
|
SignalCacheHelper.Remove(projectId);
|
}
|
return bol;
|
}
|
|
/// <summary>
|
/// 批量覆盖并返回
|
/// </summary>
|
public List<Entity.Signal> CoversR(long projectId, IEnumerable<Model.Signal> 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)
|
{
|
SignalCacheHelper.Remove(projectId);
|
}
|
return models;
|
}
|
|
#endregion
|
}
|
}
|