TmidController.cs 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. using Azure.Cosmos;
  2. using DocumentFormat.OpenXml.Office2010.Excel;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Azure.Cosmos.Table;
  5. using Microsoft.Extensions.Configuration;
  6. using Microsoft.Extensions.Options;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Reflection;
  11. using System.Text.Json;
  12. using System.Threading.Tasks;
  13. using TEAMModelOS.Models;
  14. using TEAMModelOS.SDK;
  15. using TEAMModelOS.SDK.DI;
  16. using TEAMModelOS.SDK.Extension;
  17. using TEAMModelOS.SDK.Models;
  18. using TEAMModelOS.SDK.Services;
  19. using static TEAMModelBI.Models.Extension.GeoRegion;
  20. namespace TEAMModelBI.Controllers.BITmid
  21. {
  22. [Route("tmid")]
  23. [ApiController]
  24. public class TmidController : ControllerBase
  25. {
  26. private readonly AzureCosmosFactory _azureCosmos;
  27. private readonly AzureStorageFactory _azureStorage;
  28. private readonly AzureRedisFactory _azureRedis;
  29. private readonly DingDing _dingDing;
  30. private readonly Option _option;
  31. private readonly IConfiguration _configuration;
  32. private readonly HttpTrigger _httpTrigger;
  33. public TmidController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, DingDing dingDing, IOptionsSnapshot<Option> option, IConfiguration configuration, HttpTrigger httpTrigger)
  34. {
  35. _azureCosmos = azureCosmos;
  36. _azureStorage = azureStorage;
  37. _azureRedis = azureRedis;
  38. _dingDing = dingDing;
  39. _option = option?.Value;
  40. _configuration = configuration;
  41. _httpTrigger = httpTrigger;
  42. }
  43. /// <summary>
  44. /// 取得TMID綜合資料
  45. /// </summary>
  46. /// <param name="jsonElement"></param>
  47. /// <returns></returns>
  48. [ProducesDefaultResponseType]
  49. //[AuthToken(Roles = "admin")]
  50. [HttpPost("get-tmidstics")]
  51. public async Task<IActionResult> GetTmidStics(JsonElement jsonElement)
  52. {
  53. try
  54. {
  55. if (!jsonElement.TryGetProperty("tmids", out JsonElement tmidsJobj)) return BadRequest();//TMID(array)
  56. var tmids = tmidsJobj.Deserialize<List<string>>();
  57. if (tmids.Count is 0) return BadRequest();
  58. var datetime = DateTimeOffset.UtcNow.AddDays(-1);
  59. int y = datetime.Year;
  60. int m = datetime.Month;
  61. int d = datetime.Day;
  62. string dateFromStr = (jsonElement.TryGetProperty("dateFrom", out JsonElement dateFromJobj)) ? dateFromJobj.GetString() : string.Empty; //查詢日期:起始(string)[例]2023-03-05
  63. string dateToStr = (jsonElement.TryGetProperty("dateTo", out JsonElement dateToJobj)) ? dateToJobj.GetString() : string.Empty; //查詢日期:結束(string)[例]2023-03-27
  64. string dateUnit = (jsonElement.TryGetProperty("dateUnit", out JsonElement dateUnitJobj)) ? dateUnitJobj.GetString().ToLower() : "month";
  65. string mode = (jsonElement.TryGetProperty("mode", out JsonElement modeJobj)) ? modeJobj.GetString().ToLower() : "default";
  66. //起始終止日期換算
  67. long dateTimeFromSec = 0;
  68. long dateTimeToSec = 0;
  69. if(!string.IsNullOrWhiteSpace(dateFromStr) && !string.IsNullOrWhiteSpace(dateToStr))
  70. {
  71. DateTimeOffset dateTimeFrom;
  72. DateTimeOffset dateTimeTo;
  73. List<string> dateFromList = dateFromStr.Split('-').ToList();
  74. int dateFromYear = Convert.ToInt32(dateFromList[0], 10);
  75. int dateFromMonth = (dateUnit.Equals("day") || dateUnit.Equals("month")) ? Convert.ToInt32(dateFromList[1], 10) : 1;
  76. int dateFromDay = (dateUnit.Equals("day")) ? Convert.ToInt32(dateFromList[2], 10) : 1;
  77. List<string> dateToList = dateToStr.Split('-').ToList();
  78. int dateToYear = Convert.ToInt32(dateToList[0], 10);
  79. int dateToMonth = (dateUnit.Equals("day") || dateUnit.Equals("month")) ? Convert.ToInt32(dateToList[1], 10) : 1;
  80. int dateToDay = (dateUnit.Equals("day")) ? Convert.ToInt32(dateToList[2], 10) : 1;
  81. switch (dateUnit)
  82. {
  83. case "year":
  84. dateTimeFrom = new DateTimeOffset(dateFromYear, 1, 1, 0, 0, 0, TimeSpan.Zero);
  85. dateTimeTo = new DateTimeOffset(dateToYear, 12, 31, 23, 59, 59, TimeSpan.Zero);
  86. break;
  87. case "month":
  88. dateTimeFrom = new DateTimeOffset(dateFromYear, dateFromMonth, 1, 0, 0, 0, TimeSpan.Zero);
  89. dateTimeTo = new DateTimeOffset(dateToYear, dateToMonth, DateTime.DaysInMonth(dateToYear, dateToMonth), 23, 59, 59, TimeSpan.Zero);
  90. break;
  91. case "day":
  92. dateTimeFrom = new DateTimeOffset(dateFromYear, dateFromMonth, dateFromDay, 0, 0, 0, TimeSpan.Zero);
  93. dateTimeTo = new DateTimeOffset(dateToYear, dateToMonth, dateToDay, 23, 59, 59, TimeSpan.Zero);
  94. break;
  95. default:
  96. dateTimeFrom = new DateTimeOffset(y, m, d, 0, 0, 0, TimeSpan.Zero);
  97. dateTimeTo = new DateTimeOffset(y, m, d, 23, 59, 59, TimeSpan.Zero);
  98. break;
  99. }
  100. dateTimeFromSec = dateTimeFrom.ToUnixTimeSeconds();
  101. dateTimeToSec = dateTimeTo.ToUnixTimeSeconds();
  102. y = 0;
  103. m = 0;
  104. d = 0;
  105. }
  106. //取得項目列表
  107. List<string> eventList = new List<string>();
  108. if(mode.Equals("simple"))
  109. {
  110. eventList = new List<string>() { "points" };
  111. } else
  112. {
  113. eventList = new List<string>() { "coupons", "login", "prod", "points", "iot", "ies5", "benefits", "sokrates" };
  114. }
  115. //服務Client端
  116. var cosmosClientIes5 = _azureCosmos.GetCosmosClient(); //CosmosDB IES5
  117. var cosmosClientCsv2 = _azureCosmos.GetCosmosClient(name: "CoreServiceV2"); //CosmosDB CSV2
  118. var storageClientCsv2 = _azureStorage.GetCloudTableClient(name: "CoreServiceV2"); //Storage CSV2
  119. var tableCouponClient = storageClientCsv2.GetTableReference("Coupon");
  120. var tablePointsClient = storageClientCsv2.GetTableReference("Points");
  121. var redisClient = _azureRedis.GetRedisClient(4);
  122. //取得TMID基本資料
  123. Dictionary<string, TmidStics> tmidDic = new();
  124. QueryDefinition query =
  125. 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))")
  126. .WithParameter("@key", tmids);
  127. await foreach (var item in cosmosClientCsv2
  128. .GetContainer("Core", "ID2")
  129. .GetItemQueryStreamIterator(query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("base") }))
  130. {
  131. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  132. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  133. {
  134. foreach (var doc in json.RootElement.GetProperty("Documents").EnumerateArray())
  135. {
  136. string id = doc.GetProperty("id").GetString();
  137. if(!tmids.Contains(id)) tmids.Add(id);
  138. //基本資料
  139. TmidStics tmidStics = (tmidDic.ContainsKey(id)) ? tmidDic[id] : new() { id = id };
  140. if (eventList.Contains("ies5")) tmidStics.ies5 = new();
  141. if (eventList.Contains("points")) tmidStics.points = new();
  142. if (eventList.Contains("sokrates")) tmidStics.sokrates = new();
  143. if (eventList.Contains("benefits")) tmidStics.benefits = new();
  144. if (eventList.Contains("iot")) tmidStics.iot = new();
  145. tmidStics.name = doc.GetProperty("name").GetString();
  146. tmidStics.picture = doc.GetProperty("picture").GetString();
  147. tmidStics.mobile = GenDataMask(doc.GetProperty("mobile").GetString(), "mobile");
  148. tmidStics.mail = GenDataMask(doc.GetProperty("mail").GetString(), "mail");
  149. tmidStics.lang = (doc.TryGetProperty("lang", out JsonElement lang)) ? lang.GetString() : string.Empty;
  150. tmidStics.wechat = (doc.TryGetProperty("wechat", out JsonElement wechat) && !string.IsNullOrWhiteSpace(wechat.GetString())) ? true : false;
  151. tmidStics.facebook = (doc.TryGetProperty("facebook", out JsonElement facebook) && !string.IsNullOrWhiteSpace(facebook.GetString())) ? true : false;
  152. tmidStics.google = (doc.TryGetProperty("google", out JsonElement google) && !string.IsNullOrWhiteSpace(google.GetString())) ? true : false;
  153. tmidStics.ding = (doc.TryGetProperty("ding", out JsonElement ding) && !string.IsNullOrWhiteSpace(ding.GetString())) ? true : false;
  154. tmidStics.apple = (doc.TryGetProperty("apple", out JsonElement apple) && !string.IsNullOrWhiteSpace(apple.GetString())) ? true : false;
  155. tmidStics.ts = (doc.TryGetProperty("ts", out JsonElement ts)) ? ts.GetInt64() : 0;
  156. tmidDic.Add(id, tmidStics);
  157. }
  158. }
  159. }
  160. //取得TMID進階資料
  161. regiondata regionData = GetRegionData(); //取得國省市區地理資訊架構
  162. 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))")
  163. .WithParameter("@key", tmids);
  164. await foreach (var item in cosmosClientCsv2
  165. .GetContainer("Core", "ID2")
  166. .GetItemQueryStreamIterator(query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("base-ex") }))
  167. {
  168. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  169. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  170. {
  171. foreach (var doc in json.RootElement.GetProperty("Documents").EnumerateArray())
  172. {
  173. string id = doc.GetProperty("id").GetString();
  174. TmidStics tmidStics = (tmidDic.ContainsKey(id)) ? tmidDic[id] : new() { id = id };
  175. if (eventList.Contains("ies5")) tmidStics.ies5 = new();
  176. if (eventList.Contains("points")) tmidStics.points = new();
  177. if (eventList.Contains("sokrates")) tmidStics.sokrates = new();
  178. if (eventList.Contains("benefits")) tmidStics.benefits = new();
  179. if (eventList.Contains("iot")) tmidStics.iot = new();
  180. if (string.IsNullOrWhiteSpace(tmidStics.name)) tmidStics.name = doc.GetProperty("name").GetString();
  181. if (string.IsNullOrWhiteSpace(tmidStics.mobile)) tmidStics.mobile = GenDataMask(doc.GetProperty("mobile").GetString(), "mobile");
  182. if (string.IsNullOrWhiteSpace(tmidStics.mail)) tmidStics.mail = GenDataMask(doc.GetProperty("mail").GetString(), "mail");
  183. string country = doc.GetProperty("country").GetString();
  184. string province = doc.GetProperty("province").GetString();
  185. string city = doc.GetProperty("city").GetString();
  186. string district = string.Empty;
  187. if (!string.IsNullOrWhiteSpace(country) && country.Equals("TW"))
  188. {
  189. district = city;
  190. city = province;
  191. province = string.Empty;
  192. }
  193. tmidStics.country = (!string.IsNullOrWhiteSpace(country) && regionData.country.ContainsKey(country)) ? regionData.country[country].name.Replace("地區", "").Replace("地区", "") : country;
  194. 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;
  195. if (!string.IsNullOrWhiteSpace(country) && country.Equals("TW"))
  196. tmidStics.city = (regionData.city.ContainsKey(country) && !string.IsNullOrWhiteSpace(city) && regionData.city[country]["tw"].ContainsKey(city)) ? regionData.city[country]["tw"][city].name : city;
  197. else if (!string.IsNullOrWhiteSpace(country) && country.Equals("CN"))
  198. 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;
  199. 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;
  200. tmidStics.schoolCode = (doc.TryGetProperty("schoolCode", out JsonElement schCode)) ? schCode.GetString() : string.Empty;
  201. tmidStics.schoolCodeW = (doc.TryGetProperty("schoolCodeW", out JsonElement schCodeW)) ? schCodeW.GetString() : string.Empty;
  202. tmidStics.unitType = (doc.TryGetProperty("unitType", out JsonElement unitType)) ? unitType.GetString() : string.Empty;
  203. tmidStics.unitName = (doc.TryGetProperty("unitName", out JsonElement unitName)) ? unitName.GetString() : string.Empty;
  204. tmidStics.jobTitle = (doc.TryGetProperty("jobTitle", out JsonElement jobTitle)) ? jobTitle.GetString() : string.Empty;
  205. }
  206. }
  207. }
  208. if(tmidDic.Count.Equals(0)) return Ok(new List<object>());
  209. //取得 票券、登入各服務的時間、取得積分、個人服務授權、IOT
  210. foreach (KeyValuePair<string, TmidStics> dicItem in tmidDic)
  211. {
  212. string id = dicItem.Key;
  213. TmidStics tmidStics = dicItem.Value;
  214. //票券
  215. if (eventList.Contains("coupons"))
  216. {
  217. var usersCoupons = tableCouponClient.Get<DynamicTableEntity>(id);
  218. foreach (var coupon in usersCoupons)
  219. {
  220. tmidStics.coupons.Add(
  221. new TmidCoupon()
  222. {
  223. code = coupon.RowKey,
  224. exchange = coupon.Properties["exchangeTime"].DateTimeOffsetValue.Value.ToUnixTimeSeconds(),
  225. });
  226. }
  227. }
  228. //取得登入各服務的時間
  229. if (eventList.Contains("login"))
  230. {
  231. var loginTime = await redisClient.HashGetAllAsync(id);
  232. foreach (var t in loginTime)
  233. {
  234. if (!t.Name.StartsWith("HiTeach") && !_dicClientIds.ContainsKey(t.Name)) continue;
  235. tmidStics.login.Add(
  236. new TmidLoginTime()
  237. {
  238. product = t.Name.StartsWith("HiTeach") ? t.Name.ToString().Split("-")[0] : _dicClientIds[t.Name],
  239. time = (long)t.Value
  240. });
  241. }
  242. }
  243. //個人服務授權
  244. if (eventList.Contains("prod"))
  245. {
  246. tmidStics.prod = await getTMIDAuthService(cosmosClientCsv2, id, "", true);
  247. }
  248. //IOT
  249. if (eventList.Contains("iot"))
  250. {
  251. tmidStics.iot.hiteach.year = null;
  252. tmidStics.iot.hiteach.month = null;
  253. tmidStics.iot.hiteach.day = null;
  254. if (!string.IsNullOrWhiteSpace(dateFromStr) && !string.IsNullOrWhiteSpace(dateToStr) && !string.IsNullOrWhiteSpace(dateUnit))
  255. {
  256. if (dateUnit.ToLower().Equals("year"))
  257. {
  258. tmidStics.iot.hiteach.year = await getTMIDIotData(cosmosClientIes5, id, "HiTeach", "year", y, 0, 0, dateTimeFromSec, dateTimeToSec);
  259. tmidStics.iot.hiteachcc.year = await getTMIDIotData(cosmosClientIes5, id, "HiTeachCC", "year", y, 0, 0, dateTimeFromSec, dateTimeToSec);
  260. }
  261. else if (dateUnit.ToLower().Equals("month"))
  262. {
  263. tmidStics.iot.hiteach.month = await getTMIDIotData(cosmosClientIes5, id, "HiTeach", "month", y, m, 0, dateTimeFromSec, dateTimeToSec);
  264. tmidStics.iot.hiteachcc.month = await getTMIDIotData(cosmosClientIes5, id, "HiTeachCC", "month", y, m, 0, dateTimeFromSec, dateTimeToSec);
  265. }
  266. else if (dateUnit.ToLower().Equals("day"))
  267. {
  268. tmidStics.iot.hiteach.day = await getTMIDIotData(cosmosClientIes5, id, "HiTeach", "day", y, m, d, dateTimeFromSec, dateTimeToSec);
  269. tmidStics.iot.hiteachcc.day = await getTMIDIotData(cosmosClientIes5, id, "HiTeachCC", "day", y, m, d, dateTimeFromSec, dateTimeToSec);
  270. }
  271. }
  272. else
  273. {
  274. tmidStics.iot.hiteach.year = await getTMIDIotData(cosmosClientIes5, id, "HiTeach", "year", y, 0, 0, dateTimeFromSec, dateTimeToSec);
  275. tmidStics.iot.hiteach.month = await getTMIDIotData(cosmosClientIes5, id, "HiTeach", "month", y, m, 0, dateTimeFromSec, dateTimeToSec);
  276. tmidStics.iot.hiteach.day = await getTMIDIotData(cosmosClientIes5, id, "HiTeach", "day", y, m, d, dateTimeFromSec, dateTimeToSec);
  277. tmidStics.iot.hiteachcc.year = await getTMIDIotData(cosmosClientIes5, id, "HiTeachCC", "year", y, 0, 0, dateTimeFromSec, dateTimeToSec);
  278. tmidStics.iot.hiteachcc.month = await getTMIDIotData(cosmosClientIes5, id, "HiTeachCC", "month", y, m, 0, dateTimeFromSec, dateTimeToSec);
  279. tmidStics.iot.hiteachcc.day = await getTMIDIotData(cosmosClientIes5, id, "HiTeachCC", "day", y, m, d, dateTimeFromSec, dateTimeToSec);
  280. }
  281. }
  282. }
  283. //積分
  284. if (eventList.Contains("points"))
  285. {
  286. List<string> ids = tmidDic.Keys.ToList();
  287. //防止Query過長,分次取得
  288. Dictionary<int, List<string>> idsDic = new Dictionary<int, List<string>>();
  289. int idCount = 0;
  290. int max = 100; //要分割成多少人一組
  291. foreach (string id in ids)
  292. {
  293. int indx = idCount / max;
  294. if (!idsDic.ContainsKey(indx)) idsDic.Add(indx, new List<string>());
  295. idsDic[indx].Add(id);
  296. idCount++;
  297. }
  298. foreach(var itemd in idsDic)
  299. {
  300. string filter = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "Points");
  301. string rkFilterCombine = string.Empty;
  302. foreach (string rowKey in itemd.Value)
  303. {
  304. string rkFilter = TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, rowKey);
  305. if (string.IsNullOrWhiteSpace(rkFilterCombine))
  306. {
  307. rkFilterCombine = rkFilter;
  308. }
  309. else
  310. {
  311. rkFilterCombine = TableQuery.CombineFilters(rkFilterCombine, TableOperators.Or, rkFilter);
  312. }
  313. }
  314. filter = TableQuery.CombineFilters(filter, TableOperators.And, rkFilterCombine);
  315. TableQuery tableQuery = new TableQuery().Where(filter);
  316. var usersPoints = tablePointsClient.ExecuteQuery(tableQuery);
  317. if (usersPoints.Any())
  318. {
  319. foreach (DynamicTableEntity item in usersPoints)
  320. {
  321. string id = item.RowKey.ToString();
  322. TmidStics tmidStics = tmidDic[id];
  323. tmidStics.points.points = (usersPoints != null) ? item.Properties["Points"].Int32Value.Value : 0;
  324. tmidStics.points.level = (usersPoints != null) ? item.Properties["Level"].Int32Value.Value : 0;
  325. tmidStics.points.balance = (usersPoints != null) ? item.Properties["Balance"].Int32Value.Value : 0;
  326. }
  327. }
  328. }
  329. }
  330. //IES5
  331. if (eventList.Contains("ies5"))
  332. {
  333. query = new QueryDefinition(@"SELECT c.id, c.defaultSchool, c.schools FROM c WHERE ARRAY_CONTAINS(@key, c.id)")
  334. .WithParameter("@key", tmidDic.Keys.ToList());
  335. await foreach (var item in cosmosClientIes5
  336. .GetContainer(Constant.TEAMModelOS, "Teacher")
  337. .GetItemQueryStreamIterator(query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  338. {
  339. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  340. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  341. {
  342. foreach (var doc in json.RootElement.GetProperty("Documents").EnumerateArray())
  343. {
  344. string id = doc.GetProperty("id").GetString();
  345. TmidStics tmidStics = tmidDic[id];
  346. //IES5學校資訊
  347. string defaultschool = (doc.TryGetProperty("defaultSchool", out JsonElement _defaultSchool)) ? _defaultSchool.ToString() : string.Empty; //預設學校
  348. List<string> schoolIds = new List<string>();
  349. if (doc.TryGetProperty("schools", out JsonElement schoolsJobj))
  350. {
  351. foreach (var obj in schoolsJobj.EnumerateArray())
  352. {
  353. string schoolCodeNow = $"{obj.GetProperty("schoolId")}";
  354. string status = $"{obj.GetProperty("status")}";
  355. if (status.Equals("join"))
  356. {
  357. schoolIds.Add(schoolCodeNow); //放入學校ID列,一次取
  358. }
  359. }
  360. }
  361. if (schoolIds.Count > 0)
  362. {
  363. 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)")
  364. .WithParameter("@key", schoolIds);
  365. await foreach (var itemsc in cosmosClientIes5
  366. .GetContainer(Constant.TEAMModelOS, "School")
  367. .GetItemQueryStreamIterator(querysc, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  368. {
  369. using var jsonsc = await JsonDocument.ParseAsync(itemsc.ContentStream);
  370. if (jsonsc.RootElement.TryGetProperty("_count", out JsonElement countsc) && countsc.GetUInt16() > 0)
  371. {
  372. foreach (var school in jsonsc.RootElement.GetProperty("Documents").EnumerateArray())
  373. {
  374. string schoolCodeNow = school.GetProperty("id").GetString();
  375. //當前學年 ※若有多學段,取第一個學段來取得學年資訊吧
  376. Period period = school.GetProperty("period").Deserialize<List<Period>>().First();
  377. var info = SchoolService.GetSemester(period, 0, DateTime.Now.ToString());
  378. int studyYear = info.studyYear;
  379. //學校Blob使用狀況
  380. (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);
  381. //老師在此學校的課程數 ※只取當前學年
  382. int courseCnt = 0;
  383. 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} )";
  384. await foreach (int itemCrs in cosmosClientIes5.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<int>(queryText: queryCrs, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"CourseTask-{schoolCodeNow}") }))
  385. {
  386. courseCnt = itemCrs;
  387. }
  388. //IES5學校資料製作
  389. TmidSticsIes5School schoolobj = new TmidSticsIes5School();
  390. long ssize = (school.TryGetProperty("size", out JsonElement ssizeJson)) ? ssizeJson.GetInt32() : 0;
  391. long sused = schoolUsedBlob.usedSize + schoolUsedBlob.teach * 1073741824;
  392. long stotal = ssize * 1073741824;
  393. object schzize = new
  394. {
  395. used = sused,
  396. total = stotal,
  397. avaliable = stotal - sused,
  398. };
  399. schoolobj.schoolId = schoolCodeNow;
  400. schoolobj.name = school.GetProperty("name").GetString();
  401. schoolobj.region = school.GetProperty("region").GetString();
  402. schoolobj.province = school.GetProperty("province").GetString();
  403. schoolobj.city = school.GetProperty("city").GetString();
  404. schoolobj.dist = school.GetProperty("dist").GetString();
  405. schoolobj.size = schzize;
  406. schoolobj.courseCnt = courseCnt;
  407. schoolobj.defaultSchool = (!string.IsNullOrWhiteSpace(defaultschool) && defaultschool.Equals(schoolCodeNow)) ? true : false;
  408. tmidStics.ies5.schools.Add(schoolobj);
  409. }
  410. }
  411. }
  412. }
  413. //IES5個人空間
  414. var (usedSize, teach, total, surplus, catalog) = await BlobService.GetSurplusSpace(id, "private", _option.Location, _azureCosmos, _azureRedis, _azureStorage, _dingDing, _httpTrigger);
  415. tmidStics.ies5.usedSize = usedSize;
  416. tmidStics.ies5.teachSize = teach * 1073741824;
  417. tmidStics.ies5.totalSize = total * 1073741824;
  418. tmidStics.ies5.surplusSize = surplus;
  419. //IES5個人資源數
  420. tmidStics.ies5.resCount = 0; //教材數
  421. tmidStics.ies5.itemCount = 0; //題目數
  422. tmidStics.ies5.paperCount = 0; //試卷數
  423. ///IES5個人題目數
  424. await foreach (int itemC in cosmosClientIes5.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<int>(queryText: $"SELECT VALUE COUNT(1) FROM c WHERE c.pid = null", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-{id}") }))
  425. {
  426. tmidStics.ies5.itemCount = itemC;
  427. }
  428. ///IES5個人試卷數
  429. await foreach (int paperC in cosmosClientIes5.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<int>(queryText: $"SELECT VALUE COUNT(1) FROM c WHERE c.scope = 'private'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Paper-{id}") }))
  430. {
  431. tmidStics.ies5.paperCount = paperC;
  432. }
  433. ///IES5個人教材數
  434. List<BlobService.BlobCount> blogCountList = await BlobService.BloblogCount(cosmosClientIes5, "private", id, "", "");
  435. foreach (BlobService.BlobCount blobCountRow in blogCountList)
  436. {
  437. switch (blobCountRow.type)
  438. {
  439. case "doc":
  440. case "image":
  441. case "res":
  442. case "video":
  443. case "audio":
  444. case "other":
  445. tmidStics.ies5.resCount += blobCountRow.count;
  446. break;
  447. }
  448. }
  449. if (!tmidDic.ContainsKey(id)) tmidDic.Add(id, tmidStics);
  450. }
  451. }
  452. }
  453. }
  454. //個人權益
  455. if (eventList.Contains("benefits"))
  456. {
  457. query = new QueryDefinition(@"SELECT * FROM c WHERE (ARRAY_CONTAINS(@key, c.id))")
  458. .WithParameter("@key", tmidDic.Keys.ToList());
  459. await foreach (var item in cosmosClientCsv2
  460. .GetContainer("Core", "ID2")
  461. .GetItemQueryStreamIterator(query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("benefits") }))
  462. {
  463. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  464. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  465. {
  466. foreach (var doc in json.RootElement.GetProperty("Documents").EnumerateArray())
  467. {
  468. string id = doc.GetProperty("id").GetString();
  469. if (doc.TryGetProperty("hiteach", out var elementHiteachData))
  470. {
  471. tmidDic[id].benefits.hiteach = elementHiteachData.ToObject<List<object>>();
  472. }
  473. if (doc.TryGetProperty("hiteachcc", out var elementHiteachCCData))
  474. {
  475. tmidDic[id].benefits.hiteachcc = elementHiteachCCData.ToObject<List<object>>();
  476. }
  477. }
  478. }
  479. }
  480. }
  481. //蘇格拉底資料
  482. if (eventList.Contains("sokrates"))
  483. {
  484. query = new QueryDefinition(@"SELECT * FROM c WHERE (ARRAY_CONTAINS(@key, c.id))")
  485. .WithParameter("@key", tmidDic.Keys.ToList());
  486. await foreach (var item in cosmosClientCsv2
  487. .GetContainer("Core", "ID2")
  488. .GetItemQueryStreamIterator(query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("sokrates") }))
  489. {
  490. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  491. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  492. {
  493. foreach (var doc in json.RootElement.GetProperty("Documents").EnumerateArray())
  494. {
  495. string id = doc.GetProperty("id").GetString();
  496. if (doc.TryGetProperty("hiteach_data", out var elementHiteachData))
  497. {
  498. tmidDic[id].sokrates.hiteach_data = elementHiteachData.ToObject<object>();
  499. }
  500. if (doc.TryGetProperty("user_channels", out var elementUserChannels))
  501. {
  502. tmidDic[id].sokrates.user_channels = elementUserChannels.ToObject<object>();
  503. }
  504. }
  505. }
  506. }
  507. }
  508. //輸出
  509. List<object> data = new();
  510. foreach (KeyValuePair<string, TmidStics> dicItem in tmidDic)
  511. {
  512. data.Add(dicItem.Value);
  513. }
  514. return Ok(data);
  515. }
  516. catch (Exception ex)
  517. {
  518. //await _dingDing.SendBotMsg($"BI,{_option.Location} /tmid/get-tmidstics \n {ex.Message}\n{ex.StackTrace}", GroupNames.台北開發測試群組);
  519. return BadRequest();
  520. }
  521. }
  522. //取得TMID服務授權週期
  523. public async Task<List<object>> getTMIDAuthService(CosmosClient cosmosClientCsv2, string tmid, string prodCode, bool validFlg)
  524. {
  525. long now = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
  526. var qryOption = new QueryRequestOptions() { PartitionKey = new PartitionKey("servicePeriod") };
  527. string whereSql = $"c.saleClient.tmid = '{tmid}'";
  528. if (!string.IsNullOrWhiteSpace(prodCode)) whereSql += $" AND c.prodCode = '{prodCode}'";
  529. if (validFlg) whereSql += $" AND c.startDate <= {now} AND {now} <= c.endDate";
  530. string sql = $"SELECT c.id, c.prodCode, c.type, c.startDate, c.endDate, c.number, c.unit, c.aprule FROM c WHERE {whereSql}";
  531. var client = cosmosClientCsv2.GetContainer("Habb", "Auth");
  532. var result = new List<object>();
  533. await foreach (var item in client.GetItemQueryStreamIterator(queryText: sql, requestOptions: qryOption))
  534. {
  535. var json = await JsonDocument.ParseAsync(item.ContentStream);
  536. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  537. {
  538. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  539. {
  540. result.Add(
  541. new
  542. {
  543. id = obj.GetProperty("id").GetString(),
  544. prodCode = obj.GetProperty("prodCode").GetString(),
  545. type = obj.GetProperty("type").GetInt32(),
  546. startDate = obj.GetProperty("startDate").GetInt64(),
  547. endDate = obj.GetProperty("endDate").GetInt64(),
  548. number = obj.GetProperty("number").GetInt32(),
  549. aprule = obj.GetProperty("aprule")
  550. });
  551. }
  552. }
  553. }
  554. return result;
  555. }
  556. //取得TMID購買紀錄
  557. public async Task<List<Ies5OrderHis>> getTMIDAuthOrder(CosmosClient cosmosClientCsv2, string tmid, string prodCode)
  558. {
  559. SortedDictionary<string, Ies5OrderHis> OrderDic = new SortedDictionary<string, Ies5OrderHis>();
  560. var qryOption = new QueryRequestOptions() { PartitionKey = new PartitionKey("order") };
  561. //服務 ※序號、硬體 不列入購買紀錄
  562. string strQueryV = $"SELECT * FROM c WHERE IS_DEFINED(c.saleClient.tmid) AND c.saleClient.tmid = '{tmid}' AND c.prodType = 'service'";
  563. if (!string.IsNullOrWhiteSpace(prodCode))
  564. {
  565. strQueryV += $" AND c.prodCode = '{prodCode}'";
  566. }
  567. await foreach (OrderHisService orderService in cosmosClientCsv2.GetContainer("Habb", "Auth").GetItemQueryIterator<OrderHisService>(strQueryV, null, qryOption))
  568. {
  569. Ies5OrderHisService ies5OrderVRow = new Ies5OrderHisService();
  570. ies5OrderVRow.prodCode = orderService.prodCode;
  571. ies5OrderVRow.type = (orderService.contract.Equals(1)) ? "C" : (orderService.contract.Equals(2)) ? "G" : "N"; //授權方式 [FROM]contract契約型態 0:新約 1:續約 2:變更 [TO]type N:新約 C:續約 G:變更
  572. ies5OrderVRow.sdate = orderService.startDate;
  573. ies5OrderVRow.edate = orderService.endDate;
  574. ies5OrderVRow.number = orderService.number;
  575. ies5OrderVRow.unit = orderService.unit;
  576. //加入Order字典
  577. string OrderID = orderService.orderinfo.orderid.ToString();
  578. long OrderDate = orderService.orderinfo.createDate;
  579. if (OrderDic.ContainsKey(OrderID))
  580. {
  581. OrderDic[OrderID].service.Add(ies5OrderVRow);
  582. }
  583. else
  584. {
  585. Ies5OrderHis ies5OrderHisNew = new Ies5OrderHis();
  586. ies5OrderHisNew.id = OrderID;
  587. ies5OrderHisNew.date = OrderDate;
  588. ies5OrderHisNew.service.Add(ies5OrderVRow);
  589. OrderDic.Add(OrderID, ies5OrderHisNew);
  590. }
  591. }
  592. //輸出項
  593. List<Ies5OrderHis> Result = new List<Ies5OrderHis>();
  594. foreach (KeyValuePair<string, Ies5OrderHis> item in OrderDic)
  595. {
  596. Result.Add(item.Value);
  597. }
  598. return Result;
  599. }
  600. //Tool
  601. //資料遮罩
  602. ///規則:
  603. ///1.手機、電話:隱藏後4碼
  604. ///2.Mail:隱藏@前資料
  605. ///3.姓名:只顯示第一個字元
  606. ///4.身分證、護照:隱藏後4碼
  607. public string GenDataMask(string data, string type)
  608. {
  609. int length = 0;
  610. string subString = string.Empty;
  611. if (!string.IsNullOrWhiteSpace(data))
  612. {
  613. switch (type)
  614. {
  615. case "mobile":
  616. case "id":
  617. length = (data.Length > 4) ? 4 : data.Length;
  618. subString = data.Substring(data.Length - length, length);
  619. data = data.Replace(subString, "****");
  620. break;
  621. case "mail":
  622. string[] dataList = data.Split("@");
  623. subString = dataList.First();
  624. data = data.Replace(subString, "****");
  625. break;
  626. case "name":
  627. length = 2;
  628. subString = data.Substring(data.Length - length, length);
  629. data = data.Replace(subString, "〇〇");
  630. break;
  631. }
  632. }
  633. return data;
  634. }
  635. public async Task<TmidAnalysisCal> getTMIDIotData(CosmosClient cosmosClientIes5, string tmid, string toolType, string dateUnit, int year, int month, int day, long from, long to)
  636. {
  637. TmidAnalysisCal result = new TmidAnalysisCal();
  638. 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" }; //要計算的ProdAnalysis欄位列表
  639. string strQuery = $"SELECT * FROM c WHERE c.tmid = '{tmid}' AND c.toolType = '{toolType}'";
  640. var qryOption = new QueryRequestOptions() { PartitionKey = new PartitionKey("TmidAnalysis") };
  641. if (!string.IsNullOrWhiteSpace(dateUnit)) strQuery += $" AND c.dateUnit = '{dateUnit}'";
  642. if (year > 0) strQuery += $" AND c.year = {year}";
  643. if (month > 0) strQuery += $" AND c.month = {month}";
  644. if (day > 0) strQuery += $" AND c.day = {day}";
  645. if (from > 0) strQuery += $" AND c.dateTime >= {from}";
  646. if (to > 0) strQuery += $" AND c.dateTime <= {to}";
  647. await foreach (TmidAnalysisCosmos tmidAnalysis in cosmosClientIes5.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<TmidAnalysisCosmos>(strQuery, null, qryOption))
  648. {
  649. //一般項
  650. result.tmid = tmidAnalysis.tmid;
  651. result.dateUnit = tmidAnalysis.dateUnit;
  652. result.toolType = tmidAnalysis.toolType;
  653. result.date = tmidAnalysis.date;
  654. if (tmidAnalysis.year > 0) result.year = tmidAnalysis.year;
  655. if (tmidAnalysis.month > 0) result.month = tmidAnalysis.month;
  656. if (tmidAnalysis.day > 0) result.day = tmidAnalysis.day;
  657. if (result.from.Equals(0) || tmidAnalysis.dateTime <= result.from) result.from = tmidAnalysis.dateTime;
  658. if (result.to.Equals(0) || tmidAnalysis.dateTime >= result.to) result.to = tmidAnalysis.dateTime;
  659. result.dateList.Add(tmidAnalysis.date);
  660. //統計項
  661. foreach (PropertyInfo propertyInfo in tmidAnalysis.GetType().GetProperties()) //累加項目
  662. {
  663. if (calPropList.Contains(propertyInfo.Name))
  664. {
  665. var propType = propertyInfo.PropertyType;
  666. var valNow = propertyInfo.GetValue(result);
  667. var valTodo = tmidAnalysis.GetType().GetProperty(propertyInfo.Name).GetValue(tmidAnalysis);
  668. if (propType.Equals(typeof(long))) propertyInfo.SetValue(result, Convert.ToInt64(valNow.ToString(), 10) + Convert.ToInt64(valTodo.ToString(), 10));
  669. else propertyInfo.SetValue(result, Convert.ToInt32(valNow.ToString(), 10) + Convert.ToInt32(valTodo.ToString(), 10));
  670. }
  671. }
  672. result.deviceList = result.deviceList.Union(tmidAnalysis.deviceList).ToList();
  673. result.deviceCnt = result.deviceList.Count;
  674. result.deviceNoAuthList = result.deviceNoAuthList.Union(tmidAnalysis.deviceNoAuthList).ToList();
  675. result.deviceNoAuth = result.deviceNoAuthList.Count;
  676. result.deviceAuthList = result.deviceAuthList.Union(tmidAnalysis.deviceAuthList).ToList();
  677. result.deviceAuth = result.deviceAuthList.Count;
  678. result.tmidList = result.tmidList.Union(tmidAnalysis.tmidList).ToList();
  679. result.tmidCnt = result.tmidList.Count;
  680. result.verList = result.verList.Union(tmidAnalysis.verList).ToList();
  681. }
  682. if (string.IsNullOrWhiteSpace(result.tmid)) result = null;
  683. return result;
  684. }
  685. //Model
  686. //TMID統計 基本資訊
  687. private class TmidStics
  688. {
  689. public string id { get; set; }
  690. public string name { get; set; }
  691. public string picture { get; set; }
  692. public string mobile { get; set; }
  693. public string mail { get; set; }
  694. public string country { get; set; }
  695. public string province { get; set; }
  696. public string city { get; set; }
  697. public string dist { get; set; }
  698. public string schoolCode { get; set; }
  699. public string schoolCodeW { get; set; }
  700. public string lang { get; set; }
  701. public string unitType { get; set; } //1:基礎教育機構(K-小學) 2:中等教育機構(國中、高中/職) 3:高等教育機構 4:政府單位機構 5:NGO機構 6:企業機構 7:其他 8:學前教育 9:特殊教育
  702. public string unitName { get; set; }
  703. public string jobTitle { get; set; }
  704. public TmidSticsIes5 ies5 { get; set; } //IES統計資料
  705. public TmidPoints points { get; set; }
  706. public TmidSokrates sokrates { get; set; }
  707. public TmidBenefits benefits { get; set; }
  708. public List<TmidCoupon> coupons { get; set; } = new();
  709. public List<TmidLoginTime> login { get; set; } = new();
  710. public List<object> prod { get; set; } = new();
  711. public TmidIot iot { get; set; }
  712. public bool wechat { get; set; }
  713. public bool facebook { get; set; }
  714. public bool google { get; set; }
  715. public bool ding { get; set; }
  716. public bool apple { get; set; }
  717. public bool educloudtw { get; set; }
  718. public long ts { get; set; } //資料最終變更時間
  719. }
  720. //TMID統計 IES5資訊
  721. private class TmidSticsIes5
  722. {
  723. public long usedSize { get; set; } //已使用的存儲空間(Byte)
  724. public long teachSize { get; set; } //分配給教師的空間(Byte)
  725. public long totalSize { get; set; } //總空間(Byte)
  726. public long surplusSize { get; set; } //剩余的空間(Byte)
  727. public List<TmidSticsIes5School> schools { get; set; } = new(); //各學校資料
  728. public int resCount { get; set; } //教材數
  729. public int itemCount { get; set; } //題目數
  730. public int paperCount { get; set; } //試卷數
  731. }
  732. private class TmidSticsIes5School
  733. {
  734. public string schoolId { get; set; }
  735. public string name { get; set; }
  736. public string region { get; set; }
  737. public string province { get; set; }
  738. public string city { get; set; }
  739. public string dist { get; set; }
  740. public object size { get; set; } = new();
  741. public int courseCnt { get; set; }
  742. public bool defaultSchool { get; set; }
  743. }
  744. private class TmidPoints
  745. {
  746. public int points { get; set; } = 0; //累積的點數(只加不減)
  747. public int balance { get; set; } = 0; //可用的點數
  748. public int level { get; set; } = 0; //等級
  749. }
  750. private class TmidCoupon
  751. {
  752. public string code { get; set; }
  753. public long exchange { get; set; }
  754. }
  755. private class TmidLoginTime
  756. {
  757. public string product { get; set; }
  758. public long time { get; set; }
  759. }
  760. private class TmidSokrates
  761. {
  762. public object hiteach_data { get; set; }
  763. public object user_channels { get; set; }
  764. }
  765. private class TmidBenefits
  766. {
  767. public List<object> hiteach { get; set; }
  768. public List<object> hiteachcc { get; set; }
  769. }
  770. private class TmidIot
  771. {
  772. public TmidIotYMD hiteach { get; set; } = new();
  773. public TmidIotYMD hiteachcc { get; set; } = new();
  774. }
  775. private class TmidIotYMD
  776. {
  777. public TmidAnalysisCal year { get; set; }
  778. public TmidAnalysisCal month { get; set; }
  779. public TmidAnalysisCal day { get; set; }
  780. }
  781. //[API輸出] 訂單履歷
  782. public class Ies5OrderHis
  783. {
  784. public Ies5OrderHis()
  785. {
  786. service = new List<Ies5OrderHisService>();
  787. }
  788. public string id { get; set; }
  789. public long date { get; set; }
  790. public List<Ies5OrderHisService> service { get; set; } = new ();
  791. }
  792. //[API輸出] 訂單履歷.服務型產品
  793. public class Ies5OrderHisService
  794. {
  795. public string prodCode { get; set; } //產品代碼
  796. public string type { get; set; } //授權方式 N:新約 C:續約
  797. public long sdate { get; set; } //授權起始時間
  798. public long edate { get; set; } //授權終止時間
  799. public int number { get; set; } //購買數量
  800. public string unit { get; set; } //購買單位 無單位者為null
  801. }
  802. //[承接DB] DB: Habb.Auth
  803. //[承接DB] 訂單履歷基本Class
  804. public class OrderHisBase
  805. {
  806. public string id { get; set; } //OPID
  807. public OrderHisOrderInfo orderinfo { get; set; } //訂單資訊
  808. public string prodCode { get; set; } //產品代碼
  809. public SerialSaleClient saleClient { get; set; } = null; //銷售終端
  810. public string prodType { get; set; } //產品類型 serial:序號 service:服務 hard:硬體
  811. public string dataType { get; set; } //資料類型
  812. public long operationTime { get; set; } //最新資料變更時間戳記
  813. public int ttl { get; set; } = -1; //過期刪除秒數
  814. }
  815. //[承接DB] 訂單履歷基本Class.銷售終端
  816. public class SerialSaleClient
  817. {
  818. public string name { get; set; } //[String]銷售終端姓名
  819. public string schoolCode { get; set; } = null; //[String]銷售終端學校代碼
  820. public string clientId { get; set; } = null; //[String]銷售終端客戶ID
  821. public string tmid { get; set; } = null; //[String]TMID
  822. public string type { get; set; } //[String]銷售終端資料類型 school:學校 client:經銷商客戶
  823. public string countryId { get; set; } //[String]國家代碼
  824. public string provinceId { get; set; } //[String]省代碼
  825. public string cityId { get; set; } //[String]市代碼
  826. public string schoolShortCode { get; set; } //[String]學校簡碼
  827. public string districtId { get; set; } //[String]區代碼
  828. public string dataCenter { get; set; } //[String]數據中心
  829. }
  830. //[承接DB] 訂單履歷基本Class.訂單資訊
  831. public class OrderHisOrderInfo
  832. {
  833. public string orderid { get; set; } //[String]訂單編號
  834. public int orderAudit { get; set; } //[Int]訂單審核狀態 0:待審, 1:通過, 2:否決, 3:問題
  835. public int orderProperty { get; set; } //[Int]訂單類型 0:銷售,1:展示申請 2:內部申請
  836. public long createDate { get; set; } //訂單創建時間
  837. }
  838. //[承接DB] 訂單履歷 服務類
  839. public class OrderHisService : OrderHisBase
  840. {
  841. public int type { get; set; } //[Int]授權類型 0:銷售 1:試用
  842. public int contract { get; set; } //[Int]契約型態 0:新約 1:續約
  843. public long startDate { get; set; } //[long]授權起始日期 Timestamp(UTC)
  844. public long endDate { get; set; } //[long]授權終止日期 Timestamp(UTC)
  845. public int number { get; set; } //[Int]數量
  846. public string unit { get; set; } //[String]單位 (無者為空)
  847. }
  848. //雲端服務ClientId列表
  849. private readonly Dictionary<string, string> _dicClientIds = new()
  850. {
  851. { "917d02f2-5b91-404e-a71d-7bdd926ddd81", "HiTeach" },
  852. { "c5328340-b8eb-4489-8650-8c8862d7acfe", "HiTeach" },
  853. { "118d2d4d-32a3-44c0-808a-792ca73d3706", "HiTeachCC" },
  854. { "227082f4-8db0-4517-b281-93dba9428bc7", "HiTeachCC" },
  855. { "371a99aa-7efd-4c3a-90a8-95b7db4f42c0", "HiTA" },
  856. { "0ed38cde-e7fe-4fa6-9eaa-33ad404eb532", "HiTA" },
  857. { "531fecd1-b1a5-469a-93ca-7984e1d392f2", "IES5" },
  858. { "c7317f88-7cea-4e48-ac57-a16071f7b884", "IES5" },
  859. { "c8de822b-3fcf-4831-adba-cdd14653bf7f", "Account" },
  860. { "516148eb-6a38-4657-ba98-a3699061937f", "Account" },
  861. { "d7193896-9b12-4046-ad44-c991dd48cc39", "Sokrates" },
  862. { "59009e38-6e30-4116-814b-7605939edc47", "Sokrates" },
  863. { "626c7285-15aa-4abe-8c0d-2c5ff1381538", "SokAPP" },
  864. { "5bcc16a5-7d20-4cc4-b5c2-8ed0416f32b6", "SokAPP" },
  865. { "044482b5-d024-4f23-9b55-906884243405", "IRS" },
  866. { "5e27b7e3-b36c-4ce9-b838-e94fd0cea080", "IRS" }
  867. };
  868. //TMID IOT date
  869. public class tmidIotDate
  870. {
  871. public string dateUnit { get; set; } //[string]日期單位:年月日 year, month, day
  872. public int year { get; set; } //[Int]年
  873. public int month { get; set; } //[Int]月
  874. public int day { get; set; } //[Int]日
  875. public long from { get; set; } //[long]UTC timestamp 起始日
  876. public long to { get; set; } //[long]UTC timestamp 終止日
  877. }
  878. public class TmidAnalysisCal : TmidAnalysisCosmos
  879. {
  880. public long from { get; set; } //[long]UTC timestamp 起始日
  881. public long to { get; set; } //[long]UTC timestamp 終止日
  882. public List<string> dateList { get; set; } = new();
  883. }
  884. }
  885. }