using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IStation.BLL
{
///
/// 分析结构
///
public partial class AnalyzeStructure
{
private readonly IDAL.IAnalyzeStructure _dal
= DAL.Factory.DataAccess.CreateDAL>(Settings.DataAccess.SceneDLLName, Settings.DataAccess.DALNameSpace);
#region Cache
//根据 projectId sceneId 查询缓存
private List GetSceneCache(long projectId, long sceneId)
{
return AnalyzeStructureCacheHelper.GetSet(projectId,sceneId, () =>
{
var entities = _dal.GetAll(projectId,sceneId);
var models = Entity2Models(entities);
if (models == null)
{
models = new List();
}
return models;
}, ConfigHelper.CacheKeepTime, ConfigHelper.CacheRandomTime);
}
//根据 Id 更新缓存
private void UpdateSceneCache(long projectId,long sceneId, long Id)
{
var entity_ds = _dal.GetById(projectId,sceneId, Id);
var model_ds = Entity2Model(entity_ds);
var all = GetSceneCache(projectId, sceneId);
var model = all.Find(x => x.Id == Id);
if (model == null)
{
all.Add(model_ds);
}
else
{
model.Reset(model_ds);
}
}
//根据 Ids 更新缓存
private void UpdateSceneCache(long projectId,long sceneId, IEnumerable Ids)
{
if (Ids == null || Ids.Count() < 1)
return;
var entities = _dal.GetByIds(projectId,sceneId, Ids);
var models = Entity2Models(entities);
var all = GetSceneCache(projectId, sceneId);
all.RemoveAll(x => Ids.Contains(x.Id));
if (models != null && models.Count > 0)
{
all.AddRange(models);
}
}
//根据 Id 移除缓存
private void RemoveSceneCache(long projectId,long sceneId, long Id)
{
var all = GetSceneCache(projectId, sceneId);
all.RemoveAll(x => x.Id == Id);
}
#endregion
#region Get
///
/// 查询全部
///
public List GetAll(long projectId, long sceneId)
{
var all = GetSceneCache(projectId, sceneId);
return all.ToList();
}
///
/// 根据 Id查询
///
public Model.AnalyzeStructure GetById(long projectId,long sceneId, long Id)
{
var all = GetAll(projectId, sceneId);
return all.Find(x => x.Id == Id);
}
///
/// 根据 Ids 查询
///
public List GetByIds(long projectId,long sceneId, IEnumerable Ids)
{
if (Ids == null || Ids.Count() < 1)
return default;
var all = GetAll(projectId,sceneId);
return all.Where(x => Ids.Contains(x.Id)).ToList();
}
#endregion
#region Insert
///
/// 插入
///
public long Insert(long projectId,long sceneId, Model.AnalyzeStructure model)
{
if (projectId < 1)
return default;
if (model == null)
return default;
var entities = Model2Entity(model);
var Id = _dal.Insert(projectId,sceneId, entities);
if (Id > 0)
{
UpdateSceneCache(projectId,sceneId, Id);
}
return Id;
}
///
/// 批量插入
///
public bool Inserts(long projectId,long sceneId, IEnumerable list)
{
if (projectId < 1)
return default;
if (list == null || list.Count() < 1)
return default;
var entities = Model2Entities(list.ToList());
var Ids = _dal.InsertsR(projectId,sceneId, entities);
if (Ids != null && Ids.Count > 0)
{
UpdateSceneCache(projectId,sceneId, Ids);
return true;
}
return false;
}
#endregion
#region Update
///
/// 更新
///
public bool Update(long projectId,long sceneId, Model.AnalyzeStructure model)
{
if (projectId < 1)
return default;
if (model == null)
return default;
if (model.Id < 1)
return default;
var entities = Model2Entity(model);
var bol = _dal.Update(projectId,sceneId, entities);
if (bol)
{
UpdateSceneCache(projectId,sceneId, model.Id);
}
return bol;
}
///
/// 批量更新
///
public bool Updates(long projectId,long sceneId, List list)
{
if (projectId < 1)
return default;
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(projectId,sceneId, entities);
if (bol)
{
UpdateSceneCache(projectId,sceneId, list.Select(x => x.Id).ToList());
}
return bol;
}
#endregion
#region Exist
///
/// 根据 Id 判断是否存在
///
public bool IsExistById(long projectId,long sceneId, long Id)
{
var all = GetAll(projectId,sceneId);
return all.Exists(x => x.Id == Id);
}
#endregion
#region Delete
///
/// 根据 Id 删除
///
public bool DeleteById(long projectId,long sceneId, long Id, out string msg)
{
msg = string.Empty;
var bol = _dal.DeleteById(projectId,sceneId, Id);
if (bol)
{
RemoveSceneCache(projectId,sceneId, Id);
}
return bol;
}
#endregion
}
}