|
@@ -816,7 +816,7 @@ namespace TEAMModelOS.SDK
|
|
|
writerNew.Flush();
|
|
|
if (!string.IsNullOrWhiteSpace(stud.Value.imei))
|
|
|
{
|
|
|
-
|
|
|
+ await upsertImei(stud.Key, stud.Value.imei, schoolId, "keep", cosmosContainer);
|
|
|
}
|
|
|
var response = await cosmosContainer.CreateItemStreamAsync(memoryStream, new PartitionKey($"Base-{schoolId}"));
|
|
|
|
|
@@ -919,7 +919,7 @@ namespace TEAMModelOS.SDK
|
|
|
/// <summary>
|
|
|
/// grand_type
|
|
|
/// 为import:如果导入则已最新,没导入则不动这个字段的值,维持现状
|
|
|
- /// 为create
|
|
|
+ /// 为create:如果有值,则绑定,并删除stuid关联的别的imei.
|
|
|
/// 为update
|
|
|
/// 为delete
|
|
|
/// </summary>
|
|
@@ -927,7 +927,10 @@ namespace TEAMModelOS.SDK
|
|
|
/// <param name="imei"></param>
|
|
|
/// <param name="schoolId"></param>
|
|
|
/// <param name="grand_type"></param>
|
|
|
- /// <param name="cosmosContainer"></param>
|
|
|
+ /// <param name="cosmosContainer">
|
|
|
+ /// clean 当传入的电子学生证值为空,判断要强制清除关联的学生电子学生证 ,
|
|
|
+ /// keep 保持现状,导入时
|
|
|
+ /// delete 因学生被删除,强制删除电子学生证。</param>
|
|
|
/// <returns></returns>
|
|
|
public static async Task upsertImei(string stuid ,string imeiid,string schoolId,string grand_type, CosmosContainer cosmosContainer) {
|
|
|
List<Imei> imeis = new List<Imei>();
|
|
@@ -936,44 +939,66 @@ namespace TEAMModelOS.SDK
|
|
|
{
|
|
|
imeis.Add(item);
|
|
|
}
|
|
|
- bool notin = true;
|
|
|
- List<Imei> update = new List<Imei>();
|
|
|
- imeis.ForEach(x => {
|
|
|
- if (!x.id.Equals(imeiid))
|
|
|
- {
|
|
|
- x.stuid = null;
|
|
|
- x.school = null;
|
|
|
- update.Add(x);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- notin = false;
|
|
|
- }
|
|
|
- });
|
|
|
- if (notin)
|
|
|
- {
|
|
|
- var imeiResponse = await cosmosContainer.ReadItemStreamAsync(imeiid, new PartitionKey("Imei"));
|
|
|
- if (imeiResponse.Status == 200)
|
|
|
- {
|
|
|
- Imei imeiDb = JsonDocument.Parse(imeiResponse.Content).RootElement.Deserialize<Imei>();
|
|
|
- imeiDb.stuid = stuid;
|
|
|
- imeiDb.school = schoolId;
|
|
|
- update.Add(imeiDb);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- Imei imei = new Imei { id = imeiid, code = "Imei", pk = "Imei", stuid = stuid, school = schoolId };
|
|
|
- await cosmosContainer.CreateItemAsync(imei, new PartitionKey($"Imei"));
|
|
|
- };
|
|
|
- }
|
|
|
- if (update.Any())
|
|
|
+ switch (grand_type)
|
|
|
{
|
|
|
- List<Task<ItemResponse<Imei>>> imeiTasks = new List<Task<ItemResponse<Imei>>>();
|
|
|
- update.ForEach(x => {
|
|
|
- imeiTasks.Add(cosmosContainer.ReplaceItemAsync(x, x.id, new PartitionKey("Imei")));
|
|
|
- });
|
|
|
- await Task.WhenAll(imeiTasks);
|
|
|
+ case "clean":
|
|
|
+ case "keep":
|
|
|
+ bool notin = true;
|
|
|
+ List<Imei> update = new List<Imei>();
|
|
|
+ List<Imei> delete = new List<Imei>();
|
|
|
+ imeis.ForEach(x => {
|
|
|
+ //如果传入的电子学生证id不存在,且是单个更新或创建,则解除学生id的电子学生证绑定,并删除该电子学生证。
|
|
|
+ if (string.IsNullOrWhiteSpace(imeiid)) {
|
|
|
+ if (grand_type.Equals("clean") )
|
|
|
+ {
|
|
|
+ delete.Add(x);
|
|
|
+ }// 如果是导入模式,且电子学生证没有值则不动。
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ //如果电子学生证有值则,解除之前的所有与当前电子学生证id不符的绑定。
|
|
|
+ if (!x.id.Equals(imeiid))
|
|
|
+ {
|
|
|
+ x.stuid = null;
|
|
|
+ x.school = null;
|
|
|
+ delete.Add(x);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //存在且正确绑定
|
|
|
+ notin = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ //当前学生还未有任何电子学生证,且有新的电子学生证id传入,则需要捞出电子学生证,绑定,如果没有捞出,则新建电子学生证,并绑定
|
|
|
+ if (notin && !string.IsNullOrWhiteSpace(imeiid))
|
|
|
+ {
|
|
|
+ var imeiResponse = await cosmosContainer.ReadItemStreamAsync(imeiid, new PartitionKey("Imei"));
|
|
|
+ if (imeiResponse.Status == 200)
|
|
|
+ {
|
|
|
+ Imei imeiDb = JsonDocument.Parse(imeiResponse.Content).RootElement.Deserialize<Imei>();
|
|
|
+ imeiDb.stuid = stuid;
|
|
|
+ imeiDb.school = schoolId;
|
|
|
+ await cosmosContainer.ReplaceItemAsync(imeiDb, imeiDb.id, new PartitionKey("Imei"));
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Imei imei = new Imei { id = imeiid, code = "Imei", pk = "Imei", stuid = stuid, school = schoolId };
|
|
|
+ await cosmosContainer.CreateItemAsync(imei, new PartitionKey($"Imei"));
|
|
|
+ };
|
|
|
+ }
|
|
|
+ if (delete.Any())
|
|
|
+ {
|
|
|
+ await cosmosContainer.DeleteItemsStreamAsync(delete.Select(x => x.id).ToList(), "Imei");
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "delete":
|
|
|
+ if (imeis.Any()) {
|
|
|
+ await cosmosContainer.DeleteItemsStreamAsync(imeis.Select(x => x.id).ToList(), "Imei");
|
|
|
+ }
|
|
|
+ break;
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -1051,17 +1076,15 @@ namespace TEAMModelOS.SDK
|
|
|
}
|
|
|
writer.WriteEndObject();
|
|
|
writer.Flush();
|
|
|
- if (!string.IsNullOrWhiteSpace(studCreateInfo.imei))
|
|
|
- {
|
|
|
- Imei imei = new Imei { id = studCreateInfo.imei, code = "Imei", pk = "Imei", stuid = studCreateInfo.id, school = schoolId };
|
|
|
- await _azureCosmos
|
|
|
- .GetCosmosClient()
|
|
|
- .GetContainer(Constant.TEAMModelOS, "Student").UpsertItemAsync(imei, new PartitionKey($"Imei"));
|
|
|
- }
|
|
|
+
|
|
|
var response = await _azureCosmos
|
|
|
.GetCosmosClient()
|
|
|
.GetContainer(Constant.TEAMModelOS, "Student")
|
|
|
.CreateItemStreamAsync(stream, new PartitionKey($"Base-{schoolId}"));
|
|
|
+ //更新电子学生证、
|
|
|
+ await upsertImei(studCreateInfo.id, studCreateInfo.imei, schoolId, "keep", _azureCosmos
|
|
|
+ .GetCosmosClient()
|
|
|
+ .GetContainer(Constant.TEAMModelOS, "Student"));
|
|
|
if (response.Status == (int)HttpStatusCode.Created || response.Status == (int)HttpStatusCode.OK) return true;
|
|
|
if (response.Status == (int)HttpStatusCode.Conflict) return false;
|
|
|
else
|
|
@@ -1069,6 +1092,7 @@ namespace TEAMModelOS.SDK
|
|
|
await _dingDing.SendBotMsg($"IES5,{_option.Location},StudentController/createStudent()\nCosmosDB Create response status = {response.Status}\nID:{studCreateInfo.id}", GroupNames.醍摩豆服務運維群組);
|
|
|
return false;
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
@@ -1546,9 +1570,11 @@ namespace TEAMModelOS.SDK
|
|
|
string id = string.Empty;
|
|
|
try
|
|
|
{
|
|
|
+
|
|
|
JsonElement student = students.Current;
|
|
|
id = student.GetProperty("id").GetString();
|
|
|
var ret = await container.DeleteItemStreamAsync(id, new PartitionKey($"Base-{schoolId}"));
|
|
|
+ await upsertImei(id, null, schoolId, "delete", container);
|
|
|
if (ret.Status == (int)HttpStatusCode.NoContent) sucIds.Add(id);
|
|
|
await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<GroupList>(queryText: $"select value(c) from c join A0 in c.members where A0.id = '{id}' and A0.code='{schoolId}' ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"GroupList-{schoolId}") }))
|
|
|
{
|
|
@@ -1809,7 +1835,7 @@ namespace TEAMModelOS.SDK
|
|
|
/// <param name="students"></param>
|
|
|
/// <returns></returns>
|
|
|
public static 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(AzureCosmosFactory _azureCosmos, DingDing _dingDing, Option _option, string schoolId, JsonElement.ArrayEnumerator students)
|
|
|
+ updateStudents(AzureCosmosFactory _azureCosmos, DingDing _dingDing, Option _option, string schoolId, JsonElement.ArrayEnumerator students,bool cleanImei)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
@@ -2149,23 +2175,7 @@ namespace TEAMModelOS.SDK
|
|
|
writer.WriteString("irs", studentInfos[id].irs);
|
|
|
}
|
|
|
break;
|
|
|
- case bool _ when element.Name.Equals("imei", StringComparison.Ordinal):
|
|
|
- //移除座號的話會給空的
|
|
|
- if (studentInfos[id].imei != null && studentInfos[id].imei.Length == 0)
|
|
|
- {
|
|
|
- writer.WriteNull("imei");
|
|
|
- tmpData.imei = null;
|
|
|
- }
|
|
|
- else if (string.IsNullOrWhiteSpace(studentInfos[id].imei))
|
|
|
- {
|
|
|
- element.WriteTo(writer);
|
|
|
- tmpData.imei = element.Value.GetString();
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- writer.WriteString("imei", studentInfos[id].imei);
|
|
|
- }
|
|
|
- break;
|
|
|
+
|
|
|
case bool _ when element.Name.StartsWith("_", StringComparison.Ordinal):
|
|
|
break;
|
|
|
default:
|
|
@@ -2216,12 +2226,17 @@ namespace TEAMModelOS.SDK
|
|
|
|
|
|
writer.WriteEndObject();
|
|
|
writer.Flush();
|
|
|
- if (!string.IsNullOrWhiteSpace(studentInfos[id].imei))
|
|
|
+ //编辑是是否要明确清除电子学生证。
|
|
|
+ if (cleanImei)
|
|
|
{
|
|
|
- Imei imei = new Imei { id = studentInfos[id].imei, code = "Imei", pk = "Imei", stuid = id, school = schoolId };
|
|
|
- await _azureCosmos
|
|
|
- .GetCosmosClient()
|
|
|
- .GetContainer(Constant.TEAMModelOS, "Student").UpsertItemAsync(imei, new PartitionKey($"Imei"));
|
|
|
+ await upsertImei(id, studentInfos[id].imei, schoolId, "clean", _azureCosmos
|
|
|
+ .GetCosmosClient()
|
|
|
+ .GetContainer(Constant.TEAMModelOS, "Student"));
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ await upsertImei(id, studentInfos[id].imei, schoolId, "keep", _azureCosmos
|
|
|
+ .GetCosmosClient()
|
|
|
+ .GetContainer(Constant.TEAMModelOS, "Student"));
|
|
|
}
|
|
|
try
|
|
|
{
|