using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IStation.BLL
{
///
/// 项目
///
public partial class Project
{
private readonly IDAL.IProject _dal
= DAL.Factory.DataAccess.CreateDAL>(Settings.DataAccess.ProjectDLLName, Settings.DataAccess.DALNameSpace);
#region Cache
//根据 查询缓存
private List GetCache()
{
return ProjectCacheHelper.GetSet(() =>
{
var entities = _dal.QueryAll();
var models = Entity2Models(entities);
if (models == null)
{
models = new List();
}
return models;
}, ConfigHelper.CacheKeepTime, ConfigHelper.CacheRandomTime);
}
//根据 Id 更新缓存
private void UpdateProjectCache( long Id)
{
var entity_ds = _dal.QueryById(Id);
var model_ds = Entity2Model(entity_ds);
var all = GetCache();
var model = all.Find(x => x.Id == Id);
if (model == null)
{
all.Add(model_ds);
}
else
{
model.Reset(model_ds);
}
}
//根据 Ids 更新缓存
private void UpdateProjectCache( IEnumerable Ids)
{
if (Ids == null || Ids.Count() < 1)
return;
var entities = _dal.QueryByIds(Ids);
var models = Entity2Models(entities);
var all = GetCache();
all.RemoveAll(x => Ids.Contains(x.Id));
if (models != null && models.Count > 0)
{
all.AddRange(models);
}
}
//根据 Id 移除缓存
private void RemoveProjectCache( long Id)
{
var all = GetCache();
all.RemoveAll(x => x.Id == Id);
}
#endregion
#region Query
///
/// 查询全部
///
public List QueryAll()
{
var all = GetCache();
return all.ToList();
}
///
/// 根据 Id查询
///
public Model.Project QueryById( long Id)
{
var all = QueryAll();
return all.Find(x => x.Id == Id);
}
///
/// 根据 Ids 查询
///
public List QueryByIds( IEnumerable Ids)
{
if (Ids == null || Ids.Count() < 1)
return default;
var all = QueryAll();
return all.Where(x => Ids.Contains(x.Id)).ToList();
}
#endregion
#region Insert
///
/// 插入
///
public long Insert( Model.Project model)
{
if (model == null)
return default;
var entities = Model2Entity(model);
var Id = _dal.Insert(entities);
if (Id > 0)
{
UpdateProjectCache(Id);
}
return Id;
}
///
/// 批量插入
///
public bool Inserts( IEnumerable list)
{
if (list == null || list.Count() < 1)
return default;
var entities = Model2Entities(list.ToList());
var Ids = _dal.InsertsR(entities);
if (Ids != null && Ids.Count > 0)
{
UpdateProjectCache(Ids);
return true;
}
return false;
}
#endregion
#region Update
///
/// 更新
///
public bool Update( Model.Project model)
{
if (model == null)
return default;
if (model.Id < 1)
return default;
var entities = Model2Entity(model);
var bol = _dal.Update(entities);
if (bol)
{
UpdateProjectCache(model.Id);
}
return bol;
}
///
/// 批量更新
///
public bool Updates( List list)
{
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(entities);
if (bol)
{
UpdateProjectCache(list.Select(x => x.Id).ToList());
}
return bol;
}
#endregion
#region Exist
///
/// 根据 Id 判断是否存在
///
public bool IsExistById( long Id)
{
var all = QueryAll();
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)
{
RemoveProjectCache(Id);
}
return bol;
}
#endregion
#region Import
///
/// 导入
///
public long Import(string fileName, out string msg)
{
if (!File.Exists(fileName))
{
msg = "文件不存在";
return default;
}
return _dal.Import(fileName, out msg);
}
#endregion
#region Export
///
/// 导出
///
public bool Export(long id, string fileName, out string msg)
{
if (id < 1)
{
msg = "id<1";
return default;
}
return _dal.Export(id, fileName, out msg);
}
#endregion
}
}