StuListService.cs 13 KB

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