namespace Yw.Service
{
///
/// Dmaµãλ
///
public partial class DmaSite
{
#region Cache
//»ñÈ¡»º´æ
private static List GetCache()
{
var all = DmaSiteCacheHelper.GetSet(() =>
{
var dal = DALCreateHelper.CreateDAL();
var entityList = dal.GetAll();
var modelList = Entity2Models(entityList);
if (modelList == null)
{
modelList = new List();
}
return modelList;
}, ConfigHelper.CacheKeepTime, ConfigHelper.CacheRandomTime);
return all;
}
//ͨ¹ý ID ¸üлº´æ
private static void UpdateCache(long ID)
{
var dal = DALCreateHelper.CreateDAL();
var entityDb = dal.GetByID(ID);
var modelDb = Entity2Model(entityDb);
var all = GetCache();
var model = all.Find(x => x.ID == ID);
if (model == null)
{
all.Add(modelDb);
}
else
{
model.Reset(modelDb);
}
DmaSiteCacheHelper.Trigger();
}
//ͨ¹ý Ids ¸üлº´æ
private static void UpdateCache(List Ids)
{
if (Ids == null || Ids.Count < 1)
return;
var dal = DALCreateHelper.CreateDAL();
var entityList = dal.GetByIds(Ids);
var modelList = Entity2Models(entityList);
var all = GetCache();
all.RemoveAll(x => Ids.Contains(x.ID));
if (modelList != null && modelList.Count > 0)
{
all.AddRange(modelList);
}
DmaSiteCacheHelper.Trigger();
}
//ÒÆ³ý»º´æ
private static void RemoveCache(long ID)
{
var all = GetCache();
all.RemoveAll(x => x.ID == ID);
DmaSiteCacheHelper.Trigger();
}
///
/// ·¢²¼»º´æ
///
public static void PublishCache(string key)
{
DmaSiteCacheHelper.Publish(key);
}
#endregion
#region Query
///
/// »ñÈ¡ËùÓÐ
///
public List GetAll()
{
var all = GetCache();
return all.OrderBy(x => x.SortCode).ToList();
}
///
/// ͨ¹ý ID »ñÈ¡
///
public Model.DmaSite 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)).OrderBy(x => x.SortCode).ToList();
}
///
/// ͨ¹ý BelongType ºÍ BelongID »ñÈ¡
///
public List GetByBelongTypeAndBelongID(string BelongType, long BelongID)
{
var all = GetAll();
return all.Where(x => x.BelongType == BelongType && x.BelongID == BelongID).OrderBy(x => x.SortCode).ToList();
}
///
/// »ñÈ¡×î´óÅÅÐòÂë
///
public int GetMaxSortCode(string BelongType, long BelongID)
{
var all = GetByBelongTypeAndBelongID(BelongType, BelongID);
if (all == null || all.Count < 1)
{
return 0;
}
return all.Max(x => x.SortCode);
}
#endregion
#region Insert
///
/// Ìí¼ÓÒ»Ìõ
///
public long Insert(Model.DmaSite model)
{
if (model == null)
{
return default;
}
var entity = Model2Entity(model);
var dal = DALCreateHelper.CreateDAL();
var id = dal.Insert(entity);
if (id > 0)
{
UpdateCache(id);
}
return id;
}
///
/// ÅúÁ¿²åÈë
///
public bool Inserts(List list)
{
if (list == null || list.Count < 1)
{
return false;
}
var dal = DALCreateHelper.CreateDAL();
var entity_list = Model2Entities(list);
var ids = dal.InsertsR(entity_list);
if (ids != null && ids.Count > 0)
{
UpdateCache(ids);
return true;
}
return false;
}
#endregion
#region Update
///
/// ¸üÐÂÒ»Ìõ
///
public bool Update(Model.DmaSite model)
{
if (model == null)
{
return false;
}
var entity = Model2Entity(model);
var dal = DALCreateHelper.CreateDAL();
var bol = dal.Update(entity);
if (bol)
{
UpdateCache(model.ID);
}
return bol;
}
///
/// ÅúÁ¿¸üÐÂ
///
public bool Updates(List list)
{
if (list == null || list.Count < 1)
{
return false;
}
var entity_list = Model2Entities(list.ToList());
var dal = DALCreateHelper.CreateDAL();
var bol = dal.Updates(entity_list);
if (bol)
{
UpdateCache(list.Select(x => x.ID).ToList());
}
return bol;
}
///
/// ¸üÐÂÅÅÐòÂë
///
public bool UpdateSortCode(long ID, int SortCode)
{
if (ID < 1)
{
return default;
}
var dal = DALCreateHelper.CreateDAL();
var bol = dal.UpdateSortCode(ID, SortCode);
if (bol)
{
UpdateCache(ID);
}
return bol;
}
///
/// ¸üÐÂÅÅÐò
///
public bool UpdateSorter(List sorters)
{
if (sorters == null || sorters.Count < 1)
{
return default;
}
var dal = DALCreateHelper.CreateDAL();
var bol = dal.UpdateSorter(sorters.ToEntityList());
if (bol)
{
UpdateCache(sorters.Select(x => x.ID).ToList());
}
return bol;
}
///
/// ¸üРParas
///
public bool UpdateParas(long ID, Dictionary Paras)
{
var dal = DALCreateHelper.CreateDAL();
var bol = dal.UpdateParas(ID, ParasHelper.ToString(Paras));
if (bol)
{
UpdateCache(ID);
}
return bol;
}
///
/// ¸üРFlags
///
public bool UpdateFlags(long ID, List Flags)
{
var dal = DALCreateHelper.CreateDAL();
var bol = dal.UpdateFlags(ID, FlagsHelper.ToString(Flags));
if (bol)
{
UpdateCache(ID);
}
return bol;
}
///
/// ¸üРTagName
///
public bool UpdateTagName(long ID, string TagName)
{
var dal = DALCreateHelper.CreateDAL();
var bol = dal.UpdateTagName(ID, TagName);
if (bol)
{
UpdateCache(ID);
}
return bol;
}
#endregion
#region Exist
///
/// ÅÐ¶Ï Code ÊÇ·ñ´æÔÚ
///
public bool IsExistCode(string Code)
{
if (string.IsNullOrEmpty(Code))
{
return false;
}
var all = GetAll();
return all.Exists(x => x.Code == Code);
}
///
/// ÅÐ¶Ï Code ÊÇ·ñ´æÔÚ ExceptID
///
public bool IsExistCodeExceptID(string Code, long ExceptID)
{
if (string.IsNullOrEmpty(Code))
{
return false;
}
var all = GetAll();
return all.Exists(x => x.Code == Code && x.ID != ExceptID);
}
///
/// ÅÐ¶Ï TagName ÊÇ·ñ´æÔÚ
///
public bool IsExistTagName(string TagName)
{
if (string.IsNullOrEmpty(TagName))
{
return false;
}
var all = GetAll();
return all.Exists(x => x.TagName == TagName);
}
///
/// ÅÐ¶Ï TagName ÊÇ·ñ´æÔÚ ExceptID
///
public bool IsExistTagNameExceptID(string TagName, long ExceptID)
{
if (string.IsNullOrEmpty(TagName))
{
return false;
}
var all = GetAll();
return all.Exists(x => x.TagName == TagName && x.ID != ExceptID);
}
#endregion
#region Delete
///
/// ͨ¹ý ID ɾ³ý
///
public bool DeleteByID(long ID, out string Msg)
{
Msg = string.Empty;
var model = GetByID(ID);
if (model == null)
{
Msg = "Êý¾Ý²»´æÔÚ";
return false;
}
if (new DmaSiteBinding().IsExistValidBySiteID(ID))
{
Msg = "´æÔÚÓÐЧ°ó¶¨";
return false;
}
if (new DmaSiteMapping().IsExistBySiteID(ID))
{
Msg = "´æÔÚÓ³Éä¹ØÏµ";
return false;
}
var dal = DALCreateHelper.CreateDAL();
var bol = dal.DeleteByID(ID);
if (bol)
{
RemoveCache(ID);
DmaSiteBinding.RemoveCacheBySiteID(ID);
}
return bol;
}
#endregion
}
}