ActivityHttpTrigger.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using System.IO;
  3. using System.Threading.Tasks;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.Azure.WebJobs;
  6. using Microsoft.Azure.WebJobs.Extensions.Http;
  7. using Microsoft.AspNetCore.Http;
  8. using Microsoft.Extensions.Logging;
  9. using Newtonsoft.Json;
  10. using TEAMModelOS.SDK.DI;
  11. using Azure.Cosmos;
  12. using System.Text.Json;
  13. using System.Collections.Generic;
  14. using TEAMModelOS.SDK.Models;
  15. using TEAMModelOS.SDK.Extension;
  16. using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
  17. using TEAMModelOS.SDK.Models.Cosmos;
  18. using TEAMModelOS.SDK.Models.Cosmos.Common;
  19. namespace TEAMModelFunction
  20. {
  21. public class ActivityHttpTrigger
  22. {
  23. private readonly AzureCosmosFactory _azureCosmos;
  24. private readonly DingDing _dingDing;
  25. private readonly AzureStorageFactory _azureStorage;
  26. private readonly AzureRedisFactory _azureRedis;
  27. public ActivityHttpTrigger(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage
  28. , AzureRedisFactory azureRedis)
  29. {
  30. _azureCosmos = azureCosmos;
  31. _dingDing = dingDing;
  32. _azureStorage = azureStorage;
  33. _azureRedis = azureRedis;
  34. }
  35. [FunctionName("fix-exam-activity")]
  36. public async Task<IActionResult> ExamActivity([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,ILogger log)
  37. {
  38. log.LogInformation("C# HTTP trigger function processed a request.");
  39. string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
  40. List<string> datas = JsonConvert.DeserializeObject<List<string>>(requestBody);
  41. var client = _azureCosmos.GetCosmosClient();
  42. var query = $"select * from c ";
  43. foreach (string data in datas) {
  44. List<ExamInfo> exams = new List<ExamInfo>();
  45. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(
  46. queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Exam-{data}") }))
  47. {
  48. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  49. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  50. {
  51. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  52. {
  53. exams.Add(obj.ToObject<ExamInfo>());
  54. }
  55. }
  56. }
  57. log.LogInformation($"{exams.ToJsonString()}");
  58. foreach (var info in exams) {
  59. List<string> sub = new List<string>();
  60. foreach (ExamSubject subject in info.subjects)
  61. {
  62. sub.Add(subject.id);
  63. }
  64. //ActivityData dataa;
  65. //if (info.scope == "school")
  66. //{
  67. // dataa = new ActivityData
  68. // {
  69. // id = info.id,
  70. // code = $"Activity-{info.school}",
  71. // type = "exam",
  72. // name = info.name,
  73. // startTime = info.startTime,
  74. // endTime = info.endTime,
  75. // scode = info.code,
  76. // scope = info.scope,
  77. // classes = info.classes.IsNotEmpty() ? info.classes : new List<string> { "" },
  78. // tmdids = new List<string> { "" },
  79. // progress = "going",
  80. // subjects = sub
  81. // };
  82. // await client.GetContainer("TEAMModelOS", "School").UpsertItemAsync<ActivityData>(dataa, new Azure.Cosmos.PartitionKey(dataa.code));
  83. //}
  84. //else if (info.scope == "private")
  85. //{
  86. // dataa = new ActivityData
  87. // {
  88. // id = info.id,
  89. // code = $"Activity-Common",
  90. // type = "exam",
  91. // name = info.name,
  92. // startTime = info.startTime,
  93. // endTime = info.endTime,
  94. // scode = info.code,
  95. // scope = info.scope,
  96. // progress = "going",
  97. // classes = info.classes.IsNotEmpty() ? info.classes : new List<string> { "" },
  98. // tmdids = new List<string> { "" },
  99. // subjects = sub
  100. // };
  101. // await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync<ActivityData>(dataa, new Azure.Cosmos.PartitionKey(dataa.code));
  102. //}
  103. (List<string> tmdids, List<Students> studentss) = await TriggerStuActivity.GetStuList(client, info.classes, info.school);
  104. List<StuActivity> stuActivities = new List<StuActivity>();
  105. List<StuActivity> tmdActivities = new List<StuActivity>();
  106. if (tmdids.IsNotEmpty())
  107. {
  108. tmdids.ForEach(x => {
  109. tmdActivities.Add(new StuActivity
  110. {
  111. pk = "Activity",
  112. id = info.id,
  113. code = $"Activity-{x}",
  114. type = "exam",
  115. name = info.name,
  116. startTime = info.startTime,
  117. endTime = info.endTime,
  118. scode = info.code,
  119. scope = info.scope,
  120. school = info.school,
  121. creatorId = info.creatorId,
  122. subjects = sub
  123. });
  124. });
  125. }
  126. if (studentss.IsNotEmpty())
  127. {
  128. studentss.ForEach(x => {
  129. stuActivities.Add(new StuActivity
  130. {
  131. pk = "Activity",
  132. id = info.id,
  133. code = $"Activity-{info.school}-{x.id}",
  134. type = "exam",
  135. name = info.name,
  136. startTime = info.startTime,
  137. endTime = info.endTime,
  138. scode = info.code,
  139. scope = info.scope,
  140. school = info.school,
  141. creatorId = info.creatorId,
  142. subjects = sub
  143. });
  144. });
  145. }
  146. await TriggerStuActivity.SaveStuActivity(client, stuActivities, tmdActivities);
  147. }
  148. }
  149. return new OkObjectResult(new { });
  150. }
  151. [FunctionName("fix-vote-activity")]
  152. public async Task<IActionResult> VoteActivity(
  153. [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
  154. ILogger log)
  155. {
  156. log.LogInformation("C# HTTP trigger function processed a request.1");
  157. string name = req.Query["name"];
  158. string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
  159. dynamic data = JsonConvert.DeserializeObject(requestBody);
  160. log.LogInformation($"{data}");
  161. name = name ?? data?.name;
  162. string responseMessage = string.IsNullOrEmpty(name)
  163. ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
  164. : $"Hello, {name}. This HTTP triggered function executed successfully.";
  165. return new OkObjectResult(responseMessage);
  166. }
  167. [FunctionName("fix-survey-activity")]
  168. public async Task<IActionResult> SurveyActivity(
  169. [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
  170. ILogger log)
  171. {
  172. log.LogInformation("C# HTTP trigger function processed a request.1");
  173. string name = req.Query["name"];
  174. string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
  175. dynamic data = JsonConvert.DeserializeObject(requestBody);
  176. log.LogInformation($"{data}");
  177. name = name ?? data?.name;
  178. string responseMessage = string.IsNullOrEmpty(name)
  179. ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
  180. : $"Hello, {name}. This HTTP triggered function executed successfully.";
  181. return new OkObjectResult(responseMessage);
  182. }
  183. }
  184. }