using System.Collections.Generic;
using System.Linq;
namespace IStation.BLL
{
///
/// ÐźÅÀàÐÍ
///
public partial class SignalType
{
private readonly DAL.SignalType _dal = new DAL.SignalType();
#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.SignalType 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();
}
///
/// ¸ù¾Ý groupId ²éѯ
///
public List GetByGroupID(long groupId)
{
var all = GetAll();
all = all.Where(x => x.GroupID == groupId).ToList();
return all?.OrderBy(x => x.SortCode).ToList();
}
///
/// ¸ù¾Ý identifier ²éѯ
///
public Model.SignalType GetByIdentifier(string identifier)
{
var all = GetAll();
var model = all.Find(x => x.Identifier == identifier);
return model;
}
///
/// ¸ù¾Ý identifiers ²éѯ
///
public List GetByIdentifiers(List identifiers)
{
if (identifiers == null || identifiers.Count() < 1)
return default;
var all = GetAll();
all = all.Where(x => identifiers.Contains(x.Identifier)).ToList();
return all?.OrderBy(x => x.SortCode).ToList();
}
#endregion
#region Insert
///
/// ²åÈë
///
public long Insert(Model.SignalType 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.SignalType 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 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;
}
#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
}
}