|
@@ -225,6 +225,12 @@ namespace TEAMModelOS.Controllers
|
|
|
return (dicStuds, dicClassInfo, dicClassStudNo, errorYear, duplId);
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 更新或是新增學生
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="schoolId"></param>
|
|
|
+ /// <param name="students"></param>
|
|
|
+ /// <returns></returns>
|
|
|
private async Task<(List<object> studs, Dictionary<string ,List<string>> classDuplNos, List<string> errorIds)> upsertStudents(
|
|
|
string schoolId,
|
|
|
JsonElement.ArrayEnumerator students)
|
|
@@ -254,7 +260,6 @@ namespace TEAMModelOS.Controllers
|
|
|
{
|
|
|
//(id,no)
|
|
|
var studNo = await checkStudNo(schoolId, classInfo.Value.GetProperty("id").GetString());
|
|
|
- //todo:檢查這部分有無問題
|
|
|
classStudNos.Add(classInfo.Key, studNo);
|
|
|
}));
|
|
|
}
|
|
@@ -312,7 +317,6 @@ namespace TEAMModelOS.Controllers
|
|
|
if (!string.IsNullOrWhiteSpace(existNoInfo.id))
|
|
|
{
|
|
|
isContinue = true;
|
|
|
- //TODO:同ID同NO下的檢查會有問題
|
|
|
//輪巡所有匯入的學生資料,並檢查匯入的座號。
|
|
|
sortedImpData.classStudNo[stud.Value.classNo].ForEach(
|
|
|
o =>
|
|
@@ -625,7 +629,7 @@ namespace TEAMModelOS.Controllers
|
|
|
private async Task<(List<object> students, string continuationToken)> getStudents(string schoolId, string byNameOrId = null, string byPeriod = null, string byGrade = null, string byClassId = null, int offset = -1, int limit = -1, string token = default)
|
|
|
{
|
|
|
try
|
|
|
- { //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.code = 'Base-{schoolId}'";
|
|
@@ -652,11 +656,6 @@ namespace TEAMModelOS.Controllers
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- //if (!string.IsNullOrWhiteSpace(byYear))
|
|
|
- //{
|
|
|
- // queryText = $"{queryText} AND c.year = '{byYear}'";
|
|
|
- //}
|
|
|
-
|
|
|
//檢查是否有接續token及是否要在sql語法內多增加offset及limit
|
|
|
if (string.IsNullOrWhiteSpace(token))
|
|
|
{
|
|
@@ -1096,424 +1095,6 @@ namespace TEAMModelOS.Controllers
|
|
|
return (sucStuds, impStuds, errorIds);
|
|
|
}
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// 將學生從教室內移除
|
|
|
- /// </summary>
|
|
|
- /// <param name="schoolId"></param>
|
|
|
- /// <param name="students"></param>
|
|
|
- private async Task removeStudentFromClass(string schoolId, JsonElement.ArrayEnumerator students)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- Dictionary<string, List<string>> classStudent = new Dictionary<string, List<string>>();
|
|
|
- List<string> studs = new List<string>();
|
|
|
- //整理教室學生資訊
|
|
|
- while (students.MoveNext())
|
|
|
- {
|
|
|
- JsonElement student = students.Current;
|
|
|
- string id, classId = string.Empty;
|
|
|
- if (student.TryGetProperty("id", out var tmpId) && !string.IsNullOrWhiteSpace(tmpId.GetString()))
|
|
|
- {
|
|
|
- id = tmpId.GetString();
|
|
|
- studs.Add(id);
|
|
|
- }
|
|
|
- else continue;
|
|
|
-
|
|
|
- if (student.TryGetProperty("classId", out var tmpClassId) && !string.IsNullOrWhiteSpace(tmpClassId.GetString()))
|
|
|
- {
|
|
|
- classId = tmpClassId.GetString();
|
|
|
- if (!string.IsNullOrWhiteSpace(classId) && classStudent.ContainsKey(classId)) classStudent[classId].Add(id);
|
|
|
- else classStudent.Add(classId, new List<string>() { id });
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (studs.Count != 0)
|
|
|
- {
|
|
|
- //使用子查詢來查詢students欄位裡面是否有相符的學生
|
|
|
- var queryText = $"SELECT VALUE c FROM c JOIN (SELECT VALUE t FROM t IN c.students WHERE t.id IN ({string.Join(",", studs.Select(o => $"'{o}'"))}))";
|
|
|
- await foreach (Response item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School")
|
|
|
- .GetItemQueryStreamIterator(
|
|
|
- queryText: queryText,
|
|
|
- requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Class-{schoolId}") }))
|
|
|
- {
|
|
|
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
|
|
|
- {
|
|
|
- JsonElement.ArrayEnumerator docs = json.RootElement.GetProperty("Documents").EnumerateArray();
|
|
|
- while (docs.MoveNext())
|
|
|
- {
|
|
|
- JsonElement doc = docs.Current;
|
|
|
-
|
|
|
- doc.TryGetProperty("id", out var tmpId);
|
|
|
- var id = tmpId.GetString();
|
|
|
-
|
|
|
- using var memoryStream = new MemoryStream();
|
|
|
- using var writer = new Utf8JsonWriter(memoryStream);
|
|
|
- writer.WriteStartObject();
|
|
|
- foreach (var element in doc.EnumerateObject())
|
|
|
- {
|
|
|
- if (element.Name.Equals("students", StringComparison.Ordinal))
|
|
|
- {
|
|
|
- writer.WritePropertyName("students");
|
|
|
- writer.WriteStartArray();
|
|
|
- var s = element.Value.EnumerateArray();
|
|
|
- while (s.MoveNext())
|
|
|
- {
|
|
|
- JsonElement stud = s.Current;
|
|
|
- string sutdId = stud.GetProperty("id").GetString();
|
|
|
- if (!studs.Contains(sutdId))
|
|
|
- {
|
|
|
- string name = stud.GetProperty("name").GetString();
|
|
|
- string no = stud.GetProperty("no").GetString();
|
|
|
-
|
|
|
- writer.WriteStartObject();
|
|
|
- writer.WriteString("id", sutdId);
|
|
|
- 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();
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- element.WriteTo(writer);
|
|
|
- }
|
|
|
- }
|
|
|
- writer.WriteEndObject();
|
|
|
- writer.Flush();
|
|
|
-
|
|
|
- var ret = await _azureCosmos
|
|
|
- .GetCosmosClient()
|
|
|
- .GetContainer("TEAMModelOS", "School")
|
|
|
- .ReplaceItemStreamAsync(memoryStream, id, new PartitionKey($"Class-{schoolId}"));
|
|
|
- if (ret.Status != (int)HttpStatusCode.OK)
|
|
|
- {
|
|
|
- await _dingDing.SendBotMsg(
|
|
|
- $"IES5,{_option.Location},StudentController/removeStudentFromClass()\nClass-{schoolId},{string.Join(",", studs.Select(o => $"'{o}'"))}",
|
|
|
- GroupNames.醍摩豆服務運維群組);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- catch (CosmosException ex)
|
|
|
- {
|
|
|
- await _dingDing.SendBotMsg($"IES5,{_option.Location},StudentController/removeStudentFromClass()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
- }
|
|
|
- catch (Exception ex)
|
|
|
- {
|
|
|
- await _dingDing.SendBotMsg($"IES5,{_option.Location},StudentController/removeStudentFromClass()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- //TODO:檢查學號是否重複,及處理存到CosmosDB錯誤的處理
|
|
|
- /// <summary>
|
|
|
- /// 將學生加入教室,並且會比對id及座號是否有重複,查找教室時該方法是透過classNo進行查詢而非classId。
|
|
|
- /// </summary>
|
|
|
- /// <returns></returns>
|
|
|
- private async Task<
|
|
|
- (Dictionary<string, (string classNo, string periodId, string gradeId, List<string> id)> ret,
|
|
|
- Dictionary<string, List<string>> dicExistId,
|
|
|
- Dictionary<string, List<string>> dicExistNo,
|
|
|
- Dictionary<string, List<string>> dicDuplNo)>
|
|
|
- JoinOrCreateClass(string schoolId, Dictionary<string, List<(string id, string name, string no)>> classStudents, Dictionary<string, string> classInfos)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- //key:教室no value:該間教室錯誤的資訊
|
|
|
- Dictionary<string, List<string>> dicExistId = new Dictionary<string, List<string>>();
|
|
|
- Dictionary<string, List<string>> dicExistNo = new Dictionary<string, List<string>>();
|
|
|
- Dictionary<string, List<string>> dicDuplNo = new Dictionary<string, List<string>>();
|
|
|
-
|
|
|
- //key:classNo value:學生資訊
|
|
|
- Dictionary<string, (string classId, string periodId, string gradeId, List<string> id)> retClassStud = new Dictionary<string, (string classNo, string periodId, string gradeId, List<string> id)>();
|
|
|
-
|
|
|
- foreach (var importItem in classStudents)
|
|
|
- {
|
|
|
- List<string> duplIds = new List<string>();
|
|
|
- List<string> duplNos = new List<string>();
|
|
|
-
|
|
|
- //整理輸入的學生資料
|
|
|
- List<(string id, string name, string no, string groupId, string groupName)> impStuds
|
|
|
- = new List<(string id, string name, string no, string groupId, string groupName)>();
|
|
|
- impStuds.AddRange(importItem.Value.Select(o => (o.id, o.name, o.no, (string)null, (string)null)).ToList());
|
|
|
-
|
|
|
- //檢查輸入資料的id及no是否有重複,並且將該資料加入字典內以利後面輸出給前端用。
|
|
|
- var duplicateId = impStuds.GroupBy(o => o.id).Where(o => o.Count() > 1).Select(o => o.Key).ToList();
|
|
|
- foreach (var id in duplicateId)
|
|
|
- {
|
|
|
- impStuds.RemoveAll(o => o.id.Equals(id));
|
|
|
- duplIds.Add(id);
|
|
|
- }
|
|
|
- dicExistId.Add(importItem.Key, duplIds);
|
|
|
-
|
|
|
- var duplicateNo = impStuds.GroupBy(o => o.no).Where(o => o.Count() > 1).Select(o => o.Key).ToList();
|
|
|
- foreach (var no in duplicateNo)
|
|
|
- {
|
|
|
- impStuds.RemoveAll(o => o.no.Equals(no));
|
|
|
- duplNos.Add(no);
|
|
|
- }
|
|
|
- dicExistNo.Add(importItem.Key, duplNos);
|
|
|
-
|
|
|
- var tmpImpStuds = new List<(string id, string name, string no, string groupId, string groupName)>();
|
|
|
- tmpImpStuds.AddRange(impStuds);
|
|
|
-
|
|
|
- var queryText = $"SELECT VALUE c FROM c WHERE c.no = '{importItem.Key}'";
|
|
|
- await foreach (Response response in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School")
|
|
|
- .GetItemQueryStreamIterator(
|
|
|
- queryText: queryText,
|
|
|
- requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Class-{schoolId}") }))
|
|
|
- {
|
|
|
- string classId = string.Empty, classNo = string.Empty, periodId = string.Empty, gradeId = string.Empty;
|
|
|
-
|
|
|
- //組出新的JSON
|
|
|
- using var memoryStream = new MemoryStream();
|
|
|
- using var writer = new Utf8JsonWriter(memoryStream);
|
|
|
- writer.WriteStartObject();
|
|
|
-
|
|
|
- //獲取教室資料,透過classNo。
|
|
|
- using var json = await JsonDocument.ParseAsync(response.ContentStream);
|
|
|
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
|
|
|
- {
|
|
|
- JsonElement.ArrayEnumerator docs = json.RootElement.GetProperty("Documents").EnumerateArray();
|
|
|
- while (docs.MoveNext())
|
|
|
- {
|
|
|
- JsonElement existClassData = docs.Current;
|
|
|
-
|
|
|
- existClassData.TryGetProperty("id", out var tmpId);
|
|
|
- classId = tmpId.GetString();
|
|
|
- existClassData.TryGetProperty("no", out var tmpNo);
|
|
|
- classNo = tmpNo.GetString();
|
|
|
- existClassData.TryGetProperty("periodId", out var tmpPeriodId);
|
|
|
- periodId = tmpPeriodId.GetString();
|
|
|
- existClassData.TryGetProperty("gradeId", out var tmpGradeId);
|
|
|
- gradeId = tmpGradeId.GetString();
|
|
|
-
|
|
|
- //取出目前該教室的學生清單,若存在則檢查匯入的學生是否已存在,反之則創建新的教室資訊
|
|
|
- existClassData.TryGetProperty("students", out var existStudents);
|
|
|
-
|
|
|
- if (existStudents.GetArrayLength() != 0)
|
|
|
- {
|
|
|
- //若學生數量不為0,代表裡面已有資料,故要檢查是否有重複
|
|
|
- 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();
|
|
|
-
|
|
|
- //將雲端的學生名單記錄到tmp內
|
|
|
- while (studs.MoveNext())
|
|
|
- {
|
|
|
- JsonElement stud = studs.Current;
|
|
|
- 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();
|
|
|
-
|
|
|
- //可能還是要把欲修改ID加進去,之後再刪除
|
|
|
- var existStudInfo = impStuds.Find(o => o.id.Equals(id));
|
|
|
- if (existStudInfo.id != null)
|
|
|
- {
|
|
|
- impStuds.Remove(existStudInfo);
|
|
|
- impStuds.Add((existStudInfo.id, existStudInfo.name, existStudInfo.no, groupId, groupName));
|
|
|
- }
|
|
|
- //else
|
|
|
- tmpStuds.Add((id, name, no, groupId, groupName));
|
|
|
- }
|
|
|
-
|
|
|
- //雲端與新資料進行比對,並找出沒有要被修改的雲端資料,由於該資料是沒有要修改的,所以其座號是無法變更的,故新資料並不能用到其座號。
|
|
|
- var existOldId = tmpStuds.Where(
|
|
|
- a => impStuds.Any(p => !a.id.Equals(p.id))).Select(o => o).ToList();
|
|
|
-
|
|
|
- //將沒有要修改的舊雲端資料與新資料進行比對,確認新資料是否有使用到舊雲端資料已使用的座號。
|
|
|
- var errorNo = impStuds.Where(
|
|
|
- a => existOldId.Any(p => a.no.Equals(p.no))).Select(o => o).ToList();
|
|
|
- dicDuplNo.Add(classNo, errorNo.Select(o => o.id).ToList());
|
|
|
-
|
|
|
- //去除重複座號資料
|
|
|
- foreach (var item in errorNo)
|
|
|
- {
|
|
|
- impStuds.RemoveAll(p => p.id.Equals(item.id));
|
|
|
- }
|
|
|
-
|
|
|
- //去除重複id但no不一樣的舊資料
|
|
|
- var doDeleteId = tmpStuds.Where(
|
|
|
- a => impStuds.Any(p => a.id.Equals(p.id))).Select(o => o).ToList();
|
|
|
-
|
|
|
- foreach (var item in doDeleteId)
|
|
|
- {
|
|
|
- tmpStuds.RemoveAll(p => p.id.Equals(item.id));
|
|
|
- }
|
|
|
-
|
|
|
- //將現有資料加回去
|
|
|
- impStuds.AddRange(tmpStuds);
|
|
|
- }
|
|
|
-
|
|
|
- foreach (var testDataElement in existClassData.EnumerateObject())
|
|
|
- {
|
|
|
- if (!testDataElement.Name.Equals("students", StringComparison.Ordinal)) testDataElement.WriteTo(writer);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- else //如果沒有查到資料代表該Class不存在,必須創建。
|
|
|
- {
|
|
|
- classId = Guid.NewGuid().ToString();
|
|
|
- classNo = importItem.Key;
|
|
|
- writer.WriteString("id", classId);
|
|
|
- writer.WriteString("code", $"Class-{schoolId}");
|
|
|
- writer.WriteString("pk", "Class");
|
|
|
- writer.WriteString("no", importItem.Key);
|
|
|
- if (string.IsNullOrWhiteSpace(classInfos[importItem.Key])) writer.WriteNull("name");
|
|
|
- else writer.WriteString("name", classInfos[importItem.Key]);
|
|
|
- writer.WriteNull("x");
|
|
|
- writer.WriteNull("y");
|
|
|
- writer.WritePropertyName("teacher");
|
|
|
- writer.WriteStartObject();
|
|
|
- writer.WriteNull("id");
|
|
|
- writer.WriteNull("name");
|
|
|
- writer.WriteEndObject();
|
|
|
- writer.WriteNull("gradeId");
|
|
|
- writer.WriteNull("periodId");
|
|
|
- writer.WriteNull("sn");
|
|
|
- writer.WriteNull("style");
|
|
|
- writer.WriteNull("timetable");
|
|
|
- writer.WriteNull("status");
|
|
|
- writer.WriteString("openType", "1");
|
|
|
- writer.WriteString("scope", "school");
|
|
|
- writer.WritePropertyName("courses");
|
|
|
- writer.WriteStartArray();
|
|
|
- writer.WriteEndArray();
|
|
|
- }
|
|
|
- writer.WritePropertyName("students");
|
|
|
- writer.WriteStartArray();
|
|
|
-
|
|
|
- //進行no排序
|
|
|
- impStuds = impStuds.OrderBy(o => string.IsNullOrWhiteSpace(o.no)).ThenBy(o => string.IsNullOrEmpty(o.no) ? 0 : int.Parse(o.no)).ToList();
|
|
|
- foreach (var item in impStuds)
|
|
|
- {
|
|
|
- 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();
|
|
|
-
|
|
|
-
|
|
|
- //取得學生已加入的舊教室資料。
|
|
|
- var asd = await getClassInfoUseStudent(schoolId, tmpImpStuds.Select(o => o.id).ToList());
|
|
|
-
|
|
|
-
|
|
|
- var ret = await _azureCosmos
|
|
|
- .GetCosmosClient()
|
|
|
- .GetContainer("TEAMModelOS", "School")
|
|
|
- .UpsertItemStreamAsync(memoryStream, new PartitionKey($"Class-{schoolId}"));
|
|
|
-
|
|
|
- retClassStud.Add(classNo, (classId, periodId, gradeId, impStuds.Select(o => o.id).ToList()));
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
- return (retClassStud, dicExistId, dicExistNo, dicDuplNo);
|
|
|
- }
|
|
|
- catch (CosmosException ex)
|
|
|
- {
|
|
|
- await _dingDing.SendBotMsg($"IES5,{_option.Location},StudentController/joinClass()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
- }
|
|
|
- catch (Exception ex)
|
|
|
- {
|
|
|
- await _dingDing.SendBotMsg($"IES5,{_option.Location},StudentController/joinClass()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
- }
|
|
|
- return (null, null, null, null);
|
|
|
- }
|
|
|
-
|
|
|
- private async Task<(bool isSuc,List<string> existId, List<string> existNo)> joinClass(string schoolId, string classId, (string id, string name, string no) student)
|
|
|
- {
|
|
|
- var classInfo = await getClassInfoUseId(schoolId, new List<string>() { classId });
|
|
|
-
|
|
|
- List<(string id, string name, string no, string groupId, string groupName)> existStuds = new List<(string id, string name, string no, string groupId, string groupName)>();
|
|
|
- List<string> existId = new List<string>();
|
|
|
- List<string> existNo = new List<string>();
|
|
|
- classInfo[classId].TryGetProperty("students", out var existStudents);
|
|
|
- var studs = existStudents.EnumerateArray();
|
|
|
- while (studs.MoveNext())
|
|
|
- {
|
|
|
- JsonElement stud = studs.Current;
|
|
|
- string id = stud.GetProperty("id").GetString();
|
|
|
- string name = stud.GetProperty("name").GetString();
|
|
|
- string no = stud.GetProperty("no").GetString();
|
|
|
- string groupId = stud.GetProperty("groupId").GetString();
|
|
|
- string groupName = stud.GetProperty("groupName").GetString();
|
|
|
- existStuds.Add((id, name, no, groupId, groupName));
|
|
|
- if (id.Equals(student.id)) existId.Add(id);
|
|
|
- if (!string.IsNullOrWhiteSpace(no) && no.Equals(student.no)) existNo.Add(no);
|
|
|
- }
|
|
|
- if (existId.Count != 0 || existNo.Count != 0) return (false, existId, existNo);
|
|
|
-
|
|
|
- existStuds.Add((student.id, student.name, student.no, null, null));
|
|
|
- existStuds = existStuds.OrderBy(o => string.IsNullOrWhiteSpace(o.no)).ThenBy(o => string.IsNullOrEmpty(o.no) ? 0 : int.Parse(o.no)).ToList();
|
|
|
-
|
|
|
- //組出新的JSON
|
|
|
- using var memoryStream = new MemoryStream();
|
|
|
- using var writer = new Utf8JsonWriter(memoryStream);
|
|
|
- writer.WriteStartObject();
|
|
|
- //將舊資料寫回
|
|
|
- foreach (var dataElement in classInfo[classId].EnumerateObject())
|
|
|
- {
|
|
|
- if (!dataElement.Name.Equals("students", StringComparison.Ordinal)) dataElement.WriteTo(writer);
|
|
|
- }
|
|
|
-
|
|
|
- writer.WritePropertyName("students");
|
|
|
- writer.WriteStartArray();
|
|
|
- foreach (var item in existStuds)
|
|
|
- {
|
|
|
- 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();
|
|
|
- try
|
|
|
- {
|
|
|
- var ret = await _azureCosmos
|
|
|
- .GetCosmosClient()
|
|
|
- .GetContainer("TEAMModelOS", "School")
|
|
|
- .ReplaceItemStreamAsync(memoryStream, classId, new PartitionKey($"Class-{schoolId}"));
|
|
|
- return (true, null, null);
|
|
|
- }
|
|
|
- catch (CosmosException ex)
|
|
|
- {
|
|
|
- await _dingDing.SendBotMsg($"IES5,{_option.Location},StudentController/joinClass()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
- }
|
|
|
- catch (Exception ex)
|
|
|
- {
|
|
|
- await _dingDing.SendBotMsg($"IES5,{_option.Location},StudentController/joinClass()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
- }
|
|
|
- return (false, null, null);
|
|
|
- }
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// 取得教室資訊,使用classId進行查詢。
|
|
|
/// </summary>
|
|
@@ -1604,48 +1185,6 @@ namespace TEAMModelOS.Controllers
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// 使用學生ID來查詢所屬的教室
|
|
|
- /// </summary>
|
|
|
- /// <param name="schoolId"></param>
|
|
|
- /// <param name="ids"></param>
|
|
|
- /// <returns></returns>
|
|
|
- private async Task<List<JsonElement>> getClassInfoUseStudent(string schoolId, List<string> ids)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- List<string> existId = new List<string>();
|
|
|
- List<JsonElement> listClass = new List<JsonElement>();
|
|
|
-
|
|
|
- string queryText = $"SELECT DISTINCT VALUE c FROM c JOIN (SELECT VALUE t FROM t IN c.students WHERE t.id IN ({string.Join(",", ids.Select(o => $"'{o}'"))}))";
|
|
|
- List<object> listStudent = new List<object>();
|
|
|
-
|
|
|
- await foreach (Response item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School")
|
|
|
- .GetItemQueryStreamIterator(queryText: queryText, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Class-{schoolId}") }))
|
|
|
- {
|
|
|
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
|
|
|
- {
|
|
|
- var Documents = json.RootElement.GetProperty("Documents").EnumerateArray();
|
|
|
- while (Documents.MoveNext())
|
|
|
- {
|
|
|
- listClass.Add(Documents.Current.Clone());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return listClass;
|
|
|
- }
|
|
|
- catch (CosmosException ex)
|
|
|
- {
|
|
|
- await _dingDing.SendBotMsg($"IES5,{_option.Location},StudentController/getClassInfoUseStudent()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
- }
|
|
|
- catch (Exception ex)
|
|
|
- {
|
|
|
- await _dingDing.SendBotMsg($"IES5,{_option.Location},StudentController/getClassInfoUseStudent()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// 批量更新學生資訊,目前支持更新姓名、密碼、座號、性別及教室id,匯入時ClassId為必填。
|
|
|
/// </summary>
|
|
@@ -1710,45 +1249,18 @@ namespace TEAMModelOS.Controllers
|
|
|
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;
|
|
|
+ else
|
|
|
+ {
|
|
|
+ errorClassId.Add(id.GetString());
|
|
|
+ continue;
|
|
|
+ }
|
|
|
|
|
|
//如果有給該欄位,且是給空的,代表要清空
|
|
|
if (student.TryGetProperty("no", out var tmpNo)) no = tmpNo.GetString();
|
|
|
|
|
|
if (!studentInfos.ContainsKey(id.GetString()))
|
|
|
{
|
|
|
- /**
|
|
|
- ////整理出後面查詢座號要用的參數
|
|
|
- //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;
|
|
|
|
|
@@ -1820,8 +1332,6 @@ namespace TEAMModelOS.Controllers
|
|
|
//沒查到有該間教室的資訊,故將該間教室的ID及學生資料清單記起來,並且跳過不處理該資料。
|
|
|
errorClassId.Add(item.Key);
|
|
|
item.Value.ForEach(o => nonexistentIds.Remove(o.id));
|
|
|
- //classStuds.Remove(item.Key);
|
|
|
- //errorIds.AddRange(item.Value.Select(o => o.id));
|
|
|
continue;
|
|
|
}
|
|
|
|
|
@@ -2034,263 +1544,6 @@ namespace TEAMModelOS.Controllers
|
|
|
return (null, null, null, null, null, null);
|
|
|
}
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// 批量更新學生所屬教室及學號
|
|
|
- /// </summary>
|
|
|
- /// <param name="schoolId"></param>
|
|
|
- /// <param name="students"></param>
|
|
|
- /// <returns></returns>
|
|
|
- private async Task<Dictionary<string, (string classId, string classNo, string className, string periodId, string gradeId, string name, string no)>> updateClassStudents(string schoolId, Dictionary<string, (string name, string year, string pic, string gender, string mail, string mobile, string classId, string no)> students)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- //最後輸出給前端用的資料
|
|
|
- var retStudentsClassInfo = new Dictionary<string, (string classId, string classNo,string className,string periodId, string gradeId, string name, string 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 classInfos = await getClassInfoUseStudent(schoolId, students.Select(o => o.Key).ToList());
|
|
|
-
|
|
|
- //如果有查到,代表該學生已經加入過某間教室(Class)
|
|
|
- if (classInfos.Count != 0)
|
|
|
- {
|
|
|
- foreach (var classInfo in classInfos)
|
|
|
- {
|
|
|
- //教室資訊的id(UUID)
|
|
|
- if (classInfo.TryGetProperty("id", out var tmpClassId))
|
|
|
- {
|
|
|
- string classId = tmpClassId.GetString();
|
|
|
- string classNo = null, className = null, periodId = null, gradeId = null;
|
|
|
-
|
|
|
- if (classInfo.TryGetProperty("no", out var tmpClassNo)) classNo = tmpClassNo.GetString();
|
|
|
- if (classInfo.TryGetProperty("name", out var tmpClassName)) className = tmpClassName.GetString();
|
|
|
- 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 = 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();
|
|
|
-
|
|
|
- //檢查輸入資料內學生要加入的教室是否一致
|
|
|
- if (students.ContainsKey(studId))
|
|
|
- {
|
|
|
- if (students[studId].classId.Equals(classId, StringComparison.Ordinal))
|
|
|
- {
|
|
|
- //座號及姓名檢查,如果現有資料與欲更新資料不同則進行更新
|
|
|
- if (
|
|
|
- !(students[studId].no.Equals(no, StringComparison.Ordinal)
|
|
|
- && students[studId].name.Equals(name, StringComparison.Ordinal))
|
|
|
- )
|
|
|
- {
|
|
|
- name = students[studId].name;
|
|
|
- no = students[studId].no;
|
|
|
- }
|
|
|
- tmpStuds.Remove(studId);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- 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;
|
|
|
- }
|
|
|
- }
|
|
|
- retStudentsClassInfo.Add(studId, (classId, classNo, className, 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("_")))
|
|
|
- {
|
|
|
- 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();
|
|
|
-
|
|
|
- try
|
|
|
- {
|
|
|
- var ret = await _azureCosmos
|
|
|
- .GetCosmosClient()
|
|
|
- .GetContainer("TEAMModelOS", "School")
|
|
|
- .ReplaceItemStreamAsync(memoryStream, classId, new PartitionKey($"Class-{schoolId}"));
|
|
|
- }
|
|
|
- catch (CosmosException ex)
|
|
|
- {
|
|
|
- await _dingDing.SendBotMsg($"IES5,{_option.Location},StudentController/updateClassStudents()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
- }
|
|
|
- catch (Exception ex)
|
|
|
- {
|
|
|
- await _dingDing.SendBotMsg($"IES5,{_option.Location},StudentController/updateClassStudents()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- //始終會加入新教室,除非只是單純的換座號或是姓名
|
|
|
- if (classInfos.Count == 0 || tmpStuds.Count != 0) //透過學生id查找教室,但沒找到任何已加入的教室
|
|
|
- {
|
|
|
- //使用classId來查詢教室資訊
|
|
|
- var dicClassInfo = await getClassInfoUseId(schoolId, tmpStuds.Select(o => o.Value.classId).ToList());
|
|
|
- if (dicClassInfo.Count != 0)
|
|
|
- {
|
|
|
- foreach (var classInfo in dicClassInfo)
|
|
|
- {
|
|
|
- //用以紀錄教室內已存在的學生
|
|
|
- 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));
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- string classId = classInfo.Key;
|
|
|
- string classNo = null, className = null, periodId = null, gradeId = null;
|
|
|
-
|
|
|
- if (classInfo.Value.TryGetProperty("no", out var tmpClassNo)) classNo = tmpClassNo.GetString();
|
|
|
- if (classInfo.Value.TryGetProperty("name", out var tmpClassName)) className = tmpClassName.GetString();
|
|
|
- if (classInfo.Value.TryGetProperty("periodId", out var tmpPeriodId)) periodId = tmpPeriodId.GetString();
|
|
|
- if (classInfo.Value.TryGetProperty("gradeId", out var tmpGradeId)) gradeId = tmpGradeId.GetString();
|
|
|
-
|
|
|
-
|
|
|
- //如果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, classNo, className, 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 classInfo.Value.EnumerateObject())
|
|
|
- {
|
|
|
- if (!(element.Name.Equals("students", StringComparison.Ordinal) || element.Name.StartsWith("_")))
|
|
|
- {
|
|
|
- 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();
|
|
|
- #endregion
|
|
|
-
|
|
|
- try
|
|
|
- {
|
|
|
- var ret = await _azureCosmos
|
|
|
- .GetCosmosClient()
|
|
|
- .GetContainer("TEAMModelOS", "School")
|
|
|
- .ReplaceItemStreamAsync(memoryStream, classId, new PartitionKey($"Class-{schoolId}"));
|
|
|
- }
|
|
|
- catch (CosmosException ex)
|
|
|
- {
|
|
|
- await _dingDing.SendBotMsg($"IES5,{_option.Location},StudentController/updateClassStudents()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
- }
|
|
|
- catch (Exception ex)
|
|
|
- {
|
|
|
- await _dingDing.SendBotMsg($"IES5,{_option.Location},StudentController/updateClassStudents()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- //透過教室ID進行查詢,但查不到這間教室
|
|
|
- await _dingDing.SendBotMsg(
|
|
|
- $"IES5," +
|
|
|
- $"{_option.Location}," +
|
|
|
- $"StudentController/updateClassStudents()\n" +
|
|
|
- $"找不到教室的資訊 {string.Join(",", tmpStuds.Select(o => o.Value.classId))} ",
|
|
|
- GroupNames.醍摩豆服務運維群組);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return retStudentsClassInfo;
|
|
|
- }
|
|
|
- catch (CosmosException ex)
|
|
|
- {
|
|
|
- await _dingDing.SendBotMsg($"IES5,{_option.Location},StudentController/updateClassStudents()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
- }
|
|
|
- catch (Exception ex)
|
|
|
- {
|
|
|
- await _dingDing.SendBotMsg($"IES5,{_option.Location},StudentController/updateClassStudents()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// 創建學生帳號,目前SDK4.0預覽版還不支援批量創建(TransactionalBatch),待SDK正式發行時在優化此代碼。
|
|
|
/// </summary>
|
|
@@ -2337,105 +1590,6 @@ namespace TEAMModelOS.Controllers
|
|
|
return (false, existId);
|
|
|
}
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// 批量重設密碼,若無給密碼則預設使用學號當密碼
|
|
|
- /// </summary>
|
|
|
- /// <param name="schoolId"></param>
|
|
|
- /// <param name="students"></param>
|
|
|
- /// <returns>不存在的id</returns>
|
|
|
- private async Task<List<string>> resetPW(string schoolId, JsonElement.ArrayEnumerator students)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- //取得前端欲重置的id與pw
|
|
|
- Dictionary<string, (string salt, string pw)> studentsInfo = new Dictionary<string, (string salt, string pw)>();
|
|
|
-
|
|
|
- while (students.MoveNext())
|
|
|
- {
|
|
|
- JsonElement student = students.Current;
|
|
|
- if (student.TryGetProperty("id", out var id))
|
|
|
- {
|
|
|
- //確認是否有id欄位,並且確認是否有給pw欄位,若無給或是null empty等,則使用id當密碼
|
|
|
- if (!string.IsNullOrWhiteSpace(id.GetString()))
|
|
|
- {
|
|
|
- string salt = Utils.CreatSaltString(8);
|
|
|
- string pw = student.TryGetProperty("pw", out var tmpPw) && !string.IsNullOrWhiteSpace(tmpPw.GetString())
|
|
|
- ? Utils.HashedPassword(tmpPw.GetString(), salt)
|
|
|
- : Utils.HashedPassword(id.GetString(), salt);
|
|
|
- if (!studentsInfo.ContainsKey(id.GetString())) studentsInfo.Add(id.GetString(), (salt, pw));
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- CosmosContainer cosmosContainer = _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Student");
|
|
|
- //查學生的基本資料
|
|
|
- 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>();
|
|
|
-
|
|
|
- await foreach (Response item in cosmosContainer
|
|
|
- .GetItemQueryStreamIterator(
|
|
|
- queryText: queryText,
|
|
|
- requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-{schoolId}") }))
|
|
|
- {
|
|
|
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
|
|
|
- {
|
|
|
- var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
|
|
|
- while (accounts.MoveNext())
|
|
|
- {
|
|
|
- JsonElement account = accounts.Current;
|
|
|
- string id = account.GetProperty("id").GetString();
|
|
|
- bool isGetNameSuc = account.TryGetProperty("name", out var name);
|
|
|
- bool isGetClassIdSuc = account.TryGetProperty("classId", out var classId);
|
|
|
- bool isGetNoSuc = account.TryGetProperty("no", out var no);
|
|
|
-
|
|
|
- using var memoryStream = new MemoryStream();
|
|
|
- using var writer = new Utf8JsonWriter(memoryStream);
|
|
|
- writer.WriteStartObject();
|
|
|
- foreach (var element in account.EnumerateObject())
|
|
|
- {
|
|
|
- if (
|
|
|
- !(
|
|
|
- element.Name.Equals("salt", StringComparison.Ordinal)
|
|
|
- || element.Name.Equals("pw", StringComparison.Ordinal)
|
|
|
- || element.Name.StartsWith("_", StringComparison.Ordinal)))
|
|
|
- {
|
|
|
- element.WriteTo(writer);
|
|
|
- }
|
|
|
- }
|
|
|
- writer.WriteString("salt", studentsInfo[id].salt);
|
|
|
- writer.WriteString("pw", studentsInfo[id].pw);
|
|
|
- writer.WriteEndObject();
|
|
|
- writer.Flush();
|
|
|
-
|
|
|
- try
|
|
|
- {
|
|
|
- var ret = await cosmosContainer.ReplaceItemStreamAsync(memoryStream, id, new PartitionKey($"Base-{schoolId}"));
|
|
|
- }
|
|
|
- catch (CosmosException ex)
|
|
|
- {
|
|
|
- await _dingDing.SendBotMsg($"IES5,{_option.Location},StudentController/resetPW()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
- }
|
|
|
- catch (Exception ex)
|
|
|
- {
|
|
|
- await _dingDing.SendBotMsg($"IES5,{_option.Location},StudentController/resetPW()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
- }
|
|
|
-
|
|
|
- //將更新完的id從字典內移除
|
|
|
- studentsInfo.Remove(id);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return studentsInfo.Select(o => o.Key).ToList();
|
|
|
- }
|
|
|
- catch (Exception ex)
|
|
|
- {
|
|
|
- await _dingDing.SendBotMsg($"IES5,{_option.Location},StudentController/resetPW()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// 取得該教室的學生座號,若有給座號LIST,則座號存在才會被查到;反之,若沒給則會將該間教室所有座號抓出來。
|
|
|
/// </summary>
|