LevelScoreServices.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Transactions;
  7. using Bowin.Common.Utility;
  8. using EMIS.Entities;
  9. using EMIS.ViewModel.ScoreManage.LevelScoreManage;
  10. namespace EMIS.ExtensionLogic.ServiceLogic.ScoreManage
  11. {
  12. public class LevelScoreServices : EMIS.CommonLogic.ScoreManage.LevelScoreManage.LevelScoreServices
  13. {
  14. /// <summary>
  15. /// 编辑
  16. /// 注:处理工作流程状态(河北工业大学)
  17. /// </summary>
  18. /// <param name="levelScoreView"></param>
  19. public override void LevelScoreEdit(LevelScoreView levelScoreView)
  20. {
  21. try
  22. {
  23. var approveStatusList = this.GetStatusViewList();
  24. if (approveStatusList == null || approveStatusList.Count() <= 0)
  25. {
  26. throw new Exception("工作流平台中,科目成绩录入流程未配置,请核查。");
  27. }
  28. var startStatusID = this.GetStartStatus();
  29. if (startStatusID == null)
  30. {
  31. throw new Exception("工作流平台中,科目成绩录入流程开始环节未配置,请核查。");
  32. }
  33. var correctEndStatusID = this.GetCorrectEndStatus();
  34. if (correctEndStatusID == null)
  35. {
  36. throw new Exception("工作流平台中,科目成绩录入流程结束环节未配置,请核查。");
  37. }
  38. ER_LevelScore levelScoreVerify = null;
  39. var levelScoreList = LevelScoreDAL.Value.LevelScoreRepository.GetList(x => x.LevelScoreID != levelScoreView.LevelScoreID && x.UserID == levelScoreView.UserID && x.ExaminationSubjectID == levelScoreView.ExaminationSubjectID).ToList();
  40. if (levelScoreList != null && levelScoreList.Count() > 0)
  41. {
  42. levelScoreVerify = levelScoreList.Where(x => x.ExaminationDate.ToStringEx("yyyyMM") == levelScoreView.ExaminationDate.ToStringEx("yyyyMM")).SingleOrDefault();
  43. }
  44. if (levelScoreVerify == null)
  45. {
  46. if (levelScoreView.LevelScoreID != Guid.Empty)
  47. {
  48. var levelScore = LevelScoreDAL.Value.LevelScoreRepository.GetList(x => x.LevelScoreID == levelScoreView.LevelScoreID).SingleOrDefault();
  49. if (levelScore == null)
  50. {
  51. throw new Exception("数据有误,请核查。");
  52. }
  53. else
  54. {
  55. //表示修改
  56. levelScore.UserID = levelScoreView.UserID;
  57. levelScore.ExaminationSubjectID = levelScoreView.ExaminationSubjectID;
  58. levelScore.SchoolyearID = levelScoreView.SchoolyearID;
  59. levelScore.ExaminationDate = levelScoreView.ExaminationDate;
  60. levelScore.ScoreNo = levelScoreView.ScoreNo;
  61. levelScore.TotalScore = levelScoreView.TotalScore;
  62. levelScore.Remark = levelScoreView.Remark;
  63. SetModifyStatus(levelScore);
  64. }
  65. }
  66. else
  67. {
  68. //表示新增
  69. var newLevelScore = new ER_LevelScore();
  70. newLevelScore.LevelScoreID = Guid.NewGuid();
  71. newLevelScore.UserID = levelScoreView.UserID;
  72. newLevelScore.ExaminationSubjectID = levelScoreView.ExaminationSubjectID;
  73. newLevelScore.SchoolyearID = levelScoreView.SchoolyearID;
  74. newLevelScore.ExaminationDate = levelScoreView.ExaminationDate;
  75. newLevelScore.ScoreNo = levelScoreView.ScoreNo;
  76. newLevelScore.TotalScore = levelScoreView.TotalScore;
  77. newLevelScore.Remark = levelScoreView.Remark;
  78. newLevelScore.ApprovalStatus = startStatusID;
  79. SetNewStatus(newLevelScore);
  80. UnitOfWork.Add(newLevelScore);
  81. }
  82. }
  83. else
  84. {
  85. throw new Exception("已存在相同的科目成绩信息(学生信息、考试科目、考试日期唯一),请核查。");
  86. }
  87. UnitOfWork.Commit();
  88. }
  89. catch (Exception ex)
  90. {
  91. throw new Exception(ex.Message);
  92. }
  93. }
  94. /// <summary>
  95. /// 等级成绩Excel导入
  96. /// 注:处理工作流程状态(河北工业大学)
  97. /// </summary>
  98. /// <param name="cellheader"></param>
  99. /// <param name="inCount"></param>
  100. /// <param name="upCount"></param>
  101. /// <param name="errdataList"></param>
  102. /// <param name="errCount"></param>
  103. /// <param name="sourcePhysicalPath"></param>
  104. public override void LevelScoreImport(Dictionary<string, string> cellheader, out int? inCount, out int? upCount, out List<LevelScoreView> errdataList, out int? errCount, string sourcePhysicalPath)
  105. {
  106. try
  107. {
  108. var approveStatusList = this.GetStatusViewList();
  109. if (approveStatusList == null || approveStatusList.Count() <= 0)
  110. {
  111. throw new Exception("工作流平台中,科目成绩录入流程未配置,请核查。");
  112. }
  113. var startStatusID = this.GetStartStatus();
  114. if (startStatusID == null)
  115. {
  116. throw new Exception("工作流平台中,科目成绩录入流程开始环节未配置,请核查。");
  117. }
  118. var correctEndStatusID = this.GetCorrectEndStatus();
  119. if (correctEndStatusID == null)
  120. {
  121. throw new Exception("工作流平台中,科目成绩录入流程结束环节未配置,请核查。");
  122. }
  123. StringBuilder errorMsg = new StringBuilder();
  124. var errList = new List<LevelScoreView>();
  125. cellheader.Remove("ErrorMessage");
  126. var enlist = NpoiExcelHelper.ExcelToEntityList<LevelScoreView>(cellheader, sourcePhysicalPath, out errorMsg, out errList);
  127. cellheader.Add("ErrorMessage", "未导入原因");
  128. //对List集合进行有效性校验
  129. if (enlist.Count() <= 0)
  130. {
  131. throw new Exception("Excel文件数据为空,请检查。");
  132. }
  133. Regex reg = null;
  134. DateTime result;
  135. decimal isDecimal;
  136. inCount = 0;
  137. upCount = 0;
  138. errCount = 0;
  139. string errorMsgStr = "";
  140. List<ER_LevelScore> levelScoreInList = new List<ER_LevelScore>();
  141. List<ER_LevelScore> levelScoreUpList = new List<ER_LevelScore>();
  142. //将循环中相关数据库查询统一查询出来进行匹配(尽量避免在循环中进行数据库查询)
  143. //学号
  144. var studentNoList = enlist.Where(x => !string.IsNullOrEmpty(x.StudentNo)).Select(x => x.StudentNo).ToList();
  145. var studentList = LevelScoreDAL.Value.StudentRepository.GetList(x => studentNoList.Contains(x.Sys_User.LoginID), (x => x.Sys_User)).ToList();
  146. var userIDList = studentList.Select(x => x.UserID).ToList();
  147. var examinationSubjectNameList = enlist.Where(x => !string.IsNullOrEmpty(x.ExaminationSubjectName)).Select(x => x.ExaminationSubjectName).ToList();
  148. var examinationSubjectList = LevelScoreDAL.Value.ExaminationSubjectRepository.GetList(x => examinationSubjectNameList.Contains(x.Name)).ToList();
  149. var examinationSubjectIDList = examinationSubjectList.Select(x => x.ExaminationSubjectID).ToList();
  150. var schoolyearCodeList = enlist.Where(x => !string.IsNullOrEmpty(x.SchoolyearCode)).Select(x => x.SchoolyearCode).ToList();
  151. var schoolyearList = LevelScoreDAL.Value.SchoolyearRepository.GetList(x => schoolyearCodeList.Contains(x.Code)).ToList();
  152. var schoolyearIDList = schoolyearList.Select(x => x.SchoolyearID).ToList();
  153. var levelScoreList = LevelScoreDAL.Value.LevelScoreRepository.GetList(x => schoolyearIDList.Contains(x.SchoolyearID.Value)).ToList();
  154. levelScoreList = levelScoreList.Where(x => userIDList.Contains(x.UserID.Value) && examinationSubjectIDList.Contains(x.ExaminationSubjectID.Value)).ToList();
  155. //循环检测数据列,对各数据列进行验证(必填、字典项验证、数据格式等)
  156. for (int i = 0; i < enlist.Count; i++)
  157. {
  158. LevelScoreView en = enlist[i];
  159. ER_LevelScore newLevelScore = new ER_LevelScore();
  160. //学号
  161. if (string.IsNullOrEmpty(en.StudentNo))
  162. {
  163. errCount++;
  164. errorMsgStr = "学号不能为空";
  165. en.ErrorMessage = errorMsgStr;
  166. errList.Add(en);
  167. errorMsg.AppendLine(errorMsgStr);
  168. continue;
  169. }
  170. else
  171. {
  172. var student = studentList.Where(x => x.Sys_User.LoginID == en.StudentNo.Trim()).SingleOrDefault();
  173. if (student == null)
  174. {
  175. errCount++;
  176. errorMsgStr = "学号不存在,请检查";
  177. en.ErrorMessage = errorMsgStr;
  178. errList.Add(en);
  179. errorMsg.AppendLine(errorMsgStr);
  180. continue;
  181. }
  182. else
  183. {
  184. newLevelScore.UserID = student.UserID;
  185. }
  186. }
  187. //考试科目
  188. if (string.IsNullOrEmpty(en.ExaminationSubjectName))
  189. {
  190. errCount++;
  191. errorMsgStr = "考试科目不能为空";
  192. en.ErrorMessage = errorMsgStr;
  193. errList.Add(en);
  194. errorMsg.AppendLine(errorMsgStr);
  195. continue;
  196. }
  197. else
  198. {
  199. var examinationSubject = examinationSubjectList.Where(x => x.Name == en.ExaminationSubjectName.Trim()).SingleOrDefault();
  200. if (examinationSubject == null)
  201. {
  202. errCount++;
  203. errorMsgStr = "考试科目不存在,请检查";
  204. en.ErrorMessage = errorMsgStr;
  205. errList.Add(en);
  206. errorMsg.AppendLine(errorMsgStr);
  207. continue;
  208. }
  209. else
  210. {
  211. newLevelScore.ExaminationSubjectID = examinationSubject.ExaminationSubjectID;
  212. }
  213. }
  214. //学年学期
  215. if (string.IsNullOrEmpty(en.SchoolyearCode))
  216. {
  217. errCount++;
  218. errorMsgStr = "学年学期不能为空";
  219. en.ErrorMessage = errorMsgStr;
  220. errList.Add(en);
  221. errorMsg.AppendLine(errorMsgStr);
  222. continue;
  223. }
  224. else
  225. {
  226. var schoolyear = schoolyearList.Where(x => x.Code == en.SchoolyearCode.Trim()).SingleOrDefault();
  227. if (schoolyear == null)
  228. {
  229. errCount++;
  230. errorMsgStr = "学年学期不存在,请检查";
  231. en.ErrorMessage = errorMsgStr;
  232. errList.Add(en);
  233. errorMsg.AppendLine(errorMsgStr);
  234. continue;
  235. }
  236. else
  237. {
  238. newLevelScore.SchoolyearID = schoolyear.SchoolyearID;
  239. }
  240. }
  241. //考试日期
  242. if (string.IsNullOrWhiteSpace(en.ExaminationDateStr))
  243. {
  244. errCount++;
  245. errorMsgStr = "考试日期不能为空";
  246. en.ErrorMessage = errorMsgStr;
  247. errList.Add(en);
  248. errorMsg.AppendLine(errorMsgStr);
  249. continue;
  250. }
  251. else
  252. {
  253. //reg = new Regex(@"(\d{4})-(\d{1,2})-(\d{1,2})"); //日期正则表达式,2017-12-28
  254. if (!DateTime.TryParse(en.ExaminationDateStr, out result))
  255. {
  256. errCount++;
  257. errorMsgStr = "考试日期格式不正确,请检查";
  258. en.ErrorMessage = errorMsgStr;
  259. errList.Add(en);
  260. errorMsg.AppendLine(errorMsgStr);
  261. continue;
  262. }
  263. else
  264. {
  265. newLevelScore.ExaminationDate = Convert.ToDateTime(en.ExaminationDateStr);
  266. }
  267. }
  268. //成绩单编号
  269. if (string.IsNullOrWhiteSpace(en.ScoreNo))
  270. {
  271. //不考虑
  272. }
  273. else
  274. {
  275. reg = new Regex(@"^[0-9a-zA-Z\s?]+$"); //正则表达式(请输入数字或英文字母)
  276. if (!reg.IsMatch(en.ScoreNo))
  277. {
  278. errCount++;
  279. errorMsgStr = "成绩单编号格式不正确,请检查(数字或英文字母)";
  280. en.ErrorMessage = errorMsgStr;
  281. errList.Add(en);
  282. errorMsg.AppendLine(errorMsgStr);
  283. continue;
  284. }
  285. else
  286. {
  287. newLevelScore.ScoreNo = en.ScoreNo.Trim();
  288. }
  289. }
  290. //总成绩
  291. if (string.IsNullOrEmpty(en.TotalScoreStr))
  292. {
  293. errCount++;
  294. errorMsgStr = "总成绩不能为空";
  295. en.ErrorMessage = errorMsgStr;
  296. errList.Add(en);
  297. errorMsg.AppendLine(errorMsgStr);
  298. continue;
  299. }
  300. else
  301. {
  302. if (!Decimal.TryParse(en.TotalScoreStr.Trim(), out isDecimal))
  303. {
  304. errCount++;
  305. errorMsgStr = "总成绩格式不正确,请检查";
  306. en.ErrorMessage = errorMsgStr;
  307. errList.Add(en);
  308. errorMsg.AppendLine(errorMsgStr);
  309. continue;
  310. }
  311. else
  312. {
  313. newLevelScore.TotalScore = Convert.ToDecimal(en.TotalScoreStr.Trim());
  314. }
  315. }
  316. //备注
  317. if (string.IsNullOrWhiteSpace(en.Remark))
  318. {
  319. //不考虑
  320. }
  321. else
  322. {
  323. newLevelScore.Remark = en.Remark;
  324. }
  325. //数据表重复性验证(用户ID、考试科目ID、考试日期唯一)
  326. var levelScoreVerify = levelScoreList.Where(x => x.UserID == newLevelScore.UserID && x.ExaminationSubjectID == newLevelScore.ExaminationSubjectID && x.ExaminationDate.ToStringEx("yyyyMM") == newLevelScore.ExaminationDate.ToStringEx("yyyyMM")).SingleOrDefault();
  327. if (levelScoreVerify == null)
  328. {
  329. //新增
  330. if (!levelScoreInList.Any(x => x.UserID == newLevelScore.UserID && x.ExaminationSubjectID == newLevelScore.ExaminationSubjectID && x.ExaminationDate.ToStringEx("yyyyMM") == newLevelScore.ExaminationDate.ToStringEx("yyyyMM")))
  331. {
  332. newLevelScore.LevelScoreID = Guid.NewGuid();
  333. newLevelScore.ApprovalStatus = startStatusID;
  334. SetNewStatus(newLevelScore);
  335. levelScoreInList.Add(newLevelScore);
  336. inCount++;
  337. }
  338. else
  339. {
  340. //Excel表重复性验证
  341. //(注:当数据表中没有此记录,但是Excel中有重复数据,可在此处进行抛出到失败数据文件中,目前暂不考虑)
  342. inCount++;
  343. }
  344. }
  345. else
  346. {
  347. //更新(Excel有重复时,以最后一条记录的更新为准)
  348. if (levelScoreVerify.ApprovalStatus != correctEndStatusID)
  349. {
  350. levelScoreVerify.SchoolyearID = newLevelScore.SchoolyearID;
  351. levelScoreVerify.ScoreNo = newLevelScore.ScoreNo;
  352. levelScoreVerify.TotalScore = newLevelScore.TotalScore;
  353. levelScoreVerify.Remark = newLevelScore.Remark;
  354. SetModifyStatus(levelScoreVerify);
  355. levelScoreUpList.Add(levelScoreVerify);
  356. upCount++;
  357. }
  358. else
  359. {
  360. errCount++;
  361. errorMsgStr = "存在已审核通过的相同的考试科目(学生信息、考试科目、考试日期唯一),请检查";
  362. en.ErrorMessage = errorMsgStr;
  363. errList.Add(en);
  364. errorMsg.AppendLine(errorMsgStr);
  365. continue;
  366. }
  367. }
  368. }
  369. TransactionOptions transactionOption = new TransactionOptions();
  370. transactionOption.IsolationLevel = System.Transactions.IsolationLevel.Serializable;
  371. transactionOption.Timeout = new TimeSpan(0, 2, 0);
  372. using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required, transactionOption))
  373. {
  374. UnitOfWork.BulkInsert(levelScoreInList);
  375. if (levelScoreUpList != null && levelScoreUpList.Count() > 0)
  376. {
  377. UnitOfWork.BatchUpdate(levelScoreUpList);
  378. }
  379. ts.Complete();
  380. }
  381. errdataList = errList.Distinct().ToList();
  382. }
  383. catch (Exception)
  384. {
  385. throw;
  386. }
  387. }
  388. }
  389. }