TriggerExam.cs 75 KB

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