RoleController.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using EMIS.Web.Controls;
  7. using EMIS.CommonLogic.SystemServices;
  8. using EMIS.ViewModel;
  9. using EMIS.ViewModel.SystemView;
  10. using Bowin.Common.Data;
  11. using Bowin.Common.JSON;
  12. using Bowin.Web.Controls.Mvc;
  13. using Bowin.Common.Utility;
  14. using System.ComponentModel;
  15. using System.Collections.Concurrent;
  16. using Bowin.Common.Exceptions;
  17. namespace EMIS.Web.Controllers.UserManagement
  18. {
  19. [Authorization]
  20. public class RoleController : Controller
  21. {
  22. public Lazy<IRoleServices> RoleServices { get; set; }
  23. public Lazy<IFunctionCodeServices> FunctionCodeServices { get; set; }
  24. /// <summary>
  25. /// 教师角色页面
  26. /// </summary>
  27. /// <returns></returns>
  28. public ActionResult List()
  29. {
  30. return View();
  31. }
  32. /// <summary>
  33. /// 列表查询
  34. /// </summary>
  35. /// <param name="pararms"></param>
  36. /// <returns></returns>
  37. [HttpPost]
  38. public ActionResult List(QueryParamsModel pararms)
  39. {
  40. ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(pararms);
  41. //避开全选值
  42. var dataRange = pararms.getExtraInt("DataRange");
  43. if (dataRange == DropdownList.SELECT_ALL) dataRange = null;
  44. if (configuretView.Attribute == DropdownList.SELECT_ALL.ToString()) configuretView.Attribute = "";
  45. return base.Json(RoleServices.Value.GetRoleViewList(configuretView, dataRange, (int)pararms.page, (int)pararms.rows));
  46. }
  47. /// <summary>
  48. /// 学生角色页面
  49. /// </summary>
  50. /// <returns></returns>
  51. public ActionResult StudentList()
  52. {
  53. return View();
  54. }
  55. /// <summary>
  56. /// 列表查询
  57. /// </summary>
  58. /// <param name="pararms"></param>
  59. /// <returns></returns>
  60. [HttpPost]
  61. public ActionResult StudentList(QueryParamsModel pararms)
  62. {
  63. return base.Json(RoleServices.Value.GetStudentRoleViewList((int)pararms.page, (int)pararms.rows));
  64. }
  65. /// <summary>
  66. /// 编辑页面
  67. /// </summary>
  68. /// <returns></returns>
  69. [HttpGet]
  70. public ActionResult Edit(Guid? roleID)
  71. {
  72. RoleView roleView = new RoleView();
  73. if (roleID != null && roleID != Guid.Empty)
  74. {
  75. roleView = RoleServices.Value.GetRoleViewInfo(roleID);
  76. }
  77. return View(roleView);
  78. }
  79. /// <summary>
  80. /// 编辑页面
  81. /// </summary>
  82. /// <returns></returns>
  83. [HttpGet]
  84. public ActionResult StudentEdit(Guid? roleID)
  85. {
  86. RoleView departmentView = new RoleView();
  87. if (roleID != null && roleID != Guid.Empty)
  88. {
  89. departmentView = RoleServices.Value.GetRoleViewInfo(roleID);
  90. }
  91. return View(departmentView);
  92. }
  93. /// <summary>
  94. /// 新增
  95. /// </summary>
  96. /// <returns></returns>
  97. [HttpPost]
  98. public ActionResult Edit(RoleView roleView)
  99. {
  100. try
  101. {
  102. var user = HttpContext.User as EMIS.Utility.FormValidate.CustomPrincipal;
  103. roleView.TypeID = (int)SYS_RoleType.Teacher;
  104. RoleServices.Value.Save(roleView);
  105. return Json(new ReturnMessage()
  106. {
  107. IsSuccess = true,
  108. Message = "保存成功!"
  109. });
  110. }
  111. catch (Exception ex)
  112. {
  113. return Json(new ReturnMessage()
  114. {
  115. IsSuccess = false,
  116. Message = "保存失败:" + ex.Message
  117. });
  118. }
  119. }
  120. /// <summary>
  121. /// 新增
  122. /// </summary>
  123. /// <returns></returns>
  124. [HttpPost]
  125. public ActionResult StudentEdit(RoleView roleView)
  126. {
  127. try
  128. {
  129. var user = HttpContext.User as EMIS.Utility.FormValidate.CustomPrincipal;
  130. roleView.TypeID = (int)SYS_RoleType.Student;
  131. RoleServices.Value.Save(roleView);
  132. return Json(new ReturnMessage()
  133. {
  134. IsSuccess = true,
  135. Message = "保存成功!"
  136. });
  137. }
  138. catch (Exception ex)
  139. {
  140. return Json(new ReturnMessage()
  141. {
  142. IsSuccess = false,
  143. Message = "保存失败:" + ex.Message
  144. });
  145. }
  146. }
  147. /// <summary>
  148. /// 删除
  149. /// </summary>
  150. /// <param name="roleID"></param>
  151. /// <returns></returns>
  152. [HttpPost]
  153. public ActionResult Delete(string roleIDs)
  154. {
  155. try
  156. {
  157. var roleIDList = roleIDs.Split(',').Select(x => (Guid?)new Guid(x)).ToList();
  158. RoleServices.Value.Delete(roleIDList);
  159. return base.Json("删除成功!");
  160. }
  161. catch (Exception ex)
  162. {
  163. return base.Json("删除失败,原因:" + ex.Message + "。");
  164. }
  165. }
  166. /// <summary>
  167. /// 教师角色授权
  168. /// </summary>
  169. /// <param name="roleID"></param>
  170. /// <returns></returns>
  171. public ActionResult Authenization(Guid roleID)
  172. {
  173. return View();
  174. }
  175. /// <summary>
  176. /// 学生角色授权
  177. /// </summary>
  178. /// <param name="roleID"></param>
  179. /// <returns></returns>
  180. public ActionResult StudentAuthenization(Guid roleID)
  181. {
  182. return View();
  183. }
  184. [HttpPost]
  185. public ActionResult GetAuthenizationTree(Guid roleID)
  186. {
  187. var allFunctionCode = FunctionCodeServices.Value.GetAllFunctionCodeViewList().Where(x => x.FunctionCode != "FunStu").ToList();
  188. var selectedFunctionCode = FunctionCodeServices.Value.GetSelectFunctionCodeViewList(roleID);
  189. var topTree = allFunctionCode.Where(x => string.IsNullOrEmpty(x.ParentFunctionCode))
  190. .OrderBy(x => x.OrderNo)
  191. .Select(x => new TreeItem
  192. {
  193. id = x.FunctionCode,
  194. text = x.FunctionName,
  195. @checked = selectedFunctionCode.Any(w => w.FunctionCode.Trim() == x.FunctionCode.Trim())
  196. }).ToList();
  197. topTree.ForEach(x => BindAuthenizationTreeChildren(allFunctionCode, selectedFunctionCode, x));
  198. return Json(topTree);
  199. }
  200. [HttpPost]
  201. public ActionResult GetStudentAuthenizationTree(Guid roleID)
  202. {
  203. var allFunctionCode = FunctionCodeServices.Value.GetAllFunctionCodeViewList().Where(x => x.FunctionCode != "Fun").ToList();
  204. var selectedFunctionCode = FunctionCodeServices.Value.GetSelectFunctionCodeViewList(roleID);
  205. var topTree = allFunctionCode.Where(x => string.IsNullOrEmpty(x.ParentFunctionCode))
  206. .OrderBy(x => x.OrderNo)
  207. .Select(x => new TreeItem
  208. {
  209. id = x.FunctionCode,
  210. text = x.FunctionName,
  211. @checked = selectedFunctionCode.Any(w => w.FunctionCode.Trim() == x.FunctionCode.Trim())
  212. }).ToList();
  213. topTree.ForEach(x => BindAuthenizationTreeChildren(allFunctionCode, selectedFunctionCode, x));
  214. return Json(topTree);
  215. }
  216. private void BindAuthenizationTreeChildren(IList<FunctionCodeView> allFunctionCode, IList<FunctionCodeView> selectedFunctionCode, TreeItem treeItem)
  217. {
  218. try
  219. {
  220. treeItem.children = allFunctionCode.Where(x => (x.ParentFunctionCode ?? "").Trim() == treeItem.id.Trim())
  221. .OrderBy(x => x.OrderNo)
  222. .Select(x => new TreeItem
  223. {
  224. id = x.FunctionCode,
  225. text = x.FunctionName,
  226. @checked = selectedFunctionCode.Any(w => w.FunctionCode.Trim() == x.FunctionCode.Trim())
  227. }).ToList();
  228. }
  229. catch (Exception ex)
  230. {
  231. throw ex;
  232. }
  233. treeItem.children.ForEach(x => BindAuthenizationTreeChildren(allFunctionCode, selectedFunctionCode, x));
  234. }
  235. [HttpPost]
  236. public ActionResult SaveAuthenization(Guid roleID, string functionCodeList)
  237. {
  238. try
  239. {
  240. var functionCodes = functionCodeList.Split(',').ToList();
  241. FunctionCodeServices.Value.SaveAuthenization(roleID, functionCodes);
  242. return Json(new ReturnMessage
  243. {
  244. IsSuccess = true,
  245. Message = "保存成功"
  246. });
  247. }
  248. catch (Exception ex)
  249. {
  250. return Json(new ReturnMessage
  251. {
  252. IsSuccess = false,
  253. Message = "保存失败!" + ex.Message
  254. });
  255. }
  256. }
  257. /// <summary>
  258. /// 数据范围设置
  259. /// </summary>
  260. /// <param name="roleID"></param>
  261. /// <returns></returns>
  262. public ActionResult DataRange(Guid roleID)
  263. {
  264. return View();
  265. }
  266. [HttpPost]
  267. public ActionResult DataRangeAll(Guid roleID)
  268. {
  269. var rangeData = this.RoleServices.Value.GetRoleDataRange(roleID);
  270. return Json(new JsonDataGridResult<RoleDataRangeView> { rows = rangeData, total = rangeData.Count });
  271. }
  272. [HttpPost]
  273. public ActionResult SaveDataRange(Guid roleID, string dataRangeJson)
  274. {
  275. try
  276. {
  277. RoleDataRangeSaveView data = new RoleDataRangeSaveView();
  278. data.RoleID = roleID;
  279. data.DataRangeViewList = dataRangeJson.JsonToObject<List<RoleDataRangeView>>();
  280. data.DataRangeViewList.RemoveAll(x => (x.DataRangeID ?? DropdownList.SELECT_ALL) == DropdownList.SELECT_ALL);
  281. data.DataRangeViewList.ForEach(x => x.RoleID = roleID);
  282. this.RoleServices.Value.SaveDataRange(data.RoleID, data.DataRangeViewList);
  283. return Json(new ReturnMessage
  284. {
  285. IsSuccess = true,
  286. Message = "保存成功"
  287. });
  288. }
  289. catch (Exception ex)
  290. {
  291. return Json(new ReturnMessage
  292. {
  293. IsSuccess = false,
  294. Message = "保存失败!" + ex.Message
  295. });
  296. }
  297. }
  298. [HttpPost]
  299. public ActionResult RoleDropdownList(DropdownListBindType? bindType)
  300. {
  301. List<DropdownListItem> list = RoleServices.Value.GetRoleViewList(new ConfiguretView(), null).Select(x => new DropdownListItem { Text = x.RoleName, Value = x.RoleID.ToString() }).ToList();
  302. DropdownListBindType dbt = bindType == null ? DropdownListBindType.SelectAll : bindType.Value;
  303. DropdownList.FormatDropdownItemList(dbt, list);
  304. return base.Json(list);
  305. }
  306. //[HttpPost]
  307. //public ActionResult SysRoleDropdownList(DropdownListBindType? bindType)
  308. //{
  309. // var enumType = typeof(SYS_RoleType);
  310. // var list = EnumHelper.EnumToDictionary(enumType).Select(s => new DropdownListItem { Text = s.Value, Value = s.Key }).ToList();
  311. // DropdownListBindType dbt = bindType == null ? DropdownListBindType.SelectAll : bindType.Value;
  312. // DropdownList.FormatDropdownItemList(dbt, list);
  313. // return base.Json(list);
  314. //}
  315. [HttpPost]
  316. public ActionResult StudentRoleDropdownList(DropdownListBindType? bindType)
  317. {
  318. List<DropdownListItem> list = RoleServices.Value.GetStudentRoleViewList().Select(x => new DropdownListItem { Text = x.RoleName, Value = x.RoleID.ToString() }).ToList();
  319. DropdownListBindType dbt = bindType == null ? DropdownListBindType.SelectAll : bindType.Value;
  320. DropdownList.FormatDropdownItemList(dbt, list);
  321. return base.Json(list);
  322. }
  323. [HttpPost]
  324. public ActionResult ListAll()
  325. {
  326. var list = RoleServices.Value.GetEnabledRoleViewList();
  327. return base.Json(new JsonDataGridResult<RoleView> { rows = list, total = list.Count });
  328. }
  329. [HttpPost]
  330. public ActionResult StudentVerification(Guid? roleID, int studentTypeID, string roleName)
  331. {
  332. try
  333. {
  334. RoleServices.Value.StudentVerification(roleID ?? Guid.Empty, studentTypeID, roleName);
  335. return base.Json("成功");
  336. }
  337. catch (Exception ex)
  338. {
  339. return base.Json("保存失败," + ex.Message);
  340. }
  341. }
  342. /// <summary>
  343. ///
  344. /// </summary>
  345. /// <param name="pararms"></param>
  346. /// <returns></returns>
  347. [HttpPost]
  348. public ActionResult StudentExcel(QueryParamsModel pararms)
  349. {
  350. NpoiExcelHelper neh = new NpoiExcelHelper();
  351. var dt = RoleServices.Value.GetStudentRoleViewList().Select(x => new
  352. {
  353. x.RoleName,
  354. x.Description
  355. }).ToTable();
  356. string[] liststring = { "类型名称", "备注" };
  357. neh.Export(dt, liststring, "学生角色信息");
  358. return RedirectToAction("MsgShow", "Common", new
  359. {
  360. msg = "导出成功!",
  361. url = Url.Content("~/Role/StudentList").AddMenuParameter()
  362. });
  363. }
  364. /// <summary>
  365. ///
  366. /// </summary>
  367. /// <returns></returns>
  368. [HttpPost]
  369. public ActionResult Excel()
  370. {
  371. NpoiExcelHelper neh = new NpoiExcelHelper();
  372. ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(null);
  373. //避开全选值
  374. var dataRange = Request.Form["DataRange"].ParseStrTo<int>();
  375. if (dataRange == DropdownList.SELECT_ALL) dataRange = null;
  376. if (configuretView.Attribute == DropdownList.SELECT_ALL.ToString()) configuretView.Attribute = "";
  377. var dt = RoleServices.Value.GetRoleViewList(configuretView, dataRange).Select(x => new
  378. {
  379. x.RoleName,
  380. x.DefaultDataRangeDesc,
  381. x.RecordStatusDesc,
  382. x.Description
  383. }).ToTable();
  384. string[] liststring = { "类型名称", "数据范围", "是否可用", "备注" };
  385. neh.Export(dt, liststring, "用户类型信息");
  386. return RedirectToAction("MsgShow", "Common", new
  387. {
  388. msg = "导出成功!",
  389. url = Url.Content("~/Role/List").AddMenuParameter()
  390. });
  391. }
  392. }
  393. }