LessonHttpTrigger.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 System.Text.Json;
  12. using TEAMModelOS.SDK.Models;
  13. using Azure.Cosmos;
  14. using TEAMModelOS.SDK.Extension;
  15. using TEAMModelOS.SDK.Models.Cosmos.Common;
  16. using System.Collections.Generic;
  17. using TEAMModelOS.SDK.Helper.Common.DateTimeHelper;
  18. namespace TEAMModelFunction
  19. {
  20. public class LessonHttpTrigger
  21. {
  22. private readonly AzureCosmosFactory _azureCosmos;
  23. private readonly DingDing _dingDing;
  24. public LessonHttpTrigger(AzureCosmosFactory azureCosmos, DingDing dingDing)
  25. {
  26. _azureCosmos = azureCosmos;
  27. _dingDing = dingDing;
  28. }
  29. [FunctionName("insert-class-count")]
  30. public async Task<IActionResult> Run(
  31. [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
  32. ILogger log)
  33. {
  34. log.LogInformation("C# HTTP trigger function processed a request.");
  35. var client = _azureCosmos.GetCosmosClient();
  36. string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
  37. var data = System.Text.Json.JsonSerializer.Deserialize<LessonRecord>(requestBody);
  38. /*int day = DateTimeOffset.FromUnixTimeMilliseconds(data.startTime).DayOfYear;
  39. int year = DateTimeOffset.FromUnixTimeMilliseconds(data.startTime).Year;
  40. int days = DateTimeHelper.getDays(year);
  41. //int years = DateTimeOffset.UtcNow.DayOfYear;
  42. string tbname = string.Empty;
  43. string code = string.Empty;
  44. if (data.scope.Equals("school"))
  45. {
  46. code = $"LessonCount-{data.school}";
  47. tbname = "School";
  48. }
  49. else
  50. {
  51. code = $"LessonCount";
  52. tbname = "Teacher";
  53. }
  54. var response = await client.GetContainer(Constant.TEAMModelOS, tbname).ReadItemStreamAsync(data.id.ToString(), new PartitionKey(code));
  55. if (response.Status == 200)
  56. {
  57. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  58. LessonCount count = json.ToObject<LessonCount>();
  59. if (count.courseIds.Count > 0)
  60. {
  61. if (!count.courseIds.Contains(data.courseId))
  62. {
  63. count.courseIds.Add(data.courseId);
  64. count.beginCount[day] += 1;
  65. }
  66. }
  67. await client.GetContainer("TEAMModelOS", tbname).ReplaceItemAsync(count, count.id, new PartitionKey(code));
  68. }
  69. else
  70. {
  71. LessonCount count = new LessonCount
  72. {
  73. id = data.tmdid,
  74. code = "LessonCount-" + data.school,
  75. year = year
  76. };
  77. double[] da = new double[days];
  78. List<double> list = new List<double>(da);
  79. list[day] += 1;
  80. count.beginCount.AddRange(list);
  81. //count.courseIds.Add(data.courseId);
  82. await client.GetContainer("TEAMModelOS", "tbname").CreateItemAsync(count, new PartitionKey(code));
  83. }*/
  84. return new OkObjectResult(data);
  85. }
  86. }
  87. }