AchievementService.cs 16 KB

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