StudentScoreDAL.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data.Entity;
  6. using System.Linq.Expressions;
  7. using EMIS.Entities;
  8. using EMIS.DataLogic.Repositories;
  9. namespace EMIS.ExtensionLogic.DataLogic.ScoreManage
  10. {
  11. public class StudentScoreDAL : EMIS.DataLogic.ScoreManage.StudentScoreDAL
  12. {
  13. public CourseTypeSelectCourseTypeRepository CourseTypeSelectCourseTypeRepository { get; set; }
  14. public override IQueryable<ER_SubmitedScore> GetLastSubmittedScoreBeforDelete(Expression<Func<ER_SubmitedScore, bool>> exp)
  15. {
  16. var maxSubmitedScoreList = (
  17. from ss in submitedScoreRepository.Entities
  18. join nss in submitedScoreRepository.GetList(exp)
  19. on new { ss.UserID, ss.CoursematerialID, ss.StarttermID }
  20. equals new { nss.UserID, nss.CoursematerialID, nss.StarttermID }
  21. join ctsc in CourseTypeSelectCourseTypeRepository.Entities
  22. on ss.CourseTypeID equals ctsc.CourseTypeID into dctsc
  23. from ctsc in dctsc.DefaultIfEmpty()
  24. join mss in
  25. (from ssl in submitedScoreRepository.Entities
  26. join nss in submitedScoreRepository.GetList(exp)
  27. on new { ssl.UserID, ssl.CoursematerialID, ssl.StarttermID }
  28. equals new { nss.UserID, nss.CoursematerialID, nss.StarttermID }
  29. join ctsc1 in CourseTypeSelectCourseTypeRepository.Entities
  30. on ssl.CourseTypeID equals ctsc1.CourseTypeID into dctsc1
  31. from ctsc1 in dctsc1.DefaultIfEmpty()
  32. where ssl.SubmitedScoreID != nss.SubmitedScoreID
  33. group new { Score = ssl, SelectCourseType = ctsc1 } by new { ssl.UserID, ssl.CoursematerialID, ssl.StarttermID }
  34. into g
  35. select new
  36. {
  37. g.Key.UserID,
  38. g.Key.CoursematerialID,
  39. g.Key.StarttermID,
  40. IsRequired = g.Max(x => (x.SelectCourseType.IsRequired == true) ? 1 : 0) == 1,
  41. MaxCreateTime = g.Max(x => (x.SelectCourseType.IsRequired == true) ? 1 : 0) == 1 ? g.Max(x => (x.SelectCourseType.IsRequired == true) ? x.Score.CreateTime : null) : g.Max(x => x.Score.CreateTime)
  42. })
  43. on new { ss.UserID, ss.CoursematerialID, ss.StarttermID, IsRequired = ctsc.IsRequired.Value, ss.CreateTime }
  44. equals new { mss.UserID, mss.CoursematerialID, mss.StarttermID, IsRequired = mss.IsRequired, CreateTime = mss.MaxCreateTime }
  45. where ss.SubmitedScoreID != nss.SubmitedScoreID
  46. group ss by new { ss.UserID, ss.CoursematerialID, ss.StarttermID, ss.CreateTime } into g
  47. select (g.Max(x => x.SubmitedScoreID.ToString()))
  48. );
  49. //这个算法,需要找条重录完后还有成绩的学生来试试
  50. var lastSubmittedScoreList = (from mss in maxSubmitedScoreList
  51. join ssl in submitedScoreRepository.Entities
  52. on mss equals ssl.SubmitedScoreID.ToString()
  53. select ssl);
  54. return lastSubmittedScoreList;
  55. }
  56. public override IQueryable<ER_SubmitedScore> GetLastSubmittedScore(Expression<Func<ER_SubmitedScore, bool>> exp)
  57. {
  58. var maxSubmitedScoreList = (
  59. from ss in submitedScoreRepository.Entities
  60. join ctsc in CourseTypeSelectCourseTypeRepository.Entities
  61. on ss.CourseTypeID equals ctsc.CourseTypeID into dctsc
  62. from ctsc in dctsc.DefaultIfEmpty()
  63. join mss in
  64. (
  65. from ssl in submitedScoreRepository.GetList(exp)
  66. join ssm in submitedScoreRepository.Entities on new
  67. {
  68. ssl.UserID,
  69. ssl.CoursematerialID,
  70. ssl.StarttermID
  71. } equals new { ssm.UserID, ssm.CoursematerialID, ssm.StarttermID }
  72. join ctsc1 in CourseTypeSelectCourseTypeRepository.Entities
  73. on ssm.CourseTypeID equals ctsc1.CourseTypeID into dctsc1
  74. from ctsc1 in dctsc1.DefaultIfEmpty()
  75. group new { Score = ssm, SelectCourseType = ctsc1 }
  76. by new { ssm.UserID, ssm.CoursematerialID, ssm.StarttermID }
  77. into g
  78. select new
  79. {
  80. g.Key.UserID,
  81. g.Key.CoursematerialID,
  82. g.Key.StarttermID,
  83. IsRequired = g.Max(x => x.SelectCourseType.IsRequired.Value ? 1 : 0) == 1,
  84. MaxCreateTime = (g.Max(x => x.SelectCourseType.IsRequired.Value ? 1 : 0) == 1) ? g.Max(x => (x.SelectCourseType.IsRequired.Value) ? x.Score.CreateTime : null) : g.Max(x => x.Score.CreateTime)
  85. }
  86. )
  87. on
  88. new { ss.UserID, ss.CoursematerialID, ss.StarttermID, IsRequired = ctsc.IsRequired.Value, ss.CreateTime }
  89. equals
  90. new { mss.UserID, mss.CoursematerialID, mss.StarttermID, IsRequired = mss.IsRequired, CreateTime = mss.MaxCreateTime }
  91. group ss by new { ss.UserID, ss.CoursematerialID, ss.StarttermID, ss.CreateTime }
  92. into g
  93. select g.Max(x => x.SubmitedScoreID.ToString())
  94. );
  95. var lastSubmittedScoreList = (from ssl in submitedScoreRepository.Entities
  96. join mss in maxSubmitedScoreList
  97. on ssl.SubmitedScoreID.ToString() equals mss
  98. select ssl);
  99. return lastSubmittedScoreList;
  100. }
  101. }
  102. }