ManageController.cs 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. using ICSharpCode.SharpZipLib.GZip;
  2. using IES.ExamLibrary.Models;
  3. using IES.ExamServer.DI;
  4. using IES.ExamServer.DI.SignalRHost;
  5. using IES.ExamServer.Filters;
  6. using IES.ExamServer.Helper;
  7. using IES.ExamServer.Helpers;
  8. using IES.ExamServer.Models;
  9. using IES.ExamServer.Services;
  10. using Microsoft.AspNetCore.Mvc;
  11. using Microsoft.AspNetCore.SignalR;
  12. using Microsoft.Extensions.Caching.Memory;
  13. using Microsoft.Extensions.Configuration;
  14. using System;
  15. using System.Diagnostics.Eventing.Reader;
  16. using System.Linq.Expressions;
  17. using System.Net.Http;
  18. using System.Net.Http.Json;
  19. using System.Security.Cryptography.X509Certificates;
  20. using System.Text.Json;
  21. using System.Text.Json.Nodes;
  22. using System.Text.RegularExpressions;
  23. using static IES.ExamServer.Services.ManageService;
  24. using static System.Reflection.Metadata.BlobBuilder;
  25. namespace IES.ExamServer.Controllers
  26. {
  27. [ApiController]
  28. [Route("manage")]
  29. public class ManageController : BaseController
  30. {
  31. private readonly IConfiguration _configuration;
  32. private readonly IHttpClientFactory _httpClientFactory;
  33. private readonly IMemoryCache _memoryCache;
  34. private readonly ILogger<ManageController> _logger;
  35. private readonly LiteDBFactory _liteDBFactory;
  36. private readonly CenterServiceConnectionService _connectionService;
  37. private readonly int DelayMicro = 10;//微观数据延迟
  38. private readonly int DelayMacro = 100;//宏观数据延迟
  39. private readonly IHubContext<SignalRExamServerHub> _signalRExamServerHub;
  40. public ManageController(LiteDBFactory liteDBFactory, ILogger<ManageController> logger, IConfiguration configuration,
  41. IHttpClientFactory httpClientFactory, IMemoryCache memoryCache, CenterServiceConnectionService connectionService, IHubContext<SignalRExamServerHub> signalRExamServerHub)
  42. {
  43. _logger = logger;
  44. _configuration=configuration;
  45. _httpClientFactory=httpClientFactory;
  46. _memoryCache=memoryCache;
  47. _liteDBFactory=liteDBFactory;
  48. _connectionService=connectionService;
  49. _signalRExamServerHub=signalRExamServerHub;
  50. }
  51. /// <summary>
  52. /// 导出数据
  53. ///通过线上回传数据需要鉴权验证等操作。
  54. ///通过离线包回传数据需要加密操作
  55. /// </summary>
  56. /// <param name="json"></param>
  57. /// <returns></returns>
  58. [HttpPost("export-evaluation-result")]
  59. [AuthToken("admin", "teacher", "visitor")]
  60. public async Task<IActionResult> ExportEvaluationResult(JsonNode json)
  61. {
  62. string id = $"{json["evaluationId"]}";
  63. string shortCode = $"{json["shortCode"]}";
  64. string openCode = $"{json["openCode"]}";
  65. string deviceId = $"{json["deviceId"]}";
  66. EvaluationClient? evaluationClient = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationClient>()
  67. .FindOne(x => x.id!.Equals(id) && x.openCode!.Equals(openCode)&& x.shortCode!.Equals(shortCode));
  68. return Ok();
  69. }
  70. /// <summary>
  71. /// 手动推送
  72. ///通过线上回传数据需要鉴权验证等操作。
  73. ///通过离线包回传数据需要加密操作
  74. /// </summary>
  75. /// <param name="json"></param>
  76. /// <returns></returns>
  77. [HttpPost("manual-push")]
  78. [AuthToken("admin", "teacher", "visitor")]
  79. public async Task<IActionResult> ManualPush(JsonNode json)
  80. {
  81. string id = $"{json["evaluationId"]}";
  82. string shortCode = $"{json["shortCode"]}";
  83. string openCode = $"{json["openCode"]}";
  84. string deviceId = $"{json["deviceId"]}";
  85. EvaluationClient? evaluationClient = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationClient>()
  86. .FindOne(x => x.id!.Equals(id) && x.openCode!.Equals(openCode)&& x.shortCode!.Equals(shortCode));
  87. return Ok();
  88. }
  89. /// <summary>
  90. /// 清理缓存,列出缓存占用空间,type =list列出,type=clear清理,不能清理近期及正在激活的数据,并且提示清理中暂未上传或者导出的数据。
  91. /// </summary>
  92. /// <param name="json"></param>
  93. /// <returns></returns>
  94. [HttpPost("clean-cache")]
  95. [AuthToken("admin", "teacher", "visitor")]
  96. public async Task<IActionResult> CleanCache(JsonNode json)
  97. {
  98. return Ok();
  99. }
  100. //C#.NET 6 后端与前端流式通信
  101. //https://www.doubao.com/chat/collection/687687510791426?type=Thread
  102. //下载日志记录:1.步骤,检查,2.获取描述信息,3.分类型,4下载文件,5.前端处理,6.返回结果 , 正在下载...==> [INFO]https://www.doubao.com/chat/collection/687687510791426?type=Thread [Size=180kb] Ok...
  103. //进度条 展示下载文件总大小和已下载,末尾展示 文件总个数和已下载个数
  104. //https://cdnjs.cloudflare.com/ajax/libs/microsoft-signalr/8.0.7/signalr.min.js
  105. /* int data = 0,blob=0, groupList=0
  106. {
  107. "evaluationId":"idssss",
  108. "shortCode":"1234567890",
  109. "ownerId":"hbcn/tmdid",
  110. "data":1,
  111. "blob":1,
  112. "groupList":1
  113. }
  114. */
  115. //如果要访问中心,则需要教师登录联网。
  116. [HttpPost("download-package-music")]
  117. [AuthToken("admin", "teacher", "visitor")]
  118. public async Task<IActionResult> DownloadPackageMusic(JsonNode json)
  119. {
  120. return Ok();
  121. }
  122. [HttpPost("download-package")]
  123. [AuthToken("admin", "teacher", "visitor")]
  124. public async Task<IActionResult> DownloadPackage(JsonNode json)
  125. {
  126. var token = GetAuthTokenInfo();
  127. //检查试卷文件完整性
  128. List<string> successMsgs = new List<string>();
  129. List<string> errorMsgs = new List<string>();
  130. EvaluationCheckFileResult result = new EvaluationCheckFileResult() { successMsgs=successMsgs, errorMsgs=errorMsgs };
  131. if (token.scope.Equals(ExamConstant.ScopeTeacher) || token.scope.Equals(ExamConstant.ScopeVisitor))
  132. {
  133. if (_connectionService.centerIsConnected)
  134. {
  135. Teacher? teacher = _liteDBFactory.GetLiteDatabase().GetCollection<Teacher>().FindOne(x => x.id!.Equals(token.id));
  136. string id = $"{json["evaluationId"]}";
  137. string shortCode = $"{json["shortCode"]}";
  138. string deviceId = $"{json["deviceId"]}";
  139. EvaluationClient? evaluationClient = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationClient>().FindOne(x => x.id!.Equals(id) && x.shortCode!.Equals(shortCode));
  140. if (teacher != null && evaluationClient!= null)
  141. {
  142. var dataInfo= await GetEvaluationFromCenter(GetXAuthToken(), _configuration, _httpClientFactory, shortCode, evaluationClient.id!);
  143. if (dataInfo.centerCode.Equals("200")&& dataInfo.evaluationCloud!=null)
  144. {
  145. string? CenterUrl = _configuration.GetValue<string>("ExamServer:CenterUrl");
  146. var client = _httpClientFactory.CreateClient();
  147. if (client.DefaultRequestHeaders.Contains(Constant._X_Auth_AuthToken))
  148. {
  149. client.DefaultRequestHeaders.Remove(Constant._X_Auth_AuthToken);
  150. }
  151. client.DefaultRequestHeaders.Add(Constant._X_Auth_AuthToken, teacher.x_auth_token);
  152. HttpResponseMessage message = await client.PostAsJsonAsync($"{CenterUrl}/blob/sas-read", new { containerName = $"{evaluationClient.ownerId}" });
  153. string sas = string.Empty;
  154. string url = string.Empty;
  155. string cnt = string.Empty;
  156. if (message.IsSuccessStatusCode)
  157. {
  158. //url sas timeout name
  159. string content = await message.Content.ReadAsStringAsync();
  160. JsonNode? jsonNode = content.ToObject<JsonNode>();
  161. if (jsonNode != null)
  162. {
  163. sas = $"{jsonNode["sas"]}";
  164. cnt = $"{jsonNode["name"]}";
  165. url = $"{jsonNode["url"]}";
  166. }
  167. }
  168. var httpClient = _httpClientFactory.CreateClient();
  169. string packagePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "package");
  170. string evaluationPath = Path.Combine(packagePath, evaluationClient.id!);
  171. //删除文件夹
  172. FileHelper.DeleteFolder(evaluationPath);
  173. string evaluationDataPath = Path.Combine(evaluationPath, "data");
  174. if (!Directory.Exists(evaluationDataPath))
  175. {
  176. Directory.CreateDirectory(evaluationDataPath);
  177. }
  178. string evaluationData = string.Empty;
  179. {
  180. //evaluation
  181. string evaluationUrl = $"{url}/{cnt}/package/{json["evaluationId"]}/data/evaluation.json?{sas}";
  182. HttpResponseMessage dataMessage = await httpClient.GetAsync(evaluationUrl);
  183. if (dataMessage.IsSuccessStatusCode)
  184. {
  185. var content = await dataMessage.Content.ReadAsStringAsync();
  186. evaluationData=content;
  187. string path_evaluation = Path.Combine(evaluationDataPath, "evaluation.json");
  188. await System.IO.File.WriteAllTextAsync(path_evaluation, content);
  189. successMsgs.Add("评测信息文件evaluation.json文件下载成功!");
  190. }
  191. else
  192. {
  193. errorMsgs.Add("评测信息文件evaluation.json文件下载失败!");
  194. }
  195. }
  196. {
  197. //source.json
  198. string sourceUrl = $"{url}/{cnt}/package/{json["evaluationId"]}/data/source.json?{sas}";
  199. HttpResponseMessage dataMessage = await httpClient.GetAsync(sourceUrl);
  200. if (dataMessage.IsSuccessStatusCode)
  201. {
  202. var content = await dataMessage.Content.ReadAsStringAsync();
  203. string path_source = Path.Combine(evaluationDataPath, "source.json");
  204. await System.IO.File.WriteAllTextAsync(path_source, content);
  205. successMsgs.Add("评测数据原始文件source.json文件下载成功!");
  206. }
  207. else
  208. {
  209. errorMsgs.Add("评测数据原始文件source.json文件下载失败!");
  210. }
  211. }
  212. {
  213. //grouplist.json
  214. string grouplistUrl = $"{url}/{cnt}/package/{json["evaluationId"]}/data/grouplist.json?{sas}";
  215. HttpResponseMessage groupListMessage = await httpClient.GetAsync(grouplistUrl);
  216. if (groupListMessage.IsSuccessStatusCode)
  217. {
  218. var content = await groupListMessage.Content.ReadAsStringAsync();
  219. string path_groupList = Path.Combine(evaluationDataPath, "grouplist.json");
  220. await System.IO.File.WriteAllTextAsync(path_groupList, content);
  221. successMsgs.Add("评测名单grouplist.json文件下载成功!");
  222. }
  223. else
  224. {
  225. errorMsgs.Add("评测名单grouplist.json文件下载失败!");
  226. }
  227. }
  228. {
  229. //下载试卷文件
  230. List<EvaluationExam>? evaluationExams = evaluationData.ToObject<JsonNode>()?["evaluationExams"]?.ToObject<List<EvaluationExam>>();
  231. foreach (var evaluationExam in evaluationExams!)
  232. {
  233. foreach (var evaluationPaper in evaluationExam.papers)
  234. {
  235. string path_paper = Path.Combine(evaluationPath, $"papers/{evaluationPaper.paperId}");
  236. if (!Directory.Exists(path_paper))
  237. {
  238. Directory.CreateDirectory(path_paper);
  239. }
  240. //最多开启10个线程并行下载
  241. var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = 20 };
  242. // 使用 Parallel.ForEachAsync 并行处理每个 blob
  243. await Parallel.ForEachAsync(evaluationPaper.blobs, parallelOptions, async (blob, cancellationToken) =>
  244. {
  245. try
  246. {
  247. // 下载 Blob 文件到本地
  248. HttpResponseMessage blobMessage = await httpClient.GetAsync($"{url}/{cnt}/{blob.path}?{sas}", cancellationToken);
  249. if (blobMessage.IsSuccessStatusCode)
  250. {
  251. byte[] bytes = await blobMessage.Content.ReadAsByteArrayAsync(cancellationToken);
  252. string? extension = Path.GetExtension(blob.path);
  253. if (extension!=null)
  254. {
  255. if (extension.Equals(extension.ToUpper()))
  256. {
  257. string? fileNameWithoutExtension = Path.GetFileNameWithoutExtension(blob.path);
  258. await System.IO.File.WriteAllBytesAsync(Path.Combine(path_paper, $"{fileNameWithoutExtension!}_1{extension}"), bytes, cancellationToken);
  259. }
  260. else
  261. {
  262. string? fileName = Path.GetFileName(blob.path);
  263. await System.IO.File.WriteAllBytesAsync(Path.Combine(path_paper, fileName!), bytes, cancellationToken);
  264. }
  265. }
  266. }
  267. else
  268. {
  269. string? error = await blobMessage.Content.ReadAsStringAsync(cancellationToken);
  270. errorMsgs.Add($"{evaluationExam.subjectName},{evaluationPaper.paperName},{blob.path}文件下载失败,{blobMessage.StatusCode},{error}");
  271. // Console.WriteLine($"Error downloading {blob.path},{blobMessage.StatusCode},{error}");
  272. }
  273. }
  274. catch (Exception ex)
  275. {
  276. errorMsgs.Add($"{evaluationExam.subjectName},{evaluationPaper.paperName},{blob.path}文件下载错误,{ex.Message}");
  277. // 处理异常
  278. //Console.WriteLine($"Error downloading {blob.path}: {ex.Message}");
  279. }
  280. });
  281. }
  282. }
  283. }
  284. _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationClient>().Upsert(dataInfo.evaluationCloud!);
  285. (successMsgs, errorMsgs) = await ManageService.CheckFile(dataInfo.evaluationCloud!, successMsgs, errorMsgs, _signalRExamServerHub, _memoryCache, _logger, deviceId, evaluationPath);
  286. //下载完成后,对数据进行检查,然后在加密压缩。
  287. string zipPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "zip");
  288. if (!Directory.Exists(zipPath))
  289. {
  290. Directory.CreateDirectory(zipPath);
  291. }
  292. string zipFilePath = Path.Combine(zipPath, $"{evaluationClient.id}-{evaluationClient.blobHash}.zip");
  293. var zipInfo = ZipHelper.CreatePasswordProtectedZip(evaluationPath, zipFilePath, evaluationClient.openCode!);
  294. if (zipInfo.res)
  295. {
  296. successMsgs.Add("评测数据压缩包创建成功!");
  297. }
  298. else
  299. {
  300. errorMsgs.Add("评测数据压缩包创建失败!");
  301. }
  302. }
  303. else {
  304. errorMsgs.Add($"云端数据检测结果:{dataInfo. centerMsg},状态:{dataInfo. centerCode}");
  305. }
  306. }
  307. else
  308. {
  309. string teacherMsg = teacher==null ? "未找到教师信息" : "";
  310. string evaluationMsg = evaluationClient==null ? "未找到评测信息" : "";
  311. errorMsgs.Add($"用户信息或未找到评测信息!{teacherMsg}{evaluationMsg}");
  312. }
  313. }
  314. else {
  315. errorMsgs.Add($"云端数据中心未连接");
  316. }
  317. }
  318. else
  319. {
  320. errorMsgs.Add($"请使用教师或访客账号登录!");
  321. }
  322. result.checkError=result.errorMsgs.Count();
  323. result.checkSuccess=result.successMsgs.Count();
  324. result.checkTotal=result.checkSuccess+result.checkError;
  325. if (result.errorMsgs.Count()==0)
  326. {
  327. return Ok(new { code = 200, msg = "下载成功!", result });
  328. }
  329. else {
  330. return Ok(new { code = 1, msg = "下载失败!", result });
  331. }
  332. }
  333. /// <summary>
  334. /// 输入开卷码,查询评测信息,并返回数据。
  335. /// </summary>
  336. /// <param name="json"></param>
  337. /// <returns></returns>
  338. [HttpPost("open-evaluation")]
  339. [AuthToken("admin", "teacher", "visitor")]
  340. public async Task<IActionResult> OpenEvaluation(JsonNode json)
  341. {
  342. string deviceId = $"{json["deviceId"]}";
  343. string evaluationId = $"{json["evaluationId"]}";
  344. string openCode = $"{json["openCode"]}";
  345. string shortCode = $"{json["shortCode"]}";
  346. var token = GetAuthTokenInfo();
  347. List<string> successMsgs = new List<string>();
  348. List<string> errorMsgs = new List<string>();
  349. EvaluationCheckFileResult result = new EvaluationCheckFileResult() { successMsgs=successMsgs, errorMsgs =errorMsgs };
  350. if (!string.IsNullOrEmpty(openCode) && !string.IsNullOrEmpty(evaluationId))
  351. {
  352. EvaluationClient? evaluationLocal = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationClient>().FindOne(x => x.id!.Equals(evaluationId) && x.openCode!.Equals(openCode) && x.shortCode!.Equals(shortCode));
  353. if (evaluationLocal!=null)
  354. {
  355. //ManageService.CheckData(evaluationLocal, null, successMsgs, errorMsgs, _liteDBFactory);
  356. await _signalRExamServerHub.SendMessage(_memoryCache, _logger, deviceId, Constant._Message_grant_type_check_file,
  357. new MessageContent { dataId=evaluationLocal.id, dataName=evaluationLocal.name, messageType=Constant._Message_type_message, status=0, content="开始检查评测信息文件.." });
  358. //判断文件包的压缩包是否存在。
  359. string zipPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "zip");
  360. //校验本地文件数据
  361. string packagePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "package");
  362. string evaluationPath = Path.Combine(packagePath, evaluationLocal.id!);
  363. if (System.IO.File.Exists(Path.Combine(zipPath, $"{evaluationLocal.id}-{evaluationLocal.blobHash}.zip")))
  364. {
  365. string key = $"{Constant._KeyEvaluationZipExtract}:{evaluationId}:{openCode}";
  366. successMsgs.Add("加载评测试卷文件包!");
  367. //string keyData = _memoryCache.Get<string>(key);
  368. //if (!string.IsNullOrWhiteSpace(keyData))
  369. //{
  370. //}
  371. //else {
  372. //}
  373. //删除文件夹
  374. FileHelper.DeleteFolder(evaluationPath);
  375. if (!Directory.Exists(evaluationPath))
  376. {
  377. Directory.CreateDirectory(evaluationPath);
  378. }
  379. //解压文件包
  380. var extractRes = ZipHelper.ExtractPasswordProtectedZip(Path.Combine(zipPath, $"{evaluationLocal.id}-{evaluationLocal.blobHash}.zip"), evaluationPath, evaluationLocal.openCode!);
  381. if (extractRes.res)
  382. {
  383. _memoryCache.Set(key,key,TimeSpan.FromSeconds(30));
  384. successMsgs.Add("评测试卷文件包解压成功!");
  385. (successMsgs, errorMsgs) = await ManageService.CheckFile(evaluationLocal, successMsgs, errorMsgs, _signalRExamServerHub, _memoryCache, _logger, deviceId, evaluationPath);
  386. }
  387. else {
  388. errorMsgs.Add("评测试卷文件包解压失败!");
  389. //return Ok(new { code = 3, msg = "评测试卷文件包解压失败!" });
  390. }
  391. }
  392. else {
  393. errorMsgs.Add("评测试卷文件包不存在!");
  394. //return Ok(new { code = 3, msg = "评测试卷文件包不存在!" });
  395. }
  396. }
  397. else {
  398. errorMsgs.Add("未找到评测信息!");
  399. // return Ok(new { code = 2, msg = "未找到评测信息!" });
  400. }
  401. }
  402. else
  403. {
  404. errorMsgs.Add("评测ID或提取码均未填写!");
  405. // return Ok(new { code = 1, msg = "评测ID或提取码均未填写!" });
  406. }
  407. result.checkTotal = result.errorMsgs.Count() + result.successMsgs.Count();
  408. result.checkError= result.errorMsgs.Count();
  409. result.checkSuccess = result.successMsgs.Count();
  410. if (result.errorMsgs.Count()==0)
  411. {
  412. return Ok(new { code = 200, msg = "校验成功!", result });
  413. }
  414. else
  415. {
  416. return Ok(new { code = 1, msg = "校验失败!", result });
  417. }
  418. }
  419. /// <summary>
  420. /// 检查数据包是否有更新,zip是否存在,数据文件是否完整,名单文件是否完整。
  421. /// 如果有智音模块,则需要检查智音音乐缓存是否完整。
  422. /// </summary>
  423. /// <param name="json"></param>
  424. /// <returns></returns>
  425. [HttpPost("check-evaluation")]
  426. [AuthToken("admin", "teacher", "visitor")]
  427. public async Task<IActionResult> CheckEvaluation(JsonNode json)
  428. {
  429. string shortCode = $"{json["shortCode"]}";
  430. string evaluationId = $"{json["evaluationId"]}";
  431. string checkCenter = $"{json["checkCenter"]}";
  432. string deviceId = $"{json["deviceId"]}";
  433. string centerCode = string.Empty, centerMsg = string.Empty;
  434. Expression<Func<EvaluationClient, bool>> predicate = x => true;
  435. var token = GetAuthTokenInfo();
  436. if (!string.IsNullOrEmpty(shortCode) || !string.IsNullOrEmpty(evaluationId))
  437. {
  438. if (!string.IsNullOrEmpty(shortCode))
  439. {
  440. predicate= predicate.And(x => !string.IsNullOrWhiteSpace(x.shortCode) && x.shortCode.Equals(shortCode));
  441. }
  442. if (!string.IsNullOrWhiteSpace(evaluationId))
  443. {
  444. predicate= predicate.And(x => x.id!.Equals(evaluationId));
  445. }
  446. }
  447. else
  448. {
  449. return Ok(new { code = 400, msg = "评测ID或提取码均未填写!" });
  450. }
  451. IEnumerable<EvaluationClient>? evaluationClients = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationClient>().Find(predicate);
  452. EvaluationClient? evaluationLocal = null;
  453. EvaluationClient? evaluationCloud = null;
  454. if (evaluationClients!=null && evaluationClients.Count()>0)
  455. {
  456. evaluationLocal= evaluationClients.First();
  457. }
  458. List<string> successMsgs = new List<string>();
  459. List<string> errorMsgs = new List<string>();
  460. //从数据中心搜索
  461. if ("1".Equals($"{checkCenter}"))
  462. {
  463. if (_connectionService.centerIsConnected)
  464. {
  465. if (token.scope.Equals(ExamConstant.ScopeTeacher) || token.scope.Equals(ExamConstant.ScopeVisitor))//由于已经绑定学校,访客教师也可以访问中心。
  466. {
  467. Teacher? teacher = _liteDBFactory.GetLiteDatabase().GetCollection<Teacher>().FindOne(x => x.id!.Equals(token.id));
  468. if (teacher != null)
  469. {
  470. (evaluationCloud, centerCode, centerMsg)= await ManageService.GetEvaluationFromCenter(GetXAuthToken(), _configuration,_httpClientFactory,shortCode,evaluationId);
  471. }
  472. else
  473. {
  474. centerCode = $"401";
  475. centerMsg = "当前登录账号未找到";
  476. }
  477. }
  478. }
  479. else
  480. {
  481. centerCode = $"404";
  482. centerMsg = "云端数据中心未连接";
  483. }
  484. if (centerCode.Equals("200"))
  485. {
  486. successMsgs.Add($"云端数据检测结果:{centerMsg},状态:{centerCode}");
  487. }
  488. else
  489. {
  490. errorMsgs.Add($"云端数据检测结果:{centerMsg},状态:{centerCode}");
  491. }
  492. }
  493. EvaluationCheckDataResult checkDataResult;
  494. (checkDataResult, evaluationLocal) = ManageService.CheckData( evaluationLocal, evaluationCloud, successMsgs, errorMsgs, _liteDBFactory);
  495. Dictionary<string, object?>? evaluation = null;
  496. if (evaluationLocal!=null)
  497. {
  498. string zipPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "zip");
  499. //判断文件包的压缩包是否存在。
  500. if (!System.IO.File.Exists(Path.Combine(zipPath, $"{evaluationLocal.id}-{evaluationLocal.blobHash}.zip")))
  501. {
  502. checkDataResult.zip =1;
  503. checkDataResult.blob=1;
  504. checkDataResult.data=1;
  505. checkDataResult.groupList=1;
  506. checkDataResult.blobSize=evaluationLocal.blobSize;
  507. checkDataResult.dataSize=evaluationLocal.dataSize;
  508. checkDataResult.studentCount=evaluationLocal.studentCount;
  509. errorMsgs.Add($"评测文件包压缩包不存在,需要重新下载评测!");
  510. }
  511. var properties = evaluationLocal.GetType().GetProperties();
  512. evaluation = new Dictionary<string, object?>();
  513. foreach (var property in properties)
  514. {
  515. if (!property.Name.Equals("openCode"))
  516. {
  517. evaluation[property.Name] = property.GetValue(evaluationLocal);
  518. }
  519. }
  520. }
  521. return Ok(new
  522. {
  523. code = 200,
  524. evaluation = evaluation,
  525. result = checkDataResult
  526. });
  527. }
  528. /// <summary>
  529. /// 获取当前评测的开考设置信息
  530. /// </summary>
  531. /// <param name="json"></param>
  532. /// <returns></returns>
  533. [HttpPost("list-evaluation-round")]
  534. [AuthToken("admin", "teacher", "visitor")]
  535. public IActionResult ListEvaluationRound(JsonNode json)
  536. {
  537. string evaluationId = $"{json["evaluationId"]}";
  538. string openCode = $"{json["openCode"]}";
  539. string shortCode = $"{json["shortCode"]}";
  540. EvaluationClient? evaluationClient = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationClient>()
  541. .FindOne(x => x.id!.Equals(evaluationId) && x.shortCode!.Equals(shortCode) && x.openCode!.Equals(openCode));
  542. if (evaluationClient!=null)
  543. {
  544. IEnumerable<EvaluationRoundSetting>? settings = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationRoundSetting>().Find(x => x.evaluationId!.Equals(evaluationClient.id)).OrderByDescending(x => x.activate).ThenByDescending(x => x.startline).ThenByDescending(x => x.createTime);
  545. if (settings.IsNotEmpty())
  546. {
  547. return Ok(new { code = 200, msg = "OK", settings = settings });
  548. }
  549. else
  550. {
  551. return Ok(new { code = 2, msg = "未设置开考信息!" });
  552. }
  553. }
  554. else
  555. {
  556. return Ok(new { code = 1, msg = "未找到评测信息!" });
  557. }
  558. }
  559. /// <summary>
  560. /// 获取当前评测的开考设置信息
  561. /// </summary>
  562. /// <param name="json"></param>
  563. /// <returns></returns>
  564. [HttpPost("load-evaluation-round")]
  565. [AuthToken("admin", "teacher", "visitor")]
  566. public IActionResult LoadEvaluationRound(JsonNode json)
  567. {
  568. string evaluationId = $"{json["evaluationId"]}";
  569. string openCode = $"{json["openCode"]}";
  570. string shortCode = $"{json["shortCode"]}";
  571. string settingId = $"{json["settingId"]}";
  572. EvaluationClient? evaluationClient = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationClient>()
  573. .FindOne(x => x.id!.Equals(evaluationId) && x.shortCode!.Equals(shortCode) && x.openCode!.Equals(openCode));
  574. EvaluationRoundSetting? setting = null;
  575. if (evaluationClient!=null)
  576. {
  577. setting = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationRoundSetting>().FindOne(x =>x.id!.Equals(settingId) && x.evaluationId!.Equals(evaluationClient.id));
  578. if (setting!=null)
  579. {
  580. IEnumerable<EvaluationStudentResult>? results = null;
  581. var members = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationMember>().Find(x => x.evaluationId!.Equals(evaluationClient.id) && x.roundId!.Equals(setting.id));
  582. //并获取学生的作答信息
  583. //顺便返回本轮的学生名单
  584. if (members!=null && members.Count()>0)
  585. {
  586. results = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationStudentResult>()
  587. .Find(x => members.Select(x => x.id).Contains(x.studentId)&&!string.IsNullOrWhiteSpace(x.evaluationId) && x.evaluationId.Equals(evaluationClient.id));
  588. if (results.Count()==members.Count())
  589. {
  590. return Ok(new { code = 200, setting, results });
  591. }
  592. else {
  593. return Ok(new { code = 200,msg="学生作答信息数量不匹配", setting, results });
  594. }
  595. }
  596. else
  597. {
  598. return Ok(new { code = 200,msg="未分配学生名单,或名单没有学生!", setting , results });
  599. }
  600. }
  601. else
  602. {
  603. return Ok(new { code = 2, msg = "未设置开考信息!" });
  604. }
  605. }
  606. else {
  607. return Ok(new { code = 1, msg = "未找到评测信息!" });
  608. }
  609. }
  610. /// <summary>
  611. /// 设置评测开考信息(本轮名单,计时规则,分配试卷等)
  612. /// </summary>
  613. /// <param name="json"></param>
  614. /// <returns></returns>
  615. [HttpPost("setting-evaluation-round")]
  616. [AuthToken("admin", "teacher", "visitor")]
  617. public IActionResult SettingEvaluationRound(JsonNode json)
  618. {
  619. EvaluationRoundSetting? setting = json.ToObject<EvaluationRoundSetting>();
  620. if (setting!=null)
  621. {
  622. var db = _liteDBFactory.GetLiteDatabase();
  623. var collection = db.GetCollection<EvaluationClient>() ;
  624. //&& x.openCode!.Equals($"{json["openCode"]}")&& x.shortCode!.Equals($"{json["shortCode"]}")
  625. string shortCode = $"{json["shortCode"]}";
  626. string openCode = $"{json["openCode"]}";
  627. EvaluationClient ? evaluationClient = collection.FindOne(x => x.id!.Equals(setting.evaluationId)
  628. && !string.IsNullOrWhiteSpace(x.shortCode) && x.shortCode.Equals(x.shortCode)
  629. && !string.IsNullOrWhiteSpace(x.openCode) && x.openCode.Equals(openCode) );
  630. if (evaluationClient!=null)
  631. {
  632. IEnumerable<EvaluationRoundSetting> settings = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationRoundSetting>().Find(x => x.evaluationId!.Equals(evaluationClient.id));
  633. if (settings != null && settings.Count() > 0)
  634. {
  635. var datas = settings.ToList();
  636. foreach (EvaluationRoundSetting item in datas)
  637. {
  638. item.activate = 0;
  639. }
  640. _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationRoundSetting>().Upsert(datas);
  641. }
  642. /// 判断是否包含所有分组
  643. bool isAllContained = setting.groupList.All(x => evaluationClient.grouplist.Any(y => y.id == x.id));
  644. if (isAllContained && evaluationClient.grouplist.IsNotEmpty())
  645. {
  646. //增加排序,保证id的唯一性
  647. setting.id=ShaHashHelper.GetSHA1($"{evaluationClient.id}_{string.Join("", setting.groupList.Select(x => x.id)).OrderBy(x => x)}");
  648. setting.createTime= DateTimeOffset.Now.ToUnixTimeMilliseconds();
  649. _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationRoundSetting>().Upsert(setting);
  650. _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationClient>().Upsert(evaluationClient);
  651. /// 分配试卷
  652. var (roundStudentPapers, members, results,code,msg) = AssignStudentPaper(evaluationClient, setting);
  653. return Ok(new { code = code, msg =msg , setting, results });
  654. }
  655. else
  656. {
  657. return Ok(new { code = 3, msg = "开考名单不在当前评测中!" });
  658. }
  659. }
  660. else { return Ok(new { code = 2, msg = "未找到评测,请确认评测ID、提取码、开卷码是否正确!" }); }
  661. }
  662. else
  663. {
  664. return Ok(new { code = 1, msg = "未完善设置信息!" });
  665. }
  666. }
  667. /// <summary>
  668. /// 加载本地的活动列表
  669. /// </summary>
  670. /// <param name="json"></param>
  671. /// <returns></returns>
  672. [HttpPost("list-local-evaluation")]
  673. [AuthToken("admin", "teacher", "visitor")]
  674. public IActionResult ListLocalEvaluation(JsonNode json)
  675. {
  676. if (!string.IsNullOrWhiteSpace($"{_connectionService.serverDevice?.school?.id}"))
  677. {
  678. string code = $"{_connectionService!.serverDevice!.school!.id}";
  679. IEnumerable<EvaluationClient>? evaluationClients = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationClient>().Find(x => x.ownerId!.Equals(code)).OrderByDescending(x => x.stime);
  680. if (evaluationClients != null)
  681. {
  682. var result = evaluationClients.Select(client =>
  683. {
  684. var properties = client.GetType().GetProperties();
  685. var anonymousObject = new Dictionary<string, object?>();
  686. foreach (var property in properties)
  687. {
  688. if (!property.Name.Equals("openCode"))
  689. {
  690. anonymousObject[property.Name] = property.GetValue(client);
  691. }
  692. }
  693. return anonymousObject;
  694. });
  695. return Ok(new { code = 200, evaluation = result });
  696. }
  697. else
  698. {
  699. return Ok(new { code = 200, evaluation = new List<EvaluationClient>() });
  700. }
  701. }
  702. else
  703. {
  704. return Ok(new { code = 200, evaluation = new List<EvaluationClient>() });
  705. }
  706. }
  707. /// <summary>
  708. /// 设置评测开考信息(本轮名单,计时规则等)
  709. /// </summary>
  710. /// <param name="json"></param>
  711. /// <returns></returns>
  712. //[HttpPost("assign-student-paper")]
  713. //[AuthToken("admin", "teacher", "visitor")]
  714. private (List<EvaluationStudentPaper> roundStudentPapers, List<EvaluationMember> members, List<EvaluationStudentResult> results, int code, string msg)
  715. AssignStudentPaper(EvaluationClient evaluationClient, EvaluationRoundSetting setting)
  716. {
  717. int code = 200;
  718. string msg = string.Empty;
  719. List<EvaluationStudentPaper> roundStudentPapers = new List<EvaluationStudentPaper>();
  720. List<EvaluationMember> members = new List<EvaluationMember>();
  721. List<EvaluationStudentResult> results = new List<EvaluationStudentResult>();
  722. string packagePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "package");
  723. string evaluationPath = Path.Combine(packagePath, evaluationClient.id!);
  724. string evaluationDataPath = Path.Combine(evaluationPath, "data");
  725. string path_groupList = Path.Combine(evaluationDataPath, "groupList.json");
  726. if (System.IO.File.Exists(path_groupList))
  727. {
  728. JsonNode? jsonNode = System.IO.File.ReadAllText(path_groupList).ToObject<JsonNode>();
  729. if (jsonNode!=null && jsonNode["groupList"]!=null)
  730. {
  731. List<EvaluationGroupList>? groupList = jsonNode["groupList"]?.ToObject<List<EvaluationGroupList>>();
  732. if (groupList!=null)
  733. {
  734. bool isAllContained = setting.groupList.All(x => groupList.Any(y => y.id == x.id));
  735. if (isAllContained)
  736. {
  737. foreach (var item in setting.groupList)
  738. {
  739. EvaluationGroupList? groupListItem = groupList.Find(x => x.id == item.id);
  740. if (groupListItem!=null)
  741. {
  742. groupListItem.members.ForEach(x =>
  743. {
  744. x.schoolId=_connectionService?.serverDevice?.school?.id;
  745. x.evaluationId=evaluationClient.id;
  746. x.classId= groupListItem.id;
  747. x.periodId= groupListItem.periodId;
  748. x.roundId=setting.id;
  749. x.className= groupListItem.name;
  750. });
  751. members.AddRange(groupListItem.members);
  752. }
  753. }
  754. //清空数据库,重新插入
  755. _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationMember>().DeleteAll();
  756. //插入
  757. _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationMember>().Upsert(members);
  758. foreach (var subject in evaluationClient.subjects)
  759. {
  760. var studentPaperIds = members.Select(x => ShaHashHelper.GetSHA1(x.id+evaluationClient.id+subject.examId+subject.subjectId));
  761. IEnumerable<EvaluationStudentPaper> evaluationStudentPapers = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationStudentPaper>()
  762. .Find(x => studentPaperIds.Contains(x.id) && x.evaluationId!.Equals(evaluationClient.id));
  763. List<EvaluationStudentPaper> studentPapers = new List<EvaluationStudentPaper>();
  764. int paperIndex = 0;
  765. int paperCount = subject.papers.Count();
  766. //先把试卷顺序打乱
  767. subject.papers =subject.papers.OrderBy(x => Guid.NewGuid().ToString()).ToList();
  768. //将学生顺序打乱
  769. members = members.OrderBy(x => Guid.NewGuid().ToString()).ToList();
  770. foreach (var member in members)
  771. {
  772. SubjectExamPaper studentPaper = subject.papers[paperIndex];
  773. string id = ShaHashHelper.GetSHA1(member.id+evaluationClient.id+subject.examId+subject.subjectId);
  774. var paper = evaluationStudentPapers.Where(x => x.id!.Equals(id));
  775. if (paper== null || paper.Count()==0)
  776. {
  777. studentPapers.Add(new EvaluationStudentPaper
  778. {
  779. studentId=member.id,
  780. studentName=member.name,
  781. classId=member.classId,
  782. className=member.className,
  783. evaluationId=evaluationClient.id,
  784. examId=subject.examId,
  785. examName=subject.examName,
  786. subjectId=subject.subjectId,
  787. subjectName=subject.subjectName,
  788. paperId=studentPaper.paperId,
  789. paperName=studentPaper.paperName,
  790. questionCount=studentPaper.questionCount,
  791. id=id,
  792. });
  793. // 移动到下一个试卷
  794. paperIndex = (paperIndex + 1) % paperCount;
  795. }
  796. else
  797. {
  798. // Console.WriteLine("已经分配过试卷,跳过");
  799. //已经分配过试卷,跳过
  800. }
  801. }
  802. if (studentPapers.Count>0)
  803. {
  804. _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationStudentPaper>().Upsert(studentPapers);
  805. roundStudentPapers.AddRange(studentPapers);
  806. }
  807. if (evaluationStudentPapers!=null && evaluationStudentPapers.Count()>0)
  808. {
  809. roundStudentPapers.AddRange(evaluationStudentPapers);
  810. }
  811. }
  812. long now = DateTimeOffset.Now.ToUnixTimeMilliseconds();
  813. IEnumerable<EvaluationStudentResult> studentResults = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationStudentResult>()
  814. .Find(x => members.Select(x => x.id).Contains(x.studentId)&&!string.IsNullOrWhiteSpace(x.evaluationId) && x.evaluationId.Equals(evaluationClient.id));
  815. foreach (var member in members)
  816. {
  817. EvaluationStudentResult? studentResult = null;
  818. //sha1(evaluationId-schoolId-studentId)
  819. string resultId = ShaHashHelper.GetSHA1(evaluationClient.id+_connectionService?.serverDevice?.school?.id+member.id);
  820. var result = studentResults.Where(x => x.id!.Equals(resultId) && !string.IsNullOrWhiteSpace(x.studentId) && x.studentId.Equals(member.id));
  821. if (result==null || result.Count()==0)
  822. {
  823. studentResult = new EvaluationStudentResult()
  824. {
  825. id = resultId,
  826. evaluationId = evaluationClient.id,
  827. schoolId = _connectionService?.serverDevice?.school?.id,
  828. studentId = member.id,
  829. studentName = member.name,
  830. classId = member.classId,
  831. className = member.className,
  832. ownerId= evaluationClient.ownerId,
  833. scope= evaluationClient.scope,
  834. type= evaluationClient.type,
  835. pid= evaluationClient.pid,
  836. };
  837. var studentPapers = roundStudentPapers.FindAll(x => !string.IsNullOrWhiteSpace(x.studentId)
  838. && x.studentId.Equals(member.id) &&!string.IsNullOrWhiteSpace(x.evaluationId) && x.evaluationId.Equals(evaluationClient.id));
  839. if (studentPapers.IsNotEmpty())
  840. {
  841. foreach (var studentPaper in studentPapers)
  842. {
  843. studentResult.subjectResults.Add(new EvaluationSubjectResult()
  844. {
  845. id = ShaHashHelper.GetSHA1(evaluationClient.id+studentPaper.examId+studentPaper.subjectId+member.id),
  846. evaluationId = studentPaper.evaluationId,
  847. examId = studentPaper.examId,
  848. examName = studentPaper.examName,
  849. subjectId = studentPaper.subjectId,
  850. subjectName = studentPaper.subjectName,
  851. paperId = studentPaper.paperId,
  852. paperName = studentPaper.paperName,
  853. questionCount=studentPaper.questionCount,
  854. createTime=now
  855. });
  856. }
  857. }
  858. // _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationStudentResult>().Upsert(studentResult);
  859. }
  860. else
  861. {
  862. studentResult = result.First();
  863. studentResult.studentName = member.name;
  864. studentResult.classId = member.classId;
  865. studentResult.className = member.className;
  866. var studentPapers = roundStudentPapers.FindAll(x => !string.IsNullOrWhiteSpace(x.studentId)
  867. && x.studentId.Equals(member.id) &&!string.IsNullOrWhiteSpace(x.evaluationId) && x.evaluationId.Equals(evaluationClient.id));
  868. if (studentPapers.IsNotEmpty())
  869. {
  870. foreach (var studentPaper in studentPapers)
  871. {
  872. string subjectResultId = ShaHashHelper.GetSHA1(evaluationClient.id+studentPaper.examId+studentPaper.subjectId+member.id);
  873. var subjectResult = studentResult.subjectResults.Where(x => x.id!.Equals(subjectResultId)).FirstOrDefault();
  874. if (subjectResult==null)
  875. {
  876. subjectResult= new EvaluationSubjectResult()
  877. {
  878. id = ShaHashHelper.GetSHA1(evaluationClient.id+studentPaper.examId+studentPaper.subjectId+member.id),
  879. evaluationId = studentPaper.evaluationId,
  880. examId = studentPaper.examId,
  881. examName = studentPaper.examName,
  882. subjectId = studentPaper.subjectId,
  883. subjectName = studentPaper.subjectName,
  884. paperId = studentPaper.paperId,
  885. paperName = studentPaper.paperName,
  886. questionCount= studentPaper.questionCount,
  887. createTime=now
  888. };
  889. }
  890. studentResult.subjectResults.Add(subjectResult);
  891. }
  892. }
  893. }
  894. if (studentResult!=null)
  895. {
  896. results.Add(studentResult);
  897. }
  898. }
  899. if (results.Count>0)
  900. {
  901. _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationStudentResult>().Upsert(results);
  902. }
  903. }
  904. else
  905. {
  906. msg = "开考名单不在当前评测中!";
  907. code = 4;
  908. }
  909. }
  910. else
  911. {
  912. msg = "名单文件字段提取为空!";
  913. code = 3;
  914. }
  915. }
  916. else
  917. {
  918. msg = "名单文件解析错误!";
  919. code = 2;
  920. }
  921. }
  922. else
  923. {
  924. msg = "名单文件不存在!";
  925. code = 1;
  926. }
  927. if (members.Count()!=results.Count())
  928. {
  929. code = 5;
  930. msg = "名单成员与作答记录数量不匹配!";
  931. }
  932. if (roundStudentPapers.Count()!= results.SelectMany(x => x.subjectResults).Count())
  933. {
  934. code = 6;
  935. msg = "学生分配的试卷与作答记录数量不匹配!";
  936. }
  937. return (roundStudentPapers, members, results, code, msg);
  938. }
  939. }
  940. }