CrazyIter_Bin 2 лет назад
Родитель
Сommit
fc536c488e

+ 60 - 3
TEAMModelOS.FunctionV4/HttpTrigger/IESHttpTrigger.cs

@@ -82,10 +82,67 @@ namespace TEAMModelOS.FunctionV4.HttpTrigger
             var response = req.CreateResponse(HttpStatusCode.OK);
             string data = await new StreamReader(req.Body).ReadToEndAsync();
             var json = JsonDocument.Parse(data).RootElement;
-            List<Class> graduate_classes= new List<Class>();
+           
+            string schoolId = null ;
+            if (json.TryGetProperty("schoolId", out JsonElement _schoolId))
+            {
+                schoolId = $"{_schoolId}";
+            }
+            if (string.IsNullOrEmpty(schoolId)) {
+                return response;
+            }
+            //计算毕业的
             if (json.TryGetProperty("graduate_classes", out JsonElement _graduate_classes)) {
-                graduate_classes = _graduate_classes.ToObject<List<Class>>();
-                var ids = graduate_classes.Where(x => !string.IsNullOrWhiteSpace(x.id)).Select(x => x.id);
+                List<Class> graduate_classes = _graduate_classes.ToObject<List<Class>>();
+                if (graduate_classes.IsNotEmpty())
+                {
+                    var ids = graduate_classes.Where(x => !string.IsNullOrWhiteSpace(x.id)).Select(x => $"'{x.id}'");
+                    List<Student> students = new List<Student>();
+                    string sql = $"select value c from c where  (c.graduate = 0 or  IS_DEFINED(c.graduate) = false)  and  c.classId in ({string.Join(",", ids)})";
+                    await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Student)
+                        .GetItemQueryIterator<Student>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Base-{schoolId}") }))
+                    {
+                        item.graduate = 1;
+                        students.Add(item);
+                    }
+                    foreach (var item in students)
+                    {
+                        item.graduate = 1;
+                        await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Student).ReplaceItemAsync<Student>(item, item.id, new PartitionKey($"Base-{schoolId}"));
+                    }
+                    foreach (var item in graduate_classes)
+                    {
+                        item.graduate = 1;
+                        await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).ReplaceItemAsync<Class>(item, item.id, new PartitionKey($"Class-{schoolId}"));
+                    }
+                }
+            }
+            //未毕业的
+            if (json.TryGetProperty("cancel_graduate_classes", out JsonElement _cancel_graduate_classes))
+            {
+                List<Class> cancel_graduate_classes = _cancel_graduate_classes.ToObject<List<Class>>();
+                if (cancel_graduate_classes.IsNotEmpty())
+                {
+                    var ids = cancel_graduate_classes.Where(x => !string.IsNullOrWhiteSpace(x.id)).Select(x => $"'{x.id}'");
+                    List<Student> students = new List<Student>();
+                    string sql = $"select value c from c where  c.graduate =1   and  c.classId in ({string.Join(",", ids)})";
+                    await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Student)
+                        .GetItemQueryIterator<Student>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Base-{schoolId}") }))
+                    {
+                        item.graduate = 0;
+                        students.Add(item);
+                    }
+                    foreach (var item in students)
+                    {
+                        item.graduate = 0;
+                        await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Student).ReplaceItemAsync<Student>(item, item.id, new PartitionKey($"Base-{schoolId}"));
+                    }
+                    foreach (var item in cancel_graduate_classes)
+                    {
+                        item.graduate = 0;
+                        await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).ReplaceItemAsync<Class>(item, item.id, new PartitionKey($"Class-{schoolId}"));
+                    }
+                }
             }
             return response;
         }

+ 1 - 1
TEAMModelOS.SDK/DI/HttpTrigger/HttpTrigger.cs

@@ -38,7 +38,7 @@ namespace TEAMModelOS.SDK.DI
             string domain = "";
             if (location.Equals("China-Dep"))
             {
-                domain = keys[2];
+                domain = keys[1];
             }
             else if (location.Equals("China-Test"))
             {

+ 72 - 1
TEAMModelOS.SDK/Models/Service/SchoolService.cs

@@ -1,4 +1,5 @@
-using HTEXLib.COMM.Helpers;
+using Azure.Cosmos;
+using HTEXLib.COMM.Helpers;
 using Microsoft.AspNetCore.Hosting;
 using Microsoft.International.Converters.PinYinConverter;
 using Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter;
@@ -20,6 +21,76 @@ namespace TEAMModelOS.SDK
     public class SchoolService
     {
 
+
+        public static async Task<(List<Class> school_classes, List<Class>  graduate_classes)> DoGraduateClasses(HttpTrigger _httpTrigger, AzureCosmosFactory _azureCosmos, List<string> periodIds,School school_base, Option _option) {
+            var client = _azureCosmos.GetCosmosClient();
+            //取得班级
+            List<Class> school_classes = new List<Class>();
+            int nowYear = DateTimeOffset.UtcNow.Year;
+            int nowMonth = DateTimeOffset.UtcNow.Year;
+            int nowDay = DateTimeOffset.UtcNow.Year;
+            List<Class> graduate_classes = new List<Class>();
+            //因为修改年级,而导致取消的
+            List<Class> cancel_graduate_classes = new List<Class>();
+            string sql = $"SELECT value c  FROM c ";
+            if (periodIds.IsNotEmpty()) {
+                sql = $"SELECT value c  FROM c where c.periodId in ({string.Join(",",periodIds.Select(x=>$"'{x}'"))}) ";
+            }
+            await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<Class>
+                 //(queryText: $"SELECT value c  FROM c where (c.graduate = 0 or  IS_DEFINED(c.graduate) = false)",
+                 (queryText: sql,
+                requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Class-{school_base.id}") }))
+            {
+                bool isgraduate = false;
+                if (!string.IsNullOrWhiteSpace(item.periodId))
+                {
+                    var period = school_base.period.Find(x => x.id.Equals(item.periodId));
+                    if (period != null)
+                    {
+                        var gradeCount = period.grades.Count();
+                        //2022-2016=6(待判断月份日期是否是毕业)  2022-2017=5(未毕业)
+                        if (nowYear - item.year >= gradeCount)
+                        {
+                            var semester = period.semesters.Find(x => x.start == 1);
+                            if (semester != null)
+                            {
+                                if (nowMonth > semester.month && nowDay > semester.day)
+                                {
+                                    isgraduate = true;
+                                }
+                            }
+                        }
+                    }
+                }
+                if (isgraduate)
+                {
+                    graduate_classes.Add(item);
+                }
+                else
+                {
+                    if (item.graduate == 1)
+                    {
+                        item.graduate = 0;
+                        cancel_graduate_classes.Add(item);
+                    }
+                    school_classes.Add(item);
+                }
+            }
+            if (graduate_classes.Any() || cancel_graduate_classes.Any())
+            {
+                try
+                {
+                    _ = _httpTrigger.RequestHttpTrigger(new { graduate_classes = graduate_classes, cancel_graduate_classes = cancel_graduate_classes, schoolId = $"{school_base.id}" }, _option.Location, "graduate-change");
+                }
+                catch (Exception ex)
+                {
+                    //暂不处理
+                }
+            }
+            return (school_classes,graduate_classes);
+        }
+
+
         public static async Task<object> DoScsApiSchool(HttpTrigger _httpTrigger,Dictionary<string,object> dict, Option _option, List<IdNameCode> ignore,
             string areaId,string city,string dist, AzureStorageFactory _azureStorage ,DingDing _dingDing, IWebHostEnvironment _environment
             )

+ 22 - 1
TEAMModelOS/Controllers/School/SchoolController.cs

@@ -50,7 +50,8 @@ namespace TEAMModelOS.Controllers
         private readonly CoreAPIHttpService _coreAPIHttpService;
         private readonly NotificationService _notificationService;
         private readonly HttpClient _httpClient;
-        public SchoolController(CoreAPIHttpService coreAPIHttpService, NotificationService notificationService, AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, DingDing dingDing, IOptionsSnapshot<Option> option, IConfiguration configuration, HttpClient httpClient)
+        private readonly HttpTrigger _httpTrigger;
+        public SchoolController(HttpTrigger httpTrigger,CoreAPIHttpService coreAPIHttpService, NotificationService notificationService, AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, DingDing dingDing, IOptionsSnapshot<Option> option, IConfiguration configuration, HttpClient httpClient)
         {
             _azureCosmos = azureCosmos;
             _azureStorage = azureStorage;
@@ -61,6 +62,7 @@ namespace TEAMModelOS.Controllers
             _coreAPIHttpService = coreAPIHttpService;
             _notificationService = notificationService;
             _httpClient = httpClient;
+            _httpTrigger = httpTrigger;
         }
         /// <summary>
         /// 修改学校信息
@@ -139,6 +141,25 @@ namespace TEAMModelOS.Controllers
                 var response = await schoolContainer.ReadItemStreamAsync(school.id, new PartitionKey($"Base"));
                 if (response.Status == 200)
                 {
+                    School db_school = JsonDocument.Parse(response.Content).RootElement.Deserialize<School>();
+                    //计算 比较更改前后 学段的年级变化的学段,并重新处理行政班的毕业状态。
+                    //变更后
+                    List<KeyValuePair<string, int>> period_gradeCount = new List<KeyValuePair<string, int>>();
+                    school.period.ForEach(z => { period_gradeCount.Add(new KeyValuePair<string, int>(z.id, z.grades.Count)); });
+                    //变更前
+                    List<KeyValuePair<string, int>> dbperiod_gradeCount = new List<KeyValuePair<string, int>>();
+                    db_school.period.ForEach(z => { dbperiod_gradeCount.Add(new KeyValuePair<string, int>(z.id, z.grades.Count)); });
+                    List<string> periodIds = new List<string>();
+                    period_gradeCount.ForEach(x => {
+                        var gradeCount =   dbperiod_gradeCount.Find(y => x.Key.Equals(y.Key));
+                        if (!string.IsNullOrWhiteSpace(gradeCount.Key)) {
+                            if (gradeCount.Value != x.Value) {
+                                periodIds.Add(x.Key);
+                            }
+                        }
+                    });
+                    await SchoolService.DoGraduateClasses(_httpTrigger, _azureCosmos, periodIds, school, _option);
+                    //变更教师关联的学校基本信息
                     string sql = $"SELECT distinct value(c) FROM c join A1 in c.schools where A1.schoolId='{school.id}'";
                     List<Teacher> teachers = new List<Teacher>();
                     await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<Teacher>(sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("Base") }))

+ 1 - 40
TEAMModelOS/Controllers/Teacher/InitController.cs

@@ -616,46 +616,7 @@ namespace TEAMModelOS.Controllers
                 var auth_token = JwtAuthExtension.CreateAuthToken(_option.HostName, id, name?.ToString(), picture?.ToString(), _option.JwtSecretKey, Website: "IES", scope: Constant.ScopeTeacher, schoolID: school_code.ToString(), areaId: currAreaId, standard: school_base.standard, roles: roles.ToArray(), permissions: permissions.ToArray(), expire: 1);
 
                 //取得班级
-                List<Class> school_classes = new List<Class>();
-                int nowYear = DateTimeOffset.UtcNow.Year;
-                int nowMonth = DateTimeOffset.UtcNow.Year;
-                int nowDay = DateTimeOffset.UtcNow.Year;
-                List<Class> graduate_classes = new List<Class>();
-                await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<Class>
-                    (queryText: $"SELECT value c  FROM c where (c.graduate = 0 or  IS_DEFINED(c.graduate) = false)",
-                    requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Class-{school_code}") }))
-                {
-                    bool isgraduate = false;
-                    if (!string.IsNullOrWhiteSpace(item.periodId))
-                    {
-                        var period = school_base.period.Find(x => x.id.Equals(item.periodId));
-                        if (period != null)
-                        {
-                            var gradeCount = period.grades.Count();
-                            //2022-2016=6(待判断月份日期是否是毕业)  2022-2017=5(未毕业)
-                            if (nowYear - item.year >= gradeCount)
-                            {
-                                var semester = period.semesters.Find(x => x.start == 1);
-                                if (semester != null)
-                                {
-                                    if (nowMonth > semester.month && nowDay > semester.day) { 
-                                        isgraduate = true;
-                                    }
-                                }
-                            }
-                        }
-                    }
-                    if (isgraduate)
-                    {
-                        graduate_classes.Add(item);
-                    }
-                    else {
-                        school_classes.Add(item);
-                    }
-                }
-                if (graduate_classes.Any()) { 
-                //_httpTrigger()
-                }
+                (List < Class > school_classes,_) = await SchoolService.DoGraduateClasses(_httpTrigger, _azureCosmos, null, school_base, _option);
                 //取得教室
                 List<Room> school_rooms = new List<Room>();
                 await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<Room>(queryText: $"select value(c) from c ",