DepartmentController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 Bowin.Common.JSON;
  8. using EMIS.Entities;
  9. using Bowin.Common.Linq.Entity;
  10. using EMIS.CommonLogic.AdministrativeOrgan;
  11. using EMIS.ViewModel;
  12. using EMIS.CommonLogic.SystemServices;
  13. using Bowin.Web.Controls.Mvc;
  14. using Bowin.Common.Utility;
  15. using System.Data;
  16. using Bowin.Common.Data;
  17. using Bowin.Common.Exceptions;
  18. using EMIS.Utility;
  19. using System.Text;
  20. namespace EMIS.Web.Controllers.AdministrativeOrgan
  21. {
  22. [Authorization]
  23. public class DepartmentController : Controller
  24. {
  25. public IDepartmentServices DepartmentServices { get; set; }
  26. /// <summary>
  27. /// 教研室页面
  28. /// </summary>
  29. /// <returns></returns>
  30. public ActionResult List()
  31. {
  32. return View();
  33. }
  34. /// <summary>
  35. /// 教研室页面列表查询
  36. /// </summary>
  37. /// <param name="pararms"></param>
  38. /// <returns></returns>
  39. [HttpPost]
  40. public ActionResult List(QueryParamsModel pararms)
  41. {
  42. ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(pararms);
  43. var campusID = pararms.getExtraGuid("CampusDropdown");
  44. var collegeID = pararms.getExtraGuid("CollegeDropdown");
  45. return base.Json(DepartmentServices.GetDepartmentViewGrid(configuretView, campusID, collegeID, (int)pararms.page, (int)pararms.rows));
  46. }
  47. /// <summary>
  48. /// 查询教研室信息View(无数据范围)
  49. /// </summary>
  50. /// <param name="pararms"></param>
  51. /// <returns></returns>
  52. [HttpPost]
  53. public ActionResult ListGridResultSet(QueryParamsModel pararms)
  54. {
  55. ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(pararms);
  56. var campusID = pararms.getExtraGuid("CampusDropdown");
  57. var collegeID = pararms.getExtraGuid("CollegeDropdown");
  58. return base.Json(DepartmentServices.GetDepartmentViewWithoutRange(configuretView, campusID, collegeID, (int)pararms.page, (int)pararms.rows));
  59. }
  60. /// <summary>
  61. /// 根据院系所信息ID获取教研室信息(带数据范围)
  62. /// </summary>
  63. /// <param name="bindType"></param>
  64. /// <param name="collegeID"></param>
  65. /// <returns></returns>
  66. [HttpPost]
  67. public ActionResult DepartmentDropdownListBanid(DropdownListBindType? bindType, Guid? collegeID)
  68. {
  69. List<DropdownListItem> list = null;
  70. if (collegeID == null)
  71. {
  72. list = new List<DropdownListItem>();
  73. }
  74. else
  75. {
  76. list = DepartmentServices.GetDepartmentList(collegeID)
  77. .Select(x => new DropdownListItem
  78. {
  79. Text = x.Name,
  80. Value = x.DepartmentID.ToString()
  81. }).ToList();
  82. }
  83. DropdownListBindType dbt = bindType == null ? DropdownListBindType.SelectAll : bindType.Value;
  84. DropdownList.FormatDropdownItemList(dbt, list);
  85. return base.Json(list);
  86. }
  87. /// <summary>
  88. /// 查询全部部门信息DepartmentView(带数据范围)
  89. /// </summary>
  90. /// <param name="bindType"></param>
  91. /// <param name="collegeID"></param>
  92. /// <returns></returns>
  93. [HttpPost]
  94. public ActionResult DepartmentDropdownAllBanid(DropdownListBindType? bindType)
  95. {
  96. List<DropdownListItem> list = DepartmentServices.GetDepartmentList()
  97. .Select(x => new DropdownListItem
  98. {
  99. Text = x.Name,
  100. Value = x.DepartmentID.ToString()
  101. }).ToList();
  102. DropdownListBindType dbt = bindType == null ? DropdownListBindType.SelectAll : bindType.Value;
  103. DropdownList.FormatDropdownItemList(dbt, list);
  104. return base.Json(list);
  105. }
  106. /// <summary>
  107. /// 根据学院ID获取部门绑定到combogrid控件
  108. /// </summary>
  109. /// <param name="pararms"></param>
  110. /// <param name="collegeID"></param>
  111. /// <returns></returns>
  112. [HttpPost]
  113. public ActionResult DepartmentComBoGrid(QueryParamsModel pararms, Guid? collegeID)
  114. {
  115. collegeID = collegeID == null ? Guid.Empty : collegeID;
  116. ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(pararms);
  117. return base.Json(DepartmentServices.GetDepartmentViewGrid(configuretView, Guid.Empty, (Guid)collegeID, (int)pararms.page, (int)pararms.rows));
  118. }
  119. /// <summary>
  120. /// 复制新增
  121. /// </summary>
  122. /// <param name="departmentID"></param>
  123. /// <returns></returns>
  124. public ActionResult CopyAdd(Guid departmentID)
  125. {
  126. DepartmentView departmentView = new DepartmentView();
  127. departmentView = DepartmentServices.GetDepartmentView(departmentID);
  128. return View("Edit", departmentView);
  129. }
  130. /// <summary>
  131. /// 复制新增
  132. /// </summary>
  133. /// <param name="departmentView"></param>
  134. /// <returns></returns>
  135. [HttpPost]
  136. public ActionResult CopyAdd(DepartmentView departmentView)
  137. {
  138. departmentView.DepartmentID = Guid.Empty;
  139. return this.Edit(departmentView);
  140. }
  141. /// <summary>
  142. /// 编辑(新增、修改,业务主键:部门代码或部门名称)
  143. /// </summary>
  144. /// <param name="departmentID"></param>
  145. /// <returns></returns>
  146. [HttpGet]
  147. public ActionResult Edit(Guid? departmentID)
  148. {
  149. //临时性加参数控制、屏蔽列表进入无保存按钮权限
  150. ViewBag.Type = Request.Params["Type"];
  151. DepartmentView departmentView = new DepartmentView();
  152. if (departmentID.HasValue && departmentID != Guid.Empty)
  153. {
  154. departmentView = DepartmentServices.GetDepartmentView(departmentID);
  155. }
  156. return View(departmentView);
  157. }
  158. /// <summary>
  159. /// 编辑(新增、修改,业务主键:部门代码或部门名称)
  160. /// </summary>
  161. /// <param name="departmentView"></param>
  162. /// <returns></returns>
  163. [HttpPost]
  164. public ActionResult Edit(DepartmentView departmentView)
  165. {
  166. try
  167. {
  168. DepartmentServices.DepartmentEdit(departmentView);
  169. return Json(new ReturnMessage()
  170. {
  171. IsSuccess = true,
  172. Message = "保存成功。"
  173. });
  174. }
  175. catch (Exception ex)
  176. {
  177. return Json(new ReturnMessage()
  178. {
  179. IsSuccess = false,
  180. Message = "保存失败,原因:" + ex.Message
  181. });
  182. }
  183. }
  184. /// <summary>
  185. /// 删除
  186. /// </summary>
  187. /// <param name="departmentIDs"></param>
  188. /// <returns></returns>
  189. [HttpPost]
  190. public ActionResult Delete(string departmentIDs)
  191. {
  192. try
  193. {
  194. List<Guid?> list = departmentIDs.Split(',').Where(x => !string.IsNullOrEmpty(x))
  195. .Select(x => (Guid?)new Guid(x)).ToList();
  196. DepartmentServices.DepartmentDelete(list);
  197. return base.Json("删除成功。");
  198. }
  199. catch (Exception ex)
  200. {
  201. string mge = ex.Message;
  202. System.Data.SqlClient.SqlException num = ExceptionHelper.GetSqlException(ex);
  203. if (num != null)
  204. {
  205. if (num.Number == 547)
  206. {
  207. mge = "请先删除所有关联的数据,如教师信息等";
  208. }
  209. }
  210. return base.Json("删除失败,原因:" + mge);
  211. }
  212. }
  213. /// <summary>
  214. /// Excel导出
  215. /// </summary>
  216. /// <returns></returns>
  217. [HttpPost]
  218. public ActionResult Excel()
  219. {
  220. NpoiExcelHelper neh = new NpoiExcelHelper();
  221. ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(null);
  222. var campusID = Request.Form["CampusDropdown"].ParseStrTo<Guid>();
  223. var collegeID = Request.Form["CollegeDropdown"].ParseStrTo<Guid>();
  224. var dt = DepartmentServices.GetDepartmentViewList(configuretView, campusID, collegeID)
  225. .Select(x => new
  226. {
  227. x.No,
  228. x.Name,
  229. x.SimpleName,
  230. x.CollegeCode,
  231. x.CollegeName,
  232. x.DirectorName,
  233. x.DeputyDirectorName,
  234. x.FoundDate,
  235. x.Remark
  236. }).ToTable();
  237. string[] liststring = {
  238. "教研室代码", "教研室名称", "教研室简称",
  239. RSL.Get("CollegeCode"), RSL.Get("CollegeName"),
  240. "主任姓名", "副主任", "创建年月", "备注"
  241. };
  242. neh.Export(dt, liststring, "教研室信息" + DateTime.Now.ToString("yyyyMMdd"));
  243. return Json(new ReturnMessage()
  244. {
  245. IsSuccess = true,
  246. Message = "导出成功。"
  247. });
  248. }
  249. /// <summary>
  250. /// Excel导入
  251. /// </summary>
  252. /// <param name="errorFile"></param>
  253. /// <param name="operationTips"></param>
  254. /// <returns></returns>
  255. [HttpGet]
  256. public ActionResult Import(string errorFile, string operationTips)
  257. {
  258. ViewBag.ErrorFile = errorFile;
  259. if (string.IsNullOrEmpty(operationTips))
  260. {
  261. operationTips = "点击查看失败原因...";
  262. }
  263. ViewBag.operationTips = operationTips;
  264. return View();
  265. }
  266. /// <summary>
  267. /// Excel导入
  268. /// </summary>
  269. /// <param name="file"></param>
  270. /// <returns></returns>
  271. [HttpPost]
  272. public ActionResult Import(HttpPostedFileBase file)
  273. {
  274. try
  275. {
  276. if (!NpoiExcelHelper.GetIsCompatible(file.FileName))
  277. {
  278. throw new Exception("格式错误,只允许导入xls或xlsx格式的Excel文件。");
  279. }
  280. Dictionary<string, string> cellheader = new Dictionary<string, string>
  281. {
  282. { "No", "教研室代码" },
  283. { "Name", "教研室名称" },
  284. { "SimpleName", "教研室简称" },
  285. { "EnglishName", "英文名称" },
  286. { "CollegeCode", RSL.Get("CollegeCode") },
  287. { "DirectorName", "主任姓名" },
  288. { "DeputyDirectorName", "副主任姓名" },
  289. { "FoundDateStr", "创建年月" },
  290. { "Remark", "备注" },
  291. { "ErrorMessage", "未导入原因" }
  292. };
  293. StringBuilder errorMsg = new StringBuilder(); // 错误信息
  294. string sourceWebPath = FileUploadHelper.UploadFile(file);
  295. var sourcePhysicalPath = Server.MapPath(sourceWebPath);
  296. List<DepartmentView> errList = new List<DepartmentView>();
  297. List<DepartmentView> dataList = new List<DepartmentView>();
  298. int? inCount = 0; //导入个数
  299. int? upCount = 0; //更新个数
  300. int? errCount = 0; //失败个数
  301. //导入
  302. DepartmentServices.DepartmentImport(cellheader, out inCount, out upCount, out errList, out errCount, sourcePhysicalPath);
  303. System.IO.File.Delete(sourcePhysicalPath);//删除本地缓存文件
  304. if (errList.Count() > 0)
  305. {
  306. //获取错误数据文件路径
  307. string errorWebPath = string.Format("{0}", NpoiExcelHelper
  308. .EntityListToExcel2003(cellheader, errList, "教研室信息导入失败文件", sourcePhysicalPath));
  309. ViewBag.ErrorFile = errorWebPath;
  310. string Errinfo = string.Format("提示:{0}条教研室信息导入成功,{1}条教研室信息更新成功,{2}条教研室信息导入失败,点击查看。",
  311. inCount, upCount, errCount);
  312. ViewBag.operationTips = Errinfo;
  313. return RedirectToAction("MsgShow", "Common", new
  314. {
  315. WindowID = "none",
  316. msg = Errinfo,
  317. url = Url.Action("Import").AddMenuParameter() + "&errorFile=" + errorWebPath + "&operationTips="
  318. + Errinfo + "&WindowID=" + Request["WindowID"]
  319. });
  320. }
  321. else
  322. {
  323. string successInfo = string.Format("提示:{0}条教研室信息导入成功,{1}条教研室信息更新成功。", inCount, upCount);
  324. return RedirectToAction("MsgShow", "Common", new
  325. {
  326. WindowID = Request["WindowID"],
  327. msg = successInfo,
  328. url = Url.Action("List").AddMenuParameter()
  329. });
  330. }
  331. }
  332. catch (Exception ex)
  333. {
  334. return RedirectToAction("MsgShow", "Common", new
  335. {
  336. WindowID = "none",
  337. msg = "导入失败,原因:" + ex.Message,
  338. url = Url.Action("Import").AddMenuParameter() + "&WindowID=" + Request["WindowID"]
  339. });
  340. }
  341. }
  342. }
  343. }