StuListService.cs 7.1 KB

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