using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using EMIS.Web.Controls; using Bowin.Common.JSON; using EMIS.Entities; using Bowin.Common.Linq.Entity; using EMIS.CommonLogic.AdministrativeOrgan; using EMIS.ViewModel; using EMIS.CommonLogic.SystemServices; using Bowin.Web.Controls.Mvc; using Bowin.Common.Utility; using System.Data; using Bowin.Common.Data; using Bowin.Common.Exceptions; using EMIS.Utility; using System.Text; using EMIS.CommonLogic.DataCenterSynch; namespace EMIS.Web.Controllers.AdministrativeOrgan { [Authorization] public class CollegeController : Controller { public ICollegeServices CollegeServices { get; set; } public ICollegeSynchServices CollegeSynchServices { get; set; } /// /// 院系所信息页面 /// /// public ActionResult List() { return View(); } /// /// 院系所信息列表查询 /// /// /// [HttpPost] public ActionResult List(QueryParamsModel pararms) { ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(pararms); var campusID = pararms.getExtraGuid("CampusDropdown"); var unitCategoryID = pararms.getExtraInt("DictionaryUnitCategory") == DropdownList.SELECT_ALL ? null : pararms.getExtraInt("DictionaryUnitCategory"); return base.Json(CollegeServices.GetCollegeViewGrid(configuretView, campusID, unitCategoryID, (int)pararms.page, (int)pararms.rows)); } /// /// 院系所信息列表查询(只显示院、系、部类别的院系所) /// /// /// [HttpPost] public ActionResult ListOnlyCollege(QueryParamsModel pararms) { ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(pararms); var campusID = pararms.getExtraGuid("CampusDropdown"); return base.Json(CollegeServices.GetOnlyCollegeViewList(configuretView, campusID, (int)pararms.page, (int)pararms.rows)); } /// /// 查询院系所信息View(无数据范围) /// /// /// [HttpPost] public ActionResult ListWithoutRange(QueryParamsModel pararms) { ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(pararms); var campusID = pararms.getExtraGuid("CampusDropdown"); return base.Json(CollegeServices.GetCollegeViewWithoutRange(configuretView, campusID, (int)pararms.page, (int)pararms.rows)); } /// /// 根据校区获取学院(带数据范围) /// /// /// /// [HttpPost] public ActionResult CollegeDropdownListBanid(DropdownListBindType? bindType, Guid? campusID) { List list = null; list = CollegeServices.GetCollegeList(campusID) .Select(x => new DropdownListItem { Text = x.Name, Value = x.CollegeID.ToString() }).ToList(); DropdownListBindType dbt = bindType == null ? DropdownListBindType.SelectAll : bindType.Value; DropdownList.FormatDropdownItemList(dbt, list); return base.Json(list); } /// /// 根据校区获取学院(无数据范围) /// /// /// /// [HttpPost] public ActionResult AllCollegeDropdownListBanid(DropdownListBindType? bindType, Guid? campusID) { List list = CollegeServices.GetAllCollegeList(campusID) .Select(x => new DropdownListItem { Text = x.Name, Value = x.CollegeID.ToString() }).ToList(); DropdownListBindType dbt = bindType == null ? DropdownListBindType.SelectAll : bindType.Value; DropdownList.FormatDropdownItemList(dbt, list); return base.Json(list); } /// /// 院系所下拉列表 /// /// /// [HttpPost] public ActionResult CollegeDropdownListBanids(DropdownListBindType? bindType) { List list = new List(); list = CollegeServices.GetCollegeList() .Select(x => new DropdownListItem { Text = x.Name, Value = x.CollegeID.ToString() }).ToList(); DropdownListBindType dbt = bindType == null ? DropdownListBindType.SelectAll : bindType.Value; DropdownList.FormatDropdownItemList(dbt, list); return base.Json(list); } /// /// 院系所下拉列表(只显示院、系、部类别的院系所) /// /// /// [HttpPost] public ActionResult CollegeDropdownListOnlyCollege(DropdownListBindType? bindType) { List list = new List(); list = CollegeServices.GetOnlyCollegeList() .Select(x => new DropdownListItem { Text = x.Name, Value = x.CollegeID.ToString() }).ToList(); DropdownListBindType dbt = bindType == null ? DropdownListBindType.SelectAll : bindType.Value; DropdownList.FormatDropdownItemList(dbt, list); return base.Json(list); } /// /// 获取学院(无数据范围限制) /// [HttpPost] public ActionResult CollegeDropdownListBanidsWithoutRange(DropdownListBindType? bindType) { List list = new List(); list = CollegeServices.GetCollegeViewListWithoutDataRange() .Select(x => new DropdownListItem { Text = x.Name, Value = x.CollegeID.ToString() }).ToList(); DropdownListBindType dbt = bindType == null ? DropdownListBindType.SelectAll : bindType.Value; DropdownList.FormatDropdownItemList(dbt, list); return base.Json(list); } /// /// 复制新增 /// /// /// public ActionResult CopyAdd(Guid collegeID) { CollegeView collegeView = new CollegeView(); collegeView = CollegeServices.GetCollegeView(collegeID); return View("Edit", collegeView); } /// /// 复制新增 /// /// /// [HttpPost] public ActionResult CopyAdd(CollegeView collegeView) { collegeView.CollegeID = Guid.Empty; return this.Edit(collegeView); } /// /// 编辑(新增、修改,业务主键:院系所代码或院系所名称) /// /// /// [HttpGet] public ActionResult Edit(Guid? collegeID) { //临时性加参数控制、屏蔽列表进入无保存按钮权限 ViewBag.Type = Request.Params["Type"]; CollegeView collegeView = new CollegeView(); if (collegeID.HasValue && collegeID != Guid.Empty) { collegeView = CollegeServices.GetCollegeView(collegeID); } else { collegeView.UnitCategoryID = (int)CF_UnitCategory.College; } return View(collegeView); } /// /// 编辑(新增、修改,业务主键:院系所代码或院系所名称) /// /// /// [HttpPost] public ActionResult Edit(CollegeView collegeView) { try { CollegeServices.CollegeEdit(collegeView); return Json(new ReturnMessage() { IsSuccess = true, Message = "保存成功。" }); } catch (Exception ex) { return Json(new ReturnMessage() { IsSuccess = false, Message = "保存失败,原因:" + ex.Message }); } } /// /// 删除 /// /// /// [HttpPost] public ActionResult Delete(string collegeIDs) { try { List list = collegeIDs.Split(',').Where(x => !string.IsNullOrEmpty(x)) .Select(x => (Guid?)new Guid(x)).ToList(); CollegeServices.CollegeDelete(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); } } /// /// Excel导出 /// /// /// [HttpPost] public ActionResult Excel(CollegeView pararms) { NpoiExcelHelper neh = new NpoiExcelHelper(); ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(null); var campusID = Request.Form["CampusDropdown"].ParseStrTo(); var unitCategoryID = Request.Form["DictionaryUnitCategory"].ParseStrTo() == DropdownList.SELECT_ALL ? null : Request.Form["DictionaryUnitCategory"].ParseStrTo(); var dt = CollegeServices.GetCollegeViewList(configuretView, campusID, unitCategoryID) .Select(x => new { x.No, x.Name, x.SimpleName, x.CampusNo, x.CampusName, x.PoliticalManagerName, x.AdministrativeManagerName, x.UnitCategoryName, x.FoundDate, x.Officephone, x.Remark, }).ToTable(); string[] liststring = { RSL.Get("CollegeCode"), RSL.Get("CollegeName"), "简称", RSL.Get("CampusCode"), RSL.Get("CampusName"), "党务负责人", "行政负责人", "单位类别", "建立年月","办公电话", "备注" }; neh.Export(dt, liststring, RSL.Get("College") + "信息" + DateTime.Now.ToString("yyyyMMdd")); return Json(new ReturnMessage() { IsSuccess = true, Message = "导出成功。" }); } /// /// Excel导入 /// /// /// /// [HttpGet] public ActionResult Import(string errorFile, string operationTips) { ViewBag.ErrorFile = errorFile; if (string.IsNullOrEmpty(operationTips)) { operationTips = "点击查看失败原因..."; } ViewBag.operationTips = operationTips; return View(); } /// /// Excel导入 /// /// /// [HttpPost] public ActionResult Import(HttpPostedFileBase file) { try { if (!NpoiExcelHelper.GetIsCompatible(file.FileName)) { throw new Exception("格式错误,只允许导入xls或xlsx格式的Excel文件。"); } Dictionary cellheader = new Dictionary { { "No", RSL.Get("CollegeCode") }, { "Name", RSL.Get("CollegeName") }, { "SimpleName", "简称" }, { "EnglishName", "英文名称" }, { "CampusNo", RSL.Get("CampusCode") }, { "PoliticalManagerName", "党务负责人" }, { "AdministrativeManagerName", "行政负责人" }, { "UnitCategoryStr", "单位类别" }, { "FoundDateStr", "建立年月" }, { "Officephone", "办公电话" }, { "Remark", "备注" }, { "ErrorMessage", "未导入原因" } }; StringBuilder errorMsg = new StringBuilder(); // 错误信息 string sourceWebPath = FileUploadHelper.UploadFile(file); var sourcePhysicalPath = Server.MapPath(sourceWebPath); List errList = new List(); List dataList = new List(); int? inCount = 0; //导入个数 int? upCount = 0; //更新个数 int? errCount = 0; //失败个数 //导入 CollegeServices.CollegeImport(cellheader, out inCount, out upCount, out errList, out errCount, sourcePhysicalPath); System.IO.File.Delete(sourcePhysicalPath);//删除本地缓存文件 if (errList.Count() > 0) { //获取错误数据文件路径 string errorWebPath = string.Format("{0}", NpoiExcelHelper .EntityListToExcel2003(cellheader, errList, RSL.Get("College") + "导入失败文件", sourcePhysicalPath)); ViewBag.ErrorFile = errorWebPath; string Errinfo = string.Format("提示:{0}条" + RSL.Get("College") + "导入成功,{1}条" + RSL.Get("College") + "更新成功,{2}条" + RSL.Get("College") + "导入失败,点击查看。", inCount, upCount, errCount); ViewBag.operationTips = Errinfo; return RedirectToAction("MsgShow", "Common", new { WindowID = "none", msg = Errinfo, url = Url.Action("Import").AddMenuParameter() + "&errorFile=" + errorWebPath + "&operationTips=" + Errinfo + "&WindowID=" + Request["WindowID"] }); } else { string successInfo = string.Format("提示:{0}条" + RSL.Get("College") + "导入成功,{1}条" + RSL.Get("College") + "更新成功。", inCount, upCount); return RedirectToAction("MsgShow", "Common", new { WindowID = Request["WindowID"], msg = successInfo, url = Url.Action("List").AddMenuParameter() }); } } catch (Exception ex) { return RedirectToAction("MsgShow", "Common", new { WindowID = "none", msg = "导入失败,原因:" + ex.Message, url = Url.Action("Import").AddMenuParameter() + "&WindowID=" + Request["WindowID"] }); } } [HttpPost] public ActionResult Synchr() { try { CollegeSynchServices.Synchr(); return Json(new ReturnMessage { IsSuccess = true, Message = "同步成功。" }); } catch (Exception ex) { return Json(new ReturnMessage { IsSuccess = false, Message = "同步失败:" + ex.Message + ex.InnerException?.Message ?? "" + ex.StackTrace }); } } } }