浏览代码

编写所有区的统计,和优化区域统计相关的接口、相关方法,优化空间统计

Li 3 年之前
父节点
当前提交
185d5af02c

+ 100 - 7
TEAMModelBI/Controllers/Census/ActivitySticsController.cs

@@ -258,7 +258,7 @@ namespace TEAMModelBI.Controllers.Census
                 ActivityCount tempCount = new ActivityCount() { id = school.id, name = school.name != null ? school.name : school.id };
                 ActivityCount tempCount = new ActivityCount() { id = school.id, name = school.name != null ? school.name : school.id };
                 foreach (var type in types)
                 foreach (var type in types)
                 {
                 {
-                    StringBuilder sqlTxt = new StringBuilder($"select COUNT(c.id) AS totals from c where c.pk='{type}' and c.school='{school.id}' ");                
+                    StringBuilder sqlTxt = new($"select COUNT(c.id) AS totals from c where c.pk='{type}' and c.school='{school.id}' ");                
                     long totals = await CommonFind.FindTotals(cosmosClient, sqlTxt.ToString(), new List<string>() { "Common" });
                     long totals = await CommonFind.FindTotals(cosmosClient, sqlTxt.ToString(), new List<string>() { "Common" });
 
 
                     switch (type)
                     switch (type)
@@ -299,7 +299,15 @@ namespace TEAMModelBI.Controllers.Census
 
 
             var cosmosClient = _azureCosmos.GetCosmosClient();
             var cosmosClient = _azureCosmos.GetCosmosClient();
             List<RecSchool> schools = new();
             List<RecSchool> schools = new();
-            await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<RecSchool>(queryText: $"select c.id,c.name,c.picture,c.size,c.scale,c.type from c where c.areaId='{areaId}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
+
+
+            StringBuilder scSqlTxt = new("select c.id,c.name,c.picture,c.size,c.scale,c.type from c");
+            if (!string.IsNullOrEmpty($"{areaId}"))
+            {
+                scSqlTxt.Append($" where c.areaId='{areaId}'");
+            }
+
+            await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<RecSchool>(queryText: scSqlTxt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
             {
             {
                 schools.Add(item);
                 schools.Add(item);
             }
             }
@@ -331,7 +339,7 @@ namespace TEAMModelBI.Controllers.Census
                     heCount += 1;
                     heCount += 1;
                 else if (school.type == 1)
                 else if (school.type == 1)
                     geCount += 1;
                     geCount += 1;
-                else oeCount += 0;
+                else oeCount += 1;
 
 
                 areaSize += school.size;
                 areaSize += school.size;
 
 
@@ -387,8 +395,7 @@ namespace TEAMModelBI.Controllers.Census
                 ActivityCount tempActivity = new ActivityCount() { id = school.id, name = school.name != null ? school.name : school.id };
                 ActivityCount tempActivity = new ActivityCount() { id = school.id, name = school.name != null ? school.name : school.id };
                 foreach (var type in types)
                 foreach (var type in types)
                 {
                 {
-                    string sqlTxt = new string($"select COUNT(c.id) AS totals from c where c.pk='{type}' and c.school='{school.id}' ");
-                    long totals = await CommonFind.FindTotals(cosmosClient, sqlTxt, new List<string>() { "Common" });
+                    long totals = await CommonFind.FindTotals(cosmosClient, $"select COUNT(c.id) AS totals from c where c.pk='{type}' and c.school='{school.id}' ", new List<string>() { "Common" });
 
 
                     string weekSql= $"select count(c.id) as totals from c where c.pk='{type}' and c.school='{school.id}' and c.createTime>={weekStart}   and c.createTime<={weekEnd}";
                     string weekSql= $"select count(c.id) as totals from c where c.pk='{type}' and c.school='{school.id}' and c.createTime>={weekStart}   and c.createTime<={weekEnd}";
                     long weekCount = await CommonFind.FindTotals(cosmosClient, weekSql, new List<string>() { "Common" });
                     long weekCount = await CommonFind.FindTotals(cosmosClient, weekSql, new List<string>() { "Common" });
@@ -422,6 +429,76 @@ namespace TEAMModelBI.Controllers.Census
             return Ok(new { state = 200, schoolCount = schools.Count, countArea, weekActivity, termActivity, totalTime, appraiseArea, examAreaCount, surveyAreaCount, voteAreaCount, homeworkAreaCount, schools = schoolInfos, schoolLessons });
             return Ok(new { state = 200, schoolCount = schools.Count, countArea, weekActivity, termActivity, totalTime, appraiseArea, examAreaCount, surveyAreaCount, voteAreaCount, homeworkAreaCount, schools = schoolInfos, schoolLessons });
         }
         }
 
 
+        /// <summary>
+        /// 所有区的统计接口
+        /// </summary>
+        /// <returns></returns>
+        [HttpPost("get-all")]
+        public async Task<IActionResult> GetAll() 
+        {
+            var cosmosClient = _azureCosmos.GetCosmosClient();
+
+            List<AreaInfo> areaInfos = new();
+            await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Normal").GetItemQueryIterator<AreaInfo>(queryText: $"select c.id,c.name,c.standard,c.standardName from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base-Area") }))
+            {
+                areaInfos.Add(item);
+            }
+
+            var (weekStart, weekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "week");
+            var (termStart, termEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "term");
+            int heCount = 0;//高教
+            int geCount = 0;//普教
+            int oeCount = 0; //其他教育
+            int allSize = 0;//空间总量
+            Dictionary<string, long> activitys = new(); //活动类型集合
+            long allActivity = 0;  //活动累计
+            long weekActivity = 0;//本周活动
+            long tearActivity = 0;//本学期活动
+            long weekLess = 0;      //本周课例
+            long termLess = 0;     //本学期课例
+            long allLess = 0;     //所有课例
+
+            foreach (var area in areaInfos) 
+            {
+                List<RecSchool> recSchools = new();
+                await foreach (var school in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<RecSchool>(queryText: $"select c.id,c.name,c.picture,c.type,c.size,c.scale from c where c.areaId='{area.id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") })) 
+                {
+                    recSchools.Add(school);
+                }
+
+                recSchools.ForEach(x => { if (x.type == 2) heCount += 1; else if (x.type == 1) geCount += 1; else oeCount += 1; });
+                allSize += recSchools.Select(s => s.size).Sum();
+                area.schoolCount = recSchools.Count;
+                //查教师
+                area.techCount = await CommonFind.GetPeopleNumber(cosmosClient,"School",recSchools?.Select(x=>x.id).ToList(), "Teacher");
+                //查询省
+                area.stuCount = await CommonFind.GetPeopleNumber(cosmosClient, "Student", recSchools?.Select(x => x.id).ToList(), "Base");
+            }
+
+            //统计活动
+            foreach (var type in types)
+            {
+                string sqlTxt = $"select COUNT(c.id) AS totals from c where c.pk='{type}'";
+                long totals = await CommonFind.FindTotals(cosmosClient, sqlTxt, new List<string>() { "Common" });
+
+                string weekSql = $"select COUNT(c.id) AS totals from c where c.pk='{type}' and c.createTime>={weekStart} and c.createTime<={weekStart} ";
+                weekActivity  += await CommonFind.FindTotals(cosmosClient, weekSql,new List<string> { "Common" });
+
+                string termSql = $"select COUNT(c.id) AS totals from c where c.pk='{type}' and c.createTime>={termStart} and c.createTime<={termEnd} ";
+                tearActivity += await CommonFind.FindTotals(cosmosClient, termSql, new List<string> { "Common" });
+
+                allActivity += totals;
+                activitys.Add(type, totals);
+            }
+            
+            allLess = await CommonFind.FindTotals(cosmosClient, "select count(c.id) as totals from c where c.pk='LessonRecord'", new List<string>() { "School" });
+            weekLess = await CommonFind.FindTotals(cosmosClient, $"select count(c.id) as totals from c where c.pk='LessonRecord' and c.startTime>={weekStart} and c.startTime <={weekEnd}", new List<string>() { "School", "Teacher" });
+            termLess = await CommonFind.FindTotals(cosmosClient, $"select count(c.id) as totals from c where c.pk='LessonRecord' and c.startTime>={termStart} and c.startTime <={termEnd}", new List<string>() { "School","Teacher" });
+
+            return Ok(new { state = 200, areaCount = areaInfos.Count, allSize, heCount, geCount, oeCount, allLess, termLess, weekLess, allActivity, tearActivity,weekActivity, areaInfos });
+        }
+                
+
         /// <summary>
         /// <summary>
         /// 依据活动Id查询活动详情信息  数据管理工具——查询工具
         /// 依据活动Id查询活动详情信息  数据管理工具——查询工具
         /// </summary>
         /// </summary>
@@ -467,7 +544,23 @@ namespace TEAMModelBI.Controllers.Census
             return Ok(new { state = 200, infos });
             return Ok(new { state = 200, infos });
         }
         }
 
 
+        /// <summary>
+        /// 区级信息
+        /// </summary>
+        public record AreaInfo
+        {
+            public string id { get; set; }
+            public string name { get; set; }
+            public string standard { get; set; }
+            public string standardName { get; set; }
+            public int schoolCount { get; set; }
+            public int techCount { get; set;}
+            public int stuCount { get; set; }
+        }
 
 
+        /// <summary>
+        /// 区级下学校信息显示
+        /// </summary>
         public record RecSchool
         public record RecSchool
         {
         {
             public string id { get; set; }
             public string id { get; set; }
@@ -500,10 +593,10 @@ namespace TEAMModelBI.Controllers.Census
             public string id { get; set; }
             public string id { get; set; }
             public string name { get; set; }
             public string name { get; set; }
             public string picture { get; set; }
             public string picture { get; set; }
-            public int count{get;set;}
+            public int count { get; set; }
         }
         }
 
 
-            public record ActivityCount 
+        public record ActivityCount
         {
         {
             public string id { get; set; }
             public string id { get; set; }
             public string name { get; set; }
             public string name { get; set; }

+ 169 - 0
TEAMModelBI/Controllers/Census/BlobLogController.cs

@@ -0,0 +1,169 @@
+using Azure.Cosmos;
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Options;
+using StackExchange.Redis;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Text.Json;
+using System.Threading.Tasks;
+using TEAMModelBI.Models;
+using TEAMModelBI.Tool;
+using TEAMModelBI.Tool.Cosmos;
+using TEAMModelOS.Models;
+using TEAMModelOS.SDK.DI;
+using TEAMModelOS.SDK.Extension;
+using TEAMModelOS.SDK.Models;
+
+namespace TEAMModelBI.Controllers.Census
+{
+    [Route("bloblog")]
+    [ApiController]
+    public class BlobLogController : ControllerBase
+    {
+        private readonly AzureCosmosFactory _azureCosmos;
+        private readonly AzureStorageFactory _azureStorage;
+        private readonly DingDing _dingDing;
+        private readonly Option _option;
+        private readonly AzureRedisFactory _azureRedis;
+
+        public BlobLogController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureFactory, DingDing dingDing, IOptionsSnapshot<Option> option, AzureRedisFactory azureRedis)
+        {
+            _azureCosmos = azureCosmos;
+            _azureStorage = azureFactory;
+            _dingDing = dingDing;
+            _option = option?.Value;
+            _azureRedis = azureRedis;
+        }
+
+        /// <summary>
+        /// 空间统计数量统计
+        /// </summary>
+        /// <param name="jsonElement"></param>
+        /// <returns></returns>
+        [ProducesDefaultResponseType]
+        [HttpPost("get-area")]
+        public async Task<IActionResult> GetArea(JsonElement jsonElement) 
+        {
+            jsonElement.TryGetProperty("areaId", out JsonElement areaId);
+            var cosmosClient = _azureCosmos.GetCosmosClient();
+            StringBuilder sqlSize = new($"select sum(c.size) as totals from c ");
+            long useSize = 0;
+            List<RecBlobFile> blobFiles = new();
+
+            if (!string.IsNullOrEmpty($"{areaId}"))
+            {
+                string sqlTxt = $"select c.id from c where c.areaId='{areaId}'";
+                sqlSize.Append($" where c.areaId = '{areaId}'");
+
+                List<string> schools = await CommonFind.FindSchoolIds(cosmosClient, sqlTxt.ToString(), "Base");
+                foreach (var id in schools)
+                {
+                    await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<RecBlobFile>(queryText: "SELECT c.id,c.code,c.name,c.size,c.type FROM c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Bloblog-{id}") }))
+                    {
+                        blobFiles.Add(item);
+                    }
+
+                    List<string> tecId = await CommonFind.FindRolesId(cosmosClient, schools);
+                    List<RecBlobFile> tecBlob = await GetBlobTeache(cosmosClient, tecId);
+                    if (tecBlob.Count > 0) 
+                    {
+                        blobFiles.Concat(tecBlob);
+                    }
+
+                    await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<RecBlobFile>(queryText: $"SELECT c.id,c.code,c.name,c.size,c.type FROM c where c.pk='Bloblog'", requestOptions: new QueryRequestOptions() { }))
+                    {
+                        blobFiles.Add(item);
+                    }
+
+                    long blobsize = 0;
+                    RedisValue value = _azureRedis.GetRedisClient(8).HashGet($"Blob:Record", $"{id}");
+                    if (!value.IsNullOrEmpty)
+                    {
+                        JsonElement record = value.ToString().ToObject<JsonElement>();
+                        if (record.TryGetInt64(out blobsize))
+                        {
+                            useSize += blobsize;
+                        }
+                    }
+                }
+            }
+            else
+            {
+                await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<RecBlobFile>(queryText: "SELECT c.id,c.code,c.name,c.size,c.type FROM c where c.pk='Bloblog'", requestOptions: new QueryRequestOptions() { }))
+                {
+                    blobFiles.Add(item);
+                }
+
+                await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<RecBlobFile>(queryText: "SELECT c.id,c.code,c.name,c.size,c.type FROM c where c.pk='Bloblog'", requestOptions: new QueryRequestOptions() { }))
+                {
+                    blobFiles.Add(item);
+                }
+
+                List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"select c.id from c", "Base");
+
+                foreach (var id in schoolIds) 
+                {
+                    long blobsize = 0;
+                    RedisValue value = _azureRedis.GetRedisClient(8).HashGet($"Blob:Record", $"{id}");
+                    if (!value.IsNullOrEmpty)
+                    {
+                        JsonElement record = value.ToString().ToObject<JsonElement>();
+                        if (record.TryGetInt64(out blobsize))
+                        {
+                            useSize += blobsize;
+                        }
+                    }
+                }
+            }
+
+            var typeCount = blobFiles.GroupBy(m => new { m.type }).Select(y => new { key = y.Key.type, value = y.Count() }).ToList();
+            var areaSize = await CommonFind.FindTotals(cosmosClient, sqlSize.ToString(), "School", "Base");
+
+            return Ok(new { state = 200, areaSize, useSize, typeCount });
+
+        }
+
+        /// <summary>
+        /// 依据教师获取教师个人的记录
+        /// </summary>
+        /// <param name="cosmosClient"></param>
+        /// <param name="teacherIds"></param>
+        /// <returns></returns>
+        public async static Task<List<RecBlobFile>> GetBlobTeache(CosmosClient cosmosClient, List<string> teacherIds)
+        {
+            List<RecBlobFile> blobFiles = new();
+            if (teacherIds.Count > 0)
+            {
+                foreach (var teacherId in teacherIds)
+                {
+                    await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<RecBlobFile>(queryText: $"SELECT c.id,c.code,c.name,c.size,c.type FROM c where c.pk='Bloblog'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Bloblog-{teacherId}") }))
+                    {
+                        blobFiles.Add(item);
+                    }
+                }
+            }
+
+            return blobFiles;
+        }
+
+
+
+        /// <summary>
+        /// 空间内容信息
+        /// </summary>
+        public record RecBlobFile
+        {
+            public string id { get; set; }
+            public string code { get; set; }
+            public string name { get; set; }
+            public int size { get; set; }
+            public string type { get; set; }
+
+        }
+
+
+    }
+
+}

+ 5 - 0
TEAMModelBI/Models/MonthStartEnd.cs

@@ -10,9 +10,14 @@
 
 
     }
     }
 
 
+    /// <summary>
+    /// 教师列表
+    /// </summary>
     public class SchoolTeacherRoles
     public class SchoolTeacherRoles
     {
     {
         public string tmdId { get; set; }
         public string tmdId { get; set; }
         public string tmdName { get; set; }
         public string tmdName { get; set; }
     }
     }
+
+
 }
 }

+ 1 - 0
TEAMModelBI/TEAMModelBI.csproj

@@ -27,6 +27,7 @@
 	</ItemGroup>
 	</ItemGroup>
 
 
 	<ItemGroup>
 	<ItemGroup>
+		<Folder Include="Tool\Cosmos\StudentBank\" />
 		<Folder Include="wwwroot\" />
 		<Folder Include="wwwroot\" />
 	</ItemGroup>
 	</ItemGroup>
 	<PropertyGroup>
 	<PropertyGroup>

+ 41 - 2
TEAMModelBI/Tool/CommonFind.cs

@@ -1,6 +1,7 @@
 using Azure.Cosmos;
 using Azure.Cosmos;
 using System;
 using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
+using System.Text;
 using System.Text.Json;
 using System.Text.Json;
 using System.Threading.Tasks;
 using System.Threading.Tasks;
 using TEAMModelBI.Models;
 using TEAMModelBI.Models;
@@ -182,12 +183,12 @@ namespace TEAMModelBI.Tool
             return assistSchools;
             return assistSchools;
         }
         }
 
 
-
         /// <summary>
         /// <summary>
         /// 依据学校查询教师列表
         /// 依据学校查询教师列表
         /// </summary>
         /// </summary>
         /// <param name="cosmosClient"></param>
         /// <param name="cosmosClient"></param>
-        /// <param name="schools"></param>
+        /// <param name="schools">学校列表</param>
+        /// <param name="roles">不传默认教师角色</param>
         /// <returns></returns>
         /// <returns></returns>
         public static async Task<List<string>> FindRolesId(CosmosClient cosmosClient, List<string> schools, string roles = null)
         public static async Task<List<string>> FindRolesId(CosmosClient cosmosClient, List<string> schools, string roles = null)
         {
         {
@@ -213,5 +214,43 @@ namespace TEAMModelBI.Tool
             return teachers;
             return teachers;
         }
         }
 
 
+
+        /// <summary>
+        /// 依据学校Id集合查询 学校人数统计
+        /// </summary>
+        /// <param name="cosmosClient"></param>
+        /// <param name="container"></param>
+        /// <param name="schoolId"></param>
+        /// <param name="code"></param>
+        /// <returns></returns>
+        public static async Task<int> GetPeopleNumber(CosmosClient cosmosClient, string container, List<string> schoolId, string code,string sqlWhere = null) 
+        {
+            int totals = 0;
+            StringBuilder sqlTxt = new("select count(c.id) as totals from c ");
+            if (!string.IsNullOrEmpty(sqlWhere)) 
+            {
+                sqlTxt.Append(sqlWhere);
+            }
+
+            if (schoolId.Count > 0)
+            {
+                foreach (var id in schoolId)
+                {
+                    await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", container).GetItemQueryStreamIterator(queryText: sqlTxt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}-{id}") }))
+                    {
+                        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").GetInt32();
+                            }
+                        }
+                    }
+                }
+            }
+
+            return totals;
+        }
     }
     }
 }
 }