using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using EMIS.Web.Controls; using EMIS.CommonLogic.SystemServices; using EMIS.ViewModel; using EMIS.ViewModel.SystemView; using Bowin.Common.Data; using Bowin.Common.JSON; using Bowin.Web.Controls.Mvc; using Bowin.Common.Utility; using System.ComponentModel; using System.Collections.Concurrent; using Bowin.Common.Exceptions; namespace EMIS.Web.Controllers.UserManagement { [Authorization] public class RoleController : Controller { public Lazy RoleServices { get; set; } public Lazy FunctionCodeServices { get; set; } /// /// 教师角色页面 /// /// public ActionResult List() { return View(); } /// /// 列表查询 /// /// /// [HttpPost] public ActionResult List(QueryParamsModel pararms) { ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(pararms); //避开全选值 var dataRange = pararms.getExtraInt("DataRange"); if (dataRange == DropdownList.SELECT_ALL) dataRange = null; if (configuretView.Attribute == DropdownList.SELECT_ALL.ToString()) configuretView.Attribute = ""; return base.Json(RoleServices.Value.GetRoleViewList(configuretView, dataRange, (int)pararms.page, (int)pararms.rows)); } /// /// 学生角色页面 /// /// public ActionResult StudentList() { return View(); } /// /// 列表查询 /// /// /// [HttpPost] public ActionResult StudentList(QueryParamsModel pararms) { return base.Json(RoleServices.Value.GetStudentRoleViewList((int)pararms.page, (int)pararms.rows)); } /// /// 编辑页面 /// /// [HttpGet] public ActionResult Edit(Guid? roleID) { RoleView roleView = new RoleView(); if (roleID != null && roleID != Guid.Empty) { roleView = RoleServices.Value.GetRoleViewInfo(roleID); } return View(roleView); } /// /// 编辑页面 /// /// [HttpGet] public ActionResult StudentEdit(Guid? roleID) { RoleView departmentView = new RoleView(); if (roleID != null && roleID != Guid.Empty) { departmentView = RoleServices.Value.GetRoleViewInfo(roleID); } return View(departmentView); } /// /// 新增 /// /// [HttpPost] public ActionResult Edit(RoleView roleView) { try { var user = HttpContext.User as EMIS.Utility.FormValidate.CustomPrincipal; roleView.TypeID = (int)SYS_RoleType.Teacher; RoleServices.Value.Save(roleView); return Json(new ReturnMessage() { IsSuccess = true, Message = "保存成功!" }); } catch (Exception ex) { return Json(new ReturnMessage() { IsSuccess = false, Message = "保存失败:" + ex.Message }); } } /// /// 新增 /// /// [HttpPost] public ActionResult StudentEdit(RoleView roleView) { try { var user = HttpContext.User as EMIS.Utility.FormValidate.CustomPrincipal; roleView.TypeID = (int)SYS_RoleType.Student; RoleServices.Value.Save(roleView); return Json(new ReturnMessage() { IsSuccess = true, Message = "保存成功!" }); } catch (Exception ex) { return Json(new ReturnMessage() { IsSuccess = false, Message = "保存失败:" + ex.Message }); } } /// /// 删除 /// /// /// [HttpPost] public ActionResult Delete(string roleIDs) { try { var roleIDList = roleIDs.Split(',').Select(x => (Guid?)new Guid(x)).ToList(); RoleServices.Value.Delete(roleIDList); return base.Json("删除成功!"); } catch (Exception ex) { return base.Json("删除失败,原因:" + ex.Message + "。"); } } /// /// 教师角色授权 /// /// /// public ActionResult Authenization(Guid roleID) { return View(); } /// /// 学生角色授权 /// /// /// public ActionResult StudentAuthenization(Guid roleID) { return View(); } [HttpPost] public ActionResult GetAuthenizationTree(Guid roleID) { var allFunctionCode = FunctionCodeServices.Value.GetAllFunctionCodeViewList().Where(x => x.FunctionCode != "FunStu").ToList(); var selectedFunctionCode = FunctionCodeServices.Value.GetSelectFunctionCodeViewList(roleID); var topTree = allFunctionCode.Where(x => string.IsNullOrEmpty(x.ParentFunctionCode)) .OrderBy(x => x.OrderNo) .Select(x => new TreeItem { id = x.FunctionCode, text = x.FunctionName, @checked = selectedFunctionCode.Any(w => w.FunctionCode.Trim() == x.FunctionCode.Trim()) }).ToList(); topTree.ForEach(x => BindAuthenizationTreeChildren(allFunctionCode, selectedFunctionCode, x)); return Json(topTree); } [HttpPost] public ActionResult GetStudentAuthenizationTree(Guid roleID) { var allFunctionCode = FunctionCodeServices.Value.GetAllFunctionCodeViewList().Where(x => x.FunctionCode != "Fun").ToList(); var selectedFunctionCode = FunctionCodeServices.Value.GetSelectFunctionCodeViewList(roleID); var topTree = allFunctionCode.Where(x => string.IsNullOrEmpty(x.ParentFunctionCode)) .OrderBy(x => x.OrderNo) .Select(x => new TreeItem { id = x.FunctionCode, text = x.FunctionName, @checked = selectedFunctionCode.Any(w => w.FunctionCode.Trim() == x.FunctionCode.Trim()) }).ToList(); topTree.ForEach(x => BindAuthenizationTreeChildren(allFunctionCode, selectedFunctionCode, x)); return Json(topTree); } private void BindAuthenizationTreeChildren(IList allFunctionCode, IList selectedFunctionCode, TreeItem treeItem) { try { treeItem.children = allFunctionCode.Where(x => (x.ParentFunctionCode ?? "").Trim() == treeItem.id.Trim()) .OrderBy(x => x.OrderNo) .Select(x => new TreeItem { id = x.FunctionCode, text = x.FunctionName, @checked = selectedFunctionCode.Any(w => w.FunctionCode.Trim() == x.FunctionCode.Trim()) }).ToList(); } catch (Exception ex) { throw ex; } treeItem.children.ForEach(x => BindAuthenizationTreeChildren(allFunctionCode, selectedFunctionCode, x)); } [HttpPost] public ActionResult SaveAuthenization(Guid roleID, string functionCodeList) { try { var functionCodes = functionCodeList.Split(',').ToList(); FunctionCodeServices.Value.SaveAuthenization(roleID, functionCodes); return Json(new ReturnMessage { IsSuccess = true, Message = "保存成功" }); } catch (Exception ex) { return Json(new ReturnMessage { IsSuccess = false, Message = "保存失败!" + ex.Message }); } } /// /// 数据范围设置 /// /// /// public ActionResult DataRange(Guid roleID) { return View(); } [HttpPost] public ActionResult DataRangeAll(Guid roleID) { var rangeData = this.RoleServices.Value.GetRoleDataRange(roleID); return Json(new JsonDataGridResult { rows = rangeData, total = rangeData.Count }); } [HttpPost] public ActionResult SaveDataRange(Guid roleID, string dataRangeJson) { try { RoleDataRangeSaveView data = new RoleDataRangeSaveView(); data.RoleID = roleID; data.DataRangeViewList = dataRangeJson.JsonToObject>(); data.DataRangeViewList.RemoveAll(x => (x.DataRangeID ?? DropdownList.SELECT_ALL) == DropdownList.SELECT_ALL); data.DataRangeViewList.ForEach(x => x.RoleID = roleID); this.RoleServices.Value.SaveDataRange(data.RoleID, data.DataRangeViewList); return Json(new ReturnMessage { IsSuccess = true, Message = "保存成功" }); } catch (Exception ex) { return Json(new ReturnMessage { IsSuccess = false, Message = "保存失败!" + ex.Message }); } } [HttpPost] public ActionResult RoleDropdownList(DropdownListBindType? bindType) { List list = RoleServices.Value.GetRoleViewList(new ConfiguretView(), null).Select(x => new DropdownListItem { Text = x.RoleName, Value = x.RoleID.ToString() }).ToList(); DropdownListBindType dbt = bindType == null ? DropdownListBindType.SelectAll : bindType.Value; DropdownList.FormatDropdownItemList(dbt, list); return base.Json(list); } //[HttpPost] //public ActionResult SysRoleDropdownList(DropdownListBindType? bindType) //{ // var enumType = typeof(SYS_RoleType); // var list = EnumHelper.EnumToDictionary(enumType).Select(s => new DropdownListItem { Text = s.Value, Value = s.Key }).ToList(); // DropdownListBindType dbt = bindType == null ? DropdownListBindType.SelectAll : bindType.Value; // DropdownList.FormatDropdownItemList(dbt, list); // return base.Json(list); //} [HttpPost] public ActionResult StudentRoleDropdownList(DropdownListBindType? bindType) { List list = RoleServices.Value.GetStudentRoleViewList().Select(x => new DropdownListItem { Text = x.RoleName, Value = x.RoleID.ToString() }).ToList(); DropdownListBindType dbt = bindType == null ? DropdownListBindType.SelectAll : bindType.Value; DropdownList.FormatDropdownItemList(dbt, list); return base.Json(list); } [HttpPost] public ActionResult ListAll() { var list = RoleServices.Value.GetEnabledRoleViewList(); return base.Json(new JsonDataGridResult { rows = list, total = list.Count }); } [HttpPost] public ActionResult StudentVerification(Guid? roleID, int studentTypeID, string roleName) { try { RoleServices.Value.StudentVerification(roleID ?? Guid.Empty, studentTypeID, roleName); return base.Json("成功"); } catch (Exception ex) { return base.Json("保存失败," + ex.Message); } } /// /// /// /// /// [HttpPost] public ActionResult StudentExcel(QueryParamsModel pararms) { NpoiExcelHelper neh = new NpoiExcelHelper(); var dt = RoleServices.Value.GetStudentRoleViewList().Select(x => new { x.RoleName, x.Description }).ToTable(); string[] liststring = { "类型名称", "备注" }; neh.Export(dt, liststring, "学生角色信息"); return RedirectToAction("MsgShow", "Common", new { msg = "导出成功!", url = Url.Content("~/Role/StudentList").AddMenuParameter() }); } /// /// /// /// [HttpPost] public ActionResult Excel() { NpoiExcelHelper neh = new NpoiExcelHelper(); ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(null); //避开全选值 var dataRange = Request.Form["DataRange"].ParseStrTo(); if (dataRange == DropdownList.SELECT_ALL) dataRange = null; if (configuretView.Attribute == DropdownList.SELECT_ALL.ToString()) configuretView.Attribute = ""; var dt = RoleServices.Value.GetRoleViewList(configuretView, dataRange).Select(x => new { x.RoleName, x.DefaultDataRangeDesc, x.RecordStatusDesc, x.Description }).ToTable(); string[] liststring = { "类型名称", "数据范围", "是否可用", "备注" }; neh.Export(dt, liststring, "用户类型信息"); return RedirectToAction("MsgShow", "Common", new { msg = "导出成功!", url = Url.Content("~/Role/List").AddMenuParameter() }); } } }