ItemService.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using TEAMModelOS.SDK.Models;
  6. namespace TEAMModelOS.Services.Common
  7. {
  8. public static class ItemService
  9. {
  10. /// <summary>
  11. /// 计算题目的条件数量变化
  12. /// </summary>
  13. /// <param name="newItem"></param>
  14. /// <param name="odlItem"></param>
  15. /// <param name="cond"></param>
  16. public static void CountItemCond(ItemInfo newItem, ItemInfo odlItem, ItemCond cond)
  17. {
  18. //检查两个对象是否是同一条记录
  19. if (newItem != null && odlItem == null)
  20. {
  21. string newKey = $"{newItem.subjectId}";
  22. List<string> grade = newItem.gradeIds;
  23. UpdateItemCond(cond, true, newKey, grade, newItem.type, newItem.level, newItem.field.HasValue ? newItem.field.Value : 0);
  24. }
  25. else if (newItem != null && odlItem != null)
  26. {
  27. //更新时 需要保证两个题的 id code 一致
  28. if (newItem.id == odlItem.id && newItem.code == odlItem.code)
  29. {
  30. //先增加
  31. string newKey = $"{newItem.subjectId}";
  32. List<string> newGrade = newItem.gradeIds;
  33. UpdateItemCond(cond, true, newKey, newGrade, newItem.type, newItem.level, newItem.field.HasValue ? newItem.field.Value : 0);
  34. //后变更删除
  35. string oldKey = $"{odlItem.subjectId}";
  36. List<string> oldGrade = odlItem.gradeIds;
  37. UpdateItemCond(cond, false, oldKey, oldGrade, odlItem.type, odlItem.level, odlItem.field.HasValue ? odlItem.field.Value : 0);
  38. }
  39. }
  40. else if (newItem == null && odlItem != null)
  41. {
  42. string oldKey = $"{odlItem.subjectId}";
  43. List<string> oldGrade = odlItem.gradeIds;
  44. UpdateItemCond(cond, false, oldKey, oldGrade, odlItem.type, odlItem.level, odlItem.field.HasValue ? odlItem.field.Value : 0);
  45. }
  46. else
  47. {
  48. // throw new Exception();
  49. }
  50. }
  51. /// <summary>
  52. /// opt=false 减 true 增
  53. /// key 科目id
  54. ///
  55. /// </summary>
  56. /// <param name="cond"></param>
  57. /// <param name="opt"></param>
  58. public static void UpdateItemCond(ItemCond cond, bool opt, string key, List<string> grade, string type, int level, int field)
  59. {
  60. int count = 0;
  61. if (opt)
  62. {
  63. count = 1;
  64. }
  65. else
  66. {
  67. //未计入的则默认0
  68. count = -1;
  69. }
  70. grade.ForEach(x => {
  71. bool none = true;
  72. for (int index = 0; index < cond.grades.Count; index++)
  73. {
  74. if (x == cond.grades[index].id)
  75. {
  76. cond.grades[index].count = cond.grades[index].count + count;
  77. none = false;
  78. break;
  79. }
  80. }
  81. if (none)
  82. {
  83. cond.grades.Add(new GradeCount { id = x, count = count });
  84. }
  85. });
  86. SubjectCount subject = cond.subjects.Where(x => x.id == key).FirstOrDefault();
  87. if (subject == null)
  88. {
  89. cond.subjects.Add(new SubjectCount() { id=key});
  90. }
  91. cond.subjects.Where(x => x.id == key).ToList().ForEach(y => {
  92. if (!y.types.ContainsKey(type))
  93. {
  94. var dict = new Dictionary<string, Dictionary<string, int>>
  95. {
  96. {
  97. "level",new Dictionary<string, int>
  98. {
  99. { "1",0},{ "2",0}, { "3", 0 }, { "4", 0 }, { "5", 0 }
  100. }
  101. },
  102. {
  103. "field",new Dictionary<string, int>
  104. {
  105. { "1",0},{ "2",0}, { "3", 0 }, { "4", 0 }, { "5", 0 }, { "6", 0 }
  106. }
  107. }
  108. };
  109. dict["level"][$"{level}"] = count;
  110. dict["field"][$"{field}"] = count;
  111. y.types.Add(type, dict);
  112. }
  113. else
  114. {
  115. y.types[type]["level"][$"{level}"] = y.types[type]["level"][$"{level}"] + count;
  116. y.types[type]["field"][$"{level}"] = y.types[type]["field"][$"{field}"] + count;
  117. }
  118. });
  119. //var aaa = cond.subjects.Select(x=>x.types.Values).ToList();
  120. }
  121. }
  122. }