TriggerExam.cs 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  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. ext = new Dictionary<string, JsonElement>() { { "type", info.type.ToJsonString().ToObject<JsonElement>() }, { "subjects", info.subjects.ToJsonString().ToObject<JsonElement>() } },
  713. sStatus = iss ? 1 : 0,
  714. classIds = classIds.ToList()
  715. }); ;
  716. });
  717. }
  718. if (addStudentsCls.IsNotEmpty())
  719. {
  720. addStudentsCls.ForEach(x =>
  721. {
  722. HashSet<string> classIds = new HashSet<string>();
  723. classLists.ForEach(z =>
  724. {
  725. z.members.ForEach(y =>
  726. {
  727. if (y.id.Equals(x.id) && y.code.Equals(info.school) && y.type == 2)
  728. {
  729. classIds.Add(z.id);
  730. }
  731. });
  732. });
  733. bool iss = false;
  734. //标记学生作答状态
  735. int ts = 0;
  736. if (results.Count > 0)
  737. {
  738. foreach (ExamClassResult exam in results)
  739. {
  740. int index = exam.studentIds.IndexOf(x.id);
  741. if (index != -1)
  742. {
  743. if (exam.studentAnswers[index].Count > 0)
  744. {
  745. bool flag = exam.studentScores[index].Exists(x => x == -1);
  746. if (!flag)
  747. {
  748. ts = 1;
  749. iss = true;
  750. break;
  751. }
  752. else
  753. {
  754. ts = 0;
  755. }
  756. }
  757. else
  758. {
  759. ts = -1;
  760. }
  761. }
  762. }
  763. }
  764. else
  765. {
  766. ts = -1;
  767. }
  768. stuActivities.Add(new StuActivity
  769. {
  770. pk = "Activity",
  771. id = info.id,
  772. code = $"Activity-{x.code.Replace("Base-", "")}-{x.id}",
  773. type = "Exam",
  774. name = info.name,
  775. source = info.source,
  776. startTime = info.startTime,
  777. endTime = info.endTime,
  778. scode = info.code,
  779. scope = info.scope,
  780. school = info.school,
  781. creatorId = info.creatorId,
  782. subjects = sub,
  783. blob = null,
  784. owner = info.owner,
  785. classIds = classIds.ToList(),
  786. createTime = info.createTime,
  787. ext = new Dictionary<string, JsonElement>() { { "type", info.type.ToJsonString().ToObject<JsonElement>() },
  788. { "subjects", info.subjects.ToJsonString().ToObject<JsonElement>() } },
  789. taskStatus = ts,
  790. sStatus = iss ? 1 : 0,
  791. });
  792. });
  793. }
  794. await ActivityService.SaveStuActivity(client, _dingDing, stuActivities, tmdActivities, null);
  795. return classLists;
  796. }
  797. public static async Task<List<double>> knowledgeCount(ExamInfo info, ExamSubject subject, DingDing _dingDing, int no, List<ExamClassResult> classResults,
  798. double rhwCount, double rhw, double rhlCount, double rhl, AzureCosmosFactory _azureCosmos)
  799. {
  800. List<double> akp = new();
  801. try
  802. {
  803. int phcount = 0;
  804. int plcount = 0;
  805. //存放并去重知识点
  806. HashSet<string> kname = new HashSet<string>();
  807. if (info.papers[no].knowledge.Count > 0)
  808. {
  809. info.papers[no].knowledge.ForEach(kno =>
  810. {
  811. kno.ForEach(k =>
  812. {
  813. kname.Add(k);
  814. });
  815. });
  816. List<string> knowledgeName = new List<string>();
  817. foreach (string cla in kname)
  818. {
  819. knowledgeName.Add(cla);
  820. }
  821. for (int k = 0; k < knowledgeName.Count; k++)
  822. {
  823. if (null == knowledgeName[k])
  824. {
  825. knowledgeName.Remove(knowledgeName[k]);
  826. }
  827. }
  828. foreach (ExamClassResult classResult in classResults)
  829. {
  830. if (classResult.subjectId.Equals(subject.id))
  831. {
  832. //List<int> phc = new List<int>();
  833. List<int> ph = new List<int>();
  834. List<int> pl = new List<int>();
  835. List<int> pc = new List<int>();
  836. List<double> persent = new List<double>();
  837. for (int i = 0; i < knowledgeName.Count; i++)
  838. {
  839. //初始化单个知识点得分
  840. double score = 0;
  841. double allScore = 0;
  842. int n = 0;
  843. int phCount = 0;
  844. int plCount = 0;
  845. int pCount = 0;
  846. foreach (List<string> str in info.papers[no].knowledge)
  847. {
  848. if (str.Contains(knowledgeName[i]))
  849. {
  850. var itemPersent = str.Count > 0 ? 1 / Convert.ToDouble(str.Count) : 0;
  851. allScore += info.papers[no].point.Count > 0 ? info.papers[no].point[n] * itemPersent : 0;
  852. foreach (string id in classResult.studentIds)
  853. {
  854. int index = classResult.studentIds.IndexOf(id);
  855. if (classResult.studentScores.Count > 0)
  856. {
  857. if (classResult.studentScores[index].Count > 0)
  858. {
  859. score += classResult.studentScores[index][n] == -1 ? 0 : classResult.studentScores[index][n]* itemPersent;
  860. if (classResult.studentScores[index].Sum() >= rhw && phcount < rhwCount)
  861. {
  862. if (classResult.studentScores[index][n] <= 0)
  863. {
  864. phCount++;
  865. }
  866. phcount++;
  867. continue;
  868. }
  869. if (classResult.studentScores[index].Sum() <= rhl && plcount < (info.stuCount - rhlCount))
  870. {
  871. if (classResult.studentScores[index][n] <= 0)
  872. {
  873. plCount++;
  874. }
  875. plcount++;
  876. continue;
  877. }
  878. if (classResult.studentScores[index][n] <= 0)
  879. {
  880. pCount++;
  881. }
  882. }
  883. }
  884. }
  885. }
  886. n++;
  887. }
  888. pc.Add(pCount);
  889. ph.Add(phCount);
  890. pl.Add(plCount);
  891. double per = classResult.studentIds.Count > 0 ? Math.Round(score / classResult.studentIds.Count, 2) : 0;
  892. persent.Add(allScore > 0 ? per / allScore : 0);
  893. }
  894. classResult.phc = ph;
  895. classResult.plc = pl;
  896. classResult.pc = pc;
  897. classResult.krate = persent;
  898. }
  899. //await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(classResult, classResult.id, new Azure.Cosmos.PartitionKey($"{classResult.code}"));
  900. }
  901. foreach (string k in knowledgeName)
  902. {
  903. double score = 0;
  904. double allScore = 0;
  905. int n = 0;
  906. int count = 0;
  907. foreach (ExamClassResult result in classResults)
  908. {
  909. if (result.subjectId.Equals(subject.id))
  910. {
  911. foreach (List<string> str in info.papers[no].knowledge)
  912. {
  913. if (str.Contains(k))
  914. {
  915. var itemPersent = str.Count > 0 ? 1 / Convert.ToDouble(str.Count) : 0;
  916. allScore += info.papers[no].point.Count > 0 ? info.papers[no].point[n] * itemPersent : 0;
  917. if (result.studentScores.Count > 0)
  918. {
  919. score += result.studentScores.Sum(r => r.Sum()) * itemPersent;
  920. }
  921. }
  922. n++;
  923. }
  924. count += result.studentIds.Count;
  925. }
  926. }
  927. double per = count > 0 ? Math.Round(score / count, 2) : 0;
  928. akp.Add(allScore > 0 ? Math.Round(per / allScore, 2) : 0);
  929. }
  930. }
  931. return akp;
  932. }
  933. catch (Exception ex)
  934. {
  935. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-{info.id}-评测知识点结算异常{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  936. return null;
  937. }
  938. }
  939. public static async Task<List<double>> fieldCount(ExamInfo info, ExamSubject subject, DingDing _dingDing, int no, List<ExamClassResult> classResults,
  940. double rhwCount, double rhw, double rhlCount, double rhl, AzureCosmosFactory _azureCosmos)
  941. {
  942. List<double> afp = new();
  943. try
  944. {
  945. int phcount = 0;
  946. int plcount = 0;
  947. //存放并去重知识点
  948. List<int> knowledgeName = new List<int>() { 1, 2, 3, 4, 5, 6 };
  949. /* knowledgeName.Add(1);
  950. knowledgeName.Add(2);
  951. knowledgeName.Add(3);
  952. knowledgeName.Add(4);
  953. knowledgeName.Add(5);
  954. knowledgeName.Add(6);*/
  955. foreach (ExamClassResult classResult in classResults)
  956. {
  957. if (classResult.subjectId.Equals(subject.id))
  958. {
  959. //List<int> phc = new List<int>();
  960. List<int> ph = new List<int>();
  961. List<int> pl = new List<int>();
  962. List<int> pc = new List<int>();
  963. List<double> persent = new List<double>();
  964. for (int i = 0; i < knowledgeName.Count; i++)
  965. {
  966. //初始化单个知识点得分
  967. double score = 0;
  968. double allScore = 0;
  969. int n = 0;
  970. int phCount = 0;
  971. int plCount = 0;
  972. int pCount = 0;
  973. if (info.papers[no].field.Count > 0)
  974. {
  975. foreach (int str in info.papers[no].field)
  976. {
  977. if (str == knowledgeName[i])
  978. {
  979. var itemPersent = 1;
  980. allScore += info.papers[no].point.Count > 0 ? info.papers[no].point[n] * itemPersent : 0;
  981. foreach (string id in classResult.studentIds)
  982. {
  983. int index = classResult.studentIds.IndexOf(id);
  984. if (classResult.studentScores.Count > 0)
  985. {
  986. if (classResult.studentScores[index].Count > 0)
  987. {
  988. score += classResult.studentScores[index][n] == -1 ? 0 : classResult.studentScores[index][n];
  989. if (classResult.studentScores[index].Sum() >= rhw && phcount < rhwCount)
  990. {
  991. if (classResult.studentScores[index][n] <= 0)
  992. {
  993. phCount++;
  994. }
  995. phcount++;
  996. continue;
  997. }
  998. if (classResult.studentScores[index].Sum() <= rhl && plcount < (info.stuCount - rhlCount))
  999. {
  1000. if (classResult.studentScores[index][n] <= 0)
  1001. {
  1002. plCount++;
  1003. }
  1004. plcount++;
  1005. continue;
  1006. }
  1007. if (classResult.studentScores[index][n] <= 0)
  1008. {
  1009. pCount++;
  1010. }
  1011. }
  1012. }
  1013. }
  1014. }
  1015. n++;
  1016. }
  1017. pc.Add(pCount);
  1018. ph.Add(phCount);
  1019. pl.Add(plCount);
  1020. double per = classResult.studentIds.Count > 0 ? Math.Round(score / classResult.studentIds.Count, 2) : 0;
  1021. persent.Add(allScore > 0 ? per / allScore : 0);
  1022. }
  1023. }
  1024. classResult.fphc = ph;
  1025. classResult.fplc = pl;
  1026. classResult.fpc = pc;
  1027. classResult.frate = persent;
  1028. }
  1029. //await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(classResult, classResult.id, new Azure.Cosmos.PartitionKey($"{classResult.code}"));
  1030. }
  1031. for (int i = 0; i < knowledgeName.Count; i++)
  1032. {
  1033. double score = 0;
  1034. double allScore = 0;
  1035. int n = 0;
  1036. int count = 0;
  1037. foreach (ExamClassResult result in classResults)
  1038. {
  1039. if (result.subjectId.Equals(subject.id))
  1040. {
  1041. foreach (int str in info.papers[no].field)
  1042. {
  1043. if (str == knowledgeName[i])
  1044. {
  1045. var itemPersent = 1;
  1046. allScore += info.papers[no].point.Count > 0 ? info.papers[no].point[n] * itemPersent : 0;
  1047. if (result.studentScores.Count > 0)
  1048. {
  1049. score += result.studentScores.Sum(r => r.Sum()) * itemPersent;
  1050. }
  1051. }
  1052. n++;
  1053. }
  1054. count += result.studentIds.Count;
  1055. }
  1056. }
  1057. double per = count > 0 ? Math.Round(score / count, 2) : 0;
  1058. afp.Add(allScore > 0 ? Math.Round(per / allScore, 2) : 0);
  1059. }
  1060. return afp;
  1061. }
  1062. catch (Exception ex)
  1063. {
  1064. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-{info.id}-评测认知层次结算异常{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  1065. return null;
  1066. }
  1067. }
  1068. //处理选题计数
  1069. public static List<Dictionary<string, int>> getMore(ExamInfo info, int no, List<List<List<string>>> list)
  1070. {
  1071. List<Dictionary<string, int>> recorde = new List<Dictionary<string, int>>();
  1072. try
  1073. {
  1074. for (int i = 0; i < info.papers[no].answers.Count; i++)
  1075. {
  1076. if (info.papers[no].answers[i].Count <= 0)
  1077. {
  1078. recorde.Add(new Dictionary<string, int>());
  1079. continue;
  1080. }
  1081. Dictionary<string, int> optCount = new Dictionary<string, int>();
  1082. foreach (List<List<string>> stu in list)
  1083. {
  1084. if (stu.Count == info.papers[no].answers.Count)
  1085. {
  1086. var item = stu[i];
  1087. foreach (string opt in item)
  1088. {
  1089. if (optCount.ContainsKey(opt))
  1090. {
  1091. optCount[opt] = optCount[opt] + 1;
  1092. }
  1093. else
  1094. {
  1095. optCount[opt] = 1;
  1096. }
  1097. }
  1098. }
  1099. }
  1100. recorde.Add(optCount);
  1101. }
  1102. return recorde;
  1103. }
  1104. catch (Exception)
  1105. {
  1106. return recorde;
  1107. }
  1108. }
  1109. public static async Task createClassResultAsync(ExamInfo info, List<ExamClassResult> examClassResults, ExamSubject subject, int no, AzureCosmosFactory _azureCosmos, DingDing _dingDing, AzureStorageFactory _azureStorage)
  1110. {
  1111. //保证试卷信息与科目信息同步
  1112. ExamResult result = new ExamResult();
  1113. //人数总和
  1114. int Count = 0;
  1115. int m = 0;
  1116. double score = 0;
  1117. //标准差
  1118. double powSum = 0;
  1119. double allScore = info.papers[no].point.Sum();
  1120. List<ClassRange> classRanges = new List<ClassRange>();
  1121. List<string> lostStu = new List<string>();
  1122. List<double> csRate = new List<double>();
  1123. List<List<List<string>>> opt = new List<List<List<string>>>();
  1124. foreach (ExamClassResult classResult in examClassResults)
  1125. {
  1126. double classSrate = 0;
  1127. if (classResult.subjectId.Equals(subject.id))
  1128. {
  1129. foreach (List<List<string>> op in classResult.ans)
  1130. {
  1131. opt.Add(op);
  1132. }
  1133. //记录缺考学生索引位置
  1134. int index_stu = 0;
  1135. foreach (var ans in classResult.studentAnswers)
  1136. {
  1137. if (ans.Count == 0)
  1138. {
  1139. if (!lostStu.Contains(classResult.studentIds[index_stu]))
  1140. {
  1141. lostStu.Add(classResult.studentIds[index_stu]);
  1142. }
  1143. }
  1144. index_stu++;
  1145. }
  1146. int index = 0;
  1147. foreach (List<double> scores in classResult.studentScores)
  1148. {
  1149. List<double> newScores = new List<double>();
  1150. //int count = 0;
  1151. foreach (double sc in scores)
  1152. {
  1153. newScores.Add(sc > -1 ? sc : 0);
  1154. /*if (sc == -1)
  1155. {
  1156. count++;
  1157. }*/
  1158. }
  1159. /*if (count == scores.Count)
  1160. {
  1161. if (!lostStu.Contains(classResult.studentIds[index]))
  1162. {
  1163. lostStu.Add(classResult.studentIds[index]);
  1164. }
  1165. //mcount++;
  1166. }*/
  1167. classSrate += newScores.Sum();
  1168. score += newScores.Sum();
  1169. result.studentScores.Add(newScores);
  1170. index++;
  1171. }
  1172. //处理班级信息
  1173. ClassRange range = new ClassRange();
  1174. range.id = classResult.info.id;
  1175. range.name = classResult.info.name;
  1176. range.gradeId = classResult.gradeId;
  1177. List<int> ran = new List<int>();
  1178. int stuCount = classResult.studentIds.Count;
  1179. Count += stuCount;
  1180. if (m == 0)
  1181. {
  1182. ran.Add(0);
  1183. ran.Add(stuCount - 1);
  1184. }
  1185. else
  1186. {
  1187. ran.Add(Count - stuCount);
  1188. ran.Add(Count - 1);
  1189. }
  1190. m++;
  1191. range.range = ran;
  1192. classRanges.Add(range);
  1193. //处理学生ID
  1194. foreach (string id in classResult.studentIds)
  1195. {
  1196. result.studentIds.Add(id);
  1197. }
  1198. if (allScore > 0)
  1199. {
  1200. csRate.Add(classResult.studentIds.Count > 0 ? Math.Round(classSrate * 1.0 / classResult.studentIds.Count, 2) : 0 / allScore);
  1201. }
  1202. else
  1203. {
  1204. csRate.Add(0);
  1205. }
  1206. //powSum += Math.Pow(classSrate - result.average, 2);
  1207. //处理选项计数内容
  1208. }
  1209. }
  1210. /*foreach (string id in result.lostStus) {
  1211. if (!examClassResults[0].studentIds.Contains(id)) {
  1212. int index = result.lostStus.IndexOf(id);
  1213. result.lostStus.RemoveAt(index);
  1214. }
  1215. }*/
  1216. //处理人员变更时被移除的人员
  1217. if (result.lostStus.Count > 0)
  1218. {
  1219. for (int i = 0; i < result.lostStus.Count; i++)
  1220. {
  1221. if (!examClassResults[0].studentIds.Contains(result.lostStus[i]))
  1222. {
  1223. result.lostStus.RemoveAt(i);
  1224. }
  1225. }
  1226. }
  1227. await examRecordCount(info, subject, _dingDing, no, result, examClassResults, _azureCosmos);
  1228. result.record = getMore(info, no, opt);
  1229. result.average = result.studentIds.Count > 0 ? Math.Round(score * 1.0 / result.studentIds.Count, 2) : 0;
  1230. double stand = 0;
  1231. int sco = 0;
  1232. foreach (ExamClassResult classResult in examClassResults)
  1233. {
  1234. //double classSrate = 0;
  1235. if (classResult.subjectId.Equals(subject.id))
  1236. {
  1237. stand += classResult.standard;
  1238. sco++;
  1239. }
  1240. }
  1241. result.standard = sco > 0 ? Math.Round(stand / sco, 2) : 0;
  1242. result.csRate = csRate;
  1243. result.lostStus = lostStu;
  1244. result.sRate = allScore > 0 ? Math.Round(result.average / allScore * 100, 2) : 0;
  1245. result.classes = classRanges;
  1246. result.code = "ExamResult-" + info.id;
  1247. result.school = info.school;
  1248. result.id = subject.id;
  1249. result.examId = info.id;
  1250. result.subjectId = subject.id;
  1251. result.year = info.year;
  1252. result.paper = info.papers[no];
  1253. //result.point = info.papers[j].point;
  1254. result.scope = info.scope;
  1255. result.name = info.name;
  1256. result.time = info.startTime;
  1257. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Common").UpsertItemAsync(result, new Azure.Cosmos.PartitionKey($"ExamResult-{info.id}"));
  1258. }
  1259. public class Settlement
  1260. {
  1261. public double rate { get; set; }
  1262. public double score { get; set; }
  1263. public List<string> stus { get; set; } = new List<string>();
  1264. public int total { get; set; }
  1265. }
  1266. }
  1267. }