StuListService.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.Helper.Common.CollectionHelper;
  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, StuListChange stuListChange, string type)
  17. {
  18. 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}') ";
  19. List<MQActivity> datas = new List<MQActivity>();
  20. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<MQActivity>(queryText: query,
  21. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"{type}-{stuListChange.originCode}") }))
  22. {
  23. datas.Add(item);
  24. }
  25. foreach (MQActivity activity in datas)
  26. {
  27. //已经完结的不再允许加入
  28. if (activity.progress.Equals("finish") || activity.progress.Equals("pending"))
  29. {
  30. continue;
  31. }
  32. //学生新加入名单的
  33. foreach (Students students in stuListChange.stujoin)
  34. {
  35. var stucourse = new StuActivity
  36. {
  37. id = activity.id,
  38. scode = activity.code,
  39. name = activity.name,
  40. code = $"Activity-{activity.school}-{students.id}",
  41. scope = activity.scope,
  42. school = activity.school,
  43. creatorId = activity.creatorId,
  44. pk = "Activity",
  45. type = type,
  46. subjects = activity.pk.ToLower().Equals("exam") && activity.subjects.IsNotEmpty() ? new List<string>() { activity.subjects[0].id } : new List<string>() { "" },
  47. startTime = activity.startTime,
  48. endTime = activity.endTime,
  49. blob=activity.blob,
  50. owner= activity.owner
  51. };
  52. await client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
  53. }//tmd新加入的
  54. foreach (string tmdid in stuListChange.tmdjoin)
  55. {
  56. var stucourse = new StuActivity
  57. {
  58. id = activity.id,
  59. scode = activity.code,
  60. name = activity.name,
  61. code = $"Activity-{tmdid}",
  62. scope = activity.scope,
  63. school = activity.school,
  64. creatorId = activity.creatorId,
  65. pk = "Activity",
  66. type = type,
  67. subjects = activity.pk.ToLower().Equals("exam") && activity.subjects.IsNotEmpty() ? new List<string>() { activity.subjects[0].id } : new List<string>() { "" },
  68. startTime = activity.startTime,
  69. endTime = activity.endTime,
  70. blob = activity.blob,
  71. owner = activity.owner
  72. };
  73. await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
  74. }
  75. }
  76. }
  77. public static async Task FixStuCourse(CosmosClient client, StuListChange stuListChange)
  78. {
  79. //1.查找学校或教师的课程是否包含该名单的课程。
  80. 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}'";
  81. List<Course> courses = new List<Course>();
  82. if (stuListChange.scope.Equals("school"))
  83. {
  84. await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<Course>(queryText: query,
  85. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Course-{stuListChange.originCode}") }))
  86. {
  87. courses.Add(item);
  88. }
  89. }
  90. else
  91. {
  92. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<Course>(queryText: query,
  93. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Course-{stuListChange.originCode}") }))
  94. {
  95. courses.Add(item);
  96. }
  97. }
  98. //2.获取课程的id 并尝试添加或移除对应的学生课程记录StuCourse。
  99. foreach (var course in courses)
  100. {
  101. //学生新加入名单的
  102. foreach (Students students in stuListChange.stujoin)
  103. {
  104. var stucourse = new StuCourse
  105. {
  106. id = course.id,
  107. scode = course.code,
  108. name = course.name,
  109. code = $"StuCourse-{course.school}-{students.id}",
  110. scope = course.scope,
  111. school = course.school,
  112. creatorId = course.creatorId,
  113. pk = "StuCourse"
  114. };
  115. await client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
  116. }
  117. //tmd新加入的
  118. foreach (string tmdid in stuListChange.tmdjoin)
  119. {
  120. var stucourse = new StuCourse
  121. {
  122. id = course.id,
  123. scode = course.code,
  124. name = course.name,
  125. code = $"StuCourse-{tmdid}",
  126. scope = course.scope,
  127. school = course.school,
  128. creatorId = course.creatorId,
  129. pk = "StuCourse"
  130. };
  131. await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
  132. }
  133. //移除名单的。 在点击相关的课程,再去二次校验是否存在,不存在则再去删除。
  134. //foreach (var delStu in stuListChange.stuleave)
  135. //{
  136. // await client.GetContainer("TEAMModelOS", "Student").DeleteItemStreamAsync(course.id, new PartitionKey($"Course-{course.school}-{delStu.id}"));
  137. //}
  138. //foreach (var delTmd in stuListChange.tmdhleave)
  139. //{
  140. // await client.GetContainer("TEAMModelOS", "Teacher").DeleteItemStreamAsync(course.id, new PartitionKey($"Course-{delTmd}"));
  141. //}
  142. }
  143. }
  144. }
  145. }