using System.Collections.Generic;
|
using System.Linq;
|
|
namespace IStation.BLL
|
{
|
/// <summary>
|
/// 泵曲线
|
/// </summary>
|
public partial class PumpCurve
|
{
|
private readonly DAL.PumpCurve _dal = new DAL.PumpCurve();
|
|
#region Cache
|
|
// 获取缓存
|
private List<Model.PumpCurve> GetCache()
|
{
|
return CacheHelper<Model.PumpCurve>.GetSet(Settings.Project.ID, () =>
|
{
|
var entities = _dal.GetAll();
|
var models = Entity2Models(entities);
|
if (models == null)
|
{
|
models = new List<Model.PumpCurve>();
|
}
|
return models;
|
}, ConfigHelper.CacheKeepTime, ConfigHelper.CacheRandomTime);
|
}
|
|
// 更新缓存
|
private void UpdateCache(long id)
|
{
|
if (id < 1)
|
return;
|
var entities = _dal.GetByID(id);
|
var models = Entity2Model(entities);
|
var all = GetCache();
|
var model = all.Find(x => x.ID == id);
|
if (model == null)
|
{
|
all.Add(models);
|
}
|
else
|
{
|
model.Reset(models);
|
}
|
}
|
|
// 更新缓存
|
private void UpdateCache(List<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);
|
}
|
}
|
|
// 移除缓存
|
private void RemoveCache(long id)
|
{
|
if (id < 1)
|
return;
|
var all = GetCache();
|
all.RemoveAll(x => x.ID == id);
|
}
|
|
// 移除缓存
|
private void RemoveCache(List<long> ids)
|
{
|
if (ids == null || ids.Count() < 1)
|
return;
|
var all = GetCache();
|
all.RemoveAll(x => ids.Contains(x.ID));
|
}
|
|
#endregion
|
|
#region Get
|
|
/// <summary>
|
/// 查询全部
|
/// </summary>
|
public List<Model.PumpCurve> GetAll()
|
{
|
var all = GetCache();
|
return all.ToList();
|
}
|
|
/// <summary>
|
/// 根据 ID查询
|
/// </summary>
|
public Model.PumpCurve GetByID(long id)
|
{
|
var all = GetAll();
|
return all.Find(x => x.ID == id);
|
}
|
|
/// <summary>
|
/// 根据 Ids 查询
|
/// </summary>
|
public List<Model.PumpCurve> GetByIds(List<long> ids)
|
{
|
if (ids == null || ids.Count() < 1)
|
return default;
|
var all = GetAll();
|
return all.Where(x => ids.Contains(x.ID)).ToList();
|
}
|
|
/// <summary>
|
/// 根据 PumpID获取
|
/// </summary>
|
public List<Model.PumpCurve> GetByPumpID(long pumpId)
|
{
|
var maps = new PumpCurveMapping().GetByPumpID(pumpId);
|
if (maps == null || maps.Count() == 0)
|
return null;
|
return GetByIds(maps.Select(x => x.CurveID).ToList());
|
}
|
|
/// <summary>
|
/// 根据 Pump ID获取工作曲线
|
/// </summary>
|
public Model.PumpCurve GetWorkingByPumpID(long pumpId)
|
{
|
var maps = new PumpCurveMapping().GetByPumpID(pumpId);
|
if (maps == null || maps.Count() == 0)
|
return null;
|
var map = maps.Find(x => x.IsWorking);
|
if (map == null)
|
return null;
|
return GetByID(map.CurveID);
|
}
|
|
/// <summary>
|
/// 根据 Pump ID获取默认工作曲线(没找到工作曲线,就选取最后一条)
|
/// </summary>
|
public Model.PumpCurve GetDefaultWorkingByPumpID(long pumpId)
|
{
|
var maps = new PumpCurveMapping().GetByPumpID(pumpId);
|
if (maps == null || maps.Count() == 0)
|
return null;
|
var map = maps.Find(x => x.IsWorking);
|
if (map == null)
|
{
|
map = maps.Last();
|
}
|
return GetByID(map.CurveID);
|
}
|
|
|
/// <summary>
|
/// 根据 PumpID获取
|
/// </summary>
|
public List<Model.PumpCurveExMapping> GetExMappingByPumpID(long pumpId)
|
{
|
if (pumpId < 1)
|
return default;
|
var maps = new PumpCurveMapping().GetByPumpID(pumpId);
|
if (maps == null || maps.Count() == 0)
|
return null;
|
var models = GetByIds(maps.Select(x => x.CurveID).ToList());
|
|
|
return (from m in maps
|
join c in models on m.CurveID equals c.ID
|
select new Model.PumpCurveExMapping(c, m))?.ToList();
|
}
|
|
|
/// <summary>
|
/// 根据 ID 获取
|
/// </summary>
|
public Model.PumpCurveExMapping GetExMappingByID(long pumpId, long id)
|
{
|
if (pumpId < 1)
|
return default;
|
if (id < 1)
|
return default;
|
var list = GetExMappingByPumpID(pumpId);
|
if (list == null || !list.Any())
|
return default;
|
var model = list.Find(x => x.ID == id);
|
return model;
|
}
|
|
#endregion
|
|
#region Insert
|
|
/// <summary>
|
/// 插入
|
/// </summary>
|
public long Insert(Model.PumpCurve model)
|
{
|
if (model == null)
|
return default;
|
var entity = Model2Entity(model);
|
var ID = _dal.Insert(entity);
|
if (ID > 0)
|
{
|
UpdateCache(ID);
|
}
|
return ID;
|
}
|
|
/// <summary>
|
/// 插入
|
/// </summary>
|
public long InsertEx(Model.PumpCurveExMapping model)
|
{
|
if (model == null)
|
return default;
|
var entity = Model2Entity(model);
|
var ID = _dal.Insert(entity);
|
if (ID < 1)
|
return default;
|
var mapping = new Model.PumpCurveMapping();
|
mapping.PumpID = model.PumpID;
|
mapping.CurveID = ID;
|
mapping.OtherName = model.OtherName;
|
mapping.IsWorking = false;
|
mapping.ID = new PumpCurveMapping().Insert(mapping);
|
if (mapping.ID < 1)
|
{
|
DeleteByID(ID, out string msg);
|
return default;
|
}
|
UpdateCache(ID);
|
return mapping.ID;
|
}
|
|
/// <summary>
|
/// 批量插入
|
/// </summary>
|
public bool Inserts(List<Model.PumpCurve> models)
|
{
|
if (models == null || models.Count() < 1)
|
return default;
|
var entities = Model2Entities(models.ToList());
|
var ids = _dal.InsertsR(entities);
|
if (ids != null && ids.Count > 0)
|
{
|
UpdateCache(ids);
|
return true;
|
}
|
return false;
|
}
|
|
#endregion
|
|
#region Update
|
|
/// <summary>
|
/// 更新
|
/// </summary>
|
public bool Update(Model.PumpCurve model)
|
{
|
if (model == null)
|
return default;
|
if (model.ID < 1)
|
return default;
|
var entity = Model2Entity(model);
|
var bol = _dal.Update(entity);
|
if (bol)
|
{
|
UpdateCache(model.ID);
|
}
|
return bol;
|
}
|
|
/// <summary>
|
/// 批量更新
|
/// </summary>
|
public bool Updates(List<Model.PumpCurve> models)
|
{
|
if (models == null || models.Count() < 1)
|
return default;
|
if (models.ToList().Exists(x => x.ID < 1))
|
return default;
|
var entities = Model2Entities(models.ToList());
|
var bol = _dal.Updates(entities);
|
if (bol)
|
{
|
UpdateCache(models.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)
|
{
|
RemoveCache(id);
|
new PumpCurveMapping().DeleteByPumpCurveID(id, out msg);
|
new PumpSpeedCurve().DeleteByPumpCurveID(id, out msg);
|
}
|
return bol;
|
}
|
|
#endregion
|
|
#region Cover
|
|
/// <summary>
|
/// 批量覆盖
|
/// </summary>
|
public bool Covers(List<Model.PumpCurve> models)
|
{
|
if (models == null || models.Count() < 1)
|
return default;
|
var entities = Model2Entities(models);
|
var bol = _dal.Covers(entities);
|
if (bol)
|
{
|
CacheHelper<Model.PumpCurve>.Remove(Settings.Project.ID);
|
}
|
return bol;
|
}
|
|
#endregion
|
|
|
|
}
|
}
|