ArtController.cs 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. using Azure.Core;
  2. using Azure.Cosmos;
  3. using DinkToPdf.Contracts;
  4. using DocumentFormat.OpenXml.Bibliography;
  5. using DocumentFormat.OpenXml.Office2010.Excel;
  6. using DocumentFormat.OpenXml.Office2013.Excel;
  7. using DocumentFormat.OpenXml.Office2016.Excel;
  8. using DocumentFormat.OpenXml.Spreadsheet;
  9. using DocumentFormat.OpenXml.Wordprocessing;
  10. using HTEXLib.COMM.Helpers;
  11. using HTEXLib.Helpers.ShapeHelpers;
  12. using MathNet.Numerics.Distributions;
  13. using MathNet.Numerics.RootFinding;
  14. using Microsoft.AspNetCore.Authorization;
  15. using Microsoft.AspNetCore.Http;
  16. using Microsoft.AspNetCore.Mvc;
  17. using Microsoft.Extensions.Configuration;
  18. using Microsoft.Extensions.Options;
  19. using NUnit.Framework;
  20. using OpenXmlPowerTools;
  21. using System;
  22. using System.Collections.Generic;
  23. using System.Linq;
  24. using System.Net;
  25. using System.Reflection;
  26. using System.Security.Policy;
  27. using System.Text;
  28. using System.Text.Json;
  29. using System.Threading.Tasks;
  30. using System.Web;
  31. using System.Xml.Linq;
  32. using TEAMModelOS.Controllers.Analysis;
  33. using TEAMModelOS.Filter;
  34. using TEAMModelOS.Models;
  35. using TEAMModelOS.SDK;
  36. using TEAMModelOS.SDK.DI;
  37. using TEAMModelOS.SDK.Extension;
  38. using TEAMModelOS.SDK.Models;
  39. using TEAMModelOS.SDK.Models.Cosmos;
  40. using TEAMModelOS.SDK.Models.Cosmos.Common;
  41. using Survey = TEAMModelOS.SDK.Models.Survey;
  42. namespace TEAMModelOS.Controllers.Common
  43. {
  44. [ProducesResponseType(StatusCodes.Status200OK)]
  45. [ProducesResponseType(StatusCodes.Status400BadRequest)]
  46. [Route("common/art")]
  47. [ApiController]
  48. public class ArtController : ControllerBase
  49. {
  50. private readonly AzureCosmosFactory _azureCosmos;
  51. private readonly SnowflakeId _snowflakeId;
  52. private readonly AzureServiceBusFactory _serviceBus;
  53. private readonly DingDing _dingDing;
  54. private readonly Option _option;
  55. private readonly AzureStorageFactory _azureStorage;
  56. private readonly AzureRedisFactory _azureRedis;
  57. private readonly IConverter _converter;
  58. public IConfiguration _configuration { get; set; }
  59. private readonly CoreAPIHttpService _coreAPIHttpService;
  60. public ArtController(IConverter converter, CoreAPIHttpService coreAPIHttpService, AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing,
  61. IOptionsSnapshot<Option> option, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, IConfiguration configuration)
  62. {
  63. _coreAPIHttpService = coreAPIHttpService;
  64. _azureCosmos = azureCosmos;
  65. _serviceBus = serviceBus;
  66. _snowflakeId = snowflakeId;
  67. _dingDing = dingDing;
  68. _option = option?.Value;
  69. _azureStorage = azureStorage;
  70. _azureRedis = azureRedis;
  71. _configuration = configuration;
  72. _converter = converter;
  73. }
  74. /// <summary>
  75. /// 保存艺术评价信息
  76. /// </summary>
  77. /// <param name="request"></param>
  78. /// <returns></returns>
  79. [ProducesDefaultResponseType]
  80. [AuthToken(Roles = "teacher,admin")]
  81. [HttpPost("save")]
  82. [Authorize(Roles = "IES")]
  83. public async Task<IActionResult> Save(JsonElement request)
  84. {
  85. try
  86. {
  87. if (!request.TryGetProperty("art", out JsonElement art)) return BadRequest();
  88. var client = _azureCosmos.GetCosmosClient();
  89. ArtEvaluation ae = art.ToObject<ArtEvaluation>();
  90. bool flag = false;
  91. ArtMusic music = new();
  92. if (request.TryGetProperty("ArtMusic", out JsonElement am))
  93. {
  94. music = am.ToObject<ArtMusic>();
  95. music.ttl = -1;
  96. music.code = "ArtMusic";
  97. flag = true;
  98. };
  99. var (userid, _, _, school) = HttpContext.GetAuthTokenInfo();
  100. string code = ae.school;
  101. ae.ttl = -1;
  102. ae.progress = "going";
  103. ae.creatorId = userid;
  104. if (!ae.owner.Equals("area"))
  105. {
  106. ae.owner = "school";
  107. ae.code = "Art-" + code;
  108. ae.scope = "school";
  109. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  110. ae.createTime = now;
  111. ae.publish = 0;
  112. }
  113. if (string.IsNullOrEmpty(ae.id))
  114. {
  115. ae.id = Guid.NewGuid().ToString();
  116. await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(ae, new PartitionKey($"{ae.code}"));
  117. if (flag)
  118. {
  119. music.id = ae.id;
  120. await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(music, new PartitionKey("ArtMusic"));
  121. }
  122. }
  123. else
  124. {
  125. if (flag)
  126. {
  127. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(music, music.id, new PartitionKey("ArtMusic"));
  128. }
  129. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(ae, ae.id, new PartitionKey($"{ae.code}"));
  130. }
  131. return Ok(new { ae });
  132. }
  133. catch (Exception ex)
  134. {
  135. await _dingDing.SendBotMsg($"OS,{_option.Location},art/save()\n{ex.Message}\n{ex.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
  136. return Ok(new { code = 500, msg = ex.Message });
  137. }
  138. }
  139. [ProducesDefaultResponseType]
  140. [AuthToken(Roles = "teacher,admin,student")]
  141. [HttpPost("update-state")]
  142. [Authorize(Roles = "IES")]
  143. public async Task<IActionResult> UpdateState(JsonElement request)
  144. {
  145. var client = _azureCosmos.GetCosmosClient();
  146. try
  147. {
  148. if (!request.TryGetProperty("id", out JsonElement stuId)) return BadRequest();
  149. if (!request.TryGetProperty("artId", out JsonElement artId)) return BadRequest();
  150. if (!request.TryGetProperty("isAnswer", out JsonElement isAnswer)) return BadRequest();
  151. var (userid, name, picture, school) = HttpContext.GetAuthTokenInfo();
  152. HttpContext.Items.TryGetValue("Scope", out object scope);
  153. int userType = $"{scope}".Equals(Constant.ScopeStudent) ? 2 : 1;
  154. StudentArtResult artResult = null;
  155. //long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  156. var res = await client.GetContainer(Constant.TEAMModelOS, "Student").ReadItemStreamAsync(stuId.ToString(), new PartitionKey($"ArtResult-{artId}"));
  157. if (res.Status == 200)
  158. {
  159. using var json = await JsonDocument.ParseAsync(res.ContentStream);
  160. artResult = json.ToObject<StudentArtResult>();
  161. //artResult.isAnswer = isAnswer.GetInt32();
  162. await client.GetContainer("TEAMModelOS", "Student").ReplaceItemAsync(artResult, artResult.id, new PartitionKey($"{artResult.code}"));
  163. }
  164. return Ok();
  165. }
  166. catch (Exception e)
  167. {
  168. return BadRequest(new { msg = e.Message });
  169. }
  170. }
  171. [ProducesDefaultResponseType]
  172. [AuthToken(Roles = "teacher,admin,student")]
  173. [HttpPost("upload")]
  174. [Authorize(Roles = "IES")]
  175. public async Task<IActionResult> Upload(ArtRecord request)
  176. {
  177. try
  178. {
  179. var client = _azureCosmos.GetCosmosClient();
  180. var (userid, name, picture, school) = HttpContext.GetAuthTokenInfo();
  181. HttpContext.Items.TryGetValue("Scope", out object scope);
  182. int userType = $"{scope}".Equals(Constant.ScopeStudent) ? 2 : 1;
  183. request.school = school;
  184. request.stuId = userid;
  185. request.code = "ArtRecord";
  186. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  187. request.createTime = now;
  188. ArtRecord record;
  189. StudentArtResult artResult;
  190. ArtEvaluation art;
  191. List<string> classIds = new();
  192. List<GroupListGrp> groups = await GroupListService.GetMemberInGroupList(_coreAPIHttpService, client, _dingDing, userid, userType, school, new List<string> { "class", "teach" });
  193. foreach (var grp in groups)
  194. {
  195. classIds.Add(grp.id);
  196. }
  197. /* if (string.IsNullOrEmpty(request.id))
  198. {
  199. request.id = Guid.NewGuid().ToString();
  200. record = await client.GetContainer("TEAMModelOS", "Student").CreateItemAsync(request, new PartitionKey($"{request.code}"));
  201. }
  202. else
  203. {
  204. record = await client.GetContainer("TEAMModelOS", "Student").ReplaceItemAsync(request, request.id, new PartitionKey($"{request.code}"));
  205. }*/
  206. string rId = string.Format("{0}{1}{2}", request.school, "-", userid);
  207. //首先根据大ID获取整个活动得内容
  208. var aresponse = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(request.artId, new PartitionKey($"Art-{school}"));
  209. if (aresponse.Status == 200)
  210. {
  211. using var json = await JsonDocument.ParseAsync(aresponse.ContentStream);
  212. art = json.ToObject<ArtEvaluation>();
  213. var response = await client.GetContainer("TEAMModelOS", "Student").ReadItemStreamAsync(rId, new PartitionKey($"ArtResult-{request.artId}"));
  214. if (response.Status == 200)
  215. {
  216. using var json_1 = await JsonDocument.ParseAsync(response.ContentStream);
  217. artResult = json_1.ToObject<StudentArtResult>();
  218. List<Attachment> files = new();
  219. files = request.attachments;
  220. //bool flage = artResult.results.Exists(a => a.taskId == request.acId);
  221. artResult.results.ForEach(a =>
  222. {
  223. if (a.taskId == request.acId)
  224. {
  225. a.files = files;
  226. }
  227. });
  228. await client.GetContainer("TEAMModelOS", "Student").ReplaceItemAsync(artResult, artResult.id, new PartitionKey($"{artResult.code}"));
  229. }
  230. else
  231. {
  232. artResult = new StudentArtResult
  233. {
  234. id = rId,
  235. pk = "ArtResult",
  236. code = $"ArtResult-{request.artId}",
  237. studentId = userid,
  238. picture = picture,
  239. studentName = name,
  240. school = school,
  241. userType = userType,
  242. artId = request.artId,
  243. classIds = classIds
  244. };
  245. foreach (var qIds in art.settings)
  246. {
  247. foreach (var task in qIds.task)
  248. {
  249. ArtQuotaResult quotaResult = new()
  250. {
  251. quotaId = qIds.id,
  252. quotaName = qIds.quotaname,
  253. quotaType = (int)task.type,
  254. subjectId = task.subject,
  255. taskId = task.acId
  256. };
  257. if (!string.IsNullOrEmpty(quotaResult.taskId))
  258. {
  259. if (quotaResult.taskId.Equals(request.acId))
  260. {
  261. quotaResult.files = request.attachments;
  262. }
  263. }
  264. artResult.results.Add(quotaResult);
  265. }
  266. }
  267. await client.GetContainer("TEAMModelOS", "Student").CreateItemAsync(artResult, new PartitionKey($"{artResult.code}"));
  268. }
  269. }
  270. return Ok(new { code = 200 });
  271. }
  272. catch (Exception ex)
  273. {
  274. await _dingDing.SendBotMsg($"OS,{_option.Location},art/upload()\n{ex.Message}\n{ex.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
  275. return Ok(new { code = 500, msg = ex.Message });
  276. }
  277. }
  278. [ProducesDefaultResponseType]
  279. [AuthToken(Roles = "teacher,admin,student")]
  280. [HttpPost("upload-all")]
  281. [Authorize(Roles = "IES")]
  282. public async Task<IActionResult> UploadAll(JsonElement element)
  283. {
  284. try
  285. {
  286. var client = _azureCosmos.GetCosmosClient();
  287. var (_, _, _, school) = HttpContext.GetAuthTokenInfo();
  288. if (!element.TryGetProperty("artId", out JsonElement artId)) return BadRequest();
  289. if (!element.TryGetProperty("classId", out JsonElement classId)) return BadRequest();
  290. if (!element.TryGetProperty("quotaId", out JsonElement quotaId)) return BadRequest();
  291. if (!element.TryGetProperty("acId", out JsonElement acId)) return BadRequest();
  292. if (!element.TryGetProperty("subject", out JsonElement subject)) return BadRequest();
  293. if (!element.TryGetProperty("stus", out JsonElement stus)) return BadRequest();
  294. //HttpContext.Items.TryGetValue("Scope", out object scope);
  295. //int userType = $"{scope}".Equals(Constant.ScopeStudent) ? 2 : 1;
  296. List<stuFiles> stuFiles = stus.ToObject<List<stuFiles>>();
  297. List<string> value = new List<string>();
  298. await foreach (var s in stuTask(stuFiles, client, school, artId.GetString(), classId.GetString(), quotaId.GetString(), acId.GetString(), subject.GetString()))
  299. {
  300. if (s.code == 1)
  301. {
  302. value.Add(s.value);
  303. }
  304. }
  305. if (value.Count > 0)
  306. {
  307. return Ok(new { code = 1, msg = "学生ID导入异常", value = value });
  308. }
  309. else
  310. {
  311. return Ok(new { code = 0 });
  312. }
  313. }
  314. catch (Exception ex)
  315. {
  316. await _dingDing.SendBotMsg($"OS,{_option.Location},art/uploadAll()\n{ex.Message}\n{ex.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
  317. return Ok(new { code = 500, msg = ex.Message });
  318. }
  319. }
  320. private async IAsyncEnumerable<(int code, string value)> stuTask(List<stuFiles> stuFiles, CosmosClient client, string school, string artId, string classId, string quotaId, string acId, string subject)
  321. {
  322. /* string queryScore = $" select c.id from c where c.artId ='{artId}' and c.quotaId = '{quotaId}' and c.acId = '{acId}' and c.subject = '{subject}' and c.classId = '{classId}'";
  323. List<string> ids = new();
  324. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, Constant.Student).GetItemQueryStreamIterator
  325. (queryText: queryScore, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("ArtRecord") }))
  326. {
  327. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  328. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  329. {
  330. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  331. {
  332. if (obj.TryGetProperty("id", out JsonElement subScore))
  333. {
  334. //string sId = obj.GetProperty("id").GetString();
  335. ids.Add(subScore.GetString());
  336. }
  337. }
  338. }
  339. }
  340. await client.GetContainer(Constant.TEAMModelOS, "Student").DeleteItemsAsync<ArtRecord>(ids, "ArtRecord");*/
  341. // await _azureStorage.GetBlobServiceClient().DeleteBlobs(_dingDing, school, new List<string> { $"art/{artId}" });
  342. foreach (var request in stuFiles)
  343. {
  344. string value = "";
  345. int code = 0;
  346. try
  347. {
  348. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  349. ArtRecord record = new()
  350. {
  351. school = school,
  352. stuId = request.stuId,
  353. artId = artId,
  354. classId = classId,
  355. quotaId = quotaId,
  356. acId = acId,
  357. subject = subject,
  358. createTime = now,
  359. code = "ArtRecord",
  360. attachments = request.attachments
  361. };
  362. StudentArtResult artResult;
  363. ArtEvaluation art;
  364. List<string> classIds = new();
  365. List<GroupListGrp> groups = await GroupListService.GetMemberInGroupList(_coreAPIHttpService, client, _dingDing, request.stuId, request.userType, school, new List<string> { "class", "teach" });
  366. foreach (var grp in groups)
  367. {
  368. classIds.Add(grp.id);
  369. }
  370. /*if (string.IsNullOrEmpty(record.id))
  371. {
  372. record.id = Guid.NewGuid().ToString();
  373. record = await client.GetContainer("TEAMModelOS", "Student").CreateItemAsync(record, new PartitionKey($"{record.code}"));
  374. }
  375. else
  376. {
  377. record = await client.GetContainer("TEAMModelOS", "Student").ReplaceItemAsync(record, record.id, new PartitionKey($"{record.code}"));
  378. }*/
  379. string rId = string.Format("{0}{1}{2}", record.school, "-", record.stuId);
  380. //首先根据大ID获取整个活动得内容
  381. var aresponse = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(artId, new PartitionKey($"Art-{school}"));
  382. if (aresponse.Status == 200)
  383. {
  384. using var json = await JsonDocument.ParseAsync(aresponse.ContentStream);
  385. art = json.ToObject<ArtEvaluation>();
  386. var response = await client.GetContainer("TEAMModelOS", "Student").ReadItemStreamAsync(rId, new PartitionKey($"ArtResult-{artId}"));
  387. if (response.Status == 200)
  388. {
  389. using var json_1 = await JsonDocument.ParseAsync(response.ContentStream);
  390. artResult = json_1.ToObject<StudentArtResult>();
  391. List<Attachment> files = new();
  392. files = request.attachments;
  393. //bool flage = artResult.results.Exists(a => a.taskId == request.acId);
  394. artResult.results.ForEach(a =>
  395. {
  396. if (a.taskId == acId)
  397. {
  398. a.files = files;
  399. }
  400. });
  401. await client.GetContainer("TEAMModelOS", "Student").ReplaceItemAsync(artResult, artResult.id, new PartitionKey($"{artResult.code}"));
  402. }
  403. else
  404. {
  405. artResult = new StudentArtResult
  406. {
  407. id = rId,
  408. pk = "ArtResult",
  409. code = $"ArtResult-{artId}",
  410. studentId = request.stuId,
  411. //picture = picture,
  412. studentName = request.name,
  413. school = school,
  414. userType = request.userType,
  415. artId = artId,
  416. classIds = classIds,
  417. };
  418. foreach (var qIds in art.settings)
  419. {
  420. foreach (var task in qIds.task)
  421. {
  422. ArtQuotaResult quotaResult = new()
  423. {
  424. quotaId = qIds.id,
  425. quotaName = qIds.quotaname,
  426. quotaType = (int)task.type,
  427. subjectId = task.subject,
  428. taskId = task.acId
  429. };
  430. if (!string.IsNullOrEmpty(quotaResult.taskId))
  431. {
  432. if (quotaResult.taskId.Equals(acId))
  433. {
  434. quotaResult.files = request.attachments;
  435. }
  436. }
  437. artResult.results.Add(quotaResult);
  438. }
  439. }
  440. await client.GetContainer("TEAMModelOS", "Student").CreateItemAsync(artResult, new PartitionKey($"{artResult.code}"));
  441. }
  442. }
  443. }
  444. catch (Exception e)
  445. {
  446. value = request.stuId;
  447. code = 1;
  448. }
  449. yield return (code, value);
  450. }
  451. }
  452. [ProducesDefaultResponseType]
  453. [AuthToken(Roles = "teacher,admin")]
  454. [HttpPost("delete")]
  455. [Authorize(Roles = "IES")]
  456. public async Task<IActionResult> Delete(JsonElement request)
  457. {
  458. try
  459. {
  460. //object userScope = null;
  461. //object _standard = null;
  462. if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
  463. if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  464. /* string standard = null;
  465. HttpContext?.Items?.TryGetValue("Scope", out userScope);
  466. if (userScope != null && $"{userScope}".Equals(Constant.ScopeTeacher))
  467. {
  468. HttpContext?.Items?.TryGetValue("Standard", out _standard);
  469. standard = _standard != null && string.IsNullOrEmpty($"{userScope}") ? _standard.ToString() : null;
  470. }*/
  471. var (userid, _, _, school) = HttpContext.GetAuthTokenInfo();
  472. var client = _azureCosmos.GetCosmosClient();
  473. //TODO 区级活动的联动删除评测活动
  474. var sresponse = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(id.ToString(), new PartitionKey($"Art-{code}"));
  475. if (sresponse.Status == 200)
  476. {
  477. using var json = await JsonDocument.ParseAsync(sresponse.ContentStream);
  478. ArtEvaluation art = json.ToObject<ArtEvaluation>();
  479. //必须是本人或者这个学校的管理者才能删除
  480. bool flag = false;
  481. if (art.creatorId == userid)
  482. {
  483. flag = true;
  484. }
  485. else
  486. {
  487. if (art.scope == "school" && art.school.Equals(school))
  488. {
  489. flag = true;
  490. }
  491. }
  492. /*try
  493. {
  494. (List<RMember> members, List<RGroupList> groups) = await GroupListService.GetMemberByListids(_coreAPIHttpService, client, _dingDing, null, art.school, null);
  495. await StatisticsService.DoChange(new TeacherTrainChange
  496. { standard = standard, tmdids = members.Select(x => x.id)?.ToList(), school = art.school, update = new HashSet<string> { StatisticsService.OfflineRecord }, statistics = 0 }, _azureCosmos);
  497. }
  498. catch (Exception ex)
  499. {
  500. await _dingDing.SendBotMsg($"OS,{_option.Location},art/delete()ex\n{ex.Message}\n{ex.StackTrace},\n", GroupNames.醍摩豆服務運維群組);
  501. }*/
  502. if (flag)
  503. {
  504. art.status = 404;
  505. foreach (var info in art.settings)
  506. {
  507. /* if (info.TryGetProperty("examId", out JsonElement eId)) {
  508. }*/
  509. foreach (var acs in info.task)
  510. {
  511. if (!string.IsNullOrEmpty(acs.acId))
  512. {
  513. if (acs.type == 1)
  514. {
  515. Azure.Response response = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(acs.acId, new PartitionKey($"Exam-{code}"));
  516. if (response.Status == 200)
  517. {
  518. ExamInfo data = JsonDocument.Parse(response.Content).RootElement.Deserialize<ExamInfo>();
  519. data.status = 404;
  520. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(data, data.id, new PartitionKey($"Exam-{code}"));
  521. }
  522. }
  523. }
  524. }
  525. }
  526. await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(art, art.id, new PartitionKey($"{art.code}"));
  527. //如果是区级活动删除 先查询该区下面所有的艺术评测ID集合
  528. if (art.publish == 0)
  529. {
  530. List<string> artIds = new();
  531. string sql = $"select c.id from c where c.pId = '{art.pId}'";
  532. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, Constant.Common).GetItemQueryStreamIterator
  533. (queryText: sql))
  534. {
  535. using var sjson = await JsonDocument.ParseAsync(item.ContentStream);
  536. if (sjson.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  537. {
  538. foreach (var obj in sjson.RootElement.GetProperty("Documents").EnumerateArray())
  539. {
  540. if (obj.TryGetProperty("id", out JsonElement subScore))
  541. {
  542. string sId = obj.GetProperty("id").GetString();
  543. artIds.Add(sId);
  544. }
  545. }
  546. }
  547. }
  548. foreach (string artId in artIds)
  549. {
  550. await client.GetContainer("TEAMModelOS", "Common").DeleteItemStreamAsync(artId, new PartitionKey("ArtMusic"));
  551. await client.GetContainer("TEAMModelOS", "Common").DeleteItemStreamAsync(artId, new PartitionKey("ArtExam"));
  552. }
  553. string queryScore = $" select c.id from c where c.artId in ({string.Join(",", artIds.Select(o => $"'{o}'"))})";
  554. List<string> ids = new();
  555. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, Constant.Student).GetItemQueryStreamIterator
  556. (queryText: queryScore, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("ArtRecord") }))
  557. {
  558. using var sjson = await JsonDocument.ParseAsync(item.ContentStream);
  559. if (sjson.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  560. {
  561. foreach (var obj in sjson.RootElement.GetProperty("Documents").EnumerateArray())
  562. {
  563. if (obj.TryGetProperty("id", out JsonElement subScore))
  564. {
  565. string sId = obj.GetProperty("id").GetString();
  566. ids.Add(sId);
  567. }
  568. }
  569. }
  570. }
  571. await client.GetContainer(Constant.TEAMModelOS, "Student").DeleteItemsAsync<ArtRecord>(ids, "ArtRecord");
  572. }
  573. else
  574. {
  575. string queryScore = $" select c.id from c where c.artId = '{art.id})'";
  576. List<string> ids = new();
  577. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, Constant.Student).GetItemQueryStreamIterator
  578. (queryText: queryScore, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("ArtRecord") }))
  579. {
  580. using var sjson = await JsonDocument.ParseAsync(item.ContentStream);
  581. if (sjson.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  582. {
  583. foreach (var obj in sjson.RootElement.GetProperty("Documents").EnumerateArray())
  584. {
  585. if (obj.TryGetProperty("id", out JsonElement subScore))
  586. {
  587. string sId = obj.GetProperty("id").GetString();
  588. ids.Add(sId);
  589. }
  590. }
  591. }
  592. }
  593. await client.GetContainer(Constant.TEAMModelOS, "Student").DeleteItemsAsync<ArtRecord>(ids, "ArtRecord");
  594. await client.GetContainer("TEAMModelOS", "Common").DeleteItemStreamAsync(art.id, new PartitionKey("ArtMusic"));
  595. }
  596. }
  597. }
  598. return Ok(new { id });
  599. }
  600. catch (Exception e)
  601. {
  602. await _dingDing.SendBotMsg($"OS,{_option.Location},art/delete()\n{e.Message}\n{e.StackTrace},\n", GroupNames.醍摩豆服務運維群組);
  603. return Ok(new { code = 500 });
  604. }
  605. }
  606. /// <param name="request"></param>
  607. /// <returns></returns>
  608. [ProducesDefaultResponseType]
  609. [Authorize(Roles = "IES")]
  610. [AuthToken(Roles = "teacher,admin")]
  611. [HttpPost("find")]
  612. public async Task<IActionResult> Find(JsonElement request)
  613. {
  614. try
  615. {
  616. if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  617. request.TryGetProperty("periodId", out JsonElement period);
  618. request.TryGetProperty("periodType", out JsonElement periodType);
  619. if (string.IsNullOrWhiteSpace($"{period}") && string.IsNullOrWhiteSpace($"{periodType}"))
  620. {
  621. return BadRequest();
  622. }
  623. var client = _azureCosmos.GetCosmosClient();
  624. StringBuilder stringBuilder = new($"select c.id,c.img,c.name,c.classes,c.code,c.type,c.startTime,c.endTime,c.presenter,c.topic,c.address,c.owner,c.progress from c where (c.status<>404 or IS_DEFINED(c.status) = false )");
  625. string continuationToken = string.Empty;
  626. string token = default;
  627. if (!string.IsNullOrWhiteSpace($"{period}") && !string.IsNullOrWhiteSpace($"{periodType}"))
  628. {
  629. stringBuilder.Append($" and (c.period.id = '{period}' or c.periodType = '{periodType}')");
  630. }
  631. if (string.IsNullOrWhiteSpace($"{period}") && !string.IsNullOrWhiteSpace($"{periodType}"))
  632. {
  633. stringBuilder.Append($" and c.periodType = '{periodType}' ");
  634. }
  635. if (!string.IsNullOrWhiteSpace($"{period}") && string.IsNullOrWhiteSpace($"{periodType}"))
  636. {
  637. stringBuilder.Append($" and c.period.id = '{period}' ");
  638. }
  639. stringBuilder.Append("order by c.createTime desc");
  640. //是否需要进行分页查询,默认不分页
  641. bool iscontinuation = false;
  642. if (request.TryGetProperty("token", out JsonElement token_1))
  643. {
  644. token = token_1.GetString();
  645. iscontinuation = true;
  646. };
  647. //默认不指定返回大小
  648. int? topcout = null;
  649. if (request.TryGetProperty("count", out JsonElement jcount))
  650. {
  651. if (!jcount.ValueKind.Equals(JsonValueKind.Undefined) && !jcount.ValueKind.Equals(JsonValueKind.Null) && jcount.TryGetInt32(out int data))
  652. {
  653. topcout = data;
  654. }
  655. }
  656. List<ArtEvaluation> arts = new();
  657. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: stringBuilder.ToString(), continuationToken: token, requestOptions: new QueryRequestOptions() { MaxItemCount = topcout, PartitionKey = new PartitionKey($"Art-{code}") }))
  658. {
  659. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  660. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  661. {
  662. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  663. {
  664. arts.Add(obj.ToObject<ArtEvaluation>());
  665. }
  666. }
  667. if (iscontinuation)
  668. {
  669. continuationToken = item.GetContinuationToken();
  670. break;
  671. }
  672. }
  673. arts = arts.Where((x, i) => arts.FindIndex(z => z.id == x.id) == i).ToList();
  674. return Ok(new { arts });
  675. }
  676. catch (Exception e)
  677. {
  678. await _dingDing.SendBotMsg($"OS,{_option.Location},art/find()\n{e.Message}\n{e.StackTrace}\n{e.StackTrace}", GroupNames.醍摩豆服務運維群組);
  679. return Ok(new { code = 500 });
  680. }
  681. }
  682. [ProducesDefaultResponseType]
  683. [Authorize(Roles = "IES")]
  684. [AuthToken(Roles = "teacher,admin,student")]
  685. [HttpPost("find-summary")]
  686. public async Task<IActionResult> FindSummary(JsonElement request)
  687. {
  688. try
  689. {
  690. if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
  691. if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  692. var client = _azureCosmos.GetCosmosClient();
  693. ArtEvaluation art = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<ArtEvaluation>(id.GetString(), new PartitionKey($"Art-{code}"));
  694. if (art.owner.Equals("area") && string.IsNullOrEmpty(art.pId))
  695. {
  696. }
  697. else
  698. {
  699. List<(string eId, string sId)> ids = new();
  700. var examId = art.settings.SelectMany(x => x.task).Where(c => c.type == 1).Select(z => new { z.acId, z.subject }).ToList();
  701. examId.ForEach(x =>
  702. {
  703. ids.Add((x.acId, x.subject));
  704. });
  705. List<(string scode, string sub, List<string> stu)> stuInfo = await getLostAsync(ids, client, art.school);
  706. List<string> stus = new();
  707. foreach (var (scode, sub, stu) in stuInfo)
  708. {
  709. if (stus.Count == 0)
  710. {
  711. stus = stus.Union(stu).ToList();
  712. }
  713. else
  714. {
  715. stus = stus.Intersect(stu).ToList();
  716. }
  717. LostStudent lostStudent = new()
  718. {
  719. code = scode,
  720. subject = sub,
  721. stu = stu.Count
  722. };
  723. art.lost.Add(lostStudent);
  724. }
  725. art.miss.Add(stus.Count);
  726. }
  727. //art.miss = stus.Count;
  728. //art.pass = 1;
  729. //await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync<ArtEvaluation>(art, art.id, new PartitionKey(art.code));
  730. ArtMusic music = new();
  731. ArtExam ae = new();
  732. if (art != null)
  733. {
  734. string queryArtExam = $" select value(c) from c where c.activityId = '{art.pId}' ";
  735. //List<ArtExam> aes = new List<ArtExam>();
  736. /* await foreach (var item in client.GetContainer(Constant.TEAMModelOS, Constant.Common).GetItemQueryIterator<ArtExam>
  737. (queryText: queryArtExam, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"ArtExam") }))
  738. {
  739. aes.Add(item);
  740. }*/
  741. if (!string.IsNullOrWhiteSpace(art.pId))
  742. {
  743. var response = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(art.pId, new PartitionKey("ArtMusic"));
  744. var sresponse = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(art.pId, new PartitionKey("ArtExam"));
  745. if (sresponse.Status == 200)
  746. {
  747. using var json = await JsonDocument.ParseAsync(sresponse.ContentStream);
  748. ae = json.ToObject<ArtExam>();
  749. }
  750. if (response.Status == 200)
  751. {
  752. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  753. music = json.ToObject<ArtMusic>();
  754. }
  755. }
  756. else
  757. {
  758. var response = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(art.id, new PartitionKey("ArtMusic"));
  759. if (response.Status == 200)
  760. {
  761. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  762. music = json.ToObject<ArtMusic>();
  763. }
  764. }
  765. (List<RMember> rmembers, List<RGroupList> groups) = await GroupListService.GetMemberByListids(_coreAPIHttpService, client, _dingDing, art.classes, art.school);
  766. var classes = art.classes.Select(c => new
  767. {
  768. id = c,
  769. groups.Where(g => g.id.Equals(c)).FirstOrDefault()?.name
  770. });
  771. rmembers.ForEach(x =>
  772. {
  773. groups.ForEach(z =>
  774. {
  775. if (z.members.Exists(y => y.id.Equals(x.id) && y.type == x.type))
  776. {
  777. x.groupListIds.Add(z.id);
  778. }
  779. });
  780. });
  781. List<ArtSubjectScore> subjectScores = new List<ArtSubjectScore>();
  782. art.subjects.ForEach(z => { subjectScores.Add(new ArtSubjectScore { subjectId = z.id, score = 0 }); });
  783. var students = rmembers.Select(z => new StudentArtResult
  784. {
  785. studentId = z.id,
  786. studentName = z.name,
  787. userType = z.type,
  788. classIds = z.groupListIds,
  789. school = z.schoolId,
  790. picture = z.picture,
  791. artId = $"{id}",
  792. id = $"{z.schoolId}-{z.id}",
  793. code = $"ArtResult-{id}",
  794. pk = "ArtResult",
  795. ttl = -1,
  796. subjectScores = subjectScores,
  797. }).ToList();
  798. //TODO 缺考人数的结算(多科评量检测,作业的提交)
  799. if (students.Any())
  800. {
  801. string query = $" select value c.id from c where c.id in({string.Join(",", students.Select(x => $"'{x.id}'"))}) ";
  802. List<string> list = new List<string>();
  803. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, Constant.Student).GetItemQueryIterator<string>
  804. (queryText: query, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"ArtResult-{id}") }))
  805. {
  806. list.Add(item);
  807. }
  808. students.RemoveAll(x => list.Contains(x.id));
  809. students.ForEach(x =>
  810. {
  811. art.settings.ForEach(a =>
  812. {
  813. a.task.ForEach(z =>
  814. {
  815. ArtQuotaResult quotaResult = new ArtQuotaResult
  816. {
  817. taskId = z.acId,
  818. subjectId = z.subject,
  819. quotaId = a.id,
  820. quotaType = z.type.Value,
  821. quotaName = a.quotaname
  822. };
  823. x.results.Add(quotaResult);
  824. });
  825. });
  826. });
  827. List<Task<ItemResponse<StudentArtResult>>> responses = new();
  828. students.ForEach(z =>
  829. {
  830. responses.Add(client.GetContainer(Constant.TEAMModelOS, Constant.Student).CreateItemAsync(z, new PartitionKey(z.code)));
  831. });
  832. if (responses.Count > 0)
  833. {
  834. await responses.TaskPage(10);
  835. }
  836. }
  837. if (!string.IsNullOrWhiteSpace(ae.id))
  838. {
  839. return Ok(new { art, classes, ae, music, count = rmembers.Count, code = 200 });
  840. }
  841. else
  842. {
  843. return Ok(new { art, classes, music, count = rmembers.Count, code = 200 });
  844. }
  845. }
  846. else
  847. {
  848. return Ok(new { art, code = 404 });
  849. }
  850. }
  851. catch (CosmosException ex) when (ex.Status == 404)
  852. {
  853. return Ok(new { code = 404 });
  854. }
  855. catch (Exception e)
  856. {
  857. await _dingDing.SendBotMsg($"OS,{_option.Location},art/FindSummary()\n{e.Message}\n{e.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
  858. return Ok(new { code = 500 });
  859. }
  860. }
  861. public static async Task<List<(string code, string subject, List<string> stus)>> getLostAsync(List<(string eId, string sId)> examIds, CosmosClient client, string code)
  862. {
  863. List<(string code, string sub, List<string> stu)> stuInfo = new();
  864. if (examIds.Any())
  865. {
  866. string examSql = $"select c.id,c.school,c.lostStu from c where c.id in ({string.Join(",", examIds.Select(o => $"'{o.eId}'"))})";
  867. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: examSql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Exam-{code}") }))
  868. {
  869. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  870. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  871. {
  872. var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  873. while (accounts.MoveNext())
  874. {
  875. JsonElement account = accounts.Current;
  876. List<string> lostStu = account.GetProperty("lostStu").ToObject<List<string>>();
  877. string id = account.GetProperty("id").GetString();
  878. string sub = examIds.Where(c => c.eId.Equals(id)).FirstOrDefault().sId;
  879. stuInfo.Add((account.GetProperty("school").GetString(), sub, lostStu));
  880. }
  881. }
  882. }
  883. }
  884. return stuInfo;
  885. }
  886. [ProducesDefaultResponseType]
  887. [Authorize(Roles = "IES")]
  888. [AuthToken(Roles = "teacher,admin,student")]
  889. [HttpPost("find-summary-by-student")]
  890. public async Task<IActionResult> findByStudent(JsonElement request)
  891. {
  892. try
  893. {
  894. if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
  895. if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  896. var client = _azureCosmos.GetCosmosClient();
  897. var (userid, _, _, school) = HttpContext.GetAuthTokenInfo();
  898. ArtEvaluation art = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<ArtEvaluation>(id.GetString(), new PartitionKey($"Art-{code}"));
  899. ArtMusic music = new();
  900. if (art != null)
  901. {
  902. List<StuActivity> stus = new();
  903. List<string> wIds = new();
  904. List<StudentArtResult> works = new();
  905. if (!string.IsNullOrWhiteSpace(art.pId))
  906. {
  907. var response = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(art.pId, new PartitionKey("ArtMusic"));
  908. if (response.Status == 200)
  909. {
  910. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  911. music = json.ToObject<ArtMusic>();
  912. }
  913. }
  914. else
  915. {
  916. var response = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(art.id, new PartitionKey("ArtMusic"));
  917. if (response.Status == 200)
  918. {
  919. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  920. music = json.ToObject<ArtMusic>();
  921. }
  922. }
  923. var taskType1 = art.settings.SelectMany(z => z.task).Where(t => t.type == 1);
  924. //wIds = art.settings.SelectMany(z => z.task).Where(t => t.type == 2).Select(a => a.acId).ToList();
  925. if (taskType1 != null && taskType1.Any())
  926. {
  927. string sql = $"select value c from c where c.id in ({string.Join(",", taskType1.Select(z => $"'{z.acId}'"))})";
  928. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, Constant.Student)
  929. .GetItemQueryIterator<StuActivity>(sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Activity-{school}-{userid}") }))
  930. {
  931. stus.Add(item);
  932. }
  933. }
  934. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<StudentArtResult>(
  935. queryText: $"select value(c) from c where c.id = '{code}-{userid}'",
  936. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"ArtResult-{id}") }))
  937. {
  938. works.Add(item);
  939. }
  940. return Ok(new { art, stus, works, music, code = 200 });
  941. }
  942. else
  943. {
  944. return Ok(new { art, code = 404 });
  945. }
  946. }
  947. catch (CosmosException ex) when (ex.Status == 404)
  948. {
  949. return Ok(new { code = 404 });
  950. }
  951. catch (Exception e)
  952. {
  953. await _dingDing.SendBotMsg($"OS,{_option.Location},art/find-summary-by-student()\n{e.Message}\n{e.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
  954. return Ok(new { code = 500 });
  955. }
  956. }
  957. [ProducesDefaultResponseType]
  958. [Authorize(Roles = "IES")]
  959. [AuthToken(Roles = "teacher,admin")]
  960. [HttpPost("find-summary-by-work")]
  961. public async Task<IActionResult> findBywork(JsonElement request)
  962. {
  963. try
  964. {
  965. if (!request.TryGetProperty("classId", out JsonElement classId)) return BadRequest();
  966. if (!request.TryGetProperty("subject", out JsonElement subject)) return BadRequest();
  967. //艺术评测ID
  968. if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
  969. if (!request.TryGetProperty("taskId", out JsonElement taskId)) return BadRequest();
  970. string token = HttpContext.GetXAuth("authtoken");
  971. var client = _azureCosmos.GetCosmosClient();
  972. //var (userid, _, _, school) = HttpContext.GetAuthTokenInfo();
  973. List<StudentArtResult> artResults = new();
  974. string sql = $"select value(c) from c where c.artId = '{id}' and array_contains(c.classIds,'{classId}') ";
  975. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryStreamIterator(
  976. queryText: sql,
  977. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"ArtResult-{id}") }))
  978. {
  979. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  980. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  981. {
  982. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  983. {
  984. artResults.Add(obj.ToObject<StudentArtResult>());
  985. }
  986. }
  987. }
  988. artResults.ForEach(x => x.results.RemoveAll(z => !string.IsNullOrWhiteSpace(z.subjectId) && !subject.GetString().Equals(z.subjectId)));
  989. artResults.ForEach(x => x.subjectScores.RemoveAll(z => !string.IsNullOrWhiteSpace(z.subjectId) && !subject.GetString().Equals(z.subjectId)));
  990. List<(string stuId, string url)> zyUrl = new();
  991. foreach (StudentArtResult artResult in artResults)
  992. {
  993. if (!string.IsNullOrWhiteSpace(artResult.zyanswer.thirdAnswerId))
  994. {
  995. var data = new { busType = "aqd", artResult.zyanswer.thirdAnswerId };
  996. //byte[] inputBytes = Encoding.UTF8.GetBytes(artResult.zyanswer.thirdAnswerId);
  997. //string base64Str = Convert.ToBase64String(inputBytes);
  998. var base64Str = HttpUtility.UrlEncode(Convert.ToBase64String(Encoding.UTF8.GetBytes(data.ToJsonString())));
  999. //var date = Convert.FromBase64String(base64Str);
  1000. StringBuilder url = new("https://amesopen.aimusic.art?appid=8a68f563f3384662acbc268336b98ae2");
  1001. url.Append($"&data={base64Str}");
  1002. url.Append($"&thirdToken={token}");
  1003. zyUrl.Add((artResult.studentId, url.ToString()));
  1004. }
  1005. else
  1006. {
  1007. zyUrl.Add((artResult.studentId, string.Empty));
  1008. }
  1009. }
  1010. //if (string.IsNullOrWhiteSpace(artResult.zyanswer.thirdAnswerId)) { }
  1011. if (subject.GetString().Equals("subject_music"))
  1012. {
  1013. var works = artResults.Select(x => new
  1014. {
  1015. stuId = x.studentId,
  1016. x.studentName,
  1017. x.classIds,
  1018. x.artId,
  1019. isAnswer = string.IsNullOrWhiteSpace(x.zyanswer.thirdAnswerId) ? 0 : 1,
  1020. attachments = x.results.Where(c => c.taskId.Equals(taskId.GetString())).FirstOrDefault().files,
  1021. zyUrl.Where(c => c.stuId.Equals(x.studentId)).FirstOrDefault().url
  1022. });
  1023. return Ok(new { works, code = 200 });
  1024. }
  1025. else {
  1026. var works = artResults.Select(x => new
  1027. {
  1028. stuId = x.studentId,
  1029. x.studentName,
  1030. x.classIds,
  1031. x.artId,
  1032. isAnswer = string.IsNullOrWhiteSpace(x.zyanswer.thirdAnswerId) ? 0 : 1,
  1033. attachments = x.results.Where(c => c.taskId.Equals(taskId.GetString())).FirstOrDefault().files,
  1034. });
  1035. return Ok(new { works, code = 200 });
  1036. }
  1037. }
  1038. catch (CosmosException ex) when (ex.Status == 404)
  1039. {
  1040. return Ok(new { code = 404 });
  1041. }
  1042. catch (Exception e)
  1043. {
  1044. await _dingDing.SendBotMsg($"OS,{_option.Location},art/find-summary-by-work()\n{e.Message}\n{e.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
  1045. return Ok(new { code = 500 });
  1046. }
  1047. }
  1048. /// <param name="request"></param>
  1049. /// <returns></returns>
  1050. [ProducesDefaultResponseType]
  1051. [Authorize(Roles = "IES")]
  1052. [AuthToken(Roles = "teacher,admin")]
  1053. [HttpPost("find-by-teacher")]
  1054. public async Task<IActionResult> FindByTeacher(JsonElement request)
  1055. {
  1056. try
  1057. {
  1058. if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  1059. if (!request.TryGetProperty("tId", out JsonElement tId)) return BadRequest();
  1060. var client = _azureCosmos.GetCosmosClient();
  1061. var query = $"select c.id,c.img,c.name,c.startTime,c.type,c.endTime,c.presenter,c.topic,c.address,c.owner from c join A0 in c.teachers where A0.id = '{tId}'";
  1062. List<object> arts = new();
  1063. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Art-{code}") }))
  1064. {
  1065. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  1066. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  1067. {
  1068. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  1069. {
  1070. arts.Add(obj.ToObject<object>());
  1071. }
  1072. }
  1073. }
  1074. return Ok(new { arts });
  1075. }
  1076. catch (Exception e)
  1077. {
  1078. await _dingDing.SendBotMsg($"OS,{_option.Location},art/find-by-teacher()\n{e.Message}\n{e.StackTrace}\n{e.StackTrace}", GroupNames.醍摩豆服務運維群組);
  1079. return Ok(new { code = 500 });
  1080. }
  1081. }
  1082. [ProducesDefaultResponseType]
  1083. [Authorize(Roles = "IES")]
  1084. [AuthToken(Roles = "teacher,admin")]
  1085. [HttpPost("find-summary-by-teacher")]
  1086. public async Task<IActionResult> FindSummaryByTeacher(JsonElement request)
  1087. {
  1088. try
  1089. {
  1090. if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
  1091. if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  1092. if (!request.TryGetProperty("tId", out JsonElement tId)) return BadRequest();
  1093. var client = _azureCosmos.GetCosmosClient();
  1094. List<object> arts = new();
  1095. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: $"select value(c) from c join A0 in c.teachers where A0.id = '{tId}' and c.id = '{id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Art-{code}") }))
  1096. {
  1097. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  1098. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  1099. {
  1100. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  1101. {
  1102. arts.Add(obj.ToObject<object>());
  1103. }
  1104. }
  1105. }
  1106. return Ok(new { arts });
  1107. }
  1108. catch (Exception e)
  1109. {
  1110. await _dingDing.SendBotMsg($"OS,{_option.Location},art/find-summary-by-teacher()\n{e.Message}\n{e.StackTrace}\n{e.StackTrace}", GroupNames.醍摩豆服務運維群組);
  1111. return Ok(new { code = 500 });
  1112. }
  1113. }
  1114. private class stuFiles
  1115. {
  1116. public string stuId { get; set; }
  1117. public int userType { get; set; }
  1118. public string name { get; set; }
  1119. public List<Attachment> attachments { get; set; } = new List<Attachment>();
  1120. }
  1121. }
  1122. }