OverallEducationService.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using Microsoft.Azure.Cosmos;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.Json;
  8. using System.Text.Json.Nodes;
  9. using System.Threading.Tasks;
  10. using TEAMModelOS.SDK.DI;
  11. using TEAMModelOS.SDK.Extension;
  12. using TEAMModelOS.SDK.Models.Cosmos.Student;
  13. namespace TEAMModelOS.SDK.Models
  14. {
  15. public static class OverallEducationService
  16. {
  17. public static async Task<string> GetAppraiseRecord(AzureCosmosFactory _azureCosmosFactory, string schoolId,string _studyYear, string semesterId,List<Student> students,HashSet<string> classIds)
  18. {
  19. StringBuilder sqlQ = new StringBuilder($"SELECT c.targetId,c.classId,c.id,c.academicYearId, b as appraise FROM c join b in c.nodes where c.code = 'AppraiseRecord-{schoolId}' and ARRAY_Contains(b.appraiseNode.path,'提问能力') ");
  20. if (!string.IsNullOrWhiteSpace(_studyYear)&& !string.IsNullOrWhiteSpace(semesterId))
  21. {
  22. sqlQ.Append($" and c.academicYearId ='{_studyYear}-{semesterId}'");
  23. }
  24. if (classIds.Count > 0)
  25. {
  26. sqlQ.Append($" and c.classId in ({string.Join(",",classIds.Select(x=>$"'{x}'"))})");
  27. }
  28. if (students.Count > 0)
  29. {
  30. sqlQ.Append($" and c.targetId in ({string.Join(",", students.Select(x => $"'{x.id}'"))})");
  31. }
  32. List<AppraiseRecordDto> appraiseRecords = new List<AppraiseRecordDto>();
  33. var result= await _azureCosmosFactory.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Student).GetList<AppraiseRecordDto>(sqlQ.ToString(), $"AppraiseRecord-{schoolId}");
  34. if (result.list.IsNotEmpty())
  35. {
  36. appraiseRecords.AddRange(result.list);
  37. }
  38. // 获取当前文化的日历
  39. CultureInfo culture = CultureInfo.CurrentCulture;
  40. Calendar calendar = culture.Calendar;
  41. Dictionary<string, List<AppraiseRecordDto>> dict = new Dictionary<string, List<AppraiseRecordDto>>();
  42. foreach (var appraiseRecord in appraiseRecords)
  43. {
  44. var date= DateTimeOffset.FromUnixTimeMilliseconds(appraiseRecord.appraise.createTime);
  45. int year = date.Year;
  46. // 计算当前时间是一年中的第几周
  47. int weekOfYear = calendar.GetWeekOfYear(date.DateTime, culture.DateTimeFormat.CalendarWeekRule, culture.DateTimeFormat.FirstDayOfWeek);
  48. string key = $"{appraiseRecord.targetId}::{year}-{weekOfYear}";
  49. if (dict.ContainsKey(key))
  50. {
  51. dict[key].Add(appraiseRecord);
  52. }
  53. else
  54. {
  55. dict[key] = new List<AppraiseRecordDto>() { appraiseRecord };
  56. }
  57. }
  58. foreach (var item in dict)
  59. {
  60. //正向评价-负向评价,评价次数
  61. int count = item.Value.Where(x => x.appraise.appraiseNode.isPraise).Count() - item.Value.Where(x =>! x.appraise.appraiseNode.isPraise).Count();
  62. double score = 100 / (1 + Math.Exp(-0.3 * count));//0.3是斜率,用于处理评分值曲线变化
  63. string[] sp = item.Key.Split();
  64. }
  65. //最新一周的数据 ,检查当前最新一周的数据是否大于历史均值,如果小于,则在平均次数上累加当前一周的数据,如果大于则累计在均值中。以防止当前周将均值拉下去。
  66. return null;
  67. }
  68. public static Dictionary<string, object> GetQueryCond(JsonElement request) {
  69. Dictionary<string, object> dict = new Dictionary<string, object>();
  70. if (request.TryGetProperty("periodId", out JsonElement periodId) && !string.IsNullOrWhiteSpace($"{periodId}"))
  71. {
  72. dict.Add("periodId", periodId);
  73. }
  74. if (request.TryGetProperty("classIds", out JsonElement classIds))
  75. {
  76. dict.Add("classId", classIds);
  77. }
  78. return dict;
  79. }
  80. public async static Task<(List<Class> classes, List<Student> students, List<Class> grade_classes, List<Student> grade_students, List<OverallEducation> overallEducations)>
  81. GetGradeData(CosmosClient client, HashSet<int> years,string _periodId,string _school,HashSet<string> classIds,string _studyYear,string _semesterId, AzureRedisFactory _azureRedis) {
  82. List<Class> classes = new List<Class>();
  83. List<Student> students = new List<Student>();
  84. List<Class> grade_classes = new List<Class>();
  85. List<Student> grade_students = new List<Student>();
  86. //StringBuilder sql = new StringBuilder($"select value c from c where c.year in ({string.Join(",", years)}) and c.periodId='{_periodId}' ");
  87. //if (classIds.Any() && classIds.Count>0)
  88. //{
  89. // sql.Append($" and c.id in ({string.Join(",", classIds.Select(z => $"'{z}'"))})");
  90. //}
  91. //当前条件的班级
  92. // var result = await client.GetContainer(Constant.TEAMModelOS, Constant.School).GetList<Class>(sql.ToString(), $"Class-{_school}");
  93. //classes = result.list;
  94. //StringBuilder studentCountSQL = new StringBuilder($"select c.id,c.name ,c.picture, c.classId,c.periodId ,c.year from c " +
  95. // $"where ( c.graduate = 0 or IS_DEFINED(c.graduate) = false) and c.classId in ({string.Join(",", classes.Select(z => $"'{z.id}'"))}) ");
  96. //当前条件的学生
  97. // var data = await client.GetContainer(Constant.TEAMModelOS, Constant.Student).GetList<Student>(studentCountSQL.ToString(), $"Base-{_school}");
  98. //获取年级的所有的班级
  99. StringBuilder gradeSql = new StringBuilder($"select value c from c where c.year in ({string.Join(",", years)}) and c.periodId='{_periodId}' ");
  100. var result_grade_classes = await client.GetContainer(Constant.TEAMModelOS, Constant.School).GetList<Class>(gradeSql.ToString(), $"Class-{_school}");
  101. grade_classes = result_grade_classes.list;
  102. //------获取当前条件的班级
  103. classes= grade_classes.FindAll(z => classIds!=null && classIds.Any() && classIds.Count>0 ? classIds.Contains(z.id):true && z.periodId.Equals(_periodId) && years.Contains(z.year));
  104. string classin = string.Empty;
  105. if (classes.IsNotEmpty())
  106. {
  107. classin=$" {string.Join(",", grade_classes.Select(z => $"'{z.id}'"))} ";
  108. }
  109. else
  110. {
  111. classin="'--00000--'";
  112. }
  113. StringBuilder grade_studentCountSQL = new StringBuilder($"select c.id,c.name ,c.picture, c.classId,c.periodId ,c.year from c " +
  114. $" where c.classId in ( {classin} ) and c.periodId='{_periodId}' and ( c.graduate = 0 or IS_DEFINED(c.graduate) = false) ");
  115. //获取年级所有学生
  116. var gradeData = await client.GetContainer(Constant.TEAMModelOS, Constant.Student).GetList<Student>(grade_studentCountSQL.ToString(), $"Base-{_school}");
  117. grade_students = gradeData.list;
  118. //------获取当前条件的学生
  119. students = grade_students.FindAll(z => classes.Select(x => x.id).Contains(z.classId) && z.periodId.Equals(_periodId));
  120. //获取缓存的五育数据
  121. //1.先从redis获取部分。
  122. List<OverallEducation> overallEducations = new List<OverallEducation>();
  123. foreach (var grade_class in grade_classes)
  124. {
  125. string key = $"OverallEducation:{_school}:{_periodId}:{_studyYear}:{_semesterId}:{grade_class.id}";
  126. var records = await _azureRedis.GetRedisClient(8).HashGetAllAsync(key);
  127. foreach (var rcd in records)
  128. {
  129. var value = rcd.Value.ToString().ToObject<OverallEducation>();
  130. if (value != null && !string.IsNullOrWhiteSpace(value.studentId))
  131. {
  132. overallEducations.Add(value);
  133. }
  134. }
  135. }
  136. //2.整个年级的学生基础信息,对比redis获取的差,看是否是有部门未缓存在Redis,缓存没有数据的学生。
  137. var no_overall_stduents = grade_students.ExceptBy(overallEducations.Select(z => z.studentId), v => v.id);
  138. if (no_overall_stduents.Any() && no_overall_stduents.Count() > 0)
  139. {
  140. string oesql = $"select value c from c where c.semesterId='{_semesterId}' and c.year={_studyYear} and c.periodId='{_periodId}' and c.studentId in ({string.Join(",", no_overall_stduents.Select(z => $"'{z.id}'"))}) ";
  141. var oeresults = await client.GetContainer(Constant.TEAMModelOS, Constant.Student).GetList<OverallEducation>(oesql, $"OverallEducation-{_school}");
  142. if (oeresults.list.IsNotEmpty())
  143. {
  144. overallEducations.AddRange(oeresults.list);
  145. //将获取到的最新数据缓存在redis中。下次则不需要再次获取
  146. foreach (var item in oeresults.list)
  147. {
  148. string key = $"OverallEducation:{item.schoolCode}:{item.periodId}:{item.year}:{item.semesterId}:{item.classId}";
  149. await _azureRedis.GetRedisClient(8).HashSetAsync(key, item.studentId, item.ToJsonString());
  150. await _azureRedis.GetRedisClient(8).KeyExpireAsync(key, new TimeSpan(180 * 24, 0, 0));
  151. }
  152. }
  153. }
  154. return (classes, students, grade_classes, grade_students, overallEducations);
  155. }
  156. }
  157. }