|
@@ -0,0 +1,198 @@
|
|
|
+using Azure.Cosmos;
|
|
|
+using Microsoft.AspNetCore.Http;
|
|
|
+using Microsoft.AspNetCore.Mvc;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.IdentityModel.Tokens.Jwt;
|
|
|
+using System.Linq;
|
|
|
+using System.Text;
|
|
|
+using System.Text.Json;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using TEAMModelOS.Models.Dto;
|
|
|
+using TEAMModelOS.SDK.Models;
|
|
|
+using TEAMModelOS.SDK;
|
|
|
+using TEAMModelOS.SDK.Context.Constant.Common;
|
|
|
+using TEAMModelOS.SDK.DI;
|
|
|
+using TEAMModelOS.SDK.DI.AzureCosmos.Inner;
|
|
|
+using TEAMModelOS.SDK.Extension;
|
|
|
+using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
|
|
|
+using TEAMModelOS.SDK.Helper.Common.StringHelper;
|
|
|
+using TEAMModelOS.Models;
|
|
|
+using Microsoft.Extensions.Options;
|
|
|
+using TEAMModelOS.SDK.Models.Cosmos;
|
|
|
+using Microsoft.AspNetCore.Authorization;
|
|
|
+using TEAMModelOS.Filter;
|
|
|
+using StackExchange.Redis;
|
|
|
+using TEAMModelOS.SDK.Models.Cosmos.Common.Inner;
|
|
|
+using TEAMModelOS.Services.Common;
|
|
|
+using TEAMModelOS.SDK.Models.Cosmos.Common;
|
|
|
+using Azure;
|
|
|
+
|
|
|
+namespace TEAMModelOS.Controllers.Common
|
|
|
+{
|
|
|
+ [ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
+ [ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
|
+ //[Authorize(Roles = "IES5")]
|
|
|
+ [Route("common/sheet-config")]
|
|
|
+ [ApiController]
|
|
|
+ public class SheetConfigController : ControllerBase
|
|
|
+ {
|
|
|
+ private readonly AzureRedisFactory _azureRedis;
|
|
|
+ private readonly AzureCosmosFactory _azureCosmos;
|
|
|
+ private readonly SnowflakeId _snowflakeId;
|
|
|
+ private readonly AzureServiceBusFactory _serviceBus;
|
|
|
+ private readonly DingDing _dingDing;
|
|
|
+ private readonly Option _option;
|
|
|
+ private readonly AzureStorageFactory _azureStorage;
|
|
|
+ public SheetConfigController(AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing, IOptionsSnapshot<Option> option,
|
|
|
+ AzureRedisFactory azureRedis, AzureStorageFactory azureStorage)
|
|
|
+ {
|
|
|
+ _azureCosmos = azureCosmos;
|
|
|
+ _serviceBus = serviceBus;
|
|
|
+ _snowflakeId = snowflakeId;
|
|
|
+ _dingDing = dingDing;
|
|
|
+ _option = option?.Value;
|
|
|
+ _azureRedis = azureRedis;
|
|
|
+ _azureStorage = azureStorage;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ ///查询答题卡
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="request"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ [ProducesDefaultResponseType]
|
|
|
+ [HttpPost("find")]
|
|
|
+ public async Task<IActionResult> Find(JsonElement request) {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var client = _azureCosmos.GetCosmosClient();
|
|
|
+ if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
|
|
|
+ if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
|
|
|
+ if (!request.TryGetProperty("scope", out JsonElement scope)) return BadRequest();
|
|
|
+ if ($"{scope}" == "school")
|
|
|
+ {
|
|
|
+ SheetConfig config = await client.GetContainer("TEAMModelOS", "School").ReadItemAsync<SheetConfig>($"{id}", new PartitionKey($"SheetConfig-{code}"));
|
|
|
+ return Ok(new { config = config, status = 200 });
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ SheetConfig config = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemAsync<SheetConfig>($"{id}", new PartitionKey($"SheetConfig-{code}"));
|
|
|
+ return Ok(new { config = config,status=200 });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (CosmosException e)
|
|
|
+ {
|
|
|
+ return Ok(new { status = e.Response.Status });
|
|
|
+ }
|
|
|
+ catch (Exception ex) {
|
|
|
+
|
|
|
+ await _dingDing.SendBotMsg($"OS,{_option.Location},common/SheetConfig/upsert()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
+ return BadRequest(e.StackTrace);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 新增 或 修改答题卡
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="request"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ [ProducesDefaultResponseType]
|
|
|
+ [HttpPost("upsert")]
|
|
|
+ public async Task<IActionResult> Upsert(SheetConfig request)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+
|
|
|
+ var client = _azureCosmos.GetCosmosClient();
|
|
|
+ request.pk = $"SheetConfig";
|
|
|
+ request.code = $"SheetConfig-{request.code}";
|
|
|
+ request.ttl = -1;
|
|
|
+ if (string.IsNullOrEmpty(request.id))
|
|
|
+ {
|
|
|
+ int id= Utils.CreatSaltInt(9999999);
|
|
|
+ for (int i = 0; i < 10; i++)
|
|
|
+ {
|
|
|
+ Response response = null;
|
|
|
+ if (request.scope == "school")
|
|
|
+ {
|
|
|
+ response = await client.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync($"{id}", new PartitionKey($"{request.code}"));
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ response = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemStreamAsync($"{id}", new PartitionKey($"{request.code}"));
|
|
|
+ }
|
|
|
+ if (response.Status == 404)
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ if (i == 9)
|
|
|
+ {
|
|
|
+ await _dingDing.SendBotMsg($"OS,{_option.Location},common/SheetConfig/upsert()\n id生成异常,重复生成次数超过10次", GroupNames.醍摩豆服務運維群組);
|
|
|
+ return BadRequest();
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ id = Utils.CreatSaltInt(9999999);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ request.id = $"{id}";
|
|
|
+ if (request.scope == "school")
|
|
|
+ {
|
|
|
+ request = await client.GetContainer("TEAMModelOS", "School").CreateItemAsync(request, new PartitionKey($"{request.code}"));
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ request = await client.GetContainer("TEAMModelOS", "Teacher").CreateItemAsync(request, new PartitionKey($"{request.code}"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if (request.scope == "school")
|
|
|
+ {
|
|
|
+ request = await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync(request, request.id, new PartitionKey($"{request.code}"));
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ request = await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync(request, request.id, new PartitionKey($"{request.code}"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Ok(new { config = request });
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ await _dingDing.SendBotMsg($"OS,{_option.Location},common/SheetConfig/upsert()\n{e.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
+ return BadRequest(e.StackTrace);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ ///删除答题卡
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="request"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ [ProducesDefaultResponseType]
|
|
|
+ [HttpPost("delete")]
|
|
|
+ public async Task<IActionResult> Delete(JsonElement request)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var client = _azureCosmos.GetCosmosClient();
|
|
|
+ if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
|
|
|
+ if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
|
|
|
+ if (!request.TryGetProperty("scope", out JsonElement scope)) return BadRequest();
|
|
|
+ if ($"{scope}" == "school")
|
|
|
+ {
|
|
|
+ await client.GetContainer("TEAMModelOS", "School").DeleteItemStreamAsync( $"{id}", new PartitionKey($"{code}"));
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ await client.GetContainer("TEAMModelOS", "Teacher").DeleteItemStreamAsync($"{id}", new PartitionKey($"{code}"));
|
|
|
+ }
|
|
|
+ return Ok(new { status=200 });
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ await _dingDing.SendBotMsg($"OS,{_option.Location},common/SheetConfig/delete()\n{e.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
+ return BadRequest(e.StackTrace);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|