ClassRoomController.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. using Azure.Cosmos;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Options;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Dynamic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Text.Json;
  11. using System.Threading.Tasks;
  12. using TEAMModelOS.Models;
  13. using TEAMModelOS.SDK.DI;
  14. using TEAMModelOS.SDK.DI.AzureCosmos.Inner;
  15. using TEAMModelOS.SDK.Extension;
  16. using TEAMModelOS.SDK.Models;
  17. namespace TEAMModelOS.Controllers
  18. {
  19. [ProducesResponseType(StatusCodes.Status200OK)]
  20. [ProducesResponseType(StatusCodes.Status400BadRequest)]
  21. //[Authorize(Roles = "IES5")]
  22. [Route("school/classroom")]
  23. [ApiController]
  24. public class ClassroomController : ControllerBase
  25. {
  26. public readonly AzureCosmosFactory _azureCosmos;
  27. private readonly Option _option;
  28. private readonly DingDing _dingDing;
  29. public ClassroomController(AzureCosmosFactory azureCosmos, DingDing dingDing,
  30. IOptionsSnapshot<Option> option)
  31. {
  32. _azureCosmos = azureCosmos;
  33. _dingDing = dingDing;
  34. _option = option?.Value;
  35. }
  36. [ProducesDefaultResponseType]
  37. //[AuthToken(Roles = "Teacher")]
  38. [HttpPost("upsert")]
  39. public async ValueTask<IActionResult> Upsert(JsonElement requert)
  40. {
  41. //ResponseBuilder builder = ResponseBuilder.custom();
  42. //List<Student> students = null;
  43. Class classroom;
  44. if (!requert.TryGetProperty("classroom", out JsonElement room)) return BadRequest();
  45. if (!requert.TryGetProperty("option", out JsonElement option)) return BadRequest();
  46. if (!requert.TryGetProperty("school_code", out JsonElement school_code)) return BadRequest();
  47. try
  48. {
  49. classroom = room.ToObject<Class>();
  50. var client = _azureCosmos.GetCosmosClient();
  51. classroom.code = "Class-" + school_code.ToString();
  52. //students = await _azureCosmos.FindByDict<Student>(new Dictionary<string, object>() { { "classroomCode", classroom.id } });
  53. if (option.ToString().Equals("insert"))
  54. {
  55. if (classroom.scope.Equals("private"))
  56. {
  57. var response = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemStreamAsync(classroom.id, new PartitionKey($"Class-{school_code}"));
  58. if (response.Status == 200)
  59. {
  60. //classroom = await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School").UpsertItemAsync(requert, new PartitionKey($"Classroom-{classroom.code}"));
  61. return Ok(new { error = ResponseCode.DATA_EXIST, V = "班级编码已经存在!" });
  62. }
  63. else
  64. {
  65. //string code = classroom.code;
  66. //classroom.code = "Class-" + school_code;
  67. classroom = await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher").CreateItemAsync(classroom, new PartitionKey($"Class-{school_code}"));
  68. }
  69. }
  70. else
  71. {
  72. var response = await client.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(classroom.id, new PartitionKey($"Class-{school_code}"));
  73. if (response.Status == 200)
  74. {
  75. //classroom = await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School").UpsertItemAsync(requert, new PartitionKey($"Classroom-{classroom.code}"));
  76. return Ok(new { error = ResponseCode.DATA_EXIST, V = "班级编码已经存在!" });
  77. }
  78. else
  79. {
  80. //string code = classroom.code;
  81. //classroom.code = "Class-" + school_code;
  82. classroom = await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School").CreateItemAsync(classroom, new PartitionKey($"Class-{school_code}"));
  83. }
  84. }
  85. }
  86. else
  87. {
  88. if (classroom.scope.Equals("private"))
  89. {
  90. //Class own = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemAsync<Class>(classroom.id, new PartitionKey($"Class-{school_code}"));
  91. classroom = await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync(classroom, classroom.id, new PartitionKey($"Class-{school_code}"));
  92. }
  93. else
  94. {
  95. //检查该教室是否是老师创建的个人校本教室
  96. var response = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemStreamAsync(classroom.id, new PartitionKey($"Class-{school_code}"));
  97. if (response.Status == 200)
  98. {
  99. classroom = await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync(classroom, classroom.id, new PartitionKey($"Class-{school_code}"));
  100. }
  101. else
  102. {
  103. List<StudentSimple> studentSimples = new List<StudentSimple>();
  104. StringBuilder sql = new StringBuilder();
  105. sql.Append("select A0.id,A0.name,A0.no from c join A0 in c.students ");
  106. Dictionary<string, object> dict = new Dictionary<string, object>();
  107. dict.Add("scope", classroom.scope);
  108. dict.Add("id", classroom.id);
  109. AzureCosmosQuery cosmosDbQuery = SQLHelper.GetSQL(dict, sql);
  110. await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Class-{school_code}") }))
  111. {
  112. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  113. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  114. {
  115. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  116. {
  117. studentSimples.Add(obj.ToObject<StudentSimple>());
  118. }
  119. }
  120. }
  121. classroom.students = studentSimples;
  122. List<TeacherCourse> course = new List<TeacherCourse>();
  123. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryStreamIterator(queryText: $"select value(c) from c join A0 in c.classes where A0.id = '{classroom.id}'"))
  124. {
  125. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  126. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  127. {
  128. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  129. {
  130. course.Add(obj.ToObject<TeacherCourse>());
  131. }
  132. }
  133. }
  134. for (int i = 0; i < course.Count; i++)
  135. {
  136. bool flag = false;
  137. for (int j = 0; j < course[i].classes.Count; j++)
  138. {
  139. if (!course[i].classes[j].name.Equals(classroom.name))
  140. {
  141. flag = true;
  142. course[i].classes[j].name = classroom.name;
  143. //break;
  144. }
  145. if (!course[i].classes[j].teacher.id.Equals(classroom.teacher.id))
  146. {
  147. flag = true;
  148. course[i].classes[j].teacher.id = classroom.teacher.id;
  149. course[i].classes[j].teacher.name = classroom.teacher.name;
  150. //break;
  151. }
  152. }
  153. if (flag)
  154. {
  155. await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync(course[i], course[i].id, new PartitionKey($"{course[i].code}"));
  156. }
  157. }
  158. //string code = classroom.code.Substring(classroom.pk.Length + 1);
  159. //[Jeff] CourseManagement表廢除 刪除預定:以下CourseManagement更新程序
  160. var sresponse = await client.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(classroom.id, new PartitionKey($"CourseManagement-{school_code}"));
  161. if (sresponse.Status == 200)
  162. {
  163. using var json = await JsonDocument.ParseAsync(sresponse.ContentStream);
  164. CourseManagement classroom1 = json.ToObject<CourseManagement>();
  165. if (!classroom1.name.Equals(classroom.name))
  166. {
  167. classroom1.name = classroom.name;
  168. await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync(classroom1, classroom1.id, new PartitionKey($"{classroom1.code}"));
  169. }
  170. if (!string.IsNullOrEmpty(classroom1.teacher.id) && !string.IsNullOrEmpty(classroom.teacher.id) && !classroom1.teacher.id.Equals(classroom.teacher.id))
  171. {
  172. classroom1.teacher.name = classroom.teacher.name;
  173. classroom1.teacher.id = classroom.teacher.id;
  174. await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync(classroom1, classroom1.id, new PartitionKey($"{classroom1.code}"));
  175. }
  176. }
  177. ////[Jeff] CourseManagement表廢除 刪除預定:以下CourseManagement更新程序
  178. classroom = await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School").ReplaceItemAsync(classroom, classroom.id, new PartitionKey($"Class-{school_code}"));
  179. }
  180. }
  181. }
  182. return Ok(new { classroom });
  183. }
  184. catch (Exception ex)
  185. {
  186. await _dingDing.SendBotMsg($"OS,{_option.Location},class/Upsert()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  187. return BadRequest();
  188. }
  189. }
  190. [ProducesDefaultResponseType]
  191. //[AuthToken(Roles = "Teacher")]
  192. [HttpPost("upsert-group")]
  193. public async ValueTask<IActionResult> UpsertGroup(JsonElement requert)
  194. {
  195. try
  196. {
  197. Class classroom = new Class();
  198. if (!requert.TryGetProperty("classroom", out JsonElement room)) return BadRequest();
  199. //if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
  200. classroom = room.ToObject<Class>();
  201. var client = _azureCosmos.GetCosmosClient();
  202. var sresponse = await client.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(classroom.id, new PartitionKey($"{classroom.code}"));
  203. if (sresponse.Status == 200)
  204. {
  205. using var json = await JsonDocument.ParseAsync(sresponse.ContentStream);
  206. Class classroom1 = json.ToObject<Class>();
  207. classroom1.students = classroom.students;
  208. var response = await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync(classroom1, classroom1.id, new PartitionKey($"{classroom.code}"));
  209. }
  210. return Ok(new { classroom });
  211. }
  212. catch (Exception ex)
  213. {
  214. await _dingDing.SendBotMsg($"OS,{_option.Location},class/UpsertGroup()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  215. return BadRequest();
  216. }
  217. }
  218. [ProducesDefaultResponseType]
  219. //[AuthToken(Roles = "Teacher")]
  220. [HttpPost("find")]
  221. public async Task<IActionResult> Find(JsonElement requert)
  222. {
  223. //ResponseBuilder builder = ResponseBuilder.custom();
  224. if (!requert.TryGetProperty("school_code", out JsonElement school_code)) return BadRequest();
  225. try
  226. {
  227. var client = _azureCosmos.GetCosmosClient();
  228. List<object> classrooms = new List<object>();
  229. StringBuilder sql = new StringBuilder();
  230. sql.Append("select c.id,c.no,c.point,c.name,c.teacher,c.periodId,c.gradeId,c.sn,c.style,c.scope,c.type,c.code,c.openType,c.x,c.y,ARRAY_LENGTH(c.students) AS studCount from c ");
  231. Dictionary<string, object> dict = new Dictionary<string, object>();
  232. var emobj = requert.EnumerateObject();
  233. while (emobj.MoveNext())
  234. {
  235. dict[emobj.Current.Name] = emobj.Current.Value;
  236. }
  237. //处理code
  238. if (dict.TryGetValue("school_code", out object _))
  239. {
  240. dict.Remove("school_code");
  241. }
  242. AzureCosmosQuery cosmosDbQuery = SQLHelper.GetSQL(dict, sql);
  243. await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Class-{school_code}") }))
  244. {
  245. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  246. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  247. {
  248. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  249. {
  250. classrooms.Add(obj.ToObject<object>());
  251. }
  252. }
  253. }
  254. return Ok(new { classrooms });
  255. /*List<Classroom> sc = await _azureCosmos.FindByDict<Classroom>(request);
  256. return builder.Data(sc).build();*/
  257. }
  258. catch (Exception ex)
  259. {
  260. await _dingDing.SendBotMsg($"OS,{_option.Location},class/Find()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  261. return BadRequest();
  262. }
  263. }
  264. [ProducesDefaultResponseType]
  265. //[AuthToken(Roles = "Teacher")]
  266. [HttpPost("find-students")]
  267. public async Task<IActionResult> FindStudent(JsonElement requert)
  268. {
  269. //ResponseBuilder builder = ResponseBuilder.custom();
  270. if (!requert.TryGetProperty("school_code", out JsonElement school_code)) return BadRequest();
  271. if (!requert.TryGetProperty("scope", out JsonElement scope)) return BadRequest();
  272. try
  273. {
  274. var client = _azureCosmos.GetCosmosClient();
  275. List<object> classrooms = new List<object>();
  276. StringBuilder sql = new StringBuilder();
  277. Dictionary<string, object> dict = new Dictionary<string, object>();
  278. var emobj = requert.EnumerateObject();
  279. while (emobj.MoveNext())
  280. {
  281. dict[emobj.Current.Name] = emobj.Current.Value;
  282. }
  283. //处理code
  284. if (dict.TryGetValue("school_code", out object _))
  285. {
  286. dict.Remove("school_code");
  287. }
  288. if (scope.ToString().Equals("teacher", StringComparison.OrdinalIgnoreCase))
  289. {
  290. dict.Remove("scope");
  291. }
  292. sql.Append("select c.name,c.id,c.students,c.code from c ");
  293. AzureCosmosQuery cosmosDbQuery = SQLHelper.GetSQL(dict, sql);
  294. if (scope.ToString().Equals("school", StringComparison.OrdinalIgnoreCase))
  295. {
  296. await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Class-{school_code}") }))
  297. {
  298. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  299. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  300. {
  301. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  302. {
  303. classrooms.Add(obj.ToObject<object>());
  304. }
  305. }
  306. }
  307. }
  308. else
  309. {
  310. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryStreamIterator(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Class-{school_code}") }))
  311. {
  312. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  313. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  314. {
  315. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  316. {
  317. classrooms.Add(obj.ToObject<object>());
  318. }
  319. }
  320. }
  321. }
  322. return Ok(new { classrooms });
  323. /*List<Classroom> sc = await _azureCosmos.FindByDict<Classroom>(request);
  324. return builder.Data(sc).build();*/
  325. }
  326. catch (Exception ex)
  327. {
  328. await _dingDing.SendBotMsg($"OS,{_option.Location},class/FindStudent()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  329. return BadRequest();
  330. }
  331. }
  332. [ProducesDefaultResponseType]
  333. //[AuthToken(Roles = "Teacher")]
  334. [HttpPost("delete")]
  335. public async Task<IActionResult> Delete(JsonElement request)
  336. {
  337. if (!request.TryGetProperty("school_code", out JsonElement code)) return BadRequest();
  338. if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
  339. if (!request.TryGetProperty("scope", out JsonElement scope)) return BadRequest();
  340. try
  341. {
  342. //string school_code = code.ToString().Substring(6);
  343. string school_code = code.ToString();
  344. Class classroom = new Class();
  345. var client = _azureCosmos.GetCosmosClient();
  346. if (scope.ToString().Equals("school"))
  347. {
  348. var response = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemStreamAsync(id.ToString(), new PartitionKey($"Class-{code}"));
  349. if (response.Status == 200)
  350. {
  351. classroom = await client.GetContainer("TEAMModelOS", "Teacher").DeleteItemAsync<Class>(id.ToString(), new PartitionKey($"Class-{code}"));
  352. }
  353. else
  354. {
  355. List<TeacherCourse> classes = new List<TeacherCourse>();
  356. classroom = await client.GetContainer("TEAMModelOS", "School").DeleteItemAsync<Class>(id.ToString(), new PartitionKey($"Class-{school_code}"));
  357. await client.GetContainer("TEAMModelOS", "School").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"CourseManagement-{school_code}"));
  358. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryStreamIterator(queryText: $"select value(c) from c join A0 in c.classes where A0.id = '{id}'"))
  359. {
  360. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  361. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  362. {
  363. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  364. {
  365. classes.Add(obj.ToObject<TeacherCourse>());
  366. }
  367. }
  368. }
  369. for (int i = 0; i < classes.Count; i++)
  370. {
  371. bool flag = false;
  372. for (int j = 0; j < classes[i].classes.Count; j++)
  373. {
  374. if (classes[i].classes[j].id.Equals(id.ToString()))
  375. {
  376. classes[i].classes.Remove(classes[i].classes[j]);
  377. flag = true;
  378. }
  379. }
  380. if (flag)
  381. {
  382. await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync(classes[i], classes[i].id, new PartitionKey($"{classes[i].code}"));
  383. }
  384. }
  385. }
  386. }
  387. else
  388. {
  389. classroom = await client.GetContainer("TEAMModelOS", "Teacher").DeleteItemAsync<Class>(id.ToString(), new PartitionKey($"Class-{school_code}"));
  390. }
  391. return Ok(new { classroom });
  392. }
  393. catch (Exception ex)
  394. {
  395. await _dingDing.SendBotMsg($"OS,{_option.Location},class/Delete()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  396. return BadRequest();
  397. }
  398. //ResponseBuilder builder = ResponseBuilder.custom();
  399. /*List<IdPk> idPks = new List<IdPk>();
  400. if (request.TryGetProperty("id", out JsonElement id))
  401. {
  402. List<Classroom> sc = await _azureCosmos.FindByDict<Classroom>(request);
  403. if (sc.IsNotEmpty())
  404. {
  405. await _azureCosmos.DeleteAll<ClassStudent>(new Dictionary<string, object> { { "id", sc.Select(x => x.code).ToArray() } });
  406. idPks = await _azureCosmos.DeleteAll<Classroom>(sc);
  407. //builder.Data(idPks);
  408. }
  409. }
  410. else
  411. {
  412. return Ok(new { erro = ResponseCode.PARAMS_ERROR, V = "参数未定义!" });
  413. }
  414. return Ok(new { idPks });*/
  415. /*List<VoteRecord> sc = await _azureCosmos.FindByDict<VoteRecord>(request);
  416. await _azureCosmos.DeleteAll<VoteRecord>(sc);
  417. return Ok();*/
  418. //await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School").CreateItemAsync("", new PartitionKey($"Class-{code}"));
  419. }
  420. [ProducesDefaultResponseType]
  421. //[AuthToken(Roles = "Teacher")]
  422. [HttpPost("hiteach-link")]
  423. public async Task<IActionResult> HiteachLink(JsonElement request)
  424. {
  425. // 必要檢查
  426. if (!request.TryGetProperty("school_code", out JsonElement code)) return BadRequest();
  427. if (!request.TryGetProperty("linkList", out JsonElement links)) return BadRequest();
  428. try
  429. {
  430. if (links.GetArrayLength() > 0)
  431. {
  432. // [變數宣告]
  433. string school_code = code.ToString(); // 學校簡碼
  434. JsonElement.ArrayEnumerator linkLists = request.GetProperty("linkList").EnumerateArray(); // 需要修改的資料
  435. int link_length = links.GetArrayLength(); // 需要修改的筆數
  436. // [取得DB資料]
  437. var client = _azureCosmos.GetCosmosClient();
  438. var response = await client.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(school_code, new PartitionKey("Product"));
  439. if (response.Status == 200)
  440. {
  441. var json = await JsonDocument.ParseAsync(response.ContentStream);
  442. //軟體
  443. SchoolProduct schoolProductItem = json.ToObject<SchoolProduct>();
  444. foreach (var linkList in linkLists)
  445. {
  446. var uuidBoolean = linkList.TryGetProperty("uuid", out var a);
  447. var uuid = a.GetString();
  448. var uuid2Boolean = linkList.TryGetProperty("uuid2", out var b);
  449. var uuid2 = b.GetString();
  450. var classIdBoolean = linkList.TryGetProperty("classId", out var c);
  451. var classId = c.GetString();
  452. var idBoolean = linkList.TryGetProperty("id", out var d);
  453. var id = d.GetString();
  454. SerialInfoBaseWithdeviceBound updSerialInfo = schoolProductItem.serial.Where(s => s.id == id).FirstOrDefault();
  455. if (updSerialInfo != null)
  456. {
  457. deviceBound updDeviceBound = updSerialInfo.deviceBound.Where(d => d.uuid == uuid && d.uuid2 == uuid2).FirstOrDefault();
  458. updDeviceBound.classId = classId;
  459. }
  460. }
  461. await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<SchoolProduct>(schoolProductItem, school_code, new PartitionKey("Product"));
  462. }
  463. }
  464. return Ok(new { error = 0 });
  465. }
  466. catch (Exception ex)
  467. {
  468. return BadRequest();
  469. }
  470. }
  471. [ProducesDefaultResponseType]
  472. //[AuthToken(Roles = "Teacher")]
  473. [HttpPost("hiteach-unlink-classId")]
  474. public async Task<IActionResult> HiteachUnlinkByClassId(JsonElement request)
  475. {
  476. // 必要檢查
  477. if (!request.TryGetProperty("school_code", out JsonElement code)) return BadRequest();
  478. if (!request.TryGetProperty("classId", out JsonElement id)) return BadRequest();
  479. try
  480. {
  481. // [變數宣告]
  482. string school_code = code.ToString(); // 學校簡碼
  483. string classId = id.ToString(); // 教室ID
  484. // [取得DB資料]
  485. var client = _azureCosmos.GetCosmosClient();
  486. var response = await client.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(school_code, new PartitionKey("Product"));
  487. if (response.Status == 200)
  488. {
  489. var json = await JsonDocument.ParseAsync(response.ContentStream);
  490. //軟體
  491. SchoolProduct schoolProductItem = json.ToObject<SchoolProduct>();
  492. foreach (SerialInfoBaseWithdeviceBound updSerialInfo in schoolProductItem.serial)
  493. {
  494. if (updSerialInfo.deviceBound != null)
  495. {
  496. deviceBound updDeviceBound = updSerialInfo.deviceBound.Where(d => d.classId == classId).FirstOrDefault();
  497. if (updDeviceBound != null)
  498. {
  499. updDeviceBound.classId = null;
  500. }
  501. }
  502. }
  503. await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<SchoolProduct>(schoolProductItem, school_code, new PartitionKey("Product"));
  504. }
  505. return Ok(new { error = 0 });
  506. }
  507. catch (Exception ex)
  508. {
  509. return BadRequest();
  510. }
  511. }
  512. [ProducesDefaultResponseType]
  513. //[AuthToken(Roles = "Teacher")]
  514. [HttpPost("name")]
  515. public async Task<IActionResult> GetNameList(JsonElement request)
  516. {
  517. if (!request.TryGetProperty("ids", out JsonElement ids)) return BadRequest();
  518. if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  519. try
  520. {
  521. var client = _azureCosmos.GetCosmosClient();
  522. //List<string> id = ids.ToObject<List<string>>();
  523. string info = "";
  524. for (int i = 0; i < ids.GetArrayLength(); i++)
  525. {
  526. //ids.Add(id[i].ToJsonString());
  527. info += ids[i].ToJsonString() + ",";
  528. }
  529. List<object> ClassName = new List<object>();
  530. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryStreamIterator(
  531. queryText: $"select c.id,c.name from c where c.id in ({info[0..^1]})", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Class-{code}") }))
  532. {
  533. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  534. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  535. {
  536. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  537. {
  538. ClassName.Add(obj.ToObject<object>());
  539. }
  540. }
  541. }
  542. return Ok(new { ClassName });
  543. }
  544. catch (Exception ex)
  545. {
  546. await _dingDing.SendBotMsg($"OS,{_option.Location},class/name()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  547. return BadRequest();
  548. }
  549. }
  550. }
  551. }