TriggerExam.cs 74 KB

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