|
@@ -901,5 +901,93 @@ namespace TEAMModelOS.Controllers
|
|
|
return BadRequest();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 回收某學校AClassOne固定分配、動態分配授權學生
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="request"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ [ProducesDefaultResponseType]
|
|
|
+ [HttpPost("recall-school-aclasson")]
|
|
|
+ public async Task<IActionResult> RecallSchoolAclassone(JsonElement request)
|
|
|
+ {
|
|
|
+ if (!request.TryGetProperty("school_code", out JsonElement school_code)) return BadRequest();
|
|
|
+ string action = "all"; //預設回收:固定及動態均回收 sta:回收固定 dync:回收動態
|
|
|
+ if (request.TryGetProperty("mode", out JsonElement mode))
|
|
|
+ {
|
|
|
+ if(mode.GetString() == "sta" || mode.GetString() == "dync")
|
|
|
+ {
|
|
|
+ action = mode.GetString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ int status = 0;
|
|
|
+ string err = "";
|
|
|
+ var redisClient = _azureRedis.GetRedisClient(8);
|
|
|
+ string keys = school_code.GetString() + ":" + "AclassOne" + ":" + "dynamic";
|
|
|
+ string keyd = school_code.GetString() + ":" + "AclassOne" + ":" + "dynamicIds";
|
|
|
+ var clientContainer = _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School");
|
|
|
+ var response = await clientContainer.ReadItemStreamAsync(school_code.ToString(), new PartitionKey("Product"));
|
|
|
+ if (response.Status == 200)
|
|
|
+ {
|
|
|
+ int total = 0; //(回傳值)可分配總數
|
|
|
+ int staNum = 0; //(回傳值)固定分配數
|
|
|
+ int dyncNum = 0; //(回傳值)動態分配數
|
|
|
+ List<string> staIds = new List<string>(); //(回傳值)固定學生ID列
|
|
|
+ List<string> dyncIds = new List<string>(); //(回傳值)動態學生ID列
|
|
|
+ using (Stream stream = response.ContentStream)
|
|
|
+ {
|
|
|
+ using (StreamReader streamReader = new StreamReader(stream))
|
|
|
+ {
|
|
|
+ string content = streamReader.ReadToEnd();
|
|
|
+ SchoolProduct schoolProduct = System.Text.Json.JsonSerializer.Deserialize<SchoolProduct>(content);
|
|
|
+ if (schoolProduct.aclassone != null) //有AClassOne產品授權,schoolProduct.aclassone不會為null
|
|
|
+ {
|
|
|
+ total = schoolProduct.aclassone.total;
|
|
|
+ staNum = schoolProduct.aclassone.used;
|
|
|
+ staIds = schoolProduct.aclassone.ids;
|
|
|
+ dyncNum = (int)GetSchoolDynamicAclassOneIDCount(school_code.GetString());
|
|
|
+ dyncIds = GetSchoolDynamicAclassOneIDList(school_code.GetString());
|
|
|
+ switch (action)
|
|
|
+ {
|
|
|
+ case "sta":
|
|
|
+ staIds = schoolProduct.aclassone.ids = new List<string>();
|
|
|
+ staNum = schoolProduct.aclassone.used = 0;
|
|
|
+ await clientContainer.ReplaceItemAsync<SchoolProduct>(schoolProduct, schoolProduct.id, new PartitionKey("Product"));
|
|
|
+ int updDynAclassCount = (total - dyncNum > 0) ? total - dyncNum : 0;
|
|
|
+ redisClient.StringSet(keys, updDynAclassCount);
|
|
|
+ break;
|
|
|
+ case "dync": //動態回收
|
|
|
+ dyncNum = total - staNum;
|
|
|
+ dyncIds = new List<string>();
|
|
|
+ redisClient.StringSet(keys, dyncNum);
|
|
|
+ redisClient.KeyDelete(keyd);
|
|
|
+ break;
|
|
|
+ case "all": //全回收
|
|
|
+ staIds = schoolProduct.aclassone.ids = new List<string>();
|
|
|
+ staNum = schoolProduct.aclassone.used = 0;
|
|
|
+ await clientContainer.ReplaceItemAsync<SchoolProduct>(schoolProduct, schoolProduct.id, new PartitionKey("Product"));
|
|
|
+ dyncNum = total;
|
|
|
+ dyncIds = new List<string>();
|
|
|
+ redisClient.StringSet(keys, dyncNum);
|
|
|
+ redisClient.KeyDelete(keyd);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ return Ok(new { status, err, total, staNum, dyncNum, staIds, dyncIds });
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ status = 1;
|
|
|
+ err = "AClassOne has no authorization.";
|
|
|
+ return Ok(new { status, err });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return BadRequest();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
}
|