|
@@ -0,0 +1,161 @@
|
|
|
+using Azure.Cosmos;
|
|
|
+using Microsoft.AspNetCore.Mvc;
|
|
|
+using Microsoft.Azure.Cosmos.Table;
|
|
|
+using Microsoft.Extensions.Configuration;
|
|
|
+using Microsoft.Extensions.Options;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Text.Json;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using TEAMModelOS.Models;
|
|
|
+using TEAMModelOS.SDK.DI;
|
|
|
+using TEAMModelOS.SDK.Extension;
|
|
|
+using TEAMModelOS.SDK.Models;
|
|
|
+using TEAMModelOS.SDK.Services;
|
|
|
+
|
|
|
+namespace TEAMModelBI.Controllers.BITmid
|
|
|
+{
|
|
|
+ [Route("tmid")]
|
|
|
+ [ApiController]
|
|
|
+ public class TmidController : ControllerBase
|
|
|
+ {
|
|
|
+ private readonly AzureCosmosFactory _azureCosmos;
|
|
|
+ private readonly AzureStorageFactory _azureStorage;
|
|
|
+ private readonly AzureRedisFactory _azureRedis;
|
|
|
+ private readonly DingDing _dingDing;
|
|
|
+ private readonly Option _option;
|
|
|
+ private readonly IConfiguration _configuration;
|
|
|
+ private readonly HttpTrigger _httpTrigger;
|
|
|
+
|
|
|
+ public TmidController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, DingDing dingDing, IOptionsSnapshot<Option> option, IConfiguration configuration, HttpTrigger httpTrigger)
|
|
|
+ {
|
|
|
+ _azureCosmos = azureCosmos;
|
|
|
+ _azureStorage = azureStorage;
|
|
|
+ _azureRedis = azureRedis;
|
|
|
+ _dingDing = dingDing;
|
|
|
+ _option = option?.Value;
|
|
|
+ _configuration = configuration;
|
|
|
+ _httpTrigger = httpTrigger;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 取得TMID綜合資料
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="jsonElement"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ [ProducesDefaultResponseType]
|
|
|
+ //[AuthToken(Roles = "admin")]
|
|
|
+ [HttpPost("get-tmidstics")]
|
|
|
+ public async Task<IActionResult> GetTmidStics(JsonElement jsonElement)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var cosmosClientIes5 = _azureCosmos.GetCosmosClient(); //CosmosDB IES5
|
|
|
+ var cosmosClientCsv2 = _azureCosmos.GetCosmosClient(name: "CoreServiceV2"); //CosmosDB CSV2
|
|
|
+ var storageClientCsv2 = _azureStorage.GetCloudTableClient(name: "CoreServiceV2"); //Storage CSV2
|
|
|
+ //取得參數
|
|
|
+ if (!jsonElement.TryGetProperty("tmids", out JsonElement tmidsJobj)) return BadRequest();//TMID(array)
|
|
|
+ List<string> tmids = tmidsJobj.ToObject<List<string>>();
|
|
|
+
|
|
|
+ Dictionary<string, TmidStics> tmidDic = new Dictionary<string, TmidStics>();
|
|
|
+ if (tmids.Count.Equals(0)) return BadRequest();
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //Core
|
|
|
+ //基本資料
|
|
|
+ string tmidListStr = JsonSerializer.Serialize(tmids);
|
|
|
+ string sqlIdBase = $"SELECT c.id, c.name, c.mail, c.mobile FROM c WHERE ARRAY_CONTAINS({tmidListStr}, c.id, true)";
|
|
|
+ await foreach (var item in cosmosClientCsv2.GetContainer("Core", "ID2").GetItemQueryStreamIterator(queryText: sqlIdBase, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"base") }))
|
|
|
+ {
|
|
|
+ var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
+ foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
|
|
|
+ {
|
|
|
+ string tmid = obj.GetProperty("id").GetString();
|
|
|
+ string name = Convert.ToString(obj.GetProperty("name"));
|
|
|
+ string mail = (obj.TryGetProperty("mail", out JsonElement mailJ)) ? Convert.ToString(mailJ) : string.Empty;
|
|
|
+ string mobile = (obj.TryGetProperty("mobile", out JsonElement mobileJ)) ? Convert.ToString(mobileJ) : string.Empty;
|
|
|
+ if (!tmidDic.ContainsKey(tmid))
|
|
|
+ {
|
|
|
+ TmidStics tmidStics = new TmidStics();
|
|
|
+ tmidStics.id = tmid;
|
|
|
+ tmidStics.name = name;
|
|
|
+ tmidStics.mail = mail;
|
|
|
+ tmidStics.mobile = mobile;
|
|
|
+ tmidDic.Add(tmid, tmidStics);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //IES5
|
|
|
+ //個人空間使用狀況
|
|
|
+ foreach (KeyValuePair<string, TmidStics> dicItem in tmidDic)
|
|
|
+ {
|
|
|
+ string tmidNow = dicItem.Key;
|
|
|
+ var space = await BlobService.GetSurplusSpace(tmidNow, "private", _option.Location, _azureCosmos, _azureRedis, _azureStorage, _dingDing, _httpTrigger);
|
|
|
+ tmidDic[tmidNow].ies5.usedSize = space.usedSize;
|
|
|
+ tmidDic[tmidNow].ies5.teachSize = space.teach * 1073741824;
|
|
|
+ tmidDic[tmidNow].ies5.totalSize = space.total * 1073741824;
|
|
|
+ tmidDic[tmidNow].ies5.surplusSize = space.surplus;
|
|
|
+ }
|
|
|
+
|
|
|
+ //積分
|
|
|
+ var tablePoints = storageClientCsv2.GetTableReference("Points");
|
|
|
+ foreach (KeyValuePair<string, TmidStics> dicItem in tmidDic)
|
|
|
+ {
|
|
|
+ string tmidNow = dicItem.Key;
|
|
|
+ List<string> sp = new List<string>() { "Get" };
|
|
|
+ var pointsResult = tablePoints.Get<TmidSticsPoint>(partitionKey: tmidNow);
|
|
|
+ foreach(TmidSticsPoint pointsRow in pointsResult)
|
|
|
+ {
|
|
|
+ tmidDic[tmidNow].points += pointsRow.Get;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //輸出
|
|
|
+ List<object> data = new List<object>();
|
|
|
+ foreach (KeyValuePair<string, TmidStics> dicItem in tmidDic)
|
|
|
+ {
|
|
|
+ data.Add(dicItem.Value);
|
|
|
+ }
|
|
|
+ return Ok(new { data });
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ await _dingDing.SendBotMsg($"BI,{_option.Location} /tmid/get-tmidstics \n {ex.Message}\n{ex.StackTrace}", GroupNames.台北開發測試群組);
|
|
|
+ return BadRequest();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //Model
|
|
|
+ //TMID統計 基本資訊
|
|
|
+ private class TmidStics
|
|
|
+ {
|
|
|
+ public TmidStics()
|
|
|
+ {
|
|
|
+ ies5 = new TmidSticsIes5();
|
|
|
+ }
|
|
|
+ public string id { get; set; }
|
|
|
+ public string name { get; set; }
|
|
|
+ public string mobile { get; set; }
|
|
|
+ public string mail { get; set; }
|
|
|
+ public int points { get; set; } //積分
|
|
|
+ public TmidSticsIes5 ies5 { get; set; } //IES統計資料
|
|
|
+ }
|
|
|
+ //TMID統計 IES5資訊
|
|
|
+ private class TmidSticsIes5
|
|
|
+ {
|
|
|
+ public long usedSize { get; set; } //已使用的存儲空間(Byte)
|
|
|
+ public long teachSize { get; set; } //分配給教師的空間(Byte)
|
|
|
+ public long totalSize { get; set; } //總空間(Byte)
|
|
|
+ public long surplusSize { get; set; } //剩余的空間(Byte)
|
|
|
+ }
|
|
|
+
|
|
|
+ public class TmidSticsPoint : TableEntity
|
|
|
+ {
|
|
|
+ public int Get { get; set; }
|
|
|
+ public string TaskNum { get; set; }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|