TriggerExam.cs 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  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.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. await resultStatus(client, examClassResults);
  385. Settlement settlement = await getMore(client, info, examClassResults);
  386. var isScore = examClassResults.SelectMany(e => e.studentScores).ToList().Exists(c => c.Contains(-1));
  387. int newStatus = 0;
  388. if (!isScore)
  389. {
  390. newStatus = 1;
  391. }
  392. await Activity(_coreAPIHttpService, info, classes, client, _dingDing, sub, examClassResults);
  393. foreach (ExamSubject subject in info.subjects)
  394. {
  395. await createClassResultAsync(info, examClassResults, subject, fno, _azureCosmos, _dingDing, _azureStorage);
  396. fno++;
  397. }
  398. //判断均分是否发生变化,便于实时的更新评测基本信息
  399. if (info.sRate != settlement.rate || info.average != settlement.score || info.sStatus != newStatus || info.qRate != settlement.qrate)
  400. {
  401. info.sRate = settlement.rate;
  402. info.average = settlement.score;
  403. info.sStatus = newStatus;
  404. info.lostStu = settlement.stus;
  405. info.stuCount = settlement.total;
  406. info.qRate = settlement.qrate;
  407. await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync<ExamInfo>(info, info.id, new Azure.Cosmos.PartitionKey(info.code));
  408. }
  409. }
  410. catch (Exception e)
  411. {
  412. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-{info.id}-评测finish状态异常{e.Message}\n{e.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
  413. }
  414. break;
  415. }
  416. }
  417. }
  418. catch (CosmosException e)
  419. {
  420. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-CosmosDB异常{e.Message}\n{e.StackTrace}\n{e.Status}", GroupNames.醍摩豆服務運維群組);
  421. }
  422. catch (Exception e)
  423. {
  424. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-评测结算异常{e.Message}\n{e.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
  425. }
  426. }
  427. public static async Task resultStatus(CosmosClient client, List<ExamClassResult> examClassResults) {
  428. List<Task<ItemResponse<ExamClassResult>>> tasks = new();
  429. //结算单科单班的标准差和平均分
  430. foreach (ExamClassResult classResult in examClassResults)
  431. {
  432. //标记单科单班总得分
  433. double subScore = 0;
  434. //标准差
  435. double sPowSum = 0;
  436. List<double> newSumScore = new List<double>();
  437. var scount = classResult.studentIds.Count;
  438. int index = 0;
  439. if (classResult.status.Count > 0)
  440. {
  441. foreach (var sta in classResult.status)
  442. {
  443. if (sta == 1)
  444. {
  445. for (int i = 0; i < classResult.studentScores[index].Count; i++)
  446. {
  447. if (classResult.studentScores[index][i] == -1)
  448. {
  449. classResult.studentScores[index][i] = 0;
  450. }
  451. }
  452. }
  453. else if (sta == 0)
  454. {
  455. var ans = classResult.studentScores[index];
  456. //该学生缺考,历史数据的判定
  457. if (ans.Count == 0)
  458. {
  459. for (int i = 0; i < classResult.studentScores[index].Count; i++)
  460. {
  461. if (classResult.studentScores[index][i] == -1)
  462. {
  463. classResult.studentScores[index][i] = 0;
  464. }
  465. }
  466. classResult.status[index] = 1;
  467. }
  468. }
  469. index++;
  470. }
  471. }
  472. else
  473. {
  474. List<int> sta = new();
  475. foreach (var id in classResult.studentIds)
  476. {
  477. sta.Add(0);
  478. }
  479. classResult.status = sta;
  480. foreach (var ans in classResult.studentAnswers)
  481. {
  482. //该学生缺考,历史数据的判定
  483. if (ans.Count == 0)
  484. {
  485. for (int i = 0; i < classResult.studentScores[index].Count; i++)
  486. {
  487. if (classResult.studentScores[index][i] == -1)
  488. {
  489. classResult.studentScores[index][i] = 0;
  490. }
  491. }
  492. classResult.status[index] = 1;
  493. }
  494. index++;
  495. }
  496. }
  497. int sIndex = 0;
  498. foreach (List<double> sc in classResult.studentScores)
  499. {
  500. List<double> newSc = new List<double>();
  501. foreach (double ssc in sc)
  502. {
  503. if (ssc == -1)
  504. {
  505. newSc.Add(0);
  506. }
  507. else
  508. {
  509. newSc.Add(ssc);
  510. }
  511. }
  512. double nc = Math.Round(newSc.Sum(), 2);
  513. newSumScore.Add(nc);
  514. subScore += nc;
  515. classResult.sum[sIndex] = nc;
  516. sIndex++;
  517. }
  518. double rateScore = scount > 0 ? Math.Round(subScore * 1.0 / scount, 2) : 0;
  519. foreach (double scs in newSumScore)
  520. {
  521. sPowSum += Math.Pow(scs - rateScore, 2);
  522. }
  523. classResult.standard = Math.Round(scount > 0 ? Math.Pow(sPowSum / scount, 0.5) : 0, 2);
  524. classResult.average = scount > 0 ? Math.Round(subScore / scount, 2) : 0;
  525. classResult.progress = true;
  526. tasks.Add(client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(classResult, classResult.id, new PartitionKey($"{classResult.code}")));
  527. }
  528. await Task.WhenAll(tasks);
  529. }
  530. public static async Task<Settlement> getMore(CosmosClient client, ExamInfo info, List<ExamClassResult> examClassResults)
  531. {
  532. //计算单次考试简易统计信息
  533. Settlement settlement = new();
  534. List<ExamResult> examResults = new();
  535. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<ExamResult>(
  536. queryText: $"select value(c) from c where c.examId = '{info.id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"ExamResult-{info.id}") }))
  537. {
  538. examResults.Add(item);
  539. }
  540. if (examResults.Count > 0)
  541. {
  542. //记录某次考试所有学生得分总分
  543. double score = 0;
  544. double allScore = 0;
  545. int stuCount = 0;
  546. //标准差
  547. double powSum = 0;
  548. List<string> losStu = new List<string>();
  549. //当前完成考试得人数
  550. int total = examResults[0].studentIds.Distinct().Count();
  551. //先与第一个值取并集
  552. losStu = losStu.Union(examResults[0].lostStus).ToList();
  553. foreach (ExamResult examResult in examResults)
  554. {
  555. if (info.id == examResult.examId)
  556. {
  557. foreach (List<double> sc in examResult.studentScores)
  558. {
  559. score += sc.Sum();
  560. }
  561. stuCount = examResult.studentIds.Count;
  562. }
  563. //powSum += Math.Pow(score - examResult.studentIds.Count > 0 ? Math.Round(score * 1.0 / examResult.studentIds.Count, 2) : 0, 2);
  564. //取交集
  565. losStu = losStu.Intersect(examResult.lostStus).ToList();
  566. }
  567. double NewsRateScore = stuCount > 0 ? Math.Round(score * 1.0 / stuCount, 2) : 0;
  568. foreach (PaperSimple simple in info.papers)
  569. {
  570. allScore += simple.point.Sum();
  571. }
  572. //计算全科标准差
  573. double qk = 0;
  574. foreach (string id in examResults[0].studentIds)
  575. {
  576. //计算整体缺考人数
  577. foreach (var ec in examClassResults)
  578. {
  579. int index = ec.studentIds.IndexOf(id);
  580. if (index > -1)
  581. {
  582. if (ec.status[index] == 1)
  583. {
  584. qk++;
  585. break;
  586. }
  587. }
  588. }
  589. double sc = 0;
  590. foreach (ExamResult result in examResults)
  591. {
  592. sc += result.studentScores[result.studentIds.IndexOf(id)].Sum();
  593. }
  594. powSum += Math.Pow(sc - NewsRateScore, 2);
  595. }
  596. info.standard = Math.Round(total > 0 ? Math.Pow(powSum / total, 0.5) : 0, 2);
  597. double NewsRate = allScore > 0 ? Math.Round(NewsRateScore / allScore * 100, 2) : 0;
  598. double qrate = Math.Round(qk / total * 100, 2);
  599. settlement.rate = NewsRate;
  600. settlement.score = NewsRateScore;
  601. settlement.stus = losStu;
  602. settlement.total = total;
  603. settlement.qrate = qrate;
  604. }
  605. return settlement;
  606. }
  607. //处理全部学生选题计数
  608. public static async Task examRecordCount(ExamInfo info, ExamSubject subject, DingDing _dingDing, int no, ExamResult result, List<ExamClassResult> classResults, AzureCosmosFactory _azureCosmos)
  609. {
  610. try
  611. {
  612. List<double> scores = new List<double>();
  613. foreach (List<double> sc in result.studentScores)
  614. {
  615. scores.Add(sc.Sum());
  616. }
  617. //确定高分组 最低分数
  618. scores.Sort((s1, s2) => { return s2.CompareTo(s1); });
  619. double rhwCount = Math.Floor(scores.Count * 0.27);
  620. double rhw = rhwCount > 0 ? scores[int.Parse(rhwCount.ToString("0"))] : 0;
  621. //确定低分组 最高分数
  622. //scores.Sort((s1, s2) => { return s1.CompareTo(s2); });
  623. double rhlCount = Math.Ceiling(scores.Count * 0.73);
  624. double rhl = rhlCount > 0 ? scores[int.Parse(rhlCount.ToString("0")) - 1] : 0;
  625. //存放高分组学生ID
  626. List<string> phId = new List<string>();
  627. List<string> plId = new List<string>();
  628. List<List<List<string>>> opth = new List<List<List<string>>>();
  629. List<List<List<string>>> optl = new List<List<List<string>>>();
  630. List<double> akp = await knowledgeCount(info, subject, _dingDing, no, classResults, rhwCount, rhw, rhlCount, rhl, _azureCosmos);
  631. List<double> afp = await fieldCount(info, subject, _dingDing, no, classResults, rhwCount, rhw, rhlCount, rhl, _azureCosmos);
  632. int PHCount = 0;
  633. int PLCount = 0;
  634. foreach (ExamClassResult classResult in classResults)
  635. {
  636. if (classResult.subjectId.Equals(subject.id))
  637. {
  638. foreach (string id in classResult.studentIds)
  639. {
  640. int index = classResult.studentIds.IndexOf(id);
  641. if (classResult.studentScores.Count > 0)
  642. {
  643. if (classResult.studentScores[index].Sum() >= rhw && PHCount < rhwCount)
  644. {
  645. if (classResult.ans.Count > 0)
  646. {
  647. opth.Add(classResult.ans[index]);
  648. PHCount++;
  649. continue;
  650. }
  651. }
  652. if (classResult.studentScores[index].Sum() <= rhl && PLCount < (scores.Count - rhlCount))
  653. {
  654. if (classResult.ans.Count > 0)
  655. {
  656. optl.Add(classResult.ans[index]);
  657. PLCount++;
  658. continue;
  659. }
  660. }
  661. }
  662. }
  663. }
  664. }
  665. result.phc = getMore(info, no, opth);
  666. result.plc = getMore(info, no, optl);
  667. result.afp = afp;
  668. result.akp = akp;
  669. }
  670. catch (Exception ex)
  671. {
  672. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-{info.id}-评测作答记录结算异常{ex.Message}\n{ex.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
  673. }
  674. }
  675. public static async Task<List<RGroupList>> Activity(CoreAPIHttpService _coreAPIHttpService, ExamInfo info, List<string> classes, CosmosClient client, DingDing _dingDing, List<string> sub, List<ExamClassResult> results)
  676. {
  677. List<(string pId, List<string> gid)> ps = new List<(string pId, List<string> gid)>();
  678. if (info.groupLists.Count > 0)
  679. {
  680. var group = info.groupLists;
  681. foreach (var gp in group)
  682. {
  683. foreach (KeyValuePair<string, List<string>> pp in gp)
  684. {
  685. ps.Add((pp.Key, pp.Value));
  686. }
  687. }
  688. }
  689. (List<RMember> tchList, List<RGroupList> classLists) = await GroupListService.GetStutmdidListids(_coreAPIHttpService, client, _dingDing, classes, info.school, ps);
  690. var addStudentsCls = tchList.FindAll(x => x.type == 2);
  691. var addTmdidsCls = tchList.FindAll(x => x.type == 1);
  692. List<StuActivity> stuActivities = new List<StuActivity>();
  693. List<StuActivity> tmdActivities = new List<StuActivity>();
  694. if (addTmdidsCls.IsNotEmpty())
  695. {
  696. addTmdidsCls.ForEach(x =>
  697. {
  698. HashSet<string> classIds = new HashSet<string>();
  699. classLists.ForEach(z =>
  700. {
  701. z.members.ForEach(y =>
  702. {
  703. if (y.id.Equals(x.id) && y.type == 1)
  704. {
  705. classIds.Add(z.id);
  706. }
  707. });
  708. });
  709. bool iss = false;
  710. //标记学生作答状态
  711. int ts = 0;
  712. if (results.Count > 0)
  713. {
  714. foreach (ExamClassResult exam in results)
  715. {
  716. int index = exam.studentIds.IndexOf(x.id);
  717. if (index != -1)
  718. {
  719. if (exam.studentAnswers[index].Count > 0)
  720. {
  721. bool flag = exam.studentScores[index].Exists(x => x == -1);
  722. if (!flag)
  723. {
  724. ts = 1;
  725. iss = true;
  726. break;
  727. }
  728. else
  729. {
  730. ts = 0;
  731. }
  732. }
  733. else
  734. {
  735. ts = -1;
  736. }
  737. }
  738. }
  739. }
  740. else
  741. {
  742. ts = -1;
  743. }
  744. tmdActivities.Add(new StuActivity
  745. {
  746. pk = "Activity",
  747. id = info.id,
  748. code = $"Activity-{x.id}",
  749. type = "Exam",
  750. name = info.name,
  751. source = info.source,
  752. startTime = info.startTime,
  753. endTime = info.endTime,
  754. scode = info.code,
  755. scope = info.scope,
  756. school = info.school,
  757. creatorId = info.creatorId,
  758. subjects = sub,
  759. blob = null,
  760. owner = info.owner,
  761. createTime = info.createTime,
  762. taskStatus = ts,
  763. qamode = info.qamode,
  764. ext = new Dictionary<string, JsonElement>() { { "type", info.examType.ToJsonString().ToObject<JsonElement>() }, { "subjects", info.subjects.ToJsonString().ToObject<JsonElement>() } },
  765. sStatus = iss ? 1 : 0,
  766. classIds = classIds.ToList()
  767. });
  768. });
  769. }
  770. if (addStudentsCls.IsNotEmpty())
  771. {
  772. addStudentsCls.ForEach(x =>
  773. {
  774. HashSet<string> classIds = new HashSet<string>();
  775. classLists.ForEach(z =>
  776. {
  777. z.members.ForEach(y =>
  778. {
  779. if (y.id.Equals(x.id) && y.code.Equals(info.school) && y.type == 2)
  780. {
  781. classIds.Add(z.id);
  782. }
  783. });
  784. });
  785. bool iss = false;
  786. //标记学生作答状态
  787. int ts = 0;
  788. if (results.Count > 0)
  789. {
  790. foreach (ExamClassResult exam in results)
  791. {
  792. int index = exam.studentIds.IndexOf(x.id);
  793. if (index != -1)
  794. {
  795. if (exam.studentAnswers[index].Count > 0)
  796. {
  797. bool flag = exam.studentScores[index].Exists(x => x == -1);
  798. if (!flag)
  799. {
  800. ts = 1;
  801. iss = true;
  802. break;
  803. }
  804. else
  805. {
  806. ts = 0;
  807. }
  808. }
  809. else
  810. {
  811. ts = -1;
  812. }
  813. }
  814. }
  815. }
  816. else
  817. {
  818. ts = -1;
  819. }
  820. stuActivities.Add(new StuActivity
  821. {
  822. pk = "Activity",
  823. id = info.id,
  824. code = $"Activity-{x.code.Replace("Base-", "")}-{x.id}",
  825. type = "Exam",
  826. name = info.name,
  827. source = info.source,
  828. startTime = info.startTime,
  829. endTime = info.endTime,
  830. scode = info.code,
  831. scope = info.scope,
  832. school = info.school,
  833. creatorId = info.creatorId,
  834. subjects = sub,
  835. blob = null,
  836. owner = info.owner,
  837. classIds = classIds.ToList(),
  838. createTime = info.createTime,
  839. qamode = info.qamode,
  840. ext = new Dictionary<string, JsonElement>() { { "type", info.examType.ToJsonString().ToObject<JsonElement>() },
  841. { "subjects", info.subjects.ToJsonString().ToObject<JsonElement>() } },
  842. taskStatus = ts,
  843. sStatus = iss ? 1 : 0,
  844. });
  845. });
  846. }
  847. await ActivityService.SaveStuActivity(client, _dingDing, stuActivities, tmdActivities, null);
  848. return classLists;
  849. }
  850. public static async Task<List<double>> knowledgeCount(ExamInfo info, ExamSubject subject, DingDing _dingDing, int no, List<ExamClassResult> classResults,
  851. double rhwCount, double rhw, double rhlCount, double rhl, AzureCosmosFactory _azureCosmos)
  852. {
  853. List<double> akp = new();
  854. try
  855. {
  856. int phcount = 0;
  857. int plcount = 0;
  858. //存放并去重知识点
  859. HashSet<string> kname = new HashSet<string>();
  860. if (info.papers[no].knowledge.Count > 0)
  861. {
  862. info.papers[no].knowledge.ForEach(kno =>
  863. {
  864. kno.ForEach(k =>
  865. {
  866. kname.Add(k);
  867. });
  868. });
  869. List<string> knowledgeName = new List<string>();
  870. foreach (string cla in kname)
  871. {
  872. knowledgeName.Add(cla);
  873. }
  874. for (int k = 0; k < knowledgeName.Count; k++)
  875. {
  876. if (null == knowledgeName[k])
  877. {
  878. knowledgeName.Remove(knowledgeName[k]);
  879. }
  880. }
  881. foreach (ExamClassResult classResult in classResults)
  882. {
  883. if (classResult.subjectId.Equals(subject.id))
  884. {
  885. //List<int> phc = new List<int>();
  886. List<int> ph = new List<int>();
  887. List<int> pl = new List<int>();
  888. List<int> pc = new List<int>();
  889. List<double> persent = new List<double>();
  890. for (int i = 0; i < knowledgeName.Count; i++)
  891. {
  892. //初始化单个知识点得分
  893. double score = 0;
  894. double allScore = 0;
  895. int n = 0;
  896. int phCount = 0;
  897. int plCount = 0;
  898. int pCount = 0;
  899. foreach (List<string> str in info.papers[no].knowledge)
  900. {
  901. if (str.Contains(knowledgeName[i]))
  902. {
  903. var itemPersent = str.Count > 0 ? 1 / Convert.ToDouble(str.Count) : 0;
  904. allScore += info.papers[no].point.Count > 0 ? info.papers[no].point[n] * itemPersent : 0;
  905. foreach (string id in classResult.studentIds)
  906. {
  907. int index = classResult.studentIds.IndexOf(id);
  908. if (classResult.studentScores.Count > 0)
  909. {
  910. if (classResult.studentScores[index].Count > 0)
  911. {
  912. score += classResult.studentScores[index][n] == -1 ? 0 : classResult.studentScores[index][n] * itemPersent;
  913. if (classResult.studentScores[index].Sum() >= rhw && phcount < rhwCount)
  914. {
  915. if (classResult.studentScores[index][n] <= 0)
  916. {
  917. phCount++;
  918. }
  919. phcount++;
  920. continue;
  921. }
  922. if (classResult.studentScores[index].Sum() <= rhl && plcount < (info.stuCount - rhlCount))
  923. {
  924. if (classResult.studentScores[index][n] <= 0)
  925. {
  926. plCount++;
  927. }
  928. plcount++;
  929. continue;
  930. }
  931. if (classResult.studentScores[index][n] <= 0)
  932. {
  933. pCount++;
  934. }
  935. }
  936. }
  937. }
  938. }
  939. n++;
  940. }
  941. pc.Add(pCount);
  942. ph.Add(phCount);
  943. pl.Add(plCount);
  944. double per = classResult.studentIds.Count > 0 ? Math.Round(score / classResult.studentIds.Count, 2) : 0;
  945. persent.Add(allScore > 0 ? Math.Round(per / allScore,2) : 0);
  946. }
  947. classResult.phc = ph;
  948. classResult.plc = pl;
  949. classResult.pc = pc;
  950. classResult.krate = persent;
  951. }
  952. //await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(classResult, classResult.id, new Azure.Cosmos.PartitionKey($"{classResult.code}"));
  953. }
  954. foreach (string k in knowledgeName)
  955. {
  956. double score = 0;
  957. double allScore = 0;
  958. int count = 0;
  959. foreach (ExamClassResult result in classResults)
  960. {
  961. if (result.subjectId.Equals(subject.id))
  962. {
  963. int n = 0;
  964. foreach (List<string> str in info.papers[no].knowledge)
  965. {
  966. if (str.Contains(k))
  967. {
  968. var itemPersent = str.Count > 0 ? 1 / Convert.ToDouble(str.Count) : 0;
  969. allScore += info.papers[no].point.Count > 0 ? info.papers[no].point[n] * itemPersent : 0;
  970. if (result.studentScores.Count > 0)
  971. {
  972. foreach (List<double> sc in result.studentScores)
  973. {
  974. double tsc = sc[n] == -1 ? 0 : sc[n];
  975. score += tsc * itemPersent;
  976. }
  977. }
  978. }
  979. n++;
  980. }
  981. count += result.studentIds.Count;
  982. }
  983. }
  984. double per = count > 0 ? Math.Round(score / count, 2) : 0;
  985. akp.Add(allScore > 0 ? Math.Round(per / allScore, 2) : 0);
  986. }
  987. }
  988. return akp;
  989. }
  990. catch (Exception ex)
  991. {
  992. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-{info.id}-评测知识点结算异常{ex.Message}\n{ex.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
  993. return null;
  994. }
  995. }
  996. public static async Task<List<double>> fieldCount(ExamInfo info, ExamSubject subject, DingDing _dingDing, int no, List<ExamClassResult> classResults,
  997. double rhwCount, double rhw, double rhlCount, double rhl, AzureCosmosFactory _azureCosmos)
  998. {
  999. List<double> afp = new();
  1000. try
  1001. {
  1002. int phcount = 0;
  1003. int plcount = 0;
  1004. //存放并去重知识点
  1005. List<int> knowledgeName = new List<int>() { 1, 2, 3, 4, 5, 6 };
  1006. /* knowledgeName.Add(1);
  1007. knowledgeName.Add(2);
  1008. knowledgeName.Add(3);
  1009. knowledgeName.Add(4);
  1010. knowledgeName.Add(5);
  1011. knowledgeName.Add(6);*/
  1012. foreach (ExamClassResult classResult in classResults)
  1013. {
  1014. if (classResult.subjectId.Equals(subject.id))
  1015. {
  1016. //List<int> phc = new List<int>();
  1017. List<int> ph = new List<int>();
  1018. List<int> pl = new List<int>();
  1019. List<int> pc = new List<int>();
  1020. List<double> persent = new List<double>();
  1021. for (int i = 0; i < knowledgeName.Count; i++)
  1022. {
  1023. //初始化单个知识点得分
  1024. double score = 0;
  1025. double allScore = 0;
  1026. int n = 0;
  1027. int phCount = 0;
  1028. int plCount = 0;
  1029. int pCount = 0;
  1030. if (info.papers[no].field.Count > 0)
  1031. {
  1032. foreach (int str in info.papers[no].field)
  1033. {
  1034. if (str == knowledgeName[i])
  1035. {
  1036. var itemPersent = 1;
  1037. allScore += info.papers[no].point.Count > 0 ? info.papers[no].point[n] * itemPersent : 0;
  1038. foreach (string id in classResult.studentIds)
  1039. {
  1040. int index = classResult.studentIds.IndexOf(id);
  1041. if (classResult.studentScores.Count > 0)
  1042. {
  1043. if (classResult.studentScores[index].Count > 0)
  1044. {
  1045. score += classResult.studentScores[index][n] == -1 ? 0 : classResult.studentScores[index][n];
  1046. if (classResult.studentScores[index].Sum() >= rhw && phcount < rhwCount)
  1047. {
  1048. if (classResult.studentScores[index][n] <= 0)
  1049. {
  1050. phCount++;
  1051. }
  1052. phcount++;
  1053. continue;
  1054. }
  1055. if (classResult.studentScores[index].Sum() <= rhl && plcount < (info.stuCount - rhlCount))
  1056. {
  1057. if (classResult.studentScores[index][n] <= 0)
  1058. {
  1059. plCount++;
  1060. }
  1061. plcount++;
  1062. continue;
  1063. }
  1064. if (classResult.studentScores[index][n] <= 0)
  1065. {
  1066. pCount++;
  1067. }
  1068. }
  1069. }
  1070. }
  1071. }
  1072. n++;
  1073. }
  1074. pc.Add(pCount);
  1075. ph.Add(phCount);
  1076. pl.Add(plCount);
  1077. double per = classResult.studentIds.Count > 0 ? Math.Round(score / classResult.studentIds.Count, 2) : 0;
  1078. persent.Add(allScore > 0 ? Math.Round(per / allScore, 2) : 0);
  1079. }
  1080. }
  1081. classResult.fphc = ph;
  1082. classResult.fplc = pl;
  1083. classResult.fpc = pc;
  1084. classResult.frate = persent;
  1085. }
  1086. //await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(classResult, classResult.id, new Azure.Cosmos.PartitionKey($"{classResult.code}"));
  1087. }
  1088. for (int i = 0; i < knowledgeName.Count; i++)
  1089. {
  1090. double score = 0;
  1091. double allScore = 0;
  1092. int count = 0;
  1093. foreach (ExamClassResult result in classResults)
  1094. {
  1095. if (result.subjectId.Equals(subject.id))
  1096. {
  1097. int n = 0;
  1098. foreach (int str in info.papers[no].field)
  1099. {
  1100. if (str == knowledgeName[i])
  1101. {
  1102. var itemPersent = 1;
  1103. allScore += info.papers[no].point.Count > 0 ? info.papers[no].point[n] * itemPersent : 0;
  1104. if (result.studentScores.Count > 0)
  1105. {
  1106. foreach (List<double> sc in result.studentScores)
  1107. {
  1108. double tsc = sc[n] == -1 ? 0 : sc[n];
  1109. score += tsc * itemPersent;
  1110. }
  1111. }
  1112. }
  1113. n++;
  1114. }
  1115. count += result.studentIds.Count;
  1116. }
  1117. }
  1118. double per = count > 0 ? Math.Round(score / count, 2) : 0;
  1119. afp.Add(allScore > 0 ? Math.Round(per / allScore, 2) : 0);
  1120. }
  1121. return afp;
  1122. }
  1123. catch (Exception ex)
  1124. {
  1125. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-{info.id}-评测认知层次结算异常{ex.Message}\n{ex.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
  1126. return null;
  1127. }
  1128. }
  1129. //处理选题计数
  1130. public static List<Dictionary<string, int>> getMore(ExamInfo info, int no, List<List<List<string>>> list)
  1131. {
  1132. List<Dictionary<string, int>> recorde = new List<Dictionary<string, int>>();
  1133. try
  1134. {
  1135. for (int i = 0; i < info.papers[no].answers.Count; i++)
  1136. {
  1137. if (info.papers[no].answers[i].Count <= 0)
  1138. {
  1139. recorde.Add(new Dictionary<string, int>());
  1140. continue;
  1141. }
  1142. Dictionary<string, int> optCount = new Dictionary<string, int>();
  1143. foreach (List<List<string>> stu in list)
  1144. {
  1145. if (stu.Count == info.papers[no].answers.Count)
  1146. {
  1147. var item = stu[i];
  1148. foreach (string opt in item)
  1149. {
  1150. if (optCount.ContainsKey(opt))
  1151. {
  1152. optCount[opt] = optCount[opt] + 1;
  1153. }
  1154. else
  1155. {
  1156. optCount[opt] = 1;
  1157. }
  1158. }
  1159. }
  1160. }
  1161. recorde.Add(optCount);
  1162. }
  1163. return recorde;
  1164. }
  1165. catch (Exception)
  1166. {
  1167. return recorde;
  1168. }
  1169. }
  1170. public static async Task createClassResultAsync(ExamInfo info, List<ExamClassResult> examClassResults, ExamSubject subject, int no, AzureCosmosFactory _azureCosmos, DingDing _dingDing, AzureStorageFactory _azureStorage)
  1171. {
  1172. //保证试卷信息与科目信息同步
  1173. ExamResult result = new ExamResult();
  1174. //人数总和
  1175. int Count = 0;
  1176. int m = 0;
  1177. double score = 0;
  1178. //标准差
  1179. double powSum = 0;
  1180. double allScore = info.papers[no].point.Sum();
  1181. List<ClassRange> classRanges = new List<ClassRange>();
  1182. List<string> lostStu = new List<string>();
  1183. List<double> csRate = new List<double>();
  1184. List<List<List<string>>> opt = new List<List<List<string>>>();
  1185. foreach (ExamClassResult classResult in examClassResults)
  1186. {
  1187. double classSrate = 0;
  1188. if (classResult.subjectId.Equals(subject.id))
  1189. {
  1190. foreach (List<List<string>> op in classResult.ans)
  1191. {
  1192. opt.Add(op);
  1193. }
  1194. //记录缺考学生索引位置
  1195. int index_stu = 0;
  1196. foreach (var ans in classResult.studentAnswers)
  1197. {
  1198. if (ans.Count == 0)
  1199. {
  1200. if (!lostStu.Contains(classResult.studentIds[index_stu]))
  1201. {
  1202. lostStu.Add(classResult.studentIds[index_stu]);
  1203. }
  1204. }
  1205. index_stu++;
  1206. }
  1207. int index = 0;
  1208. foreach (List<double> scores in classResult.studentScores)
  1209. {
  1210. List<double> newScores = new List<double>();
  1211. //int count = 0;
  1212. foreach (double sc in scores)
  1213. {
  1214. newScores.Add(sc > -1 ? sc : 0);
  1215. }
  1216. classSrate += newScores.Sum();
  1217. score += newScores.Sum();
  1218. result.studentScores.Add(newScores);
  1219. index++;
  1220. }
  1221. //处理班级信息
  1222. ClassRange range = new ClassRange();
  1223. range.id = classResult.info.id;
  1224. range.name = classResult.info.name;
  1225. range.gradeId = classResult.gradeId;
  1226. List<int> ran = new List<int>();
  1227. int stuCount = classResult.studentIds.Count;
  1228. Count += stuCount;
  1229. if (m == 0)
  1230. {
  1231. ran.Add(0);
  1232. ran.Add(stuCount - 1);
  1233. }
  1234. else
  1235. {
  1236. ran.Add(Count - stuCount);
  1237. ran.Add(Count - 1);
  1238. }
  1239. m++;
  1240. range.range = ran;
  1241. classRanges.Add(range);
  1242. //处理学生ID
  1243. foreach (string id in classResult.studentIds)
  1244. {
  1245. result.studentIds.Add(id);
  1246. }
  1247. if (allScore > 0)
  1248. {
  1249. csRate.Add(classResult.studentIds.Count > 0 ? Math.Round(classSrate * 1.0 / classResult.studentIds.Count, 2) : 0 / allScore);
  1250. }
  1251. else
  1252. {
  1253. csRate.Add(0);
  1254. }
  1255. //powSum += Math.Pow(classSrate - result.average, 2);
  1256. //处理选项计数内容
  1257. }
  1258. }
  1259. /*foreach (string id in result.lostStus) {
  1260. if (!examClassResults[0].studentIds.Contains(id)) {
  1261. int index = result.lostStus.IndexOf(id);
  1262. result.lostStus.RemoveAt(index);
  1263. }
  1264. }*/
  1265. //处理人员变更时被移除的人员
  1266. if (result.lostStus.Count > 0)
  1267. {
  1268. for (int i = 0; i < result.lostStus.Count; i++)
  1269. {
  1270. if (!examClassResults[0].studentIds.Contains(result.lostStus[i]))
  1271. {
  1272. result.lostStus.RemoveAt(i);
  1273. }
  1274. }
  1275. }
  1276. await examRecordCount(info, subject, _dingDing, no, result, examClassResults, _azureCosmos);
  1277. result.record = getMore(info, no, opt);
  1278. result.average = result.studentIds.Count > 0 ? Math.Round(score * 1.0 / result.studentIds.Count, 2) : 0;
  1279. double stand = 0;
  1280. int sco = 0;
  1281. List<Task<ItemResponse<ExamClassResult>>> tasks = new();
  1282. foreach (ExamClassResult classResult in examClassResults)
  1283. {
  1284. //double classSrate = 0;
  1285. if (classResult.subjectId.Equals(subject.id))
  1286. {
  1287. stand += classResult.standard;
  1288. sco++;
  1289. }
  1290. tasks.Add(_azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(classResult, classResult.id, new PartitionKey($"{classResult.code}")));
  1291. }
  1292. await Task.WhenAll(tasks);
  1293. result.standard = sco > 0 ? Math.Round(stand / sco, 2) : 0;
  1294. result.csRate = csRate;
  1295. result.lostStus = lostStu;
  1296. result.sRate = allScore > 0 ? Math.Round(result.average / allScore * 100, 2) : 0;
  1297. result.classes = classRanges;
  1298. result.code = "ExamResult-" + info.id;
  1299. result.school = info.school;
  1300. result.id = subject.id;
  1301. result.examId = info.id;
  1302. result.subjectId = subject.id;
  1303. result.year = info.year;
  1304. result.paper = info.papers[no];
  1305. //result.point = info.papers[j].point;
  1306. result.scope = info.scope;
  1307. result.name = info.name;
  1308. result.time = info.startTime;
  1309. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Common").UpsertItemAsync(result, new Azure.Cosmos.PartitionKey($"ExamResult-{info.id}"));
  1310. }
  1311. public class Settlement
  1312. {
  1313. public double rate { get; set; }
  1314. public double score { get; set; }
  1315. public List<string> stus { get; set; } = new List<string>();
  1316. public int total { get; set; }
  1317. public double qrate { get; set; }
  1318. }
  1319. }
  1320. }