DepartmentController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. using LeaRun.Application.Busines.BaseManage;
  2. using LeaRun.Application.Cache;
  3. using LeaRun.Application.Code;
  4. using LeaRun.Application.Entity.BaseManage;
  5. using LeaRun.Util;
  6. using LeaRun.Util.WebControl;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Web.Mvc;
  10. namespace LeaRun.Application.Web.Areas.BaseManage.Controllers
  11. {
  12. /// <summary>
  13. /// 版 本 6.1
  14. /// Copyright (c) 2013-2016 上海力软信息技术有限公司
  15. /// 创建人:佘赐雄
  16. /// 日 期:2015.11.02 14:27
  17. /// 描 述:部门管理
  18. /// </summary>
  19. public class DepartmentController : MvcControllerBase
  20. {
  21. private OrganizeCache organizeCache = new OrganizeCache();
  22. private DepartmentBLL departmentBLL = new DepartmentBLL();
  23. private DepartmentCache departmentCache = new DepartmentCache();
  24. #region 视图功能
  25. /// <summary>
  26. /// 部门管理
  27. /// </summary>
  28. /// <returns></returns>
  29. [HttpGet]
  30. [HandlerAuthorize(PermissionMode.Enforce)]
  31. public ActionResult Index()
  32. {
  33. return View();
  34. }
  35. /// <summary>
  36. /// 部门表单
  37. /// </summary>
  38. /// <returns></returns>
  39. [HttpGet]
  40. [HandlerAuthorize(PermissionMode.Enforce)]
  41. public ActionResult Form()
  42. {
  43. return View();
  44. }
  45. #endregion
  46. #region 获取数据
  47. /// <summary>
  48. /// 部门列表
  49. /// </summary>
  50. /// <param name="organizeId">公司Id</param>
  51. /// <param name="keyword">关键字</param>
  52. /// <returns>返回树形Json</returns>
  53. [HttpGet]
  54. public ActionResult GetTreeJson(string organizeId, string keyword)
  55. {
  56. var data = departmentCache.GetList(organizeId).ToList();
  57. if (!string.IsNullOrEmpty(keyword))
  58. {
  59. data = data.TreeWhere(t => t.FullName.Contains(keyword), "DepartmentId");
  60. }
  61. var treeList = new List<TreeEntity>();
  62. foreach (DepartmentEntity item in data)
  63. {
  64. TreeEntity tree = new TreeEntity();
  65. bool hasChildren = data.Count(t => t.ParentId == item.DepartmentId) == 0 ? false : true;
  66. tree.id = item.DepartmentId;
  67. tree.text = item.FullName;
  68. tree.value = item.DepartmentId;
  69. tree.isexpand = true;
  70. tree.complete = true;
  71. tree.hasChildren = hasChildren;
  72. tree.parentId = item.ParentId;
  73. treeList.Add(tree);
  74. }
  75. return Content(treeList.TreeToJson());
  76. }
  77. /// <summary>
  78. /// 部门列表
  79. /// </summary>
  80. /// <param name="keyword">关键字</param>
  81. /// <returns>返回机构+部门树形Json</returns>
  82. public ActionResult GetOrganizeTreeJson(string keyword)
  83. {
  84. var organizedata = organizeCache.GetList();
  85. var departmentdata = departmentBLL.GetList();
  86. departmentdata = departmentdata.Where(a => a.IsBusinessAddress != 1);
  87. var treeList = new List<TreeEntity>();
  88. foreach (OrganizeEntity item in organizedata)
  89. {
  90. #region 机构
  91. TreeEntity tree = new TreeEntity();
  92. bool hasChildren = organizedata.Count(t => t.ParentId == item.OrganizeId) == 0 ? false : true;
  93. //if (hasChildren == false)
  94. //{
  95. // hasChildren = departmentdata.Count(t => t.OrganizeId == item.OrganizeId) == 0 ? false : true;
  96. // if (hasChildren == false)
  97. // {
  98. // continue;
  99. // }
  100. //}
  101. tree.id = item.OrganizeId;
  102. tree.text = item.FullName;
  103. tree.value = item.OrganizeId;
  104. tree.parentId = item.ParentId;
  105. tree.isexpand = true;
  106. tree.complete = true;
  107. tree.hasChildren = hasChildren;
  108. tree.Attribute = "Sort";
  109. tree.AttributeValue = "Organize";
  110. treeList.Add(tree);
  111. #endregion
  112. }
  113. foreach (DepartmentEntity item in departmentdata)
  114. {
  115. #region 部门
  116. TreeEntity tree = new TreeEntity();
  117. bool hasChildren = departmentdata.Count(t => t.ParentId == item.DepartmentId) == 0 ? false : true;
  118. tree.id = item.DepartmentId;
  119. tree.text = item.FullName;
  120. tree.value = item.DepartmentId;
  121. if (item.ParentId == "0")
  122. {
  123. tree.parentId = item.OrganizeId;
  124. }
  125. else
  126. {
  127. tree.parentId = item.ParentId;
  128. }
  129. tree.isexpand = true;
  130. tree.complete = true;
  131. tree.hasChildren = hasChildren;
  132. tree.Attribute = "Sort";
  133. tree.AttributeValue = "Department";
  134. treeList.Add(tree);
  135. #endregion
  136. }
  137. if (!string.IsNullOrEmpty(keyword))
  138. {
  139. treeList = treeList.TreeWhere(t => t.text.Contains(keyword), "id", "parentId");
  140. }
  141. return Content(treeList.TreeToJson());
  142. }
  143. /// <summary>
  144. /// 部门列表
  145. /// </summary>
  146. /// <param name="condition">查询条件</param>
  147. /// <param name="keyword">关键字</param>
  148. /// <returns>返回树形列表Json</returns>
  149. [HttpGet]
  150. public ActionResult GetTreeListJson(string condition, string keyword)
  151. {
  152. var organizedata = organizeCache.GetList();
  153. var departmentdata = departmentBLL.GetList().ToList();
  154. if (!string.IsNullOrEmpty(condition) && !string.IsNullOrEmpty(keyword))
  155. {
  156. #region 多条件查询
  157. switch (condition)
  158. {
  159. case "FullName": //部门名称
  160. departmentdata = departmentdata.TreeWhere(t => t.FullName.Contains(keyword), "DepartmentId");
  161. break;
  162. case "EnCode": //部门编号
  163. departmentdata = departmentdata.TreeWhere(t => t.EnCode.Contains(keyword), "DepartmentId");
  164. break;
  165. case "ShortName": //部门简称
  166. departmentdata = departmentdata.TreeWhere(t => t.ShortName.Contains(keyword), "DepartmentId");
  167. break;
  168. case "Manager": //负责人
  169. departmentdata = departmentdata.TreeWhere(t => t.Manager.Contains(keyword), "DepartmentId");
  170. break;
  171. case "OuterPhone": //电话号
  172. departmentdata = departmentdata.TreeWhere(t => t.OuterPhone.Contains(keyword), "DepartmentId");
  173. break;
  174. case "InnerPhone": //分机号
  175. departmentdata = departmentdata.TreeWhere(t => t.Manager.Contains(keyword), "DepartmentId");
  176. break;
  177. default:
  178. break;
  179. }
  180. #endregion
  181. }
  182. var treeList = new List<TreeGridEntity>();
  183. foreach (OrganizeEntity item in organizedata)
  184. {
  185. TreeGridEntity tree = new TreeGridEntity();
  186. bool hasChildren = organizedata.Count(t => t.ParentId == item.OrganizeId) == 0 ? false : true;
  187. //if (hasChildren == false)
  188. //{
  189. // hasChildren = departmentdata.Count(t => t.OrganizeId == item.OrganizeId) == 0 ? false : true;
  190. // if (hasChildren == false)
  191. // {
  192. // continue;
  193. // }
  194. //}
  195. tree.id = item.OrganizeId;
  196. tree.hasChildren = hasChildren;
  197. tree.parentId = item.ParentId;
  198. tree.expanded = true;
  199. item.EnCode = ""; item.ShortName = ""; item.Nature = ""; item.Manager = ""; item.OuterPhone = ""; item.InnerPhone = ""; item.Description = "";
  200. string itemJson = item.ToJson();
  201. itemJson = itemJson.Insert(1, "\"DepartmentId\":\"" + item.OrganizeId + "\",");
  202. itemJson = itemJson.Insert(1, "\"Sort\":\"Organize\",");
  203. tree.entityJson = itemJson;
  204. treeList.Add(tree);
  205. }
  206. foreach (DepartmentEntity item in departmentdata)
  207. {
  208. TreeGridEntity tree = new TreeGridEntity();
  209. bool hasChildren = organizedata.Count(t => t.ParentId == item.DepartmentId) == 0 ? false : true;
  210. tree.id = item.DepartmentId;
  211. if (item.ParentId == "0")
  212. {
  213. tree.parentId = item.OrganizeId;
  214. }
  215. else
  216. {
  217. tree.parentId = item.ParentId;
  218. }
  219. tree.expanded = true;
  220. tree.hasChildren = hasChildren;
  221. string itemJson = item.ToJson();
  222. itemJson = itemJson.Insert(1, "\"Sort\":\"Department\",");
  223. tree.entityJson = itemJson;
  224. treeList.Add(tree);
  225. }
  226. return Content(treeList.TreeJson());
  227. }
  228. /// <summary>
  229. /// 部门实体
  230. /// </summary>
  231. /// <param name="keyValue">主键值</param>
  232. /// <returns>返回对象Json</returns>
  233. [HttpGet]
  234. public ActionResult GetFormJson(string keyValue)
  235. {
  236. var data = departmentBLL.GetEntity(keyValue);
  237. return Content(data.ToJson());
  238. }
  239. #endregion
  240. #region 验证数据
  241. /// <summary>
  242. /// 部门编号不能重复
  243. /// </summary>
  244. /// <param name="enCode">编号</param>
  245. /// <param name="keyValue">主键</param>
  246. /// <returns></returns>
  247. [HttpGet]
  248. public ActionResult ExistEnCode(string EnCode, string keyValue)
  249. {
  250. bool IsOk = departmentBLL.ExistEnCode(EnCode, keyValue);
  251. return Content(IsOk.ToString());
  252. }
  253. /// <summary>
  254. /// 部门名称不能重复
  255. /// </summary>
  256. /// <param name="FullName">名称</param>
  257. /// <param name="keyValue">主键</param>
  258. /// <returns></returns>
  259. [HttpGet]
  260. public ActionResult ExistFullName(string FullName, string keyValue)
  261. {
  262. bool IsOk = departmentBLL.ExistFullName(FullName, keyValue);
  263. return Content(IsOk.ToString());
  264. }
  265. #endregion
  266. #region 提交数据
  267. /// <summary>
  268. /// 删除部门
  269. /// </summary>
  270. /// <param name="keyValue">主键值</param>
  271. /// <returns></returns>
  272. [HttpPost]
  273. [ValidateAntiForgeryToken]
  274. [AjaxOnly]
  275. [HandlerAuthorize(PermissionMode.Enforce)]
  276. public ActionResult RemoveForm(string keyValue)
  277. {
  278. departmentBLL.RemoveForm(keyValue);
  279. return Success("删除成功。");
  280. }
  281. /// <summary>
  282. /// 保存部门表单(新增、修改)
  283. /// </summary>
  284. /// <param name="keyValue">主键值</param>
  285. /// <param name="departmentEntity">部门实体</param>
  286. /// <returns></returns>
  287. [HttpPost]
  288. [ValidateAntiForgeryToken]
  289. [AjaxOnly]
  290. public ActionResult SaveForm(string keyValue, DepartmentEntity departmentEntity)
  291. {
  292. List< DepartmentEntity> listPart=departmentBLL.GetList().ToList();
  293. //修改操作
  294. if (keyValue!="")
  295. {
  296. if (listPart.Where(a => a.IsBusinessAddress == 1 && a.BusinessNum == departmentEntity.BusinessNum && a.DepartmentId != departmentEntity.DepartmentId&&a.DeleteMark==0).ToList().Count > 0)
  297. {
  298. return Error("已存在该营业厅");
  299. }
  300. }
  301. else
  302. {//新增操作
  303. if (listPart.Where(a => a.IsBusinessAddress == 1 && a.BusinessNum == departmentEntity.BusinessNum && a.DeleteMark == 0).ToList().Count > 0)
  304. {
  305. return Error("已存在该营业厅");
  306. }
  307. }
  308. departmentBLL.SaveForm(keyValue, departmentEntity);
  309. return Success("操作成功。");
  310. }
  311. #endregion
  312. }
  313. }