using IStation.Untity;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace IStation.BLL
|
{
|
/// <summary>
|
/// É豸×é
|
/// </summary>
|
public partial class ProductGroup
|
{
|
private readonly IDAL.IProductGroup<Entity.ProductGroup> _dal
|
= DAL.Factory.DataAccessBase.CreateDAL<IDAL.IProductGroup<Entity.ProductGroup>>(Settings.DataAccess.ProductDLLName, Settings.DataAccess.DALNameSpace);
|
|
#region Cache
|
|
//ͨ¹ý projectId »ñÈ¡»º´æ
|
private List<Model.ProductGroup> GetProjectCache(long projectId)
|
{
|
return ProductGroupCacheHelper.GetSet(projectId, () =>
|
{
|
var entities = _dal.QueryAll(projectId);
|
var models = Entity2Models(entities);
|
if (models == null)
|
{
|
models = new List<Model.ProductGroup>();
|
}
|
return models;
|
}, ConfigHelper.CacheKeepTime, ConfigHelper.CacheRandomTime);
|
}
|
|
//ͨ¹ý Id ¸üлº´æ
|
private void UpdateProjectCache(long projectId, long id)
|
{
|
var entity_ds = _dal.QueryById(projectId, id);
|
var model_ds = Entity2Model(entity_ds);
|
var all = GetProjectCache(projectId);
|
var model = all.Find(x => x.Id == id);
|
if (model == null)
|
{
|
all.Add(model_ds);
|
}
|
else
|
{
|
model.Reset(model_ds);
|
}
|
}
|
|
//ͨ¹ý Ids ¸üлº´æ
|
private void UpdateProjectCache(long projectId, IEnumerable<long> ids)
|
{
|
if (ids == null || ids.Count() < 1)
|
return;
|
var entities = _dal.QueryByIds(projectId, ids);
|
var models = Entity2Models(entities);
|
var all = GetProjectCache(projectId);
|
all.RemoveAll(x => ids.Contains(x.Id));
|
if (models != null && models.Count > 0)
|
{
|
all.AddRange(models);
|
}
|
}
|
|
//ͨ¹ý Id ÒÆ³ý»º´æ
|
private void RemoveProjectCache(long projectId, long id)
|
{
|
var all = GetProjectCache(projectId);
|
all.RemoveAll(x => x.Id == id);
|
}
|
|
#endregion
|
|
#region Query
|
|
/// <summary>
|
/// ²éѯȫ²¿
|
/// </summary>
|
public List<Model.ProductGroup> QueryAll(long projectId)
|
{
|
var all = GetProjectCache(projectId);
|
return all.OrderBy(x => x.SortCode).ToList();
|
}
|
|
/// <summary>
|
/// ¸ù¾ÝId²éѯ
|
/// </summary>
|
public Model.ProductGroup QueryById(long projectId, long id)
|
{
|
var all = QueryAll(projectId);
|
return all.Find(x => x.Id == id);
|
}
|
|
/// <summary>
|
/// ͨ¹ý Ids »ñÈ¡
|
/// </summary>
|
public List<Model.ProductGroup> QueryByIds(long projectId, IEnumerable<long> ids)
|
{
|
if (ids == null || ids.Count() < 1)
|
return default;
|
var all = QueryAll(projectId);
|
return all.Where(x => ids.Contains(x.Id)).ToList();
|
}
|
#endregion
|
|
#region Insert
|
|
/// <summary>
|
/// ²åÈë
|
/// </summary>
|
public long Insert(long projectId, Model.ProductGroup model)
|
{
|
if (projectId < 1)
|
return default;
|
if (model == null)
|
return default;
|
var entities = Model2Entity(model);
|
var id = _dal.Insert(projectId, entities);
|
if (id > 0)
|
{
|
UpdateProjectCache(projectId, id);
|
}
|
return id;
|
}
|
|
/// <summary>
|
/// ÅúÁ¿²åÈë
|
/// </summary>
|
public bool Inserts(long projectId, IEnumerable<Model.ProductGroup> list)
|
{
|
if (projectId < 1)
|
return default;
|
if (list == null || list.Count() < 1)
|
return default;
|
var entities = Model2Entities(list.ToList());
|
var ids = _dal.InsertsR(projectId, entities);
|
if (ids != null && ids.Count > 0)
|
{
|
UpdateProjectCache(projectId, ids);
|
return true;
|
}
|
return false;
|
}
|
|
#endregion
|
|
#region Update
|
|
/// <summary>
|
/// ¸üÐÂ
|
/// </summary>
|
public bool Update(long projectId, Model.ProductGroup model)
|
{
|
if (projectId < 0)
|
return default;
|
if (model == null)
|
return default;
|
if (model.Id < 1)
|
return default;
|
var entities = Model2Entity(model);
|
var bol = _dal.Update(projectId, entities);
|
if (bol)
|
{
|
UpdateProjectCache(projectId, model.Id);
|
}
|
return bol;
|
}
|
|
/// <summary>
|
/// ÅúÁ¿¸üÐÂ
|
/// </summary>
|
public bool Updates(long projectId, List<Model.ProductGroup> list)
|
{
|
if (projectId < 0)
|
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, entities);
|
if (bol)
|
{
|
UpdateProjectCache(projectId, list.Select(x => x.Id).ToList());
|
}
|
return bol;
|
}
|
|
#endregion
|
|
#region Exist
|
|
/// <summary>
|
/// ¸ù¾Ý Id ÅжÏÊÇ·ñ´æÔÚ
|
/// </summary>
|
public bool IsExistById(long projectId, long Id)
|
{
|
var all = QueryAll(projectId);
|
return all.Exists(x => x.Id == Id);
|
}
|
#endregion
|
|
#region Delete
|
|
/// <summary>
|
/// ͨ¹ý Id ɾ³ý
|
/// </summary>
|
public bool DeleteById(long projectId, long id, out string msg)
|
{
|
msg = string.Empty;
|
var bol = _dal.DeleteById(projectId, id);
|
if (bol)
|
{
|
RemoveProjectCache(projectId, id);
|
}
|
return bol;
|
}
|
|
#endregion
|
|
}
|
}
|