HiTeachController.cs 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987
  1. using Azure.Cosmos;
  2. using Azure.Storage.Blobs.Models;
  3. using Azure.Storage.Sas;
  4. using Microsoft.AspNetCore.Authorization;
  5. using Microsoft.AspNetCore.Http;
  6. using Microsoft.AspNetCore.Mvc;
  7. using Microsoft.Extensions.Options;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IdentityModel.Tokens.Jwt;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Text.Json;
  15. using System.Threading.Tasks;
  16. using TEAMModelOS.Models;
  17. using TEAMModelOS.Filter;
  18. using TEAMModelOS.SDK.DI;
  19. using TEAMModelOS.SDK.Extension;
  20. using Microsoft.Azure.Cosmos.Table;
  21. using TEAMModelOS.SDK.Models;
  22. using System.Dynamic;
  23. using HTEXLib.COMM.Helpers;
  24. using StackExchange.Redis;
  25. using TEAMModelOS.SDK.Models.Cosmos.Common;
  26. using TEAMModelOS.SDK;
  27. using Microsoft.Extensions.Configuration;
  28. using Azure.Messaging.ServiceBus;
  29. using TEAMModelOS.SDK.Services;
  30. using TEAMModelOS.SDK.Models.Service;
  31. using TEAMModelOS.Models.Request;
  32. using Azure;
  33. namespace TEAMModelOS.Controllers.Client
  34. {
  35. [ProducesResponseType(StatusCodes.Status200OK)]
  36. [ProducesResponseType(StatusCodes.Status400BadRequest)]
  37. //[Authorize(Roles = "HiTeach")]
  38. [Route("hiteach")]
  39. [ApiController]
  40. public class HiTeachController : ControllerBase
  41. {
  42. private readonly AzureStorageFactory _azureStorage;
  43. private readonly AzureRedisFactory _azureRedis;
  44. private readonly AzureCosmosFactory _azureCosmos;
  45. private readonly DingDing _dingDing;
  46. private readonly Option _option;
  47. private readonly SnowflakeId _snowflakeId;
  48. private readonly AzureServiceBusFactory _serviceBus;
  49. private readonly IConfiguration _configuration;
  50. private readonly CoreAPIHttpService _coreAPIHttpService;
  51. private readonly IPSearcher _searcher;
  52. private readonly HttpTrigger _httpTrigger;
  53. public HiTeachController(
  54. AzureStorageFactory azureStorage,
  55. AzureRedisFactory azureRedis,
  56. AzureCosmosFactory azureCosmos,
  57. DingDing dingDing,
  58. SnowflakeId snowflakeId,
  59. IOptionsSnapshot<Option> option,
  60. AzureServiceBusFactory serviceBus,
  61. IConfiguration configuration,
  62. CoreAPIHttpService coreAPIHttpService, IPSearcher searcher, HttpTrigger httpTrigger)
  63. {
  64. _azureStorage = azureStorage;
  65. _azureRedis = azureRedis;
  66. _azureCosmos = azureCosmos;
  67. _dingDing = dingDing;
  68. _snowflakeId = snowflakeId;
  69. _option = option?.Value;
  70. _serviceBus = serviceBus;
  71. _configuration = configuration;
  72. _coreAPIHttpService = coreAPIHttpService;
  73. _searcher = searcher;
  74. _httpTrigger = httpTrigger;
  75. }
  76. /// <summary>
  77. /// 更新课堂记录
  78. /// </summary>
  79. /// <param name="request"></param>
  80. /// <returns></returns>
  81. [Authorize(Roles = "HiTeach")]
  82. [ProducesDefaultResponseType]
  83. [HttpPost("update-lesson-record")]
  84. public async Task<IActionResult> UpdateLessonRecord(JsonElement request)
  85. {
  86. var client = _azureCosmos.GetCosmosClient();
  87. if (!request.TryGetProperty("lesson_id", out JsonElement _lessonId)) return BadRequest();
  88. if (!request.TryGetProperty("tmdid", out JsonElement _tmdid)) return BadRequest();
  89. request.TryGetProperty("name", out JsonElement name);
  90. request.TryGetProperty("school", out JsonElement _school);
  91. if (!request.TryGetProperty("scope", out JsonElement _scope)) return BadRequest();
  92. request.TryGetProperty("grant_types", out JsonElement _grant_types);
  93. string tbname;
  94. string code;
  95. if (_scope.GetString().Equals("school") && !string.IsNullOrWhiteSpace(_school.GetString()))
  96. {
  97. code = $"LessonRecord-{_school}";
  98. tbname = "School";
  99. }
  100. else if ($"{_scope}".Equals("private"))
  101. {
  102. code = $"LessonRecord";
  103. tbname = "Teacher";
  104. }
  105. else
  106. {
  107. return BadRequest();
  108. }
  109. try
  110. {
  111. LessonRecord lessonRecord = await client.GetContainer(Constant.TEAMModelOS, tbname).ReadItemAsync<LessonRecord>($"{_lessonId}", new PartitionKey(code));
  112. List<LessonUpdate> updates = new List<LessonUpdate>() { new LessonUpdate { grant_type = "up-base" } };
  113. if (_grant_types.ValueKind.Equals(JsonValueKind.Array))
  114. {
  115. updates = _grant_types.ToObject<List<LessonUpdate>>();
  116. if (!updates.Select(x => x.grant_type).Contains("up-base"))
  117. {
  118. updates.Add(new LessonUpdate { grant_type = "up-base" });
  119. }
  120. }
  121. if (!string.IsNullOrWhiteSpace($"{name}"))
  122. {
  123. lessonRecord.name = $"{name}";
  124. await client.GetContainer(Constant.TEAMModelOS, tbname).ReplaceItemAsync<LessonRecord>(lessonRecord, $"{_lessonId}", new PartitionKey(code));
  125. }
  126. var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  127. string msg = null;
  128. msg = new { lesson_id = $"{_lessonId}", tmdid = $"{_tmdid}", grant_types = updates, school = $"{_school}", scope = $"{_scope}" }.ToJsonString();
  129. var messageChange = new ServiceBusMessage(msg);
  130. messageChange.ApplicationProperties.Add("name", "LessonRecordEvent");
  131. //await _dingDing.SendBotMsg($"{_option.Location},课堂id:{_lessonId} 更新事件,{msg}", GroupNames.醍摩豆服務運維群組);
  132. await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageChange);
  133. return Ok(new { status = 200 });
  134. }
  135. catch (CosmosException ex) when (ex.Status == 404)
  136. {
  137. return BadRequest("课堂记录不存在");
  138. }
  139. catch (Exception ex)
  140. {
  141. return BadRequest();
  142. }
  143. }
  144. /// <summary>
  145. /// 创建课堂开课记录(新版)
  146. /// </summary>
  147. /// <param name="request"></param>
  148. /// <returns></returns>
  149. [Authorize(Roles = "HiTeach")]
  150. [ProducesDefaultResponseType]
  151. [HttpPost("create-lesson")]
  152. public async Task<IActionResult> CreateLesson(CreateLessondRequest request)
  153. {
  154. try
  155. {
  156. string id_token = HttpContext.GetXAuth("IdToken");
  157. //判斷ID Token
  158. if (string.IsNullOrWhiteSpace(id_token)) return BadRequest();
  159. var jwt = new JwtSecurityToken(id_token);
  160. if (!jwt.Payload.Iss.Equals("account.teammodel", StringComparison.OrdinalIgnoreCase)) return BadRequest();
  161. var tid = jwt.Payload.Sub;
  162. //初始化
  163. var r8 = _azureRedis.GetRedisClient(8);
  164. var db = _azureCosmos.GetCosmosClient();
  165. var sbm = new List<ServiceBusMessage>();
  166. var sp = request.sp.Equals("school", StringComparison.OrdinalIgnoreCase);
  167. int size = 0; //學校或個人總空間
  168. int tsize = 0; //學校分配給老師的總空間
  169. double usize = 0; //學校或個人已使用空間
  170. string timezone = string.Empty;
  171. if (sp)
  172. {
  173. //取得學校資訊
  174. if (string.IsNullOrWhiteSpace(request.school)) return BadRequest();
  175. await foreach (Response item in db.GetContainer(Constant.TEAMModelOS, Constant.School).GetItemQueryStreamIterator(
  176. queryText: $"SELECT TOP 1 c.id, c.size, c.tsize, c.timeZone FROM c WHERE c.id = '{request.school}'",
  177. requestOptions: new() { PartitionKey = new("Base") }))
  178. {
  179. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  180. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  181. {
  182. var school = json.RootElement.GetProperty("Documents").EnumerateArray().First();
  183. timezone = school.GetProperty("timeZone").GetProperty("value").GetString();
  184. size = school.GetProperty("size").GetInt32();
  185. tsize = school.TryGetProperty("tsize", out var tsvalue) ? tsvalue.GetInt32() : 0;
  186. }
  187. else return BadRequest();
  188. }
  189. }
  190. else
  191. {
  192. Teacher teacher = await db.GetContainer(Constant.TEAMModelOS, Constant.Teacher).ReadItemAsync<Teacher>(tid, new PartitionKey("Base"));
  193. size = teacher.size;
  194. foreach (var school in teacher.schools)
  195. {
  196. SchoolTeacher st = await db.GetContainer(Constant.TEAMModelOS, Constant.School).ReadItemAsync<SchoolTeacher>(tid, new PartitionKey($"Teacher-{school.schoolId}"));
  197. size += st.size;
  198. }
  199. }
  200. //計算學校或個人的使用空間
  201. RedisValue redisValue = r8.HashGet($"Blob:Record", $"{(sp ? request.school : tid)}");
  202. if (redisValue.HasValue && long.TryParse(redisValue.ToString(), out var bsize))
  203. {
  204. usize = Math.Round(bsize / 1073741824.0 - (sp ? tsize : 0), 2, MidpointRounding.AwayFromZero); //1073741824 1G
  205. }
  206. else //如果檢測不到緩存,觸發刷新計算空間
  207. {
  208. await BlobService.RefreshBlobRoot(new BlobRefreshMessage { progress = "update", root = "records", name = $"{(sp ? request.school : tid)}" }, _serviceBus, _configuration, _azureRedis);
  209. }
  210. //取得學校或個人名單
  211. (List<RMember> students, _) = await GroupListService.GetMemberByListids(_coreAPIHttpService, db, _dingDing, new List<string>() { request.sid }, request.school);
  212. //觸發IMEI更新消息
  213. var stus = students.Select(x => x.id).ToList();
  214. var imeimsg = new ServiceBusMessage(new { request.channel, userid = request.did, request.school, stus }.ToJsonString());
  215. imeimsg.ApplicationProperties.Add("name", "Imei");
  216. sbm.Add(imeimsg);
  217. //開課記錄保存
  218. LessonRecord lr = new()
  219. {
  220. school = request.sp.Equals("school") ? request?.school : null,
  221. code = request.sp.Equals("school") ? $"LessonRecord-{request.school}" : $"LessonRecord",
  222. id = _snowflakeId.NextId().ToString(), //取得授課ID
  223. courseId = request.cid,
  224. groupIds = new() { request.sid },
  225. tmdid = tid,
  226. scope = request.sp,
  227. pk = "LessonRecord",
  228. name = request.cname,
  229. startTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
  230. };
  231. await db.GetContainer(Constant.TEAMModelOS, request.sp.Equals("school") ? "School" : "Teacher").CreateItemAsync(lr, new PartitionKey(lr.code));
  232. //觸發開課統計
  233. var messageChange = new ServiceBusMessage(lr.ToJsonString());
  234. messageChange.ApplicationProperties.Add("name", "LessonRecordEvent");
  235. sbm.Add(messageChange);
  236. //批量發送消息
  237. await _serviceBus.GetServiceBusClient().SendBatchMessageAsync(_configuration.GetValue<string>("Azure:ServiceBus:ActiveTask"), sbm);
  238. if (sp && usize>size)
  239. {
  240. ////处理学校开课,空间不足时。检查是否有 当前教师tid,强制保存save<>1,没有标记未删除status<>404,没有被收藏favorite<=0 ,时间最旧的一条记录startTime
  241. LessonRecord lessonRecord = null;
  242. string sql = $"SELECT top 1 value(c) FROM c where ( c.expire<=0 or IS_DEFINED(c.expire) = false ) and c.tmdid='{tid}' and c.save<>1 and c.status<>404 and c.favorite<=0 order by c.startTime ";
  243. await foreach (var item in db.GetContainer(Constant.TEAMModelOS, Constant.School).GetItemQueryIterator<LessonRecord>(
  244. queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"LessonRecord-{request.school}") }))
  245. {
  246. lessonRecord = item;
  247. break;
  248. }
  249. if (lessonRecord != null)
  250. {
  251. lessonRecord.status = 404;
  252. await db.GetContainer(Constant.TEAMModelOS, Constant.School).ReplaceItemAsync(lessonRecord, lessonRecord.id, new PartitionKey(lessonRecord.code));
  253. var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  254. var messageChangeEvent = new ServiceBusMessage(request.ToJsonString());
  255. messageChangeEvent.ApplicationProperties.Add("name", "LessonRecordEvent");
  256. await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageChangeEvent);
  257. //保证客户端可以正常开课。
  258. usize -= 1;
  259. }
  260. else
  261. { //没有找到匹配当前 教师tid,save<>1,status<>404,没有被收藏,时间最旧的一条记录。无法手动再继续 usize -= 1;,则不能继续开课。
  262. }
  263. }
  264. return Ok(new { status = 200, lr.id, students, size, usize });
  265. }
  266. catch (Exception ex)
  267. {
  268. await _dingDing.SendBotMsg($"IES5,{_option.Location}, hiteach/create-lesson:()\n{ex.Message}\n{ex.StackTrace}{request.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
  269. return BadRequest();
  270. }
  271. }
  272. /// <summary>
  273. /// 创建课堂开课记录(舊版,已被CreateLesson取代)
  274. /// </summary>
  275. /// <param name="request"></param>
  276. /// <returns></returns>
  277. [Authorize(Roles = "HiTeach")]
  278. [ProducesDefaultResponseType]
  279. [HttpPost("create-lesson-record")]
  280. public async Task<IActionResult> CreateLessonRecord(JsonElement request)
  281. {
  282. if (!request.TryGetProperty("lesson", out JsonElement _lesson)) return BadRequest();
  283. var client = _azureCosmos.GetCosmosClient();
  284. LessonRecord lessonRecord = _lesson.ToObject<LessonRecord>();
  285. int blobTotal = 0;
  286. long teach = 0;
  287. string blobName = "";
  288. if (!string.IsNullOrEmpty(lessonRecord.scope) && !string.IsNullOrEmpty(lessonRecord.tmdid) && !string.IsNullOrEmpty(lessonRecord.id))
  289. {
  290. string tbname = null;
  291. if (lessonRecord.scope.Equals("school") && !string.IsNullOrEmpty(lessonRecord.school))
  292. {
  293. School school = await client.GetContainer(Constant.TEAMModelOS, Constant.School).ReadItemAsync<School>(lessonRecord.school, new PartitionKey("Base"));
  294. lessonRecord.code = $"LessonRecord-{lessonRecord.school}";
  295. tbname = "School";
  296. blobTotal = school.size;
  297. //计算分配给学校教师的空间
  298. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: $"SELECT sum(c.size) as size FROM c ",
  299. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{lessonRecord.school}") }))
  300. {
  301. var json = await JsonDocument.ParseAsync(item.ContentStream);
  302. foreach (var elmt in json.RootElement.GetProperty("Documents").EnumerateArray())
  303. {
  304. if (elmt.TryGetProperty("size", out JsonElement _size) && _size.ValueKind.Equals(JsonValueKind.Number))
  305. {
  306. teach = _size.GetInt32();
  307. break;
  308. }
  309. }
  310. }
  311. blobName = lessonRecord.school;
  312. }
  313. if (lessonRecord.scope.Equals("private"))
  314. {
  315. lessonRecord.code = $"LessonRecord";
  316. tbname = "Teacher";
  317. Teacher teacher = await client.GetContainer(Constant.TEAMModelOS, Constant.Teacher).ReadItemAsync<Teacher>(lessonRecord.tmdid, new PartitionKey("Base"));
  318. blobTotal = teacher.size;
  319. foreach (var school in teacher.schools)
  320. {
  321. SchoolTeacher st = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<SchoolTeacher>(lessonRecord.tmdid, new PartitionKey($"Teacher-{school.schoolId}"));
  322. blobTotal += st.size;
  323. }
  324. blobName = lessonRecord.tmdid;
  325. }
  326. RedisValue redisValue = _azureRedis.GetRedisClient(8).HashGet($"Blob:Record", $"{blobName}");
  327. long blobsize = 0;
  328. if (redisValue != default && !redisValue.IsNullOrEmpty)
  329. {
  330. JsonElement record = redisValue.ToString().ToObject<JsonElement>();
  331. if (record.TryGetInt64(out blobsize))
  332. {
  333. }
  334. }
  335. else
  336. {
  337. await BlobService.RefreshBlobRoot(new BlobRefreshMessage { progress = "update", root = "records", name = $"{blobName}" }, _serviceBus, _configuration, _azureRedis);
  338. }
  339. double blobUsed = blobsize / 1073741824.0 + teach; //1073741824 1G
  340. if (tbname == null)
  341. {
  342. return BadRequest("参数异常:scope应为school或private,scope=school ,school字段不能为空");
  343. }
  344. if (lessonRecord.startTime == 0)
  345. {
  346. lessonRecord.startTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  347. }
  348. lessonRecord.pk = "LessonRecord";
  349. try
  350. {
  351. await client.GetContainer(Constant.TEAMModelOS, tbname).CreateItemAsync(lessonRecord, new PartitionKey(lessonRecord.code));
  352. var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  353. var messageChange = new ServiceBusMessage(lessonRecord.ToJsonString());
  354. messageChange.ApplicationProperties.Add("name", "LessonRecordEvent");
  355. await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageChange);
  356. return Ok(new { status = 200, blobTotal, blobUsed });
  357. }
  358. catch (Exception ex)
  359. {
  360. await _dingDing.SendBotMsg($"IES5,{_option.Location}, hiteach/create-lesson-record:()\n{ex.Message}\n{ex.StackTrace}{request.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
  361. return BadRequest();
  362. }
  363. }
  364. else
  365. {
  366. return BadRequest();
  367. }
  368. }
  369. [Authorize(Roles = "HiTeach")]
  370. [ProducesDefaultResponseType]
  371. [HttpPost("get-teacher-info")]
  372. public async Task<IActionResult> GetTeacherInfo()
  373. {
  374. //Debug
  375. //string json = System.Text.Json.JsonSerializer.Serialize(id_token);
  376. try
  377. {
  378. string id_token = HttpContext.GetXAuth("IdToken");
  379. (string ip, string region) = await LoginService.LoginIp(HttpContext, _searcher);
  380. if (string.IsNullOrEmpty(id_token)) return BadRequest();
  381. var jwt = new JwtSecurityToken(id_token);
  382. if (!jwt.Payload.Iss.Equals("account.teammodel", StringComparison.OrdinalIgnoreCase)) return BadRequest();
  383. var id = jwt.Payload.Sub;
  384. jwt.Payload.TryGetValue("name", out object name);
  385. jwt.Payload.TryGetValue("picture", out object picture);
  386. List<object> schools = new List<object>();
  387. string defaultschool = null;
  388. //TODK 取得Teacher 個人相關數據(課程清單、虛擬教室清單、歷史紀錄清單等),學校數據另外API處理,多校切換時不同
  389. var client = _azureCosmos.GetCosmosClient();
  390. var response = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemStreamAsync(id, new PartitionKey("Base"));
  391. if (response.Status == 200)
  392. {
  393. var jsonsc = await JsonDocument.ParseAsync(response.ContentStream);
  394. if (jsonsc.RootElement.TryGetProperty("schools", out JsonElement value))
  395. {
  396. GetTeacherInfoApiSchool schoolExtobj;
  397. foreach (var obj in value.EnumerateArray())
  398. {
  399. string statusNow = obj.GetProperty("status").ToString();
  400. if (statusNow.Equals("join")) //成為老師的才放入
  401. {
  402. var schoolJson = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{obj.GetProperty("schoolId")}", new PartitionKey("Base"));
  403. var school = await JsonDocument.ParseAsync(schoolJson.ContentStream);
  404. schoolExtobj = new GetTeacherInfoApiSchool();
  405. schoolExtobj.schoolId = Convert.ToString(obj.GetProperty("schoolId"));
  406. schoolExtobj.name = Convert.ToString(school.RootElement.GetProperty("name"));
  407. schoolExtobj.status = Convert.ToString(obj.GetProperty("status"));
  408. schoolExtobj.picture = Convert.ToString(school.RootElement.GetProperty("picture"));
  409. schools.Add(schoolExtobj);
  410. var sctch = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(id, new PartitionKey($"Teacher-{obj.GetProperty("schoolId")}"));
  411. if (sctch.Status == 200 && sctch != null && sctch.ContentStream != null)
  412. {
  413. var jsonDoc = await JsonDocument.ParseAsync(sctch.ContentStream);
  414. SchoolTeacher schoolTeacher = jsonDoc.RootElement.ToObject<SchoolTeacher>();
  415. }
  416. }
  417. }
  418. }
  419. //預設學校ID
  420. if (jsonsc.RootElement.TryGetProperty("defaultSchool", out JsonElement valueD) && !string.IsNullOrEmpty(valueD.ToString()))
  421. {
  422. defaultschool = valueD.ToString();
  423. }
  424. }
  425. else
  426. {
  427. //如果沒有,則初始化Teacher基本資料到Cosmos
  428. Teacher teacher = new Teacher
  429. {
  430. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  431. id = id,
  432. pk = "Base",
  433. code = "Base",
  434. name = name?.ToString(),
  435. picture = picture?.ToString(),
  436. //创建账号并第一次登录IES5则默认赠送1G
  437. size = 1,
  438. defaultSchool = null,
  439. schools = new List<Teacher.TeacherSchool>(),
  440. };
  441. teacher = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync<Teacher>(teacher, new PartitionKey("Base"));
  442. }
  443. //老師個人課程清單
  444. List<GetTeacherInfoApiCourse> courses = new List<GetTeacherInfoApiCourse>();
  445. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIterator(queryText: $"SELECT c.id, c.name, c.schedule, c.scope FROM c ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Course-{id}") }))
  446. {
  447. var jsontcs = await JsonDocument.ParseAsync(item.ContentStream);
  448. if (jsontcs.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  449. {
  450. GetTeacherInfoApiCourse courseExtobj;
  451. List<GetTeacherInfoApiCourseClass> classes;
  452. foreach (var obj in jsontcs.RootElement.GetProperty("Documents").EnumerateArray())
  453. {
  454. courseExtobj = new GetTeacherInfoApiCourse();
  455. courseExtobj.id = Convert.ToString(obj.GetProperty("id"));
  456. courseExtobj.name = Convert.ToString(obj.GetProperty("name"));
  457. courseExtobj.scope = Convert.ToString(obj.GetProperty("scope"));
  458. classes = new List<GetTeacherInfoApiCourseClass>();
  459. if (obj.TryGetProperty("schedule", out JsonElement schedule))
  460. {
  461. GetTeacherInfoApiCourseClass classExtobj;
  462. foreach (var scheduleobj in schedule.EnumerateArray())
  463. {
  464. classExtobj = new GetTeacherInfoApiCourseClass();
  465. classExtobj.id = null;
  466. classExtobj.code = null;
  467. classExtobj.teacher = null;
  468. if (scheduleobj.TryGetProperty("teacherId", out JsonElement teacherId) && !string.IsNullOrWhiteSpace(Convert.ToString(teacherId)))
  469. {
  470. await foreach (var teaitem in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIterator(queryText: $"SELECT c.id, c.name FROM c WHERE c.id = '{teacherId}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  471. {
  472. var jsontea = await JsonDocument.ParseAsync(teaitem.ContentStream);
  473. foreach (var teaobj in jsontea.RootElement.GetProperty("Documents").EnumerateArray())
  474. {
  475. classExtobj.teacher = new GetTeacherInfoApiSimlpeBase();
  476. classExtobj.teacher = teaobj.ToObject<GetTeacherInfoApiSimlpeBase>();
  477. }
  478. }
  479. }
  480. classExtobj.scope = "private";
  481. int stuCount = 0;
  482. string stuListId = Convert.ToString(scheduleobj.GetProperty("stulist"));
  483. classExtobj.stuListId = stuListId;
  484. await foreach (var stuitem in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIterator(queryText: $"SELECT c.id, c.name, c.tcount, c.scount FROM c WHERE c.id = '{stuListId}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("GroupList") }))
  485. {
  486. //取得學生總數
  487. var jsonstu = await JsonDocument.ParseAsync(stuitem.ContentStream);
  488. foreach (var stuobj in jsonstu.RootElement.GetProperty("Documents").EnumerateArray())
  489. {
  490. classExtobj.name = Convert.ToString(stuobj.GetProperty("name"));
  491. stuCount += stuobj.GetProperty("scount").GetInt32();
  492. stuCount += stuobj.GetProperty("tcount").GetInt32();
  493. }
  494. }
  495. classExtobj.stuCnt = stuCount;
  496. classExtobj.grpCnt = 0;
  497. classExtobj.gradeId = null;
  498. classExtobj.year = 0;
  499. classes.Add(classExtobj);
  500. }
  501. }
  502. courseExtobj.classes = classes;
  503. courses.Add(courseExtobj);
  504. }
  505. }
  506. }
  507. //取得老師個人評測
  508. List<object> exams = new List<object>();
  509. //取得評測ID List (HiTeach只取source='1'(課中评量) && progress = 'going'(進行中))
  510. List<string> examIdList = new List<string>();
  511. await foreach (var exam in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIterator(queryText: $"SELECT c.id FROM c where (c.status<>404 or IS_DEFINED(c.status) = false ) and c.source = '1' AND c.progress = 'going'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Exam-{id}") }))
  512. {
  513. using var json = await JsonDocument.ParseAsync(exam.ContentStream);
  514. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  515. {
  516. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  517. {
  518. examIdList.Add(obj.GetProperty("id").GetString());
  519. }
  520. }
  521. }
  522. //取得有作答的評測班級
  523. Dictionary<string, List<string>> examClassFinDic = new Dictionary<string, List<string>>();
  524. await foreach (var exam in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIterator(queryText: $"SELECT c.examId, c.info.id as classId FROM c where (c.status<>404 or IS_DEFINED(c.status) = false ) and ARRAY_CONTAINS({JsonSerializer.Serialize(examIdList)}, c.examId) AND c.progress=true", requestOptions: new QueryRequestOptions() { }))
  525. {
  526. var jsonecr = await JsonDocument.ParseAsync(exam.ContentStream);
  527. if (jsonecr.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  528. {
  529. foreach (var obj in jsonecr.RootElement.GetProperty("Documents").EnumerateArray())
  530. {
  531. string examId = obj.GetProperty("examId").ToString();
  532. string classId = obj.GetProperty("classId").ToString();
  533. if (examClassFinDic.ContainsKey(examId) && !examClassFinDic[examId].Contains(classId))
  534. {
  535. examClassFinDic[examId].Add(classId);
  536. }
  537. else
  538. {
  539. List<string> classIdList = new List<string>();
  540. classIdList.Add(classId);
  541. examClassFinDic.Add(examId, classIdList);
  542. }
  543. }
  544. }
  545. }
  546. //取得評測資料
  547. await foreach (var exam in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIterator(queryText: $"SELECT c.code, c.id, c.name, c.createTime, c.startTime, c.endTime ,c.year, c.source, c.type, c.progress, c.stuCount, c.scope, c.owner, c.period, c.grades, c.subjects, c.classes, c.stuLists, ARRAY(SELECT p.id, p.code, p.name, p.blob, p.scope FROM p IN c.papers) AS papers FROM c WHERE ARRAY_CONTAINS({JsonSerializer.Serialize(examIdList)}, c.id)", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Exam-{id}") }))
  548. {
  549. var jsonex = await JsonDocument.ParseAsync(exam.ContentStream);
  550. if (jsonex.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  551. {
  552. dynamic examExtobj;
  553. foreach (var obj in jsonex.RootElement.GetProperty("Documents").EnumerateArray())
  554. {
  555. examExtobj = new ExpandoObject();
  556. string examId = obj.GetProperty("id").GetString();
  557. examExtobj.code = obj.GetProperty("code");
  558. examExtobj.id = examId;
  559. examExtobj.name = obj.GetProperty("name");
  560. examExtobj.createTime = obj.GetProperty("createTime");
  561. examExtobj.startTime = obj.GetProperty("startTime");
  562. examExtobj.endTime = obj.GetProperty("endTime");
  563. examExtobj.year = obj.GetProperty("year");
  564. examExtobj.source = obj.GetProperty("source");
  565. examExtobj.type = obj.GetProperty("type");
  566. examExtobj.progress = obj.GetProperty("progress");
  567. examExtobj.stuCount = obj.GetProperty("stuCount");
  568. examExtobj.scope = obj.GetProperty("scope");
  569. examExtobj.owner = obj.GetProperty("owner");
  570. examExtobj.period = obj.GetProperty("period");
  571. examExtobj.grades = obj.GetProperty("grades");
  572. examExtobj.subjects = obj.GetProperty("subjects");
  573. examExtobj.classes = (obj.TryGetProperty("classes", out JsonElement classes)) ? classes.ToObject<List<string>>() : new List<string>();
  574. examExtobj.stuLists = (obj.TryGetProperty("stuLists", out JsonElement stuLists)) ? stuLists.ToObject<List<string>>() : new List<string>();
  575. examExtobj.finishClasses = (examClassFinDic.ContainsKey(examId)) ? examClassFinDic[examId] : new List<string>();
  576. examExtobj.papers = obj.GetProperty("papers");
  577. //examExtobj = obj.ToObject<object>();
  578. exams.Add(examExtobj);
  579. }
  580. }
  581. }
  582. //用户在线记录
  583. try
  584. {
  585. _ = _httpTrigger.RequestHttpTrigger(new { school = defaultschool, scope = $"{Constant.ScopeTeacher}", id = $"{id}", ip = $"{ip}", expire = 1 }, _option.Location, "online-record");
  586. }
  587. catch { }
  588. //取得Teacher Blob 容器位置及SAS
  589. var container = _azureStorage.GetBlobContainerClient(id);
  590. await container.CreateIfNotExistsAsync(PublicAccessType.None); //嘗試創建Teacher私有容器,如存在則不做任何事,保障容器一定存在
  591. var (blob_uri, blob_sas) = _azureStorage.GetBlobContainerSAS(id, BlobContainerSasPermissions.Write | BlobContainerSasPermissions.Read | BlobContainerSasPermissions.List | BlobContainerSasPermissions.Delete);
  592. var (blob_uri_read, blob_sas_read) = _azureStorage.GetBlobContainerSAS(id, BlobContainerSasPermissions.Read);
  593. var (blob_uri_write, blob_sas_write) = _azureStorage.GetBlobContainerSAS(id, BlobContainerSasPermissions.Write);
  594. return Ok(new { blob_uri, blob_sas, blob_sas_read, blob_sas_write, schools, defaultschool, courses, exams });
  595. }
  596. catch (Exception ex)
  597. {
  598. await _dingDing.SendBotMsg($"IES5,{_option.Location},hiteach/GetTeacherInfo()\n{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  599. return BadRequest();
  600. }
  601. }
  602. [Authorize(Roles = "HiTeach")]
  603. [ProducesDefaultResponseType]
  604. [HttpPost("get-school-info")]
  605. public async Task<IActionResult> GetSchoolInfo(JsonElement request)
  606. {
  607. try
  608. {
  609. string id_token = HttpContext.GetXAuth("IdToken");
  610. (string ip, string region) = await LoginService.LoginIp(HttpContext, _searcher);
  611. if (string.IsNullOrEmpty(id_token)) return BadRequest();
  612. if (!request.TryGetProperty("school_code", out JsonElement school_code)) return BadRequest();
  613. var jwt = new JwtSecurityToken(id_token);
  614. if (!jwt.Payload.Iss.Equals("account.teammodel", StringComparison.Ordinal)) return BadRequest();
  615. var id = jwt.Payload.Sub;
  616. var client = _azureCosmos.GetCosmosClient();
  617. //取得學校學段、年級、科目、考試類型
  618. List<object> periods = new List<object>();
  619. List<object> grades = new List<object>();
  620. List<object> subjects = new List<object>();
  621. List<object> examTypes = new List<object>();
  622. School school_base = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>($"{school_code}", new PartitionKey("Base"));
  623. if (!string.IsNullOrWhiteSpace(school_base.id))
  624. {
  625. foreach(Period periodinfo in school_base.period)
  626. {
  627. periods.Add(new { id= periodinfo.id, name= periodinfo.name });
  628. int gradeIndex = 0;
  629. foreach (string gradeName in periodinfo.grades)
  630. {
  631. grades.Add(new { id = gradeIndex.ToString(), name = gradeName, periodId = periodinfo.id });
  632. gradeIndex++;
  633. }
  634. foreach (Subject subjectinfo in periodinfo.subjects)
  635. {
  636. subjects.Add(new { id = subjectinfo.id, name = subjectinfo.name, periodId = periodinfo.id });
  637. }
  638. foreach (var examType in periodinfo.analysis.type)
  639. {
  640. examTypes.Add(examType);
  641. }
  642. }
  643. }
  644. else //無此學校資料
  645. {
  646. return BadRequest();
  647. }
  648. //該老師排定的學校課程
  649. List<object> courses = new List<object>();
  650. List<string> classIds = new List<string>(); //班級ID列表 篩選校園評測用
  651. List<string> groupIds = new List<string>(); //團體ID列表 篩選校園評測用
  652. //取得所有班级
  653. (List<Class> school_classes, _) = await SchoolService.DoGraduateClasses(_httpTrigger, _azureCosmos, null, school_base, _option, _dingDing);
  654. //取得學校安排老師課程
  655. var query = $"SELECT DISTINCT c.id, c.name, c.scope, c.subject, c.period.id AS periodId, schedule.classId AS scheduleClassId, schedule.teacher AS scheduleTeacher, schedule.stulist AS scheduleStulist, schedule.notice AS scheduleNotice FROM c JOIN schedule IN c.schedule WHERE schedule.teacherId = '{id}'";
  656. //var query = $"SELECT c.id, c.name, c.teacher, cc.course, c.scope FROM c JOIN cc IN c.courses JOIN cct IN cc.teachers WHERE cct.id = '{id}'";
  657. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Course-{school_code}") }))
  658. {
  659. var jsoncs = await JsonDocument.ParseAsync(item.ContentStream);
  660. if (jsoncs.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  661. {
  662. foreach (var obj in jsoncs.RootElement.GetProperty("Documents").EnumerateArray())
  663. {
  664. dynamic classExtobj = new ExpandoObject();
  665. classExtobj.id = null;
  666. classExtobj.code = null;
  667. classExtobj.name = null;
  668. classExtobj.scope = null;
  669. classExtobj.gradeId = null;
  670. classExtobj.gradeName = null;
  671. classExtobj.year = 0;
  672. classExtobj.teacher = null;
  673. classExtobj.stuListId = null;
  674. classExtobj.stuCnt = 0;
  675. classExtobj.grpCnt = 0;
  676. //編制班
  677. string classIdNow = string.Empty;
  678. if (obj.TryGetProperty("scheduleClassId", out JsonElement scheduleClassId))
  679. {
  680. classIdNow = Convert.ToString(scheduleClassId);
  681. }
  682. if (!string.IsNullOrWhiteSpace(classIdNow))
  683. {
  684. Class classInfo = school_classes.Where((Class x) => x.id == classIdNow).FirstOrDefault();
  685. if(classInfo != null && !string.IsNullOrWhiteSpace(classInfo.id))
  686. {
  687. classIds.Add(classInfo.id);
  688. classExtobj.id = classInfo.id;
  689. classExtobj.code = classInfo.code;
  690. classExtobj.name = classInfo.name;
  691. classExtobj.year = classInfo.year;
  692. classExtobj.teacher = classInfo.teacher;
  693. //取得學生數
  694. var queryclstc = $"SELECT Count(1) AS stuCnt FROM c WHERE c.classId = '{classIdNow}'";
  695. await foreach (var itemclstc in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryStreamIterator(queryText: queryclstc, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-{school_code}") }))
  696. {
  697. var jsonclstc = await JsonDocument.ParseAsync(itemclstc.ContentStream);
  698. foreach (var objstc in jsonclstc.RootElement.GetProperty("Documents").EnumerateArray())
  699. {
  700. classExtobj.stuCnt = objstc.GetProperty("stuCnt").GetInt32();
  701. }
  702. }
  703. //取得分組數
  704. var queryclgp = $"SELECT c.groupId FROM c WHERE c.classId = '{classIdNow}' AND IS_NULL(c.groupId)=false GROUP BY c.groupId";
  705. await foreach (var itemgp in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryStreamIterator(queryText: queryclgp, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-{school_code}") }))
  706. {
  707. var jsongp = await JsonDocument.ParseAsync(itemgp.ContentStream);
  708. if (jsongp.RootElement.TryGetProperty("_count", out JsonElement gpcount) && gpcount.GetInt32() > 0)
  709. {
  710. classExtobj.grpCnt = gpcount.GetInt32();
  711. }
  712. }
  713. //取得年級
  714. if (obj.TryGetProperty("periodId", out JsonElement curPeriodIdJson))
  715. {
  716. string curPeriodId = curPeriodIdJson.GetString();
  717. Period curPeriod = school_base.period.Where((Period x) => x.id == curPeriodId).FirstOrDefault();
  718. if(curPeriodId != null)
  719. {
  720. var gradeInfo = getGradeInfoByYear(classInfo.year, curPeriod);
  721. if(!string.IsNullOrWhiteSpace(gradeInfo.id))
  722. {
  723. classExtobj.gradeId = gradeInfo.id;
  724. classExtobj.gradeName = gradeInfo.name;
  725. }
  726. }
  727. }
  728. }
  729. }
  730. //選課班
  731. var stuListId = obj.GetProperty("scheduleStulist");
  732. if (!stuListId.ValueKind.Equals(JsonValueKind.Null) && !string.IsNullOrWhiteSpace(stuListId.GetString()))
  733. {
  734. classExtobj.stuListId = Convert.ToString(stuListId);
  735. await foreach (var stuitem in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: $"SELECT c.code, c.id, c.name, c.year, c.tcount, c.scount FROM c WHERE c.id = '{stuListId}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"GroupList-{school_code}") }))
  736. {
  737. var jsonstu = await JsonDocument.ParseAsync(stuitem.ContentStream);
  738. foreach (var stuobj in jsonstu.RootElement.GetProperty("Documents").EnumerateArray())
  739. {
  740. groupIds.Add(Convert.ToString(stuobj.GetProperty("id")));
  741. classExtobj.id = Convert.ToString(stuobj.GetProperty("id"));
  742. classExtobj.code = Convert.ToString(stuobj.GetProperty("code"));
  743. classExtobj.name = Convert.ToString(stuobj.GetProperty("name"));
  744. classExtobj.gradeId = null;
  745. classExtobj.year = (stuobj.TryGetProperty("year", out JsonElement yearJson)) ? yearJson.GetInt32() : 0;
  746. classExtobj.teacher = null;
  747. classExtobj.stuCnt += stuobj.GetProperty("tcount").GetInt32();
  748. classExtobj.stuCnt += stuobj.GetProperty("scount").GetInt32();
  749. }
  750. }
  751. }
  752. //課程
  753. string courseIdNow = obj.GetProperty("id").ToString();
  754. var courseExist = courses.Where((dynamic x) => x.id == courseIdNow).FirstOrDefault();
  755. if (courseExist == null)
  756. {
  757. dynamic courseExtobj = new ExpandoObject();
  758. courseExtobj.id = courseIdNow;
  759. courseExtobj.name = obj.GetProperty("name").ToString();
  760. courseExtobj.scope = obj.GetProperty("scope").ToString();
  761. courseExtobj.classes = new List<object>();
  762. courseExtobj.subject = obj.GetProperty("subject");
  763. //classExtobj.teacher = scheduleTeacherInfo;
  764. if (!string.IsNullOrWhiteSpace(classExtobj.id))
  765. {
  766. courseExtobj.classes.Add(classExtobj);
  767. }
  768. courses.Add(courseExtobj);
  769. }
  770. else
  771. {
  772. //classExtobj.teacher = scheduleTeacherInfo;
  773. if (!string.IsNullOrWhiteSpace(classExtobj.id))
  774. {
  775. courseExist.classes.Add(classExtobj);
  776. }
  777. }
  778. }
  779. }
  780. }
  781. //取得校園評測 (HiTeach只取source='1'(課中评量) && progress = 'going'(進行中)) && 排定的學校課程班級
  782. List<object> exams = new List<object>();
  783. string classSqlString = "";
  784. if (classIds.Count > 0 || groupIds.Count > 0)
  785. {
  786. if (classIds.Count > 0)
  787. {
  788. foreach (string classId in classIds)
  789. {
  790. if (!string.IsNullOrWhiteSpace(classSqlString))
  791. {
  792. classSqlString += " OR ";
  793. }
  794. classSqlString += $"ARRAY_CONTAINS(c.classes, '{classId}')";
  795. }
  796. }
  797. if (groupIds.Count > 0)
  798. {
  799. foreach (string groupId in groupIds)
  800. {
  801. if (!string.IsNullOrWhiteSpace(classSqlString))
  802. {
  803. classSqlString += " OR ";
  804. }
  805. classSqlString += $"ARRAY_CONTAINS(c.classes, '{groupId}')";
  806. }
  807. }
  808. }
  809. else //無分配任何課程教室,校園評測不取
  810. {
  811. classSqlString += "c.id = '0'";
  812. }
  813. classSqlString = $"AND ({classSqlString})";
  814. //取得評測ID List
  815. List<string> examIdList = new List<string>();
  816. await foreach (var exam in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIterator(queryText: $"SELECT c.id FROM c WHERE c.source = '1' AND c.progress = 'going' {classSqlString}", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Exam-{school_code}") }))
  817. {
  818. using var json = await JsonDocument.ParseAsync(exam.ContentStream);
  819. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  820. {
  821. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  822. {
  823. examIdList.Add(obj.GetProperty("id").GetString());
  824. }
  825. }
  826. }
  827. //取得有作答的評測班級
  828. Dictionary<string, List<string>> examClassFinDic = new Dictionary<string, List<string>>();
  829. await foreach (var exam in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIterator(queryText: $"SELECT c.examId, c.info.id as classId FROM c WHERE ARRAY_CONTAINS({JsonSerializer.Serialize(examIdList)}, c.examId) AND c.progress=true", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"ExamClassResult-{school_code}") }))
  830. {
  831. var jsonecr = await JsonDocument.ParseAsync(exam.ContentStream);
  832. if (jsonecr.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  833. {
  834. foreach (var obj in jsonecr.RootElement.GetProperty("Documents").EnumerateArray())
  835. {
  836. string examId = obj.GetProperty("examId").ToString();
  837. string classId = obj.GetProperty("classId").ToString();
  838. if (examClassFinDic.ContainsKey(examId) && !examClassFinDic[examId].Contains(classId))
  839. {
  840. examClassFinDic[examId].Add(classId);
  841. }
  842. else
  843. {
  844. List<string> classIdList = new List<string>();
  845. classIdList.Add(classId);
  846. examClassFinDic.Add(examId, classIdList);
  847. }
  848. }
  849. }
  850. }
  851. //取得評測資料
  852. await foreach (var exam in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIterator(queryText: $"SELECT c.code, c.id, c.name, c.createTime, c.startTime, c.endTime ,c.year, c.source, c.type, c.progress, c.stuCount, c.scope, c.owner, c.period, c.grades, c.subjects, c.classes, c.stuLists, ARRAY(SELECT p.id, p.code, p.name, p.blob, p.scope FROM p IN c.papers) AS papers FROM c WHERE ARRAY_CONTAINS({JsonSerializer.Serialize(examIdList)}, c.id)", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Exam-{school_code}") }))
  853. {
  854. var jsonex = await JsonDocument.ParseAsync(exam.ContentStream);
  855. if (jsonex.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  856. {
  857. foreach (var obj in jsonex.RootElement.GetProperty("Documents").EnumerateArray())
  858. {
  859. dynamic examExtobj = new ExpandoObject();
  860. string examId = obj.GetProperty("id").GetString();
  861. examExtobj.code = obj.GetProperty("code");
  862. examExtobj.id = examId;
  863. examExtobj.name = obj.GetProperty("name");
  864. examExtobj.createTime = obj.GetProperty("createTime");
  865. examExtobj.startTime = obj.GetProperty("startTime");
  866. examExtobj.endTime = obj.GetProperty("endTime");
  867. examExtobj.year = obj.GetProperty("year");
  868. examExtobj.source = obj.GetProperty("source");
  869. examExtobj.type = obj.GetProperty("type");
  870. examExtobj.progress = obj.GetProperty("progress");
  871. examExtobj.stuCount = obj.GetProperty("stuCount");
  872. examExtobj.scope = obj.GetProperty("scope");
  873. examExtobj.owner = obj.GetProperty("owner");
  874. examExtobj.period = obj.GetProperty("period");
  875. examExtobj.grades = obj.GetProperty("grades");
  876. examExtobj.subjects = obj.GetProperty("subjects");
  877. examExtobj.classes = (obj.TryGetProperty("classes", out JsonElement classes)) ? classes.ToObject<List<string>>() : new List<string>();
  878. examExtobj.stuLists = (obj.TryGetProperty("stuLists", out JsonElement stuLists)) ? stuLists.ToObject<List<string>>() : new List<string>();
  879. examExtobj.finishClasses = (examClassFinDic.ContainsKey(examId)) ? examClassFinDic[examId] : new List<string>();
  880. examExtobj.papers = obj.GetProperty("papers");
  881. //examExtobj = obj.ToObject<object>();
  882. exams.Add(examExtobj);
  883. }
  884. }
  885. }
  886. //用户在线记录
  887. try
  888. {
  889. _ = _httpTrigger.RequestHttpTrigger(new { school = school_code.GetString(), scope = $"{Constant.ScopeTeacher}", id = $"{id}", ip = $"{ip}", expire = 1 }, _option.Location, "online-record");
  890. }
  891. catch { }
  892. //取得School Blob 容器位置及SAS
  893. string school_code_blob = school_code.GetString().ToLower();
  894. var (blob_uri, blob_sas) = _azureStorage.GetBlobContainerSAS(school_code_blob, BlobContainerSasPermissions.Read | BlobContainerSasPermissions.List | BlobContainerSasPermissions.Write); //讀列
  895. var (blob_uri_read, blob_sas_read) = _azureStorage.GetBlobContainerSAS(school_code_blob, BlobContainerSasPermissions.Read); //讀
  896. var (blob_uri_write, blob_sas_write) = _azureStorage.GetBlobContainerSAS(school_code_blob, BlobContainerSasPermissions.Write); //寫
  897. return Ok(new { blob_uri, blob_sas, blob_sas_read, blob_sas_write, periods, grades, subjects, courses, examTypes, exams });
  898. }
  899. catch (Exception ex)
  900. {
  901. await _dingDing.SendBotMsg($"IES5,{_option.Location},hiteach/GetSchoolInfo()\n{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  902. return BadRequest();
  903. }
  904. }
  905. //取得試卷
  906. [Authorize(Roles = "HiTeach")]
  907. [ProducesDefaultResponseType]
  908. [HttpPost("get-paper")]
  909. public async Task<IActionResult> GetPaperList(JsonElement request)
  910. {
  911. try
  912. {
  913. //Header驗證
  914. string id_token = HttpContext.GetXAuth("IdToken");
  915. if (string.IsNullOrEmpty(id_token)) return BadRequest();
  916. var jwt = new JwtSecurityToken(id_token);
  917. if (!jwt.Payload.Iss.Equals("account.teammodel", StringComparison.Ordinal)) return BadRequest();
  918. var id = jwt.Payload.Sub;
  919. var client = _azureCosmos.GetCosmosClient();
  920. //參數
  921. if (!request.TryGetProperty("grant_type", out JsonElement grant_type)) return BadRequest();
  922. string partitionid = string.Empty;
  923. string container = string.Empty;
  924. if (grant_type.ToString().Equals("school"))
  925. {
  926. if (!request.TryGetProperty("school_code", out JsonElement school_code_json))
  927. {
  928. return BadRequest();
  929. }
  930. else
  931. {
  932. partitionid = school_code_json.ToString();
  933. container = "School";
  934. }
  935. }
  936. else
  937. {
  938. partitionid = id.ToString();
  939. container = "Teacher";
  940. }
  941. //SQL文
  942. List<object> papers = new List<object>();
  943. string queryWhere = " WHERE 1=1 ";
  944. string queryOption = string.Empty;
  945. //學段
  946. if (request.TryGetProperty("periodId", out JsonElement periodId) && container.Equals("School"))
  947. {
  948. queryWhere += $" AND c.periodId = '{periodId}'";
  949. }
  950. //年級
  951. if (request.TryGetProperty("gradeId", out JsonElement gradeIds) && gradeIds.GetArrayLength() > 0 && container.Equals("School"))
  952. {
  953. string queryOptionForGrade = string.Empty;
  954. for (int i = 0; i < gradeIds.GetArrayLength(); i++)
  955. {
  956. if (!string.IsNullOrWhiteSpace(queryOptionForGrade))
  957. {
  958. queryOptionForGrade += " OR ";
  959. }
  960. queryOptionForGrade += $" ARRAY_CONTAINS(c.gradeIds, '{gradeIds[i]}', true)";
  961. }
  962. queryWhere += $" AND ( {queryOptionForGrade} )";
  963. }
  964. //科目ID
  965. if (request.TryGetProperty("subjectId", out JsonElement subjectId) && container.Equals("School"))
  966. {
  967. queryWhere += $" AND c.subjectId = '{subjectId}'";
  968. }
  969. //科目名稱
  970. if (request.TryGetProperty("subjectName", out JsonElement subjectName))
  971. {
  972. queryWhere += $" AND c.subjectName = '{subjectName}'";
  973. }
  974. //試卷ID
  975. if (request.TryGetProperty("id", out JsonElement paperId))
  976. {
  977. queryWhere += $" AND c.id = '{paperId}'";
  978. }
  979. int perpage = 0; //每頁幾條
  980. if (request.TryGetProperty("perpage", out JsonElement perpage_json)) perpage = perpage_json.GetInt32();
  981. int page = 0; //目前第幾頁
  982. if (request.TryGetProperty("page", out JsonElement page_json))
  983. {
  984. if (page_json.GetInt32() > 0)
  985. {
  986. page = page_json.GetInt32() - 1;
  987. }
  988. }
  989. string order = "createTime"; //排序項目
  990. if (request.TryGetProperty("order", out JsonElement order_json))
  991. {
  992. if (order_json.ToString().Equals("useCount"))
  993. {
  994. order = order_json.ToString();
  995. }
  996. }
  997. queryOption += $" Order By c." + order + " DESC ";
  998. if (perpage > 0)
  999. {
  1000. queryOption += $" OFFSET {perpage * page} LIMIT {perpage}";
  1001. }
  1002. //資料取得
  1003. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, container).GetItemQueryStreamIterator(queryText: $"SELECT c.code, c.id, c.periodId, c.gradeIds, c.subjectId, c.subjectName, c.name, REPLACE(c.blob, 'index.json', '') AS blob, c.score, c.useCount, ARRAY_LENGTH(c.scoring) AS itemCount, c.createTime, c.scope From c {queryWhere + queryOption}", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Paper-{partitionid}") }))
  1004. {
  1005. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  1006. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  1007. {
  1008. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  1009. {
  1010. papers.Add(obj.ToObject<object>());
  1011. }
  1012. }
  1013. }
  1014. //總件數
  1015. int totalCount = 0;
  1016. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, container).GetItemQueryStreamIterator(queryText: $"SELECT VALUE COUNT(1) From c {queryWhere}", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Paper-{partitionid}") }))
  1017. {
  1018. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  1019. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  1020. {
  1021. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  1022. {
  1023. totalCount = obj.GetInt32();
  1024. }
  1025. }
  1026. }
  1027. return Ok(new { papers, totalCount });
  1028. }
  1029. catch (Exception ex)
  1030. {
  1031. return BadRequest();
  1032. }
  1033. }
  1034. //取得試題
  1035. [Authorize(Roles = "HiTeach")]
  1036. [ProducesDefaultResponseType]
  1037. [HttpPost("get-item")]
  1038. public async Task<IActionResult> GetItemList(JsonElement request)
  1039. {
  1040. try
  1041. {
  1042. //Header驗證
  1043. string id_token = HttpContext.GetXAuth("IdToken");
  1044. if (string.IsNullOrEmpty(id_token)) return BadRequest();
  1045. var jwt = new JwtSecurityToken(id_token);
  1046. if (!jwt.Payload.Iss.Equals("account.teammodel", StringComparison.Ordinal)) return BadRequest();
  1047. var id = jwt.Payload.Sub;
  1048. var client = _azureCosmos.GetCosmosClient();
  1049. //參數
  1050. if (!request.TryGetProperty("grant_type", out JsonElement grant_type)) return BadRequest();
  1051. string partitionid = string.Empty;
  1052. string container = string.Empty;
  1053. if (grant_type.ToString().Equals("school"))
  1054. {
  1055. if (!request.TryGetProperty("school_code", out JsonElement school_code_json))
  1056. {
  1057. return BadRequest();
  1058. }
  1059. else
  1060. {
  1061. partitionid = school_code_json.ToString();
  1062. container = "School";
  1063. }
  1064. }
  1065. else
  1066. {
  1067. partitionid = id.ToString();
  1068. container = "Teacher";
  1069. }
  1070. //SQL文
  1071. List<object> items = new List<object>();
  1072. string queryWhere = " WHERE 1=1 ";
  1073. string queryOption = string.Empty;
  1074. //學段
  1075. if (request.TryGetProperty("periodId", out JsonElement periodId) && container.Equals("School"))
  1076. {
  1077. queryWhere += $" AND c.periodId = '{periodId}'";
  1078. }
  1079. //年級
  1080. if (request.TryGetProperty("gradeId", out JsonElement gradeIds) && gradeIds.GetArrayLength() > 0 && container.Equals("School"))
  1081. {
  1082. string queryOptionForGrade = string.Empty;
  1083. for (int i = 0; i < gradeIds.GetArrayLength(); i++)
  1084. {
  1085. if (!string.IsNullOrWhiteSpace(queryOptionForGrade))
  1086. {
  1087. queryOptionForGrade += " OR ";
  1088. }
  1089. queryOptionForGrade += $" ARRAY_CONTAINS(c.gradeIds, '{gradeIds[i]}', true)";
  1090. }
  1091. queryWhere += $" AND ( {queryOptionForGrade} )";
  1092. }
  1093. //科目ID
  1094. if (request.TryGetProperty("subjectId", out JsonElement subjectId) && container.Equals("school"))
  1095. {
  1096. queryWhere += $" AND c.subjectId = '{subjectId}'";
  1097. }
  1098. //科目名稱
  1099. if (request.TryGetProperty("subjectName", out JsonElement subjectName))
  1100. {
  1101. queryWhere += $" AND c.subjectName = '{subjectName}'";
  1102. }
  1103. //題型
  1104. int dummy = 0;
  1105. if (request.TryGetProperty("type", out JsonElement type))
  1106. {
  1107. queryWhere += $" AND c.type = '{type}'";
  1108. }
  1109. //難度
  1110. dummy = 0;
  1111. if (request.TryGetProperty("level", out JsonElement level) && int.TryParse(level.ToString(), out dummy))
  1112. {
  1113. queryWhere += $" AND c.level = {level}";
  1114. }
  1115. //層次
  1116. if (request.TryGetProperty("field", out JsonElement field) && int.TryParse(field.ToString(), out dummy))
  1117. {
  1118. queryWhere += $" AND c.field = {field}";
  1119. }
  1120. int perpage = 0; //每頁幾條
  1121. if (request.TryGetProperty("perpage", out JsonElement perpage_json)) perpage = perpage_json.GetInt32();
  1122. int page = 0; //目前第幾頁
  1123. if (request.TryGetProperty("page", out JsonElement page_json))
  1124. {
  1125. if (page_json.GetInt32() > 0)
  1126. {
  1127. page = page_json.GetInt32() - 1;
  1128. }
  1129. }
  1130. string order = "createTime"; //排序項目
  1131. if (request.TryGetProperty("order", out JsonElement order_json))
  1132. {
  1133. if (order_json.ToString().Equals("useCount"))
  1134. {
  1135. order = order_json.ToString();
  1136. }
  1137. }
  1138. queryOption += $" Order By c." + order + " DESC ";
  1139. if (perpage > 0)
  1140. {
  1141. queryOption += $" OFFSET {perpage * page} LIMIT {perpage}";
  1142. }
  1143. //資料取得
  1144. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, container).GetItemQueryStreamIterator(queryText: $"SELECT c.id, c.periodId, c.gradeIds, c.subjectId, c.subjectName, c.blob, c.field, c.level, c.type, c.useCount, c.createTime From c {queryWhere + queryOption}", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-{partitionid}") }))
  1145. {
  1146. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  1147. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  1148. {
  1149. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  1150. {
  1151. items.Add(obj.ToObject<object>());
  1152. }
  1153. }
  1154. }
  1155. //總件數
  1156. int totalCount = 0;
  1157. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, container).GetItemQueryStreamIterator(queryText: $"SELECT VALUE COUNT(1) From c {queryWhere}", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-{partitionid}") }))
  1158. {
  1159. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  1160. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  1161. {
  1162. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  1163. {
  1164. totalCount = obj.GetInt32();
  1165. }
  1166. }
  1167. }
  1168. return Ok(new { items, totalCount });
  1169. }
  1170. catch (Exception ex)
  1171. {
  1172. return BadRequest();
  1173. }
  1174. }
  1175. //取得知識點
  1176. [Authorize(Roles = "HiTeach")]
  1177. [ProducesDefaultResponseType]
  1178. [HttpPost("get-knowledge")]
  1179. public async Task<IActionResult> GetKnowledgePointList(JsonElement request)
  1180. {
  1181. var client = _azureCosmos.GetCosmosClient();
  1182. if (!request.TryGetProperty("school_code", out JsonElement school_code)) return BadRequest();
  1183. //知識點
  1184. List<object> points = new List<object>();
  1185. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: $"SELECT c.id, c.name, c.subjectId FROM c WHERE c.type = 'point'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Knowledge-{school_code}") }))
  1186. {
  1187. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  1188. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  1189. {
  1190. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  1191. {
  1192. points.Add(obj.ToObject<object>());
  1193. }
  1194. }
  1195. }
  1196. return Ok(points);
  1197. }
  1198. //取得課綱
  1199. [Authorize(Roles = "HiTeach")]
  1200. [ProducesDefaultResponseType]
  1201. [HttpPost("get-syllabus")]
  1202. public async Task<IActionResult> GetSyllabusList(JsonElement request)
  1203. {
  1204. //Header驗證
  1205. string id_token = HttpContext.GetXAuth("IdToken");
  1206. if (string.IsNullOrEmpty(id_token)) return BadRequest();
  1207. var jwt = new JwtSecurityToken(id_token);
  1208. if (!jwt.Payload.Iss.Equals("account.teammodel", StringComparison.OrdinalIgnoreCase)) return BadRequest();
  1209. var id = jwt.Payload.Sub;
  1210. //參數驗證
  1211. if (!request.TryGetProperty("grant_type", out JsonElement grant_type)) return BadRequest();
  1212. string grantType = (grant_type.GetString().Equals("school")) ? "school" : "private";
  1213. JsonElement school_code = new();
  1214. if (grantType == "school" && !request.TryGetProperty("school_code", out school_code)) return BadRequest();
  1215. string dataId = (grantType.Equals("school")) ? school_code.GetString() : id;
  1216. string container = (grantType.Equals("school")) ? "School" : "Teacher";
  1217. string periodId = (request.TryGetProperty("periodId", out JsonElement periodIdJobj)) ? periodIdJobj.GetString() : String.Empty;
  1218. string subjectId = (request.TryGetProperty("subjectId", out JsonElement subjectIdJobj)) ? subjectIdJobj.GetString() : String.Empty;
  1219. string volumeId = (request.TryGetProperty("volumeId", out JsonElement volumeIdJobj)) ? volumeIdJobj.GetString() : String.Empty;
  1220. string syllabusId = (request.TryGetProperty("syllabusId", out JsonElement syllabusIdJobj)) ? syllabusIdJobj.GetString() : String.Empty;
  1221. int display = (request.TryGetProperty("display", out JsonElement displayJobj)) ? displayJobj.GetInt32() : 0;
  1222. var client = _azureCosmos.GetCosmosClient();
  1223. //取得卷前置作業:有給syllabusId,取得該課綱的卷ID
  1224. string volumeIdFromSyllabusId = String.Empty;
  1225. if (!string.IsNullOrWhiteSpace(syllabusId))
  1226. {
  1227. await foreach (string volid in client.GetContainer(Constant.TEAMModelOS, container).GetItemQueryIterator<string>(queryText: $"SELECT value(c.volumeId) FROM c WHERE c.id = '{syllabusId}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Syllabus-{dataId}") }))
  1228. {
  1229. volumeIdFromSyllabusId = volid;
  1230. }
  1231. }
  1232. //取得卷
  1233. List<object> volumes = new();
  1234. List<Volume> volumeList = new();
  1235. List<string> volumeIdList = new();
  1236. string queryTextVol = string.Empty;
  1237. List<string> WhereVol = new();
  1238. if (!string.IsNullOrWhiteSpace(periodId)) WhereVol.Add($" c.periodId = '{periodId}' ");
  1239. if (!string.IsNullOrWhiteSpace(subjectId)) WhereVol.Add($" c.subjectId = '{subjectId}' ");
  1240. if (!string.IsNullOrWhiteSpace(volumeId)) WhereVol.Add($" c.id = '{volumeId}' ");
  1241. if (!string.IsNullOrWhiteSpace(volumeIdFromSyllabusId)) WhereVol.Add($" c.id = '{volumeIdFromSyllabusId}' ");
  1242. foreach (string Where in WhereVol)
  1243. {
  1244. queryTextVol += (String.IsNullOrWhiteSpace(queryTextVol)) ? $" WHERE {Where} " : $" AND {Where} ";
  1245. }
  1246. queryTextVol = "SELECT * FROM c " + queryTextVol;
  1247. await foreach (var itemv in client.GetContainer(Constant.TEAMModelOS, container).GetItemQueryStreamIterator(queryText: queryTextVol, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Volume-{dataId}") }))
  1248. {
  1249. var jsons = await JsonDocument.ParseAsync(itemv.ContentStream);
  1250. if (jsons.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  1251. {
  1252. foreach (var obj in jsons.RootElement.GetProperty("Documents").EnumerateArray())
  1253. {
  1254. Volume volExtobj = obj.ToObject<Volume>();
  1255. volumeList.Add(volExtobj);
  1256. volumeIdList.Add(volExtobj.id);
  1257. }
  1258. }
  1259. }
  1260. //取得課綱 ※display=1時不取任何課綱
  1261. List<SyllabusTreeNode> treeNodes = new();
  1262. if (!display.Equals(1))
  1263. {
  1264. string queryTextSyl = $"SELECT value(c) FROM c WHERE ARRAY_CONTAINS({JsonSerializer.Serialize(volumeIdList)}, c.volumeId, true)";
  1265. if (!string.IsNullOrWhiteSpace(syllabusId)) queryTextSyl += $" AND c.id = '{syllabusId}'";
  1266. await foreach (Syllabus items in client.GetContainer(Constant.TEAMModelOS, container).GetItemQueryIterator<Syllabus>(queryText: queryTextSyl, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Syllabus-{dataId}") }))
  1267. {
  1268. List<SyllabusTree> trees = SyllabusService.ListToTree(items.children);
  1269. SyllabusTreeNode tree = new() { id = items.id, scope = items.scope, trees = trees, volumeId = items.volumeId, auth = items.auth, codeval = $"{id}" };
  1270. treeNodes.Add(tree);
  1271. }
  1272. }
  1273. //輸出結果
  1274. foreach (Volume vr in volumeList)
  1275. {
  1276. volumes.Add(new
  1277. {
  1278. vr.periodId,
  1279. vr.subjectId,
  1280. vr.id,
  1281. vr.gradeId,
  1282. vr.semesterId,
  1283. vr.name,
  1284. vr.creatorId,
  1285. vr.creatorName,
  1286. vr.school,
  1287. vr.scope,
  1288. vr.syllabusIds,
  1289. vr.auth,
  1290. vr.order,
  1291. syllabus = treeNodes.Where(t => t.volumeId == vr.id).ToList()
  1292. });
  1293. }
  1294. return Ok(volumes);
  1295. }
  1296. //取得被分享的課綱
  1297. [ProducesDefaultResponseType]
  1298. [Authorize(Roles = "HiTeach")]
  1299. [HttpPost("get-share-syllabus")]
  1300. public async Task<IActionResult> GetShareSyllabusList(JsonElement request)
  1301. {
  1302. //Header驗證
  1303. string id_token = HttpContext.GetXAuth("IdToken");
  1304. if (string.IsNullOrEmpty(id_token)) return BadRequest();
  1305. var jwt = new JwtSecurityToken(id_token);
  1306. if (!jwt.Payload.Iss.Equals("account.teammodel", StringComparison.OrdinalIgnoreCase)) return BadRequest();
  1307. var id = jwt.Payload.Sub;
  1308. //參數驗證
  1309. string volumeId = (request.TryGetProperty("volumeId", out JsonElement volumeIdJobj)) ? volumeIdJobj.GetString() : String.Empty;
  1310. string syllabusId = (request.TryGetProperty("syllabusId", out JsonElement syllabusIdJobj)) ? syllabusIdJobj.GetString() : String.Empty;
  1311. int display = (request.TryGetProperty("display", out JsonElement displayJobj)) ? displayJobj.GetInt32() : 0;
  1312. var client = _azureCosmos.GetCosmosClient();
  1313. //取得分享內容
  1314. Dictionary<string, string> tmidDic = new Dictionary<string, string>();
  1315. List<Volume> volumeList = new List<Volume>();
  1316. StringBuilder queryText = new StringBuilder("SELECT value(c) FROM c WHERE c.type='share' ");
  1317. if (!string.IsNullOrWhiteSpace(volumeId))
  1318. {
  1319. queryText.Append($"AND c.volumeId='{volumeId}'");
  1320. }
  1321. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<Share>(queryText: queryText.ToString(),
  1322. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Share-share-{id}") }))
  1323. {
  1324. if(!string.IsNullOrWhiteSpace(syllabusId))
  1325. {
  1326. if(item.id.Equals(syllabusId))
  1327. {
  1328. Volume volumeExist = volumeList.Where((Volume v) => v.id == item.volumeId).FirstOrDefault();
  1329. if(volumeExist == null)
  1330. {
  1331. Volume volume = new Volume();
  1332. volume.id = item.volumeId;
  1333. volume.name = item.volumeName;
  1334. volume.creatorId = item.issuer;
  1335. volume.creatorName = item.issuerName;
  1336. volume.syllabusIds.Add(item.id);
  1337. volumeList.Add(volume);
  1338. var (blob_uri, blob_sas) = _azureStorage.GetBlobContainerSAS(item.issuer, BlobContainerSasPermissions.Read | BlobContainerSasPermissions.List);
  1339. if (!tmidDic.ContainsKey(item.issuer)) tmidDic.Add(item.issuer, blob_sas);
  1340. }
  1341. else
  1342. {
  1343. volumeExist.syllabusIds.Add(item.id);
  1344. }
  1345. }
  1346. }
  1347. else
  1348. {
  1349. Volume volumeExist = volumeList.Where((Volume v) => v.id == item.volumeId).FirstOrDefault();
  1350. if (volumeExist == null)
  1351. {
  1352. Volume volume = new Volume();
  1353. volume.id = item.volumeId;
  1354. volume.name = item.volumeName;
  1355. volume.creatorId = item.issuer;
  1356. volume.creatorName = item.issuerName;
  1357. volume.syllabusIds.Add(item.id);
  1358. volumeList.Add(volume);
  1359. var (blob_uri, blob_sas) = _azureStorage.GetBlobContainerSAS(item.issuer, BlobContainerSasPermissions.Read | BlobContainerSasPermissions.List);
  1360. if(!tmidDic.ContainsKey(item.issuer)) tmidDic.Add(item.issuer, blob_sas);
  1361. }
  1362. else
  1363. {
  1364. volumeExist.syllabusIds.Add(item.id);
  1365. }
  1366. }
  1367. }
  1368. //取得課綱並輸出回傳值
  1369. List<object> result = new List<object>();
  1370. foreach(Volume volTmp in volumeList)
  1371. {
  1372. List<SyllabusTreeNode> treeNodes = new();
  1373. if (!display.Equals(1))
  1374. {
  1375. string queryTextSyl = $"SELECT value(c) FROM c WHERE ARRAY_CONTAINS({JsonSerializer.Serialize(volTmp.syllabusIds)}, c.id, true)";
  1376. await foreach (Syllabus items in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<Syllabus>(queryText: queryTextSyl, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Syllabus-{volTmp.creatorId}") }))
  1377. {
  1378. List<SyllabusTree> trees = SyllabusService.ListToTree(items.children);
  1379. SyllabusTreeNode tree = new() { id = items.id, scope = items.scope, trees = trees, volumeId = items.volumeId, auth = items.auth, codeval = $"{volTmp.creatorId}" };
  1380. treeNodes.Add(tree);
  1381. }
  1382. }
  1383. result.Add(new
  1384. {
  1385. id = volTmp.id,
  1386. name = volTmp.name,
  1387. creatorId = volTmp.creatorId,
  1388. creatorName = volTmp.creatorName,
  1389. creatorSas = (tmidDic.ContainsKey(volTmp.creatorId)) ? tmidDic[volTmp.creatorId] : null,
  1390. syllabusIds = volTmp.syllabusIds,
  1391. syllabus = treeNodes.ToList(),
  1392. });
  1393. }
  1394. return Ok(result);
  1395. }
  1396. //取得某班級的學生成員
  1397. [Authorize(Roles = "HiTeach")]
  1398. [ProducesDefaultResponseType]
  1399. [HttpPost("get-students-list")]
  1400. public async Task<IActionResult> GetStudentsList(JsonElement request)
  1401. {
  1402. string id_token = HttpContext.GetXAuth("IdToken");
  1403. if (string.IsNullOrWhiteSpace(id_token)) return BadRequest();
  1404. var jwt = new JwtSecurityToken(id_token);
  1405. if (!jwt.Payload.Iss.Equals("account.teammodel", StringComparison.Ordinal)) return BadRequest();
  1406. var id = jwt.Payload.Sub;
  1407. if (!request.TryGetProperty("grant_type", out JsonElement grant_type)) return BadRequest();
  1408. request.TryGetProperty("class_code", out JsonElement class_code);
  1409. request.TryGetProperty("stulist_id", out JsonElement stulist_id);
  1410. string classId = Convert.ToString(class_code);
  1411. string stulist = Convert.ToString(stulist_id);
  1412. if (string.IsNullOrWhiteSpace(classId) && string.IsNullOrWhiteSpace(stulist)) return BadRequest();
  1413. request.TryGetProperty("school_code", out JsonElement school_code);
  1414. if (grant_type.GetString().Equals("school") && string.IsNullOrWhiteSpace(Convert.ToString(school_code))) return BadRequest();
  1415. var client = _azureCosmos.GetCosmosClient();
  1416. Dictionary<string, string> irsDic = new Dictionary<string, string>(); //key:學生ID或TMID value:irs號碼
  1417. string container = (grant_type.GetString().Equals("school")) ? "School" : "Teacher";
  1418. List<string> listids = new List<string>();
  1419. if (!string.IsNullOrWhiteSpace(stulist))
  1420. {
  1421. listids.Add(stulist);
  1422. }
  1423. else if (!string.IsNullOrWhiteSpace(classId))
  1424. {
  1425. listids.Add(classId);
  1426. }
  1427. (List<RMember> students, List<RGroupList> groupList) = await GroupListService.GetMemberByListids(_coreAPIHttpService, client, _dingDing, listids, $"{school_code}");
  1428. return Ok(new { students });
  1429. }
  1430. /// <summary>
  1431. /// 開始課堂(舊版,已被CreateLesson取代)
  1432. /// </summary>
  1433. /// <param name="request"></param>
  1434. /// <returns></returns>
  1435. [Authorize(Roles = "HiTeach")]
  1436. [ProducesDefaultResponseType]
  1437. [HttpPost("start-lesson")]
  1438. public async Task<IActionResult> StartLesson(JsonElement request)
  1439. {
  1440. //醍摩豆ID驗證
  1441. string teacherId = string.Empty;
  1442. string id_token = HttpContext.GetXAuth("IdToken");
  1443. if (!string.IsNullOrWhiteSpace(id_token))
  1444. {
  1445. var jwt = new JwtSecurityToken(id_token);
  1446. if (!jwt.Payload.Iss.Equals("account.teammodel", StringComparison.OrdinalIgnoreCase)) return BadRequest();
  1447. teacherId = jwt.Payload.Sub;
  1448. }
  1449. if (string.IsNullOrWhiteSpace(teacherId)) return BadRequest(); //無醍摩豆ID,BadRequest
  1450. //取得授課ID
  1451. string lesson_code = _snowflakeId.NextId().ToString();
  1452. //bool get_lesson_id = (string.IsNullOrWhiteSpace(Convert.ToString(lesson_id)) || Convert.ToString(lesson_id) != lesson_code) ? false : true;
  1453. //string tableName = "TeacherLesson";
  1454. return Ok(new { lesson_code });
  1455. }
  1456. //上傳評測結果
  1457. //錯誤代碼:error = 1001 message = "Paper blob copy failure."
  1458. // error = 1002 message = "Student answers blob upload failure."
  1459. [Authorize(Roles = "HiTeach")]
  1460. [ProducesDefaultResponseType]
  1461. [HttpPost("upd-exam-result")]
  1462. public async Task<IActionResult> UploadExamResult(JsonElement request)
  1463. {
  1464. try
  1465. {
  1466. string message = "";
  1467. int error = 0;
  1468. string id_token = HttpContext.GetXAuth("IdToken");
  1469. if (string.IsNullOrWhiteSpace(id_token)) return BadRequest();
  1470. var jwt = new JwtSecurityToken(id_token);
  1471. if (!jwt.Payload.Iss.Equals("account.teammodel", StringComparison.Ordinal)) return BadRequest();
  1472. var id = jwt.Payload.Sub;
  1473. if (!request.TryGetProperty("exam", out JsonElement exam)) return BadRequest();
  1474. if (!request.TryGetProperty("examClassResult", out JsonElement examClassResult)) return BadRequest();
  1475. bool blobUploaded = (request.TryGetProperty("blobUploaded", out JsonElement blobUploadedJson)) ? blobUploadedJson.GetBoolean() : false;
  1476. bool recordSwitch = (request.TryGetProperty("recordSwitch", out JsonElement recordSwitchJson)) ? recordSwitchJson.GetBoolean() : false;
  1477. //ExamInfo ExamInfoFromReq = exam.ToObject<ExamInfo>();
  1478. string strExam = JsonSerializer.Serialize(exam);
  1479. if (strExam.Contains("\"publish\":\"0\""))
  1480. {
  1481. strExam = strExam.Replace("\"publish\":\"0\"", "\"publish\":0");
  1482. }
  1483. ExamInfo ExamInfoFromReq = JsonSerializer.Deserialize<ExamInfo>(strExam);
  1484. string examId = ExamInfoFromReq.id;
  1485. string excode = ExamInfoFromReq.code;
  1486. //ExamInfo dbExamInfo = exam.ToObject<ExamInfo>();
  1487. ExamInfo dbExamInfo = JsonSerializer.Deserialize<ExamInfo>(strExam);
  1488. var queryex = $"SELECT * FROM c WHERE c.id = '{examId}'";
  1489. await foreach (var itemex in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIterator(queryText: queryex, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{excode}") }))
  1490. {
  1491. var jsonex = await JsonDocument.ParseAsync(itemex.ContentStream);
  1492. if (jsonex.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  1493. {
  1494. foreach (var obj in jsonex.RootElement.GetProperty("Documents").EnumerateArray())
  1495. {
  1496. string strExamDb = JsonSerializer.Serialize(obj);
  1497. if (strExamDb.Contains("\"publish\":\"0\""))
  1498. {
  1499. strExamDb = strExamDb.Replace("\"publish\":\"0\"", "\"publish\":0");
  1500. }
  1501. dbExamInfo = JsonSerializer.Deserialize<ExamInfo>(strExamDb);
  1502. //dbExamInfo = obj.ToObject<ExamInfo>();
  1503. }
  1504. }
  1505. }
  1506. if (string.IsNullOrWhiteSpace(dbExamInfo.id))
  1507. {
  1508. dbExamInfo = ExamInfoFromReq;
  1509. }
  1510. //Boolean isTestFlg = (_option.Location.Contains("Test") || _option.Location.Contains("Dep")) ? true : false;
  1511. //ExamInfo內容取得、調整 [2021-7-13 廢除,給予HiTeach學校Blob寫入權限,API不再對Blob做搬運]
  1512. //※規則 owner:"school" => 校園評測 "teacher" => 個人評測
  1513. //※規則 scope:"school" => 校本班級 "private" => 個人班級
  1514. //※規則 BLOB容器: scope:"school" => {學校ID}下 scope:"private" => {個人ID}下
  1515. string blobContainer = (!string.IsNullOrWhiteSpace(dbExamInfo.school) && dbExamInfo.scope.Equals("school")) ? dbExamInfo.school : id; //blob容器
  1516. //dbExamInfo.source = "1"; //評測來源固定為 1:課中評量(不應由API擅自變更)
  1517. //試卷List
  1518. List<Dictionary<string, string>> recordPaperInfo = new List<Dictionary<string, string>>();
  1519. int paperIndex = 0;
  1520. foreach (PaperSimple paperInfo in ExamInfoFromReq.papers)
  1521. {
  1522. string paperBlobPath = (!paperInfo.blob.EndsWith("/")) ? paperInfo.blob + "/" : paperInfo.blob;
  1523. paperBlobPath = (paperInfo.blob.StartsWith("/")) ? paperBlobPath.Remove(0, 1) : paperBlobPath;
  1524. string subjectId = dbExamInfo.subjects[paperIndex].id;
  1525. recordPaperInfo.Add(new Dictionary<string, string>() { { "id", paperInfo.id }, { "blob", paperBlobPath }, { "subjectId", subjectId }, { "itemcount", paperInfo.point.Count.ToString() } });
  1526. paperIndex++;
  1527. }
  1528. //取得課堂紀錄下的試卷資料(blob)、複製到評測紀錄下
  1529. bool paperDataCopyErrFlg = false; //試卷資料拷貝錯誤Flag true:拷貝錯誤 [2021-7-13 (測試站)不再對Blob做搬運 永為false]
  1530. //Blob搬運 (待HiTeach完善後刪除)
  1531. if (!blobUploaded)
  1532. {
  1533. foreach (Dictionary<string, string> recordPaperInfoDic in recordPaperInfo)
  1534. {
  1535. string targetScope = dbExamInfo.scope; //評測對象 school:校本班級 private:私人課程
  1536. var blobPrivateContainer = _azureStorage.GetBlobContainerClient(id);
  1537. string sourceBlobPath = recordPaperInfoDic["blob"];
  1538. string destBlobPath = $"exam/{dbExamInfo.id}/paper/{recordPaperInfoDic["subjectId"]}/"; //拷貝對象路徑 path:exam/{評測ID}/paper/{subjectID}/ ※2022-1-6 式樣變更[原]/paper/{試卷ID}/ [新]/paper/{subjectID}/
  1539. if (targetScope.Equals("school")) //校本
  1540. {
  1541. string schoolId = dbExamInfo.school;
  1542. var blobSchoolContainer = _azureStorage.GetBlobContainerClient(schoolId);
  1543. var sourceBlobContainer = (recordSwitch) ? blobSchoolContainer : blobPrivateContainer;
  1544. Azure.Pageable<BlobItem> sourceBlobs = sourceBlobContainer.GetBlobs(prefix: sourceBlobPath);
  1545. if (sourceBlobs.Count() > 0)
  1546. {
  1547. foreach (var blob in sourceBlobs)
  1548. {
  1549. var sourceFileBlob = sourceBlobContainer.GetBlobClient(blob.Name);
  1550. if (sourceFileBlob.Exists())
  1551. {
  1552. var sourceFileUri = sourceBlobContainer.GetBlobClient(blob.Name).Uri;
  1553. string fileName = blob.Name.Replace(sourceBlobPath, "");
  1554. string destBlobFilePath = $"{destBlobPath}{fileName}";
  1555. await blobSchoolContainer.GetBlobClient(destBlobFilePath).StartCopyFromUriAsync(sourceFileUri);
  1556. }
  1557. else
  1558. {
  1559. paperDataCopyErrFlg = true;
  1560. }
  1561. }
  1562. }
  1563. }
  1564. else //私人
  1565. {
  1566. var sourceBlobContainer = blobPrivateContainer;
  1567. Azure.Pageable<BlobItem> sourceBlobs = sourceBlobContainer.GetBlobs(prefix: sourceBlobPath);
  1568. if (sourceBlobs.Count() > 0)
  1569. {
  1570. foreach (var blob in sourceBlobs)
  1571. {
  1572. var sourceFileBlob = sourceBlobContainer.GetBlobClient(blob.Name);
  1573. if (sourceFileBlob.Exists())
  1574. {
  1575. var sourceFileUri = sourceBlobContainer.GetBlobClient(blob.Name).Uri;
  1576. string fileName = blob.Name.Replace(sourceBlobPath, "");
  1577. string destBlobFilePath = $"{destBlobPath}{fileName}";
  1578. await blobPrivateContainer.GetBlobClient(destBlobFilePath).StartCopyFromUriAsync(sourceFileUri);
  1579. }
  1580. else
  1581. {
  1582. paperDataCopyErrFlg = true;
  1583. }
  1584. }
  1585. }
  1586. }
  1587. //替換 Exam.papers.blob
  1588. PaperSimple paperInfoNow = dbExamInfo.papers.Where((PaperSimple p) => p.id == recordPaperInfoDic["id"]).FirstOrDefault();
  1589. string destBlobPathForDocument = (destBlobPath.EndsWith("/")) ? destBlobPath.Remove(destBlobPath.Length - 1, 1) : destBlobPath;
  1590. destBlobPathForDocument = (!destBlobPathForDocument.StartsWith("/")) ? "/" + destBlobPathForDocument : destBlobPathForDocument;
  1591. paperInfoNow.blob = destBlobPathForDocument;
  1592. }
  1593. }
  1594. //ExamClassResult內容調整
  1595. bool studentAnswerCopyErrFlg = false; //學生作答資料拷貝錯誤Flag true:拷貝錯誤 [2021-7-13 不再對Blob做搬運 永為false]
  1596. //Blob搬運 (待HiTeach完善後刪除)
  1597. if (!blobUploaded)
  1598. {
  1599. List<ExamClassResultStudentAnswerArrayOld> dbExamClassResultList = examClassResult.ToObject<List<ExamClassResultStudentAnswerArrayOld>>();
  1600. foreach (ExamClassResultStudentAnswerArrayOld examClassResultRow in dbExamClassResultList)
  1601. {
  1602. //以subjectId及classId(info.id)取得DB中的ExamClassResult
  1603. ExamClassResult examClassResultUpd = new ExamClassResult();
  1604. string exclcode = examClassResultRow.code;
  1605. string classId = examClassResultRow.info.id;
  1606. string subjectId = examClassResultRow.subjectId;
  1607. var querycr = $"SELECT * FROM c WHERE c.examId = '{examId}' AND c.info.id = '{classId}' AND c.subjectId = '{subjectId}'";
  1608. await foreach (var itemcr in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIterator(queryText: querycr, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{exclcode}") }))
  1609. {
  1610. var jsontcr = await JsonDocument.ParseAsync(itemcr.ContentStream);
  1611. if (jsontcr.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  1612. {
  1613. foreach (var obj in jsontcr.RootElement.GetProperty("Documents").EnumerateArray())
  1614. {
  1615. examClassResultUpd = obj.ToObject<ExamClassResult>();
  1616. examClassResultUpd.progress = examClassResultRow.progress;
  1617. examClassResultUpd.studentIds = examClassResultRow.studentIds;
  1618. examClassResultUpd.studentAnswers = new List<List<string>>();
  1619. examClassResultUpd.studentScores = examClassResultRow.studentScores;
  1620. examClassResultUpd.status = examClassResultRow.status;
  1621. examClassResultUpd.sum = examClassResultRow.sum;
  1622. }
  1623. }
  1624. }
  1625. //無法取得既有ExamClassResult,新建
  1626. if (string.IsNullOrWhiteSpace(examClassResultUpd.id))
  1627. {
  1628. examClassResultUpd.pk = examClassResultRow.pk;
  1629. examClassResultUpd.code = examClassResultRow.code;
  1630. examClassResultUpd.id = examClassResultRow.id;
  1631. examClassResultUpd.school = examClassResultRow.school;
  1632. examClassResultUpd.examId = examClassResultRow.examId;
  1633. examClassResultUpd.subjectId = examClassResultRow.subjectId;
  1634. examClassResultUpd.gradeId = examClassResultRow.gradeId;
  1635. examClassResultUpd.year = examClassResultRow.year;
  1636. examClassResultUpd.info = examClassResultRow.info;
  1637. examClassResultUpd.progress = examClassResultRow.progress;
  1638. examClassResultUpd.studentIds = examClassResultRow.studentIds;
  1639. examClassResultUpd.studentAnswers = new List<List<string>>(); //學生作答Blob位置
  1640. examClassResultUpd.studentScores = examClassResultRow.studentScores;
  1641. examClassResultUpd.status = examClassResultRow.status;
  1642. examClassResultUpd.scope = examClassResultRow.scope;
  1643. examClassResultUpd.sum = examClassResultRow.sum;
  1644. }
  1645. //examClassResult.studentAnswers (1)將學生答案上傳blob後轉換內容為blob路徑 //[2021-7-13 不再對Blob做搬運] (2)將學生答案放入examClassResult.ans
  1646. if (examClassResultRow.studentIds != null && examClassResultRow.studentIds.Count > 0 && examClassResultRow.studentAnswersArray != null && examClassResultRow.studentAnswersArray.Count > 0)
  1647. {
  1648. for (int i = 0; i < examClassResultRow.studentAnswersArray.Count; i++)
  1649. {
  1650. string studentId = examClassResultRow.studentIds[i];
  1651. string fileName = examId + "/" + subjectId + "/" + studentId;
  1652. string blob = fileName + "/" + "ans.json";
  1653. var uploadFileResult = await _azureStorage.GetBlobContainerClient(blobContainer).UploadFileByContainer(examClassResultRow.studentAnswersArray[i].ToJsonString(), "exam", blob, false);
  1654. if (string.IsNullOrWhiteSpace(uploadFileResult))
  1655. {
  1656. studentAnswerCopyErrFlg = true;
  1657. }
  1658. //studentAnswers
  1659. List<string> studenrAnswerRow = new List<string>();
  1660. studenrAnswerRow.Add(blob);
  1661. examClassResultUpd.studentAnswers.Add(studenrAnswerRow);
  1662. }
  1663. //examClassResult.ans 將學生答案放入
  1664. examClassResultUpd.ans = examClassResultRow.studentAnswersArray;
  1665. }
  1666. //批註欄位處理
  1667. Dictionary<string, string> paperStatusNow = recordPaperInfo.Where((Dictionary<string, string> p) => p.TryGetValue("subjectId", out string paperStatusSubjectId) && paperStatusSubjectId.Equals(subjectId)).FirstOrDefault();
  1668. int itemCount = Convert.ToInt32(paperStatusNow["itemcount"]);
  1669. if (examClassResultUpd.mark.Count != examClassResultUpd.studentIds.Count || (examClassResultUpd.mark.ElementAtOrDefault(0) != null && examClassResultUpd.mark[0].Count != itemCount)) //第一層:學生數不符 OR 第二層:第一位學生批註數!=試卷題數 => 批註格式不符
  1670. {
  1671. examClassResultUpd.mark = this.createEmptyMark(examClassResultUpd.studentIds.Count, itemCount);
  1672. }
  1673. //UPDATE ExamClassResult
  1674. var examClassResultResponse = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Common").UpsertItemAsync(examClassResultUpd, new PartitionKey(examClassResultUpd.code));
  1675. }
  1676. }
  1677. else
  1678. {
  1679. List<ExamClassResultStudentAnswerArray> dbExamClassResultList = examClassResult.ToObject<List<ExamClassResultStudentAnswerArray>>();
  1680. foreach (ExamClassResultStudentAnswerArray examClassResultRow in dbExamClassResultList)
  1681. {
  1682. //以subjectId及classId(info.id)取得DB中的ExamClassResult
  1683. ExamClassResult examClassResultUpd = new ExamClassResult();
  1684. string exclcode = examClassResultRow.code;
  1685. string classId = examClassResultRow.info.id;
  1686. string subjectId = examClassResultRow.subjectId;
  1687. var querycr = $"SELECT * FROM c WHERE c.examId = '{examId}' AND c.info.id = '{classId}' AND c.subjectId = '{subjectId}'";
  1688. await foreach (var itemcr in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIterator(queryText: querycr, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{exclcode}") }))
  1689. {
  1690. var jsontcr = await JsonDocument.ParseAsync(itemcr.ContentStream);
  1691. if (jsontcr.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  1692. {
  1693. foreach (var obj in jsontcr.RootElement.GetProperty("Documents").EnumerateArray())
  1694. {
  1695. examClassResultUpd = obj.ToObject<ExamClassResult>();
  1696. examClassResultUpd.progress = examClassResultRow.progress;
  1697. examClassResultUpd.studentIds = examClassResultRow.studentIds;
  1698. examClassResultUpd.studentAnswers = new List<List<string>>();
  1699. examClassResultUpd.studentScores = examClassResultRow.studentScores;
  1700. examClassResultUpd.status = examClassResultRow.status;
  1701. examClassResultUpd.sum = examClassResultRow.sum;
  1702. }
  1703. }
  1704. }
  1705. //無法取得既有ExamClassResult,新建
  1706. if (string.IsNullOrWhiteSpace(examClassResultUpd.id))
  1707. {
  1708. examClassResultUpd.pk = examClassResultRow.pk;
  1709. examClassResultUpd.code = examClassResultRow.code;
  1710. examClassResultUpd.id = examClassResultRow.id;
  1711. examClassResultUpd.school = examClassResultRow.school;
  1712. examClassResultUpd.examId = examClassResultRow.examId;
  1713. examClassResultUpd.subjectId = examClassResultRow.subjectId;
  1714. examClassResultUpd.gradeId = examClassResultRow.gradeId;
  1715. examClassResultUpd.year = examClassResultRow.year;
  1716. examClassResultUpd.info = examClassResultRow.info;
  1717. examClassResultUpd.progress = examClassResultRow.progress;
  1718. examClassResultUpd.studentIds = examClassResultRow.studentIds;
  1719. examClassResultUpd.studentAnswers = new List<List<string>>();
  1720. examClassResultUpd.studentScores = examClassResultRow.studentScores;
  1721. examClassResultUpd.status = examClassResultRow.status;
  1722. examClassResultUpd.scope = examClassResultRow.scope;
  1723. examClassResultUpd.sum = examClassResultRow.sum;
  1724. }
  1725. //學生作答Blob位置[2021-7-13 不再對Blob做搬運 學生答案直接寫入DB]
  1726. examClassResultUpd.studentAnswers = examClassResultRow.studentAnswersArray;
  1727. //學生作答答案 (從blob取得學生答案、放入ans欄位)
  1728. string targetScope = dbExamInfo.scope; //評測對象 school:校本班級 private:私人課程
  1729. var targetBlobContainer = _azureStorage.GetBlobContainerClient(id);
  1730. string blobCntr = id;
  1731. if (targetScope.Equals("school")) //校本
  1732. {
  1733. string schoolId = dbExamInfo.school;
  1734. blobCntr = schoolId;
  1735. }
  1736. targetBlobContainer = _azureStorage.GetBlobContainerClient(blobCntr);
  1737. if (examClassResultRow.studentAnswersArray.Count > 0)
  1738. {
  1739. foreach (List<string> studentAnswerBlobPath in examClassResultRow.studentAnswersArray)
  1740. {
  1741. string stuAnswerBlobPath = studentAnswerBlobPath.FirstOrDefault();
  1742. if (stuAnswerBlobPath != null)
  1743. {
  1744. StringBuilder builder = new StringBuilder();
  1745. builder.Append("exam").Append("/").Append(stuAnswerBlobPath);
  1746. var Download = await targetBlobContainer.GetBlobClient(builder.ToString()).DownloadAsync();
  1747. var json = await JsonDocument.ParseAsync(Download.Value.Content);
  1748. var Record = json.RootElement.ToObject<List<List<string>>>();
  1749. examClassResultUpd.ans.Add(Record);
  1750. }
  1751. }
  1752. }
  1753. //批註欄位處理
  1754. Dictionary<string, string> paperStatusNow = recordPaperInfo.Where((Dictionary<string, string> p) => p.TryGetValue("subjectId", out string subjectId) && subjectId.Equals(examClassResultUpd.subjectId)).FirstOrDefault();
  1755. int itemCount = Convert.ToInt32(paperStatusNow["itemcount"]);
  1756. if (examClassResultUpd.mark.Count != examClassResultUpd.studentIds.Count || (examClassResultUpd.mark.ElementAtOrDefault(0) != null && examClassResultUpd.mark[0].Count != itemCount)) //第一層:學生數不符 OR 第二層:第一位學生批註數!=試卷題數 => 批註格式不符
  1757. {
  1758. examClassResultUpd.mark = this.createEmptyMark(examClassResultUpd.studentIds.Count, itemCount);
  1759. }
  1760. //UPDATE ExamClassResult
  1761. var examClassResultResponse = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Common").UpsertItemAsync(examClassResultUpd, new PartitionKey(examClassResultUpd.code));
  1762. }
  1763. }
  1764. //UPDATE Exam
  1765. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Common").UpsertItemAsync(dbExamInfo, new PartitionKey(dbExamInfo.code));
  1766. //錯誤處理
  1767. if (paperDataCopyErrFlg) //試卷Blob拷貝失敗
  1768. {
  1769. error = 1001;
  1770. message = "Paper blob copy failure.";
  1771. }
  1772. else if (studentAnswerCopyErrFlg) //學生作答資料上傳blob失敗
  1773. {
  1774. error = 1002;
  1775. message = "Student answers blob upload failure.";
  1776. }
  1777. return Ok(new { error, message });
  1778. }
  1779. catch (CosmosException cex)
  1780. {
  1781. return BadRequest(cex.Message);
  1782. }
  1783. catch (StorageException bex)
  1784. {
  1785. return BadRequest(bex.Message);
  1786. }
  1787. catch (Exception ex)
  1788. {
  1789. return BadRequest(ex.Message);
  1790. }
  1791. }
  1792. /**
  1793. * 根据学年获取年级信息
  1794. * @param year 学年
  1795. * @param Period 学段資料
  1796. */
  1797. private ExamSimple getGradeInfoByYear(int year, Period curPeriod)
  1798. {
  1799. ExamSimple result = new ExamSimple();
  1800. if (year > 0)
  1801. {
  1802. DateTime date = DateTime.UtcNow;
  1803. int curYear = date.Year;
  1804. int month = date.Month;
  1805. Semester semesterStart = curPeriod.semesters.Where((Semester x) => x.start.Equals(1)).FirstOrDefault();
  1806. if (semesterStart != null)
  1807. {
  1808. if (month < semesterStart.month)
  1809. {
  1810. curYear--;
  1811. }
  1812. int gradeIndex = curYear - year;
  1813. if (gradeIndex >= curPeriod.grades.Count)
  1814. {
  1815. result.id = gradeIndex.ToString();
  1816. result.name = "graduated";
  1817. }
  1818. else
  1819. {
  1820. result.id = gradeIndex.ToString();
  1821. result.name = curPeriod.grades[gradeIndex];
  1822. }
  1823. }
  1824. }
  1825. return result;
  1826. }
  1827. private List<List<List<Details>>> createEmptyMark(int studentNum, int itemNum)
  1828. {
  1829. List<List<List<Details>>> mark = new List<List<List<Details>>>();
  1830. for (int i = 0; i < studentNum; i++)
  1831. {
  1832. List<List<Details>> markRow = new List<List<Details>>();
  1833. for (int j = 0; j < itemNum; j++)
  1834. {
  1835. markRow.Add(new List<Details>());
  1836. }
  1837. mark.Add(markRow);
  1838. }
  1839. return mark;
  1840. }
  1841. public class ClassStudents
  1842. {
  1843. public string id { get; set; }
  1844. public string name { get; set; }
  1845. public string no { get; set; }
  1846. public string schoolId { get; set; }
  1847. public string groupId { get; set; }
  1848. public string groupName { get; set; }
  1849. public string irs { get; set; }
  1850. public int type { get; set; } //类型 1 tmdid,2 student
  1851. public string nickname { get; set; }
  1852. }
  1853. public class ClassGroups
  1854. {
  1855. public ClassGroups()
  1856. {
  1857. studentIds = new List<string>();
  1858. }
  1859. public string id { get; set; }
  1860. public string name { get; set; }
  1861. public List<string> studentIds { get; set; }
  1862. }
  1863. //ExamClassResult 學生作答紀錄(舊式 正式站暫時用這個)
  1864. public class ExamClassResultStudentAnswerArrayOld : ExamClassResult
  1865. {
  1866. public List<List<List<string>>> studentAnswersArray { get; set; }
  1867. }
  1868. //ExamClassResult 學生作答紀錄
  1869. public class ExamClassResultStudentAnswerArray : ExamClassResult
  1870. {
  1871. public List<List<string>> studentAnswersArray { get; set; }
  1872. }
  1873. //get-teacher-info API輸出 schools
  1874. private class GetTeacherInfoApiSchool
  1875. {
  1876. public string schoolId { get; set; }
  1877. public string name { get; set; }
  1878. public string status { get; set; }
  1879. public string picture { get; set; }
  1880. }
  1881. //get-teacher-info輸出 courses
  1882. private class GetTeacherInfoApiCourse : GetTeacherInfoApiSimlpeBase
  1883. {
  1884. public string scope { get; set; }
  1885. public List<GetTeacherInfoApiCourseClass> classes { get; set; }
  1886. }
  1887. //get-teacher-info輸出 courses.classes
  1888. private class GetTeacherInfoApiCourseClass : GetTeacherInfoApiSimlpeBase
  1889. {
  1890. public string code { get; set; }
  1891. public GetTeacherInfoApiSimlpeBase teacher { get; set; }
  1892. public string scope { get; set; }
  1893. public string stuListId { get; set; }
  1894. public int stuCnt { get; set; }
  1895. public int grpCnt { get; set; }
  1896. public string gradeId { get; set; }
  1897. public int year { get; set; }
  1898. }
  1899. //get-teacher-info輸出 基本class(各class繼承用)
  1900. internal class GetTeacherInfoApiSimlpeBase
  1901. {
  1902. public string id { get; set; }
  1903. public string name { get; set; }
  1904. }
  1905. }
  1906. }