TriggerExam.cs 72 KB

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