using System; using System.Collections.Generic; using System.Linq; using System.Web.Mvc; using Bowin.Common.Data; using Bowin.Common.Exceptions; using Bowin.Common.Utility; using Bowin.Web.Controls.Mvc; using EMIS.CommonLogic.AdministrativeOrgan; using EMIS.CommonLogic.SystemServices; using EMIS.CommonLogic.TeacherManagement; using EMIS.Utility; using EMIS.ViewModel; using EMIS.ViewModel.TeacherManagement; using EMIS.Web.Controls; using System.Web; using System.IO; using System.Text; using Bowin.Common.DataTime; namespace EMIS.Web.Controllers.UserManagement { [Authorization] public class StaffController : Controller { public Lazy staffServices { get; set; } public Lazy userServices { get; set; } public Lazy campusServices { get; set; } public Lazy collegeServices { get; set; } public Lazy departmentServices { get; set; } /// /// 教师信息页面 /// /// public ActionResult List() { return View(); } /// /// 列表查询 /// /// /// [HttpPost] public ActionResult List(QueryParamsModel pararms) { ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(pararms); var userID = pararms.getExtraGuid("SaffDropdown"); var campusID = pararms.getExtraGuid("CampusDropdown"); var collegeID = pararms.getExtraGuid("CollegeDropdown"); var departmentID = pararms.getExtraGuid("DepartmentDropdown"); var isphotoUrl = pararms.getExtraInt("PhotoUrltmentDropdown") == DropdownList.SELECT_ALL ? null : pararms.getExtraInt("PhotoUrltmentDropdown"); return base.Json(staffServices.Value.GetStaffViewGrid(configuretView, campusID, collegeID, departmentID, isphotoUrl, userID, (int)pararms.page, (int)pararms.rows)); } /// /// 列表查询 /// /// /// [HttpPost] public ActionResult ListWithNoDataRange(QueryParamsModel pararms) { ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(pararms); var userID = pararms.getExtraGuid("SaffDropdown"); var campusID = pararms.getExtraGuid("CampusDropdown"); var collegeID = pararms.getExtraGuid("CollegeDropdown"); var departmentID = pararms.getExtraGuid("DepartmentDropdown"); var isphotoUrl = pararms.getExtraInt("PhotoUrltmentDropdown") == DropdownList.SELECT_ALL ? null : pararms.getExtraInt("PhotoUrltmentDropdown"); return base.Json(staffServices.Value.GetStaffViewGridWithNoDataRange(configuretView, campusID, collegeID, departmentID, isphotoUrl, userID, (int)pararms.page, (int)pararms.rows)); } /// /// /// /// /// [HttpPost] public ActionResult ListForTeacherEdit(QueryParamsModel pararms) { var campusID = pararms.getExtraGuid("CampusDropdown"); var collegeID = pararms.getExtraGuid("CollegeDropdown"); var loginID = pararms.getExtraString("LoginID"); var name = pararms.getExtraString("Name"); return base.Json(staffServices.Value.GetStaffViewGrid(campusID, collegeID, loginID, name, (int)pararms.page, (int)pararms.rows)); } /// /// 编辑页面 /// /// [HttpGet] public ActionResult Edit(Guid? userID) { ViewBag.Type = Request.Params["Type"];//临时性加参数控制、屏蔽列表进入无保存按钮权限 StaffView staffView = new StaffView(); if (userID.HasValue) staffView = staffServices.Value.GetStaffView(userID.Value); return View(staffView); } /// /// 新增 /// /// /// [HttpPost] public ActionResult Edit(StaffView staffView) { try { var accepts = new List { ".jpg" }; var postedFile = Request.Files["PhotoUrl"]; if (postedFile != null && !string.IsNullOrEmpty(postedFile.FileName) && !accepts.Contains(Path.GetExtension(postedFile.FileName).ToLower())) { throw new Exception("只允许上传.jpg格式的文件。"); } string photoUrl = FileUploadHelper.UploadFile(postedFile); staffServices.Value.StaffAdd(staffView, photoUrl); return RedirectToAction("MsgShow", "Common", new { WindowID = Request["WindowID"], msg = "保存成功。", url = Url.Action("List").AddMenuParameter() }); } catch (Exception ex) { return RedirectToAction("MsgShow", "Common", new { WindowID = Request["WindowID"], msg = "保存失败,原因:" + ex.Message + "。", url = Url.Action("List").AddMenuParameter() }); } } /// /// 编辑页面 /// /// [HttpGet] public ActionResult Edits(Guid? userID) { ViewBag.Type = Request.Params["Type"];//临时性加参数控制、屏蔽列表进入无保存按钮权限 StaffView staffView = new StaffView(); if (userID.HasValue) staffView = staffServices.Value.GetStaffView(userID.Value); return View(staffView); } /// /// 新增 /// /// /// [HttpPost] public ActionResult Edits(StaffView staffView) { try { var accepts = new List { ".jpg" }; var postedFile = Request.Files["PhotoUrl"]; if (postedFile != null && !string.IsNullOrEmpty(postedFile.FileName) && !accepts.Contains(Path.GetExtension(postedFile.FileName).ToLower())) { throw new Exception("只允许上传.jpg格式的文件。"); } string photoUrl = FileUploadHelper.UploadFile(postedFile); staffServices.Value.StaffAdd(staffView, photoUrl); return RedirectToAction("MsgShow", "Common", new { WindowID = Request["WindowID"], msg = "保存成功。", url = Url.Action("List").AddMenuParameter() }); } catch (Exception ex) { return RedirectToAction("MsgShow", "Common", new { WindowID = Request["WindowID"], msg = "保存失败,原因:" + ex.Message + "。", url = Url.Action("List").AddMenuParameter() }); } } /// /// 删除 /// /// /// [HttpPost] public ActionResult Delete(string userIDs) { try { List list = new List(); for (int i = 0; i < userIDs.Split(',').Length; i++) { string id = userIDs.Split(',')[i]; if (!string.IsNullOrEmpty(id)) { Guid userID = new Guid(id); list.Add(userID); } } staffServices.Value.StaffDelete(list); return base.Json("删除成功。"); } catch (Exception ex) { string mge = ex.Message; System.Data.SqlClient.SqlException num = ExceptionHelper.GetSqlException(ex); if (num != null) { if (num.Number == 547) mge = "请先删除所有关联的数据,如:班级信息、课程信息等"; } return base.Json("删除失败,原因:" + mge + "。"); } } /// /// 添加更新前验证 /// /// /// /// [HttpPost] public ActionResult Verification(Guid? userID, string Code, Guid? CollegeID, Guid? DepartmentID) { return Json(staffServices.Value.GetVerification(userID, Code, CollegeID, DepartmentID)); } [HttpPost] public ActionResult DeletePhoto(Guid userID) { try { this.staffServices.Value.DeletePhoto(userID); return Json(new ReturnMessage() { IsSuccess = true, Message = "删除成功。" }); } catch (Exception ex) { return Json(new ReturnMessage() { IsSuccess = false, Message = "删除失败:" + ex.Message }); } } /// /// 获取教师信息 /// [HttpPost] public ActionResult GetStaffDataBind(DropdownListBindType? bindType, Guid? userID) { List list = null; if (userID == null) list = new List(); else list = staffServices.Value.GetListStaffView(userID).Select(x => new DropdownListItem { Text = x.Name, Value = x.UserID.ToString() }).ToList(); DropdownListBindType dbt = bindType == null ? DropdownListBindType.SelectAll : bindType.Value; DropdownList.FormatDropdownItemList(dbt, list); return base.Json(list); } /// /// 获取教师信息 /// [HttpPost] public ActionResult GetAllStaffDataBind(QueryParamsModel pararms) { ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(pararms); return base.Json(staffServices.Value.GetAllStaffView(configuretView, (int)pararms.page, (int)pararms.rows)); } /// /// 获取教师信息 /// [HttpPost] public ActionResult GetDepartmentStaffDataBind(DropdownListBindType? bindType, Guid? departmentID) { List list = null; if (departmentID == null) list = new List(); else list = staffServices.Value.GetStaffViewList(new ConfiguretView(), null, null, departmentID, null) .Select(x => new DropdownListItem { Text = x.Name, Value = x.UserID.ToString() }).ToList(); DropdownListBindType dbt = bindType == null ? DropdownListBindType.SelectAll : bindType.Value; DropdownList.FormatDropdownItemList(dbt, list); return base.Json(list); } /// /// 教师信息Excel导入 /// /// /// /// [HttpGet] public ActionResult Import(string errorFile, string operationTips) { ViewBag.ErrorFile = errorFile; if (string.IsNullOrEmpty(operationTips)) { operationTips = "错误数据下载"; } ViewBag.operationTips = operationTips; return View(); } [HttpPost] public ActionResult Import(HttpPostedFileBase file) { try { //1.0 HSSFWorkbook:是操作Excel2003以前(包括2003)的版本,扩展名是.xls //2.0 XSSFWorkbook:是操作Excel2007的版本,扩展名是.xlsx if (!NpoiExcelHelper.GetIsCompatible(file.FileName)) { throw new Exception("只允许上传xls和xlsx格式的Excel文件!"); } StringBuilder errorMsg = new StringBuilder(); // 错误信息 string sourceWebPath = FileUploadHelper.UploadFile(file); var sourcePhysicalPath = Server.MapPath(sourceWebPath); List errList = new List(); int errCount = 0;//失败条数 int OkCount = 0;//成功条数 string errorWebPath = ""; string Errinfo = ""; staffServices.Value.StaffImport(out errCount, out errList, out OkCount, sourcePhysicalPath,sourceWebPath,out errorWebPath,out Errinfo); System.IO.File.Delete(sourcePhysicalPath);//删除本地缓存文件 if (errCount > 0) { ViewBag.ErrorFile = errorWebPath; ViewBag.operationTips = Errinfo; return RedirectToAction("MsgShow", "Common", new { WindowID = "none", msg = "导入失败!", url = Url.Action("Import").AddMenuParameter() + "&errorFile=" + errorWebPath + "&operationTips=" + Errinfo + "&WindowID=" + Request["WindowID"] }); } else { return RedirectToAction("MsgShow", "Common", new { WindowID = Request["WindowID"], msg = "导入成功!", url = Url.Action("List").AddMenuParameter() }); } } catch (Exception ex) { return RedirectToAction("MsgShow", "Common", new { WindowID = Request["WindowID"], msg = "导入失败,原因:" +ex.Message + "!", url = Url.Action("List").AddMenuParameter() }); } } /// /// Excel导出 /// /// /// [HttpPost] public ActionResult Excel() { NpoiExcelHelper neh = new NpoiExcelHelper(); ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(null); var campusID = Request.Form["CampusDropdown"].ParseStrTo(); var collegeID = Request.Form["CollegeDropdown"].ParseStrTo(); var departmentID = Request.Form["DepartmentDropdown"].ParseStrTo(); var isphotoUrl = Request.Form["ChekBoxPhotoUrl"].ParseStrTo(); var dt = staffServices.Value.GetStaffViewList(configuretView, campusID, collegeID, departmentID, isphotoUrl).Select(x => new { x.StaffCode, x.Name, x.CampusCode, x.CampusName, x.CollegeNo, x.CollegeName, x.DepartmentCode, x.DepartmentName, BirthDate = x.BirthDate.HasValue == true ? x.BirthDate.Value.ToString("yyyy-MM-dd") : "", x.SexName, x.UsedName, x.NationName, x.Place, x.EducationCodeName, x.Telephone, x.OfficeTelephone, x.Mobile, x.Email, x.QQ, x.Nationality, x.HealthStateName, x.HousePhone, x.Address, x.Postcode, x.HomeAddress, x.NowAddress, x.Residence, x.Speciality, x.WeChatNum, x.ReligionName, x.TeacherTypeName, x.IncumbencyStateName, x.CertificatesTypeName, x.CertificatesNum, x.SituationName, x.LiteracyLevelsName, x.DegreeStateName, x.LearnPositionName, x.TitleName, x.PaymentLevelName, x.IsDualTeacherName, WorkDate = x.WorkDate.HasValue == true ? x.WorkDate.Value.ToString("yyyy-MM") : "", ComeSchoolDate = x.ComeSchoolDate.HasValue == true ? x.ComeSchoolDate.Value.ToString("yyyy-MM") : "", TeachingDate = x.TeachingDate.HasValue == true ? x.TeachingDate.Value.ToString("yyyy-MM") : "", x.Profile, x.Remarks }).ToTable(); string[] liststring = { "教职工号", "姓名", RSL.Get("CampusCode"), RSL.Get("CampusName"), RSL.Get("CollegeCode"), RSL.Get("College"), "所在教研室代码", "所在教研室", "出生日期", "性别", "曾用名", "民族", "籍贯", "学历", "家庭电话", "办公室电话", "移动电话", "电子邮箱", "QQ", "国籍","健康状况", "住宅电话", "通信地址", "邮政编码", "家庭住址", "现住地址", "户口所在地", "特长", "微信号", "宗教信仰","教师类别", "在职状态", "证件类型", "证件号码", "任课状况", "文化程度", "学位状况", "授予学位", "职称", "课酬级别", "是否双师型教师", "参加工作年月", "来校日期", "从教年月", "简介", "备注" }; neh.Export(dt, liststring, "教职工信息" + DateTime.Now.ToString("yyyyMMdd")); return Json(new ReturnMessage() { IsSuccess = true, Message = "导出成功。" }); } } }