RoleController.cs 15 KB

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