ManageController.cs 53 KB

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