TriggerExam.cs 72 KB

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