12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- using System;
- using System.Collections.Generic;
- using System.Text.Json;
- using System.Threading.Tasks;
- using Azure.Cosmos;
- using Microsoft.Azure.WebJobs;
- using Microsoft.Azure.WebJobs.Host;
- using Microsoft.Extensions.Logging;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Extension;
- namespace TEAMModelFunction
- {
- public class ServiceBusTopic
- {
- private readonly AzureCosmosFactory _azureCosmos;
- public ServiceBusTopic( AzureCosmosFactory azureCosmos)
- {
- _azureCosmos = azureCosmos;
- }
- [FunctionName("ServiceBusTopic")]
- public async Task ExamBus([ServiceBusTrigger("active-task", "active-exam-recive-task", Connection = "ConnectionBusName")] string mySbMsg)
- {
- Dictionary<string, object> keyValuePairs = mySbMsg.ToObject<Dictionary<string, object>>();
- var client = _azureCosmos.GetCosmosClient();
- keyValuePairs.TryGetValue("id", out object id);
- keyValuePairs.TryGetValue("name", out object name);
- keyValuePairs.TryGetValue("code", out object code);
- //keyValuePairs.TryGetValue("status", out object progress);
- if (name.ToString().Equals("ExamInfo",StringComparison.OrdinalIgnoreCase)) {
- ExamInfo exam = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ExamInfo>(id.ToString(), new PartitionKey($"{code}"));
- if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(exam.startTime) > 0 && DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(exam.endTime) < 0) {
- exam.progress = "going";
- } else if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(exam.endTime) > 0) {
- exam.progress = "finish";
- }
- await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(exam, id.ToString(), new PartitionKey($"{code}"));
- }
- }
- public async Task VoteBus([ServiceBusTrigger("active-task", "active-vote-recive-task", Connection = "ConnectionBusName")] string mySbMsg)
- {
- Dictionary<string, object> keyValuePairs = mySbMsg.ToObject<Dictionary<string, object>>();
- var client = _azureCosmos.GetCosmosClient();
- keyValuePairs.TryGetValue("id", out object id);
- keyValuePairs.TryGetValue("name", out object name);
- keyValuePairs.TryGetValue("code", out object code);
- //keyValuePairs.TryGetValue("status", out object progress);
- if (name.ToString().Equals("Vote",StringComparison.OrdinalIgnoreCase)) {
- Vote vote;
- var sresponse = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(id.ToString(), new PartitionKey($"{code}"));
- if (sresponse.Status == 200)
- {
- using var json = await JsonDocument.ParseAsync(sresponse.ContentStream);
- vote = json.ToObject<Vote>();
- if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(vote.startTime) > 0 && DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(vote.endTime) < 0)
- {
- vote.progress = "going";
- }
- else if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(vote.endTime) > 0)
- {
- vote.progress = "finish";
- }
- await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(vote, id.ToString(), new PartitionKey($"{code}"));
- }
- }
- }
- public async Task SurveyBus([ServiceBusTrigger("active-task", "active-survey-recive-task", Connection = "ConnectionBusName")] string mySbMsg)
- {
- Dictionary<string, object> keyValuePairs = mySbMsg.ToObject<Dictionary<string, object>>();
- var client = _azureCosmos.GetCosmosClient();
- keyValuePairs.TryGetValue("id", out object id);
- keyValuePairs.TryGetValue("name", out object name);
- keyValuePairs.TryGetValue("code", out object code);
- //keyValuePairs.TryGetValue("status", out object progress);
- if (name.ToString().Equals("Survey",StringComparison.OrdinalIgnoreCase))
- {
- Survey survey = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Survey>(id.ToString(), new PartitionKey($"{code}"));
- if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(survey.startTime) > 0 && DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(survey.endTime) < 0)
- {
- survey.progress = "going";
- }
- else if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(survey.endTime) > 0)
- {
- survey.progress = "finish";
- }
- await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(survey, id.ToString(), new PartitionKey($"{code}"));
- }
-
- }
- }
- }
|