TriggerExam.cs 74 KB

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