namespace Yw.Service { /// /// 单位值 /// public partial class SysUnitValue { #region Cache //获取缓存 private static List GetCache() { var all = SysUnitValueCacheHelper.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); } SysUnitValueCacheHelper.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); } SysUnitValueCacheHelper.Trigger(); } //移除缓存 private static void RemoveCache(long ID) { var all = GetCache(); all.RemoveAll(x => x.ID == ID); SysUnitValueCacheHelper.Trigger(); } /// /// 发布缓存 /// public static void PublishCache(string key) { SysUnitValueCacheHelper.Publish(key); } #endregion #region Query /// /// 获取所有 /// public List GetAll() { var all = GetCache(); var list = all.OrderBy(x => x.SortCode).ToList(); return list; } /// /// 通过 ID 获取 /// public Model.SysUnitValue GetByID(long ID) { var all = GetAll(); var model = all.Find(x => x.ID == ID); return model; } /// /// 通过 ID 获取 /// public List GetByIds(List Ids) { if (Ids == null || Ids.Count < 1) { return default; } var all = GetAll(); var list = all.Where(x => Ids.Contains(x.ID)).OrderBy(x => x.SortCode).ToList(); return list; } /// /// 通过 TypeID 获取 /// public List GetByTypeID(long TypeID) { var all = GetAll(); var list = all.Where(x => x.TypeID == TypeID).OrderBy(x => x.SortCode).ToList(); return list; } /// /// 通过类型 Code 获取 /// public List GetByTypeCode(string Code) { var type = new SysUnitType().GetByCode(Code); if (type == null) { return default; } return GetByTypeID(type.ID); } /// /// 通过 Code 获取 /// public Model.SysUnitValue GetByCode(long TypeID, string Code) { var all = GetAll(); return all.Find(x => x.TypeID == TypeID && x.Code == Code); } /// /// 通过 Code 获取 /// public Model.SysUnitValue GetByCode(string TypeCode, string Code) { var type = new Service.SysUnitType().GetByCode(TypeCode); if (type == null) { return default; } return GetByCode(type.ID, Code); } /// /// 通过 TypeID 获取分页列表 /// public List GetPageListByTypeID(long TypeID, int PageIndex, int PageSize, out int Total) { Total = 0; if (PageIndex < 1 || PageSize < 1) { return default; } var all = GetByTypeID(TypeID); if (all == null || all.Count < 1) { return default; } Total = all.Count; var list = all.OrderBy(x => x.SortCode).Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList(); return list; } /// /// 获取最大排序码 /// public int GetMaxSortCode(long TypeID) { var all = GetByTypeID(TypeID); if (all == null || all.Count < 1) { return 0; } return all.Max(x => x.SortCode); } #endregion #region Insert /// /// 添加一条 /// public long Insert(Model.SysUnitValue 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 default; } var dal = DALCreateHelper.CreateDAL(); var entityList = Model2Entities(list); var ids = dal.InsertsR(entityList); if (ids != null && ids.Count > 0) { UpdateCache(ids); return true; } return false; } #endregion #region Update /// /// 更新一条 /// public bool Update(Model.SysUnitValue model) { if (model == null) { return false; } if (model.ID < 1) { 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; } if (list.Exists(x => x.ID < 1)) { return false; } var entityList = Model2Entities(list.ToList()); var dal = DALCreateHelper.CreateDAL(); var bol = dal.Updates(entityList); if (bol) { UpdateCache(list.Select(x => x.ID).ToList()); } return bol; } /// /// 更新排序码 /// public bool UpdateSortCode(long ID, int SortCode) { if (ID < 1) { return false; } 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.Any()) { return false; } if (sorters.Exists(x => x.ID < 1)) { return false; } var dal = DALCreateHelper.CreateDAL(); var bol = dal.UpdateSorter(sorters.ToEntityList()); if (bol) { UpdateCache(sorters.Select(x => x.ID).ToList()); } return bol; } /// /// 更新编码 /// public bool UpdateCode(long ID, string Code) { if (ID < 1) { return false; } var dal = DALCreateHelper.CreateDAL(); var bol = dal.UpdateCode(ID, Code); if (bol) { UpdateCache(ID); } return bol; } #endregion #region Exist /// /// 根据 TypeID 判断是否存在 /// public bool IsExistByTypeID(long TypeID) { var all = GetAll(); return all.Exists(x => x.TypeID == TypeID); } /// /// 判断Code是否存在 /// public bool IsExistCode(long TypeID, string Code) { if (string.IsNullOrEmpty(Code)) { return false; } var all = GetAll(); if (all == null || all.Count < 1) { return false; } return all.Exists(x => x.TypeID == TypeID && x.Code == Code); } /// /// 判断Code是否存在 ExceptID /// public bool IsExistCodeExceptID(long TypeID, string Code, long ExceptID) { if (string.IsNullOrEmpty(Code)) { return false; } var all = GetAll(); if (all == null || all.Count < 1) { return false; } return all.Exists(x => x.TypeID == TypeID && x.Code == Code && x.ID != ExceptID); } #endregion #region Delete /// /// 通过 ID 删除 /// public bool DeleteByID(long ID, out string Msg) { Msg = string.Empty; if (new SysUnitName().IsExistByValueID(ID)) { Msg = "已配置单位名称"; return false; } if (new SysUnitTransfer().IsExistByValueID(ID)) { Msg = "已配置单位转换"; return false; } var dal = DALCreateHelper.CreateDAL(); var bol = dal.DeleteByID(ID); if (bol) { RemoveCache(ID); } return bol; } #endregion } }