TriggerExam.cs 76 KB

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