TriggerExam.cs 75 KB

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