using System.Collections.Generic;
|
using System.Linq;
|
|
namespace IStation.BLL
|
{
|
/// <summary>
|
/// 测点映射
|
/// </summary>
|
public partial class MonitorPointMapping
|
{
|
private readonly DAL.MonitorPointMapping _dal = new DAL.MonitorPointMapping();
|
|
#region Cache
|
|
//根据 查询缓存
|
private List<Model.MonitorPointMapping> GetCache()
|
{
|
return MonitorPointMappingCacheHelper.GetSet(() =>
|
{
|
var entities = _dal.GetAll();
|
var models = Entity2Models(entities);
|
if (models == null)
|
{
|
models = new List<Model.MonitorPointMapping>();
|
}
|
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<long> 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
|
|
|
/// <summary>
|
/// 查询全部
|
/// </summary>
|
public List<Model.MonitorPointMapping> GetAll()
|
{
|
var all = GetCache();
|
return all.ToList();
|
}
|
|
/// <summary>
|
/// 根据 ID查询
|
/// </summary>
|
public Model.MonitorPointMapping GetByID(long ID)
|
{
|
var all = GetAll();
|
return all.Find(x => x.ID == ID);
|
}
|
|
/// <summary>
|
/// 根据 Ids 查询
|
/// </summary>
|
public List<Model.MonitorPointMapping> GetByIds(IEnumerable<long> Ids)
|
{
|
if (Ids == null || Ids.Count() < 1)
|
return default;
|
var all = GetAll();
|
return all.Where(x => Ids.Contains(x.ID)).ToList();
|
}
|
|
/// <summary>
|
/// 根据 Ids 查询
|
/// </summary>
|
public List<Model.MonitorPointMapping> GetByMappingTypeAndMappingID(string mappingType, long mappingId)
|
{
|
var all = GetAll();
|
return all.Where(x => x.MappingType == mappingType && x.MappingID == mappingId).ToList();
|
}
|
#endregion
|
|
#region Insert
|
|
/// <summary>
|
/// 插入
|
/// </summary>
|
public long Insert(Model.MonitorPointMapping model)
|
{
|
if (model == null)
|
return default;
|
var entities = Model2Entity(model);
|
var ID = _dal.Insert(entities);
|
if (ID > 0)
|
{
|
UpdateProjectCache(ID);
|
}
|
return ID;
|
}
|
|
/// <summary>
|
/// 批量插入
|
/// </summary>
|
public bool Inserts(IEnumerable<Model.MonitorPointMapping> 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
|
|
/// <summary>
|
/// 更新
|
/// </summary>
|
public bool Update(Model.MonitorPointMapping 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;
|
}
|
|
/// <summary>
|
/// 批量更新
|
/// </summary>
|
public bool Updates(List<Model.MonitorPointMapping> 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
|
|
/// <summary>
|
/// 根据 ID 判断是否存在
|
/// </summary>
|
public bool IsExistByID(long ID)
|
{
|
var all = GetAll();
|
return all.Exists(x => x.ID == ID);
|
}
|
#endregion
|
|
#region Delete
|
|
/// <summary>
|
/// 根据 ID 删除
|
/// </summary>
|
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
|
|
/// <summary>
|
/// 批量覆盖
|
/// </summary>
|
public bool Covers(IEnumerable<Model.MonitorPointMapping> list)
|
{
|
if (list == null || list.Count() < 1)
|
return default;
|
var entities = Model2Entities(list.ToList());
|
var bol = _dal.Covers(entities);
|
if (bol)
|
{
|
MonitorPointCacheHelper.Remove();
|
}
|
return bol;
|
}
|
|
/// <summary>
|
/// 批量覆盖并返回
|
/// </summary>
|
public List<Entity.MonitorPointMapping> CoversR(IEnumerable<Model.MonitorPointMapping> 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)
|
{
|
MonitorPointCacheHelper.Remove();
|
}
|
return models;
|
}
|
#endregion
|
|
|
}
|
}
|