CrazyIter_Bin 3 éve
szülő
commit
f716dddf26
1 módosított fájl, 59 hozzáadás és 13 törlés
  1. 59 13
      TEAMModelOS/Controllers/School/StudentController.cs

+ 59 - 13
TEAMModelOS/Controllers/School/StudentController.cs

@@ -112,8 +112,40 @@ namespace TEAMModelOS.Controllers
                     case "remove":
                         //將學生基本資料內的classId、no、groupId及groupName寫入null
                         (List<string> studs, List<string> nonexistentIds, List<string> errorIds) retRemove = await removeStudentClassInfo(schoolId.GetString(), request.GetProperty("students").EnumerateArray());
-
                         return Ok(new { code = $"Base-{schoolId.GetString()}", ids = retRemove.studs, retRemove.nonexistentIds, retRemove.errorIds });
+                    case "avatar":
+                        if (request.TryGetProperty("avatar", out JsonElement _avatar) && _avatar.ValueKind.Equals(JsonValueKind.Array))
+                        {
+                            List<Avatar> avatars = _avatar.ToObject<List<Avatar>>();
+                            if (avatars.IsNotEmpty())
+                            {
+                                List<Student> studentsp = new List<Student>();
+                                string insql = string.Join(',', avatars.Select(x => $"'{x.studentId}'"));
+                                string sql = $"select value(c) from  c where c.id in ({insql}) ";
+                                await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Student")
+                                    .GetItemQueryIterator<Student>(sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Base-{schoolId}") }))
+                                {
+                                    studentsp.Add(item);
+                                }
+                                (string url, string sas) = _azureStorage.GetBlobContainerSAS99Year($"{schoolId}", BlobContainerSasPermissions.Read);
+                                foreach (Student student in studentsp)
+                                {
+                                    Avatar avatar = avatars.Find(x => x.studentId.Equals(student.id));
+                                    student.picture = avatar != null ? $"{avatar.picture}?{sas}" : null;
+                                    await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Student").ReplaceItemAsync<Student>(student, student.id, new PartitionKey(student.code));
+                                }
+                                return Ok(new { students = studentsp.Select(x => new { x.id, x.picture, x.code, x.name }) });
+                            }
+                            else
+                            {
+                                return BadRequest();
+                            }
+                        }
+                        else
+                        {
+                            return BadRequest();
+                        }
+
                     default:
                         return BadRequest();
                 }
@@ -125,7 +157,11 @@ namespace TEAMModelOS.Controllers
             }
             return BadRequest();
         }
-
+        public record Avatar
+        {
+            public string studentId { get; set; }
+            public string picture { get; set; }
+        }
         /// <summary>
         /// 整理前端匯入的學生資訊
         /// </summary>
@@ -1023,8 +1059,10 @@ namespace TEAMModelOS.Controllers
                         {
                             teStuLists.Add(item);
                         }
-                        if (scStuLists.Count > 0) {
-                            foreach (StuList stuList in scStuLists) {
+                        if (scStuLists.Count > 0)
+                        {
+                            foreach (StuList stuList in scStuLists)
+                            {
                                 for (int j = 0; j < stuList.students.Count; j++)
                                 {
                                     if (id.Equals(stuList.students[j].id))
@@ -1036,7 +1074,8 @@ namespace TEAMModelOS.Controllers
                                 await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync(stuList, stuList.id, new PartitionKey(stuList.code));
                             }
                         }
-                        if (teStuLists.Count > 0) {
+                        if (teStuLists.Count > 0)
+                        {
                             foreach (StuList stuList in teStuLists)
                             {
                                 for (int j = 0; j < stuList.students.Count; j++)
@@ -1239,6 +1278,8 @@ namespace TEAMModelOS.Controllers
                        .GetItemQueryIterator<Class>(queryText: queryText, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Class-{schoolId}") }))
                         {
                             dicClassInfo[item.id] = item;
+                            item.name = classNos[key].className;
+                            await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<Class>(item, item.id, new PartitionKey(item.code));
                         }
                     }
                     return dicClassInfo;
@@ -2013,7 +2054,7 @@ namespace TEAMModelOS.Controllers
                         tmdids.ForEach(x => { inids.Add($"'{x}'"); });
                         var insql = string.Join(",", inids);
                         var queryslt = $"SELECT c.id,c.name,c.picture FROM c where c.id in ({insql})";
-                        await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<TmdInfo>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
+                        await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<TmdInfo>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
                         {
                             tmdinfos.Add(item);
                         }
@@ -2126,17 +2167,21 @@ namespace TEAMModelOS.Controllers
                         userType = usertype.GetString();
                     }
                 }
-                if (string.IsNullOrEmpty(school)) {
-                    if (userType.Equals("tmdid")) {
+                if (string.IsNullOrEmpty(school))
+                {
+                    if (userType.Equals("tmdid"))
+                    {
                         Teacher teacher = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<Teacher>(id, new PartitionKey("Base"));
-                        if (teacher.schools.IsNotEmpty()) {
-                           var tech= teacher.schools.Find(x => x.status.Equals("join"));
+                        if (teacher.schools.IsNotEmpty())
+                        {
+                            var tech = teacher.schools.Find(x => x.status.Equals("join"));
                             if (tech == null)
                             {
                                 school = teacher.schools[0].schoolId;
                             }
-                            else { 
-                                school = tech.schoolId; 
+                            else
+                            {
+                                school = tech.schoolId;
                             }
                         }
                     }
@@ -2171,7 +2216,8 @@ namespace TEAMModelOS.Controllers
 
                     return Ok(new { school_base, school_classes, school_rooms, status = 200 });
                 }
-                else {
+                else
+                {
                     return Ok(new { status = 404 }); ;
                 }
             }