StuListService.cs 14 KB

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