Forráskód Böngészése

[[School/StudentController]StudentManage 調整更新API,批量更新ClassId。]

Mickey 4 éve
szülő
commit
26feaedc72
1 módosított fájl, 83 hozzáadás és 60 törlés
  1. 83 60
      TEAMModelOS/Controllers/School/StudentController.cs

+ 83 - 60
TEAMModelOS/Controllers/School/StudentController.cs

@@ -1647,12 +1647,13 @@ namespace TEAMModelOS.Controllers
         }
 
         /// <summary>
-        /// 批量更新學生資訊,目前支持更新姓名、密碼、座號、性別及教室id。
+        /// 批量更新學生資訊,目前支持更新姓名、密碼、座號、性別及教室id,匯入時ClassId為必填
         /// </summary>
         /// <param name="schoolId"></param>
         /// <param name="students"></param>
         /// <returns></returns>
-        private async Task<(List<object> studs, Dictionary<string, List<string>> classDuplNos, List<string> nonexistentIds, List<string> errorIds, Dictionary<string, List<string>> errorNos, List<string> errorClassId)> updateStudents(string schoolId, JsonElement.ArrayEnumerator students)
+        private async Task<(List<object> studs, Dictionary<string, List<string>> classDuplNos, List<string> nonexistentIds, List<string> errorIds, Dictionary<string, List<string>> errorNos, List<string> errorClassId)> 
+            updateStudents(string schoolId, JsonElement.ArrayEnumerator students)
         {
             try
             {
@@ -1686,15 +1687,15 @@ namespace TEAMModelOS.Controllers
                         //確認是否有id欄位,並且確認是否有給pw欄位,若無給或是null empty等,則使用id當密碼。
                         if (!string.IsNullOrWhiteSpace(id.GetString()))
                         {
-                            string salt = string.Empty,
-                                pw = string.Empty,
-                                name = string.Empty,
-                                year = string.Empty,
-                                gender = string.Empty,
-                                mail = string.Empty,
-                                mobile = string.Empty,
-                                classId = string.Empty,
-                                no = string.Empty;
+                            string salt = null,
+                                   pw = null,
+                                   name = null,
+                                   year = null,
+                                   gender = null,
+                                   mail = null,
+                                   mobile = null,
+                                   classId = null,
+                                   no = null;
 
                             //有給pw欄位才進行處理
                             if (student.TryGetProperty("pw", out var tmpPw))
@@ -1704,59 +1705,69 @@ namespace TEAMModelOS.Controllers
                                     ? Utils.HashedPassword(tmpPw.GetString(), salt)
                                     : Utils.HashedPassword(id.GetString(), salt);
                             }
-                            if (student.TryGetProperty("name", out var tmpName))
-                            {
-                                name = tmpName.GetString();
-                            }
-                            if (student.TryGetProperty("gender", out var tmpGender))
-                            {
-                                gender = tmpGender.GetString();
-                            }
-                            if (student.TryGetProperty("mail", out var tmpMail))
-                            {
-                                mail = tmpMail.GetString();
-                            }
-                            if (student.TryGetProperty("mobile", out var tmpMobile))
-                            {
-                                mobile = tmpMobile.GetString();
-                            }
-                            if (student.TryGetProperty("year", out var tmpYear))
-                            {
-                                year = tmpYear.GetString();
-                            }
-                            if (student.TryGetProperty("classId", out var tmpclassId))
-                            {
-                                classId = tmpclassId.GetString();
-                            }
-                            if (student.TryGetProperty("no", out var tmpNo))
-                            {
-                                no = tmpNo.GetString();
-                            }
-
+                            if (student.TryGetProperty("name", out var tmpName)) name = tmpName.GetString();                            
+                            if (student.TryGetProperty("gender", out var tmpGender)) gender = tmpGender.GetString();
+                            if (student.TryGetProperty("mail", out var tmpMail)) mail = tmpMail.GetString();
+                            if (student.TryGetProperty("mobile", out var tmpMobile)) mobile = tmpMobile.GetString();
+                            if (student.TryGetProperty("year", out var tmpYear)) year = tmpYear.GetString();
+                            //TODO : 處理未給該欄位的id
+                            if (student.TryGetProperty("classId", out var tmpclassId)) classId = tmpclassId.GetString();
+                            else continue;
+
+                            //如果有給該欄位,且是給空的,代表要清空
+                            if (student.TryGetProperty("no", out var tmpNo)) no = tmpNo.GetString();
+                            
                             if (!studentInfos.ContainsKey(id.GetString()))
                             {
-                                //整理出後面查詢座號要用的參數
-                                if (string.IsNullOrWhiteSpace(classId))
+                                /**
+                                ////整理出後面查詢座號要用的參數
+                                //if (string.IsNullOrWhiteSpace(classId))
+                                //{
+                                //    //如果有給classId且是給空的,則也將no設為空,後續才能將no欄位清空。
+                                //    if (classId != null && classId.Length == 0) no = string.Empty;
+                                //    //pic,mail,mobile暫不支持更新
+                                //    studentInfos.Add(id.GetString(), (salt, pw, name, year, null, gender, null, null, classId, no));
+                                //}
+                                //else
+                                //{
+                                //    //如果有給classId且是給空的,則也將no設為空,後續才能將no欄位清空。
+                                //    if (classId != null && classId.Length == 0) no = string.Empty;
+
+                                //    if (classStuds.ContainsKey(classId))
+                                //    {
+                                //        classStuds[classId].Add((id.GetString(), salt, pw, name, year, null, gender, null, null, classId, no));
+                                //    }
+                                //    else
+                                //    {
+                                //        classStuds.Add(
+                                //            classId,
+                                //            new List<(string id, string salt, string pw, string name, string year, string pic, string gender, string mail, string mobile, string classId, string no)>()
+                                //            { (id.GetString(), salt, pw, name, year, null, gender, null, null, classId, no) });
+                                //    }
+                                //    //pic,mail,mobile暫不支持批量更新
+                                //    studentInfos.Add(id.GetString(), (salt, pw, name, year, null, gender, null, null, classId, no));
+                                //}
+                                */
+
+                                //如果有給classId且是給空的,則也將no設為空,後續才能將no欄位清空。
+                                if (classId != null && classId.Length == 0) no = string.Empty;
+
+                                //classId => 沒給欄位(null) 有給欄位("") 但更新一定得給教室?
+                                if (classStuds.ContainsKey(classId))
                                 {
-                                    //pic,mail,mobile暫不支持更新
-                                    studentInfos.Add(id.GetString(), (salt, pw, name, year, null, gender, null, null, null, null));
+                                    classStuds[classId].Add((id.GetString(), salt, pw, name, year, null, gender, null, null, classId, no));
                                 }
                                 else
                                 {
-                                    if (classStuds.ContainsKey(classId))
-                                    {
-                                        classStuds[classId].Add((id.GetString(), salt, pw, name, year, null, gender, null, null, classId, no));
-                                    }
-                                    else
-                                    {
-                                        classStuds.Add(
-                                            classId,
-                                            new List<(string id, string salt, string pw, string name, string year, string pic, string gender, string mail, string mobile, string classId, string no)>()
-                                            { (id.GetString(), salt, pw, name, year, null, gender, null, null, classId, no) });
-                                    }
-                                    //pic,mail,mobile暫不支持批量更新
-                                    studentInfos.Add(id.GetString(), (salt, pw, name, year, null, gender, null, null, classId, no));
+                                    classStuds.Add(
+                                        classId,
+                                        new List<(string id, string salt, string pw, string name, string year, string pic, string gender, string mail, string mobile, string classId, string no)>()
+                                        { (id.GetString(), salt, pw, name, year, null, gender, null, null, classId, no) });
                                 }
+
+                                //pic,mail,mobile暫不支持批量更新
+                                studentInfos.Add(id.GetString(), (salt, pw, name, year, null, gender, null, null, classId, no));
+
                                 //先將id加進去後面再做刪除動作
                                 nonexistentIds.Add(id.GetString());
                             }
@@ -1782,7 +1793,8 @@ namespace TEAMModelOS.Controllers
                 //準備查詢db資料
                 CosmosContainer cosmosContainer = _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Student");
 
-
+                //查要移除教室或是沒加入教室的學生
+                
                 foreach (var item in classStuds)
                 {
                     //如果沒有任何學生要更新,則跳過該間教室。
@@ -1791,6 +1803,7 @@ namespace TEAMModelOS.Controllers
                     string classId = null, className = null, classNo = null, gradeId = null, periodId = null;
 
                     //如果教室不存在的話(填錯教室之類的狀況),則記錄教室的id及學生id。
+                   
                     if (classInfo.ContainsKey(item.Key))
                     {
                         classId = item.Key;
@@ -1799,6 +1812,9 @@ namespace TEAMModelOS.Controllers
                         gradeId = classInfo[item.Key].GetProperty("gradeId").GetString();
                         periodId = classInfo[item.Key].GetProperty("periodId").GetString();
                     }
+                    else if (item.Key.Length == 0)
+                    {
+                    }
                     else
                     {
                         //沒查到有該間教室的資訊,故將該間教室的ID及學生資料清單記起來,並且跳過不處理該資料。
@@ -1898,7 +1914,14 @@ namespace TEAMModelOS.Controllers
                                                 }
                                                 break;
                                             case bool _ when element.Name.Equals("classId", StringComparison.Ordinal):
-                                                if (string.IsNullOrWhiteSpace(studentInfos[id].classId))
+                                                if (studentInfos[id].classId != null && studentInfos[id].classId.Length == 0)
+                                                {
+                                                    writer.WriteNull("classId");
+                                                    writer.WriteNull("groupId");
+                                                    writer.WriteNull("groupName");
+                                                    tmpData.classId = null;
+                                                }
+                                                else if (string.IsNullOrWhiteSpace(studentInfos[id].classId))
                                                 {
                                                     element.WriteTo(writer);
                                                     tmpData.classId = element.Value.GetString();
@@ -1910,7 +1933,7 @@ namespace TEAMModelOS.Controllers
                                                 break;
                                             case bool _ when element.Name.Equals("no", StringComparison.Ordinal):
                                                 //移除座號的話會給空的
-                                                if (studentInfos[id].no.Length == 0)
+                                                if (studentInfos[id].no != null && studentInfos[id].no.Length == 0)
                                                 {
                                                     writer.WriteNull("no");
                                                     tmpData.no = null;