ManageController.cs 55 KB

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