|
@@ -0,0 +1,74 @@
|
|
|
|
+using Microsoft.AspNetCore.Http;
|
|
|
|
+using Microsoft.AspNetCore.Mvc;
|
|
|
|
+using Microsoft.Extensions.Options;
|
|
|
|
+using System;
|
|
|
|
+using System.Collections.Generic;
|
|
|
|
+using System.IO;
|
|
|
|
+using System.Linq;
|
|
|
|
+using System.Net.Http;
|
|
|
|
+using System.Net.Http.Json;
|
|
|
|
+using System.Text;
|
|
|
|
+using System.Text.Json;
|
|
|
|
+using System.Threading.Tasks;
|
|
|
|
+using TEAMModelOS.Models;
|
|
|
|
+using TEAMModelOS.SDK.DI;
|
|
|
|
+
|
|
|
|
+namespace TEAMModelOS.Controllers.Common
|
|
|
|
+{
|
|
|
|
+ [ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
+ [ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
|
|
+ //[Authorize(Roles = "IES5")
|
|
|
|
+ [Route("common")]
|
|
|
|
+ [ApiController]
|
|
|
|
+ public class CommonController:ControllerBase
|
|
|
|
+ {
|
|
|
|
+ private readonly AzureCosmosFactory _azureCosmos;
|
|
|
|
+ private readonly SnowflakeId _snowflakeId;
|
|
|
|
+ private readonly AzureServiceBusFactory _serviceBus;
|
|
|
|
+ private readonly DingDing _dingDing;
|
|
|
|
+ private readonly Option _option;
|
|
|
|
+ private readonly AzureStorageFactory _azureStorage;
|
|
|
|
+ private readonly IHttpClientFactory _clientFactory;
|
|
|
|
+ public CommonController(AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing, IOptionsSnapshot<Option> option, AzureStorageFactory azureStorage, IHttpClientFactory clientFactory) {
|
|
|
|
+ _azureCosmos = azureCosmos;
|
|
|
|
+ _serviceBus = serviceBus;
|
|
|
|
+ _snowflakeId = snowflakeId;
|
|
|
|
+ _dingDing = dingDing;
|
|
|
|
+ _option = option?.Value;
|
|
|
|
+ _azureStorage = azureStorage;
|
|
|
|
+ _clientFactory = clientFactory;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //立即结束某个活动
|
|
|
|
+ [ProducesDefaultResponseType]
|
|
|
|
+ //[AuthToken(Roles = "Student")]
|
|
|
|
+ [HttpPost("finish")]
|
|
|
|
+ public async Task<IActionResult> finish(JsonElement element)
|
|
|
|
+ {
|
|
|
|
+ //ResponseBuilder builder = ResponseBuilder.custom();
|
|
|
|
+ //var (id, school) = HttpContext.GetAuthTokenInfo();
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+ if (!element.TryGetProperty("id", out JsonElement id)) return BadRequest();
|
|
|
|
+ if (!element.TryGetProperty("code", out JsonElement code)) return BadRequest();
|
|
|
|
+ var client = _azureCosmos.GetCosmosClient();
|
|
|
|
+ var data = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync($"{id}", new Azure.Cosmos.PartitionKey ($"{code}"));
|
|
|
|
+ using var json = await JsonDocument.ParseAsync(data.ContentStream);
|
|
|
|
+ long now= DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
|
|
+ Dictionary<string,object> dy = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string,object>>(json.RootElement.ToString());
|
|
|
|
+ dy["endTime"] = now;
|
|
|
|
+ dy["progress"] = "finish";
|
|
|
|
+ await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync<Dictionary<string, object>>(dy,dy["id"].ToString(),new Azure.Cosmos.PartitionKey(dy["code"].ToString()));
|
|
|
|
+ var httpClient= _clientFactory.CreateClient();
|
|
|
|
+ var content = new { id = $"{id}", code = $"{code}" };
|
|
|
|
+ var response= await httpClient.PostAsJsonAsync( $"{_option.HttpTrigger }refresh-stu-activity", content);
|
|
|
|
+ return Ok( await JsonDocument.ParseAsync(await response.Content.ReadAsStreamAsync()) );
|
|
|
|
+ }
|
|
|
|
+ catch (Exception ex)
|
|
|
|
+ {
|
|
|
|
+ await _dingDing.SendBotMsg($"OS,{_option.Location},common/finish\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
|
+ return BadRequest();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|