StuListService.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using Azure.Cosmos;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Text.Json;
  6. using System.Threading.Tasks;
  7. using TEAMModelOS.SDK.DI;
  8. using TEAMModelOS.SDK.Extension;
  9. using TEAMModelOS.SDK;
  10. using TEAMModelOS.SDK.Models;
  11. using TEAMModelOS.SDK.Models.Cosmos.Common;
  12. namespace TEAMModelFunction
  13. {
  14. public class StuListService
  15. {
  16. public static async Task FixActivity(CosmosClient client,DingDing _dingDing, StuListChange stuListChange, string type)
  17. {
  18. try
  19. {
  20. var query = $"SELECT distinct c.owner, c.id,c.code, c.classes,c.subjects,c.progress,c.scope,c.startTime,c.school,c.creatorId,c.name,c.pk ,c.endTime FROM c join A1 in c.classes where c.pk='{type}' and A1 in('{stuListChange.listid}') ";
  21. List<MQActivity> datas = new List<MQActivity>();
  22. if (stuListChange.scope.Equals("school", StringComparison.OrdinalIgnoreCase) && !string.IsNullOrEmpty(stuListChange.school)) {
  23. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<MQActivity>(queryText: query,
  24. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"{type}-{stuListChange.school}") }))
  25. {
  26. datas.Add(item);
  27. }
  28. ///还要处理该学校每个老师发布的班级的
  29. }
  30. if (stuListChange.scope.Equals("private", StringComparison.OrdinalIgnoreCase)&&!string.IsNullOrEmpty(stuListChange.creatorId))
  31. {
  32. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<MQActivity>(queryText: query,
  33. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"{type}-{stuListChange.creatorId}") }))
  34. {
  35. datas.Add(item);
  36. }
  37. }
  38. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-StuListService-FixActivity\n名单发生变更 需要处理的活动\n{datas.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
  39. foreach (MQActivity activity in datas)
  40. {
  41. //已经完结的不再允许加入
  42. if (activity.progress.Equals("finish") || activity.progress.Equals("pending"))
  43. {
  44. continue;
  45. }
  46. //学生新加入名单的
  47. foreach (Students students in stuListChange.stujoin)
  48. {
  49. var stucourse = new StuActivity
  50. {
  51. id = activity.id,
  52. scode = activity.code,
  53. name = activity.name,
  54. code = $"Activity-{activity.school}-{students.id}",
  55. scope = activity.scope,
  56. school = activity.school,
  57. creatorId = activity.creatorId,
  58. pk = "Activity",
  59. type = type,
  60. subjects = activity.pk.ToLower().Equals("exam") && activity.subjects.IsNotEmpty() ? new List<string>() { activity.subjects[0].id } : new List<string>() { "" },
  61. startTime = activity.startTime,
  62. endTime = activity.endTime,
  63. blob = activity.blob,
  64. owner = activity.owner
  65. };
  66. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-StuListService-FixActivity\n名单发生变更 新建活动中间表\n{stucourse.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
  67. await client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
  68. }//tmd新加入的
  69. foreach (string tmdid in stuListChange.tmdjoin)
  70. {
  71. var stucourse = new StuActivity
  72. {
  73. id = activity.id,
  74. scode = activity.code,
  75. name = activity.name,
  76. code = $"Activity-{tmdid}",
  77. scope = activity.scope,
  78. school = activity.school,
  79. creatorId = activity.creatorId,
  80. pk = "Activity",
  81. type = type,
  82. subjects = activity.pk.ToLower().Equals("exam") && activity.subjects.IsNotEmpty() ? new List<string>() { activity.subjects[0].id } : new List<string>() { "" },
  83. startTime = activity.startTime,
  84. endTime = activity.endTime,
  85. blob = activity.blob,
  86. owner = activity.owner
  87. };
  88. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-StuListService-FixActivity\n名单发生变更 新建活动中间表\n{stucourse.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
  89. await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
  90. }
  91. foreach (Students students in stuListChange.stuleave)
  92. {
  93. try
  94. {
  95. await client.GetContainer("TEAMModelOS", "Teacher").DeleteItemAsync<StuActivity>(activity.id, new PartitionKey($"Activity-{activity.school}-{students.id}"));
  96. }
  97. catch (CosmosException ex)
  98. {
  99. //仅处理未写入的数据。
  100. }
  101. }
  102. foreach (string tmdid in stuListChange.tmdhleave)
  103. {
  104. try
  105. {
  106. await client.GetContainer("TEAMModelOS", "Teacher").DeleteItemAsync<StuActivity>(activity.id, new PartitionKey($"Activity-{tmdid}"));
  107. }
  108. catch(CosmosException ex )
  109. {
  110. //仅处理未写入的数据。
  111. }
  112. }
  113. }
  114. }
  115. catch(Exception ex ) {
  116. }
  117. }
  118. public static async Task FixStuCourse(CosmosClient client,DingDing _dingDing, StuListChange stuListChange)
  119. {
  120. //1.查找学校或教师的课程是否包含该名单的课程。
  121. var query = $"select distinct c.code,c.id,c.no,c.name,c.scope, c.creatorId,c.school from c join A0 in c.schedule where A0.stulist = '{stuListChange.listid}'";
  122. List<Course> courses = new List<Course>();
  123. if (stuListChange.scope.Equals("school") && !string.IsNullOrEmpty(stuListChange.school))
  124. {
  125. await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<Course>(queryText: query,
  126. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Course-{stuListChange.school}") }))
  127. {
  128. courses.Add(item);
  129. }
  130. }
  131. if (stuListChange.scope.Equals("private") && !string.IsNullOrEmpty(stuListChange.creatorId))
  132. {
  133. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<Course>(queryText: query,
  134. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Course-{stuListChange.creatorId}") }))
  135. {
  136. courses.Add(item);
  137. }
  138. }
  139. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-StuListService-FixStuCourse\n名单发生变更 需要处理的课程\n{courses.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
  140. //2.获取课程的id 并尝试添加或移除对应的学生课程记录StuCourse。
  141. foreach (var course in courses)
  142. {
  143. //学生新加入名单的
  144. foreach (Students students in stuListChange.stujoin)
  145. {
  146. var stucourse = new StuCourse
  147. {
  148. id = course.id,
  149. scode = course.code,
  150. name = course.name,
  151. code = $"StuCourse-{course.school}-{students.id}",
  152. scope = course.scope,
  153. school = course.school,
  154. creatorId = course.creatorId,
  155. pk = "StuCourse"
  156. };
  157. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-StuListService-FixStuCourse\n名单发生变更 新建课程中间表\n{stucourse.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
  158. await client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
  159. }
  160. //tmd新加入的
  161. foreach (string tmdid in stuListChange.tmdjoin)
  162. {
  163. var stucourse = new StuCourse
  164. {
  165. id = course.id,
  166. scode = course.code,
  167. name = course.name,
  168. code = $"StuCourse-{tmdid}",
  169. scope = course.scope,
  170. school = course.school,
  171. creatorId = course.creatorId,
  172. pk = "StuCourse"
  173. };
  174. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-StuListService-FixStuCourse\n名单发生变更 新建课程中间表\n{stucourse.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
  175. await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
  176. }
  177. //移除名单的。 在点击相关的课程,再去二次校验是否存在,不存在则再去删除。
  178. foreach (var delStu in stuListChange.stuleave)
  179. {
  180. await client.GetContainer("TEAMModelOS", "Student").DeleteItemStreamAsync(course.id, new PartitionKey($"Course-{course.school}-{delStu.id}"));
  181. }
  182. foreach (var delTmd in stuListChange.tmdhleave)
  183. {
  184. await client.GetContainer("TEAMModelOS", "Teacher").DeleteItemStreamAsync(course.id, new PartitionKey($"Course-{delTmd}"));
  185. }
  186. }
  187. }
  188. }
  189. }