AchievementService.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using TEAMModelOS.Service.Model.Exam.Models;
  5. using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
  6. using TEAMModelOS.SDK.Module.AzureCosmosDB.Interfaces;
  7. using TEAMModelOS.Service.Analysis.Interfaces;
  8. using TEAMModelOS.Service.Models.Core;
  9. using TEAMModelOS.SDK.Module.AzureCosmosDBV3;
  10. using TEAMModelOS.Service.Models.Exam.Models;
  11. namespace TEAMModelOS.Service.Analysis.Implements
  12. {
  13. public class AchievementService : IAchievementService
  14. {
  15. public IAzureCosmosDBV3Repository _cosmosrepository;
  16. public AchievementService(IAzureCosmosDBV3Repository cosmosDBRepository)
  17. {
  18. _cosmosrepository = cosmosDBRepository;
  19. }
  20. //处理各个班级均分分析
  21. public Dictionary<string, object> GetAverage(List<ExamResult> exams, List<string> gav, List<string> cav, List<List<int>> subSum, List<Student> students)
  22. {
  23. Dictionary<string, object> Average = new Dictionary<string, object>();
  24. List<List<string>> classAverage = new List<List<string>>();
  25. HashSet<string> classList = new HashSet<string>();
  26. List<string> keys = new List<string>();
  27. if (exams.IsNotEmpty())
  28. {
  29. keys.Add("setNo");
  30. keys.Add("name");
  31. keys.Add("score");
  32. keys.Add("classAverage");
  33. keys.Add("gradeAverage");
  34. int j = 0;
  35. exams[0].classes.ForEach(c =>
  36. {
  37. //c.Keys遍历key值
  38. foreach (string key in c.Keys)
  39. {
  40. classList.Add(key);
  41. }
  42. int k = 0;
  43. //每个班级平均分
  44. foreach (int[] value in c.Values)
  45. {
  46. for (int i = value[0]; i <= value[1]; i++)
  47. {
  48. List<string> stuPoint = new List<string>();
  49. keys.ForEach(x =>
  50. {
  51. stuPoint.Add("-");
  52. });
  53. stuPoint[0] = (i + 1).ToString();
  54. foreach (Student ss in students)
  55. {
  56. if (exams[0].ids[i].Equals(ss.studentId))
  57. {
  58. stuPoint[1] = ss.name;
  59. break;
  60. }
  61. }
  62. stuPoint[2] = subSum[j][k].ToString() ;
  63. stuPoint[3] = cav[j];
  64. stuPoint[4] = gav[0];
  65. classAverage.Add(stuPoint);
  66. k++;
  67. }
  68. j++;
  69. }
  70. });
  71. Average.Add("keys",keys);
  72. Average.Add("datas", classAverage);
  73. }
  74. return Average;
  75. }
  76. public List<Dictionary<string, object>> GetPR(List<ExamResult> exams, List<List<int>> subSum,List<Student> students)
  77. {
  78. // 计算公式 PR=100- (100*R-50)/N
  79. List<Dictionary<string, object>> subPR = new List<Dictionary<string, object>>();
  80. List<Dictionary<string, object>> datas = new List<Dictionary<string, object>>();
  81. HashSet<string> classList = new HashSet<string>();
  82. List<string> keys = new List<string>();
  83. List<string> className = new List<string>();
  84. if (exams.IsNotEmpty()) {
  85. keys.Add("name");
  86. keys.Add("setNo");
  87. keys.Add("score");
  88. keys.Add("classRank");
  89. keys.Add("classPR");
  90. keys.Add("gradeRank");
  91. keys.Add("gradePR");
  92. keys.Add("areaRank");
  93. keys.Add("areaPR");
  94. //keys.Add("classAverage");
  95. //keys.Add("gradeAverage");
  96. exams.ForEach(e =>
  97. {
  98. Dictionary<string, object> PR = new Dictionary<string, object>
  99. {
  100. { "name", e.subjectCode }
  101. };
  102. List<string> stuPRG = new List<string>();
  103. List<string> ClassAverage = new List<string>();
  104. List<List<List<string>>> classPR = new List<List<List<string>>>();
  105. //初始化科目总分
  106. double subjectPoint = 0;
  107. //科目平均分
  108. double subAverage = 0;
  109. e.classes.ForEach(c =>
  110. {
  111. List<List<string>> classPRL = new List<List<string>>();
  112. //c.Keys遍历key值
  113. List<string> stuPR = new List<string>();
  114. //初始化各班参考人数
  115. int counts = 0;
  116. //初始化班级缺考人数
  117. int classCount = 0;
  118. //初始化班级平均分
  119. double points = 0;
  120. //初始化班级总分
  121. int classPoint = 0;
  122. foreach (string key in c.Keys)
  123. {
  124. classList.Add(key);
  125. }
  126. //每个班级平均分
  127. foreach (int[] value in c.Values)
  128. {
  129. int seatNo = 1;
  130. for (int i = value[0]; i <= value[1]; i++)
  131. {
  132. List<string> SPR = new List<string>();
  133. keys.ForEach(x =>
  134. {
  135. SPR.Add("-");
  136. });
  137. if (e.result[i].Sum() == 0)
  138. {
  139. classCount++;
  140. seatNo++;
  141. continue;
  142. }
  143. stuPR.Add(e.result[i].Sum().ToString());
  144. stuPRG.Add(e.result[i].Sum().ToString());
  145. SPR[1] = seatNo.ToString() ;
  146. SPR[2] = e.result[i].Sum().ToString();
  147. SPR[0] = e.ids[i];
  148. classPRL.Add(SPR);
  149. //计算班级PR值
  150. //int classPR = 100 - (100*)
  151. classPoint += e.result[i].Sum();
  152. seatNo++;
  153. }
  154. counts = value[1] - value[0] - classCount + 1;
  155. points = Convert.ToDouble(classPoint) / counts;
  156. ClassAverage.Add(points.ToString("0.00"));
  157. }
  158. //foreach (List<string> pl in classPRL) {
  159. //单科计算班级排名
  160. stuPR.Sort(delegate (string s1, string s2) { return int.Parse(s2).CompareTo(int.Parse(s1)); });
  161. foreach (List<string> pl in classPRL) {
  162. int index = stuPR.IndexOf(pl[2]);
  163. //int gradeIndex = stuPRG.IndexOf(pl[2]);
  164. pl[3] = (index + 1).ToString();
  165. int CPR = 100 - (100 * int.Parse(pl[3]) - 50) / stuPR.Count;
  166. pl[4] = CPR.ToString();
  167. //pl.Add(index.ToString());
  168. foreach (Student ss in students)
  169. {
  170. if (pl[0].Equals(ss.studentId))
  171. {
  172. pl[0] = ss.name;
  173. break;
  174. }
  175. }
  176. }
  177. //}
  178. classPR.Add(classPRL);
  179. });
  180. //科目平均分
  181. ClassAverage.ForEach(a =>
  182. {
  183. subjectPoint += Convert.ToDouble(a);
  184. });
  185. subAverage = subjectPoint / ClassAverage.Count();
  186. className = new List<string>();
  187. foreach (string cla in classList)
  188. {
  189. className.Add(cla);
  190. }
  191. //单科计算年级排名
  192. stuPRG.Sort(delegate (string s1, string s2) { return int.Parse(s2).CompareTo(int.Parse(s1)); });
  193. foreach (List<List<string>> p2 in classPR) {
  194. foreach (List<string> p3 in p2) {
  195. int index = stuPRG.IndexOf(p3[2]);
  196. p3[5] = (index + 1).ToString();
  197. int GPR = 100 - (100 * int.Parse(p3[5]) - 50) / stuPRG.Count;
  198. p3[6] = GPR.ToString();
  199. }
  200. }
  201. Dictionary<string, object> data = new Dictionary<string, object>();
  202. Dictionary<string, object> classData = new Dictionary<string, object>();
  203. for (int i =0;i< classPR.Count;i++) {
  204. data.Add(className[i], classPR[i]);
  205. classData.Add(className[i], ClassAverage[i]);
  206. //data.Add(i.ToString(), ClassAverage[i]);
  207. }
  208. PR.Add("keys", keys);
  209. PR.Add("datas", data);
  210. PR.Add("classAverage", classData);
  211. PR.Add("gradeAverage", subAverage.ToString("0.00"));
  212. PR.Add("areaAverage", subAverage.ToString("0.00"));
  213. subPR.Add(PR);
  214. });
  215. //处理全科PR值计算
  216. List<string> stuIds = exams[0].ids;
  217. /*for (int k=0;k< stuIds.Count;k++) {
  218. if (stuIds[k].Equals("0")) {
  219. stuIds.Remove(stuIds[k]);
  220. }
  221. }*/
  222. Dictionary<string, object> dataAll = new Dictionary<string, object>();
  223. dataAll.Add("name", "Total");
  224. dataAll.Add("keys",keys);
  225. List<int> stuAll = new List<int>();
  226. List<int> sortGradePoint = new List<int>();
  227. Dictionary<string, object> classInfo = new Dictionary<string, object>();
  228. List<List<List<string>>> GradePR = new List<List<List<string>>>();
  229. int m = 0;
  230. int p = 0;
  231. List<string> allClassAverages = new List<string>();
  232. Dictionary<string, object> classAllData = new Dictionary<string, object>();
  233. foreach (List<int> classPoint in subSum)
  234. {
  235. List<int> sortClassPoint = new List<int>();
  236. int n = 1;
  237. //初始化缺考人数
  238. int people = 0;
  239. double sumTotle = 0;
  240. List<List<string>> classStuInfo = new List<List<string>>();
  241. for (int i = 0;i < classPoint.Count();i++) {
  242. List<string> stu = new List<string>();
  243. keys.ForEach(x =>
  244. {
  245. stu.Add("-");
  246. });
  247. if (stuIds[m] == "0")
  248. {
  249. people++;
  250. n++;
  251. m++;
  252. continue;
  253. }
  254. stu[0] = stuIds[m];
  255. stu[1] = n.ToString();
  256. stu[2] = classPoint[i].ToString();
  257. sortClassPoint.Add(classPoint[i]);
  258. stuAll.Add(classPoint[i]);
  259. sortGradePoint.Add(classPoint[i]);
  260. sumTotle += classPoint[i];
  261. n++;
  262. m++;
  263. classStuInfo.Add(stu);
  264. }
  265. double allClassAverage = sumTotle / (classPoint.Count - people);
  266. classAllData.Add(className[p], allClassAverage.ToString("0.00"));
  267. //allClassAverages.Add(allClassAverage.ToString("0.00"));
  268. GradePR.Add(classStuInfo);
  269. sortClassPoint.Sort(delegate (int s1, int s2) { return s2.CompareTo(s1); });
  270. foreach (List<string> stuIn in classStuInfo) {
  271. int index = sortClassPoint.IndexOf(int.Parse(stuIn[2]));
  272. stuIn[3] = (index + 1).ToString();
  273. int CPR = 100 - (100 * (index + 1) - 50) / sortClassPoint.Count;
  274. stuIn[4] = CPR.ToString();
  275. foreach (Student ss in students)
  276. {
  277. if (stuIn[0].Equals(ss.studentId))
  278. {
  279. stuIn[0] = ss.name;
  280. break;
  281. }
  282. }
  283. }
  284. p++;
  285. }
  286. sortGradePoint.Sort(delegate (int s1, int s2) { return s2.CompareTo(s1); });
  287. double allGradeAverage = sortGradePoint.Sum() / sortGradePoint.Count();
  288. foreach (List<List<string>> gradeInfo in GradePR)
  289. {
  290. foreach (List<string> info in gradeInfo)
  291. {
  292. int index = sortGradePoint.IndexOf(int.Parse(info[2]));
  293. info[5] = (index + 1).ToString();
  294. int GPR = 100 - (100 * int.Parse(info[5]) - 50) / sortGradePoint.Count;
  295. info[6] = GPR.ToString();
  296. }
  297. }
  298. Dictionary<string, object> classDataAll = new Dictionary<string, object>();
  299. for (int i = 0; i < GradePR.Count; i++)
  300. {
  301. classDataAll.Add(className[i], GradePR[i]);
  302. }
  303. dataAll.Add("datas", classDataAll);
  304. dataAll.Add("classAverage", classAllData);
  305. dataAll.Add("gradeAverage", allGradeAverage.ToString("0.00"));
  306. dataAll.Add("areaAverage", allGradeAverage.ToString("0.00"));
  307. subPR.Add(dataAll);
  308. }
  309. return subPR;
  310. }
  311. public List<List<string>> ReName(List<List<string>> datas, List<string> ids, List<Dictionary<string, int[]>> classToName, List<Student> students)
  312. {
  313. foreach (List<string> tableName in datas)
  314. {
  315. int index = ids.IndexOf(tableName[0]);
  316. if (index == -1)
  317. {
  318. continue;
  319. }
  320. foreach (Dictionary<string, int[]> classKey in classToName)
  321. {
  322. bool flag = false;
  323. foreach (KeyValuePair<string, int[]> kvp in classKey)
  324. {
  325. if (kvp.Value[1] >= index && index >= kvp.Value[0])
  326. {
  327. tableName[1] = kvp.Key;
  328. flag = true;
  329. break;
  330. }
  331. }
  332. if (flag)
  333. {
  334. break;
  335. }
  336. }
  337. foreach (Student ss in students)
  338. {
  339. if (tableName[0].Equals(ss.studentId))
  340. {
  341. tableName[0] = ss.name;
  342. break;
  343. }
  344. }
  345. }
  346. return datas;
  347. }
  348. }
  349. }