StuListService.cs 14 KB

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