ProjectScoreServices.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Text;
  6. using System.Transactions;
  7. using Bowin.Common.Linq;
  8. using Bowin.Common.Linq.Entity;
  9. using EMIS.CommonLogic.CalendarManage;
  10. using EMIS.DataLogic.ExamManage;
  11. using EMIS.DataLogic.ScoreManage;
  12. using EMIS.Entities;
  13. using EMIS.ViewModel;
  14. using EMIS.ViewModel.ScoreManage;
  15. namespace EMIS.CommonLogic.ScoreManage
  16. {
  17. public class ProjectScoreServices : BaseServices, IProjectScoreServices
  18. {
  19. public LevelSettingSubjectDAL LevelSettingSubjectDAL { get; set; }
  20. public ExaminationScoreDAL ExaminationScoreDAL { get; set; }
  21. public ProjectScoreDAL ProjectScoreDAL { get; set; }
  22. public ResitDAL ResitDAL { get; set; }
  23. public Lazy<ISchoolYearServices> SchoolYearServices { get; set; }
  24. public IGridResultSet<ProjectScoreView> GetProjectScoreViewGrid(ConfiguretView conditionView, Guid? schoolyearID, Guid? examinationTypeID,
  25. Guid? examinationProjectID, int? schoolAreaID, Guid? collegeID, int? yearID, int? standardID, Guid? classmajorID, bool? hasTrain, int? pageIndex, int? pageSize)
  26. {
  27. Expression<Func<ER_ProjectScore, bool>> exp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE);
  28. Expression<Func<EX_ExaminationProject, bool>> projectExp = (x => true);
  29. Expression<Func<CF_Facultymajor, bool>> facultExp = (x => true);
  30. Expression<Func<CF_Grademajor, bool>> grademajorExp = (x => true);
  31. Expression<Func<CF_Classmajor, bool>> classmajorExp = (x => true);
  32. if (schoolyearID.HasValue)
  33. {
  34. exp = exp.And(x => x.SchoolyearID == schoolyearID);
  35. }
  36. if (examinationTypeID.HasValue)
  37. {
  38. projectExp = projectExp.And(x => x.ExaminationTypeID == examinationTypeID);
  39. }
  40. if (examinationProjectID.HasValue)
  41. {
  42. exp = exp.And(x => x.ExaminationProjectID == examinationProjectID);
  43. }
  44. if (schoolAreaID.HasValue)
  45. {
  46. grademajorExp = grademajorExp.And(x => x.SchoolAreaID == schoolAreaID);
  47. }
  48. if (collegeID.HasValue)
  49. {
  50. facultExp = facultExp.And(x => x.CollegeID == collegeID);
  51. }
  52. if (yearID.HasValue)
  53. {
  54. grademajorExp = grademajorExp.And(x => x.SchoolyearID == yearID);
  55. }
  56. if (standardID.HasValue)
  57. {
  58. facultExp = facultExp.And(x => x.StandardID == standardID);
  59. }
  60. if (classmajorID.HasValue)
  61. {
  62. classmajorExp = classmajorExp.And(x => x.ClassmajorID == classmajorID);
  63. }
  64. var query = ProjectScoreDAL.GetProjectScoreViewQueryable(exp, projectExp, facultExp, grademajorExp, classmajorExp);
  65. if (hasTrain.HasValue)
  66. {
  67. if (hasTrain.Value)
  68. {
  69. query = query.Where(x => x.TrainingClassID != null);
  70. }
  71. else
  72. {
  73. query = query.Where(x => x.TrainingClassID == null);
  74. }
  75. }
  76. if (!string.IsNullOrEmpty(conditionView.ConditionValue) && !string.IsNullOrEmpty(conditionView.Attribute))
  77. {
  78. query = query.DynamicWhere(conditionView.Attribute, conditionView.Condition, conditionView.ConditionValue);
  79. }
  80. //var result = GetQueryByDataRangeByCollege(query);
  81. return query.OrderByDescending(x => x.SchoolyearCode).ThenBy(x => x.ExaminationTypeName)
  82. .ThenBy(x => x.ExaminationProjectName).ThenBy(x => x.ClassmajorName).ThenBy(x => x.UserName)
  83. .ToGridResultSet<ProjectScoreView>(pageIndex, pageSize);
  84. }
  85. public List<ProjectScoreView> GetProjectScoreViewList(ConfiguretView conditionView, Guid? schoolyearID, Guid? examinationTypeID, Guid? examinationProjectID,
  86. int? schoolAreaID, Guid? collegeID, int? yearID, int? standardID, Guid? classmajorID, bool? hasTrain)
  87. {
  88. Expression<Func<ER_ProjectScore, bool>> exp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE);
  89. Expression<Func<EX_ExaminationProject, bool>> projectExp = (x => true);
  90. Expression<Func<CF_Facultymajor, bool>> facultExp = (x => true);
  91. Expression<Func<CF_Grademajor, bool>> grademajorExp = (x => true);
  92. Expression<Func<CF_Classmajor, bool>> classmajorExp = (x => true);
  93. if (schoolyearID.HasValue)
  94. {
  95. exp = exp.And(x => x.SchoolyearID == schoolyearID);
  96. }
  97. if (examinationTypeID.HasValue)
  98. {
  99. projectExp = projectExp.And(x => x.ExaminationTypeID == examinationTypeID);
  100. }
  101. if (examinationProjectID.HasValue)
  102. {
  103. exp = exp.And(x => x.ExaminationProjectID == examinationProjectID);
  104. }
  105. if (schoolAreaID.HasValue)
  106. {
  107. grademajorExp = grademajorExp.And(x => x.SchoolAreaID == schoolAreaID);
  108. }
  109. if (collegeID.HasValue)
  110. {
  111. facultExp = facultExp.And(x => x.CollegeID == collegeID);
  112. }
  113. if (yearID.HasValue)
  114. {
  115. grademajorExp = grademajorExp.And(x => x.SchoolyearID == yearID);
  116. }
  117. if (standardID.HasValue)
  118. {
  119. facultExp = facultExp.And(x => x.StandardID == standardID);
  120. }
  121. if (classmajorID.HasValue)
  122. {
  123. classmajorExp = classmajorExp.And(x => x.ClassmajorID == classmajorID);
  124. }
  125. var query = ProjectScoreDAL.GetProjectScoreViewQueryable(exp, projectExp, facultExp, grademajorExp, classmajorExp);
  126. if (hasTrain.HasValue)
  127. {
  128. if (hasTrain.Value)
  129. {
  130. query = query.Where(x => x.TrainingClassID != null);
  131. }
  132. else
  133. {
  134. query = query.Where(x => x.TrainingClassID == null);
  135. }
  136. }
  137. if (!string.IsNullOrEmpty(conditionView.ConditionValue) && !string.IsNullOrEmpty(conditionView.Attribute))
  138. {
  139. query = query.DynamicWhere(conditionView.Attribute, conditionView.Condition, conditionView.ConditionValue);
  140. }
  141. //var result = GetQueryByDataRangeByCollege(query);
  142. return this.GetQueryByAssistant(query, (x => x.ClassmajorID), this.GetQueryByDataRangeByCollege(query)).OrderByDescending(x => x.SchoolyearCode).ThenBy(x => x.ExaminationTypeName)
  143. .ThenBy(x => x.ExaminationProjectName).ThenBy(x => x.ClassmajorName).ThenBy(x => x.UserName)
  144. .ToList();
  145. }
  146. //对所有不及格而又没有产生过补考记录的科目成绩生成补考
  147. //不一定是不及格的科目,也包括完全匹配不上任何成绩等级的科目
  148. //生成补考记录必须保证在该科至少有一项有有效期的成绩,贞媚说第一科考试失败是没办法补考的2019-06-25
  149. //流程:先找出对应的及格线和当前范围考试的科目成绩,对比后看看哪个是不及格的,再找当前批次的补考记录,看看哪个是没有生成过补考名单的,
  150. //再找上次正常考试之后的所有补考成绩,看看次数够了没,
  151. //再找是否存在有效期内的成绩,最后生成补考
  152. private List<ER_Resit> GenerateResit(List<ER_LevelSetting> levelSettingList,
  153. List<EX_ExaminationProjectSubject> subjectList, List<ProjectScoreGenerateScoreView> allScoreList,
  154. List<ProjectScoreGenerateScoreView> curScoreList)
  155. {
  156. var nowTime = DateTime.Today;
  157. var passLevelSettingList = levelSettingList.Where(x => x.IsPassLine == true).ToList();
  158. var examinationScoreIDList = curScoreList.Select(x => x.ExaminationScoreID).Distinct().ToList();
  159. var curResitList = ResitDAL.ResitRepository.GetList(x => examinationScoreIDList.Contains(x.ResitID)).ToList();
  160. var insertResitList = new List<ER_Resit>();
  161. foreach (var score in curScoreList)
  162. {
  163. var subject = subjectList.FirstOrDefault(x => x.ExaminationProjectID == score.ExaminationProjectID && x.ExaminationSubjectID == score.ExaminationSubjectID);
  164. if (subject == null)
  165. {
  166. continue;
  167. }
  168. var passLevelSetting = passLevelSettingList.Where(x => x.ExaminationProjectID == score.ExaminationProjectID)
  169. .SelectMany(x => x.ER_LevelSettingSubject).FirstOrDefault(x => x.ExaminationSubjectID == score.ExaminationSubjectID);
  170. if (passLevelSetting == null)
  171. {
  172. continue;
  173. }
  174. if (score.Score >= passLevelSetting.PassScore)
  175. {
  176. continue;
  177. }
  178. if (curResitList.Any(x => x.ExaminationScoreID == score.ExaminationScoreID))
  179. {
  180. continue;
  181. }
  182. if (!allScoreList.Any(x => x.ExaminationProjectID == score.ExaminationProjectID && x.UserID == score.UserID && x.Score >= passLevelSetting.PassScore && (x.Expire >= nowTime || x.Expire == null)))
  183. {
  184. continue;
  185. }
  186. var subjectScoreList = allScoreList.Where(x => x.ExaminationProjectID == score.ExaminationProjectID && x.ExaminationSubjectID == score.ExaminationSubjectID);
  187. var lastNormalExamTime = subjectScoreList.Where(x => x.IsResit != true).Max(x => x.BatchStartDate);
  188. var resitCount = subjectScoreList.Where(x => x.IsResit == true && x.BatchStartDate > lastNormalExamTime).Count();
  189. if (subject.ResitCount <= resitCount)
  190. {
  191. continue;
  192. }
  193. var resit = new ER_Resit()
  194. {
  195. ResitID = Guid.NewGuid(),
  196. ExaminationScoreID = score.ExaminationScoreID
  197. };
  198. this.SetNewStatus(resit);
  199. insertResitList.Add(resit);
  200. }
  201. return insertResitList;
  202. }
  203. public void GenerateProjectScore(Guid schoolyearID, Guid? examinationBatchID, Guid? examinationTypeID, Guid? examinationProjectID)
  204. {
  205. var nowTime = DateTime.Today;
  206. Expression<Func<EX_ExaminationBatch, bool>> batchExp = (x => x.SchoolyearID == schoolyearID);
  207. Expression<Func<EX_ExaminationProject, bool>> projectExp = (x => true);
  208. if (examinationBatchID.HasValue)
  209. {
  210. batchExp = batchExp.And(x => x.ExaminationBatchID == examinationBatchID);
  211. }
  212. if (examinationTypeID.HasValue)
  213. {
  214. projectExp = projectExp.And(x => x.ExaminationTypeID == examinationTypeID);
  215. }
  216. if (examinationProjectID.HasValue)
  217. {
  218. projectExp = projectExp.And(x => x.ExaminationProjectID == examinationProjectID);
  219. }
  220. //读出和范围相关的所有考试成绩(这里不分有效期,因为不及格的成绩有效期是无效的)
  221. var allScore = ProjectScoreDAL.GetAllExaminationScoreViewByBatchStudent(batchExp, projectExp).ToList();
  222. var curScoreList = ProjectScoreDAL.GetExaminationScoreViewByBatchStudent(batchExp, projectExp).ToList();
  223. var projectIDList = allScore.Select(x => x.ExaminationProjectID).Distinct().ToList();
  224. var projectSubjectList = ProjectScoreDAL.ExaminationProjectRepository.GetList(x => projectIDList.Contains(x.ExaminationProjectID), (x => x.EX_ExaminationProjectSubject)).ToList();
  225. var projectUserIDLIst = allScore.GroupBy(x => new { ExaminationProjectID = x.ExaminationProjectID, UserID = x.UserID }).Select(x => x.Key).ToList();
  226. var dbProjectScoreList = ProjectScoreDAL.ProjectScoreRepository.Entities.SelectByKeys(projectUserIDLIst);
  227. var levelSettingList = LevelSettingSubjectDAL.levelSettingRepository.GetList(x => projectIDList.Contains(x.ExaminationProjectID) && x.RecordStatus > (int)SYS_STATUS.UNUSABLE,
  228. (x => x.ER_LevelSettingSubject)).ToList();
  229. var schoolyear = SchoolYearServices.Value.GetCurrentSchoolYear();
  230. var dbcertisfierDistributeList = ProjectScoreDAL.CertisfierDistributeRepository.GetList(x => projectIDList.Contains(x.ExaminationProjectID)).ToList();
  231. var insertList = new List<ER_ProjectScore>();
  232. var certisfierDistributeList = new List<ER_CertisfierDistribute>();
  233. foreach (var projectUser in projectUserIDLIst)
  234. {
  235. var subjectList = projectSubjectList.Where(x => x.ExaminationProjectID == projectUser.ExaminationProjectID).SelectMany(x => x.EX_ExaminationProjectSubject).ToList();
  236. var projectLevelSettingList = levelSettingList.Where(x => x.ExaminationProjectID == projectUser.ExaminationProjectID)
  237. .OrderByDescending(x => subjectList
  238. .Join(x.ER_LevelSettingSubject, (w => w.ExaminationSubjectID), (w => w.ExaminationSubjectID), ((w,v) => v.PassScore))
  239. .DefaultIfEmpty()
  240. .Average(w => w == null ? 0 : w)).ToList();
  241. var passLevelSetting = projectLevelSettingList.FirstOrDefault(x => x.IsPassLine == true);
  242. var scoreList = allScore.Where(x => x.ExaminationProjectID == projectUser.ExaminationProjectID && x.UserID == projectUser.UserID).ToList();
  243. var userCurScoreList = curScoreList.Where(x => x.ExaminationProjectID == projectUser.ExaminationProjectID && x.UserID == projectUser.UserID).ToList();
  244. foreach (var levelSetting in projectLevelSettingList)
  245. {
  246. bool allMatch = true;
  247. var newProjectScore = new ER_ProjectScore();
  248. foreach (var levelSubject in levelSetting.ER_LevelSettingSubject)
  249. {
  250. var subjectMaxScore = scoreList.Where(x => x.ExaminationSubjectID == levelSubject.ExaminationSubjectID && (x.Expire == null || x.Expire >= nowTime)).Max(x => x.Score);
  251. if (subjectMaxScore == null || subjectMaxScore < levelSubject.PassScore)
  252. {
  253. allMatch = false;
  254. break;
  255. }
  256. var score = scoreList.Where(x => x.ExaminationSubjectID == levelSubject.ExaminationSubjectID && (x.Expire == null || x.Expire >= nowTime) && x.Score == subjectMaxScore).FirstOrDefault();
  257. newProjectScore.SchoolyearID = score.SchoolYearID;
  258. }
  259. if (allMatch)
  260. {
  261. newProjectScore.ProjectScoreID = Guid.NewGuid();
  262. newProjectScore.ExaminationProjectID = projectUser.ExaminationProjectID;
  263. newProjectScore.UserID = projectUser.UserID;
  264. newProjectScore.LevelSettingID = levelSetting.LevelSettingID;
  265. this.SetNewStatus(newProjectScore);
  266. insertList.Add(newProjectScore);
  267. break;
  268. }
  269. }
  270. }
  271. foreach (var proScore in insertList)
  272. {
  273. var ispass = levelSettingList.FirstOrDefault(x => x.LevelSettingID == proScore.LevelSettingID).IsPassed;
  274. if (ispass.Value && dbcertisfierDistributeList.FirstOrDefault(x => x.ExaminationProjectID == proScore.ExaminationProjectID && x.UserID == proScore.UserID) == null)
  275. {
  276. ER_CertisfierDistribute certisfierDistribute = new ER_CertisfierDistribute();
  277. certisfierDistribute.CertisfierDistributeID = Guid.NewGuid();
  278. certisfierDistribute.ExaminationProjectID = proScore.ExaminationProjectID;
  279. certisfierDistribute.UserID = proScore.UserID;
  280. certisfierDistribute.SchoolyearID = proScore.SchoolyearID;
  281. SetNewStatus<ER_CertisfierDistribute>(certisfierDistribute);
  282. SetModifyStatus<ER_CertisfierDistribute>(certisfierDistribute);
  283. certisfierDistributeList.Add(certisfierDistribute);
  284. }
  285. }
  286. var insertResitList = GenerateResit(levelSettingList, projectSubjectList.SelectMany(x => x.EX_ExaminationProjectSubject).ToList(), allScore, curScoreList);
  287. var deleteResitScoreIDList = curScoreList.Select(x => (Guid?)x.ExaminationScoreID).Distinct().ToList();
  288. using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions { IsolationLevel = IsolationLevel.Serializable }))
  289. {
  290. UnitOfWork.Delete(dbProjectScoreList);
  291. UnitOfWork.BulkInsert(insertList);
  292. UnitOfWork.Delete<ER_Resit>(x => deleteResitScoreIDList.Contains(x.ExaminationScoreID));
  293. UnitOfWork.BulkInsert(insertResitList);
  294. UnitOfWork.BulkInsert(certisfierDistributeList);
  295. scope.Complete();
  296. }
  297. }
  298. public IGridResultSet<ProjectScoreDetailView> GetProjectScoreDetailViewGrid(ConfiguretView conditionView, Guid projectScoreID, int? pageIndex, int? pageSize)
  299. {
  300. Expression<Func<ER_ProjectScore, bool>> exp = (x => x.ProjectScoreID == projectScoreID);
  301. var query = ProjectScoreDAL.GetProjectScoreDetailViewQueryable(exp);
  302. if (!string.IsNullOrEmpty(conditionView.ConditionValue) && !string.IsNullOrEmpty(conditionView.Attribute))
  303. {
  304. query = query.DynamicWhere(conditionView.Attribute, conditionView.Condition, conditionView.ConditionValue);
  305. }
  306. return query.OrderByDescending(x => x.CreateTime).ToGridResultSet(pageIndex, pageSize);
  307. }
  308. }
  309. }