ArtAnalysisController.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. using Microsoft.Azure.Cosmos;
  2. using ClouDASLibx;
  3. using Google.Protobuf.WellKnownTypes;
  4. using Microsoft.AspNetCore.Authorization;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.Extensions.Options;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Text.Json;
  12. using System.Threading.Tasks;
  13. using TEAMModelOS.Filter;
  14. using TEAMModelOS.SDK;
  15. using TEAMModelOS.SDK.DI;
  16. using TEAMModelOS.SDK.Extension;
  17. using TEAMModelOS.SDK.Helper.Common.StringHelper;
  18. using TEAMModelOS.SDK.Models;
  19. using TEAMModelOS.SDK.Models.Cosmos.Student;
  20. namespace TEAMModelOS.Controllers.Analysis
  21. {
  22. [Route("analysis/art")]
  23. [ApiController]
  24. public class ArtAnalysisController : ControllerBase
  25. {
  26. private readonly AzureCosmosFactory _azureCosmos;
  27. private readonly DingDing _dingDing;
  28. private readonly Option _option;
  29. private readonly CoreAPIHttpService _coreAPIHttpService;
  30. private readonly AzureStorageFactory _azureStorage;
  31. public ArtAnalysisController(AzureCosmosFactory azureCosmos, DingDing dingDing, IOptionsSnapshot<Option> option, CoreAPIHttpService coreAPIHttpService, AzureStorageFactory azureStorage)
  32. {
  33. _azureCosmos = azureCosmos;
  34. _dingDing = dingDing;
  35. _option = option?.Value;
  36. _coreAPIHttpService = coreAPIHttpService;
  37. _azureStorage = azureStorage;
  38. }
  39. /* [ProducesDefaultResponseType]
  40. [HttpPost("find")]
  41. //[Authorize(Roles = "IES")]
  42. //[AuthToken(Roles = "teacher,admin")]
  43. public async Task<IActionResult> find(JsonElement request)
  44. {
  45. if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  46. var client = _azureCosmos.GetCosmosClient();
  47. List<ArtEvaluation> arts = new();
  48. try {
  49. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIteratorSql(queryText: $"SELECT top 1 * FROM c where c.period.id ='{period}' order by c.createTime ",
  50. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Art-{code}") }))
  51. {
  52. using var json = await JsonDocument.ParseAsync(item.Content);
  53. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  54. {
  55. arts.Add(obj.ToObject<ArtEvaluation>());
  56. }
  57. }
  58. } catch (Exception ex) {
  59. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-analysis/art,find()\n{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  60. }
  61. }*/
  62. [ProducesDefaultResponseType]
  63. [HttpPost("statistics")]
  64. [Authorize(Roles = "IES")]
  65. [AuthToken(Roles = "teacher,admin")]
  66. public async Task<IActionResult> getAnalysis(JsonElement request)
  67. {
  68. var client = _azureCosmos.GetCosmosClient();
  69. if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  70. if (!request.TryGetProperty("periodId", out JsonElement periodId)) return BadRequest();
  71. if (!request.TryGetProperty("subjectId", out JsonElement subjectId)) return BadRequest();
  72. if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
  73. if (!request.TryGetProperty("classIds", out JsonElement cIds)) return BadRequest();
  74. if (!request.TryGetProperty("examId", out JsonElement examId)) return BadRequest();
  75. try
  76. {
  77. /*string url = string.Empty;
  78. if (!_azureStorage.GetBlobContainerClient($"{code}").GetBlobClient($"/art/{id}/{subjectId}.json").Exists())
  79. {*/
  80. List<ArtSubjectScore> As = new();
  81. List<(List<ArtSubjectScore> scs, string sIds, string cd)> stus = new();
  82. //List<ArtEvaluation> arts = new();
  83. string artId = id.GetString();
  84. List<string> classIds = cIds.ToObject<List<string>>();
  85. if (!request.TryGetProperty("areaId", out JsonElement _areaId)) return BadRequest();
  86. ArtSetting setting = await client.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemAsync<ArtSetting>($"{_areaId}", partitionKey: new PartitionKey("ArtSetting"));
  87. //学校基本信息
  88. School scInfo = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>($"{code}", partitionKey: new PartitionKey("Base"));
  89. var perMore = scInfo.period.Where(c => c.id.Equals(periodId.GetString())).FirstOrDefault()?.grades;
  90. string queryScore = $" select c.studentId,c.classIds,c.totalScore,c.subjectScores,c.results from c ";
  91. if (classIds.Count == 1)
  92. {
  93. queryScore = $" select c.studentId,c.classIds,c.totalScore,c.subjectScores,c.results from c where array_contains(c.classIds,'{classIds[0]}') ";
  94. }
  95. //List<(string stu,List<string> cId)> clads = new();
  96. List<List<ArtQuotaResult>> results = new();
  97. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, Constant.Student).GetItemQueryStreamIteratorSql
  98. (queryText: queryScore, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"ArtResult-{artId}") }))
  99. {
  100. using var json = await JsonDocument.ParseAsync(item.Content);
  101. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  102. {
  103. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  104. {
  105. if (obj.TryGetProperty("subjectScores", out JsonElement subScore))
  106. {
  107. string sId = obj.GetProperty("studentId").GetString();
  108. List<ArtSubjectScore> sc = subScore.ToObject<List<ArtSubjectScore>>();
  109. string cd = obj.GetProperty("classIds").ToObject<List<string>>()[0];
  110. //clads.Add((sId,obj.GetProperty("classIds").ToObject<List<string>>()));
  111. As.AddRange(sc);
  112. stus.Add((sc, sId, cd));
  113. }
  114. else
  115. {
  116. As.AddRange(new List<ArtSubjectScore>());
  117. }
  118. results.Add(obj.GetProperty("results").ToObject<List<ArtQuotaResult>>());
  119. }
  120. }
  121. }
  122. var subjectScore = new
  123. {
  124. name = As.Where(a => a.subjectId.Equals(subjectId.GetString())).Select(x => x.score).Where(c => c > 0)
  125. };
  126. double max = subjectScore.name.Max(s => Math.Abs(s));
  127. double min = subjectScore.name.Min(s => Math.Abs(s));
  128. double total = subjectScore.name.Sum();
  129. double average = Math.Round(total / stus.Count, 2);
  130. double excellent = Math.Round(subjectScore.name.Where(s => s >= 80).Count() * 1.0 / stus.Count, 4);
  131. double pass = Math.Round(subjectScore.name.Where(s => s >= 60).Count() * 1.0 / stus.Count, 4);
  132. double powSum = 0;
  133. foreach (var sc in subjectScore.name)
  134. {
  135. powSum += Math.Pow(sc - average, 2);
  136. }
  137. var pow = Math.Round(stus.Count > 0 ? Math.Pow(powSum / stus.Count, 0.5) : 0, 2);
  138. //获取本次评测所有科目结算结果
  139. List<ExamResult> examResults = new();
  140. ExamInfo info = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<ExamInfo>(examId.ToString(), new PartitionKey($"Exam-{code}"));
  141. (List<RMember> tchList, List<RGroupList> classLists) = await GroupListService.GetMemberByListids(_coreAPIHttpService, client, _dingDing, classIds, code.GetString(), null,-1,info.startTime);
  142. //获取评测ID
  143. //var examId = arts[0].settings.SelectMany(s => s.task).Where(a => a.type == 1 && a.subject.Equals(subjectId.GetString())).FirstOrDefault().acId;
  144. //根据科目标识获取科目ID以及知识块和知识点关系TODO 引用不同试卷时 获取知识点得差异
  145. int index = 0;
  146. foreach (var pr in info.subjects)
  147. {
  148. if (pr.id.Equals(subjectId.GetString()))
  149. {
  150. break;
  151. }
  152. index++;
  153. }
  154. List<(string name, List<string> kno)> knos = new();
  155. if (string.IsNullOrEmpty(info.papers[index].periodId))
  156. {
  157. (string subId, List<(string name, List<string> kno)> values) = await getKnowledge("hbcn", client, subjectId.GetString(), "be32942d-97a9-52ba-45d6-2e5b722583f5");
  158. knos = values;
  159. }
  160. else
  161. {
  162. (string subId, List<(string name, List<string> kno)> values) = await getKnowledge(info.papers[index].code, client, subjectId.GetString(), info.papers[index].periodId);
  163. knos = values;
  164. }
  165. var query = $"select c.id,c.name,c.subjectId,c.studentScores,c.studentIds,c.paper,c.classes,c.sRate,c.average,c.standard,c.lostStus,c.record,c.phc,c.plc from c where c.examId = '{examId}' and c.subjectId = '{subjectId}' ";
  166. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIteratorSql<ExamResult>(queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"ExamResult-{examId}") }))
  167. {
  168. examResults.Add(item);
  169. }
  170. List<KeyValuePair<string, List<(string name, double score, double aver)>>> pointPersent = new();
  171. List<KeyValuePair<string, List<(string name, double score, double point, string sId)>>> stuPersent = new();
  172. (KeyValuePair<string, List<string>> key1, KeyValuePair<string, List<string>> key2, KeyValuePair<string, List<(string name, double score, double average)>> key3,
  173. KeyValuePair<string, List<(string name, double score)>> key4, KeyValuePair<string, List<(string name, double score, double point, string cId)>> key5,
  174. KeyValuePair<string, List<(string name, double score, double point, string sId)>> key6) = DoKnowledgePoint(examResults[0], info);
  175. //KeyValuePair<string, List<(string id, double sta, double pass, string stu)>> key = DoSubjectScatter(examResults[0]);
  176. pointPersent.Add(key3);
  177. stuPersent.Add(key6);
  178. List<(string name, double score, double av)> blockScore = new();
  179. List<(string name, double score, double av, string sId)> stuBlockScore = new();
  180. List<(string name, double score, double av, string cId)> claBlockScore = new();
  181. foreach (var block in knos)
  182. {
  183. double sc = 0;
  184. double av = 0;
  185. foreach (var no in pointPersent)
  186. {
  187. foreach (var (name, score, aver) in no.Value)
  188. {
  189. if (null != block.kno && block.kno.Contains(name))
  190. {
  191. sc += score;
  192. av += aver;
  193. }
  194. }
  195. }
  196. foreach (var (name, score, point, sId) in key6.Value)
  197. {
  198. double realScore = 0;
  199. double realPoint = 0;
  200. if (null != block.kno && block.kno.Contains(name))
  201. {
  202. realScore = score;
  203. realPoint = point;
  204. }
  205. stuBlockScore.Add((block.name, realScore, realPoint, sId));
  206. }
  207. foreach (var (name, score, point, cId) in key5.Value)
  208. {
  209. double realScore = 0;
  210. double realPoint = 0;
  211. if (null != block.kno && block.kno.Contains(name))
  212. {
  213. realScore = score;
  214. realPoint = point;
  215. }
  216. claBlockScore.Add((block.name, realScore, realPoint, cId));
  217. }
  218. blockScore.Add((block.name, sc, av));
  219. }
  220. var stuBlock = stuBlockScore.GroupBy(x => x.sId).Select(z => new
  221. {
  222. z.Key,
  223. block = z.ToList().GroupBy(c => c.name).Select(k => new
  224. {
  225. k.Key,
  226. score = k.ToList().Sum(p => p.score),
  227. point = k.ToList().Sum(p => p.av)
  228. })
  229. });
  230. var claBlock = claBlockScore.GroupBy(x => x.cId).Select(z => new
  231. {
  232. z.Key,
  233. block = z.ToList().GroupBy(c => c.name).Select(k => new
  234. {
  235. k.Key,
  236. score = k.ToList().Sum(p => p.score),
  237. point = k.ToList().Sum(p => p.av)
  238. })
  239. });
  240. var blk = blockScore.Select(x => new
  241. {
  242. x.name,
  243. x.score,
  244. persent = Math.Round(x.score > 0 ? x.av / x.score : 0, 4),
  245. dimension = setting.dimensions.Where(s => s.blocks.Contains(x.name)).Select(x => x.dimension)
  246. });
  247. //获取维度得分率
  248. List<(string stuId, double score, double point, string dim)> stuDims = new();
  249. List<(string claId, double score, double point, string dim)> claDims = new();
  250. foreach (var dimm in setting.dimensions)
  251. {
  252. double dimScore = 0;
  253. double dimPoint = 0;
  254. if (dimm.subjectBind.Equals(subjectId.GetString()))
  255. {
  256. foreach (var stuDim in stuBlock)
  257. {
  258. foreach (var dd in stuDim.block)
  259. {
  260. if (dimm.blocks.Contains(dd.Key))
  261. {
  262. dimScore += dd.score;
  263. dimPoint += dd.point;
  264. }
  265. }
  266. stuDims.Add((stuDim.Key, dimScore, dimPoint, dimm.dimension));
  267. }
  268. foreach (var claDim in claBlock)
  269. {
  270. foreach (var dd in claDim.block)
  271. {
  272. if (dimm.blocks.Contains(dd.Key))
  273. {
  274. dimScore += dd.score;
  275. dimPoint += dd.point;
  276. }
  277. }
  278. claDims.Add((claDim.Key, dimScore, dimPoint, dimm.dimension));
  279. }
  280. }
  281. }
  282. var dim = setting.dimensions.Where(q => q.subjectBind.Equals(subjectId.GetString())).Select(x => new
  283. {
  284. x.dimension,
  285. persent = Math.Round(x.blocks.Select(c => new
  286. {
  287. persent =
  288. blockScore.Where(z => z.name.Equals(c)).Sum(v => v.score) > 0 ?
  289. blockScore.Where(z => z.name.Equals(c)).Sum(v => v.av) / blockScore.Where(z => z.name.Equals(c)).Sum(v => v.score) : 0
  290. }).Sum(o => o.persent) / x.blocks.Count, 4)
  291. });
  292. var kno = key4.Value.Select(x => new
  293. {
  294. x.name,
  295. x.score,
  296. block = knos.Where(v => null != v.kno && v.kno.Contains(x.name)).Select(x => x.name)
  297. });
  298. //学生信息
  299. var students = stus.Select(s => new
  300. {
  301. id = s.sIds,
  302. s.scs.FirstOrDefault(x => !string.IsNullOrWhiteSpace(x.subjectId) && x.subjectId.Equals(subjectId.GetString()))?.score,
  303. tchList.Where(t => t.id.Equals(s.sIds)).FirstOrDefault()?.name,
  304. classId = s.cd,
  305. className = examResults[0].classes.Where(c => c.id.Equals(s.cd)).FirstOrDefault()?.name,
  306. examResults[0].classes.Where(c => c.id.Equals(s.cd)).FirstOrDefault()?.gradeId,
  307. //key.Value.Where(c => c.id.Equals(s.sIds))?.FirstOrDefault().sta,
  308. //key.Value.Where(c => c.id.Equals(s.sIds))?.FirstOrDefault().pass,
  309. // key.Value.Where(c => c.id.Equals(s.sIds))?.FirstOrDefault().stu,
  310. kno = key6.Value.Where(c => c.sId.Equals(s.sIds))?.Select(z => new
  311. {
  312. z.name,
  313. persent = z.point > 0 ? Math.Round(z.score / z.point, 4) : 0,
  314. block = knos.Where(v => null != v.kno && v.kno.Contains(z.name))?.Select(x => x.name)
  315. }),
  316. block = stuBlock.Where(c => c.Key.Equals(s.sIds)).FirstOrDefault()?.block.Select(x => new
  317. {
  318. name = x.Key,
  319. persent = x.point > 0 ? Math.Round(x.score / x.point, 4) : 0
  320. }),
  321. dim = stuDims.Where(c => c.stuId.Equals(s.sIds))?.Select(z => new
  322. {
  323. name = z.dim,
  324. persent = z.point > 0 ? Math.Round(z.score / z.point, 4) : 0
  325. })
  326. });
  327. List<(string cId, double sc, double max, double min, double excellent, double pass, double count)> clsInfo = new();
  328. var cInfo = students.GroupBy(c => (c.classId, c.className)).Select(x => new
  329. {
  330. id = x.Key.classId,
  331. name = x.Key.className,
  332. max = x.ToList().Where(p => p.score > 0).Select(z => z.score).ToList().Count > 0 ? x.ToList().Where(p => p.score > 0).Select(z => z.score).ToList().Max(s => Math.Abs((double)s)) : 0,
  333. min = x.ToList().Where(p => p.score > 0).Select(z => z.score).ToList().Count > 0 ? x.ToList().Where(p => p.score > 0).Select(z => z.score).ToList().Min(s => Math.Abs((double)s)) : 0,
  334. excellent = x.ToList().Where(p => p.score > 0).Select(z => z.score).ToList().Count > 0 ? Math.Round(x.ToList().Where(p => p.score > 0).Select(z => z.score).ToList().Where(s => s >= 80).Count() * 1.0 / x.ToList().Count, 4) : 0,
  335. pass = x.ToList().Where(p => p.score > 0).Select(z => z.score).ToList().Count > 0 ? Math.Round(x.ToList().Where(p => p.score > 0).Select(z => z.score).ToList().Where(s => s >= 60).Count() * 1.0 / x.ToList().Count, 4) : 0,
  336. score = x.ToList().Count > 0 ? Math.Round((double)x.ToList().Sum(z => z.score) * 1.0 / x.ToList().Count, 2) : 0,
  337. kno = key5.Value.Where(c => c.cId.Equals(x.Key.classId)).Select(z => new
  338. {
  339. z.name,
  340. persent = Math.Round(z.point > 0 ? z.score / x.ToList().Count / z.point : 0, 4),
  341. block = knos.Where(v => null != v.kno && v.kno.Contains(z.name)).Select(x => x.name)
  342. }),
  343. block = claBlock.Count() > 0 ? claBlock.Where(c => c.Key.Equals(x.Key.classId))?.FirstOrDefault().block.Select(z => new
  344. {
  345. name = z.Key,
  346. persent = Math.Round(z.point > 0 ? z.score / x.ToList().Count / z.point : 0, 4)
  347. }) : null,
  348. dim = claDims.Where(c => c.claId.Equals(x.Key.classId)).Select(z => new
  349. {
  350. name = z.dim,
  351. persent = Math.Round(z.point > 0 ? z.score / x.ToList().Count / z.point : 0, 4)
  352. }),
  353. examResults[0].classes.Where(c => c.id.Equals(x.Key.classId))?.FirstOrDefault().gradeId
  354. });
  355. //年级信息
  356. var grades = students.GroupBy(c => c.gradeId).Select(x => new { gradeId = x.Key, list = x.ToList().Select(v => v.score).Where(c => c > 0) });
  357. var gscore = grades.Select(x => new
  358. {
  359. id = x.gradeId,
  360. name = perMore[int.Parse(x.gradeId)],
  361. score = x.list.Any() ? Math.Round((double)(x.list.Sum() / x.list.Count()), 2) : 0,
  362. max = x.list.Any() ? x.list.Max(s => Math.Abs((double)s)) : 0,
  363. min = x.list.Any() ? x.list.Min(s => Math.Abs((double)s)) : 0,
  364. excellent = x.list.Any() ? Math.Round(x.list.Where(s => s >= 80).Count() * 1.0 / x.list.Count(), 4) : 0,
  365. pass = x.list.Any() ? Math.Round(x.list.Where(s => s >= 60).Count() * 1.0 / x.list.Count(), 4) : 0
  366. });
  367. //获奖次数
  368. List<ArtAttachment> artAttachments = new();
  369. string sqlTask = $"select value(c) from c where c.artId = '{id}' and c.subjectId = '{subjectId}'";
  370. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Student).
  371. GetItemQueryIteratorSql<ArtAttachment>(queryText: sqlTask, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"ArtAttachment-{code}") }))
  372. {
  373. artAttachments.Add(item);
  374. }
  375. List<int> level = artAttachments.Where(z => z.level > -1).Select(c => c.level).ToList();
  376. //List<Dictionary<string, int>> recorde = new List<Dictionary<string, int>>();
  377. Dictionary<int, int> optCount = new Dictionary<int, int>();
  378. foreach (var s in level)
  379. {
  380. if (optCount.ContainsKey(s))
  381. {
  382. optCount[s] = optCount[s] + 1;
  383. }
  384. else
  385. {
  386. optCount[s] = 1;
  387. }
  388. }
  389. //var Analysis_school = new { count = tchList.Count, scount = stus.Count - info.lostStu.Count, max, min, average, excellent, pass, pow, students, cInfo, blk, kno, dim, optCount, gscore };
  390. //url = await _azureStorage.GetBlobContainerClient($"{code}").UploadFileByContainer(Analysis_school.ToJsonString(), $"art/{id}", $"{subjectId}.json");
  391. /* }
  392. else {
  393. url = _azureStorage.GetBlobContainerClient($"{code}").GetBlobClient($"/art/{id}/{subjectId}.json").Uri.ToString();
  394. }*/
  395. var realCount = stus.Count - info.lostStu.Count;
  396. if (classIds.Count == 1)
  397. {
  398. return Ok(new { count = tchList.Count, scount = realCount, max, min, average, excellent, pass, pow, blk, kno, dim, optCount, students, cInfo, gscore });
  399. }
  400. else if (classIds.Count == 0)
  401. {
  402. return Ok(new { count = tchList.Count, scount = realCount, max, min, average, excellent, pass, pow, blk, kno, dim, optCount });
  403. }
  404. else {
  405. return Ok(new { count = tchList.Count, scount = realCount, max, min, average, excellent, pass, pow, blk, kno, dim, optCount,cInfo });
  406. }
  407. //return Ok(new { count = tchList.Count, scount = realCount, max, min, average, excellent, pass, pow, blk, kno, dim, optCount, gscore });
  408. }
  409. catch (Exception e)
  410. {
  411. return Ok(new { code = 500 });
  412. }
  413. }
  414. private static async Task<(string subId, List<(string name, List<string> kno)>)> getKnowledge(string school, CosmosClient client, string subjectBid, string pId)
  415. {
  416. try
  417. {
  418. var response = await client.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(school, new PartitionKey($"Base"));
  419. string subjectId = string.Empty;
  420. List<Knowledge> knowledges = new();
  421. List<(string name, List<string> kno)> blocks = new();
  422. if (response.StatusCode==System.Net.HttpStatusCode.OK)
  423. {
  424. using var json = await JsonDocument.ParseAsync(response.Content);
  425. School sc = json.ToObject<School>();
  426. var subjects = sc.period.Where(p => p.id.Equals(pId)).Select(x => x.subjects);
  427. foreach (var sj in subjects)
  428. {
  429. foreach (var s in sj)
  430. {
  431. if (!string.IsNullOrWhiteSpace(s.bindId) && s.bindId.Equals(subjectBid))
  432. {
  433. subjectId = s.id;
  434. }
  435. }
  436. }
  437. string code = $"Knowledge-{school}-{subjectId}";
  438. StringBuilder sql = new StringBuilder($"select value(c) from c");
  439. if (string.IsNullOrWhiteSpace(pId))
  440. {
  441. sql.Append($" where c.periodId = '{pId}'");
  442. }
  443. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql<Knowledge>(queryText: sql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") }))
  444. {
  445. knowledges.Add(item);
  446. }
  447. }
  448. foreach (var know in knowledges)
  449. {
  450. foreach (var block in know.blocks)
  451. {
  452. blocks.Add((block.name, block.points));
  453. }
  454. }
  455. return (subjectId, blocks);
  456. }
  457. catch (Exception e)
  458. {
  459. return (null, null);
  460. }
  461. }
  462. //获取知识点得分率
  463. private static (KeyValuePair<string, List<string>>, KeyValuePair<string, List<string>>, KeyValuePair<string, List<(string name, double score, double average)>>,
  464. KeyValuePair<string, List<(string name, double score)>>, KeyValuePair<string, List<(string name, double score, double point, string cId)>>, KeyValuePair<string, List<(string name, double score, double point, string sId)>>) DoKnowledgePoint(ExamResult exam, ExamInfo info)
  465. {
  466. HashSet<string> knowledge = new HashSet<string>();
  467. List<double> point = new List<double>();
  468. List<List<double>> result = new List<List<double>>();
  469. List<ClassRange> classes = new List<ClassRange>();
  470. //求单个知识点所占分数
  471. List<string> per = new List<string>();
  472. //定位试卷信息
  473. int index = 0;
  474. foreach (ExamSubject subject in info.subjects)
  475. {
  476. if (subject.id.Equals(exam.subjectId))
  477. {
  478. break;
  479. }
  480. else
  481. {
  482. index++;
  483. }
  484. }
  485. if (info.papers[index].knowledge != null && info.papers[index].knowledge.Count > 0)
  486. {
  487. info.papers[index].knowledge.ForEach(k =>
  488. {
  489. k.ForEach(e =>
  490. {
  491. knowledge.Add(e);
  492. });
  493. });
  494. }
  495. else
  496. {
  497. return (default, default, default, default, default, default);
  498. }
  499. point = info.papers[index].point;
  500. result = exam.studentScores;
  501. classes = exam.classes;
  502. List<string> knowledgeName = new List<string>();
  503. foreach (string cla in knowledge)
  504. {
  505. knowledgeName.Add(cla);
  506. }
  507. for (int k = 0; k < knowledgeName.Count; k++)
  508. {
  509. if (null == knowledgeName[k])
  510. {
  511. knowledgeName.Remove(knowledgeName[k]);
  512. }
  513. }
  514. //初始化年级总分
  515. double total = 0;
  516. //处理年级单个知识点得分率
  517. foreach (List<double> grade in result)
  518. {
  519. total += grade.Sum();
  520. }
  521. //试卷总分
  522. double TotalPoint = point.Sum();
  523. List<double> knowScore = new List<double>();
  524. //学生得分情况
  525. List<double> Score = new List<double>();
  526. List<(string name, double score, double average)> pointScore = new();
  527. List<(string name, double score, double point, string cId)> classInfo = new();
  528. List<(string name, double score, double point, string sId)> stuInfo = new();
  529. List<(string name, double score)> pointTScore = new();
  530. for (int k = 0; k < knowledgeName.Count; k++)
  531. {
  532. double OnePoint = 0;
  533. List<string> itemNo = new List<string>();
  534. int n = 0;
  535. double scores = 0;
  536. info.papers[index].knowledge.ForEach(kno =>
  537. {
  538. if (kno.Contains(knowledgeName[k]))
  539. {
  540. var itemPersent = kno.Count > 0 ? 1 / Convert.ToDouble(kno.Count) : 0;
  541. OnePoint += point[n] * itemPersent;
  542. foreach (string id in exam.studentIds) {
  543. scores += exam.studentScores[index][n] * itemPersent;
  544. }
  545. }
  546. n++;
  547. });
  548. foreach (string id in exam.studentIds)
  549. {
  550. double stuScore = 0;
  551. int stuIndex = exam.studentIds.IndexOf(id);
  552. int a = 0;
  553. info.papers[index].knowledge.ForEach(kno =>
  554. {
  555. if (kno.Contains(knowledgeName[k]))
  556. {
  557. var itemPersent = kno.Count > 0 ? 1 / Convert.ToDouble(kno.Count) : 0;
  558. stuScore += exam.studentScores[stuIndex][a] * itemPersent;
  559. }
  560. a++;
  561. });
  562. stuInfo.Add((knowledgeName[k], stuScore, OnePoint, id));
  563. }
  564. foreach (var cla in exam.classes)
  565. {
  566. double classScores = 0;
  567. int b = 0;
  568. info.papers[index].knowledge.ForEach(kno =>
  569. {
  570. if (kno.Contains(knowledgeName[k]))
  571. {
  572. var itemPersent = kno.Count > 0 ? 1 / Convert.ToDouble(kno.Count) : 0;
  573. for (int i = cla.range[0]; i <= cla.range[1]; i++)
  574. {
  575. classScores += exam.studentScores[i][b] * itemPersent;
  576. }
  577. }
  578. b++;
  579. });
  580. classInfo.Add((knowledgeName[k], classScores, OnePoint, cla.id));
  581. }
  582. Score.Add(scores);
  583. //该知识点平均得分
  584. double sc = exam.studentIds.Count > 0 ? Math.Round(scores * 1.0 / exam.studentIds.Count, 2) : 0;
  585. //知识点占比 此处为了让结果更好看 乘以了系数1.5
  586. double average = sc * 1.5;
  587. if (average > OnePoint)
  588. {
  589. average = sc;
  590. }
  591. double persent = Math.Round(OnePoint > 0 ? average / OnePoint : 0, 2);
  592. per.Add(persent.ToString("0.00"));
  593. //单个知识点所有学生得分率
  594. pointTScore.Add((knowledgeName[k], persent));
  595. //单个知识点的配分
  596. pointScore.Add((knowledgeName[k], OnePoint, average));
  597. }
  598. KeyValuePair<string, List<string>> key1 = new(exam.subjectId, knowledgeName);
  599. KeyValuePair<string, List<string>> key2 = new(exam.subjectId, per);
  600. KeyValuePair<string, List<(string name, double score, double average)>> key3 = new(exam.subjectId, pointScore);
  601. KeyValuePair<string, List<(string name, double score)>> key4 = new(exam.subjectId, pointTScore);
  602. KeyValuePair<string, List<(string name, double score, double point, string cId)>> key5 = new(exam.subjectId, classInfo);
  603. KeyValuePair<string, List<(string name, double score, double point, string sId)>> key6 = new(exam.subjectId, stuInfo);
  604. //KeyValuePair<string, List<double>> key3 = new KeyValuePair<string, List<double>>(exam.subjectId, allPer);
  605. return (key1, key2, key3, key4, key5, key6);
  606. }
  607. private KeyValuePair<string, List<(string id, double sta, double pass, string stu)>> DoSubjectScatter(ExamResult e)
  608. {
  609. double[] point = StringHelper.ListTodouble(e.paper.point);
  610. double[,] result = StringHelper.ListToDouble(e.studentScores);
  611. try
  612. {
  613. var cdm = new ClouDASMatrix(result, point);
  614. //学生通过率
  615. List<double> pass = cdm.ScoringRate;
  616. //学生稳定度
  617. List<double> sta = cdm.StabilityRate;
  618. //落点区域
  619. List<string> stu = cdm.StuFallArea;
  620. int i = 0;
  621. List<(string id, double sta, double pass, string stu)> stus = new();
  622. e.studentIds.ForEach(s =>
  623. {
  624. var stuSta = sta[i] > 1 ? 1 : sta[i];
  625. stus.Add((s, stuSta, pass[i] * 0.01, stu[i]));
  626. i++;
  627. });
  628. return new KeyValuePair<string, List<(string id, double sta, double pass, string stu)>>(e.subjectId, stus);
  629. }
  630. catch (Exception ex)
  631. {
  632. BadRequest(ex.Message + ex.StackTrace);
  633. }
  634. return default;
  635. }
  636. }
  637. }