using System.Collections.Generic;
|
using System.Linq;
|
|
namespace IStation.BLL
|
{
|
/// <summary>
|
/// 电力价格
|
/// </summary>
|
public partial class ElecPrice
|
{
|
private readonly DAL.ElecPrice _dal = new DAL.ElecPrice();
|
|
#region Cache
|
|
// 获取缓存
|
private List<Model.ElecPrice> GetCache()
|
{
|
return CacheHelper<Model.ElecPrice>.GetSet(SettingsD.Project.ID, () =>
|
{
|
var entities = _dal.GetAll();
|
var models = Entity2Models(entities);
|
if (models == null)
|
{
|
models = new List<Model.ElecPrice>();
|
}
|
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.ElecPrice> GetAll()
|
{
|
var all = GetCache();
|
return all.ToList();
|
}
|
|
/// <summary>
|
/// 根据 ID查询
|
/// </summary>
|
public Model.ElecPrice GetByID(long id)
|
{
|
var all = GetAll();
|
return all.Find(x => x.ID == id);
|
}
|
|
/// <summary>
|
/// 根据 Ids 查询
|
/// </summary>
|
public List<Model.ElecPrice> GetByIds(List<long> ids)
|
{
|
if (ids == null || ids.Count() < 1)
|
return default;
|
var all = GetAll();
|
return all.Where(x => ids.Contains(x.ID)).ToList();
|
}
|
#endregion
|
|
#region Insert
|
|
/// <summary>
|
/// 插入
|
/// </summary>
|
public long Insert(Model.ElecPrice 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 bool Inserts(List<Model.ElecPrice> 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.ElecPrice 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.ElecPrice> 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);
|
}
|
return bol;
|
}
|
|
#endregion
|
|
#region Cover
|
|
/// <summary>
|
/// 批量覆盖
|
/// </summary>
|
public bool Covers(List<Model.ElecPrice> models)
|
{
|
if (models == null || models.Count() < 1)
|
return default;
|
var entities = Model2Entities(models);
|
var bol = _dal.Covers(entities);
|
if (bol)
|
{
|
CacheHelper<Model.ElecPrice>.Remove(SettingsD.Project.ID);
|
}
|
return bol;
|
}
|
|
#endregion
|
|
}
|
}
|