using Learun.Cache.Base; using Learun.Cache.Factory; using Learun.Util; using System; using System.Collections.Generic; using System.Linq; namespace Learun.Application.Organization { /// /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园 /// Copyright (c) 2013-2018 北京泉江科技有限公司 /// 创建人:陈彬彬 /// 日 期:2017.04.17 /// 描 述:部门管理 /// public class DepartmentBLL : DepartmentIBLL { #region 属性 private DepartmentService departmentService = new DepartmentService(); #endregion #region 缓存定义 private ICache cache = CacheFactory.CaChe(); private string cacheKey = "Learun_adms_department_"; // +加公司主键 #endregion #region 获取数据 /// /// 获取部门列表信息(根据公司Id) /// /// 公司Id /// public List GetList(string companyId) { try { List list = cache.Read>(cacheKey + companyId, CacheId.department); if (list == null) { list = (List)departmentService.GetList(companyId); cache.Write>(cacheKey + companyId, list, CacheId.department); } return list; } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowBusinessException(ex); } } } /// /// 获取部门列表信息(根据公司Id) /// /// 公司Id /// 查询关键字 /// public List GetList(string companyId, string keyWord) { try { List list = GetList(companyId); if (!string.IsNullOrEmpty(keyWord)) { list = list.FindAll(t => t.F_FullName.Contains(keyWord) || t.F_EnCode.Contains(keyWord) || t.F_ShortName.Contains(keyWord)); } return list; } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowBusinessException(ex); } } } /// /// 获取部门数据实体 /// /// 主键 /// public DepartmentEntity GetEntity(string keyValue) { try { return departmentService.GetEntity(keyValue); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowBusinessException(ex); } } } /// /// 获取部门列表名称 /// /// 部门id /// public string GetDepartmentList(string keyValue) { try { return departmentService.GetDepartmentList(keyValue); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowBusinessException(ex); } } } /// /// 获取部门数据实体 /// /// 部门编号 /// public DepartmentEntity GetEntityByCode(string code) { try { return departmentService.GetEntityByCode(code); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowBusinessException(ex); } } } /// /// 获取部门数据实体 /// /// 公司主键 /// 部门主键 /// public DepartmentEntity GetEntity(string companyId, string departmentId) { try { return GetList(companyId).Find(t => t.F_DepartmentId.Equals(departmentId)); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowBusinessException(ex); } } } /// /// 获取树形数据 /// /// 公司id /// 父级id /// public List GetTree(string companyId, string parentId) { try { if (string.IsNullOrEmpty(companyId)) {// 如果公司主键没有的话,需要加载公司信息 return new List(); } List list = GetList(companyId); List treeList = new List(); foreach (var item in list) { TreeModel node = new TreeModel { id = item.F_DepartmentId, text = item.F_FullName, value = item.F_DepartmentId, showcheck = true, checkstate = 0, isexpand = true, parentId = item.F_ParentId }; treeList.Add(node); } return treeList.ToTree(parentId); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowBusinessException(ex); } } } public List GetTreeNoCheck(string companyId, string parentId) { try { if (string.IsNullOrEmpty(companyId)) {// 如果公司主键没有的话,需要加载公司信息 return new List(); } List list = GetList(companyId); List treeList = new List(); foreach (var item in list) { TreeModel node = new TreeModel { id = item.F_DepartmentId, text = item.F_FullName, value = item.F_DepartmentId, showcheck = false, checkstate = 0, isexpand = true, parentId = item.F_ParentId }; treeList.Add(node); } return treeList.ToTree(parentId); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowBusinessException(ex); } } } /// /// 获取树形数据 /// /// 公司数据列表 /// public List GetTree(List companylist) { try { List treeList = new List(); foreach (var companyone in companylist) { List departmentTree = GetTree(companyone.F_CompanyId, ""); if (departmentTree.Count > 0) { TreeModel node = new TreeModel { id = companyone.F_CompanyId, text = companyone.F_FullName, value = companyone.F_CompanyId, showcheck = true, checkstate = 0, isexpand = true, parentId = "0", hasChildren = true, ChildNodes = departmentTree, complete = true }; treeList.Add(node); } } return treeList; } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowBusinessException(ex); } } } /// /// 获取部门树无复选框 /// /// /// public List GetTreeNoCheck(List companylist) { try { List treeList = new List(); foreach (var companyone in companylist) { List departmentTree = GetTreeNoCheck(companyone.F_CompanyId, ""); if (departmentTree.Count > 0) { TreeModel node = new TreeModel { id = companyone.F_CompanyId, text = companyone.F_FullName, value = companyone.F_CompanyId, showcheck = false, checkstate = 0, isexpand = true, parentId = "0", hasChildren = true, ChildNodes = departmentTree, complete = true }; treeList.Add(node); } } return treeList; } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowBusinessException(ex); } } } /// /// 获取部门本身和子部门的id /// /// 父级ID /// public List GetSubNodes(string companyId, string parentId) { try { if (string.IsNullOrEmpty(parentId) || string.IsNullOrEmpty(companyId)) { return new List(); } List res = new List(); res.Add(parentId); List list = GetTree(companyId, parentId); GetSubNodes(list, res); return res; } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowBusinessException(ex); } } } /// /// 遍历树形数据获取全部子节点ID /// /// 树形数据列表 /// 输出数据列表 private void GetSubNodes(List list, List ourList) { foreach (var item in list) { ourList.Add(item.id); if (item.hasChildren) { GetSubNodes(item.ChildNodes, ourList); } } } public bool GetAny() { try { return departmentService.GetAny(); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowBusinessException(ex); } } } /// /// 获取部门映射数据 /// /// public Dictionary GetModelMap() { try { Dictionary dic = cache.Read>(cacheKey + "dic", CacheId.department); if (dic == null) { dic = new Dictionary(); var list = departmentService.GetAllList(); foreach (var item in list) { DepartmentModel model = new DepartmentModel() { companyId = item.F_CompanyId, parentId = item.F_ParentId, name = item.F_FullName }; dic.Add(item.F_DepartmentId, model); cache.Write(cacheKey + "dic", dic, CacheId.department); } } return dic; } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowBusinessException(ex); } } } #endregion #region 提交数据 /// /// 虚拟删除部门信息 /// /// 主键 public void VirtualDelete(string keyValue) { try { DepartmentEntity entity = GetEntity(keyValue); cache.Remove(cacheKey + entity.F_CompanyId, CacheId.department); cache.Remove(cacheKey + "dic", CacheId.department); departmentService.VirtualDelete(keyValue); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowBusinessException(ex); } } } /// /// 保存部门信息(新增、修改) /// /// 主键值 /// 部门实体 /// public void SaveEntity(string keyValue, DepartmentEntity departmentEntity) { try { cache.Remove(cacheKey + departmentEntity.F_CompanyId, CacheId.department); cache.Remove(cacheKey + "dic", CacheId.department); departmentService.SaveEntity(keyValue, departmentEntity); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowBusinessException(ex); } } } public List GetAllList() { try { return departmentService.GetAllList().ToList(); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowBusinessException(ex); } } } #endregion } }