ItemService.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using TEAMModelOS.SDK;
  6. using TEAMModelOS.SDK.Extension;
  7. using TEAMModelOS.SDK.Models;
  8. namespace TEAMModelOS.SDK.Services
  9. {
  10. public static class ItemService
  11. {
  12. /// <summary>
  13. /// 计算题目的条件数量变化
  14. /// </summary>
  15. /// <param name="newItem"></param>
  16. /// <param name="odlItem"></param>
  17. /// <param name="cond"></param>
  18. public static void CountItemCond(ItemInfo newItem, ItemInfo odlItem, ItemCond cond)
  19. {
  20. if (newItem != null ? newItem.scope.Equals("school") : false || odlItem != null ? odlItem.scope.Equals("school") : false)
  21. {
  22. //检查两个对象是否是同一条记录
  23. if (newItem != null && odlItem == null)
  24. {
  25. string newKey = $"{newItem.subjectId}";
  26. List<string> grade = newItem.gradeIds;
  27. UpdateItemCond(cond, true, newKey, grade, newItem.type, newItem.level, newItem.field.HasValue ? newItem.field.Value : 0, newItem);
  28. }
  29. else if (newItem != null && odlItem != null)
  30. {
  31. //更新时 需要保证两个题的 id code 一致
  32. if (newItem.id == odlItem.id && newItem.code == odlItem.code)
  33. {
  34. //先增加
  35. string newKey = $"{newItem.subjectId}";
  36. List<string> newGrade = newItem.gradeIds;
  37. UpdateItemCond(cond, true, newKey, newGrade, newItem.type, newItem.level, newItem.field.HasValue ? newItem.field.Value : 0, newItem);
  38. //后变更删除
  39. string oldKey = $"{odlItem.subjectId}";
  40. List<string> oldGrade = odlItem.gradeIds;
  41. UpdateItemCond(cond, false, oldKey, oldGrade, odlItem.type, odlItem.level, odlItem.field.HasValue ? odlItem.field.Value : 0, newItem);
  42. }
  43. }
  44. else if (newItem == null && odlItem != null)
  45. {
  46. string oldKey = $"{odlItem.subjectId}";
  47. List<string> oldGrade = odlItem.gradeIds;
  48. UpdateItemCond(cond, false, oldKey, oldGrade, odlItem.type, odlItem.level, odlItem.field.HasValue ? odlItem.field.Value : 0, null);
  49. }
  50. else
  51. {
  52. // throw new Exception();
  53. }
  54. } else if (newItem != null ? newItem.scope.Equals("private") : false || odlItem != null ? odlItem.scope.Equals("private") : false) {
  55. //检查两个对象是否是同一条记录
  56. if (newItem != null && odlItem == null)
  57. {
  58. string newKey = $"{newItem.code}".Replace("Item-", "");
  59. List<string> grade = new List<string> { newKey };
  60. UpdateItemCond(cond, true, newKey, grade, newItem.type, newItem.level, newItem.field.HasValue ? newItem.field.Value : 0, newItem);
  61. }
  62. else if (newItem != null && odlItem != null)
  63. {
  64. //更新时 需要保证两个题的 id code 一致
  65. if (newItem.id == odlItem.id && newItem.code == odlItem.code)
  66. {
  67. //先增加
  68. string newKey = $"{newItem.code}".Replace("Item-","");
  69. List<string> newGrade = new List<string> { newKey };
  70. UpdateItemCond(cond, true, newKey, newGrade, newItem.type, newItem.level, newItem.field.HasValue ? newItem.field.Value : 0, newItem);
  71. //后变更删除
  72. string oldKey = $"{odlItem.code}".Replace("Item-", "");
  73. List<string> oldGrade = new List<string> { oldKey };
  74. UpdateItemCond(cond, false, oldKey, oldGrade, odlItem.type, odlItem.level, odlItem.field.HasValue ? odlItem.field.Value : 0, newItem);
  75. }
  76. }
  77. else if (newItem == null && odlItem != null)
  78. {
  79. string oldKey = $"{odlItem.code}".Replace("Item-", "");
  80. List<string> oldGrade = new List<string> { oldKey };
  81. UpdateItemCond(cond, false, oldKey, oldGrade, odlItem.type, odlItem.level, odlItem.field.HasValue ? odlItem.field.Value : 0, null);
  82. }
  83. else
  84. {
  85. // throw new Exception();
  86. }
  87. }
  88. }
  89. /// <summary>
  90. /// opt=false 减 true 增
  91. /// key 科目id
  92. ///
  93. /// </summary>
  94. /// <param name="cond"></param>
  95. /// <param name="opt"></param>
  96. public static void UpdateItemCond(ItemCond cond, bool opt, string key, List<string> grade, string type, int level, int field,ItemInfo newItem)
  97. {
  98. int count = 0;
  99. if (opt)
  100. {
  101. count = 1;
  102. }
  103. else
  104. {
  105. //未计入的则默认0
  106. count = -1;
  107. }
  108. foreach (var x in grade) {
  109. bool none = true;
  110. if (x == null)
  111. {
  112. }
  113. for (int index = 0; index < cond.grades.Count; index++)
  114. {
  115. if (x == cond.grades[index].id)
  116. {
  117. var gc = cond.grades[index].count + count;
  118. cond.grades[index].count = gc >= 0 ? gc : 0;
  119. none = false;
  120. break;
  121. }
  122. }
  123. if (none)
  124. {
  125. cond.grades.Add(new GradeCount { id = x, count = count });
  126. }
  127. }
  128. SubjectItemCount subject = cond.subjects.Find(x => x.id == key);
  129. if (subject == null)
  130. {
  131. cond.subjects.Add(new SubjectItemCount() { id=key, count =0});
  132. }
  133. var list = cond.subjects.FindAll(x => x.id == key).ToList();
  134. foreach (var y in list) {
  135. if (!y.types.ContainsKey(type))
  136. {
  137. var dict = new Dictionary<string, Dictionary<string, int>>
  138. {
  139. {
  140. "level",new Dictionary<string, int>
  141. {
  142. { "1",0},{ "2",0}, { "3", 0 }, { "4", 0 }, { "5", 0 }
  143. }
  144. },
  145. {
  146. "field",new Dictionary<string, int>
  147. {
  148. { "1",0},{ "2",0}, { "3", 0 }, { "4", 0 }, { "5", 0 }, { "6", 0 }
  149. }
  150. }
  151. };
  152. dict["level"][$"{level}"] = count;
  153. dict["field"][$"{field}"] = count;
  154. y.types.Add(type, dict);
  155. }
  156. else
  157. {
  158. var lc = y.types[type]["level"][$"{level}"] + count;
  159. y.types[type]["level"][$"{level}"] = lc >= 0? lc : 0;
  160. var fc = y.types[type]["field"][$"{field}"] + count;
  161. y.types[type]["field"][$"{field}"] = fc >= 0? fc : 0;
  162. }
  163. y.count = y.count + count;
  164. }
  165. var sum = cond.subjects.Select(x => x.count).Sum();
  166. cond.count = sum>=0?sum:0;
  167. }
  168. }
  169. }