TriggerStuActivity.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using Azure;
  2. using Azure.Cosmos;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Text.Json;
  9. using System.Threading.Tasks;
  10. using TEAMModelOS.SDK.DI;
  11. using TEAMModelOS.SDK.Extension;
  12. using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
  13. using TEAMModelOS.SDK.Models;
  14. using TEAMModelOS.SDK.Models.Cosmos.Common;
  15. namespace TEAMModelFunction
  16. {
  17. public class TriggerStuActivity
  18. {
  19. public static async Task RefreshStuActivity(CosmosClient client, DingDing _dingDing, string id, string code)
  20. {
  21. MQActivity activity = null;
  22. try
  23. {
  24. var aactivity = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(id, new Azure.Cosmos.PartitionKey(code));
  25. using var da = await JsonDocument.ParseAsync(aactivity.ContentStream);
  26. activity = da.ToObject<MQActivity>();
  27. }
  28. catch (CosmosException ex)
  29. {
  30. }
  31. if (activity != null)
  32. {
  33. (List<TmdInfo> tmdids, List<StuInfo> students) = await GetStuList(client, _dingDing, activity.classes, activity.school);
  34. if (tmdids.IsNotEmpty())
  35. {
  36. foreach (TmdInfo tmdid in tmdids)
  37. {
  38. var stucourse = new StuActivity
  39. {
  40. id = activity.id,
  41. scode = activity.code,
  42. name = activity.name,
  43. code = $"Activity-{tmdid.tmdid}",
  44. scope = activity.scope,
  45. school = activity.school,
  46. creatorId = activity.creatorId,
  47. pk = "Activity",
  48. type = activity.pk,
  49. subjects = activity.pk.ToLower().Equals("exam") && activity.subjects.IsNotEmpty() ? new List<string>() { activity.subjects[0].id } : new List<string>() { "" },
  50. startTime = activity.startTime,
  51. endTime = activity.endTime,
  52. blob = activity.blob,
  53. owner = activity.owner
  54. };
  55. await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
  56. }
  57. }
  58. if (students.IsNotEmpty())
  59. {
  60. foreach (StuInfo student in students)
  61. {
  62. var stucourse = new StuActivity
  63. {
  64. id = activity.id,
  65. scode = activity.code,
  66. name = activity.name,
  67. code = $"Activity-{activity.school}-{student.id}",
  68. scope = activity.scope,
  69. school = activity.school,
  70. creatorId = activity.creatorId,
  71. pk = "Activity",
  72. type = activity.pk,
  73. subjects = activity.pk.ToLower().Equals("exam") && activity.subjects.IsNotEmpty() ? new List<string>() { activity.subjects[0].id } : new List<string>() { "" },
  74. startTime = activity.startTime,
  75. endTime = activity.endTime,
  76. blob = activity.blob,
  77. owner = activity.owner
  78. };
  79. await client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
  80. }
  81. }
  82. }
  83. }
  84. public static async Task<string> SaveStuActivity(CosmosClient client, DingDing _dingDing, List<StuActivity> stuActivities,List<StuActivity> tmdActivities) {
  85. try {
  86. if (stuActivities.IsNotEmpty())
  87. {
  88. foreach (var x in stuActivities)
  89. {
  90. await client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync(x, new PartitionKey(x.code));
  91. }
  92. }
  93. if (tmdActivities.IsNotEmpty())
  94. {
  95. foreach (var x in tmdActivities)
  96. {
  97. await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(x, new PartitionKey(x.code));
  98. }
  99. }
  100. } catch (Exception ex) {
  101. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-OS,TriggerStuActivity-SaveStuActivity\n{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  102. }
  103. return "";
  104. }
  105. public static async Task<List<ClassInfo>> GetClassInfo(CosmosClient client, DingDing _dingDing, List<string> classes, string school)
  106. {
  107. List<ClassInfo> classInfos = new List<ClassInfo>();
  108. List<string> sqlList = new List<string>();
  109. classes.ForEach(x => { sqlList.Add($" '{x}' "); });
  110. string sql = string.Join(" , ", sqlList);
  111. if (!string.IsNullOrEmpty(school)) {
  112. await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<ClassInfo>(queryText: $"select c.id,c.name from c where c.id in ({sql})",
  113. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Class-{school}") }))
  114. {
  115. classInfos.Add(item);
  116. }
  117. await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<ClassInfo>(queryText: $"select c.id,c.name from c where c.id in ({sql})",
  118. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList-{school}") }))
  119. {
  120. classInfos.Add(item);
  121. }
  122. }
  123. List<StuList> tchLists = new List<StuList>();
  124. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<ClassInfo>(queryText: $"select c.id,c.name from c where c.id in ({sql})",
  125. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList") }))
  126. {
  127. classInfos.Add(item);
  128. }
  129. return classInfos;
  130. }
  131. public static async Task<(List<TmdInfo> tmdids, List<StuInfo> students)> GetStuList(CosmosClient client, DingDing _dingDing, List<string> classes, string school) {
  132. try {
  133. List<TmdInfo> tmdids = new List<TmdInfo>();
  134. List<Students> studentss = new List<Students>();
  135. List<StuInfo> stuInfos = new List<StuInfo>();
  136. if (!classes.IsNotEmpty()) { return (tmdids, new List<StuInfo>()); }
  137. List<string> sqlList = new List<string>();
  138. classes.ForEach(x => { sqlList.Add($" '{x}' "); });
  139. string sql = string.Join(" , ", sqlList);
  140. List<StuList> schList = new List<StuList>();
  141. List<Student> students = new List<Student>();
  142. if (!string.IsNullOrEmpty(school)) {
  143. await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<StuList>(queryText: $"select value(c) from c where c.id in ({sql})",
  144. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList-{school}") }))
  145. {
  146. schList.Add(item);
  147. }
  148. await foreach (var item in client.GetContainer("TEAMModelOS", "Student").GetItemQueryIterator<Student>(queryText: $"select value(c) from c where c.classId in ({sql})",
  149. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Base-{school}") }))
  150. {
  151. students.Add(item);
  152. }
  153. }
  154. List<StuList> tchLists = new List<StuList>();
  155. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<StuList>(queryText: $"select value(c) from c where c.id in ({sql})",
  156. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList") }))
  157. {
  158. tchLists.Add(item);
  159. }
  160. foreach(var x in schList) {
  161. if (x.students.IsNotEmpty())
  162. {
  163. studentss.AddRange(x.students);
  164. }
  165. if (x.tmids.IsNotEmpty())
  166. {
  167. List<TmdInfo> infos = new List<TmdInfo>();
  168. List<string> inids = new List<string>();
  169. x.tmids.ForEach(x => { inids.Add($"'{x}'"); });
  170. var insql = string.Join(",", inids);
  171. var queryslt = $"SELECT value(c) FROM c where c.id in ({insql})";
  172. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<TmdInfo>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") })) {
  173. infos.Add(item);
  174. }
  175. tmdids.AddRange(infos);
  176. }
  177. }
  178. foreach (var x in tchLists)
  179. {
  180. if (x.students.IsNotEmpty()) {
  181. studentss.AddRange(x.students);
  182. }
  183. if (x.tmids.IsNotEmpty())
  184. {
  185. List<TmdInfo> infos = new List<TmdInfo>();
  186. List<string> inids = new List<string>();
  187. x.tmids.ForEach(x => { inids.Add($"'{x}'"); });
  188. var insql = string.Join(",", inids);
  189. var queryslt = $"SELECT value(c) FROM c where c.id in ({insql})";
  190. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<TmdInfo>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  191. {
  192. infos.Add(item);
  193. }
  194. tmdids.AddRange(infos);
  195. }
  196. }
  197. students.ForEach(x => {
  198. studentss.Add(new Students { id = x.id, code = x.code, schoolId = x.schoolId });
  199. });
  200. List<string> inidstus = new List<string>();
  201. studentss.Select(x => x.id).ToList().ForEach(x => { inidstus.Add($"'{x}'"); });
  202. var insqlstu = string.Join(",", inidstus);
  203. var querystu = $"SELECT c.id,c.code,c.name,c.picture,c.classId,c.year,c.schoolId FROM c where c.id in ({insqlstu})";
  204. await foreach (var item in client.GetContainer("TEAMModelOS", "Student").GetItemQueryIterator<StuInfo>(queryText: querystu, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-{school}") }))
  205. {
  206. stuInfos.Add(item);
  207. }
  208. return (tmdids, stuInfos);
  209. }
  210. catch (Exception ex)
  211. {
  212. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-OS,TriggerStuActivity-SaveStuActivity\n{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  213. }
  214. return (null, null);
  215. }
  216. }
  217. }