|
@@ -0,0 +1,130 @@
|
|
|
+using Azure.Cosmos;
|
|
|
+using Microsoft.AspNetCore.Http;
|
|
|
+using Microsoft.AspNetCore.Mvc;
|
|
|
+using Microsoft.Extensions.Options;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.IdentityModel.Tokens.Jwt;
|
|
|
+using System.Linq;
|
|
|
+using System.Text.Json;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using TEAMModelOS.Models;
|
|
|
+using TEAMModelOS.SDK.Models;
|
|
|
+using TEAMModelOS.SDK;
|
|
|
+using TEAMModelOS.SDK.DI;
|
|
|
+using TEAMModelOS.SDK.Extension;
|
|
|
+using Azure;
|
|
|
+using TEAMModelOS.SDK.Models.Cosmos.Common;
|
|
|
+using Azure.Messaging.ServiceBus;
|
|
|
+using Microsoft.Extensions.Configuration;
|
|
|
+using TEAMModelOS.Filter;
|
|
|
+using HTEXLib.COMM.Helpers;
|
|
|
+namespace TEAMModelOS.Controllers
|
|
|
+{
|
|
|
+ [ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
+ [ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
|
+ //[Authorize(Roles = "IES5")]
|
|
|
+ [Route("group-list")]
|
|
|
+ [ApiController]
|
|
|
+ public class GroupListController : ControllerBase
|
|
|
+ {
|
|
|
+ private AzureCosmosFactory _azureCosmos;
|
|
|
+ private readonly DingDing _dingDing;
|
|
|
+ private readonly Option _option;
|
|
|
+ private readonly AzureServiceBusFactory _serviceBus;
|
|
|
+ private readonly AzureStorageFactory _azureStorage;
|
|
|
+ public IConfiguration _configuration { get; set; }
|
|
|
+ public GroupListController(AzureCosmosFactory azureCosmos, DingDing dingDing, IOptionsSnapshot<Option> option, AzureServiceBusFactory serviceBus, AzureStorageFactory azureStorage, IConfiguration configuration)
|
|
|
+ {
|
|
|
+ _azureCosmos = azureCosmos;
|
|
|
+ _dingDing = dingDing;
|
|
|
+ _option = option?.Value;
|
|
|
+ _serviceBus = serviceBus;
|
|
|
+ _configuration = configuration;
|
|
|
+ _azureStorage = azureStorage;
|
|
|
+ }
|
|
|
+ //处理通用名单
|
|
|
+ [ProducesDefaultResponseType]
|
|
|
+ [AuthToken(Roles = "teacher,admin")]
|
|
|
+ [HttpPost("upsert-stu-list")]
|
|
|
+ public async Task<IActionResult> upsertList(GroupList list)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var (userid, _, _, school) = HttpContext.GetAuthTokenInfo();
|
|
|
+ list.creatorId = userid;
|
|
|
+ list.school = school;
|
|
|
+ return Ok(new { list });
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ await _dingDing.SendBotMsg($"OS,{_option.Location},course/upsert-list()\n{ex.Message}{ex.StackTrace}\n{list}", GroupNames.醍摩豆服務運維群組);
|
|
|
+ return BadRequest();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private async Task<GroupList> UpsertList(GroupList list, string scope) {
|
|
|
+ try {
|
|
|
+ var client = _azureCosmos.GetCosmosClient();
|
|
|
+ if (string.IsNullOrEmpty(list.no)) {
|
|
|
+ list.no = $"{Utils.CreatSaltString(6, "0123456789")}";
|
|
|
+ for (int i = 0; i < 10; i++)
|
|
|
+ {
|
|
|
+ var queryNo = $"SELECT c.no FROM c where c.no ='{list.no}'";
|
|
|
+ List<string> noStus = new();
|
|
|
+ await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: queryNo,
|
|
|
+ requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"{list.code}") }))
|
|
|
+ {
|
|
|
+ using var jsonNo = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
+ if (jsonNo.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
|
|
|
+ {
|
|
|
+ var accounts = jsonNo.RootElement.GetProperty("Documents").EnumerateArray();
|
|
|
+ while (accounts.MoveNext())
|
|
|
+ {
|
|
|
+ JsonElement account = accounts.Current;
|
|
|
+ noStus.Add(account.GetProperty("no").GetString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIterator(queryText: queryNo,
|
|
|
+ requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey("GroupList") }))
|
|
|
+ {
|
|
|
+ using var jsonNo = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
+ if (jsonNo.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
|
|
|
+ {
|
|
|
+ var accounts = jsonNo.RootElement.GetProperty("Documents").EnumerateArray();
|
|
|
+ while (accounts.MoveNext())
|
|
|
+ {
|
|
|
+ JsonElement account = accounts.Current;
|
|
|
+ noStus.Add(account.GetProperty("no").GetString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (noStus.Count == 0)
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if (i == 9)
|
|
|
+ {
|
|
|
+ string msg = $"OS,{_option.Location},school/course/upsert-list()\n 编号生成异常,重复生成次数超过10次";
|
|
|
+ await _dingDing.SendBotMsg(msg, GroupNames.醍摩豆服務運維群組);
|
|
|
+ throw new Exception(msg);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ list.no = $"{Utils.CreatSaltString(6, "0123456789")}";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception ex) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|