using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace IStation.BLL
|
{
|
/// <summary>
|
/// 分析结构
|
/// </summary>
|
public partial class AnalyzeStructure
|
{
|
private readonly IDAL.IAnalyzeStructure<Entity.AnalyzeStructure> _dal
|
= DAL.Factory.DataAccess.CreateDAL<IDAL.IAnalyzeStructure<Entity.AnalyzeStructure>>(Settings.DataAccess.SceneDLLName, Settings.DataAccess.DALNameSpace);
|
|
#region Cache
|
|
//根据 projectId sceneId 查询缓存
|
private List<Model.AnalyzeStructure> GetSceneCache(long projectId, long sceneId)
|
{
|
return AnalyzeStructureCacheHelper.GetSet(projectId,sceneId, () =>
|
{
|
var entities = _dal.QueryAll(projectId,sceneId);
|
var models = Entity2Models(entities);
|
if (models == null)
|
{
|
models = new List<Model.AnalyzeStructure>();
|
}
|
return models;
|
}, ConfigHelper.CacheKeepTime, ConfigHelper.CacheRandomTime);
|
}
|
|
//根据 Id 更新缓存
|
private void UpdateSceneCache(long projectId,long sceneId, long Id)
|
{
|
var entity_ds = _dal.QueryById(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<long> Ids)
|
{
|
if (Ids == null || Ids.Count() < 1)
|
return;
|
var entities = _dal.QueryByIds(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 Query
|
|
/// <summary>
|
/// 查询全部
|
/// </summary>
|
public List<Model.AnalyzeStructure> QueryAll(long projectId, long sceneId)
|
{
|
var all = GetSceneCache(projectId, sceneId);
|
return all.ToList();
|
}
|
|
/// <summary>
|
/// 根据 Id查询
|
/// </summary>
|
public Model.AnalyzeStructure QueryById(long projectId,long sceneId, long Id)
|
{
|
var all = QueryAll(projectId, sceneId);
|
return all.Find(x => x.Id == Id);
|
}
|
|
/// <summary>
|
/// 根据 Ids 查询
|
/// </summary>
|
public List<Model.AnalyzeStructure> QueryByIds(long projectId,long sceneId, IEnumerable<long> Ids)
|
{
|
if (Ids == null || Ids.Count() < 1)
|
return default;
|
var all = QueryAll(projectId,sceneId);
|
return all.Where(x => Ids.Contains(x.Id)).ToList();
|
}
|
#endregion
|
|
#region Insert
|
|
/// <summary>
|
/// 插入
|
/// </summary>
|
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;
|
}
|
|
/// <summary>
|
/// 批量插入
|
/// </summary>
|
public bool Inserts(long projectId,long sceneId, IEnumerable<Model.AnalyzeStructure> 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
|
|
/// <summary>
|
/// 更新
|
/// </summary>
|
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;
|
}
|
|
/// <summary>
|
/// 批量更新
|
/// </summary>
|
public bool Updates(long projectId,long sceneId, List<Model.AnalyzeStructure> 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
|
|
/// <summary>
|
/// 根据 Id 判断是否存在
|
/// </summary>
|
public bool IsExistById(long projectId,long sceneId, long Id)
|
{
|
var all = QueryAll(projectId,sceneId);
|
return all.Exists(x => x.Id == Id);
|
}
|
#endregion
|
|
#region Delete
|
|
/// <summary>
|
/// 根据 Id 删除
|
/// </summary>
|
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
|
|
}
|
}
|