123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using TEAMModelOS.SDK;
- using TEAMModelOS.SDK.Extension;
- using TEAMModelOS.SDK.Models;
- namespace TEAMModelOS.SDK.Services
- {
- public static class ItemService
- {
- /// <summary>
- /// 计算题目的条件数量变化
- /// </summary>
- /// <param name="newItem"></param>
- /// <param name="odlItem"></param>
- /// <param name="cond"></param>
- public static void CountItemCond(ItemInfo newItem, ItemInfo odlItem, ItemCond cond)
- {
-
- if (newItem != null ? newItem.scope.Equals("school") : false || odlItem != null ? odlItem.scope.Equals("school") : false)
- {
- //检查两个对象是否是同一条记录
- if (newItem != null && odlItem == null)
- {
- string newKey = $"{newItem.subjectId}";
- List<string> grade = newItem.gradeIds;
- UpdateItemCond(cond, true, newKey, grade, newItem.type, newItem.level, newItem.field.HasValue ? newItem.field.Value : 0, newItem);
- }
- else if (newItem != null && odlItem != null)
- {
- //更新时 需要保证两个题的 id code 一致
- if (newItem.id == odlItem.id && newItem.code == odlItem.code)
- {
- //先增加
- string newKey = $"{newItem.subjectId}";
- List<string> newGrade = newItem.gradeIds;
- UpdateItemCond(cond, true, newKey, newGrade, newItem.type, newItem.level, newItem.field.HasValue ? newItem.field.Value : 0, newItem);
- //后变更删除
- string oldKey = $"{odlItem.subjectId}";
- List<string> oldGrade = odlItem.gradeIds;
- UpdateItemCond(cond, false, oldKey, oldGrade, odlItem.type, odlItem.level, odlItem.field.HasValue ? odlItem.field.Value : 0, newItem);
- }
- }
- else if (newItem == null && odlItem != null)
- {
- string oldKey = $"{odlItem.subjectId}";
- List<string> oldGrade = odlItem.gradeIds;
- UpdateItemCond(cond, false, oldKey, oldGrade, odlItem.type, odlItem.level, odlItem.field.HasValue ? odlItem.field.Value : 0, null);
- }
- else
- {
- // throw new Exception();
- }
- } else if (newItem != null ? newItem.scope.Equals("private") : false || odlItem != null ? odlItem.scope.Equals("private") : false) {
- //检查两个对象是否是同一条记录
- if (newItem != null && odlItem == null)
- {
- string newKey = $"{newItem.code}".Replace("Item-", "");
- List<string> grade = new List<string> { newKey };
- UpdateItemCond(cond, true, newKey, grade, newItem.type, newItem.level, newItem.field.HasValue ? newItem.field.Value : 0, newItem);
- }
- else if (newItem != null && odlItem != null)
- {
- //更新时 需要保证两个题的 id code 一致
- if (newItem.id == odlItem.id && newItem.code == odlItem.code)
- {
- //先增加
- string newKey = $"{newItem.code}".Replace("Item-","");
- List<string> newGrade = new List<string> { newKey };
- UpdateItemCond(cond, true, newKey, newGrade, newItem.type, newItem.level, newItem.field.HasValue ? newItem.field.Value : 0, newItem);
- //后变更删除
- string oldKey = $"{odlItem.code}".Replace("Item-", "");
- List<string> oldGrade = new List<string> { oldKey };
- UpdateItemCond(cond, false, oldKey, oldGrade, odlItem.type, odlItem.level, odlItem.field.HasValue ? odlItem.field.Value : 0, newItem);
- }
- }
- else if (newItem == null && odlItem != null)
- {
- string oldKey = $"{odlItem.code}".Replace("Item-", "");
- List<string> oldGrade = new List<string> { oldKey };
- UpdateItemCond(cond, false, oldKey, oldGrade, odlItem.type, odlItem.level, odlItem.field.HasValue ? odlItem.field.Value : 0, null);
- }
- else
- {
- // throw new Exception();
- }
- }
- }
-
- /// <summary>
- /// opt=false 减 true 增
- /// key 科目id
- ///
- /// </summary>
- /// <param name="cond"></param>
- /// <param name="opt"></param>
- public static void UpdateItemCond(ItemCond cond, bool opt, string key, List<string> grade, string type, int level, int field,ItemInfo newItem)
- {
-
- int count = 0;
- if (opt)
- {
- count = 1;
- }
- else
- {
- //未计入的则默认0
- count = -1;
- }
- foreach (var x in grade) {
- bool none = true;
- if (x == null)
- {
- }
- for (int index = 0; index < cond.grades.Count; index++)
- {
- if (x == cond.grades[index].id)
- {
- var gc = cond.grades[index].count + count;
- cond.grades[index].count = gc >= 0 ? gc : 0;
- none = false;
- break;
- }
- }
- if (none)
- {
- cond.grades.Add(new GradeCount { id = x, count = count });
- }
- }
- SubjectCount subject = cond.subjects.Find(x => x.id == key);
- if (subject == null)
- {
- cond.subjects.Add(new SubjectCount() { id=key, count =0});
- }
- var list = cond.subjects.FindAll(x => x.id == key).ToList();
- foreach (var y in list) {
-
- if (!y.types.ContainsKey(type))
- {
- var dict = new Dictionary<string, Dictionary<string, int>>
- {
- {
- "level",new Dictionary<string, int>
- {
- { "1",0},{ "2",0}, { "3", 0 }, { "4", 0 }, { "5", 0 }
- }
- },
- {
- "field",new Dictionary<string, int>
- {
- { "1",0},{ "2",0}, { "3", 0 }, { "4", 0 }, { "5", 0 }, { "6", 0 }
- }
- }
- };
- dict["level"][$"{level}"] = count;
- dict["field"][$"{field}"] = count;
- y.types.Add(type, dict);
- }
- else
- {
- var lc = y.types[type]["level"][$"{level}"] + count;
- y.types[type]["level"][$"{level}"] = lc >= 0? lc : 0;
- var fc = y.types[type]["field"][$"{field}"] + count;
- y.types[type]["field"][$"{field}"] = fc >= 0? fc : 0;
- }
- y.count = y.count + count;
- }
- var sum = cond.subjects.Select(x => x.count).Sum();
- cond.count = sum>=0?sum:0;
- }
- }
- }
|