ImportScoreController.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. using System;
  2. using System.Text;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Mvc;
  7. using EMIS.ViewModel;
  8. using EMIS.CommonLogic.ScoreManage;
  9. using EMIS.Web.Controls;
  10. using EMIS.ViewModel.ScoreManage;
  11. using Bowin.Web.Controls.Mvc;
  12. using Bowin.Common.Utility;
  13. using Bowin.Common.Data;
  14. using EMIS.Utility;
  15. namespace EMIS.Web.Controllers.ScoreManage
  16. {
  17. [Authorization]
  18. public class ImportScoreController : Controller
  19. {
  20. public IImportScoreServices ImportScoreServices { get; set; }
  21. /// <summary>
  22. /// 平时成绩页面
  23. /// </summary>
  24. /// <returns></returns>
  25. public ActionResult List()
  26. {
  27. return View();
  28. }
  29. /// <summary>
  30. /// 平时成绩列表查询
  31. /// </summary>
  32. /// <param name="pararms"></param>
  33. /// <returns></returns>
  34. [HttpPost]
  35. public ActionResult List(QueryParamsModel pararms)
  36. {
  37. ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(pararms);
  38. var schoolyearID = pararms.getExtraGuid("SchoolyearDropdown");
  39. var collegeID = pararms.getExtraGuid("CollegeDropdown");
  40. var yearID = pararms.getExtraInt("SchoolyearDictionaryDropDown") == DropdownList.SELECT_ALL ? null : pararms.getExtraInt("SchoolyearDictionaryDropDown");
  41. var standardID = pararms.getExtraInt("StandardDictionaryDropDown") == DropdownList.SELECT_ALL ? null : pararms.getExtraInt("StandardDictionaryDropDown");
  42. var educationID = pararms.getExtraInt("DictionaryEducation") == DropdownList.SELECT_ALL ? null : pararms.getExtraInt("DictionaryEducation");
  43. var learningformID = pararms.getExtraInt("DictionaryLearningform") == DropdownList.SELECT_ALL ? null : pararms.getExtraInt("DictionaryLearningform");
  44. var learnSystem = pararms.getExtraString("DictionaryLearnSystem");
  45. //在校状态
  46. var inSchoolStatus = pararms.getExtraInt("DictionaryInschoolStatus") == DropdownList.SELECT_ALL ? null : pararms.getExtraInt("DictionaryInschoolStatus");
  47. return Json(ImportScoreServices.GetImportScoreViewGrid(configuretView, schoolyearID, collegeID, yearID,
  48. standardID, educationID, learningformID, learnSystem, inSchoolStatus, (int)pararms.page, (int)pararms.rows));
  49. }
  50. /// <summary>
  51. /// 复制新增
  52. /// </summary>
  53. /// <param name="importScoreID"></param>
  54. /// <returns></returns>
  55. public ActionResult CopyAdd(Guid ImportScoreID)
  56. {
  57. ImportScoreView importScoreView = new ImportScoreView();
  58. importScoreView = ImportScoreServices.GetImportScoreViewByID(ImportScoreID);
  59. return View("Edit", importScoreView);
  60. }
  61. /// <summary>
  62. /// 复制新增
  63. /// </summary>
  64. /// <param name="importScoreView"></param>
  65. /// <returns></returns>
  66. [HttpPost]
  67. public ActionResult CopyAdd(ImportScoreView importScoreView)
  68. {
  69. importScoreView.ImportScoreID = Guid.Empty;
  70. return this.Edit(importScoreView);
  71. }
  72. /// <summary>
  73. /// 编辑(新增、修改)
  74. /// </summary>
  75. /// <param name="importScoreID"></param>
  76. /// <returns></returns>
  77. [HttpGet]
  78. public ActionResult Edit(Guid? ImportScoreID)
  79. {
  80. ImportScoreView importScoreView = new ImportScoreView();
  81. if (ImportScoreID.HasValue && ImportScoreID != Guid.Empty)
  82. {
  83. importScoreView = ImportScoreServices.GetImportScoreViewByID(ImportScoreID.Value);
  84. }
  85. else
  86. {
  87. importScoreView.SchoolyearID = BaseExtensions.GetCurrentSchoolYearID();
  88. }
  89. return View(importScoreView);
  90. }
  91. /// <summary>
  92. /// 编辑(新增、修改)
  93. /// </summary>
  94. /// <param name="importScoreView"></param>
  95. /// <returns></returns>
  96. [HttpPost]
  97. public ActionResult Edit(ImportScoreView importScoreView)
  98. {
  99. try
  100. {
  101. ImportScoreServices.ImportScoreEdit(importScoreView);
  102. return Json(new ReturnMessage()
  103. {
  104. IsSuccess = true,
  105. Message = "保存成功。"
  106. });
  107. }
  108. catch (Exception ex)
  109. {
  110. return Json(new ReturnMessage()
  111. {
  112. IsSuccess = false,
  113. Message = "保存失败,原因:" + ex.Message + "。"
  114. });
  115. }
  116. }
  117. /// <summary>
  118. /// 删除
  119. /// </summary>
  120. /// <param name="importScoreIDs"></param>
  121. /// <returns></returns>
  122. [HttpPost]
  123. public ActionResult Delete(string importScoreIDs)
  124. {
  125. try
  126. {
  127. var importScoreIDList = importScoreIDs.Split(',').Where(x => !string.IsNullOrEmpty(x))
  128. .Select(x => new Guid(x)).ToList();
  129. ImportScoreServices.ImportScoreDelete(importScoreIDList);
  130. return base.Json("删除成功。");
  131. }
  132. catch (Exception ex)
  133. {
  134. return base.Json("删除失败,原因:" + ex.Message);
  135. }
  136. }
  137. /// <summary>
  138. /// Excel导出
  139. /// </summary>
  140. /// <returns></returns>
  141. [HttpPost]
  142. public ActionResult Excel()
  143. {
  144. NpoiExcelHelper neh = new NpoiExcelHelper();
  145. ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(null);
  146. var schoolyearID = Request.Form["SchoolyearDropdown"] == DropdownList.SELECT_ALL.ToString() ? null : Request.Form["SchoolyearDropdown"].ParseStrTo<Guid>();
  147. var collegeID = Request.Form["CollegeDropdown"].ParseStrTo<Guid>();
  148. var yearID = Request.Form["SchoolyearDictionaryDropDown"].ParseStrTo<int>() == DropdownList.SELECT_ALL ? null : Request.Form["SchoolyearDictionaryDropDown"].ParseStrTo<int>();
  149. var standardID = Request.Form["StandardDictionaryDropDown"].ParseStrTo<int>() == DropdownList.SELECT_ALL ? null : Request.Form["StandardDictionaryDropDown"].ParseStrTo<int>();
  150. var educationID = Request.Form["DictionaryEducation"].ParseStrTo<int>() == DropdownList.SELECT_ALL ? null : Request.Form["DictionaryEducation"].ParseStrTo<int>();
  151. var learningformID = Request.Form["DictionaryLearningform"].ParseStrTo<int>() == DropdownList.SELECT_ALL ? null : Request.Form["DictionaryLearningform"].ParseStrTo<int>();
  152. var learnSystem = Request.Form["DictionaryLearnSystem"].ToString();
  153. //在校状态
  154. var inSchoolStatus = Request.Form["DictionaryInschoolStatus"].ParseStrTo<int>() == DropdownList.SELECT_ALL ? null : Request.Form["DictionaryInschoolStatus"].ParseStrTo<int>();
  155. var dt = ImportScoreServices.GetImportScoreViewList(configuretView, schoolyearID, collegeID, yearID,
  156. standardID, educationID, learningformID, learnSystem, inSchoolStatus)
  157. .Select(x => new
  158. {
  159. x.SchoolyearCode,
  160. x.StudentNo,
  161. x.UserName,
  162. x.SexName,
  163. x.ClassNo,
  164. x.ClassName,
  165. x.GrademajorCode,
  166. x.GrademajorName,
  167. x.FacultymajorNo,
  168. x.FacultymajorName,
  169. x.CollegeNo,
  170. x.CollegeName,
  171. x.GradeID,
  172. x.StandardID,
  173. x.StandardCode,
  174. x.StandardName,
  175. x.EducationName,
  176. x.LearningformName,
  177. x.LearnSystem,
  178. x.CourseCode,
  179. x.CourseName,
  180. x.Score,
  181. x.Remark,
  182. x.ModifyUserNo,
  183. x.ModifyUserName,
  184. x.ModifyTime
  185. }).ToTable();
  186. string[] liststring = {
  187. "学年学期", "学号", "姓名", "性别", "班级编号", "班级名称", "年级专业编号", "年级专业名称",
  188. "院系专业编号", "院系专业名称", RSL.Get("CollegeCode"), RSL.Get("CollegeName"), "年级",
  189. "专业ID(Value)", "专业代码", "专业名称", RSL.Get("EducationID"), "学习形式",
  190. "学制", "课程代码", "课程名称", "平时成绩", "备注", "操作人编号", "操作人", "操作时间"
  191. };
  192. neh.Export(dt, liststring, "平时成绩信息" + DateTime.Now.ToString("yyyyMMdd"));
  193. return Json(new ReturnMessage()
  194. {
  195. IsSuccess = true,
  196. Message = "导出成功。"
  197. });
  198. }
  199. /// <summary>
  200. /// Excel导入
  201. /// </summary>
  202. /// <param name="errorFile"></param>
  203. /// <param name="operationTips"></param>
  204. /// <returns></returns>
  205. [HttpGet]
  206. public ActionResult Import(string errorFile, string operationTips)
  207. {
  208. ViewBag.ErrorFile = errorFile;
  209. if (string.IsNullOrEmpty(operationTips))
  210. {
  211. operationTips = "点击查看失败原因...";
  212. }
  213. ViewBag.operationTips = operationTips;
  214. return View();
  215. }
  216. /// <summary>
  217. /// Excel导入
  218. /// </summary>
  219. /// <param name="file"></param>
  220. /// <returns></returns>
  221. [HttpPost]
  222. public ActionResult Import(HttpPostedFileBase file)
  223. {
  224. try
  225. {
  226. if (!NpoiExcelHelper.GetIsCompatible(file.FileName))
  227. {
  228. throw new Exception("格式错误,只允许导入xls或xlsx格式的Excel文件。");
  229. }
  230. Dictionary<string, string> cellheader = new Dictionary<string, string>
  231. {
  232. { "SchoolyearCode", "学年学期" },
  233. { "StudentNo", "学号" },
  234. { "CourseCode", "课程代码" },
  235. { "ScoreStr", "平时成绩" },
  236. { "Remark", "备注" },
  237. { "ErrorMessage", "未导入原因" }
  238. };
  239. StringBuilder errorMsg = new StringBuilder(); // 错误信息
  240. string sourceWebPath = FileUploadHelper.UploadFile(file);
  241. var sourcePhysicalPath = Server.MapPath(sourceWebPath);
  242. List<ImportScoreView> errList = new List<ImportScoreView>();
  243. List<ImportScoreView> dataList = new List<ImportScoreView>();
  244. int? inCount = 0; //导入个数
  245. int? upCount = 0; //更新个数
  246. int? errCount = 0; //失败个数
  247. //导入
  248. ImportScoreServices.ImportScoreImport(cellheader, out inCount, out upCount, out errList, out errCount, sourcePhysicalPath);
  249. System.IO.File.Delete(sourcePhysicalPath);//删除本地缓存文件
  250. if (errList.Count() > 0)
  251. {
  252. //获取错误数据文件路径
  253. string errorWebPath = string.Format("{0}", NpoiExcelHelper.EntityListToExcel2003(cellheader, errList, "平时成绩导入失败文件", sourcePhysicalPath));
  254. ViewBag.ErrorFile = errorWebPath;
  255. string Errinfo = string.Format("提示:{0}条平时成绩导入成功,{1}条平时成绩更新成功,{2}条平时成绩导入失败,点击查看。", inCount, upCount, errCount);
  256. ViewBag.operationTips = Errinfo;
  257. return RedirectToAction("MsgShow", "Common", new
  258. {
  259. WindowID = "none",
  260. msg = Errinfo,
  261. url = Url.Action("Import").AddMenuParameter() + "&errorFile=" + errorWebPath + "&operationTips=" + Errinfo + "&WindowID=" + Request["WindowID"]
  262. });
  263. }
  264. else
  265. {
  266. string successInfo = string.Format("提示:{0}条平时成绩导入成功,{1}条平时成绩更新成功。", inCount, upCount);
  267. return RedirectToAction("MsgShow", "Common", new
  268. {
  269. WindowID = Request["WindowID"],
  270. msg = successInfo,
  271. url = Url.Action("List").AddMenuParameter()
  272. });
  273. }
  274. }
  275. catch (Exception ex)
  276. {
  277. return RedirectToAction("MsgShow", "Common", new
  278. {
  279. WindowID = "none",
  280. msg = "导入失败,原因:" + ex.Message,
  281. url = Url.Action("Import").AddMenuParameter() + "&WindowID=" + Request["WindowID"]
  282. });
  283. }
  284. }
  285. }
  286. }