TriggerExam.cs 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
  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. try
  100. {
  101. await _serviceBus.GetServiceBusClient().cancelMessage(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().cancelMessage(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 n = 0;
  920. int count = 0;
  921. foreach (ExamClassResult result in classResults)
  922. {
  923. if (result.subjectId.Equals(subject.id))
  924. {
  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 n = 0;
  1054. int count = 0;
  1055. foreach (ExamClassResult result in classResults)
  1056. {
  1057. if (result.subjectId.Equals(subject.id))
  1058. {
  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. }