|
@@ -11,6 +11,8 @@ using Azure.Cosmos;
|
|
|
using System.Text.Json;
|
|
|
using TEAMModelOS.SDK.Models.Cosmos.Common;
|
|
|
using TEAMModelOS.SDK.Models;
|
|
|
+using TEAMModelBI.Models;
|
|
|
+using TEAMModelOS.SDK.Extension;
|
|
|
|
|
|
namespace TEAMModelBI.Controllers.Census
|
|
|
{
|
|
@@ -42,68 +44,43 @@ namespace TEAMModelBI.Controllers.Census
|
|
|
{
|
|
|
jsonElement.TryGetProperty("tmdId", out JsonElement tmdId);
|
|
|
var cosmosClient = _azureCosmos.GetCosmosClient();
|
|
|
- object total = new object();
|
|
|
+ object totals = new object();
|
|
|
if (!string.IsNullOrEmpty($"{tmdId}"))
|
|
|
{
|
|
|
List<SchoolLen> schoolLens = new List<SchoolLen>();
|
|
|
|
|
|
- List<string> schoolIds = new List<string>();
|
|
|
-
|
|
|
- string schoolSql = $"SELECT DISTINCT REPLACE(c.code,'Teacher-','') AS schoolId,c.code,c.roles,c.id,c.name From c where ARRAY_CONTAINS(c.roles,'assist',true) AND c.pk = 'Teacher' AND c.status = 'join' AND c.id='{tmdId}'";
|
|
|
-
|
|
|
- await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: schoolSql, requestOptions: new QueryRequestOptions() { }))
|
|
|
- {
|
|
|
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
|
|
|
- {
|
|
|
- schoolIds.Add(obj.GetProperty("schoolId").GetString());
|
|
|
- }
|
|
|
- }
|
|
|
+ List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"{tmdId}");
|
|
|
|
|
|
foreach (var itemId in schoolIds)
|
|
|
{
|
|
|
- SchoolLen schoolLen = new SchoolLen() { id = itemId };
|
|
|
+
|
|
|
School school = new();
|
|
|
- try
|
|
|
- {
|
|
|
- school = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(itemId, new PartitionKey("Base"));
|
|
|
- }
|
|
|
- catch
|
|
|
+ var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(itemId, new PartitionKey("Base"));
|
|
|
+ if (response.Status == 200)
|
|
|
{
|
|
|
+ using var json = await JsonDocument.ParseAsync(response.ContentStream);
|
|
|
+ school = json.ToObject<School>();
|
|
|
}
|
|
|
- schoolLen.name = school != null ? school.name : itemId;
|
|
|
|
|
|
- await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: "SELECT DISTINCT c.id,c.code,c.name FROM c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonRecord-{itemId}") }))
|
|
|
- {
|
|
|
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetInt32() > 0)
|
|
|
- {
|
|
|
- schoolLen.total += count.GetInt32();
|
|
|
- }
|
|
|
- }
|
|
|
+ SchoolLen schoolLen = new SchoolLen() { id = itemId, name = school.name != null ? school.name : itemId };
|
|
|
+
|
|
|
+ string sqlTxt = $"SELECT COUNT(c.id) AS totals FROM c WHERE c.code='LessonRecord-{itemId}'";
|
|
|
+
|
|
|
+ schoolLen.totals = await CommonFind.FindTotals(cosmosClient, sqlTxt, "School");
|
|
|
|
|
|
schoolLens.Add(schoolLen);
|
|
|
}
|
|
|
- total = schoolLens;
|
|
|
+ totals = schoolLens;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
+ string lessonSql = $"select COUNT(c.id) AS totals from c where c.pk='LessonRecord'";
|
|
|
+ long tempTotal = await CommonFind.FindTotals(cosmosClient, lessonSql, "School");
|
|
|
|
|
|
- string lessonSql = $"select c.id,c.tmid,c.scope from c where c.pk='LessonRecord'";
|
|
|
- int tempTotal = 0;
|
|
|
- await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: lessonSql, requestOptions: new QueryRequestOptions() { }))
|
|
|
- {
|
|
|
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetInt32() > 0)
|
|
|
- {
|
|
|
- tempTotal = count.GetInt32();
|
|
|
- }
|
|
|
- }
|
|
|
- total = tempTotal;
|
|
|
-
|
|
|
+ totals = tempTotal;
|
|
|
}
|
|
|
|
|
|
- return Ok(new { state = 200, total });
|
|
|
+ return Ok(new { state = 200, totals });
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -116,16 +93,8 @@ namespace TEAMModelBI.Controllers.Census
|
|
|
try
|
|
|
{
|
|
|
var cosmosClient = _azureCosmos.GetCosmosClient();
|
|
|
- string lessonSql = $"select c.id,c.tmid,c.scope from c where c.pk='LessonRecord'";
|
|
|
- int total = 0;
|
|
|
- await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: lessonSql, requestOptions: new QueryRequestOptions() { }))
|
|
|
- {
|
|
|
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetInt32() > 0)
|
|
|
- {
|
|
|
- total = count.GetInt32();
|
|
|
- }
|
|
|
- }
|
|
|
+ string lessonSql = $"select COUNT(c.id) AS totals from c where c.pk='LessonRecord'";
|
|
|
+ long total = await CommonFind.FindTotals(cosmosClient, lessonSql, "School");
|
|
|
|
|
|
return Ok(new { state = 200, total });
|
|
|
}
|
|
@@ -151,41 +120,23 @@ namespace TEAMModelBI.Controllers.Census
|
|
|
var cosmosClient = _azureCosmos.GetCosmosClient();
|
|
|
List<SchoolLen> schoolLens = new List<SchoolLen>();
|
|
|
|
|
|
- List<string> schoolIds = new List<string>();
|
|
|
-
|
|
|
- string schoolSql = $"SELECT DISTINCT REPLACE(c.code,'Teacher-','') AS schoolId,c.code,c.roles,c.id,c.name From c where ARRAY_CONTAINS(c.roles,'assist',true) AND c.pk = 'Teacher' AND c.status = 'join' AND c.id='{tmdId}'";
|
|
|
-
|
|
|
- await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: schoolSql, requestOptions: new QueryRequestOptions() { }))
|
|
|
- {
|
|
|
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
|
|
|
- {
|
|
|
- schoolIds.Add(obj.GetProperty("schoolId").GetString());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
+ List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"{tmdId}");
|
|
|
+
|
|
|
foreach (var itemId in schoolIds)
|
|
|
{
|
|
|
- SchoolLen schoolLen = new SchoolLen() { id = itemId };
|
|
|
School school = new();
|
|
|
- try
|
|
|
- {
|
|
|
- school = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(itemId, new PartitionKey("Base"));
|
|
|
- }
|
|
|
- catch
|
|
|
- {
|
|
|
- }
|
|
|
- schoolLen.name = school != null ? school.name : itemId;
|
|
|
|
|
|
- await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: "SELECT DISTINCT c.id,c.code,c.name FROM c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonRecord-{itemId}") }))
|
|
|
+ var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(itemId, new PartitionKey("Base"));
|
|
|
+ if (response.Status == 200)
|
|
|
{
|
|
|
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetInt32() > 0)
|
|
|
- {
|
|
|
- schoolLen.total += count.GetInt32();
|
|
|
- }
|
|
|
+ using var json = await JsonDocument.ParseAsync(response.ContentStream);
|
|
|
+ school = json.ToObject<School>();
|
|
|
}
|
|
|
|
|
|
+ SchoolLen schoolLen = new SchoolLen() { id = itemId, name = school != null ? school.name : itemId };
|
|
|
+ string sqlTxt = $"SELECT COUNT(c.id) AS totals FROM c WHERE c.code='LessonRecord-{itemId}'";
|
|
|
+ schoolLen.totals = await CommonFind.FindTotals(cosmosClient, sqlTxt, "School");
|
|
|
+
|
|
|
schoolLens.Add(schoolLen);
|
|
|
}
|
|
|
|
|
@@ -205,7 +156,7 @@ namespace TEAMModelBI.Controllers.Census
|
|
|
|
|
|
public string name { get; set;}
|
|
|
|
|
|
- public int total { get; set; }
|
|
|
+ public long totals { get; set; }
|
|
|
}
|
|
|
|
|
|
}
|