123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533 |
- using Microsoft.Azure.Cosmos;
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.Options;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TEAMModelOS.Models;
- using TEAMModelOS.SDK;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Extension;
- using TEAMModelOS.SDK.Models;
- using TEAMModelOS.SDK.Services;
- using static TEAMModelBI.Models.Extension.GeoRegion;
- using Microsoft.Azure.Documents.Client;
- using Microsoft.Azure.Cosmos.Table;
- 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]
- [Authorize(Roles = "IES")]
- [HttpPost("get-tmidstics")]
- public async Task<IActionResult> GetTmidStics(JsonElement jsonElement)
- {
- try
- {
- if (!jsonElement.TryGetProperty("tmids", out JsonElement tmidsJobj)) return BadRequest();//TMID(array)
- var tmids = tmidsJobj.Deserialize<List<string>>();
- if (tmids.Count is 0) return BadRequest();
- var datetime = DateTimeOffset.UtcNow.AddDays(-1);
- int y = datetime.Year;
- int m = datetime.Month;
- int d = datetime.Day;
- string dateFromStr = (jsonElement.TryGetProperty("dateFrom", out JsonElement dateFromJobj)) ? dateFromJobj.GetString() : string.Empty; //查詢日期:起始(string)[例]2023-03-05
- string dateToStr = (jsonElement.TryGetProperty("dateTo", out JsonElement dateToJobj)) ? dateToJobj.GetString() : string.Empty; //查詢日期:結束(string)[例]2023-03-27
- string dateUnit = (jsonElement.TryGetProperty("dateUnit", out JsonElement dateUnitJobj)) ? dateUnitJobj.GetString().ToLower() : "month";
- string mode = (jsonElement.TryGetProperty("mode", out JsonElement modeJobj)) ? modeJobj.GetString().ToLower() : "default";
-
- //起始終止日期換算
- long dateTimeFromSec = 0;
- long dateTimeToSec = 0;
- if(!string.IsNullOrWhiteSpace(dateFromStr) && !string.IsNullOrWhiteSpace(dateToStr))
- {
- DateTimeOffset dateTimeFrom;
- DateTimeOffset dateTimeTo;
- List<string> dateFromList = dateFromStr.Split('-').ToList();
- int dateFromYear = Convert.ToInt32(dateFromList[0], 10);
- int dateFromMonth = (dateUnit.Equals("day") || dateUnit.Equals("month")) ? Convert.ToInt32(dateFromList[1], 10) : 1;
- int dateFromDay = (dateUnit.Equals("day")) ? Convert.ToInt32(dateFromList[2], 10) : 1;
- List<string> dateToList = dateToStr.Split('-').ToList();
- int dateToYear = Convert.ToInt32(dateToList[0], 10);
- int dateToMonth = (dateUnit.Equals("day") || dateUnit.Equals("month")) ? Convert.ToInt32(dateToList[1], 10) : 1;
- int dateToDay = (dateUnit.Equals("day")) ? Convert.ToInt32(dateToList[2], 10) : 1;
- switch (dateUnit)
- {
- case "year":
- dateTimeFrom = new DateTimeOffset(dateFromYear, 1, 1, 0, 0, 0, TimeSpan.Zero);
- dateTimeTo = new DateTimeOffset(dateToYear, 12, 31, 23, 59, 59, TimeSpan.Zero);
- break;
- case "month":
- dateTimeFrom = new DateTimeOffset(dateFromYear, dateFromMonth, 1, 0, 0, 0, TimeSpan.Zero);
- dateTimeTo = new DateTimeOffset(dateToYear, dateToMonth, DateTime.DaysInMonth(dateToYear, dateToMonth), 23, 59, 59, TimeSpan.Zero);
- break;
- case "day":
- dateTimeFrom = new DateTimeOffset(dateFromYear, dateFromMonth, dateFromDay, 0, 0, 0, TimeSpan.Zero);
- dateTimeTo = new DateTimeOffset(dateToYear, dateToMonth, dateToDay, 23, 59, 59, TimeSpan.Zero);
- break;
- default:
- dateTimeFrom = new DateTimeOffset(y, m, d, 0, 0, 0, TimeSpan.Zero);
- dateTimeTo = new DateTimeOffset(y, m, d, 23, 59, 59, TimeSpan.Zero);
- break;
- }
- dateTimeFromSec = dateTimeFrom.ToUnixTimeSeconds();
- dateTimeToSec = dateTimeTo.ToUnixTimeSeconds();
- y = 0;
- m = 0;
- d = 0;
- }
- //取得項目列表
- List<string> eventList = new List<string>();
- if(mode.Equals("simple"))
- {
- eventList = new List<string>() { "points" };
- } else
- {
- eventList = new List<string>() { "coupons", "login", "prod", "points", "iot", "ies5", "benefits", "sokrates" };
- }
- //服務Client端
- var cosmosClientIes5 = _azureCosmos.GetCosmosClient(); //CosmosDB IES5
- var cosmosClientCsv2 = _azureCosmos.GetCosmosClient(name: "CoreServiceV2"); //CosmosDB CSV2
- var storageClientCsv2 = _azureStorage.GetCloudTableClient(name: "CoreServiceV2"); //Storage CSV2
- var tableCouponClient = storageClientCsv2.GetTableReference("Coupon");
- var tablePointsClient = storageClientCsv2.GetTableReference("Points");
- var redisClient = _azureRedis.GetRedisClient(4);
- //取得TMID基本資料
- Dictionary<string, TmidStics> tmidDic = new();
- QueryDefinition query =
- new QueryDefinition(@"SELECT c.id, c.name, c.picture, c.mobile, c.mail, c.lang, c.wechat, c.facebook, c.google, c.ding, c.apple, c.educloudtw, c.ts FROM c WHERE (ARRAY_CONTAINS(@key, c.id) OR ARRAY_CONTAINS(@key, c.mobile))")
- .WithParameter("@key", tmids);
- await foreach (var item in cosmosClientCsv2
- .GetContainer("Core", "ID2")
- .GetItemQueryStreamIteratorQuery(query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("base") }))
- {
- using var json = await JsonDocument.ParseAsync(item.Content);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- foreach (var doc in json.RootElement.GetProperty("Documents").EnumerateArray())
- {
- string id = doc.GetProperty("id").GetString();
- if(!tmids.Contains(id)) tmids.Add(id);
- //基本資料
- TmidStics tmidStics = (tmidDic.ContainsKey(id)) ? tmidDic[id] : new() { id = id };
- if (eventList.Contains("ies5")) tmidStics.ies5 = new();
- if (eventList.Contains("points")) tmidStics.points = new();
- if (eventList.Contains("sokrates")) tmidStics.sokrates = new();
- if (eventList.Contains("benefits")) tmidStics.benefits = new();
- if (eventList.Contains("iot")) tmidStics.iot = new();
- tmidStics.name = doc.GetProperty("name").GetString();
- tmidStics.picture = doc.GetProperty("picture").GetString();
- tmidStics.mobile = GenDataMask(doc.GetProperty("mobile").GetString(), "mobile");
- tmidStics.mail = GenDataMask(doc.GetProperty("mail").GetString(), "mail");
- tmidStics.lang = (doc.TryGetProperty("lang", out JsonElement lang)) ? lang.GetString() : string.Empty;
- tmidStics.wechat = (doc.TryGetProperty("wechat", out JsonElement wechat) && !string.IsNullOrWhiteSpace(wechat.GetString())) ? true : false;
- tmidStics.facebook = (doc.TryGetProperty("facebook", out JsonElement facebook) && !string.IsNullOrWhiteSpace(facebook.GetString())) ? true : false;
- tmidStics.google = (doc.TryGetProperty("google", out JsonElement google) && !string.IsNullOrWhiteSpace(google.GetString())) ? true : false;
- tmidStics.ding = (doc.TryGetProperty("ding", out JsonElement ding) && !string.IsNullOrWhiteSpace(ding.GetString())) ? true : false;
- tmidStics.apple = (doc.TryGetProperty("apple", out JsonElement apple) && !string.IsNullOrWhiteSpace(apple.GetString())) ? true : false;
- tmidStics.ts = (doc.TryGetProperty("ts", out JsonElement ts)) ? ts.GetInt64() : 0;
- tmidDic.Add(id, tmidStics);
- }
- }
- }
- //取得TMID進階資料
- regiondata regionData = GetRegionData(); //取得國省市區地理資訊架構
- query = new QueryDefinition(@"SELECT c.id, c.name, c.mobile, c.mail, c.country, c.province, c.city, c.schoolCode, c.schoolCodeW, c.unitType, c.unitName, c.jobTitle FROM c WHERE (ARRAY_CONTAINS(@key, c.id) OR ARRAY_CONTAINS(@key, c.mobile))")
- .WithParameter("@key", tmids);
- await foreach (var item in cosmosClientCsv2
- .GetContainer("Core", "ID2")
- .GetItemQueryStreamIteratorQuery(query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("base-ex") }))
- {
- using var json = await JsonDocument.ParseAsync(item.Content);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- foreach (var doc in json.RootElement.GetProperty("Documents").EnumerateArray())
- {
- string id = doc.GetProperty("id").GetString();
- TmidStics tmidStics = (tmidDic.ContainsKey(id)) ? tmidDic[id] : new() { id = id };
- if (eventList.Contains("ies5")) tmidStics.ies5 = new();
- if (eventList.Contains("points")) tmidStics.points = new();
- if (eventList.Contains("sokrates")) tmidStics.sokrates = new();
- if (eventList.Contains("benefits")) tmidStics.benefits = new();
- if (eventList.Contains("iot")) tmidStics.iot = new();
- if (string.IsNullOrWhiteSpace(tmidStics.name)) tmidStics.name = doc.GetProperty("name").GetString();
- if (string.IsNullOrWhiteSpace(tmidStics.mobile)) tmidStics.mobile = GenDataMask(doc.GetProperty("mobile").GetString(), "mobile");
- if (string.IsNullOrWhiteSpace(tmidStics.mail)) tmidStics.mail = GenDataMask(doc.GetProperty("mail").GetString(), "mail");
- string country = doc.GetProperty("country").GetString();
- string province = doc.GetProperty("province").GetString();
- string city = doc.GetProperty("city").GetString();
- string district = string.Empty;
- if (!string.IsNullOrWhiteSpace(country) && country.Equals("TW"))
- {
- district = city;
- city = province;
- province = string.Empty;
- }
- tmidStics.country = (!string.IsNullOrWhiteSpace(country) && regionData.country.ContainsKey(country)) ? regionData.country[country].name.Replace("地區", "").Replace("地区", "") : country;
- tmidStics.province = (!string.IsNullOrWhiteSpace(country) && regionData.country.ContainsKey(country) && !string.IsNullOrWhiteSpace(province) && regionData.province.ContainsKey(country) && regionData.province[country].ContainsKey(province)) ? regionData.province[country][province].name : province;
- if (!string.IsNullOrWhiteSpace(country) && country.Equals("TW"))
- tmidStics.city = (regionData.city.ContainsKey(country) && !string.IsNullOrWhiteSpace(city) && regionData.city[country]["tw"].ContainsKey(city)) ? regionData.city[country]["tw"][city].name : city;
- else if (!string.IsNullOrWhiteSpace(country) && country.Equals("CN"))
- tmidStics.city = (regionData.city.ContainsKey(country) && !string.IsNullOrWhiteSpace(province) && !string.IsNullOrWhiteSpace(city) && regionData.city[country].ContainsKey(province) && regionData.city[country][province].ContainsKey(city)) ? regionData.city[country][province][city].name : city;
- tmidStics.dist = (!string.IsNullOrWhiteSpace(district) && country.Equals("TW") && regionData.city[country]["tw"].ContainsKey(city) && regionData.dist[country]["tw"][city].ContainsKey(district)) ? regionData.dist[country]["tw"][city][district].name : district;
- tmidStics.schoolCode = (doc.TryGetProperty("schoolCode", out JsonElement schCode)) ? schCode.GetString() : string.Empty;
- tmidStics.schoolCodeW = (doc.TryGetProperty("schoolCodeW", out JsonElement schCodeW)) ? schCodeW.GetString() : string.Empty;
- tmidStics.unitType = (doc.TryGetProperty("unitType", out JsonElement unitType)) ? unitType.GetString() : string.Empty;
- tmidStics.unitName = (doc.TryGetProperty("unitName", out JsonElement unitName)) ? unitName.GetString() : string.Empty;
- tmidStics.jobTitle = (doc.TryGetProperty("jobTitle", out JsonElement jobTitle)) ? jobTitle.GetString() : string.Empty;
- }
- }
- }
- if(tmidDic.Count.Equals(0)) return Ok(new List<object>());
- //取得 票券、登入各服務的時間、取得積分、個人服務授權、IOT
- foreach (KeyValuePair<string, TmidStics> dicItem in tmidDic)
- {
- string id = dicItem.Key;
- TmidStics tmidStics = dicItem.Value;
- //票券
- if (eventList.Contains("coupons"))
- {
- var usersCoupons = tableCouponClient.Get<DynamicTableEntity>(id);
- foreach (var coupon in usersCoupons)
- {
- tmidStics.coupons.Add(
- new TmidCoupon()
- {
- code = coupon.RowKey,
- exchange = coupon.Properties["exchangeTime"].DateTimeOffsetValue.Value.ToUnixTimeSeconds(),
- });
- }
- }
- //取得登入各服務的時間
- if (eventList.Contains("login"))
- {
- var loginTime = await redisClient.HashGetAllAsync(id);
- foreach (var t in loginTime)
- {
- if (!t.Name.StartsWith("HiTeach") && !_dicClientIds.ContainsKey(t.Name)) continue;
- tmidStics.login.Add(
- new TmidLoginTime()
- {
- product = t.Name.StartsWith("HiTeach") ? t.Name.ToString().Split("-")[0] : _dicClientIds[t.Name],
- time = (long)t.Value
- });
- }
- }
- //個人服務授權
- if (eventList.Contains("prod"))
- {
- tmidStics.prod = await getTMIDAuthService(cosmosClientCsv2, id, "", true);
- }
- }
- //IOT
- if (eventList.Contains("iot"))
- {
- List<string> ids = tmidDic.Keys.ToList();
- List<TmidAnalysisCal> iotResult = new List<TmidAnalysisCal>();
- Dictionary<string, List<TmidAnalysisCal>> iotDic = new Dictionary<string, List<TmidAnalysisCal>>();
- if (!string.IsNullOrWhiteSpace(dateFromStr) && !string.IsNullOrWhiteSpace(dateToStr) && !string.IsNullOrWhiteSpace(dateUnit))
- {
- if (dateUnit.ToLower().Equals("year"))
- {
- iotDic["HiTeach-year"] = await getTMIDIotData(cosmosClientIes5, ids, "HiTeach", "year", y, 0, 0, dateTimeFromSec, dateTimeToSec);
- iotDic["HiTeachCC-year"] = await getTMIDIotData(cosmosClientIes5, ids, "HiTeachCC", "year", y, 0, 0, dateTimeFromSec, dateTimeToSec);
- }
- else if (dateUnit.ToLower().Equals("month"))
- {
- iotDic["HiTeach-month"] = await getTMIDIotData(cosmosClientIes5, ids, "HiTeach", "month", y, m, 0, dateTimeFromSec, dateTimeToSec);
- iotDic["HiTeachCC-month"] = await getTMIDIotData(cosmosClientIes5, ids, "HiTeachCC", "month", y, m, 0, dateTimeFromSec, dateTimeToSec);
- }
- else if (dateUnit.ToLower().Equals("day"))
- {
- iotDic["HiTeach-day"] = await getTMIDIotData(cosmosClientIes5, ids, "HiTeach", "day", y, m, d, dateTimeFromSec, dateTimeToSec);
- iotDic["HiTeachCC-day"] = await getTMIDIotData(cosmosClientIes5, ids, "HiTeachCC", "day", y, m, d, dateTimeFromSec, dateTimeToSec);
- }
- }
- else
- {
- iotDic["HiTeach-year"] = await getTMIDIotData(cosmosClientIes5, ids, "HiTeach", "year", y, 0, 0, dateTimeFromSec, dateTimeToSec);
- iotDic["HiTeach-month"] = await getTMIDIotData(cosmosClientIes5, ids, "HiTeach", "month", y, m, 0, dateTimeFromSec, dateTimeToSec);
- iotDic["HiTeach-day"] = await getTMIDIotData(cosmosClientIes5, ids, "HiTeach", "day", y, m, d, dateTimeFromSec, dateTimeToSec);
- iotDic["HiTeachCC-year"] = await getTMIDIotData(cosmosClientIes5, ids, "HiTeachCC", "year", y, 0, 0, dateTimeFromSec, dateTimeToSec);
- iotDic["HiTeachCC-month"] = await getTMIDIotData(cosmosClientIes5, ids, "HiTeachCC", "month", y, m, 0, dateTimeFromSec, dateTimeToSec);
- iotDic["HiTeachCC-day"] = await getTMIDIotData(cosmosClientIes5, ids, "HiTeachCC", "day", y, m, d, dateTimeFromSec, dateTimeToSec);
- }
- //資料整理
- List<string> iotDicKeyList = new List<string>() { "HiTeach-year", "HiTeachCC-year", "HiTeach-month", "HiTeachCC-month", "HiTeach-day", "HiTeachCC-day" };
- foreach(string iotDicKey in iotDicKeyList)
- {
- if (iotDic.ContainsKey(iotDicKey))
- {
- foreach(TmidAnalysisCal iotCalData in iotDic[iotDicKey])
- {
- string id = iotCalData.tmid;
- TmidStics tmidStics = tmidDic[id];
- switch (iotDicKey)
- {
- case "HiTeach-year":
- tmidStics.iot.hiteach.year = iotCalData;
- break;
- case "HiTeachCC-year":
- tmidStics.iot.hiteachcc.year = iotCalData;
- break;
- case "HiTeach-month":
- tmidStics.iot.hiteach.month = iotCalData;
- break;
- case "HiTeachCC-month":
- tmidStics.iot.hiteachcc.month = iotCalData;
- break;
- case "HiTeach-day":
- tmidStics.iot.hiteach.day = iotCalData;
- break;
- case "HiTeachCC-day":
- tmidStics.iot.hiteachcc.day = iotCalData;
- break;
- }
- }
- }
- }
- }
- //積分
- if (eventList.Contains("points"))
- {
- List<string> ids = tmidDic.Keys.ToList();
- //防止Query過長,分次取得
- Dictionary<int, List<string>> idsDic = new Dictionary<int, List<string>>();
- int idCount = 0;
- int max = 100; //要分割成多少人一組
- foreach (string id in ids)
- {
- int indx = idCount / max;
- if (!idsDic.ContainsKey(indx)) idsDic.Add(indx, new List<string>());
- idsDic[indx].Add(id);
- idCount++;
- }
- foreach(var itemd in idsDic)
- {
- string filter = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "Points");
- string rkFilterCombine = string.Empty;
- foreach (string rowKey in itemd.Value)
- {
- string rkFilter = TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, rowKey);
- if (string.IsNullOrWhiteSpace(rkFilterCombine))
- {
- rkFilterCombine = rkFilter;
- }
- else
- {
- rkFilterCombine = TableQuery.CombineFilters(rkFilterCombine, TableOperators.Or, rkFilter);
- }
- }
- filter = TableQuery.CombineFilters(filter, TableOperators.And, rkFilterCombine);
- TableQuery tableQuery = new TableQuery().Where(filter);
- var usersPoints = tablePointsClient.ExecuteQuery(tableQuery);
- if (usersPoints.Any())
- {
- foreach (DynamicTableEntity item in usersPoints)
- {
- string id = item.RowKey.ToString();
- TmidStics tmidStics = tmidDic[id];
- tmidStics.points.points = (usersPoints != null) ? item.Properties["Points"].Int32Value.Value : 0;
- tmidStics.points.level = (usersPoints != null) ? item.Properties["Level"].Int32Value.Value : 0;
- tmidStics.points.balance = (usersPoints != null) ? item.Properties["Balance"].Int32Value.Value : 0;
- }
- }
- }
- }
- //IES5
- if (eventList.Contains("ies5"))
- {
- query = new QueryDefinition(@"SELECT c.id, c.defaultSchool, c.schools FROM c WHERE ARRAY_CONTAINS(@key, c.id)")
- .WithParameter("@key", tmidDic.Keys.ToList());
- await foreach (var item in cosmosClientIes5
- .GetContainer(Constant.TEAMModelOS, "Teacher")
- .GetItemQueryStreamIteratorQuery(query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
- {
- using var json = await JsonDocument.ParseAsync(item.Content);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- foreach (var doc in json.RootElement.GetProperty("Documents").EnumerateArray())
- {
- string id = doc.GetProperty("id").GetString();
- TmidStics tmidStics = tmidDic[id];
- //IES5學校資訊
- string defaultschool = (doc.TryGetProperty("defaultSchool", out JsonElement _defaultSchool)) ? _defaultSchool.ToString() : string.Empty; //預設學校
- List<string> schoolIds = new List<string>();
- if (doc.TryGetProperty("schools", out JsonElement schoolsJobj))
- {
- foreach (var obj in schoolsJobj.EnumerateArray())
- {
- string schoolCodeNow = $"{obj.GetProperty("schoolId")}";
- string status = $"{obj.GetProperty("status")}";
- if (status.Equals("join"))
- {
- schoolIds.Add(schoolCodeNow); //放入學校ID列,一次取
- }
- }
- }
- if (schoolIds.Count > 0)
- {
- QueryDefinition querysc = new QueryDefinition(@"SELECT c.id, c.name, c.region, c.province, c.city, c.dist, c.size, c.period FROM c WHERE ARRAY_CONTAINS(@key, c.id)")
- .WithParameter("@key", schoolIds);
- await foreach (var itemsc in cosmosClientIes5
- .GetContainer(Constant.TEAMModelOS, "School")
- .GetItemQueryStreamIteratorQuery(querysc, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
- {
- using var jsonsc = await JsonDocument.ParseAsync(itemsc.Content);
- if (jsonsc.RootElement.TryGetProperty("_count", out JsonElement countsc) && countsc.GetUInt16() > 0)
- {
- foreach (var school in jsonsc.RootElement.GetProperty("Documents").EnumerateArray())
- {
- string schoolCodeNow = school.GetProperty("id").GetString();
- //當前學年 ※若有多學段,取第一個學段來取得學年資訊吧
- Period period = school.GetProperty("period").Deserialize<List<Period>>().First();
- var info = SchoolService.GetSemester(period, 0, DateTime.Now.ToString());
- int studyYear = info.studyYear;
- //學校Blob使用狀況
- (long usedSize, long teach, long total, long surplus, Dictionary<string, double?> catalog) schoolUsedBlob = await BlobService.GetSurplusSpace(schoolCodeNow, "school", _option.Location, _azureCosmos, _azureRedis, _azureStorage, _dingDing, _httpTrigger);
- //老師在此學校的課程數 ※只取當前學年
- int courseCnt = 0;
- var queryCrs = $"SELECT VALUE COUNT(1) FROM ( SELECT DISTINCT VALUE(c.id) FROM c JOIN schedules IN c.schedules WHERE schedules.teacherId = '{id}' AND c.year = {studyYear} )";
- await foreach (int itemCrs in cosmosClientIes5.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql<int>(queryText: queryCrs, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"CourseTask-{schoolCodeNow}") }))
- {
- courseCnt = itemCrs;
- }
- //IES5學校資料製作
- TmidSticsIes5School schoolobj = new TmidSticsIes5School();
- long ssize = (school.TryGetProperty("size", out JsonElement ssizeJson)) ? ssizeJson.GetInt32() : 0;
- long sused = schoolUsedBlob.usedSize + schoolUsedBlob.teach * 1073741824;
- long stotal = ssize * 1073741824;
- object schzize = new
- {
- used = sused,
- total = stotal,
- avaliable = stotal - sused,
- };
- schoolobj.schoolId = schoolCodeNow;
- schoolobj.name = school.GetProperty("name").GetString();
- schoolobj.region = school.GetProperty("region").GetString();
- schoolobj.province = school.GetProperty("province").GetString();
- schoolobj.city = school.GetProperty("city").GetString();
- schoolobj.dist = school.GetProperty("dist").GetString();
- schoolobj.size = schzize;
- schoolobj.courseCnt = courseCnt;
- schoolobj.defaultSchool = (!string.IsNullOrWhiteSpace(defaultschool) && defaultschool.Equals(schoolCodeNow)) ? true : false;
- tmidStics.ies5.schools.Add(schoolobj);
- }
- }
- }
- }
- //IES5個人空間
- var (usedSize, teach, total, surplus, catalog) = await BlobService.GetSurplusSpace(id, "private", _option.Location, _azureCosmos, _azureRedis, _azureStorage, _dingDing, _httpTrigger);
- tmidStics.ies5.usedSize = usedSize;
- tmidStics.ies5.teachSize = teach * 1073741824;
- tmidStics.ies5.totalSize = total * 1073741824;
- tmidStics.ies5.surplusSize = surplus;
- //IES5個人資源數
- tmidStics.ies5.resCount = 0; //教材數
- tmidStics.ies5.itemCount = 0; //題目數
- tmidStics.ies5.paperCount = 0; //試卷數
- ///IES5個人題目數
- await foreach (int itemC in cosmosClientIes5.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIteratorSql<int>(queryText: $"SELECT VALUE COUNT(1) FROM c WHERE c.pid = null", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-{id}") }))
- {
- tmidStics.ies5.itemCount = itemC;
- }
- ///IES5個人試卷數
- await foreach (int paperC in cosmosClientIes5.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIteratorSql<int>(queryText: $"SELECT VALUE COUNT(1) FROM c WHERE c.scope = 'private'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Paper-{id}") }))
- {
- tmidStics.ies5.paperCount = paperC;
- }
- ///IES5個人教材數
- List<BlobService.BlobCount> blogCountList = await BlobService.BloblogCount(cosmosClientIes5, "private", id, "", "");
- foreach (BlobService.BlobCount blobCountRow in blogCountList)
- {
- switch (blobCountRow.type)
- {
- case "doc":
- case "image":
- case "res":
- case "video":
- case "audio":
- case "other":
- tmidStics.ies5.resCount += blobCountRow.count;
- break;
- }
- }
- if (!tmidDic.ContainsKey(id)) tmidDic.Add(id, tmidStics);
- }
- }
- }
- }
- //個人權益
- if (eventList.Contains("benefits"))
- {
- query = new QueryDefinition(@"SELECT * FROM c WHERE (ARRAY_CONTAINS(@key, c.id))")
- .WithParameter("@key", tmidDic.Keys.ToList());
- await foreach (var item in cosmosClientCsv2
- .GetContainer("Core", "ID2")
- .GetItemQueryStreamIteratorQuery(query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("benefits") }))
- {
- using var json = await JsonDocument.ParseAsync(item.Content);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- foreach (var doc in json.RootElement.GetProperty("Documents").EnumerateArray())
- {
- string id = doc.GetProperty("id").GetString();
- if (doc.TryGetProperty("hiteach", out var elementHiteachData))
- {
- tmidDic[id].benefits.hiteach = elementHiteachData.ToObject<List<object>>();
- }
- if (doc.TryGetProperty("hiteachcc", out var elementHiteachCCData))
- {
- tmidDic[id].benefits.hiteachcc = elementHiteachCCData.ToObject<List<object>>();
- }
- }
- }
- }
- }
- //蘇格拉底資料
- if (eventList.Contains("sokrates"))
- {
- query = new QueryDefinition(@"SELECT * FROM c WHERE (ARRAY_CONTAINS(@key, c.id))")
- .WithParameter("@key", tmidDic.Keys.ToList());
- await foreach (var item in cosmosClientCsv2
- .GetContainer("Core", "ID2")
- .GetItemQueryStreamIteratorQuery(query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("sokrates") }))
- {
- using var json = await JsonDocument.ParseAsync(item.Content);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- foreach (var doc in json.RootElement.GetProperty("Documents").EnumerateArray())
- {
- string id = doc.GetProperty("id").GetString();
- if (doc.TryGetProperty("hiteach_data", out var elementHiteachData))
- {
- tmidDic[id].sokrates.hiteach_data = elementHiteachData.ToObject<TmidSokratesHiteach>();
- }
- if (doc.TryGetProperty("user_channels", out var elementUserChannels))
- {
- tmidDic[id].sokrates.user_channels = elementUserChannels.ToObject<object>();
- }
- }
- }
- }
- }
- //輸出
- List<object> data = new();
- foreach (KeyValuePair<string, TmidStics> dicItem in tmidDic)
- {
- data.Add(dicItem.Value);
- }
- return Ok(data);
- }
- catch (Exception ex)
- {
- //await _dingDing.SendBotMsg($"BI,{_option.Location} /tmid/get-tmidstics \n {ex.Message}\n{ex.StackTrace}", GroupNames.台北開發測試群組);
- return BadRequest();
- }
- }
- /// <summary>
- /// 取得TMID IOT資料
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [Authorize(Roles = "IES")]
- [HttpPost("get-tmid-iot")]
- public async Task<IActionResult> GetTmidIot(JsonElement jsonElement)
- {
- try
- {
- if (!jsonElement.TryGetProperty("tmids", out JsonElement tmidsJobj)) return BadRequest(); //TMID(array)
- var tmids = tmidsJobj.Deserialize<List<string>>();
- if (tmids.Count is 0) return BadRequest();
- string dateFromStr = (jsonElement.TryGetProperty("dateFrom", out JsonElement dateFromJobj)) ? dateFromJobj.GetString() : string.Empty; //查詢日期:起始(string)[例]2023-03-05
- string dateToStr = (jsonElement.TryGetProperty("dateTo", out JsonElement dateToJobj)) ? dateToJobj.GetString() : string.Empty; //查詢日期:結束(string)[例]2023-03-27
- string dateUnit = (jsonElement.TryGetProperty("dateUnit", out JsonElement dateUnitJobj)) ? dateUnitJobj.GetString().ToLower() : "month";
- if(string.IsNullOrWhiteSpace(dateFromStr) || string.IsNullOrWhiteSpace(dateToStr))
- {
- return BadRequest();
- }
- Dictionary<string, TmidStics> tmidDic = await GetTmidIotCore(tmids, dateFromStr, dateToStr, dateUnit);
- //輸出
- List<object> data = new();
- foreach (KeyValuePair<string, TmidStics> dicItem in tmidDic)
- {
- data.Add(dicItem.Value);
- }
- return Ok(data);
- }
- catch (Exception ex)
- {
- //await _dingDing.SendBotMsg($"BI,{_option.Location} /tmid/get-tmidstics \n {ex.Message}\n{ex.StackTrace}", GroupNames.台北開發測試群組);
- return BadRequest();
- }
- }
- private async Task<Dictionary<string, TmidStics>> GetTmidIotCore(List<string> tmids, string dateFromStr, string dateToStr, string dateUnit)
- {
- //起始終止日期換算
- long dateTimeFromSec = 0;
- long dateTimeToSec = 0;
- DateTimeOffset dateTimeFrom;
- DateTimeOffset dateTimeTo;
- List<string> dateFromList = dateFromStr.Split('-').ToList();
- int dateFromYear = Convert.ToInt32(dateFromList[0], 10);
- int dateFromMonth = (dateUnit.Equals("day") || dateUnit.Equals("month")) ? Convert.ToInt32(dateFromList[1], 10) : 1;
- int dateFromDay = (dateUnit.Equals("day")) ? Convert.ToInt32(dateFromList[2], 10) : 1;
- List<string> dateToList = dateToStr.Split('-').ToList();
- int dateToYear = Convert.ToInt32(dateToList[0], 10);
- int dateToMonth = (dateUnit.Equals("day") || dateUnit.Equals("month")) ? Convert.ToInt32(dateToList[1], 10) : 1;
- int dateToDay = (dateUnit.Equals("day")) ? Convert.ToInt32(dateToList[2], 10) : 1;
- switch (dateUnit)
- {
- case "year":
- dateTimeFrom = new DateTimeOffset(dateFromYear, 1, 1, 0, 0, 0, TimeSpan.Zero);
- dateTimeTo = new DateTimeOffset(dateToYear, 12, 31, 23, 59, 59, TimeSpan.Zero);
- break;
- case "month":
- dateTimeFrom = new DateTimeOffset(dateFromYear, dateFromMonth, 1, 0, 0, 0, TimeSpan.Zero);
- dateTimeTo = new DateTimeOffset(dateToYear, dateToMonth, DateTime.DaysInMonth(dateToYear, dateToMonth), 23, 59, 59, TimeSpan.Zero);
- break;
- case "day":
- dateTimeFrom = new DateTimeOffset(dateFromYear, dateFromMonth, dateFromDay, 0, 0, 0, TimeSpan.Zero);
- dateTimeTo = new DateTimeOffset(dateToYear, dateToMonth, dateToDay, 23, 59, 59, TimeSpan.Zero);
- break;
- default: //同"month"
- dateTimeFrom = new DateTimeOffset(dateFromYear, dateFromMonth, 1, 0, 0, 0, TimeSpan.Zero);
- dateTimeTo = new DateTimeOffset(dateToYear, dateToMonth, DateTime.DaysInMonth(dateToYear, dateToMonth), 23, 59, 59, TimeSpan.Zero);
- break;
- }
- dateTimeFromSec = dateTimeFrom.ToUnixTimeSeconds();
- dateTimeToSec = dateTimeTo.ToUnixTimeSeconds();
- //資料取得
- Dictionary<string, TmidStics> tmidDic = new();
- foreach (string id in tmids)
- {
- if (!tmidDic.ContainsKey(id))
- {
- TmidStics tmidStics = new TmidStics() { id = id, iot = new() };
- tmidDic.Add(id, tmidStics);
- }
- }
- //IOT資料
- var cosmosClientIes5 = _azureCosmos.GetCosmosClient(); //CosmosDB IES5
- List<TmidAnalysisCal> iotResult = new List<TmidAnalysisCal>();
- Dictionary<string, List<TmidAnalysisCal>> iotDic = new Dictionary<string, List<TmidAnalysisCal>>();
- if (dateUnit.ToLower().Equals("year"))
- {
- iotDic["HiTeach-year"] = await getTMIDIotData(cosmosClientIes5, tmids, "HiTeach", "year", 0, 0, 0, dateTimeFromSec, dateTimeToSec);
- iotDic["HiTeachCC-year"] = await getTMIDIotData(cosmosClientIes5, tmids, "HiTeachCC", "year", 0, 0, 0, dateTimeFromSec, dateTimeToSec);
- }
- else if (dateUnit.ToLower().Equals("month"))
- {
- iotDic["HiTeach-month"] = await getTMIDIotData(cosmosClientIes5, tmids, "HiTeach", "month", 0, 0, 0, dateTimeFromSec, dateTimeToSec);
- iotDic["HiTeachCC-month"] = await getTMIDIotData(cosmosClientIes5, tmids, "HiTeachCC", "month", 0, 0, 0, dateTimeFromSec, dateTimeToSec);
- }
- else if (dateUnit.ToLower().Equals("day"))
- {
- iotDic["HiTeach-day"] = await getTMIDIotData(cosmosClientIes5, tmids, "HiTeach", "day", 0, 0, 0, dateTimeFromSec, dateTimeToSec);
- iotDic["HiTeachCC-day"] = await getTMIDIotData(cosmosClientIes5, tmids, "HiTeachCC", "day", 0, 0, 0, dateTimeFromSec, dateTimeToSec);
- }
- //IOT資料整理
- List<string> iotDicKeyList = new List<string>() { "HiTeach-year", "HiTeachCC-year", "HiTeach-month", "HiTeachCC-month", "HiTeach-day", "HiTeachCC-day" };
- foreach (string iotDicKey in iotDicKeyList)
- {
- if (iotDic.ContainsKey(iotDicKey))
- {
- foreach (TmidAnalysisCal iotCalData in iotDic[iotDicKey])
- {
- string id = iotCalData.tmid;
- TmidStics tmidStics = tmidDic[id];
- switch (iotDicKey)
- {
- case "HiTeach-year":
- tmidStics.iot.hiteach.year = iotCalData;
- break;
- case "HiTeachCC-year":
- tmidStics.iot.hiteachcc.year = iotCalData;
- break;
- case "HiTeach-month":
- tmidStics.iot.hiteach.month = iotCalData;
- break;
- case "HiTeachCC-month":
- tmidStics.iot.hiteachcc.month = iotCalData;
- break;
- case "HiTeach-day":
- tmidStics.iot.hiteach.day = iotCalData;
- break;
- case "HiTeachCC-day":
- tmidStics.iot.hiteachcc.day = iotCalData;
- break;
- }
- }
- }
- }
- return tmidDic;
- }
- /// <summary>
- /// 取得TMID使用產品資料
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [Authorize(Roles = "IES")]
- [HttpPost("get-tmid-useprod")]
- public async Task<IActionResult> GetTmidUseProd(JsonElement jsonElement)
- {
- string dateFromStr = (jsonElement.TryGetProperty("dateFrom", out JsonElement dateFromJobj)) ? dateFromJobj.GetString() : string.Empty; //查詢日期:起始(string)[例]2023-03-05
- string dateToStr = (jsonElement.TryGetProperty("dateTo", out JsonElement dateToJobj)) ? dateToJobj.GetString() : string.Empty; //查詢日期:結束(string)[例]2023-03-27
- List<DateFromTo> dateList = (jsonElement.TryGetProperty("dateList", out JsonElement dateListJobj)) ? dateListJobj.ToObject<List<DateFromTo>>() : new List<DateFromTo>(); //查詢日期起始結束列表
- bool dateFromToValid = (!string.IsNullOrWhiteSpace(dateFromStr) && !string.IsNullOrWhiteSpace(dateToStr)) ? true : false;
- bool dateListValid = (dateList.Count > 0) ? true : false;
- if (dateFromToValid.Equals(false) && dateListValid.Equals(false)) return BadRequest();
- List<string> targets = (jsonElement.TryGetProperty("targets", out JsonElement targetsJobj)) ? targetsJobj.ToObject<List<string>>() : new List<string>() {"tmid", "sch", "geo" }; //資料取得 "tmid":必定會取 "sch":學校資訊 "geo":地理資訊
- bool getLoginTime = (jsonElement.TryGetProperty("getLoginTime", out JsonElement getLoginTimeJobj)) ? getLoginTimeJobj.GetBoolean() : false; //是否取得各服務Login時間
- if (dateFromToValid)
- {
- object result = await GetTmidUseprodCore(dateFromStr, dateToStr, targets, getLoginTime);
- return Ok(result);
- }
- else if(dateListValid)
- {
- List<object> result = new List<object>();
- foreach (DateFromTo dateFromTo in dateList)
- {
- string dateFrom = dateFromTo.dateFrom;
- string dateTo = dateFromTo.dateTo;
- object tmidUseprod = await GetTmidUseprodCore(dateFrom, dateTo, targets, getLoginTime);
- result.Add(tmidUseprod);
- }
- return Ok(result);
- }
- else
- {
- return Ok(new { });
- }
- }
- private async Task<object> GetTmidUseprodCore(string dateFromStr, string dateToStr, List<string> targets, bool getLoginTime)
- {
- Dictionary<string, TmidStics> tmidDic = new();
- HashSet<string> schIds = new HashSet<string>(); //生成的TMID所關聯的學校ID列表
- DateTime dateFrom = DateTime.ParseExact(dateFromStr, "yyyy-MM-dd", null);
- DateTime dateTo = DateTime.ParseExact(dateToStr, "yyyy-MM-dd", null).Add(new TimeSpan(0, 23, 59, 59));
- DateTimeOffset dateTimeFrom = new DateTimeOffset(dateFrom, TimeSpan.Zero);
- DateTimeOffset dateTimeTo = new DateTimeOffset(dateTo, TimeSpan.Zero);
- long unixTimeFrom = dateTimeFrom.ToUnixTimeSeconds();
- long unixTimeTo = dateTimeTo.ToUnixTimeSeconds();
- //大陸站 ID計算方式特化處理
- if (_option.Location.Contains("China"))
- {
- unixTimeFrom += 5000000000;
- unixTimeTo += 5000000000;
- }
- //服務Client端
- var cosmosClientIes5 = _azureCosmos.GetCosmosClient(); //CosmosDB IES5
- var cosmosClientCsv2 = _azureCosmos.GetCosmosClient(name: "CoreServiceV2"); //CosmosDB CSV2
- var cosmosClientCsv2CnRead = _azureCosmos.GetCosmosClient(name: "CoreServiceV2CnRead"); //CosmosDB CSV2 CN Read
- var storageClientCsv2 = _azureStorage.GetCloudTableClient(name: "CoreServiceV2"); //Storage CSV2
- var tableCouponClient = storageClientCsv2.GetTableReference("Coupon");
- var tablePointsClient = storageClientCsv2.GetTableReference("Points");
- var redisClient = _azureRedis.GetRedisClient(4);
- //取得TMID資料
- HashSet<string> tmids = new HashSet<string>();
- int pageSize = (getLoginTime) ? 100 : 5000;
- string? continuationToken = null;
- string query = $"SELECT * FROM c WHERE {unixTimeFrom} <= StringToNumber(c.id) AND StringToNumber(c.id) <= {unixTimeTo}";
- do
- {
- await foreach (var item in cosmosClientCsv2
- .GetContainer("Core", "ID2")
- .GetItemQueryStreamIteratorSql(query, continuationToken: continuationToken, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("base"), MaxItemCount = pageSize }))
- {
- using var json = await JsonDocument.ParseAsync(item.Content);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- foreach (var doc in json.RootElement.GetProperty("Documents").EnumerateArray())
- {
- string id = doc.GetProperty("id").GetString();
- if (!tmidDic.ContainsKey(id)) tmidDic.Add(id, new TmidStics());
- tmids.Add(id);
- TmidStics tmidStics = tmidDic[id];
- //基本資料
- tmidStics.name = doc.GetProperty("name").GetString();
- //取得登入各服務的時間 ※getLoginTime == true 才取
- if (getLoginTime)
- {
- var loginTime = await redisClient.HashGetAllAsync(id);
- foreach (var t in loginTime)
- {
- if (!t.Name.StartsWith("HiTeach") && !_dicClientIds.ContainsKey(t.Name)) continue;
- tmidStics.login.Add(
- new TmidLoginTime()
- {
- product = t.Name.StartsWith("HiTeach") ? t.Name.ToString().Split("-")[0] : _dicClientIds[t.Name],
- time = (long)t.Value
- });
- }
- }
- }
- }
- continuationToken = item.ContinuationToken;
- }
- }
- while (!string.IsNullOrWhiteSpace(continuationToken));
- //取得TMID進階資料
- continuationToken = null;
- do
- {
- QueryDefinition querye = new QueryDefinition(@"SELECT c.id, c.name, c.mobile, c.mail, c.country, c.province, c.city, c.schoolCode, c.schoolCodeW, c.unitType, c.unitName, c.jobTitle FROM c WHERE (ARRAY_CONTAINS(@key, c.id) OR ARRAY_CONTAINS(@key, c.mobile))")
- .WithParameter("@key", tmids);
- await foreach (var item in cosmosClientCsv2
- .GetContainer("Core", "ID2")
- .GetItemQueryStreamIteratorQuery(querye, continuationToken: continuationToken, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("base-ex"), MaxItemCount = pageSize }))
- {
- using var json = await JsonDocument.ParseAsync(item.Content);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- foreach (var doc in json.RootElement.GetProperty("Documents").EnumerateArray())
- {
- string id = doc.GetProperty("id").GetString();
- TmidStics tmidStics = tmidDic[id];
- tmidStics.schoolCode = (doc.TryGetProperty("schoolCode", out JsonElement schCode)) ? schCode.GetString() : string.Empty;
- tmidStics.schoolCodeW = (doc.TryGetProperty("schoolCodeW", out JsonElement schCodeW)) ? schCodeW.GetString() : string.Empty;
- if (!string.IsNullOrWhiteSpace(tmidStics.schoolCode)) schIds.Add(tmidStics.schoolCode);
- if (!string.IsNullOrWhiteSpace(tmidStics.schoolCodeW)) schIds.Add(tmidStics.schoolCodeW);
- if (!string.IsNullOrWhiteSpace(tmidStics.schoolCode) && string.IsNullOrWhiteSpace(tmidStics.schoolId))
- {
- tmidStics.schoolId = tmidStics.schoolCode;
- }
- else if (!string.IsNullOrWhiteSpace(tmidStics.schoolCodeW) && string.IsNullOrWhiteSpace(tmidStics.schoolId))
- {
- tmidStics.schoolId = tmidStics.schoolCodeW;
- }
- }
- }
- continuationToken = item.ContinuationToken;
- }
- }
- while (!string.IsNullOrWhiteSpace(continuationToken));
- //TMID資料整理
- foreach (var item in tmidDic)
- {
- TmidStics tmidStics = item.Value;
- if (!string.IsNullOrWhiteSpace(tmidStics.schoolCode) || !string.IsNullOrWhiteSpace(tmidStics.schoolCodeW))
- {
- if (!string.IsNullOrWhiteSpace(tmidStics.schoolCode))
- {
- tmidStics.schoolId = tmidStics.schoolCode;
- }
- else if (!string.IsNullOrWhiteSpace(tmidStics.schoolCodeW))
- {
- tmidStics.schoolId = tmidStics.schoolCodeW;
- }
- }
- }
- //取得學校資料
- Dictionary<string, Dictionary<string, string>> school = new Dictionary<string, Dictionary<string, string>>();
- if (targets.Contains("sch"))
- {
- string querys = $"SELECT * FROM c WHERE ARRAY_CONTAINS({JsonSerializer.Serialize(schIds)}, c.shortCode, true)";
- await foreach (var item in cosmosClientCsv2CnRead
- .GetContainer("Core", "School")
- .GetItemQueryStreamIteratorSql(querys, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("base") }))
- {
- using var json = await JsonDocument.ParseAsync(item.Content);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- foreach (var doc in json.RootElement.GetProperty("Documents").EnumerateArray())
- {
- Dictionary<string, string> schoolRow = new Dictionary<string, string>();
- string shortCode = doc.GetProperty("shortCode").GetString();
- string name = doc.GetProperty("name").GetString();
- string countryId = doc.GetProperty("countryId").GetString();
- string countryName = doc.GetProperty("countryName").GetString();
- string provinceId = doc.GetProperty("provinceId").GetString();
- string provinceName = doc.GetProperty("provinceName").GetString();
- string cityId = doc.GetProperty("cityId").GetString();
- string cityName = doc.GetProperty("cityName").GetString();
- string distId = doc.GetProperty("distId").GetString();
- string distName = doc.GetProperty("distName").GetString();
- schoolRow.Add("shortCode", shortCode);
- schoolRow.Add("name", name);
- schoolRow.Add("countryId", countryId);
- schoolRow.Add("countryName", countryName);
- schoolRow.Add("provinceId", provinceId);
- schoolRow.Add("provinceName", provinceName);
- schoolRow.Add("cityId", cityId);
- schoolRow.Add("cityName", cityName);
- schoolRow.Add("distId", distId);
- schoolRow.Add("distName", distName);
- school.Add(shortCode, schoolRow);
- }
- }
- }
- }
- //返回值整理
- List<TmidIotProdCnt> tmid = new List<TmidIotProdCnt>();
- List<TmidIotProdCntGeo> sch = new List<TmidIotProdCntGeo>(); //取得的IOT學校統計資訊
- foreach (var item in tmidDic)
- {
- //TMID
- string id = item.Key;
- TmidIotProdCnt tmidRow = new TmidIotProdCnt();
- tmidRow.id = id;
- tmidRow.name = item.Value.name;
- tmidRow.schid = (!string.IsNullOrWhiteSpace(item.Value.schoolId)) ? item.Value.schoolId : null;
- foreach (TmidLoginTime login in item.Value.login)
- {
- switch (login.product)
- {
- case "HiTeach":
- tmidRow.hiteach++;
- break;
- case "HiTeachCC":
- tmidRow.hiteachcc++;
- break;
- case "HiTA":
- tmidRow.hita++;
- break;
- case "IES5":
- tmidRow.ies5++;
- break;
- case "Account":
- tmidRow.account++;
- break;
- case "Sokrates":
- tmidRow.sokrates++;
- break;
- case "SokAPP":
- tmidRow.sokapp++;
- break;
- case "IRS":
- tmidRow.irs++;
- break;
- }
- }
- tmid.Add(tmidRow);
- //school
- if (targets.Contains("sch"))
- {
- if (!string.IsNullOrWhiteSpace(tmidRow.schid))
- {
- var schRow = sch.Where(s => s.id.Equals(tmidRow.schid)).FirstOrDefault();
- if (schRow == null)
- {
- TmidIotProdCntGeo tmpData = new TmidIotProdCntGeo();
- tmpData.id = tmidRow.schid;
- tmpData.schid = tmidRow.schid;
- if (school.ContainsKey(tmidRow.schid))
- {
- tmpData.name = school[tmidRow.schid]["name"];
- tmpData.countryId = school[tmidRow.schid]["countryId"];
- tmpData.countryName = school[tmidRow.schid]["countryName"];
- tmpData.provinceId = school[tmidRow.schid]["provinceId"];
- tmpData.provinceName = school[tmidRow.schid]["provinceName"];
- tmpData.cityId = school[tmidRow.schid]["cityId"];
- tmpData.cityName = school[tmidRow.schid]["cityName"];
- tmpData.distId = school[tmidRow.schid]["distId"];
- tmpData.distName = school[tmidRow.schid]["distName"];
- }
- sch.Add(tmpData);
- schRow = sch.Where(s => s.id.Equals(tmidRow.schid)).FirstOrDefault();
- }
- if (tmidRow.hiteach > 0) schRow.hiteach++;
- if (tmidRow.hiteachcc > 0) schRow.hiteachcc++;
- if (tmidRow.hita > 0) schRow.hita++;
- if (tmidRow.ies5 > 0) schRow.ies5++;
- if (tmidRow.account > 0) schRow.account++;
- if (tmidRow.sokrates > 0) schRow.sokrates++;
- if (tmidRow.sokapp > 0) schRow.sokapp++;
- if (tmidRow.irs > 0) schRow.irs++;
- if (!schRow.tmids.Contains(id)) schRow.tmids.Add(id);
- }
- }
- }
- //geo
- List<TmidIotProdCntGeo> geo = new List<TmidIotProdCntGeo>();
- if (targets.Contains("geo"))
- {
- foreach (TmidIotProdCntGeo schRow in sch)
- {
- string countryId = (!string.IsNullOrWhiteSpace(schRow.countryId)) ? schRow.countryId : string.Empty;
- string provinceId = (!string.IsNullOrWhiteSpace(schRow.provinceId)) ? schRow.provinceId : string.Empty;
- string cityId = (!string.IsNullOrWhiteSpace(schRow.cityId)) ? schRow.cityId : string.Empty;
- string distId = (!string.IsNullOrWhiteSpace(schRow.distId)) ? schRow.distId : string.Empty;
- string geoid = $"{countryId}-{provinceId}-{cityId}-{distId}";
- var geoRow = geo.Where(s => s.id.Equals(geoid)).FirstOrDefault();
- if (geoRow == null)
- {
- geo.Add(new TmidIotProdCntGeo()
- {
- id = geoid,
- name = string.Empty,
- countryId = schRow.countryId,
- countryName = schRow.countryName,
- provinceId = schRow.provinceId,
- provinceName = schRow.provinceName,
- cityId = schRow.cityId,
- cityName = schRow.cityName,
- distId = schRow.distId,
- distName = schRow.distName,
- });
- geoRow = geo.Where(s => s.id.Equals(geoid)).FirstOrDefault();
- }
- geoRow.hiteach += schRow.hiteach;
- geoRow.hiteachcc += schRow.hiteachcc;
- geoRow.hita += schRow.hita;
- geoRow.ies5 += schRow.ies5;
- geoRow.account += schRow.account;
- geoRow.sokrates += schRow.sokrates;
- geoRow.sokapp += schRow.sokapp;
- geoRow.irs += schRow.irs;
- geoRow.tmids = geoRow.tmids.Union(schRow.tmids).ToList();
- }
- }
- //IOT(學校)
- string queryiot = $"SELECT * FROM c WHERE ARRAY_CONTAINS({JsonSerializer.Serialize(schIds)}, c.schoolId, true) AND c.toolType = 'HiTeach' AND c.dateUnit = 'month' AND c.dateTime >= {dateTimeFrom.ToUnixTimeSeconds()} AND c.dateTime <= {dateTimeTo.ToUnixTimeSeconds()}";
- await foreach (ProdAnalysisCosmos item in cosmosClientIes5.GetContainer("TEAMModelOS", "School").GetItemQueryIteratorSql<ProdAnalysisCosmos>(queryText: queryiot, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"ProdAnalysis") }))
- {
- //各校IOT累加
- var schRow = sch.Where(s => s.id.Equals(item.schoolId)).FirstOrDefault();
- if (schRow != null)
- {
- schRow.tLesson += item.tLesson;
- schRow.tGreen += item.tGreen;
- //地理位置IOT累加
- string countryId = (!string.IsNullOrWhiteSpace(schRow.countryId)) ? schRow.countryId : string.Empty;
- string provinceId = (!string.IsNullOrWhiteSpace(schRow.provinceId)) ? schRow.provinceId : string.Empty;
- string cityId = (!string.IsNullOrWhiteSpace(schRow.cityId)) ? schRow.cityId : string.Empty;
- string distId = (!string.IsNullOrWhiteSpace(schRow.distId)) ? schRow.distId : string.Empty;
- string geoid = $"{countryId}-{provinceId}-{cityId}-{distId}";
- var geoRow = geo.Where(s => s.id.Equals(geoid)).FirstOrDefault();
- if (geoRow != null)
- {
- geoRow.tLesson += item.tLesson;
- geoRow.tGreen += item.tGreen;
- }
- }
- }
- //總IOT累加
- Dictionary<string, int> iotResult = new Dictionary<string, int>();
- iotResult.Add("tLesson", 0);
- iotResult.Add("tGreen", 0);
- Dictionary<string, TmidStics> iotDic = await GetTmidIotCore(tmids.ToList(), dateFromStr, dateToStr, "month");
- foreach (KeyValuePair<string, TmidStics> iot in iotDic)
- {
- string tid = iot.Key;
- TmidStics iotCalData = iot.Value;
- if (iotCalData.iot.hiteach.month != null)
- {
- iotResult["tLesson"] += iotCalData.iot.hiteach.month.tLesson;
- iotResult["tGreen"] += iotCalData.iot.hiteach.month.tGreen;
- }
- }
-
- return new { dateFrom = dateFromStr, dateTo = dateToStr, tmids, tmid, sch, geo, iot = iotResult };
- }
- //取得TMID服務授權週期
- public async Task<List<object>> getTMIDAuthService(CosmosClient cosmosClientCsv2, string tmid, string prodCode, bool validFlg)
- {
- long now = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
- var qryOption = new QueryRequestOptions() { PartitionKey = new PartitionKey("servicePeriod") };
- string whereSql = $"c.saleClient.tmid = '{tmid}'";
- if (!string.IsNullOrWhiteSpace(prodCode)) whereSql += $" AND c.prodCode = '{prodCode}'";
- if (validFlg) whereSql += $" AND c.startDate <= {now} AND {now} <= c.endDate";
- string sql = $"SELECT c.id, c.prodCode, c.type, c.startDate, c.endDate, c.number, c.unit, c.aprule FROM c WHERE {whereSql}";
- var client = cosmosClientCsv2.GetContainer("Habb", "Auth");
- var result = new List<object>();
- await foreach (var item in client.GetItemQueryStreamIteratorSql(queryText: sql, requestOptions: qryOption))
- {
- var json = await JsonDocument.ParseAsync(item.Content);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
- {
- result.Add(
- new
- {
- id = obj.GetProperty("id").GetString(),
- prodCode = obj.GetProperty("prodCode").GetString(),
- type = obj.GetProperty("type").GetInt32(),
- startDate = obj.GetProperty("startDate").GetInt64(),
- endDate = obj.GetProperty("endDate").GetInt64(),
- number = obj.GetProperty("number").GetInt32(),
- aprule = obj.GetProperty("aprule")
- });
- }
- }
- }
- return result;
- }
- //取得TMID購買紀錄
- public async Task<List<Ies5OrderHis>> getTMIDAuthOrder(CosmosClient cosmosClientCsv2, string tmid, string prodCode)
- {
- SortedDictionary<string, Ies5OrderHis> OrderDic = new SortedDictionary<string, Ies5OrderHis>();
- var qryOption = new QueryRequestOptions() { PartitionKey = new PartitionKey("order") };
- //服務 ※序號、硬體 不列入購買紀錄
- string strQueryV = $"SELECT * FROM c WHERE IS_DEFINED(c.saleClient.tmid) AND c.saleClient.tmid = '{tmid}' AND c.prodType = 'service'";
- if (!string.IsNullOrWhiteSpace(prodCode))
- {
- strQueryV += $" AND c.prodCode = '{prodCode}'";
- }
- await foreach (OrderHisService orderService in cosmosClientCsv2.GetContainer("Habb", "Auth").GetItemQueryIteratorSql<OrderHisService>(queryText:strQueryV, continuationToken: null, requestOptions: qryOption))
- {
- Ies5OrderHisService ies5OrderVRow = new Ies5OrderHisService();
- ies5OrderVRow.prodCode = orderService.prodCode;
- ies5OrderVRow.type = (orderService.contract.Equals(1)) ? "C" : (orderService.contract.Equals(2)) ? "G" : "N"; //授權方式 [FROM]contract契約型態 0:新約 1:續約 2:變更 [TO]type N:新約 C:續約 G:變更
- ies5OrderVRow.sdate = orderService.startDate;
- ies5OrderVRow.edate = orderService.endDate;
- ies5OrderVRow.number = orderService.number;
- ies5OrderVRow.unit = orderService.unit;
- //加入Order字典
- string OrderID = orderService.orderinfo.orderid.ToString();
- long OrderDate = orderService.orderinfo.createDate;
- if (OrderDic.ContainsKey(OrderID))
- {
- OrderDic[OrderID].service.Add(ies5OrderVRow);
- }
- else
- {
- Ies5OrderHis ies5OrderHisNew = new Ies5OrderHis();
- ies5OrderHisNew.id = OrderID;
- ies5OrderHisNew.date = OrderDate;
- ies5OrderHisNew.service.Add(ies5OrderVRow);
- OrderDic.Add(OrderID, ies5OrderHisNew);
- }
- }
- //輸出項
- List<Ies5OrderHis> Result = new List<Ies5OrderHis>();
- foreach (KeyValuePair<string, Ies5OrderHis> item in OrderDic)
- {
- Result.Add(item.Value);
- }
- return Result;
- }
- //Tool
- //資料遮罩
- ///規則:
- ///1.手機、電話:隱藏後4碼
- ///2.Mail:隱藏@前資料
- ///3.姓名:只顯示第一個字元
- ///4.身分證、護照:隱藏後4碼
- public string GenDataMask(string data, string type)
- {
- int length = 0;
- string subString = string.Empty;
- if (!string.IsNullOrWhiteSpace(data))
- {
- switch (type)
- {
- case "mobile":
- case "id":
- length = (data.Length > 4) ? 4 : data.Length;
- subString = data.Substring(data.Length - length, length);
- data = data.Replace(subString, "****");
- break;
- case "mail":
- string[] dataList = data.Split("@");
- subString = dataList.First();
- data = data.Replace(subString, "****");
- break;
- case "name":
- length = 2;
- subString = data.Substring(data.Length - length, length);
- data = data.Replace(subString, "〇〇");
- break;
- }
- }
- return data;
- }
- private async Task<List<TmidAnalysisCal>> getTMIDIotData(CosmosClient cosmosClientIes5, List<string> tmids, string toolType, string dateUnit, int year, int month, int day, long from, long to)
- {
- List<TmidAnalysisCal> result = new List<TmidAnalysisCal>();
- List<string> calPropList = new List<string>() { "lessonRecord", "useIES", "useIES5Resource", "useWebIrs", "useDeviceIrs", "useHaboard", "useHita", "lessonLengMin", "lessonLeng0", "stuShow", "stuLessonLengMin", "tGreen", "tLesson", "lTypeCoop", "lTypeIact", "lTypeMis", "lTypeTst", "lTypeDif", "lTypeNone", "lessonCnt928", "lessonCntId", "lessonCntDevice", "lessonCntIdDevice", "mission", "missionFin", "item", "interact", "sendSok", "learnPeer", "learnCoop", "useWordCloud", "useClouDAS", "useGPT", "useIes5Test", "usePaperTest", "useExcelTest", "useScoreBoard", "learnParticipationCnt", "learnParticipationT", "coopMission", "coopWork", "coopContributionT", "peerAct", "peerStuParticipationT" }; //要計算的ProdAnalysis欄位列表
- string tmidsListStr = JsonSerializer.Serialize(tmids);
- string strQuery = $"SELECT * FROM c WHERE ARRAY_CONTAINS({tmidsListStr}, c.tmid, true) AND c.toolType = '{toolType}'";
- var qryOption = new QueryRequestOptions() { PartitionKey = new PartitionKey("TmidAnalysis") };
- if (!string.IsNullOrWhiteSpace(dateUnit)) strQuery += $" AND c.dateUnit = '{dateUnit}'";
- if (year > 0) strQuery += $" AND c.year = {year}";
- if (month > 0) strQuery += $" AND c.month = {month}";
- if (day > 0) strQuery += $" AND c.day = {day}";
- if (from > 0) strQuery += $" AND c.dateTime >= {from}";
- if (to > 0) strQuery += $" AND c.dateTime <= {to}";
- await foreach (TmidAnalysisCosmos tmidAnalysis in cosmosClientIes5.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIteratorSql<TmidAnalysisCosmos>(queryText:strQuery, continuationToken:null, requestOptions:qryOption))
- {
- //特殊修正
- tmidAnalysis.lTypeNone = ((tmidAnalysis.lTypeNone - tmidAnalysis.lessonLeng0) < 0) ? 0 : tmidAnalysis.lTypeNone - tmidAnalysis.lessonLeng0; //無學習型態數值須扣除未上課
- //一般項
- TmidAnalysisCal tmidAnalysisCalNow = result.Where(t => t.tmid.Equals(tmidAnalysis.tmid)).FirstOrDefault();
- if(tmidAnalysisCalNow == null)
- {
- TmidAnalysisCal tmpTmidAnalysisCal = new TmidAnalysisCal();
- tmpTmidAnalysisCal.tmid = tmidAnalysis.tmid;
- result.Add(tmpTmidAnalysisCal);
- tmidAnalysisCalNow = result.Where(t => t.tmid.Equals(tmidAnalysis.tmid)).FirstOrDefault();
- }
- tmidAnalysisCalNow.dateUnit = tmidAnalysis.dateUnit;
- tmidAnalysisCalNow.toolType = tmidAnalysis.toolType;
- tmidAnalysisCalNow.date = tmidAnalysis.date;
- if (tmidAnalysis.year > 0) tmidAnalysisCalNow.year = tmidAnalysis.year;
- if (tmidAnalysis.month > 0) tmidAnalysisCalNow.month = tmidAnalysis.month;
- if (tmidAnalysis.day > 0) tmidAnalysisCalNow.day = tmidAnalysis.day;
- if (tmidAnalysisCalNow.from.Equals(0) || tmidAnalysis.dateTime <= tmidAnalysisCalNow.from) tmidAnalysisCalNow.from = tmidAnalysis.dateTime;
- if (tmidAnalysisCalNow.to.Equals(0) || tmidAnalysis.dateTime >= tmidAnalysisCalNow.to) tmidAnalysisCalNow.to = tmidAnalysis.dateTime;
- tmidAnalysisCalNow.dateList.Add(tmidAnalysis.date);
- //統計項
- foreach (PropertyInfo propertyInfo in tmidAnalysis.GetType().GetProperties()) //累加項目
- {
- if (calPropList.Contains(propertyInfo.Name))
- {
- var propType = propertyInfo.PropertyType;
- var valNow = propertyInfo.GetValue(tmidAnalysisCalNow);
- var valTodo = tmidAnalysis.GetType().GetProperty(propertyInfo.Name).GetValue(tmidAnalysis);
- if (propType.Equals(typeof(long))) propertyInfo.SetValue(tmidAnalysisCalNow, Convert.ToInt64(valNow.ToString(), 10) + Convert.ToInt64(valTodo.ToString(), 10));
- else propertyInfo.SetValue(tmidAnalysisCalNow, Convert.ToInt32(valNow.ToString(), 10) + Convert.ToInt32(valTodo.ToString(), 10));
- }
- }
- tmidAnalysisCalNow.deviceList = tmidAnalysisCalNow.deviceList.Union(tmidAnalysis.deviceList).ToList();
- tmidAnalysisCalNow.deviceCnt = tmidAnalysisCalNow.deviceList.Count;
- tmidAnalysisCalNow.deviceNoAuthList = tmidAnalysisCalNow.deviceNoAuthList.Union(tmidAnalysis.deviceNoAuthList).ToList();
- tmidAnalysisCalNow.deviceNoAuth = tmidAnalysisCalNow.deviceNoAuthList.Count;
- tmidAnalysisCalNow.deviceAuthList = tmidAnalysisCalNow.deviceAuthList.Union(tmidAnalysis.deviceAuthList).ToList();
- tmidAnalysisCalNow.deviceAuth = tmidAnalysisCalNow.deviceAuthList.Count;
- tmidAnalysisCalNow.tmidList = tmidAnalysisCalNow.tmidList.Union(tmidAnalysis.tmidList).ToList();
- tmidAnalysisCalNow.tmidCnt = tmidAnalysisCalNow.tmidList.Count;
- tmidAnalysisCalNow.verList = tmidAnalysisCalNow.verList.Union(tmidAnalysis.verList).ToList();
- decimal learnParticipationTmp = (tmidAnalysisCalNow.learnParticipationCnt > 0) ? (decimal)tmidAnalysisCalNow.learnParticipationT / (decimal)tmidAnalysisCalNow.learnParticipationCnt : 0;
- tmidAnalysisCalNow.learnParticipation = Math.Round(learnParticipationTmp, 2); //學習參與度指數(平均)
- }
- return result;
- }
- //Model
- //TMID統計 基本資訊
- private class TmidStics
- {
- public string id { get; set; }
- public string name { get; set; }
- public string picture { get; set; }
- public string mobile { get; set; }
- public string mail { get; set; }
- public string country { get; set; }
- public string province { get; set; }
- public string city { get; set; }
- public string dist { get; set; }
- public string schoolCode { get; set; }
- public string schoolCodeW { get; set; }
- public string lang { get; set; }
- public string unitType { get; set; } //1:基礎教育機構(K-小學) 2:中等教育機構(國中、高中/職) 3:高等教育機構 4:政府單位機構 5:NGO機構 6:企業機構 7:其他 8:學前教育 9:特殊教育
- public string unitName { get; set; }
- public string jobTitle { get; set; }
- public TmidSticsIes5 ies5 { get; set; } //IES統計資料
- public TmidPoints points { get; set; }
- public TmidSokrates sokrates { get; set; }
- public TmidBenefits benefits { get; set; }
- public List<TmidCoupon> coupons { get; set; } = new();
- public List<TmidLoginTime> login { get; set; } = new();
- public List<object> prod { get; set; } = new();
- public TmidIot iot { get; set; }
- public bool wechat { get; set; }
- public bool facebook { get; set; }
- public bool google { get; set; }
- public bool ding { get; set; }
- public bool apple { get; set; }
- public bool educloudtw { get; set; }
- public long ts { get; set; } //資料最終變更時間
- public string schoolId { get; set; } //歸屬學校資訊
- }
- //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 List<TmidSticsIes5School> schools { get; set; } = new(); //各學校資料
- public int resCount { get; set; } //教材數
- public int itemCount { get; set; } //題目數
- public int paperCount { get; set; } //試卷數
- }
- private class TmidSticsIes5School
- {
- public string schoolId { get; set; }
- public string name { get; set; }
- public string region { get; set; }
- public string province { get; set; }
- public string city { get; set; }
- public string dist { get; set; }
- public object size { get; set; } = new();
- public int courseCnt { get; set; }
- public bool defaultSchool { get; set; }
- }
- private class TmidPoints
- {
- public int points { get; set; } = 0; //累積的點數(只加不減)
- public int balance { get; set; } = 0; //可用的點數
- public int level { get; set; } = 0; //等級
- }
- private class TmidCoupon
- {
- public string code { get; set; }
- public long exchange { get; set; }
- }
- private class TmidLoginTime
- {
- public string product { get; set; }
- public long time { get; set; }
- }
- private class TmidSokrates
- {
- public object hiteach_data { get; set; }
- public object user_channels { get; set; }
- }
- public class TmidSokratesHiteach
- {
- public TmidSokratesHiteachItem total { get; set; }
- public TmidSokratesHiteachItem this_year { get; set; }
- public TmidSokratesHiteachItem this_month { get; set; }
- public TmidSokratesHiteachItem this_week { get; set; }
- }
- public class TmidSokratesHiteachItem
- {
- public string start_date { get; set; } = "";
- public string end_date { get; set; } = "";
- public string t_data { get; set; } = "0";
- public string duration { get; set; } = "0";
- public string attendance { get; set; } = "0";
- public string interaction { get; set; } = "0";
- public string t_green { get; set; } = "0";
- public string learning_duration { get; set; } = "0";
- public string all_total { get; set; } = "0";
- public string double_green_light { get; set; } = "0";
- public string public_count { get; set; } = "0";
- public string material_count { get; set; } = "0";
- public string personal_comment { get; set; } = "0";
- public string comment_count { get; set; } = "0";
- public string watchHistory { get; set; } = "0";
- public string sokrates_summary { get; set; } = "0";
- }
- private class TmidBenefits
- {
- public List<object> hiteach { get; set; }
- public List<object> hiteachcc { get; set; }
- }
- private class TmidIot
- {
- public TmidIotYMD hiteach { get; set; } = new();
- public TmidIotYMD hiteachcc { get; set; } = new();
- }
- private class TmidIotYMD
- {
- public TmidAnalysisCal year { get; set; }
- public TmidAnalysisCal month { get; set; }
- public TmidAnalysisCal day { get; set; }
- }
- private class TmidIotSchool
- {
- public string id { get; set; }
- public string name { get; set; }
- public string country { get; set; }
- public string province { get; set; }
- public string city { get; set; }
- public string dist { get; set; }
- }
- private class TmidIotProdCnt
- {
- public string id { get; set; }
- public string name { get; set; }
- public string schid { get; set; }
- public int hiteach { get; set; }
- public int hiteachcc { get; set; }
- public int hita { get; set; }
- public int ies5 { get; set; }
- public int account { get; set; }
- public int sokrates { get; set; }
- public int sokapp { get; set; }
- public int irs { get; set; }
- public int tLesson { get; set; }
- public int tGreen { get; set; }
- }
- private class TmidIotProdCntGeo : TmidIotProdCnt
- {
- public string countryId { get; set; }
- public string countryName { get; set; }
- public string provinceId { get; set; }
- public string provinceName { get; set; }
- public string cityName { get; set; }
- public string cityId { get; set; }
- public string distId { get; set; }
- public string distName { get; set; }
- public List<string> tmids { get; set; } = new();
- }
- //[API輸出] 訂單履歷
- public class Ies5OrderHis
- {
- public Ies5OrderHis()
- {
- service = new List<Ies5OrderHisService>();
- }
- public string id { get; set; }
- public long date { get; set; }
- public List<Ies5OrderHisService> service { get; set; } = new ();
- }
- //[API輸出] 訂單履歷.服務型產品
- public class Ies5OrderHisService
- {
- public string prodCode { get; set; } //產品代碼
- public string type { get; set; } //授權方式 N:新約 C:續約
- public long sdate { get; set; } //授權起始時間
- public long edate { get; set; } //授權終止時間
- public int number { get; set; } //購買數量
- public string unit { get; set; } //購買單位 無單位者為null
- }
- //[承接DB] DB: Habb.Auth
- //[承接DB] 訂單履歷基本Class
- public class OrderHisBase
- {
- public string id { get; set; } //OPID
- public OrderHisOrderInfo orderinfo { get; set; } //訂單資訊
- public string prodCode { get; set; } //產品代碼
- public SerialSaleClient saleClient { get; set; } = null; //銷售終端
- public string prodType { get; set; } //產品類型 serial:序號 service:服務 hard:硬體
- public string dataType { get; set; } //資料類型
- public long operationTime { get; set; } //最新資料變更時間戳記
- public int ttl { get; set; } = -1; //過期刪除秒數
- }
- //[承接DB] 訂單履歷基本Class.銷售終端
- public class SerialSaleClient
- {
- public string name { get; set; } //[String]銷售終端姓名
- public string schoolCode { get; set; } = null; //[String]銷售終端學校代碼
- public string clientId { get; set; } = null; //[String]銷售終端客戶ID
- public string tmid { get; set; } = null; //[String]TMID
- public string type { get; set; } //[String]銷售終端資料類型 school:學校 client:經銷商客戶
- public string countryId { get; set; } //[String]國家代碼
- public string provinceId { get; set; } //[String]省代碼
- public string cityId { get; set; } //[String]市代碼
- public string schoolShortCode { get; set; } //[String]學校簡碼
- public string districtId { get; set; } //[String]區代碼
- public string dataCenter { get; set; } //[String]數據中心
- }
- //[承接DB] 訂單履歷基本Class.訂單資訊
- public class OrderHisOrderInfo
- {
- public string orderid { get; set; } //[String]訂單編號
- public int orderAudit { get; set; } //[Int]訂單審核狀態 0:待審, 1:通過, 2:否決, 3:問題
- public int orderProperty { get; set; } //[Int]訂單類型 0:銷售,1:展示申請 2:內部申請
- public long createDate { get; set; } //訂單創建時間
- }
- //[承接DB] 訂單履歷 服務類
- public class OrderHisService : OrderHisBase
- {
- public int type { get; set; } //[Int]授權類型 0:銷售 1:試用
- public int contract { get; set; } //[Int]契約型態 0:新約 1:續約
- public long startDate { get; set; } //[long]授權起始日期 Timestamp(UTC)
- public long endDate { get; set; } //[long]授權終止日期 Timestamp(UTC)
- public int number { get; set; } //[Int]數量
- public string unit { get; set; } //[String]單位 (無者為空)
- }
- //雲端服務ClientId列表
- private readonly Dictionary<string, string> _dicClientIds = new()
- {
- { "917d02f2-5b91-404e-a71d-7bdd926ddd81", "HiTeach" },
- { "c5328340-b8eb-4489-8650-8c8862d7acfe", "HiTeach" },
- { "118d2d4d-32a3-44c0-808a-792ca73d3706", "HiTeachCC" },
- { "227082f4-8db0-4517-b281-93dba9428bc7", "HiTeachCC" },
- { "371a99aa-7efd-4c3a-90a8-95b7db4f42c0", "HiTA" },
- { "0ed38cde-e7fe-4fa6-9eaa-33ad404eb532", "HiTA" },
- { "531fecd1-b1a5-469a-93ca-7984e1d392f2", "IES5" },
- { "c7317f88-7cea-4e48-ac57-a16071f7b884", "IES5" },
- { "c8de822b-3fcf-4831-adba-cdd14653bf7f", "Account" },
- { "516148eb-6a38-4657-ba98-a3699061937f", "Account" },
- { "d7193896-9b12-4046-ad44-c991dd48cc39", "Sokrates" },
- { "59009e38-6e30-4116-814b-7605939edc47", "Sokrates" },
- { "626c7285-15aa-4abe-8c0d-2c5ff1381538", "SokAPP" },
- { "5bcc16a5-7d20-4cc4-b5c2-8ed0416f32b6", "SokAPP" },
- { "044482b5-d024-4f23-9b55-906884243405", "IRS" },
- { "5e27b7e3-b36c-4ce9-b838-e94fd0cea080", "IRS" }
- };
- //TMID IOT date
- public class tmidIotDate
- {
- public string dateUnit { get; set; } //[string]日期單位:年月日 year, month, day
- public int year { get; set; } //[Int]年
- public int month { get; set; } //[Int]月
- public int day { get; set; } //[Int]日
- public long from { get; set; } //[long]UTC timestamp 起始日
- public long to { get; set; } //[long]UTC timestamp 終止日
- }
- public class TmidAnalysisCal : TmidAnalysisCosmos
- {
- public long from { get; set; } //[long]UTC timestamp 起始日
- public long to { get; set; } //[long]UTC timestamp 終止日
- public List<string> dateList { get; set; } = new();
- }
- private class DateFromTo
- {
- public string dateFrom { get; set; }
- public string dateTo { get; set; }
- }
- }
- }
|