CoursematerialController.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using System.Text;
  7. using System.IO;
  8. using Bowin.Web.Controls.Mvc;
  9. using Bowin.Common.Utility;
  10. using Bowin.Common.Data;
  11. using Bowin.Common.Exceptions;
  12. using EMIS.Utility;
  13. using EMIS.Entities;
  14. using EMIS.Web.Controls;
  15. using EMIS.ViewModel;
  16. using EMIS.ViewModel.Cultureplan;
  17. using EMIS.CommonLogic.Cultureplan;
  18. namespace EMIS.Web.Controllers.Cultureplan
  19. {
  20. [Authorization]
  21. public class CoursematerialController : Controller
  22. {
  23. public ICoursematerialServices CoursematerialServices { 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. /// <param name="pararms"></param>
  36. /// <returns></returns>
  37. [HttpPost]
  38. public ActionResult List(QueryParamsModel pararms)
  39. {
  40. ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(pararms);
  41. //课程级别
  42. var courseLevelID = pararms.getExtraInt("DictionaryCourseLevel") == DropdownList.SELECT_ALL ? null : pararms.getExtraInt("DictionaryCourseLevel");
  43. //课程科类
  44. var courseScienceID = pararms.getExtraInt("DictionaryCourseScience") == DropdownList.SELECT_ALL ? null : pararms.getExtraInt("DictionaryCourseScience");
  45. //上课类型
  46. var classGroupingID = pararms.getExtraGuid("TeachTypeDropdown");
  47. //是否启用
  48. var isEnable = pararms.getExtraInt("DictionaryIsEnable") == DropdownList.SELECT_ALL ? null : pararms.getExtraInt("DictionaryIsEnable");
  49. return base.Json(CoursematerialServices.GetCoursematerialViewGrid(configuretView, courseLevelID, courseScienceID,
  50. classGroupingID, isEnable, (int)pararms.page, (int)pararms.rows));
  51. }
  52. /// <summary>
  53. /// 课程信息下拉列表
  54. /// </summary>
  55. /// <param name="pararms"></param>
  56. /// <returns></returns>
  57. [HttpPost]
  58. public ActionResult ListForDropdown(QueryParamsModel pararms)
  59. {
  60. ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(pararms);
  61. //课程级别
  62. var courseLevelID = pararms.getExtraInt("DictionaryCourseLevel") == DropdownList.SELECT_ALL ? null : pararms.getExtraInt("DictionaryCourseLevel");
  63. //课程科类
  64. var courseScienceID = pararms.getExtraInt("DictionaryCourseScience") == DropdownList.SELECT_ALL ? null : pararms.getExtraInt("DictionaryCourseScience");
  65. //上课类型
  66. var classGroupingID = pararms.getExtraGuid("TeachTypeDropdown");
  67. //是否启用
  68. var isEnable = pararms.getExtraInt("DictionaryIsEnable") == DropdownList.SELECT_ALL ? null : pararms.getExtraInt("DictionaryIsEnable");
  69. return base.Json(CoursematerialServices.GetCoursematerialViewGrid(configuretView, courseLevelID, courseScienceID,
  70. classGroupingID, isEnable, (int)pararms.page, (int)pararms.rows));
  71. }
  72. /// <summary>
  73. /// 课程信息下拉列表(启用状态为是的信息)
  74. /// </summary>
  75. /// <param name="pararms"></param>
  76. /// <returns></returns>
  77. [HttpPost]
  78. public ActionResult GetIsEnableCoursematerialView(QueryParamsModel pararms)
  79. {
  80. ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(pararms);
  81. //课程级别
  82. var courseLevelID = pararms.getExtraInt("DictionaryCourseLevel") == DropdownList.SELECT_ALL ? null : pararms.getExtraInt("DictionaryCourseLevel");
  83. //课程科类
  84. var courseScienceID = pararms.getExtraInt("DictionaryCourseScience") == DropdownList.SELECT_ALL ? null : pararms.getExtraInt("DictionaryCourseScience");
  85. //上课类型
  86. var classGroupingID = pararms.getExtraGuid("TeachTypeDropdown");
  87. //是否启用(默认为是)
  88. var isEnable = (int)CF_GeneralPurpose.IsYes;
  89. return base.Json(CoursematerialServices.GetCoursematerialViewGrid(configuretView, courseLevelID, courseScienceID,
  90. classGroupingID, isEnable, (int)pararms.page, (int)pararms.rows));
  91. }
  92. /// <summary>
  93. /// 课程信息下拉列表(启用状态为是或已使用的信息)
  94. /// </summary>
  95. /// <param name="pararms"></param>
  96. /// <returns></returns>
  97. [HttpPost]
  98. public ActionResult GetEnableAndUseCoursematerialView(QueryParamsModel pararms)
  99. {
  100. ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(pararms);
  101. //课程信息ID
  102. var coursematerialID = Request["coursematerialID"].ParseStrTo<Guid>();
  103. //是否启用(默认为是)
  104. var isEnable = (int)CF_GeneralPurpose.IsYes;
  105. return base.Json(CoursematerialServices.GetEnableAndUseCoursematerialViewGrid(configuretView, coursematerialID, isEnable, (int)pararms.page, (int)pararms.rows));
  106. }
  107. /// <summary>
  108. /// 复制新增
  109. /// </summary>
  110. /// <param name="coursematerialID"></param>
  111. /// <returns></returns>
  112. public ActionResult CopyAdd(Guid coursematerialID)
  113. {
  114. CoursematerialView coursematerialView = new CoursematerialView();
  115. coursematerialView = CoursematerialServices.GetCoursematerialView(coursematerialID);
  116. if (coursematerialView != null)
  117. {
  118. coursematerialView.Abbreviation = null;
  119. coursematerialView.StandardName = null;
  120. }
  121. return View("Edit", coursematerialView);
  122. }
  123. /// <summary>
  124. /// 复制新增
  125. /// </summary>
  126. /// <param name="teachTypeView"></param>
  127. /// <returns></returns>
  128. [HttpPost]
  129. public ActionResult CopyAdd(CoursematerialView coursematerialView)
  130. {
  131. coursematerialView.CoursematerialID = Guid.Empty;
  132. return this.Edit(coursematerialView);
  133. }
  134. /// <summary>
  135. /// 编辑
  136. /// </summary>
  137. /// <param name="coursematerialID"></param>
  138. /// <returns></returns>
  139. public ActionResult Edit(Guid? coursematerialID)
  140. {
  141. CoursematerialView coursematerialView = new CoursematerialView();
  142. if (coursematerialID.HasValue && coursematerialID != Guid.Empty)
  143. {
  144. coursematerialView = CoursematerialServices.GetCoursematerialView(coursematerialID);
  145. }
  146. else
  147. {
  148. coursematerialView.IsEnable = true;
  149. }
  150. return View(coursematerialView);
  151. }
  152. /// <summary>
  153. /// 编辑
  154. /// </summary>
  155. /// <param name="coursematerialView"></param>
  156. /// <returns></returns>
  157. [HttpPost]
  158. public ActionResult Edit(CoursematerialView coursematerialView)
  159. {
  160. try
  161. {
  162. CoursematerialServices.CoursematerialEdit(coursematerialView);
  163. return Json(new ReturnMessage()
  164. {
  165. IsSuccess = true,
  166. Message = "保存成功。"
  167. });
  168. }
  169. catch (Exception ex)
  170. {
  171. return Json(new ReturnMessage()
  172. {
  173. IsSuccess = false,
  174. Message = "保存失败:" + ex.Message
  175. });
  176. }
  177. }
  178. /// <summary>
  179. /// 删除
  180. /// </summary>
  181. /// <param name="coursematerialIDs"></param>
  182. /// <returns></returns>
  183. [HttpPost]
  184. public ActionResult Delete(string coursematerialIDs)
  185. {
  186. try
  187. {
  188. List<Guid?> list = coursematerialIDs.Split(',').Where(x => !string.IsNullOrEmpty(x))
  189. .Select(x => (Guid?)new Guid(x)).ToList();
  190. CoursematerialServices.CoursematerialDelete(list);
  191. return base.Json(new ReturnMessage { IsSuccess = true, Message = "删除成功。" });
  192. }
  193. catch (Exception ex)
  194. {
  195. string mge = ex.Message;
  196. System.Data.SqlClient.SqlException num = ExceptionHelper.GetSqlException(ex);
  197. if (num != null)
  198. {
  199. if (num.Number == 547)
  200. {
  201. mge = "请先删除与其关联的数据,如:执行计划、专业计划、专业课程等。";
  202. }
  203. }
  204. return base.Json(new ReturnMessage { IsSuccess = false, Message = "删除失败,原因:" + mge });
  205. }
  206. }
  207. /// <summary>
  208. /// Excel导出
  209. /// </summary>
  210. /// <returns></returns>
  211. [HttpPost]
  212. public ActionResult Excel()
  213. {
  214. NpoiExcelHelper neh = new NpoiExcelHelper();
  215. ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(null);
  216. //课程级别
  217. var courseLevelID = Request.Form["DictionaryCourseLevel"].ParseStrTo<int>() == DropdownList.SELECT_ALL ? null : Request.Form["DictionaryCourseLevel"].ParseStrTo<int>();
  218. //课程科类
  219. var courseScienceID = Request.Form["DictionaryCourseScience"].ParseStrTo<int>() == DropdownList.SELECT_ALL ? null : Request.Form["DictionaryCourseScience"].ParseStrTo<int>();
  220. //上课类型
  221. var classGroupingID = Request.Form["TeachTypeDropdown"].ParseStrTo<Guid>();
  222. //是否启用
  223. var isEnable = Request.Form["DictionaryIsEnable"].ParseStrTo<int>() == DropdownList.SELECT_ALL ? null : Request.Form["DictionaryIsEnable"].ParseStrTo<int>();
  224. var dt = CoursematerialServices.GetCoursematerialViewList(configuretView, courseLevelID, courseScienceID, classGroupingID, isEnable)
  225. .Select(x => new
  226. {
  227. x.CourseCode,
  228. x.CourseName,
  229. x.Abbreviation,
  230. x.EnglishName,
  231. x.StandardName,
  232. x.CourseEdition,
  233. x.CourseLevelName,
  234. x.CourseScienceName,
  235. x.ClassName,
  236. x.CourseSynopsis,
  237. x.CourseReserve,
  238. x.IsEnableName,
  239. x.Remark
  240. }).ToTable();
  241. string[] liststring = {
  242. "课程代码", "课程名称", "课程简称", "英文名称", "课程规范名", "课程版本",
  243. "课程级别", "课程科类", "上课类型", "课程简介", "预修课程集", "是否启用", "备注"
  244. };
  245. neh.Export(dt, liststring, "课程信息" + DateTime.Now.ToString("yyyyMMdd"));
  246. return Json(new ReturnMessage()
  247. {
  248. IsSuccess = true,
  249. Message = "导出成功。"
  250. });
  251. }
  252. /// <summary>
  253. /// Excel导入
  254. /// </summary>
  255. /// <param name="errorFile"></param>
  256. /// <param name="operationTips"></param>
  257. /// <returns></returns>
  258. [HttpGet]
  259. public ActionResult Import(string errorFile, string operationTips)
  260. {
  261. ViewBag.ErrorFile = errorFile;
  262. if (string.IsNullOrEmpty(operationTips))
  263. {
  264. operationTips = "点击查看失败原因...";
  265. }
  266. ViewBag.operationTips = operationTips;
  267. return View();
  268. }
  269. /// <summary>
  270. /// Excel导入
  271. /// </summary>
  272. /// <param name="file"></param>
  273. /// <returns></returns>
  274. [HttpPost]
  275. public ActionResult Import(HttpPostedFileBase file)
  276. {
  277. try
  278. {
  279. if (!NpoiExcelHelper.GetIsCompatible(file.FileName))
  280. {
  281. throw new Exception("格式错误,只允许导入xls或xlsx格式的Excel文件。");
  282. }
  283. Dictionary<string, string> cellheader = new Dictionary<string, string>
  284. {
  285. { "CourseCode", "课程代码" },
  286. { "CourseName", "课程名称" },
  287. { "Abbreviation", "课程简称" },
  288. { "EnglishName", "英文名称" },
  289. { "StandardName", "课程规范名" },
  290. { "CourseEdition", "课程版本" },
  291. { "CourseLevelStr", "课程级别" },
  292. { "CourseScienceStr", "课程科类" },
  293. { "ClassName", "上课类型" },
  294. { "CourseSynopsis", "课程简介" },
  295. { "CourseReserve", "预修课程集" },
  296. { "IsEnableStr", "是否启用" },
  297. { "Remark", "备注" },
  298. { "ErrorMessage", "未导入原因" }
  299. };
  300. StringBuilder errorMsg = new StringBuilder(); // 错误信息
  301. string sourceWebPath = FileUploadHelper.UploadFile(file);
  302. var sourcePhysicalPath = Server.MapPath(sourceWebPath);
  303. List<CoursematerialView> errList = new List<CoursematerialView>();
  304. List<CoursematerialView> dataList = new List<CoursematerialView>();
  305. int? inCount = 0; //导入个数
  306. int? upCount = 0; //更新个数
  307. int? errCount = 0; //失败个数
  308. //导入
  309. CoursematerialServices.CoursematerialImport(cellheader, out inCount, out upCount, out errList, out errCount, sourcePhysicalPath);
  310. System.IO.File.Delete(sourcePhysicalPath);//删除本地缓存文件
  311. if (errList.Count() > 0)
  312. {
  313. //获取错误数据文件路径
  314. string errorWebPath = string.Format("{0}", NpoiExcelHelper
  315. .EntityListToExcel2003(cellheader, errList, "课程信息导入失败文件", sourcePhysicalPath));
  316. ViewBag.ErrorFile = errorWebPath;
  317. string Errinfo = string.Format("提示:{0}条课程信息导入成功,{1}条课程信息更新成功,{2}条课程信息导入失败,点击查看。",
  318. inCount, upCount, errCount);
  319. ViewBag.operationTips = Errinfo;
  320. return RedirectToAction("MsgShow", "Common", new
  321. {
  322. WindowID = "none",
  323. msg = Errinfo,
  324. url = Url.Action("Import").AddMenuParameter() + "&errorFile=" + errorWebPath + "&operationTips="
  325. + Errinfo + "&WindowID=" + Request["WindowID"]
  326. });
  327. }
  328. else
  329. {
  330. string successInfo = string.Format("提示:{0}条课程信息导入成功,{1}条课程信息更新成功。", inCount, upCount);
  331. return RedirectToAction("MsgShow", "Common", new
  332. {
  333. WindowID = Request["WindowID"],
  334. msg = successInfo,
  335. url = Url.Action("List").AddMenuParameter()
  336. });
  337. }
  338. }
  339. catch (Exception ex)
  340. {
  341. return RedirectToAction("MsgShow", "Common", new
  342. {
  343. WindowID = "none",
  344. msg = "导入失败,原因:" + ex.Message,
  345. url = Url.Action("Import").AddMenuParameter() + "&WindowID=" + Request["WindowID"]
  346. });
  347. }
  348. }
  349. }
  350. }