TriggerExam.cs 77 KB

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