TriggerExam.cs 77 KB

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