using System.Collections.Generic;
using System.Linq;
namespace IStation.BLL
{
///
/// 泵曲线映射关系
///
public partial class PumpCurveMapping
{
private readonly DAL.PumpCurveMapping _dal = new DAL.PumpCurveMapping();
#region Cache
// 获取缓存
private List GetCache()
{
return CacheHelper.GetSet(SettingsD.Project.ID, () =>
{
var entities = _dal.GetAll();
var models = Entity2Models(entities);
if (models == null)
{
models = new List();
}
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 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 ids)
{
if (ids == null || ids.Count() < 1)
return;
var all = GetCache();
all.RemoveAll(x => ids.Contains(x.ID));
}
#endregion
#region Get
///
/// 查询全部
///
public List GetAll()
{
var all = GetCache();
return all.ToList();
}
///
/// 根据 ID查询
///
public Model.PumpCurveMapping GetByID(long id)
{
var all = GetAll();
return all.Find(x => x.ID == id);
}
///
/// 根据 Ids 查询
///
public List GetByIds(List ids)
{
if (ids == null || ids.Count() < 1)
return default;
var all = GetAll();
return all.Where(x => ids.Contains(x.ID)).ToList();
}
///
/// 根据 PumpID 查询
///
public List GetByPumpID(long pumpId)
{
var all = GetAll();
return all.Where(x => x.PumpID == pumpId).OrderBy(x => x.SortCode).ToList();
}
///
/// 根据 PumpIds 查询
///
public List GetByPumpIds(List pumpIds)
{
if (pumpIds == null || pumpIds.Count() < 1)
return default;
var all = GetAll();
return all?.Where(x => pumpIds.Contains(x.PumpID)).OrderBy(x => x.SortCode).ToList();
}
///
/// 根据 PumpCurveID 查询
///
public List GetByPumpCurveID(long pumpCurveId)
{
var all = GetAll();
return all?.Where(x => x.CurveID == pumpCurveId).OrderBy(x => x.SortCode).ToList();
}
///
/// 根据 CurveIds 查询
///
public List GetByPumpCurveIds(List pumpCurveIds)
{
if (pumpCurveIds == null || pumpCurveIds.Count() < 1)
return default;
var all = GetAll();
return all.Where(x => pumpCurveIds.Contains(x.CurveID)).OrderBy(x => x.SortCode).ToList();
}
///
/// 根据 PumpID 查询
///
public Model.PumpCurveMapping GetWorkCurveByPumpID(long pumpId)
{
var all = GetAll();
var mappings = all.Where(x => x.PumpID == pumpId).OrderBy(x => x.SortCode).ToList();
if (mappings == null || !mappings.Any())
return default;
var workCurve = mappings.Find(x => x.IsWorking);
if (workCurve == null)
workCurve = mappings.FirstOrDefault();
return workCurve;
}
#endregion
#region Insert
///
/// 插入
///
public long Insert(Model.PumpCurveMapping model)
{
if (model == null)
return default;
var entity = Model2Entity(model);
var ID = _dal.Insert(entity);
if (ID > 0)
{
UpdateCache(ID);
}
return ID;
}
///
/// 批量插入
///
public bool Inserts(List 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
///
/// 更新
///
public bool Update(Model.PumpCurveMapping 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;
}
///
/// 批量更新
///
public bool Updates(List 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;
}
///
/// 设置工作曲线
///
public bool UpdateWorkCurve(long id)
{
if (id < 1)
return default;
var all = GetAll();
if (all == null || !all.Any())
return default;
var model = all.Find(x => x.ID == id);
if (model == null)
return default;
var existWorkCurve = all.Find(x => x.PumpID == model.PumpID && x.ID != model.ID && x.IsWorking);
if (existWorkCurve != null)
{
existWorkCurve.IsWorking = false;
var entityExistWorkCurve = Model2Entity(existWorkCurve);
if (!_dal.Update(entityExistWorkCurve))
return default;
UpdateCache(existWorkCurve.ID);
}
model.IsWorking = true;
var entity = Model2Entity(model);
var bol = _dal.Update(entity);
if (bol)
{
UpdateCache(model.ID);
}
return bol;
}
///
/// 更新排序码
///
public bool UpdateSortCode(long id, int sortCode)
{
if (id < 1)
return false;
var bol = _dal.UpdateSortCode(id, sortCode);
if (bol)
{
UpdateCache(id);
}
return bol;
}
///
/// 更新排序
///
public virtual bool UpdateSorter(List sorters)
{
if (sorters == null || sorters.Count() < 1)
return default;
if (sorters.Exists(x => x.ID < 1))
return default;
var entities = sorters.Model2Entities();
var bol = _dal.UpdateSorter(entities);
if (bol)
{
var ids = entities.Select(x => x.ID).ToList();
UpdateCache(ids);
}
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)
{
RemoveCache(id);
}
return bol;
}
///
/// 根据 Ids 删除
///
public bool DeleteByIds(List ids, out string msg)
{
msg = string.Empty;
var bol = _dal.DeleteByIds(ids);
if (bol)
{
RemoveCache(ids);
}
return bol;
}
////
/// 根据 PumpCurveID 删除
///
public bool DeleteByPumpCurveID(long pumpCurveId, out string msg)
{
msg = string.Empty;
var all = GetByPumpCurveID(pumpCurveId);
if (all != null && all.Count > 0)
{
var ids = all.Select(x => x.ID).ToList();
return DeleteByIds(ids, out msg);
}
return true;
}
#endregion
#region Cover
///
/// 批量覆盖
///
public bool Covers(List models)
{
if (models == null || models.Count() < 1)
return default;
var entities = Model2Entities(models);
var bol = _dal.Covers(entities);
if (bol)
{
CacheHelper.Remove(SettingsD.Project.ID);
}
return bol;
}
#endregion
}
}