1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- using System;
- using System.IO;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Azure.WebJobs;
- using Microsoft.Azure.WebJobs.Extensions.Http;
- using Microsoft.AspNetCore.Http;
- using Microsoft.Extensions.Logging;
- using Newtonsoft.Json;
- using TEAMModelOS.SDK.DI;
- using System.Text.Json;
- using TEAMModelOS.SDK.Models;
- using Azure.Cosmos;
- using TEAMModelOS.SDK.Extension;
- using TEAMModelOS.SDK.Models.Cosmos.Common;
- using System.Collections.Generic;
- using TEAMModelOS.SDK.Helper.Common.DateTimeHelper;
- namespace TEAMModelFunction
- {
- public class LessonHttpTrigger
- {
- private readonly AzureCosmosFactory _azureCosmos;
- private readonly DingDing _dingDing;
- public LessonHttpTrigger(AzureCosmosFactory azureCosmos, DingDing dingDing)
- {
- _azureCosmos = azureCosmos;
- _dingDing = dingDing;
- }
- [FunctionName("insert-class-count")]
- public async Task<IActionResult> Run(
- [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
- ILogger log)
- {
- log.LogInformation("C# HTTP trigger function processed a request.");
- var client = _azureCosmos.GetCosmosClient();
- string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
- var data = System.Text.Json.JsonSerializer.Deserialize<LessonRecord>(requestBody);
- /*int day = DateTimeOffset.FromUnixTimeMilliseconds(data.startTime).DayOfYear;
- int year = DateTimeOffset.FromUnixTimeMilliseconds(data.startTime).Year;
- int days = DateTimeHelper.getDays(year);
- //int years = DateTimeOffset.UtcNow.DayOfYear;
- string tbname = string.Empty;
- string code = string.Empty;
- if (data.scope.Equals("school"))
- {
- code = $"LessonCount-{data.school}";
- tbname = "School";
- }
- else
- {
- code = $"LessonCount";
- tbname = "Teacher";
- }
- var response = await client.GetContainer(Constant.TEAMModelOS, tbname).ReadItemStreamAsync(data.id.ToString(), new PartitionKey(code));
- if (response.Status == 200)
- {
- using var json = await JsonDocument.ParseAsync(response.ContentStream);
- LessonCount count = json.ToObject<LessonCount>();
- if (count.courseIds.Count > 0)
- {
- if (!count.courseIds.Contains(data.courseId))
- {
- count.courseIds.Add(data.courseId);
- count.beginCount[day] += 1;
- }
- }
- await client.GetContainer("TEAMModelOS", tbname).ReplaceItemAsync(count, count.id, new PartitionKey(code));
- }
- else
- {
- LessonCount count = new LessonCount
- {
- id = data.tmdid,
- code = "LessonCount-" + data.school,
- year = year
- };
- double[] da = new double[days];
- List<double> list = new List<double>(da);
- list[day] += 1;
- count.beginCount.AddRange(list);
- //count.courseIds.Add(data.courseId);
- await client.GetContainer("TEAMModelOS", "tbname").CreateItemAsync(count, new PartitionKey(code));
- }*/
- return new OkObjectResult(data);
- }
- }
- }
|