|
@@ -343,7 +343,7 @@ namespace TEAMModelOS.Controllers
|
|
|
using var memoryStream = new MemoryStream();
|
|
|
using var writer = new Utf8JsonWriter(memoryStream);
|
|
|
writer.WriteStartObject();
|
|
|
- writer.WriteString("pk", "Class");
|
|
|
+ writer.WriteString("pk", "Classroom");
|
|
|
writer.WriteString("code", $"Class-{schoolId}");
|
|
|
writer.WriteString("id", classId);
|
|
|
writer.WriteNull("x");
|
|
@@ -361,6 +361,8 @@ namespace TEAMModelOS.Controllers
|
|
|
else writer.WriteString("name", name);
|
|
|
if (string.IsNullOrWhiteSpace(no)) writer.WriteNull("no");
|
|
|
else writer.WriteString("no", no);
|
|
|
+ writer.WriteNull("groupId");
|
|
|
+ writer.WriteNull("groupName");
|
|
|
writer.WriteEndObject();
|
|
|
ids.Add(id);
|
|
|
}
|
|
@@ -424,7 +426,7 @@ namespace TEAMModelOS.Controllers
|
|
|
{ //TODO : 進階查詢選項調整
|
|
|
//以學校學生角度去抓資料
|
|
|
List<(string id, string name, string pic, string year)> listStudent = new List<(string id, string name, string pic, string year)>();
|
|
|
- string queryText = $"SELECT c.id, c.name, c.picture, c.year FROM c WHERE c.pk = 'Base'";
|
|
|
+ string queryText = $"SELECT c.id, c.name, c.picture, c.year FROM c WHERE c.code = 'Base-{schoolId}'";
|
|
|
|
|
|
//如果有選擇ClassId的話,則先取得該教室內的學生。
|
|
|
List<string> searchId = new List<string>();
|
|
@@ -566,7 +568,7 @@ namespace TEAMModelOS.Controllers
|
|
|
{ //TODO : 進階查詢選項調整
|
|
|
//以學校學生角度去抓資料
|
|
|
List<(string id, string name, string pic, string year)> listStudent = new List<(string id, string name, string pic, string year)>();
|
|
|
- string queryText = $"SELECT c.id, c.name, c.picture, c.year FROM c WHERE c.pk = 'Base'";
|
|
|
+ string queryText = $"SELECT c.id, c.name, c.picture, c.year FROM c WHERE c.code = 'Base-{schoolId}'";
|
|
|
|
|
|
//回傳用ContinuationToken
|
|
|
string continuationToken = string.Empty;
|
|
@@ -735,8 +737,8 @@ namespace TEAMModelOS.Controllers
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
- string queryText = $"SELECT c.id, c.students FROM c WHERE c.pk = 'Class'";
|
|
|
- if (!string.IsNullOrWhiteSpace(classId)) queryText += $" AND c.id = '{classId}'";
|
|
|
+ string queryText = $"SELECT c.id, c.students FROM c WHERE c = c";
|
|
|
+ //if (!string.IsNullOrWhiteSpace(classId)) queryText += $" AND c.id = '{classId}'";
|
|
|
Dictionary<string, JsonElement> listStudent = new Dictionary<string, JsonElement>();
|
|
|
|
|
|
await foreach (Response item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School")
|
|
@@ -941,18 +943,22 @@ namespace TEAMModelOS.Controllers
|
|
|
|
|
|
foreach (var importItem in classStudents)
|
|
|
{
|
|
|
+ List<(string id, string name, string no, string groupId, string groupName)> retStudents
|
|
|
+ = new List<(string id, string name, string no, string groupId, string groupName)>();
|
|
|
+ retStudents.AddRange(importItem.Value.Select(o => (o.id, o.name, o.no, (string)null, (string)null)).ToList());
|
|
|
+
|
|
|
//檢查輸入資料的id及no是否有重複
|
|
|
- var duplicateId = importItem.Value.GroupBy(o => o.id).Where(o => o.Count() > 1).Select(o => o.Key).ToList();
|
|
|
+ var duplicateId = retStudents.GroupBy(o => o.id).Where(o => o.Count() > 1).Select(o => o.Key).ToList();
|
|
|
foreach (var id in duplicateId)
|
|
|
{
|
|
|
- importItem.Value.RemoveAll(o => o.id.Equals(id));
|
|
|
+ retStudents.RemoveAll(o => o.id.Equals(id));
|
|
|
existId.Add(id);
|
|
|
}
|
|
|
|
|
|
- var duplicateNo = importItem.Value.GroupBy(o => o.no).Where(o => o.Count() > 1).Select(o => o.Key).ToList();
|
|
|
+ var duplicateNo = retStudents.GroupBy(o => o.no).Where(o => o.Count() > 1).Select(o => o.Key).ToList();
|
|
|
foreach (var no in duplicateNo)
|
|
|
{
|
|
|
- importItem.Value.RemoveAll(o => o.no.Equals(no));
|
|
|
+ retStudents.RemoveAll(o => o.no.Equals(no));
|
|
|
existNo.Add(no);
|
|
|
}
|
|
|
|
|
@@ -988,7 +994,8 @@ namespace TEAMModelOS.Controllers
|
|
|
if (existStudents.GetArrayLength() != 0)
|
|
|
{
|
|
|
//若學生數量不為0,代表裡面已有資料,故要檢查是否有重複
|
|
|
- List<(string id, string name, string no)> tmpStuds = new List<(string id, string name, string no)>();
|
|
|
+ List<(string id, string name, string no, string groupId, string groupName)> tmpStuds
|
|
|
+ = new List<(string id, string name, string no, string groupId, string groupName)>();
|
|
|
//取得雲端教室的名單,並檢查id及座號是否重複
|
|
|
var studs = existStudents.EnumerateArray();
|
|
|
|
|
@@ -999,8 +1006,10 @@ namespace TEAMModelOS.Controllers
|
|
|
string id = stud.GetProperty("id").GetString();
|
|
|
string no = stud.GetProperty("no").GetString();
|
|
|
string name = stud.GetProperty("name").GetString();
|
|
|
+ string groupId = stud.GetProperty("groupId").GetString();
|
|
|
+ string groupName = stud.GetProperty("groupName").GetString();
|
|
|
|
|
|
- tmpStuds.Add((id, name, no));
|
|
|
+ tmpStuds.Add((id, name, no, groupId, groupName));
|
|
|
}
|
|
|
|
|
|
//先檢查ID及座號是否重複,重複的則記錄在duplicate內
|
|
@@ -1011,27 +1020,26 @@ namespace TEAMModelOS.Controllers
|
|
|
//var duplicate = importItem.Value.Where(
|
|
|
// a => tmpStuds.Exists(t => a.no.Contains(t.no))
|
|
|
// || tmpStuds.Exists(t => a.id.Contains(t.id))).ToList();
|
|
|
- var duplicate = importItem.Value.Where(
|
|
|
+ var duplicate = retStudents.Where(
|
|
|
a => tmpStuds.Any(p => (p.id == a.id) || (p.no == a.no))).Select(o => o);
|
|
|
|
|
|
- foreach (var (id, name, no) in duplicate)
|
|
|
+ foreach (var item in duplicate)
|
|
|
{
|
|
|
- importItem.Value.RemoveAll(o => o.id.Equals(id) || o.no.Equals(no));
|
|
|
+ retStudents.RemoveAll(o => o.id.Equals(item.id) || o.no.Equals(item.no));
|
|
|
}
|
|
|
|
|
|
//將現有資料加回去
|
|
|
- importItem.Value.AddRange(tmpStuds);
|
|
|
+ retStudents.AddRange(tmpStuds);
|
|
|
}
|
|
|
|
|
|
foreach (var testDataElement in existClassData.RootElement.EnumerateObject())
|
|
|
{
|
|
|
if (!testDataElement.Name.Equals("students", StringComparison.Ordinal)) testDataElement.WriteTo(writer);
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
else if (response.Status == 404)
|
|
|
{
|
|
|
- writer.WriteString("pk", "Class");
|
|
|
+ writer.WriteString("pk", "Classroom");
|
|
|
writer.WriteString("code", $"Class-{schoolId}");
|
|
|
writer.WriteString("id", importItem.Key);
|
|
|
writer.WriteNull("x");
|
|
@@ -1057,12 +1065,20 @@ namespace TEAMModelOS.Controllers
|
|
|
writer.WritePropertyName("students");
|
|
|
writer.WriteStartArray();
|
|
|
|
|
|
- foreach (var (id, name, no) in importItem.Value)
|
|
|
+ //進行no排序
|
|
|
+ retStudents = retStudents.OrderBy(o => string.IsNullOrWhiteSpace(o.no)).ThenBy(o => string.IsNullOrEmpty(o.no) ? 0 : int.Parse(o.no)).ToList();
|
|
|
+ foreach (var item in retStudents)
|
|
|
{
|
|
|
writer.WriteStartObject();
|
|
|
- writer.WriteString("id", id);
|
|
|
- writer.WriteString("name", name);
|
|
|
- writer.WriteString("no", no);
|
|
|
+ writer.WriteString("id", item.id);
|
|
|
+ if (string.IsNullOrWhiteSpace(item.name)) writer.WriteNull("name");
|
|
|
+ else writer.WriteString("name", item.name);
|
|
|
+ if (string.IsNullOrWhiteSpace(item.no)) writer.WriteNull("no");
|
|
|
+ else writer.WriteString("no", item.no);
|
|
|
+ if (string.IsNullOrWhiteSpace(item.groupId)) writer.WriteNull("groupId");
|
|
|
+ else writer.WriteString("groupId", item.groupId);
|
|
|
+ if (string.IsNullOrWhiteSpace(item.groupName)) writer.WriteNull("groupName");
|
|
|
+ else writer.WriteString("groupName", item.groupName);
|
|
|
writer.WriteEndObject();
|
|
|
}
|
|
|
|
|
@@ -1076,7 +1092,7 @@ namespace TEAMModelOS.Controllers
|
|
|
.GetContainer("TEAMModelOS", "School")
|
|
|
.UpsertItemStreamAsync(memoryStream, new PartitionKey($"Class-{schoolId}"));
|
|
|
|
|
|
- retClassStud.Add(classId, (periodId, gradeId, importItem.Value.Select(o => o.id).ToList()));
|
|
|
+ retClassStud.Add(classId, (periodId, gradeId, retStudents.Select(o => o.id).ToList()));
|
|
|
}
|
|
|
}
|
|
|
return (retClassStud, existId, existNo);
|
|
@@ -1102,7 +1118,7 @@ namespace TEAMModelOS.Controllers
|
|
|
{
|
|
|
if (!(classIds == null || classIds.Count == 0))
|
|
|
{
|
|
|
- string queryText = $"SELECT * FROM c WHERE c.pk = 'Class' AND c.id IN ({string.Join(",", classIds.Select(o => $"'{o}'"))})";
|
|
|
+ string queryText = $"SELECT * FROM c WHERE c.code = 'Class-{schoolId}' AND c.id IN ({string.Join(",", classIds.Select(o => $"'{o}'"))})";
|
|
|
|
|
|
Dictionary<string, JsonElement> dicClassInfo = new Dictionary<string, JsonElement>();
|
|
|
|
|
@@ -1214,7 +1230,7 @@ namespace TEAMModelOS.Controllers
|
|
|
{
|
|
|
CosmosContainer cosmosContainer = _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Student");
|
|
|
//查學生的基本資料
|
|
|
- string queryText = $"SELECT * FROM c WHERE c.pk = 'Base' AND c.id IN ({string.Join(",", studentsInfos.Select(o => $"'{o.Key}'"))})";
|
|
|
+ string queryText = $"SELECT * FROM c WHERE c.id IN ({string.Join(",", studentsInfos.Select(o => $"'{o.Key}'"))})";
|
|
|
|
|
|
List<JsonElement> listStudent = new List<JsonElement>();
|
|
|
|
|
@@ -1242,9 +1258,6 @@ namespace TEAMModelOS.Controllers
|
|
|
{
|
|
|
switch (true)
|
|
|
{
|
|
|
- //case bool _ when element.Name.Equals("name", StringComparison.Ordinal) && !string.IsNullOrWhiteSpace(studentsInfos[id].name):
|
|
|
- // writer.WriteString("name", studentsInfos[id].name);
|
|
|
- // break;
|
|
|
case bool _ when element.Name.Equals("name", StringComparison.Ordinal):
|
|
|
if (string.IsNullOrWhiteSpace(studentsInfos[id].name))
|
|
|
{
|
|
@@ -1265,9 +1278,6 @@ namespace TEAMModelOS.Controllers
|
|
|
upPwDone = true;
|
|
|
}
|
|
|
break;
|
|
|
- //case bool _ when element.Name.Equals("gender", StringComparison.Ordinal) && !string.IsNullOrWhiteSpace(studentsInfos[id].gender):
|
|
|
- // writer.WriteString("gender", studentsInfos[id].gender);
|
|
|
- // break;
|
|
|
case bool _ when element.Name.Equals("gender", StringComparison.Ordinal):
|
|
|
if (string.IsNullOrWhiteSpace(studentsInfos[id].gender))
|
|
|
{
|
|
@@ -1279,9 +1289,6 @@ namespace TEAMModelOS.Controllers
|
|
|
writer.WriteString("gender", studentsInfos[id].gender);
|
|
|
}
|
|
|
break;
|
|
|
- //case bool _ when element.Name.Equals("year", StringComparison.Ordinal) && !string.IsNullOrWhiteSpace(studentsInfos[id].year):
|
|
|
- // writer.WriteString("year", studentsInfos[id].year);
|
|
|
- // break;
|
|
|
case bool _ when element.Name.Equals("year", StringComparison.Ordinal):
|
|
|
if (string.IsNullOrWhiteSpace(studentsInfos[id].year))
|
|
|
{
|
|
@@ -1362,127 +1369,196 @@ namespace TEAMModelOS.Controllers
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
- //輸出用資料格式
|
|
|
+ //最後輸出給前端用的資料
|
|
|
var retStudentsClassInfo = new Dictionary<string, (string classId, string periodId, string gradeId, string name, string no)>();
|
|
|
+ /**
|
|
|
+ 整理輸入的資料
|
|
|
+ var studentsClassInfo = new Dictionary<string, (string classId, string name, string no)>();
|
|
|
+ while (students.MoveNext())
|
|
|
+ {
|
|
|
+ JsonElement student = students.Current;
|
|
|
+ if (student.TryGetProperty("id", out var tmpId))
|
|
|
+ {
|
|
|
+ if (!string.IsNullOrWhiteSpace(tmpId.GetString()))
|
|
|
+ {
|
|
|
+ string classId = string.Empty, className = string.Empty, no = string.Empty, name = string.Empty;
|
|
|
|
|
|
- //整理輸入的資料
|
|
|
- //var studentsClassInfo = new Dictionary<string, (string classId, string name, string no)>();
|
|
|
- //while (students.MoveNext())
|
|
|
- //{
|
|
|
- // JsonElement student = students.Current;
|
|
|
- // if (student.TryGetProperty("id", out var tmpId))
|
|
|
- // {
|
|
|
- // if (!string.IsNullOrWhiteSpace(tmpId.GetString()))
|
|
|
- // {
|
|
|
- // string classId = string.Empty, className = string.Empty, no = string.Empty, name = string.Empty;
|
|
|
-
|
|
|
- // if (student.TryGetProperty("name", out var tmpName)) name = tmpName.GetString();
|
|
|
- // if (student.TryGetProperty("classId", out var tmpClassId)) classId = tmpClassId.GetString();
|
|
|
- // if (student.TryGetProperty("no", out var tmpNo)) no = tmpNo.GetString();
|
|
|
-
|
|
|
- // studentsClassInfo.Add(tmpId.GetString(), (classId, name, no));
|
|
|
- // }
|
|
|
- // }
|
|
|
- //}
|
|
|
+ if (student.TryGetProperty("name", out var tmpName)) name = tmpName.GetString();
|
|
|
+ if (student.TryGetProperty("classId", out var tmpClassId)) classId = tmpClassId.GetString();
|
|
|
+ if (student.TryGetProperty("no", out var tmpNo)) no = tmpNo.GetString();
|
|
|
+
|
|
|
+ studentsClassInfo.Add(tmpId.GetString(), (classId, name, no));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ */
|
|
|
if (students.Count == 0) return retStudentsClassInfo;
|
|
|
|
|
|
+ var tmpStuds = new Dictionary<string, (string name, string year, string pic, string gender, string mail, string mobile, string classId, string no)>(students);
|
|
|
+
|
|
|
//透過id查找已加入的教室
|
|
|
- var classInfo = await getClassInfoUseStudent(schoolId, students.Select(o => o.Key).ToList());
|
|
|
+ var classInfos = await getClassInfoUseStudent(schoolId, students.Select(o => o.Key).ToList());
|
|
|
|
|
|
//如果有查到,代表該學生已經加入過某間教室(Class)
|
|
|
- if (classInfo.Count != 0)
|
|
|
+ if (classInfos.Count != 0)
|
|
|
{
|
|
|
- foreach (var item in classInfo)
|
|
|
+ foreach (var classInfo in classInfos)
|
|
|
{
|
|
|
//教室資訊的id
|
|
|
- if (item.TryGetProperty("id", out var tmpClassId))
|
|
|
+ if (classInfo.TryGetProperty("id", out var tmpClassId))
|
|
|
{
|
|
|
string classId = tmpClassId.GetString();
|
|
|
string periodId = string.Empty, gradeId = string.Empty;
|
|
|
|
|
|
- if (item.TryGetProperty("periodId", out var tmpPeriodId)) periodId = tmpPeriodId.GetString();
|
|
|
- if (item.TryGetProperty("gradeId", out var tmpGradeId)) gradeId = tmpGradeId.GetString();
|
|
|
-
|
|
|
- using var memoryStream = new MemoryStream();
|
|
|
- using var writer = new Utf8JsonWriter(memoryStream);
|
|
|
- writer.WriteStartObject();
|
|
|
- foreach (var element in item.EnumerateObject())
|
|
|
- {
|
|
|
- if (!(element.Name.Equals("students", StringComparison.Ordinal) || element.Name.StartsWith("_")))
|
|
|
- {
|
|
|
- element.WriteTo(writer);
|
|
|
- }
|
|
|
- }
|
|
|
- writer.WritePropertyName("students");
|
|
|
- writer.WriteStartArray();
|
|
|
-
|
|
|
- //查詢學生欄位
|
|
|
- var Documents = item.GetProperty("students").EnumerateArray();
|
|
|
+ if (classInfo.TryGetProperty("periodId", out var tmpPeriodId)) periodId = tmpPeriodId.GetString();
|
|
|
+ if (classInfo.TryGetProperty("gradeId", out var tmpGradeId)) gradeId = tmpGradeId.GetString();
|
|
|
+
|
|
|
+ //用以紀錄教室內已存在的學生
|
|
|
+ List<(string id, string name, string no, string groupId, string groupName)> existStudents
|
|
|
+ = new List<(string id, string name, string no, string groupId, string groupName)>();
|
|
|
+ var Documents = classInfo.GetProperty("students").EnumerateArray();
|
|
|
while (Documents.MoveNext())
|
|
|
{
|
|
|
JsonElement Document = Documents.Current;
|
|
|
- string studId = string.Empty, no = string.Empty, name = string.Empty;
|
|
|
+ string studId = null, no = null, name = null, groupId = null, groupName = null;
|
|
|
|
|
|
- if (Document.TryGetProperty("name", out var tmpName)) name = tmpName.GetString();
|
|
|
if (Document.TryGetProperty("id", out var tmpId)) studId = tmpId.GetString();
|
|
|
+ if (Document.TryGetProperty("name", out var tmpName)) name = tmpName.GetString();
|
|
|
if (Document.TryGetProperty("no", out var tmpNo)) no = tmpNo.GetString();
|
|
|
+ if (Document.TryGetProperty("groupId", out var tmpgroupId)) groupId = tmpgroupId.GetString();
|
|
|
+ if (Document.TryGetProperty("groupName", out var tmpgroupName)) groupName = tmpgroupName.GetString();
|
|
|
|
|
|
//檢查輸入資料內學生要加入的教室是否一致
|
|
|
if (students.ContainsKey(studId))
|
|
|
{
|
|
|
- //如果是相同的教室id
|
|
|
if (students[studId].classId.Equals(classId, StringComparison.Ordinal))
|
|
|
{
|
|
|
- retStudentsClassInfo.Add(studId, (classId, periodId, gradeId, name, no));
|
|
|
-
|
|
|
- //座號及姓名檢查,如果不相同則進行更新
|
|
|
+ //座號及姓名檢查,如果現有資料與欲更新資料不同則進行更新
|
|
|
if (
|
|
|
- students[studId].no.Equals(no, StringComparison.Ordinal)
|
|
|
- && students[studId].name.Equals(name, StringComparison.Ordinal)
|
|
|
+ !(students[studId].no.Equals(no, StringComparison.Ordinal)
|
|
|
+ && students[studId].name.Equals(name, StringComparison.Ordinal))
|
|
|
)
|
|
|
{
|
|
|
- writer.WriteStartObject();
|
|
|
- writer.WriteString("id", studId);
|
|
|
- if (string.IsNullOrWhiteSpace(name)) writer.WriteNull("name");
|
|
|
- else writer.WriteString("name", name);
|
|
|
- if (string.IsNullOrWhiteSpace(no)) writer.WriteNull("no");
|
|
|
- else writer.WriteString("no", no);
|
|
|
- writer.WriteEndObject();
|
|
|
+ name = students[studId].name;
|
|
|
+ no = students[studId].no;
|
|
|
+ //retStudentsClassInfo.Add(studId, (classId, periodId, gradeId, name, no));
|
|
|
}
|
|
|
- else
|
|
|
- {
|
|
|
- writer.WriteStartObject();
|
|
|
- writer.WriteString("id", studId);
|
|
|
- if (string.IsNullOrWhiteSpace(students[studId].name)) writer.WriteNull("name");
|
|
|
- else writer.WriteString("name", students[studId].name);
|
|
|
- if (string.IsNullOrWhiteSpace(students[studId].no)) writer.WriteNull("no");
|
|
|
- else writer.WriteString("no", students[studId].no);
|
|
|
- writer.WriteEndObject();
|
|
|
- //更新輸出結果的資料
|
|
|
- retStudentsClassInfo[studId] = (classId, periodId, gradeId, students[studId].name, students[studId].no);
|
|
|
- }
|
|
|
- //將已處理好的學生從字典裡移除
|
|
|
- students.Remove(studId);
|
|
|
+ tmpStuds.Remove(studId);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //retStudentsClassInfo.Add(studId, (students[studId].classId, null, null, name, no));
|
|
|
+ tmpStuds[studId] = (tmpStuds[studId].name, tmpStuds[studId].year, tmpStuds[studId].pic, tmpStuds[studId].gender, tmpStuds[studId].mail, tmpStuds[studId].mobile, tmpStuds[studId].classId, no);
|
|
|
+ continue;
|
|
|
}
|
|
|
- //不是相同教室id,則要移除該學生的資訊,不寫入
|
|
|
- else continue;
|
|
|
}
|
|
|
- //寫入原本已存在的學生資訊
|
|
|
- else
|
|
|
+ retStudentsClassInfo.Add(studId, (classId, periodId, gradeId, name, no));
|
|
|
+ existStudents.Add((studId, name, no, groupId, groupName));
|
|
|
+ }
|
|
|
+ existStudents = existStudents.OrderBy(o => string.IsNullOrWhiteSpace(o.no)).ThenBy(o => string.IsNullOrEmpty(o.no) ? 0 : int.Parse(o.no)).ToList();
|
|
|
+
|
|
|
+ using var memoryStream = new MemoryStream();
|
|
|
+ using var writer = new Utf8JsonWriter(memoryStream);
|
|
|
+
|
|
|
+ writer.WriteStartObject();
|
|
|
+ //將非students的欄位資料寫回去
|
|
|
+ foreach (var element in classInfo.EnumerateObject())
|
|
|
+ {
|
|
|
+ if (!(element.Name.Equals("students", StringComparison.Ordinal) || element.Name.StartsWith("_")))
|
|
|
{
|
|
|
- writer.WriteStartObject();
|
|
|
- writer.WriteString("id", studId);
|
|
|
- if (string.IsNullOrWhiteSpace(name)) writer.WriteNull("name");
|
|
|
- else writer.WriteString("name", name);
|
|
|
- if (string.IsNullOrWhiteSpace(no)) writer.WriteNull("no");
|
|
|
- else writer.WriteString("no", no);
|
|
|
- writer.WriteEndObject();
|
|
|
+ element.WriteTo(writer);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ writer.WritePropertyName("students");
|
|
|
+ writer.WriteStartArray();
|
|
|
+
|
|
|
+ foreach (var item in existStudents)
|
|
|
+ {
|
|
|
+ writer.WriteStartObject();
|
|
|
+ writer.WriteString("id", item.id);
|
|
|
+ if (string.IsNullOrWhiteSpace(item.name)) writer.WriteNull("name");
|
|
|
+ else writer.WriteString("name", item.name);
|
|
|
+ if (string.IsNullOrWhiteSpace(item.no)) writer.WriteNull("no");
|
|
|
+ else writer.WriteString("no", item.no);
|
|
|
+ if (string.IsNullOrWhiteSpace(item.groupId)) writer.WriteNull("groupId");
|
|
|
+ else writer.WriteString("groupId", item.groupId);
|
|
|
+ if (string.IsNullOrWhiteSpace(item.groupName)) writer.WriteNull("groupName");
|
|
|
+ else writer.WriteString("groupName", item.groupName);
|
|
|
+ writer.WriteEndObject();
|
|
|
+ }
|
|
|
+
|
|
|
writer.WriteEndArray();
|
|
|
writer.WriteEndObject();
|
|
|
writer.Flush();
|
|
|
|
|
|
+ /**
|
|
|
+ //while (Documents.MoveNext())
|
|
|
+ //{
|
|
|
+ // JsonElement Document = Documents.Current;
|
|
|
+ // string studId = string.Empty, no = string.Empty, name = string.Empty;
|
|
|
+
|
|
|
+ // if (Document.TryGetProperty("name", out var tmpName)) name = tmpName.GetString();
|
|
|
+ // if (Document.TryGetProperty("id", out var tmpId)) studId = tmpId.GetString();
|
|
|
+ // if (Document.TryGetProperty("no", out var tmpNo)) no = tmpNo.GetString();
|
|
|
+
|
|
|
+ // //檢查輸入資料內學生要加入的教室是否一致
|
|
|
+ // if (students.ContainsKey(studId))
|
|
|
+ // {
|
|
|
+ // //如果是相同的教室id
|
|
|
+ // if (students[studId].classId.Equals(classId, StringComparison.Ordinal))
|
|
|
+ // {
|
|
|
+ // retStudentsClassInfo.Add(studId, (classId, periodId, gradeId, name, no));
|
|
|
+
|
|
|
+ // //座號及姓名檢查,如果不相同則進行更新
|
|
|
+ // if (
|
|
|
+ // students[studId].no.Equals(no, StringComparison.Ordinal)
|
|
|
+ // && students[studId].name.Equals(name, StringComparison.Ordinal)
|
|
|
+ // )
|
|
|
+ // {
|
|
|
+ // writer.WriteStartObject();
|
|
|
+ // writer.WriteString("id", studId);
|
|
|
+ // if (string.IsNullOrWhiteSpace(name)) writer.WriteNull("name");
|
|
|
+ // else writer.WriteString("name", name);
|
|
|
+ // if (string.IsNullOrWhiteSpace(no)) writer.WriteNull("no");
|
|
|
+ // else writer.WriteString("no", no);
|
|
|
+ // writer.WriteEndObject();
|
|
|
+ // }
|
|
|
+ // else
|
|
|
+ // {
|
|
|
+ // writer.WriteStartObject();
|
|
|
+ // writer.WriteString("id", studId);
|
|
|
+ // if (string.IsNullOrWhiteSpace(students[studId].name)) writer.WriteNull("name");
|
|
|
+ // else writer.WriteString("name", students[studId].name);
|
|
|
+ // if (string.IsNullOrWhiteSpace(students[studId].no)) writer.WriteNull("no");
|
|
|
+ // else writer.WriteString("no", students[studId].no);
|
|
|
+ // writer.WriteEndObject();
|
|
|
+ // //更新輸出結果的資料
|
|
|
+ // retStudentsClassInfo[studId] = (classId, periodId, gradeId, students[studId].name, students[studId].no);
|
|
|
+ // }
|
|
|
+ // //將已處理好的學生從字典裡移除
|
|
|
+ // students.Remove(studId);
|
|
|
+ // }
|
|
|
+ // //不是相同教室id,則要移除該學生的資訊,不寫入
|
|
|
+ // else continue;
|
|
|
+ // }
|
|
|
+ // //寫入原本已存在的學生資訊
|
|
|
+ // else
|
|
|
+ // {
|
|
|
+ // writer.WriteStartObject();
|
|
|
+ // writer.WriteString("id", studId);
|
|
|
+ // if (string.IsNullOrWhiteSpace(name)) writer.WriteNull("name");
|
|
|
+ // else writer.WriteString("name", name);
|
|
|
+ // if (string.IsNullOrWhiteSpace(no)) writer.WriteNull("no");
|
|
|
+ // else writer.WriteString("no", no);
|
|
|
+ // writer.WriteEndObject();
|
|
|
+ // }
|
|
|
+ //}
|
|
|
+ //writer.WriteEndArray();
|
|
|
+ //writer.WriteEndObject();
|
|
|
+ //writer.Flush();
|
|
|
+ */
|
|
|
+
|
|
|
try
|
|
|
{
|
|
|
var ret = await _azureCosmos
|
|
@@ -1502,26 +1578,55 @@ namespace TEAMModelOS.Controllers
|
|
|
}
|
|
|
}
|
|
|
//始終會加入新教室,除非只是單純的換座號或是姓名
|
|
|
- if (classInfo.Count == 0 || students.Count != 0) //透過學生id查找教室,但沒找到任何已加入的教室
|
|
|
+ if (classInfos.Count == 0 || tmpStuds.Count != 0) //透過學生id查找教室,但沒找到任何已加入的教室
|
|
|
{
|
|
|
//使用classId來查詢教室資訊
|
|
|
- var classInfos = await getClassInfoUseId(schoolId, students.Select(o => o.Value.classId).ToList());
|
|
|
- if (classInfos.Count != 0)
|
|
|
+ var dicClassInfo = await getClassInfoUseId(schoolId, tmpStuds.Select(o => o.Value.classId).ToList());
|
|
|
+ if (dicClassInfo.Count != 0)
|
|
|
{
|
|
|
- foreach (var item in classInfos)
|
|
|
+ foreach (var classInfo in dicClassInfo)
|
|
|
{
|
|
|
- string classId = item.Key;
|
|
|
+ string classId = classInfo.Key;
|
|
|
string periodId = string.Empty, gradeId = string.Empty;
|
|
|
|
|
|
- if (item.Value.TryGetProperty("periodId", out var tmpPeriodId)) periodId = tmpPeriodId.GetString();
|
|
|
- if (item.Value.TryGetProperty("gradeId", out var tmpGradeId)) gradeId = tmpGradeId.GetString();
|
|
|
+ if (classInfo.Value.TryGetProperty("periodId", out var tmpPeriodId)) periodId = tmpPeriodId.GetString();
|
|
|
+ if (classInfo.Value.TryGetProperty("gradeId", out var tmpGradeId)) gradeId = tmpGradeId.GetString();
|
|
|
+
|
|
|
+ //用以紀錄教室內已存在的學生
|
|
|
+ List<(string id, string name, string no, string groupId, string groupName)> existStudents
|
|
|
+ = new List<(string id, string name, string no, string groupId, string groupName)>();
|
|
|
+ var Documents = classInfo.Value.GetProperty("students").EnumerateArray();
|
|
|
+ while (Documents.MoveNext())
|
|
|
+ {
|
|
|
+ JsonElement Document = Documents.Current;
|
|
|
+ string studId = null, no = null, name = null, groupId = null, groupName = null;
|
|
|
+
|
|
|
+ if (Document.TryGetProperty("id", out var tmpId)) studId = tmpId.GetString();
|
|
|
+ if (Document.TryGetProperty("name", out var tmpName)) name = tmpName.GetString();
|
|
|
+ if (Document.TryGetProperty("no", out var tmpNo)) no = tmpNo.GetString();
|
|
|
+ if (Document.TryGetProperty("groupId", out var tmpgroupId)) groupId = tmpgroupId.GetString();
|
|
|
+ if (Document.TryGetProperty("groupName", out var tmpgroupName)) groupName = tmpgroupName.GetString();
|
|
|
+ existStudents.Add((studId, name, no, groupId, groupName));
|
|
|
+ }
|
|
|
+
|
|
|
+ //如果classId相同,則將該學生加入現有的學生清單內。
|
|
|
+ foreach (var item in tmpStuds)
|
|
|
+ {
|
|
|
+ if (item.Value.classId.Equals(classId, StringComparison.Ordinal))
|
|
|
+ {
|
|
|
+ existStudents.Add((item.Key, item.Value.name, item.Value.no, null, null));
|
|
|
+ retStudentsClassInfo.Add(item.Key, (classId, periodId, gradeId, item.Value.name, item.Value.no));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ existStudents = existStudents.OrderBy(o => string.IsNullOrWhiteSpace(o.no)).ThenBy(o => string.IsNullOrEmpty(o.no) ? 0 : int.Parse(o.no)).ToList();
|
|
|
+
|
|
|
|
|
|
#region 組JSON
|
|
|
using var memoryStream = new MemoryStream();
|
|
|
using var writer = new Utf8JsonWriter(memoryStream);
|
|
|
writer.WriteStartObject();
|
|
|
//寫入除了students及開頭為"_"的數據資料
|
|
|
- foreach (var element in item.Value.EnumerateObject())
|
|
|
+ foreach (var element in classInfo.Value.EnumerateObject())
|
|
|
{
|
|
|
if (!(element.Name.Equals("students", StringComparison.Ordinal) || element.Name.StartsWith("_")))
|
|
|
{
|
|
@@ -1531,7 +1636,23 @@ namespace TEAMModelOS.Controllers
|
|
|
writer.WritePropertyName("students");
|
|
|
writer.WriteStartArray();
|
|
|
|
|
|
- //將原本已存在的學生寫回去
|
|
|
+ foreach (var item in existStudents)
|
|
|
+ {
|
|
|
+ writer.WriteStartObject();
|
|
|
+ writer.WriteString("id", item.id);
|
|
|
+ if (string.IsNullOrWhiteSpace(item.name)) writer.WriteNull("name");
|
|
|
+ else writer.WriteString("name", item.name);
|
|
|
+ if (string.IsNullOrWhiteSpace(item.no)) writer.WriteNull("no");
|
|
|
+ else writer.WriteString("no", item.no);
|
|
|
+ if (string.IsNullOrWhiteSpace(item.groupId)) writer.WriteNull("groupId");
|
|
|
+ else writer.WriteString("groupId", item.groupId);
|
|
|
+ if (string.IsNullOrWhiteSpace(item.groupName)) writer.WriteNull("groupName");
|
|
|
+ else writer.WriteString("groupName", item.groupName);
|
|
|
+ writer.WriteEndObject();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ 將原本已存在的學生寫回去
|
|
|
var Documents = item.Value.GetProperty("students").EnumerateArray();
|
|
|
while (Documents.MoveNext())
|
|
|
{
|
|
@@ -1551,7 +1672,7 @@ namespace TEAMModelOS.Controllers
|
|
|
writer.WriteEndObject();
|
|
|
}
|
|
|
|
|
|
- //將欲加入的學生寫入該教室名單內
|
|
|
+ 將欲加入的學生寫入該教室名單內
|
|
|
var studList = students
|
|
|
.Where(o => o.Value.classId.Equals(classId, StringComparison.Ordinal))
|
|
|
.Select(o => new { id = o.Key, o.Value.name, o.Value.no }).ToList();
|
|
@@ -1567,6 +1688,7 @@ namespace TEAMModelOS.Controllers
|
|
|
|
|
|
retStudentsClassInfo.Add(stud.id, (classId, periodId, gradeId, stud.name, stud.no));
|
|
|
}
|
|
|
+ */
|
|
|
|
|
|
writer.WriteEndArray();
|
|
|
writer.WriteEndObject();
|
|
@@ -1688,7 +1810,7 @@ namespace TEAMModelOS.Controllers
|
|
|
|
|
|
CosmosContainer cosmosContainer = _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Student");
|
|
|
//查學生的基本資料
|
|
|
- string queryText = $"SELECT * FROM c WHERE c.pk = 'Base' AND c.id IN ({string.Join(",", studentsInfo.Select(o => $"'{o.Key}'"))})";
|
|
|
+ string queryText = $"SELECT * FROM c WHERE c.code = 'Base-{schoolId}' AND c.id IN ({string.Join(",", studentsInfo.Select(o => $"'{o.Key}'"))})";
|
|
|
|
|
|
List<JsonElement> listStudent = new List<JsonElement>();
|
|
|
|