TmidController.cs 44 KB

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