ImportScoreServices.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Data;
  8. using System.IO;
  9. using System.Web;
  10. using System.Web.Mvc;
  11. using System.Transactions;
  12. using Bowin.Common.Data;
  13. using Bowin.Common.Linq;
  14. using Bowin.Common.Utility;
  15. using Bowin.Common.Linq.Entity;
  16. using EMIS.Utility;
  17. using EMIS.Entities;
  18. using EMIS.ViewModel;
  19. using EMIS.ViewModel.ScoreManage;
  20. using EMIS.ViewModel.CalendarManage;
  21. using EMIS.DataLogic.ScoreManage;
  22. using EMIS.CommonLogic.Students;
  23. using EMIS.CommonLogic.StudentManage.StudentStatistics;
  24. namespace EMIS.CommonLogic.ScoreManage
  25. {
  26. public class ImportScoreServices : BaseServices, IImportScoreServices
  27. {
  28. public ImportScoreDAL ImportScoreDAL { get; set; }
  29. public IInSchoolSettingServices InSchoolSettingServices { get; set; }
  30. /// <summary>
  31. /// 查询对应的导入成绩(平时成绩)信息View
  32. /// </summary>
  33. /// <param name="configuretView"></param>
  34. /// <param name="schoolyearID"></param>
  35. /// <param name="collegeID"></param>
  36. /// <param name="yearID"></param>
  37. /// <param name="standardID"></param>
  38. /// <param name="educationID"></param>
  39. /// <param name="learningformID"></param>
  40. /// <param name="learnSystem"></param>
  41. /// <param name="inSchoolStatus"></param>
  42. /// <param name="pageIndex"></param>
  43. /// <param name="pageSize"></param>
  44. /// <returns></returns>
  45. public IGridResultSet<ImportScoreView> GetImportScoreViewGrid(ConfiguretView configuretView, Guid? schoolyearID,
  46. Guid? collegeID, int? yearID, int? standardID, int? educationID, int? learningformID, string learnSystem,
  47. int? inSchoolStatus, int pageIndex, int pageSize)
  48. {
  49. //导入成绩
  50. Expression<Func<ER_ImportScore, bool>> expImportScore = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE);
  51. if (schoolyearID.HasValue)
  52. {
  53. expImportScore = expImportScore.And(x => x.SchoolyearID == schoolyearID);
  54. }
  55. //学生信息
  56. Expression<Func<CF_Student, bool>> expStudent = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE);
  57. if (inSchoolStatus != null && inSchoolStatus > -1)
  58. {
  59. var inschoolStatusList = InSchoolSettingServices.GetInschoolStatusList(true);
  60. if (inSchoolStatus == 1)
  61. {
  62. //表示在校
  63. expStudent = expStudent.And(x => inschoolStatusList.Contains(x.InSchoolStatusID));
  64. }
  65. if (inSchoolStatus == 0)
  66. {
  67. //不在校
  68. expStudent = expStudent.And(x => !inschoolStatusList.Contains(x.InSchoolStatusID));
  69. }
  70. }
  71. var query = ImportScoreDAL.GetImportScoreViewQueryable(expImportScore, expStudent);
  72. if (collegeID.HasValue)
  73. {
  74. query = query.Where(x => x.CollegeID == collegeID);
  75. }
  76. if (yearID.HasValue)
  77. {
  78. query = query.Where(x => x.GradeID == yearID);
  79. }
  80. if (standardID.HasValue)
  81. {
  82. query = query.Where(x => x.StandardID == standardID);
  83. }
  84. if (educationID.HasValue)
  85. {
  86. query = query.Where(x => x.EducationID == educationID);
  87. }
  88. if (learningformID.HasValue)
  89. {
  90. query = query.Where(x => x.LearningformID == learningformID);
  91. }
  92. if (!string.IsNullOrEmpty(learnSystem) && learnSystem != "-1")
  93. {
  94. var LearnSystems = Convert.ToDecimal(learnSystem);
  95. query = query.Where(x => x.LearnSystem == LearnSystems);
  96. }
  97. //查询条件
  98. if (!string.IsNullOrEmpty(configuretView.ConditionValue))
  99. {
  100. query = query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue.Trim());
  101. }
  102. return this.GetQueryByDataRangeByCollege(query)
  103. .OrderBy(x => x.StudentNo.Length).ThenBy(x => x.StudentNo)
  104. .ThenBy(x => x.SchoolyearCode).ThenBy(x => x.CourseCode.Length)
  105. .ThenBy(x => x.CourseCode)
  106. .ToGridResultSet<ImportScoreView>(pageIndex, pageSize);
  107. }
  108. /// <summary>
  109. /// 查询对应的导入成绩(平时成绩)信息List
  110. /// </summary>
  111. /// <param name="configuretView"></param>
  112. /// <param name="schoolyearID"></param>
  113. /// <param name="collegeID"></param>
  114. /// <param name="yearID"></param>
  115. /// <param name="standardID"></param>
  116. /// <param name="educationID"></param>
  117. /// <param name="learningformID"></param>
  118. /// <param name="learnSystem"></param>
  119. /// <param name="inSchoolStatus"></param>
  120. /// <returns></returns>
  121. public List<ImportScoreView> GetImportScoreViewList(ConfiguretView configuretView, Guid? schoolyearID,
  122. Guid? collegeID, int? yearID, int? standardID, int? educationID, int? learningformID, string learnSystem,
  123. int? inSchoolStatus)
  124. {
  125. //导入成绩
  126. Expression<Func<ER_ImportScore, bool>> expImportScore = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE);
  127. if (schoolyearID.HasValue)
  128. {
  129. expImportScore = expImportScore.And(x => x.SchoolyearID == schoolyearID);
  130. }
  131. //学生信息
  132. Expression<Func<CF_Student, bool>> expStudent = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE);
  133. if (inSchoolStatus != null && inSchoolStatus > -1)
  134. {
  135. var inschoolStatusList = InSchoolSettingServices.GetInschoolStatusList(true);
  136. if (inSchoolStatus == 1)
  137. {
  138. //表示在校
  139. expStudent = expStudent.And(x => inschoolStatusList.Contains(x.InSchoolStatusID));
  140. }
  141. if (inSchoolStatus == 0)
  142. {
  143. //不在校
  144. expStudent = expStudent.And(x => !inschoolStatusList.Contains(x.InSchoolStatusID));
  145. }
  146. }
  147. var query = ImportScoreDAL.GetImportScoreViewQueryable(expImportScore, expStudent);
  148. if (collegeID.HasValue)
  149. {
  150. query = query.Where(x => x.CollegeID == collegeID);
  151. }
  152. if (yearID.HasValue)
  153. {
  154. query = query.Where(x => x.GradeID == yearID);
  155. }
  156. if (standardID.HasValue)
  157. {
  158. query = query.Where(x => x.StandardID == standardID);
  159. }
  160. if (educationID.HasValue)
  161. {
  162. query = query.Where(x => x.EducationID == educationID);
  163. }
  164. if (learningformID.HasValue)
  165. {
  166. query = query.Where(x => x.LearningformID == learningformID);
  167. }
  168. if (!string.IsNullOrEmpty(learnSystem) && learnSystem != "-1")
  169. {
  170. var LearnSystems = Convert.ToDecimal(learnSystem);
  171. query = query.Where(x => x.LearnSystem == LearnSystems);
  172. }
  173. //查询条件
  174. if (!string.IsNullOrEmpty(configuretView.ConditionValue))
  175. {
  176. query = query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue.Trim());
  177. }
  178. return this.GetQueryByDataRangeByCollege(query)
  179. .OrderBy(x => x.StudentNo.Length).ThenBy(x => x.StudentNo)
  180. .ThenBy(x => x.SchoolyearCode).ThenBy(x => x.CourseCode.Length)
  181. .ThenBy(x => x.CourseCode)
  182. .ToList();
  183. }
  184. /// <summary>
  185. /// 查询对应的导入成绩信息ImportScoreView(根据主键ID)
  186. /// </summary>
  187. /// <param name="importScoreID"></param>
  188. /// <returns></returns>
  189. public ImportScoreView GetImportScoreViewByID(Guid importScoreID)
  190. {
  191. try
  192. {
  193. Expression<Func<ER_ImportScore, bool>> expImportScore = (x => x.ImportScoreID == importScoreID);
  194. Expression<Func<CF_Student, bool>> expStudent = (x => true);
  195. return this.ImportScoreDAL.GetImportScoreViewQueryable(expImportScore, expStudent)
  196. .SingleOrDefault();
  197. }
  198. catch (Exception ex)
  199. {
  200. throw new Exception(ex.Message);
  201. }
  202. }
  203. /// <summary>
  204. /// 编辑(新增、修改,业务主键:学年学期ID、用户ID、课程信息ID)
  205. /// </summary>
  206. /// <param name="importScoreView"></param>
  207. public void ImportScoreEdit(ImportScoreView importScoreView)
  208. {
  209. try
  210. {
  211. //查询数据库进行验证
  212. var importScoreVerification = ImportScoreDAL.ImportScoreRepository
  213. .GetList(x => x.ImportScoreID != importScoreView.ImportScoreID
  214. && x.SchoolyearID == importScoreView.SchoolyearID
  215. && x.UserID == importScoreView.UserID
  216. && x.CoursematerialID == importScoreView.CoursematerialID)
  217. .SingleOrDefault();
  218. if (importScoreVerification == null)
  219. {
  220. //数据有误验证
  221. if (importScoreView.ImportScoreID != Guid.Empty)
  222. {
  223. var importScore = ImportScoreDAL.ImportScoreRepository
  224. .GetList(x => x.ImportScoreID == importScoreView.ImportScoreID)
  225. .SingleOrDefault();
  226. if (importScore == null)
  227. {
  228. throw new Exception("数据有误,请核查");
  229. }
  230. else
  231. {
  232. //表示修改
  233. importScore.SchoolyearID = importScoreView.SchoolyearID;
  234. importScore.UserID = importScoreView.UserID;
  235. importScore.CoursematerialID = importScoreView.CoursematerialID;
  236. importScore.Score = importScoreView.Score;
  237. importScore.Remark = importScoreView.Remark;
  238. SetModifyStatus(importScore);
  239. }
  240. }
  241. else
  242. {
  243. //表示新增
  244. ER_ImportScore importScore = new ER_ImportScore();
  245. importScore.ImportScoreID = Guid.NewGuid();
  246. importScore.SchoolyearID = importScoreView.SchoolyearID;
  247. importScore.UserID = importScoreView.UserID;
  248. importScore.CoursematerialID = importScoreView.CoursematerialID;
  249. importScore.Score = importScoreView.Score;
  250. importScore.Remark = importScoreView.Remark;
  251. SetNewStatus(importScore);
  252. UnitOfWork.Add(importScore);
  253. }
  254. }
  255. else
  256. {
  257. throw new Exception("已存在相同的平时成绩,请核查");
  258. }
  259. //事务提交
  260. UnitOfWork.Commit();
  261. }
  262. catch (Exception ex)
  263. {
  264. throw new Exception(ex.Message);
  265. }
  266. }
  267. /// <summary>
  268. /// 删除
  269. /// </summary>
  270. /// <param name="importScoreIDList"></param>
  271. /// <returns></returns>
  272. public bool ImportScoreDelete(IList<Guid> importScoreIDList)
  273. {
  274. try
  275. {
  276. UnitOfWork.Remove<ER_ImportScore>(x => importScoreIDList.Contains(x.ImportScoreID));
  277. UnitOfWork.Commit();
  278. return true;
  279. }
  280. catch (Exception)
  281. {
  282. throw;
  283. }
  284. }
  285. /// <summary>
  286. /// Excel导入
  287. /// </summary>
  288. /// <param name="cellheader"></param>
  289. /// <param name="inCount"></param>
  290. /// <param name="upCount"></param>
  291. /// <param name="errdataList"></param>
  292. /// <param name="errCount"></param>
  293. /// <param name="sourcePhysicalPath"></param>
  294. public void ImportScoreImport(Dictionary<string, string> cellheader, out int? inCount, out int? upCount, out List<ImportScoreView> errdataList, out int? errCount, string sourcePhysicalPath)
  295. {
  296. try
  297. {
  298. StringBuilder errorMsg = new StringBuilder();
  299. List<ImportScoreView> errList = new List<ImportScoreView>();
  300. cellheader.Remove("ErrorMessage");
  301. List<ImportScoreView> enlist = NpoiExcelHelper.ExcelToEntityList<ImportScoreView>(cellheader, sourcePhysicalPath, out errorMsg, out errList);
  302. cellheader.Add("ErrorMessage", "未导入原因");
  303. //对List集合进行有效性校验
  304. if (enlist.Count() <= 0)
  305. {
  306. throw new Exception("Excel文件数据为空,请检查。");
  307. }
  308. //Regex reg = null; //正则表达式
  309. decimal isDecimal; //用于返回判断decimal列格式
  310. inCount = 0; //导入个数
  311. upCount = 0; //更新个数
  312. errCount = 0; //失败个数
  313. string errorMsgStr = ""; //错误信息
  314. List<ER_ImportScore> importScoreInList = new List<ER_ImportScore>();
  315. List<ER_ImportScore> importScoreUpList = new List<ER_ImportScore>();
  316. //将循环中相关数据库查询统一查询出来进行匹配(尽量避免在循环中进行数据库查询)
  317. //学年学期
  318. var schoolyearCodeList = enlist.Where(x => !string.IsNullOrEmpty(x.SchoolyearCode)).Select(x => x.SchoolyearCode).ToList();
  319. //查询对应的学年学期List
  320. var schoolyearList = ImportScoreDAL.SchoolyearRepository.GetList(x => schoolyearCodeList.Contains(x.Code)).ToList();
  321. //对应的学年学期信息schoolyearIDList
  322. var schoolyearIDList = schoolyearList.Select(x => x.SchoolyearID).ToList();
  323. //学号
  324. var studentNoList = enlist.Where(x => !string.IsNullOrEmpty(x.StudentNo)).Select(x => x.StudentNo).ToList();
  325. //查询对应的学生信息List
  326. var studentList = ImportScoreDAL.StudentRepository.GetList(x => studentNoList.Contains(x.Sys_User.LoginID), (x => x.Sys_User)).ToList();
  327. //对应的学生信息userIDList
  328. var userIDList = studentList.Select(x => x.UserID).ToList();
  329. //课程代码
  330. var CourseCodeList = enlist.Where(x => !string.IsNullOrEmpty(x.CourseCode)).Select(x => x.CourseCode).ToList();
  331. //查询对应的课程信息List
  332. var courseList = ImportScoreDAL.CoursematerialRepository.GetList(x => CourseCodeList.Contains(x.CourseCode)).ToList();
  333. //对应的课程信息coursematerialIDList
  334. var coursematerialIDList = courseList.Select(x => x.CoursematerialID).ToList();
  335. //平时成绩信息List(暂时只根据schoolyearIDList查询)
  336. var importScoreList = ImportScoreDAL.ImportScoreRepository.GetList(x => schoolyearIDList.Contains(x.SchoolyearID.Value)).ToList();
  337. //对比后的成绩信息List(再比对userIDList、coursematerialIDList)
  338. importScoreList = importScoreList.Where(x => userIDList.Contains(x.UserID.Value) && coursematerialIDList.Contains(x.CoursematerialID.Value)).ToList();
  339. //循环检测数据列,对各数据列进行验证(必填、字典项验证、数据格式等)
  340. for (int i = 0; i < enlist.Count; i++)
  341. {
  342. ImportScoreView en = enlist[i]; //Excel表数据视图
  343. ER_ImportScore importScore = new ER_ImportScore();
  344. //学年学期
  345. if (string.IsNullOrEmpty(en.SchoolyearCode))
  346. {
  347. errCount++;
  348. errorMsgStr = "学年学期不能为空";
  349. en.ErrorMessage = errorMsgStr;
  350. errList.Add(en);
  351. errorMsg.AppendLine(errorMsgStr);
  352. continue;
  353. }
  354. else
  355. {
  356. var schoolyear = schoolyearList.Where(x => x.Code == en.SchoolyearCode.Trim()).SingleOrDefault();
  357. if (schoolyear == null)
  358. {
  359. errCount++;
  360. errorMsgStr = "学年学期不存在,请检查";
  361. en.ErrorMessage = errorMsgStr;
  362. errList.Add(en);
  363. errorMsg.AppendLine(errorMsgStr);
  364. continue;
  365. }
  366. else
  367. {
  368. //学年学期ID
  369. importScore.SchoolyearID = schoolyear.SchoolyearID;
  370. }
  371. }
  372. //学号
  373. if (string.IsNullOrEmpty(en.StudentNo))
  374. {
  375. errCount++;
  376. errorMsgStr = "学号不能为空";
  377. en.ErrorMessage = errorMsgStr;
  378. errList.Add(en);
  379. errorMsg.AppendLine(errorMsgStr);
  380. continue;
  381. }
  382. else
  383. {
  384. var student = studentList.Where(x => x.Sys_User.LoginID == en.StudentNo.Trim()).SingleOrDefault();
  385. if (student == null)
  386. {
  387. errCount++;
  388. errorMsgStr = "学号不存在,请检查";
  389. en.ErrorMessage = errorMsgStr;
  390. errList.Add(en);
  391. errorMsg.AppendLine(errorMsgStr);
  392. continue;
  393. }
  394. else
  395. {
  396. //用户ID
  397. importScore.UserID = student.UserID;
  398. }
  399. }
  400. //课程代码
  401. if (string.IsNullOrEmpty(en.CourseCode))
  402. {
  403. errCount++;
  404. errorMsgStr = "课程代码不能为空";
  405. en.ErrorMessage = errorMsgStr;
  406. errList.Add(en);
  407. errorMsg.AppendLine(errorMsgStr);
  408. continue;
  409. }
  410. else
  411. {
  412. var course = courseList.Where(x => x.CourseCode == en.CourseCode.Trim()).SingleOrDefault();
  413. if (course == null)
  414. {
  415. errCount++;
  416. errorMsgStr = "课程代码不存在,请检查";
  417. en.ErrorMessage = errorMsgStr;
  418. errList.Add(en);
  419. errorMsg.AppendLine(errorMsgStr);
  420. continue;
  421. }
  422. else
  423. {
  424. //课程信息ID
  425. importScore.CoursematerialID = course.CoursematerialID;
  426. }
  427. }
  428. //平时成绩
  429. if (string.IsNullOrEmpty(en.ScoreStr))
  430. {
  431. errCount++;
  432. errorMsgStr = "平时成绩不能为空";
  433. en.ErrorMessage = errorMsgStr;
  434. errList.Add(en);
  435. errorMsg.AppendLine(errorMsgStr);
  436. continue;
  437. }
  438. else
  439. {
  440. if (!Decimal.TryParse(en.ScoreStr.Trim(), out isDecimal))
  441. {
  442. errCount++;
  443. errorMsgStr = "平时成绩格式不正确,请检查";
  444. en.ErrorMessage = errorMsgStr;
  445. errList.Add(en);
  446. errorMsg.AppendLine(errorMsgStr);
  447. continue;
  448. }
  449. else
  450. {
  451. //平时成绩
  452. if (isDecimal < 0)
  453. {
  454. errCount++;
  455. errorMsgStr = "平时成绩不能小于0,请检查";
  456. en.ErrorMessage = errorMsgStr;
  457. errList.Add(en);
  458. errorMsg.AppendLine(errorMsgStr);
  459. continue;
  460. }
  461. else
  462. {
  463. importScore.Score = Convert.ToDecimal(en.ScoreStr.Trim());
  464. }
  465. }
  466. }
  467. //备注
  468. importScore.Remark = en.Remark;
  469. //数据表重复性验证(学年学期ID、用户ID、课程信息ID唯一)
  470. var importScoreVerify = importScoreList
  471. .Where(x => x.SchoolyearID == importScore.SchoolyearID
  472. && x.UserID == importScore.UserID
  473. && x.CoursematerialID == importScore.CoursematerialID)
  474. .SingleOrDefault();
  475. if (importScoreVerify == null)
  476. {
  477. //新增
  478. if (!importScoreInList.Any(x => x.SchoolyearID == importScore.SchoolyearID
  479. && x.UserID == importScore.UserID
  480. && x.CoursematerialID == importScore.CoursematerialID))
  481. {
  482. importScore.ImportScoreID = Guid.NewGuid();
  483. SetNewStatus(importScore);
  484. importScoreInList.Add(importScore);
  485. inCount++;
  486. }
  487. else
  488. {
  489. //Excel表重复性验证
  490. //(注:当数据表中没有此记录,但是Excel中有重复数据,可在此处进行抛出到失败数据文件中,目前暂不考虑)
  491. inCount++;
  492. }
  493. }
  494. else
  495. {
  496. //更新(Excel有重复时,以最后一条记录的更新为准)
  497. importScoreVerify.Score = importScore.Score;
  498. importScoreVerify.Remark = importScore.Remark;
  499. SetModifyStatus(importScoreVerify);
  500. importScoreUpList.Add(importScoreVerify);
  501. upCount++;
  502. }
  503. }
  504. using (TransactionScope ts = new TransactionScope())
  505. {
  506. UnitOfWork.BulkInsert(importScoreInList);
  507. if (importScoreUpList != null && importScoreUpList.Count() > 0)
  508. {
  509. UnitOfWork.BatchUpdate(importScoreUpList);
  510. }
  511. ts.Complete();
  512. }
  513. errdataList = errList.Distinct().ToList();
  514. }
  515. catch (Exception)
  516. {
  517. throw;
  518. }
  519. }
  520. }
  521. }