TriggerExam.cs 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  1. using Azure.Cosmos;
  2. using Azure.Messaging.ServiceBus;
  3. using Microsoft.Azure.Documents;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Text.Json;
  9. using System.Threading.Tasks;
  10. using TEAMModelOS.SDK.DI;
  11. using TEAMModelOS.SDK.Extension;
  12. using TEAMModelOS.SDK;
  13. using TEAMModelOS.SDK.Models;
  14. using TEAMModelOS.SDK.Models.Service;
  15. using HTEXLib.COMM.Helpers;
  16. using System.Net.Http;
  17. namespace TEAMModelOS.FunctionV4
  18. {
  19. public class TriggerExam
  20. {
  21. public static async Task Trigger(CoreAPIHttpService _coreAPIHttpService, AzureCosmosFactory _azureCosmos, AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing,
  22. CosmosClient client, JsonElement input, TriggerData data)
  23. {
  24. List<ExamClassResult> examClassResults = new();
  25. List<ExamSubject> examSubjects = new();
  26. try
  27. {
  28. if ((data.status != null && data.status.Value == 404))
  29. {
  30. await client.GetContainer(Constant.TEAMModelOS, "Common").DeleteItemStreamAsync(data.id, new PartitionKey(data.code));
  31. ActivityList activity = input.ToObject<ActivityList>();
  32. await ActivityService.DeleteActivity(_coreAPIHttpService, client, _dingDing, activity);
  33. var table_cancel = _azureStorage.GetCloudTableClient().GetTableReference("ChangeRecord");
  34. List<ChangeRecord> records = await table_cancel.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", data.id } });
  35. foreach (var record in records)
  36. {
  37. try
  38. {
  39. await table_cancel.DeleteSingle<ChangeRecord>(record.PartitionKey, record.RowKey);
  40. await _serviceBus.GetServiceBusClient().CancelMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), record.sequenceNumber);
  41. }
  42. catch (Exception)
  43. {
  44. continue;
  45. }
  46. }
  47. return;
  48. }
  49. ExamInfo info = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<ExamInfo>(data.id, new Azure.Cosmos.PartitionKey($"{data.code}"));
  50. if (info != null)
  51. {
  52. if (info.scope.Equals("teacher", StringComparison.OrdinalIgnoreCase) || info.scope.Equals("private", StringComparison.OrdinalIgnoreCase))
  53. {
  54. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIterator(queryText: $"select value(c) from c where c.examId = '{info.id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"ExamClassResult-{info.creatorId}") }))
  55. {
  56. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  57. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  58. {
  59. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  60. {
  61. examClassResults.Add(obj.ToObject<ExamClassResult>());
  62. }
  63. }
  64. }
  65. }
  66. else
  67. {
  68. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIterator(queryText: $"select value(c) from c where c.examId = '{info.id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"ExamClassResult-{data.school}") }))
  69. {
  70. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  71. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  72. {
  73. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  74. {
  75. examClassResults.Add(obj.ToObject<ExamClassResult>());
  76. }
  77. }
  78. }
  79. }
  80. var table = _azureStorage.GetCloudTableClient().GetTableReference("ChangeRecord");
  81. string PartitionKey = string.Format("{0}{1}{2}", info.code, "-", info.progress);
  82. List<ChangeRecord> records = await table.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", data.id }, { "PartitionKey", PartitionKey } });
  83. //处理科目信息
  84. List<string> sub = new List<string>();
  85. foreach (ExamSubject subject in info.subjects)
  86. {
  87. sub.Add(subject.id);
  88. }
  89. //整合名单
  90. List<string> classes = ExamService.getClasses(info.classes, info.stuLists);
  91. //ChangeRecord record = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<ChangeRecord>(input.Id, new Azure.Cosmos.PartitionKey($"{info.progress}"));
  92. switch (info.progress)
  93. {
  94. case "pending":
  95. var message = new ServiceBusMessage(new { id = data.id, progress = "going", code = data.code }.ToJsonString());
  96. message.ApplicationProperties.Add("name", "Exam");
  97. if (records.Count > 0)
  98. {
  99. try
  100. {
  101. await _serviceBus.GetServiceBusClient().CancelMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), records[0].sequenceNumber);
  102. }
  103. catch (Exception)
  104. {
  105. }
  106. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), message, DateTimeOffset.FromUnixTimeMilliseconds(data.startTime));
  107. records[0].sequenceNumber = start;
  108. await table.SaveOrUpdate<ChangeRecord>(records[0]);
  109. //await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(record, record.id, new Azure.Cosmos.PartitionKey($"{record.code}"));
  110. }
  111. else
  112. {
  113. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), message, DateTimeOffset.FromUnixTimeMilliseconds(data.startTime));
  114. //string pk = String.Format("{0}{1}{2}", info.code, "-", "pending");
  115. ChangeRecord changeRecord = new ChangeRecord
  116. {
  117. RowKey = data.id,
  118. PartitionKey = PartitionKey,
  119. sequenceNumber = start,
  120. msgId = message.MessageId
  121. };
  122. await table.Save<ChangeRecord>(changeRecord);
  123. //await client.GetContainer(Constant.TEAMModelOS, "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
  124. }
  125. break;
  126. case "going":
  127. try
  128. {
  129. //向学生或醍摩豆账号发起通知
  130. #region
  131. //Notice notice = new Notice()
  132. //{
  133. // msgId = info.id,
  134. // creation = info.startTime,
  135. // expire = info.endTime,
  136. // creatorId = info.creatorId,
  137. // stuids = studentss,
  138. // tmdids = tmdids,
  139. // type = "notice",//评测参加通知
  140. // priority = "normal",
  141. // school = info.school,
  142. // scope = info.scope,
  143. // //data = new { }.ToJsonString()
  144. // body = new Body { sid = info.id, scode = info.code, spk = info.pk, biztype = "exam-join" }
  145. //};
  146. //var messageBlob = new ServiceBusMessage(notice.ToJsonString());
  147. //messageBlob.ApplicationProperties.Add("name", "Notice");
  148. //await _serviceBus.GetServiceBusClient().SendMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageBlob);
  149. #endregion
  150. //List<string> classes = new List<string>();
  151. //处理活动中间件
  152. List<RGroupList> members = await Activity(_coreAPIHttpService, info, classes, client, _dingDing, sub, examClassResults);
  153. if (examClassResults.Count == 0)
  154. {
  155. foreach (string cla in classes)
  156. {
  157. int m = 0;
  158. RGroupList rGroup = new();
  159. rGroup = members.Where(m => m.id.Equals(cla)).FirstOrDefault();
  160. foreach (ExamSubject subject in info.subjects)
  161. {
  162. string classCode = string.Empty;
  163. string cname = string.Empty;
  164. if (string.IsNullOrEmpty(info.school) || !info.scope.Equals("school", StringComparison.OrdinalIgnoreCase))
  165. {
  166. classCode = "ExamClassResult-" + info.creatorId;
  167. }
  168. else
  169. {
  170. classCode = "ExamClassResult-" + info.school;
  171. }
  172. cname = members.Where(m => m.id.Equals(cla)).FirstOrDefault()?.name;
  173. ExamClassResult result = new()
  174. {
  175. code = classCode,
  176. examId = info.id,
  177. id = Guid.NewGuid().ToString(),
  178. subjectId = subject.id,
  179. year = info.year,
  180. scope = info.scope
  181. };
  182. result.info.id = cla;
  183. result.info.name = cname;
  184. List<string> ans = new List<string>();
  185. List<List<string>> anses = new List<List<string>>();
  186. List<List<Details>> marks = new List<List<Details>>();
  187. List<double> ansPoint = new List<double>();
  188. List<(string sId, string scode)> ids = new List<(string sId, string scode)>();
  189. foreach (double p in info.papers[m].point)
  190. {
  191. //Details details = new Details();
  192. //ans.Add(new List<string>());
  193. anses.Add(new List<string>());
  194. marks.Add(new List<Details>());
  195. ansPoint.Add(-1);
  196. }
  197. var sresponse = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(cla, new Azure.Cosmos.PartitionKey($"Class-{info.school}"));
  198. if (sresponse.Status == 200)
  199. {
  200. using var json = await JsonDocument.ParseAsync(sresponse.ContentStream);
  201. Class classroom = json.ToObject<Class>();
  202. School sc = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(info.school, new Azure.Cosmos.PartitionKey("Base"));
  203. foreach (Period period in sc.period)
  204. {
  205. if (period.id.Equals(classroom.periodId))
  206. {
  207. foreach (Semester semester in period.semesters)
  208. {
  209. if (semester.start == 1)
  210. {
  211. int year = DateTimeOffset.UtcNow.Year;
  212. int month = DateTimeOffset.UtcNow.Month;
  213. int day = DateTimeOffset.UtcNow.Day;
  214. int time = 0;
  215. if (month == semester.month)
  216. {
  217. time = day >= semester.day ? 0 : 1;
  218. }
  219. else
  220. {
  221. time = month > semester.month ? 0 : 1;
  222. }
  223. int eyear = year - time;
  224. result.gradeId = (eyear - classroom.year).ToString();
  225. }
  226. }
  227. }
  228. }
  229. //result.info.id = classroom.id;
  230. //result.info.name = classroom.name;
  231. //result.gradeId = classroom.year.ToString();
  232. //处理班级人数
  233. /* await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryStreamIterator(queryText: $"select c.id from c where c.classId = '{classroom.id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Base-{info.school}") }))
  234. {
  235. using var json_stu = await JsonDocument.ParseAsync(item.ContentStream);
  236. if (json_stu.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  237. {
  238. var accounts = json_stu.RootElement.GetProperty("Documents").EnumerateArray();
  239. while (accounts.MoveNext())
  240. {
  241. JsonElement account = accounts.Current;
  242. ids.Add(account.GetProperty("id").GetString());
  243. }
  244. }
  245. }*/
  246. }
  247. /*if (info.scope.Equals("private", StringComparison.OrdinalIgnoreCase))
  248. {
  249. var stuResponse = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemStreamAsync(cla, new Azure.Cosmos.PartitionKey($"GroupList"));
  250. if (stuResponse.Status == 200)
  251. {
  252. using var json = await JsonDocument.ParseAsync(stuResponse.ContentStream);
  253. GroupList stuList = json.ToObject<GroupList>();
  254. //result.info.id = stuList.id;
  255. result.info.name = stuList.name;
  256. //处理发布对象为自选名单(个人)
  257. foreach (Member stus in stuList.members)
  258. {
  259. if (!ids.Contains(stus.id))
  260. {
  261. ids.Add(stus.id);
  262. }
  263. }
  264. }
  265. }
  266. else
  267. {
  268. var stuResponse = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(cla, new Azure.Cosmos.PartitionKey($"GroupList-{info.school}"));
  269. if (stuResponse.Status == 200)
  270. {
  271. using var json = await JsonDocument.ParseAsync(stuResponse.ContentStream);
  272. GroupList stuList = json.ToObject<GroupList>();
  273. //result.info.id = stuList.id;
  274. result.info.name = stuList.name;
  275. //处理发布对象为自选名单(校本)
  276. foreach (Member stus in stuList.members)
  277. {
  278. if (!ids.Contains(stus.id))
  279. {
  280. ids.Add(stus.id);
  281. }
  282. }
  283. }
  284. }*/
  285. foreach (RMember member in rGroup.members)
  286. {
  287. ids.Add((member.id, member.code));
  288. }
  289. //ids = members.Where(c => c.id.Equals(cla)).SelectMany(m => m.members).Select(g => g.id).ToList();
  290. foreach (var (sId, scode) in ids)
  291. {
  292. result.mark.Add(marks);
  293. result.studentIds.Add(sId);
  294. //result.scIds.Add(scode ?? "");
  295. result.studentAnswers.Add(ans);
  296. result.studentScores.Add(ansPoint);
  297. result.ans.Add(anses);
  298. result.sum.Add(0);
  299. result.status.Add(1);
  300. }
  301. //result.progress = info.progress;
  302. result.school = info.school;
  303. m++;
  304. await client.GetContainer(Constant.TEAMModelOS, "Common").CreateItemAsync(result, new Azure.Cosmos.PartitionKey($"{result.code}"));
  305. }
  306. }
  307. }
  308. else
  309. {
  310. //处理单科结算时科目与试卷信息匹配的问题
  311. int gno = 0;
  312. foreach (ExamSubject subject in info.subjects)
  313. {
  314. if (subject.classCount == classes.Count)
  315. {
  316. await createClassResultAsync(info, examClassResults, subject, gno, _azureCosmos, _dingDing, _azureStorage);
  317. }
  318. gno++;
  319. }
  320. var isScore = examClassResults.SelectMany(e => e.studentScores).ToList().Exists(c => c.Contains(-1));
  321. int newStatus = 0;
  322. if (!isScore)
  323. {
  324. newStatus = 1;
  325. }
  326. Settlement settlement = await getMore(client, info, examClassResults);
  327. long nowTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  328. //判断评分状态是否发生变化,便于实时的更新评测基本信息
  329. if ((info.updateTime != nowTime && info.average != settlement.score) || info.sStatus != newStatus)
  330. {
  331. info.sRate = settlement.rate;
  332. info.sStatus = newStatus;
  333. info.updateTime = nowTime;
  334. info.average = settlement.score;
  335. await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync<ExamInfo>(info, info.id, new PartitionKey(info.code));
  336. }
  337. }
  338. }
  339. catch (Exception e)
  340. {
  341. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-{info.id}-评测going状态异常{e.Message}\n{e.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
  342. }
  343. finally
  344. {
  345. // 发送信息通知
  346. var messageEnd = new ServiceBusMessage(new { id = data.id, progress = "finish", code = data.code }.ToJsonString());
  347. messageEnd.ApplicationProperties.Add("name", "Exam");
  348. if (records.Count > 0)
  349. {
  350. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageEnd, DateTimeOffset.FromUnixTimeMilliseconds(data.endTime));
  351. try
  352. {
  353. await _serviceBus.GetServiceBusClient().CancelMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), records[0].sequenceNumber);
  354. }
  355. catch (Exception)
  356. {
  357. }
  358. records[0].sequenceNumber = end;
  359. await table.SaveOrUpdate<ChangeRecord>(records[0]);
  360. //await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(record, record.id, new Azure.Cosmos.PartitionKey($"{record.code}"));
  361. }
  362. else
  363. {
  364. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageEnd, DateTimeOffset.FromUnixTimeMilliseconds(data.endTime));
  365. //string pk = String.Format("{0}{1}{2}", info.code, "-", "going");
  366. ChangeRecord changeRecord = new ChangeRecord
  367. {
  368. RowKey = data.id,
  369. PartitionKey = PartitionKey,
  370. sequenceNumber = end,
  371. msgId = messageEnd.MessageId
  372. };
  373. await table.Save<ChangeRecord>(changeRecord);
  374. //await client.GetContainer(Constant.TEAMModelOS, "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
  375. }
  376. }
  377. break;
  378. case "finish":
  379. int fno = 0;
  380. try
  381. {
  382. //用来判定是否完成评分
  383. //bool isScore = true;
  384. Settlement settlement = await getMore(client, info, examClassResults);
  385. var isScore = examClassResults.SelectMany(e => e.studentScores).ToList().Exists(c => c.Contains(-1));
  386. int newStatus = 0;
  387. if (!isScore)
  388. {
  389. newStatus = 1;
  390. }
  391. /*//处理活动中间件
  392. List<StuActivity> stus = new();
  393. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<StuActivity>(
  394. queryText: $"select c.id from c where c.id = '{info.id}'"))
  395. {
  396. stus.Add(item);
  397. }*/
  398. /*if (info.source.Equals("1") && stus.Count == 0)
  399. {
  400. }*/
  401. await Activity(_coreAPIHttpService, info, classes, client, _dingDing, sub, examClassResults);
  402. foreach (ExamSubject subject in info.subjects)
  403. {
  404. await createClassResultAsync(info, examClassResults, subject, fno, _azureCosmos, _dingDing, _azureStorage);
  405. fno++;
  406. }
  407. //判断均分是否发生变化,便于实时的更新评测基本信息
  408. if (info.sRate != settlement.rate || info.average != settlement.score || info.sStatus != newStatus || info.qRate != settlement.qrate)
  409. {
  410. info.sRate = settlement.rate;
  411. info.average = settlement.score;
  412. info.sStatus = newStatus;
  413. info.lostStu = settlement.stus;
  414. info.stuCount = settlement.total;
  415. info.qRate = settlement.qrate;
  416. await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync<ExamInfo>(info, info.id, new Azure.Cosmos.PartitionKey(info.code));
  417. }
  418. }
  419. catch (Exception e)
  420. {
  421. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-{info.id}-评测finish状态异常{e.Message}\n{e.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
  422. }
  423. break;
  424. }
  425. }
  426. }
  427. catch (CosmosException e)
  428. {
  429. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-CosmosDB异常{e.Message}\n{e.StackTrace}\n{e.Status}", GroupNames.醍摩豆服務運維群組);
  430. }
  431. catch (Exception e)
  432. {
  433. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-评测结算异常{e.Message}\n{e.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
  434. }
  435. }
  436. public static async Task<Settlement> getMore(CosmosClient client, ExamInfo info, List<ExamClassResult> examClassResults)
  437. {
  438. //计算单次考试简易统计信息
  439. Settlement settlement = new();
  440. List<ExamResult> examResults = new();
  441. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<ExamResult>(
  442. queryText: $"select value(c) from c where c.examId = '{info.id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"ExamResult-{info.id}") }))
  443. {
  444. examResults.Add(item);
  445. }
  446. if (examResults.Count > 0)
  447. {
  448. List<Task<ItemResponse<ExamClassResult>>> tasks = new List<Task<ItemResponse<ExamClassResult>>>();
  449. //结算单科单班的标准差和平均分
  450. foreach (ExamClassResult classResult in examClassResults)
  451. {
  452. //标记单科单班总得分
  453. double subScore = 0;
  454. //标准差
  455. double sPowSum = 0;
  456. List<double> newSumScore = new List<double>();
  457. var scount = classResult.studentIds.Count;
  458. int index = 0;
  459. if (classResult.status.Count > 0)
  460. {
  461. foreach (var sta in classResult.status)
  462. {
  463. if (sta == 1)
  464. {
  465. for (int i = 0; i < classResult.studentScores[index].Count; i++)
  466. {
  467. if (classResult.studentScores[index][i] == -1)
  468. {
  469. classResult.studentScores[index][i] = 0;
  470. }
  471. }
  472. }
  473. else if (sta == 0)
  474. {
  475. var ans = classResult.studentScores[index];
  476. //该学生缺考,历史数据的判定
  477. if (ans.Count == 0)
  478. {
  479. for (int i = 0; i < classResult.studentScores[index].Count; i++)
  480. {
  481. if (classResult.studentScores[index][i] == -1)
  482. {
  483. classResult.studentScores[index][i] = 0;
  484. }
  485. }
  486. classResult.status[index] = 1;
  487. }
  488. }
  489. index++;
  490. }
  491. }
  492. else
  493. {
  494. List<int> sta = new();
  495. foreach (var id in classResult.studentIds) {
  496. sta.Add(0);
  497. }
  498. classResult.status = sta;
  499. foreach (var ans in classResult.studentAnswers)
  500. {
  501. //该学生缺考,历史数据的判定
  502. if (ans.Count == 0)
  503. {
  504. for (int i = 0; i < classResult.studentScores[index].Count; i++)
  505. {
  506. if (classResult.studentScores[index][i] == -1)
  507. {
  508. classResult.studentScores[index][i] = 0;
  509. }
  510. }
  511. classResult.status[index] = 1;
  512. }
  513. index++;
  514. }
  515. }
  516. int sIndex = 0;
  517. foreach (List<double> sc in classResult.studentScores)
  518. {
  519. List<double> newSc = new List<double>();
  520. foreach (double ssc in sc)
  521. {
  522. if (ssc == -1)
  523. {
  524. newSc.Add(0);
  525. }
  526. else
  527. {
  528. newSc.Add(ssc);
  529. }
  530. }
  531. double nc = Math.Round(newSc.Sum(), 2);
  532. newSumScore.Add(nc);
  533. subScore += nc;
  534. classResult.sum[sIndex] = nc;
  535. sIndex++;
  536. }
  537. double rateScore = scount > 0 ? Math.Round(subScore * 1.0 / scount, 2) : 0;
  538. foreach (double scs in newSumScore)
  539. {
  540. sPowSum += Math.Pow(scs - rateScore, 2);
  541. }
  542. classResult.standard = Math.Round(scount > 0 ? Math.Pow(sPowSum / scount, 0.5) : 0, 2);
  543. classResult.average = scount > 0 ? Math.Round(subScore / scount, 2) : 0;
  544. classResult.progress = true;
  545. tasks.Add(client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(classResult, classResult.id, new PartitionKey($"{classResult.code}")));
  546. }
  547. await Task.WhenAll(tasks);
  548. //记录某次考试所有学生得分总分
  549. double score = 0;
  550. double allScore = 0;
  551. int stuCount = 0;
  552. //标准差
  553. double powSum = 0;
  554. List<string> losStu = new List<string>();
  555. //当前完成考试得人数
  556. int total = examResults[0].studentIds.Count;
  557. //先与第一个值取并集
  558. losStu = losStu.Union(examResults[0].lostStus).ToList();
  559. foreach (ExamResult examResult in examResults)
  560. {
  561. if (info.id == examResult.examId)
  562. {
  563. foreach (List<double> sc in examResult.studentScores)
  564. {
  565. score += sc.Sum();
  566. }
  567. stuCount = examResult.studentIds.Count;
  568. }
  569. //powSum += Math.Pow(score - examResult.studentIds.Count > 0 ? Math.Round(score * 1.0 / examResult.studentIds.Count, 2) : 0, 2);
  570. //取交集
  571. losStu = losStu.Intersect(examResult.lostStus).ToList();
  572. }
  573. double NewsRateScore = stuCount > 0 ? Math.Round(score * 1.0 / stuCount, 2) : 0;
  574. foreach (PaperSimple simple in info.papers)
  575. {
  576. allScore += simple.point.Sum();
  577. }
  578. //计算全科标准差
  579. double qk = 0;
  580. foreach (string id in examResults[0].studentIds)
  581. {
  582. //计算整体缺考人数
  583. foreach (var ec in examClassResults)
  584. {
  585. int index = ec.studentIds.IndexOf(id);
  586. if (index > -1)
  587. {
  588. if (ec.status[index] == 1)
  589. {
  590. qk++;
  591. break;
  592. }
  593. }
  594. }
  595. double sc = 0;
  596. foreach (ExamResult result in examResults)
  597. {
  598. sc += result.studentScores[result.studentIds.IndexOf(id)].Sum();
  599. }
  600. powSum += Math.Pow(sc - NewsRateScore, 2);
  601. }
  602. info.standard = Math.Round(total > 0 ? Math.Pow(powSum / total, 0.5) : 0, 2);
  603. double NewsRate = allScore > 0 ? Math.Round(NewsRateScore / allScore * 100, 2) : 0;
  604. double qrate = Math.Round(qk / total * 100, 2);
  605. settlement.rate = NewsRate;
  606. settlement.score = NewsRateScore;
  607. settlement.stus = losStu;
  608. settlement.total = total;
  609. settlement.qrate = qrate;
  610. }
  611. return settlement;
  612. }
  613. //处理全部学生选题计数
  614. public static async Task examRecordCount(ExamInfo info, ExamSubject subject, DingDing _dingDing, int no, ExamResult result, List<ExamClassResult> classResults, AzureCosmosFactory _azureCosmos)
  615. {
  616. try
  617. {
  618. List<double> scores = new List<double>();
  619. foreach (List<double> sc in result.studentScores)
  620. {
  621. scores.Add(sc.Sum());
  622. }
  623. //确定高分组 最低分数
  624. scores.Sort((s1, s2) => { return s2.CompareTo(s1); });
  625. double rhwCount = Math.Floor(scores.Count * 0.27);
  626. double rhw = rhwCount > 0 ? scores[int.Parse(rhwCount.ToString("0"))] : 0;
  627. //确定低分组 最高分数
  628. //scores.Sort((s1, s2) => { return s1.CompareTo(s2); });
  629. double rhlCount = Math.Ceiling(scores.Count * 0.73);
  630. double rhl = rhlCount > 0 ? scores[int.Parse(rhlCount.ToString("0")) - 1] : 0;
  631. //存放高分组学生ID
  632. List<string> phId = new List<string>();
  633. List<string> plId = new List<string>();
  634. List<List<List<string>>> opth = new List<List<List<string>>>();
  635. List<List<List<string>>> optl = new List<List<List<string>>>();
  636. List<double> akp = await knowledgeCount(info, subject, _dingDing, no, classResults, rhwCount, rhw, rhlCount, rhl, _azureCosmos);
  637. List<double> afp = await fieldCount(info, subject, _dingDing, no, classResults, rhwCount, rhw, rhlCount, rhl, _azureCosmos);
  638. int PHCount = 0;
  639. int PLCount = 0;
  640. foreach (ExamClassResult classResult in classResults)
  641. {
  642. if (classResult.subjectId.Equals(subject.id))
  643. {
  644. foreach (string id in classResult.studentIds)
  645. {
  646. int index = classResult.studentIds.IndexOf(id);
  647. if (classResult.studentScores.Count > 0)
  648. {
  649. if (classResult.studentScores[index].Sum() >= rhw && PHCount < rhwCount)
  650. {
  651. if (classResult.ans.Count > 0)
  652. {
  653. opth.Add(classResult.ans[index]);
  654. PHCount++;
  655. continue;
  656. }
  657. }
  658. if (classResult.studentScores[index].Sum() <= rhl && PLCount < (scores.Count - rhlCount))
  659. {
  660. if (classResult.ans.Count > 0)
  661. {
  662. optl.Add(classResult.ans[index]);
  663. PLCount++;
  664. continue;
  665. }
  666. }
  667. }
  668. }
  669. }
  670. }
  671. result.phc = getMore(info, no, opth);
  672. result.plc = getMore(info, no, optl);
  673. result.afp = afp;
  674. result.akp = akp;
  675. }
  676. catch (Exception ex)
  677. {
  678. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-{info.id}-评测作答记录结算异常{ex.Message}\n{ex.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
  679. }
  680. }
  681. public static async Task<List<RGroupList>> Activity(CoreAPIHttpService _coreAPIHttpService, ExamInfo info, List<string> classes, CosmosClient client, DingDing _dingDing, List<string> sub, List<ExamClassResult> results)
  682. {
  683. List<(string pId, List<string> gid)> ps = new List<(string pId, List<string> gid)>();
  684. if (info.groupLists.Count > 0)
  685. {
  686. var group = info.groupLists;
  687. foreach (var gp in group)
  688. {
  689. foreach (KeyValuePair<string, List<string>> pp in gp)
  690. {
  691. ps.Add((pp.Key, pp.Value));
  692. }
  693. }
  694. }
  695. (List<RMember> tchList, List<RGroupList> classLists) = await GroupListService.GetStutmdidListids(_coreAPIHttpService, client, _dingDing, classes, info.school, ps);
  696. var addStudentsCls = tchList.FindAll(x => x.type == 2);
  697. var addTmdidsCls = tchList.FindAll(x => x.type == 1);
  698. List<StuActivity> stuActivities = new List<StuActivity>();
  699. List<StuActivity> tmdActivities = new List<StuActivity>();
  700. if (addTmdidsCls.IsNotEmpty())
  701. {
  702. addTmdidsCls.ForEach(x =>
  703. {
  704. HashSet<string> classIds = new HashSet<string>();
  705. classLists.ForEach(z =>
  706. {
  707. z.members.ForEach(y =>
  708. {
  709. if (y.id.Equals(x.id) && y.type == 1)
  710. {
  711. classIds.Add(z.id);
  712. }
  713. });
  714. });
  715. bool iss = false;
  716. //标记学生作答状态
  717. int ts = 0;
  718. if (results.Count > 0)
  719. {
  720. foreach (ExamClassResult exam in results)
  721. {
  722. int index = exam.studentIds.IndexOf(x.id);
  723. if (index != -1)
  724. {
  725. if (exam.studentAnswers[index].Count > 0)
  726. {
  727. bool flag = exam.studentScores[index].Exists(x => x == -1);
  728. if (!flag)
  729. {
  730. ts = 1;
  731. iss = true;
  732. break;
  733. }
  734. else
  735. {
  736. ts = 0;
  737. }
  738. }
  739. else
  740. {
  741. ts = -1;
  742. }
  743. }
  744. }
  745. }
  746. else
  747. {
  748. ts = -1;
  749. }
  750. tmdActivities.Add(new StuActivity
  751. {
  752. pk = "Activity",
  753. id = info.id,
  754. code = $"Activity-{x.id}",
  755. type = "Exam",
  756. name = info.name,
  757. source = info.source,
  758. startTime = info.startTime,
  759. endTime = info.endTime,
  760. scode = info.code,
  761. scope = info.scope,
  762. school = info.school,
  763. creatorId = info.creatorId,
  764. subjects = sub,
  765. blob = null,
  766. owner = info.owner,
  767. createTime = info.createTime,
  768. taskStatus = ts,
  769. qamode = info.qamode,
  770. ext = new Dictionary<string, JsonElement>() { { "type", info.examType.ToJsonString().ToObject<JsonElement>() }, { "subjects", info.subjects.ToJsonString().ToObject<JsonElement>() } },
  771. sStatus = iss ? 1 : 0,
  772. classIds = classIds.ToList()
  773. });
  774. });
  775. }
  776. if (addStudentsCls.IsNotEmpty())
  777. {
  778. addStudentsCls.ForEach(x =>
  779. {
  780. HashSet<string> classIds = new HashSet<string>();
  781. classLists.ForEach(z =>
  782. {
  783. z.members.ForEach(y =>
  784. {
  785. if (y.id.Equals(x.id) && y.code.Equals(info.school) && y.type == 2)
  786. {
  787. classIds.Add(z.id);
  788. }
  789. });
  790. });
  791. bool iss = false;
  792. //标记学生作答状态
  793. int ts = 0;
  794. if (results.Count > 0)
  795. {
  796. foreach (ExamClassResult exam in results)
  797. {
  798. int index = exam.studentIds.IndexOf(x.id);
  799. if (index != -1)
  800. {
  801. if (exam.studentAnswers[index].Count > 0)
  802. {
  803. bool flag = exam.studentScores[index].Exists(x => x == -1);
  804. if (!flag)
  805. {
  806. ts = 1;
  807. iss = true;
  808. break;
  809. }
  810. else
  811. {
  812. ts = 0;
  813. }
  814. }
  815. else
  816. {
  817. ts = -1;
  818. }
  819. }
  820. }
  821. }
  822. else
  823. {
  824. ts = -1;
  825. }
  826. stuActivities.Add(new StuActivity
  827. {
  828. pk = "Activity",
  829. id = info.id,
  830. code = $"Activity-{x.code.Replace("Base-", "")}-{x.id}",
  831. type = "Exam",
  832. name = info.name,
  833. source = info.source,
  834. startTime = info.startTime,
  835. endTime = info.endTime,
  836. scode = info.code,
  837. scope = info.scope,
  838. school = info.school,
  839. creatorId = info.creatorId,
  840. subjects = sub,
  841. blob = null,
  842. owner = info.owner,
  843. classIds = classIds.ToList(),
  844. createTime = info.createTime,
  845. qamode = info.qamode,
  846. ext = new Dictionary<string, JsonElement>() { { "type", info.examType.ToJsonString().ToObject<JsonElement>() },
  847. { "subjects", info.subjects.ToJsonString().ToObject<JsonElement>() } },
  848. taskStatus = ts,
  849. sStatus = iss ? 1 : 0,
  850. });
  851. });
  852. }
  853. await ActivityService.SaveStuActivity(client, _dingDing, stuActivities, tmdActivities, null);
  854. return classLists;
  855. }
  856. public static async Task<List<double>> knowledgeCount(ExamInfo info, ExamSubject subject, DingDing _dingDing, int no, List<ExamClassResult> classResults,
  857. double rhwCount, double rhw, double rhlCount, double rhl, AzureCosmosFactory _azureCosmos)
  858. {
  859. List<double> akp = new();
  860. try
  861. {
  862. int phcount = 0;
  863. int plcount = 0;
  864. //存放并去重知识点
  865. HashSet<string> kname = new HashSet<string>();
  866. if (info.papers[no].knowledge.Count > 0)
  867. {
  868. info.papers[no].knowledge.ForEach(kno =>
  869. {
  870. kno.ForEach(k =>
  871. {
  872. kname.Add(k);
  873. });
  874. });
  875. List<string> knowledgeName = new List<string>();
  876. foreach (string cla in kname)
  877. {
  878. knowledgeName.Add(cla);
  879. }
  880. for (int k = 0; k < knowledgeName.Count; k++)
  881. {
  882. if (null == knowledgeName[k])
  883. {
  884. knowledgeName.Remove(knowledgeName[k]);
  885. }
  886. }
  887. foreach (ExamClassResult classResult in classResults)
  888. {
  889. if (classResult.subjectId.Equals(subject.id))
  890. {
  891. //List<int> phc = new List<int>();
  892. List<int> ph = new List<int>();
  893. List<int> pl = new List<int>();
  894. List<int> pc = new List<int>();
  895. List<double> persent = new List<double>();
  896. for (int i = 0; i < knowledgeName.Count; i++)
  897. {
  898. //初始化单个知识点得分
  899. double score = 0;
  900. double allScore = 0;
  901. int n = 0;
  902. int phCount = 0;
  903. int plCount = 0;
  904. int pCount = 0;
  905. foreach (List<string> str in info.papers[no].knowledge)
  906. {
  907. if (str.Contains(knowledgeName[i]))
  908. {
  909. var itemPersent = str.Count > 0 ? 1 / Convert.ToDouble(str.Count) : 0;
  910. allScore += info.papers[no].point.Count > 0 ? info.papers[no].point[n] * itemPersent : 0;
  911. foreach (string id in classResult.studentIds)
  912. {
  913. int index = classResult.studentIds.IndexOf(id);
  914. if (classResult.studentScores.Count > 0)
  915. {
  916. if (classResult.studentScores[index].Count > 0)
  917. {
  918. score += classResult.studentScores[index][n] == -1 ? 0 : classResult.studentScores[index][n] * itemPersent;
  919. if (classResult.studentScores[index].Sum() >= rhw && phcount < rhwCount)
  920. {
  921. if (classResult.studentScores[index][n] <= 0)
  922. {
  923. phCount++;
  924. }
  925. phcount++;
  926. continue;
  927. }
  928. if (classResult.studentScores[index].Sum() <= rhl && plcount < (info.stuCount - rhlCount))
  929. {
  930. if (classResult.studentScores[index][n] <= 0)
  931. {
  932. plCount++;
  933. }
  934. plcount++;
  935. continue;
  936. }
  937. if (classResult.studentScores[index][n] <= 0)
  938. {
  939. pCount++;
  940. }
  941. }
  942. }
  943. }
  944. }
  945. n++;
  946. }
  947. pc.Add(pCount);
  948. ph.Add(phCount);
  949. pl.Add(plCount);
  950. double per = classResult.studentIds.Count > 0 ? Math.Round(score / classResult.studentIds.Count, 2) : 0;
  951. persent.Add(allScore > 0 ? Math.Round(per / allScore,2) : 0);
  952. }
  953. classResult.phc = ph;
  954. classResult.plc = pl;
  955. classResult.pc = pc;
  956. classResult.krate = persent;
  957. }
  958. //await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(classResult, classResult.id, new Azure.Cosmos.PartitionKey($"{classResult.code}"));
  959. }
  960. foreach (string k in knowledgeName)
  961. {
  962. double score = 0;
  963. double allScore = 0;
  964. int count = 0;
  965. foreach (ExamClassResult result in classResults)
  966. {
  967. if (result.subjectId.Equals(subject.id))
  968. {
  969. int n = 0;
  970. foreach (List<string> str in info.papers[no].knowledge)
  971. {
  972. if (str.Contains(k))
  973. {
  974. var itemPersent = str.Count > 0 ? 1 / Convert.ToDouble(str.Count) : 0;
  975. allScore += info.papers[no].point.Count > 0 ? info.papers[no].point[n] * itemPersent : 0;
  976. if (result.studentScores.Count > 0)
  977. {
  978. foreach (List<double> sc in result.studentScores)
  979. {
  980. double tsc = sc[n] == -1 ? 0 : sc[n];
  981. score += tsc * itemPersent;
  982. }
  983. }
  984. }
  985. n++;
  986. }
  987. count += result.studentIds.Count;
  988. }
  989. }
  990. double per = count > 0 ? Math.Round(score / count, 2) : 0;
  991. akp.Add(allScore > 0 ? Math.Round(per / allScore, 2) : 0);
  992. }
  993. }
  994. return akp;
  995. }
  996. catch (Exception ex)
  997. {
  998. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-{info.id}-评测知识点结算异常{ex.Message}\n{ex.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
  999. return null;
  1000. }
  1001. }
  1002. public static async Task<List<double>> fieldCount(ExamInfo info, ExamSubject subject, DingDing _dingDing, int no, List<ExamClassResult> classResults,
  1003. double rhwCount, double rhw, double rhlCount, double rhl, AzureCosmosFactory _azureCosmos)
  1004. {
  1005. List<double> afp = new();
  1006. try
  1007. {
  1008. int phcount = 0;
  1009. int plcount = 0;
  1010. //存放并去重知识点
  1011. List<int> knowledgeName = new List<int>() { 1, 2, 3, 4, 5, 6 };
  1012. /* knowledgeName.Add(1);
  1013. knowledgeName.Add(2);
  1014. knowledgeName.Add(3);
  1015. knowledgeName.Add(4);
  1016. knowledgeName.Add(5);
  1017. knowledgeName.Add(6);*/
  1018. foreach (ExamClassResult classResult in classResults)
  1019. {
  1020. if (classResult.subjectId.Equals(subject.id))
  1021. {
  1022. //List<int> phc = new List<int>();
  1023. List<int> ph = new List<int>();
  1024. List<int> pl = new List<int>();
  1025. List<int> pc = new List<int>();
  1026. List<double> persent = new List<double>();
  1027. for (int i = 0; i < knowledgeName.Count; i++)
  1028. {
  1029. //初始化单个知识点得分
  1030. double score = 0;
  1031. double allScore = 0;
  1032. int n = 0;
  1033. int phCount = 0;
  1034. int plCount = 0;
  1035. int pCount = 0;
  1036. if (info.papers[no].field.Count > 0)
  1037. {
  1038. foreach (int str in info.papers[no].field)
  1039. {
  1040. if (str == knowledgeName[i])
  1041. {
  1042. var itemPersent = 1;
  1043. allScore += info.papers[no].point.Count > 0 ? info.papers[no].point[n] * itemPersent : 0;
  1044. foreach (string id in classResult.studentIds)
  1045. {
  1046. int index = classResult.studentIds.IndexOf(id);
  1047. if (classResult.studentScores.Count > 0)
  1048. {
  1049. if (classResult.studentScores[index].Count > 0)
  1050. {
  1051. score += classResult.studentScores[index][n] == -1 ? 0 : classResult.studentScores[index][n];
  1052. if (classResult.studentScores[index].Sum() >= rhw && phcount < rhwCount)
  1053. {
  1054. if (classResult.studentScores[index][n] <= 0)
  1055. {
  1056. phCount++;
  1057. }
  1058. phcount++;
  1059. continue;
  1060. }
  1061. if (classResult.studentScores[index].Sum() <= rhl && plcount < (info.stuCount - rhlCount))
  1062. {
  1063. if (classResult.studentScores[index][n] <= 0)
  1064. {
  1065. plCount++;
  1066. }
  1067. plcount++;
  1068. continue;
  1069. }
  1070. if (classResult.studentScores[index][n] <= 0)
  1071. {
  1072. pCount++;
  1073. }
  1074. }
  1075. }
  1076. }
  1077. }
  1078. n++;
  1079. }
  1080. pc.Add(pCount);
  1081. ph.Add(phCount);
  1082. pl.Add(plCount);
  1083. double per = classResult.studentIds.Count > 0 ? Math.Round(score / classResult.studentIds.Count, 2) : 0;
  1084. persent.Add(allScore > 0 ? Math.Round(per / allScore, 2) : 0);
  1085. }
  1086. }
  1087. classResult.fphc = ph;
  1088. classResult.fplc = pl;
  1089. classResult.fpc = pc;
  1090. classResult.frate = persent;
  1091. }
  1092. //await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(classResult, classResult.id, new Azure.Cosmos.PartitionKey($"{classResult.code}"));
  1093. }
  1094. for (int i = 0; i < knowledgeName.Count; i++)
  1095. {
  1096. double score = 0;
  1097. double allScore = 0;
  1098. int count = 0;
  1099. foreach (ExamClassResult result in classResults)
  1100. {
  1101. if (result.subjectId.Equals(subject.id))
  1102. {
  1103. int n = 0;
  1104. foreach (int str in info.papers[no].field)
  1105. {
  1106. if (str == knowledgeName[i])
  1107. {
  1108. var itemPersent = 1;
  1109. allScore += info.papers[no].point.Count > 0 ? info.papers[no].point[n] * itemPersent : 0;
  1110. if (result.studentScores.Count > 0)
  1111. {
  1112. foreach (List<double> sc in result.studentScores)
  1113. {
  1114. double tsc = sc[n] == -1 ? 0 : sc[n];
  1115. score += tsc * itemPersent;
  1116. }
  1117. }
  1118. }
  1119. n++;
  1120. }
  1121. count += result.studentIds.Count;
  1122. }
  1123. }
  1124. double per = count > 0 ? Math.Round(score / count, 2) : 0;
  1125. afp.Add(allScore > 0 ? Math.Round(per / allScore, 2) : 0);
  1126. }
  1127. return afp;
  1128. }
  1129. catch (Exception ex)
  1130. {
  1131. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-{info.id}-评测认知层次结算异常{ex.Message}\n{ex.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
  1132. return null;
  1133. }
  1134. }
  1135. //处理选题计数
  1136. public static List<Dictionary<string, int>> getMore(ExamInfo info, int no, List<List<List<string>>> list)
  1137. {
  1138. List<Dictionary<string, int>> recorde = new List<Dictionary<string, int>>();
  1139. try
  1140. {
  1141. for (int i = 0; i < info.papers[no].answers.Count; i++)
  1142. {
  1143. if (info.papers[no].answers[i].Count <= 0)
  1144. {
  1145. recorde.Add(new Dictionary<string, int>());
  1146. continue;
  1147. }
  1148. Dictionary<string, int> optCount = new Dictionary<string, int>();
  1149. foreach (List<List<string>> stu in list)
  1150. {
  1151. if (stu.Count == info.papers[no].answers.Count)
  1152. {
  1153. var item = stu[i];
  1154. foreach (string opt in item)
  1155. {
  1156. if (optCount.ContainsKey(opt))
  1157. {
  1158. optCount[opt] = optCount[opt] + 1;
  1159. }
  1160. else
  1161. {
  1162. optCount[opt] = 1;
  1163. }
  1164. }
  1165. }
  1166. }
  1167. recorde.Add(optCount);
  1168. }
  1169. return recorde;
  1170. }
  1171. catch (Exception)
  1172. {
  1173. return recorde;
  1174. }
  1175. }
  1176. public static async Task createClassResultAsync(ExamInfo info, List<ExamClassResult> examClassResults, ExamSubject subject, int no, AzureCosmosFactory _azureCosmos, DingDing _dingDing, AzureStorageFactory _azureStorage)
  1177. {
  1178. //保证试卷信息与科目信息同步
  1179. ExamResult result = new ExamResult();
  1180. //人数总和
  1181. int Count = 0;
  1182. int m = 0;
  1183. double score = 0;
  1184. //标准差
  1185. double powSum = 0;
  1186. double allScore = info.papers[no].point.Sum();
  1187. List<ClassRange> classRanges = new List<ClassRange>();
  1188. List<string> lostStu = new List<string>();
  1189. List<double> csRate = new List<double>();
  1190. List<List<List<string>>> opt = new List<List<List<string>>>();
  1191. foreach (ExamClassResult classResult in examClassResults)
  1192. {
  1193. double classSrate = 0;
  1194. if (classResult.subjectId.Equals(subject.id))
  1195. {
  1196. foreach (List<List<string>> op in classResult.ans)
  1197. {
  1198. opt.Add(op);
  1199. }
  1200. //记录缺考学生索引位置
  1201. int index_stu = 0;
  1202. foreach (var ans in classResult.studentAnswers)
  1203. {
  1204. if (ans.Count == 0)
  1205. {
  1206. if (!lostStu.Contains(classResult.studentIds[index_stu]))
  1207. {
  1208. lostStu.Add(classResult.studentIds[index_stu]);
  1209. }
  1210. }
  1211. index_stu++;
  1212. }
  1213. int index = 0;
  1214. foreach (List<double> scores in classResult.studentScores)
  1215. {
  1216. List<double> newScores = new List<double>();
  1217. //int count = 0;
  1218. foreach (double sc in scores)
  1219. {
  1220. newScores.Add(sc > -1 ? sc : 0);
  1221. /*if (sc == -1)
  1222. {
  1223. count++;
  1224. }*/
  1225. }
  1226. /*if (count == scores.Count)
  1227. {
  1228. if (!lostStu.Contains(classResult.studentIds[index]))
  1229. {
  1230. lostStu.Add(classResult.studentIds[index]);
  1231. }
  1232. //mcount++;
  1233. }*/
  1234. classSrate += newScores.Sum();
  1235. score += newScores.Sum();
  1236. result.studentScores.Add(newScores);
  1237. index++;
  1238. }
  1239. //处理班级信息
  1240. ClassRange range = new ClassRange();
  1241. range.id = classResult.info.id;
  1242. range.name = classResult.info.name;
  1243. range.gradeId = classResult.gradeId;
  1244. List<int> ran = new List<int>();
  1245. int stuCount = classResult.studentIds.Count;
  1246. Count += stuCount;
  1247. if (m == 0)
  1248. {
  1249. ran.Add(0);
  1250. ran.Add(stuCount - 1);
  1251. }
  1252. else
  1253. {
  1254. ran.Add(Count - stuCount);
  1255. ran.Add(Count - 1);
  1256. }
  1257. m++;
  1258. range.range = ran;
  1259. classRanges.Add(range);
  1260. //处理学生ID
  1261. foreach (string id in classResult.studentIds)
  1262. {
  1263. result.studentIds.Add(id);
  1264. }
  1265. if (allScore > 0)
  1266. {
  1267. csRate.Add(classResult.studentIds.Count > 0 ? Math.Round(classSrate * 1.0 / classResult.studentIds.Count, 2) : 0 / allScore);
  1268. }
  1269. else
  1270. {
  1271. csRate.Add(0);
  1272. }
  1273. //powSum += Math.Pow(classSrate - result.average, 2);
  1274. //处理选项计数内容
  1275. }
  1276. }
  1277. /*foreach (string id in result.lostStus) {
  1278. if (!examClassResults[0].studentIds.Contains(id)) {
  1279. int index = result.lostStus.IndexOf(id);
  1280. result.lostStus.RemoveAt(index);
  1281. }
  1282. }*/
  1283. //处理人员变更时被移除的人员
  1284. if (result.lostStus.Count > 0)
  1285. {
  1286. for (int i = 0; i < result.lostStus.Count; i++)
  1287. {
  1288. if (!examClassResults[0].studentIds.Contains(result.lostStus[i]))
  1289. {
  1290. result.lostStus.RemoveAt(i);
  1291. }
  1292. }
  1293. }
  1294. await examRecordCount(info, subject, _dingDing, no, result, examClassResults, _azureCosmos);
  1295. result.record = getMore(info, no, opt);
  1296. result.average = result.studentIds.Count > 0 ? Math.Round(score * 1.0 / result.studentIds.Count, 2) : 0;
  1297. double stand = 0;
  1298. int sco = 0;
  1299. List<Task<ItemResponse<ExamClassResult>>> tasks = new();
  1300. foreach (ExamClassResult classResult in examClassResults)
  1301. {
  1302. //double classSrate = 0;
  1303. if (classResult.subjectId.Equals(subject.id))
  1304. {
  1305. stand += classResult.standard;
  1306. sco++;
  1307. }
  1308. tasks.Add(_azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(classResult, classResult.id, new PartitionKey($"{classResult.code}")));
  1309. }
  1310. await Task.WhenAll(tasks);
  1311. result.standard = sco > 0 ? Math.Round(stand / sco, 2) : 0;
  1312. result.csRate = csRate;
  1313. result.lostStus = lostStu;
  1314. result.sRate = allScore > 0 ? Math.Round(result.average / allScore * 100, 2) : 0;
  1315. result.classes = classRanges;
  1316. result.code = "ExamResult-" + info.id;
  1317. result.school = info.school;
  1318. result.id = subject.id;
  1319. result.examId = info.id;
  1320. result.subjectId = subject.id;
  1321. result.year = info.year;
  1322. result.paper = info.papers[no];
  1323. //result.point = info.papers[j].point;
  1324. result.scope = info.scope;
  1325. result.name = info.name;
  1326. result.time = info.startTime;
  1327. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Common").UpsertItemAsync(result, new Azure.Cosmos.PartitionKey($"ExamResult-{info.id}"));
  1328. }
  1329. public class Settlement
  1330. {
  1331. public double rate { get; set; }
  1332. public double score { get; set; }
  1333. public List<string> stus { get; set; } = new List<string>();
  1334. public int total { get; set; }
  1335. public double qrate { get; set; }
  1336. }
  1337. }
  1338. }