|
@@ -12,6 +12,8 @@ using TEAMModelOS.SDK.Models;
|
|
|
using System.Text.Json;
|
|
|
using TEAMModelBI.Tool;
|
|
|
using System.Text;
|
|
|
+using TEAMModelOS.SDK.Extension;
|
|
|
+using TEAMModelBI.Models;
|
|
|
|
|
|
namespace TEAMModelBI.Controllers.Census
|
|
|
{
|
|
@@ -44,38 +46,30 @@ namespace TEAMModelBI.Controllers.Census
|
|
|
{
|
|
|
jsonElement.TryGetProperty("tmdId", out JsonElement tmdId);
|
|
|
if(!jsonElement.TryGetProperty("term", out JsonElement term)) return BadRequest();
|
|
|
- var (start, end) = DateTimeHeloer.GetTermStartOrEnd(DateTime.Now);
|
|
|
+ long start = 0, end = 0;
|
|
|
+ if (bool.Parse($"{term}") == true)
|
|
|
+ {
|
|
|
+ (start, end) = DateTimeHeloer.GetTermStartOrEnd(DateTime.Now);
|
|
|
+ }
|
|
|
var cosmosClient = _azureCosmos.GetCosmosClient();
|
|
|
List<object> activityCount = new List<object>();
|
|
|
|
|
|
if (!string.IsNullOrEmpty($"{tmdId}"))
|
|
|
{
|
|
|
- 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)
|
|
|
{
|
|
|
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>();
|
|
|
}
|
|
|
|
|
|
ActivityCount tempCount = new ActivityCount() { id = itemId, name = school.name != null ? school.name : itemId };
|
|
|
|
|
|
-
|
|
|
foreach (var type in types)
|
|
|
{
|
|
|
StringBuilder sqlTxt = new StringBuilder($"select COUNT(c.id) AS totals from c where c.pk='{type}' and c.school='{itemId}' ");
|
|
@@ -83,46 +77,24 @@ namespace TEAMModelBI.Controllers.Census
|
|
|
{
|
|
|
sqlTxt.Append($" and c.createTime >= {start} and c.createTime <= {end}");
|
|
|
}
|
|
|
- long totals = 0;
|
|
|
- await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIterator(queryText: sqlTxt.ToString(), requestOptions: new QueryRequestOptions() { }))
|
|
|
- {
|
|
|
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
|
|
|
- {
|
|
|
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
|
|
|
- {
|
|
|
- totals += obj.GetProperty("totals").GetInt64();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ long totals = await CommonFind.FindTotals(cosmosClient, sqlTxt.ToString(), "Common");
|
|
|
+
|
|
|
tempCount.census.Add(new KeyValuePair<object, long>(type, totals));
|
|
|
}
|
|
|
activityCount.Add(tempCount);
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
foreach (var type in types)
|
|
|
{
|
|
|
- long totals = 0;
|
|
|
- StringBuilder sqlTxt = new StringBuilder($"SELECT distinct c.id,c.code,c.name,c.pk FROM c where c.pk='{type}' ");
|
|
|
+ StringBuilder sqlTxt = new StringBuilder($"SELECT COUNT(c.id) AS totals FROM c where c.pk='{type}' ");
|
|
|
if (bool.Parse($"{term}") == true)
|
|
|
{
|
|
|
sqlTxt.Append($" and c.createTime >= {start} and c.createTime <= {end}");
|
|
|
}
|
|
|
|
|
|
- await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIterator(queryText: sqlTxt.ToString(), requestOptions: new QueryRequestOptions() { }))
|
|
|
- {
|
|
|
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetInt32() > 0)
|
|
|
- {
|
|
|
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
|
|
|
- {
|
|
|
- totals += obj.GetProperty("totals").GetInt64();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ long totals = await CommonFind.FindTotals(cosmosClient, sqlTxt.ToString(), "Common");
|
|
|
|
|
|
activityCount.Add(new KeyValuePair<string, object>(type, totals));
|
|
|
}
|
|
@@ -141,25 +113,14 @@ namespace TEAMModelBI.Controllers.Census
|
|
|
try
|
|
|
{
|
|
|
var cosmosClient = _azureCosmos.GetCosmosClient();
|
|
|
- List<KeyValuePair<string, int>> typeCount = new List<KeyValuePair<string, int>>();
|
|
|
+ List<KeyValuePair<string, long>> typeCount = new List<KeyValuePair<string, long>>();
|
|
|
foreach (var type in types)
|
|
|
{
|
|
|
- int acount = 0;
|
|
|
- string querySql = $"SELECT distinct c.id,c.code,c.name,c.pk FROM c where c.pk='{type}' ";
|
|
|
- await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIterator(queryText: querySql, requestOptions: new QueryRequestOptions() { }))
|
|
|
- {
|
|
|
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetInt32() > 0)
|
|
|
- {
|
|
|
- acount += count.GetInt32();
|
|
|
- //foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
|
|
|
- //{
|
|
|
- // acount += 1;
|
|
|
- //}
|
|
|
- }
|
|
|
- }
|
|
|
+ string querySql = $"SELECT Count(c.id) as totals FROM c where c.pk='{type}' ";
|
|
|
+
|
|
|
+ long totals = await CommonFind.FindTotals(cosmosClient, querySql, "Common");
|
|
|
|
|
|
- KeyValuePair<string, int> valuePair = new KeyValuePair<string, int>(type, acount);
|
|
|
+ KeyValuePair<string, long> valuePair = new KeyValuePair<string, long>(type, totals);
|
|
|
typeCount.Add(valuePair);
|
|
|
}
|
|
|
|
|
@@ -183,18 +144,8 @@ namespace TEAMModelBI.Controllers.Census
|
|
|
{
|
|
|
if (!jsonElement.TryGetProperty("tmdId", out JsonElement tmdId)) return BadRequest();
|
|
|
var cosmosClient = _azureCosmos.GetCosmosClient();
|
|
|
- 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}'";
|
|
|
+ List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"{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<KeyValuePair<string, object>> typeCount = new List<KeyValuePair<string, object>>();
|
|
|
foreach (var type in types)
|
|
|
{
|
|
@@ -203,30 +154,19 @@ namespace TEAMModelBI.Controllers.Census
|
|
|
foreach (var itemId in schoolIds)
|
|
|
{
|
|
|
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>();
|
|
|
}
|
|
|
|
|
|
SchoolActivity schoolActivity = new SchoolActivity() { id = itemId, name = school.name != null ? school.name : itemId };
|
|
|
|
|
|
string activitySql = $"SELECT DISTINCT c.id,c.code,c.name FROM c WHERE c.pk='{type}' AND c.school='{itemId}'";
|
|
|
- await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIterator(queryText: activitySql, requestOptions: new QueryRequestOptions() { }))
|
|
|
- {
|
|
|
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
|
|
|
- {
|
|
|
- acount += count.GetInt32();
|
|
|
- schoolActivity.total += count.GetInt64();
|
|
|
- //foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
|
|
|
- //{
|
|
|
- // acount += 1;
|
|
|
- //}
|
|
|
- }
|
|
|
- }
|
|
|
+
|
|
|
+ schoolActivity.total = await CommonFind.FindTotals(cosmosClient, activitySql, "Common");
|
|
|
+
|
|
|
schoolActivities.Add(schoolActivity);
|
|
|
}
|
|
|
|
|
@@ -253,46 +193,28 @@ namespace TEAMModelBI.Controllers.Census
|
|
|
|
|
|
if (!string.IsNullOrEmpty($"{tmdId}"))
|
|
|
{
|
|
|
- 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}'";
|
|
|
+ List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"{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());
|
|
|
- }
|
|
|
- }
|
|
|
foreach (var type in types)
|
|
|
{
|
|
|
List<SchoolActivity> schoolActivities = new List<SchoolActivity>();
|
|
|
foreach (var itemId in schoolIds)
|
|
|
{
|
|
|
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>();
|
|
|
}
|
|
|
|
|
|
SchoolActivity schoolActivity = new SchoolActivity() { id = itemId, name = school.name != null ? school.name : itemId };
|
|
|
|
|
|
string activitySql = $"SELECT COUNT(c.id) AS totals FROM c WHERE c.pk='{type}' AND c.school='{itemId}' and c.createTime >= {start} and c.createTime <= {end}";
|
|
|
- await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIterator(queryText: activitySql, requestOptions: new QueryRequestOptions() { }))
|
|
|
- {
|
|
|
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
|
|
|
- {
|
|
|
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
|
|
|
- {
|
|
|
- schoolActivity.total += obj.GetProperty("totals").GetInt64();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+
|
|
|
+ long totals = await CommonFind.FindTotals(cosmosClient, activitySql, "Common");
|
|
|
+ schoolActivity.total = totals;
|
|
|
+
|
|
|
schoolActivities.Add(schoolActivity);
|
|
|
}
|
|
|
|
|
@@ -304,21 +226,10 @@ namespace TEAMModelBI.Controllers.Census
|
|
|
{
|
|
|
foreach (var type in types)
|
|
|
{
|
|
|
- long acount = 0;
|
|
|
string querySql = $"SELECT COUNT(c.id) AS totals FROM c where c.pk='{type}' and c.createTime >= {start} and c.createTime <= {end}";
|
|
|
- await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIterator(queryText: querySql, requestOptions: new QueryRequestOptions() { }))
|
|
|
- {
|
|
|
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetInt32() > 0)
|
|
|
- {
|
|
|
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
|
|
|
- {
|
|
|
- acount += obj.GetProperty("totals").GetInt64();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ long totals = await CommonFind.FindTotals(cosmosClient, querySql, "Common");
|
|
|
|
|
|
- KeyValuePair<string, object> valuePair = new KeyValuePair<string, object>(type, acount);
|
|
|
+ KeyValuePair<string, object> valuePair = new KeyValuePair<string, object>(type, totals);
|
|
|
typeCount.Add(valuePair);
|
|
|
}
|
|
|
}
|