Bläddra i källkod

优化所有区级接口代码,和添加课例和活动接口

Li 3 år sedan
förälder
incheckning
1259ffe11d

+ 22 - 21
TEAMModelBI/Controllers/Census/ActivitySticsController.cs

@@ -15,6 +15,7 @@ using System.Text;
 using TEAMModelOS.SDK.Extension;
 using TEAMModelBI.Models;
 using TEAMModelOS.SDK;
+using TEAMModelBI.Tool.CosmosBank;
 
 namespace TEAMModelBI.Controllers.Census
 {
@@ -321,7 +322,7 @@ namespace TEAMModelBI.Controllers.Census
             long homeworkAreaCount = 0;  //作业活动
             long weekActivity = 0; //周活动数量
             long termActivity=0;   //学期活动
-            int Basics = 0; //基础版数
+            int basics = 0; //基础版数
             int standard = 0; //标准版数
             int major = 0;   //专业版数
             int geCount = 0; //普教
@@ -331,7 +332,6 @@ namespace TEAMModelBI.Controllers.Census
             List<SchoolLesson> schoolLessons = new();  //学校课例集合 
             List<SchoolInfo> schoolInfos = new();
 
-
             int totalTime = 0;
             foreach (var school in schools)
             {
@@ -369,7 +369,7 @@ namespace TEAMModelBI.Controllers.Census
                     major += 1;
                 else if (school.size >= 300 && school.scale >= 500)
                     standard += 1;
-                else Basics += 1;
+                else basics += 1;
 
                 //课例
                 int lessCount = await CommonFind.FindTotals(cosmosClient, "select count(c.id) totals from c", "School", $"LessonRecord-{school.id}");
@@ -392,7 +392,7 @@ namespace TEAMModelBI.Controllers.Census
                     trainCount = tempCount 
                 };
 
-                ActivityCount tempActivity = new ActivityCount() { id = school.id, name = school.name != null ? school.name : school.id };
+                ActivityCount tempActivity = new() { id = school.id, name = school.name != null ? school.name : school.id };
                 foreach (var type in types)
                 {
                     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" });
@@ -460,6 +460,12 @@ namespace TEAMModelBI.Controllers.Census
             int tecCount = 0;    //教师数量
             int scCount = 0;    //学校数量
 
+            int basics = 0; //基础版数
+            int standard = 0; //标准版数
+            int major = 0;   //专业版数
+            long resourceCount = 0; //累计资源
+            long totalTime = 0; //总学时
+
             foreach (var area in areaInfos) 
             {
                 List<RecSchool> recSchools = new();
@@ -471,7 +477,11 @@ namespace TEAMModelBI.Controllers.Census
                 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;
-
+                //统计服务
+                var (tempb, temps, tempm) = await ProductWay.GetVersionCount(cosmosClient, recSchools);
+                basics += tempb;
+                standard += temps;
+                major += tempm;
                 int tempCount = await CommonFind.GetPeopleNumber(cosmosClient, "School", recSchools?.Select(x => x.id).ToList(), "Teacher");
 
                 tecCount += tempCount;
@@ -496,14 +506,17 @@ namespace TEAMModelBI.Controllers.Census
                 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" });
+
+            allLess = await CommonFind.FindTotals(cosmosClient, "select count(c.id) as totals from c where c.pk='LessonRecord'", new List<string>() { "School", "Teacher" });
             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, termActivity, weekActivity, areaInfos });
+            totalTime = await CommonFind.FindTotals(cosmosClient, "select sum(c.totalTime) as totals from c where c.pk='TeacherTrain'", new List<string>() { "Teacher" });
+            resourceCount = await CommonFind.FindTotals(cosmosClient, "select count(c.id) as totals from c where c.pk='Bloblog'", new List<string>() { "School", "Teacher" });
+
+            return Ok(new { state = 200, areaCount = areaInfos.Count, allSize, heCount, geCount, oeCount, allLess, termLess, weekLess, allActivity, termActivity, weekActivity, resourceCount, basics, standard, major, areaInfos });
         }
-                
+
 
         /// <summary>
         /// 依据活动Id查询活动详情信息  数据管理工具——查询工具
@@ -564,18 +577,6 @@ namespace TEAMModelBI.Controllers.Census
             public int stuCount { get; set; }
         }
 
-        /// <summary>
-        /// 区级下学校信息显示
-        /// </summary>
-        public record RecSchool
-        {
-            public string id { get; set; }
-            public string name { get; set; }
-            public string picture { get; set; }
-            public int type { get; set; }
-            public int size { get; set; }
-            public int scale { get; set; }
-        }
 
         private class SchoolInfo
         {

+ 64 - 7
TEAMModelBI/Controllers/Census/LessonSticsController.cs

@@ -16,6 +16,7 @@ using TEAMModelOS.SDK.Extension;
 using System.Text;
 using TEAMModelBI.Tool;
 using MathNet.Numerics.LinearAlgebra.Double;
+using TEAMModelBI.Tool.CosmosBank;
 
 namespace TEAMModelBI.Controllers.Census
 {
@@ -27,6 +28,7 @@ namespace TEAMModelBI.Controllers.Census
         private readonly AzureStorageFactory _azureStorage;
         private readonly DingDing _dingDing;
         private readonly Option _option;
+        public readonly List<string> types = new() { "Exam", "Survey", "Vote", "Homework" };
 
         public LessonSticsController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureFactory, DingDing dingDing, IOptionsSnapshot<Option> option)
         {
@@ -325,11 +327,17 @@ namespace TEAMModelBI.Controllers.Census
         [HttpPost("get-areacount")]
         public async Task<IActionResult> GetAreaCount(JsonElement jsonElement) 
         {
-            if (!jsonElement.TryGetProperty("areaId", out JsonElement areaId)) return BadRequest();
+            if(!jsonElement.TryGetProperty("areaId", out JsonElement areaId)) return BadRequest();
             List<string> schools = new();
             var cosmosClient = _azureCosmos.GetCosmosClient();
 
-            schools = await CommonFind.FindSchoolIds(cosmosClient, $"select c.id from c where c.areaId='{areaId}'", "Base");
+            StringBuilder scSqlTxt = new("select c.id from c");
+            if (!string.IsNullOrEmpty($"{areaId}"))
+            {
+                scSqlTxt.Append($" where c.areaId='{areaId}'");
+            }
+
+            schools = await CommonFind.FindSchoolIds(cosmosClient, scSqlTxt.ToString(), "Base");
 
             //所有的课程记录
             List<LessonRecord> records = new();
@@ -376,6 +384,7 @@ namespace TEAMModelBI.Controllers.Census
             return Ok(new { state = 200, lessonCount = records.Count, teachCount, dayCount, weekCount, monthCount, termCount });
         }
 
+
         /// <summary>
         /// 统计区级一年每周的课例数据趋势
         /// </summary>
@@ -385,18 +394,28 @@ namespace TEAMModelBI.Controllers.Census
         [HttpPost("get-weekcount")]
         public async Task<IActionResult> GetWeekCount(JsonElement jsonElement) 
         {
-            if (!jsonElement.TryGetProperty("areaId", out JsonElement areaId)) return BadRequest();
+            jsonElement.TryGetProperty("areaId", out JsonElement areaId);
 
             Dictionary<int, double> weeks = new();
             var cosmosClient = _azureCosmos.GetCosmosClient();
 
             int year = DateTimeOffset.UtcNow.Year;
             int dayOfweek = (int)DateTimeOffset.Parse($"{year}-1-1").DayOfWeek;
+            int currentTime = DateTimeOffset.UtcNow.DayOfYear / 7 + 1;
+            int currentTime1 = DateTimeOffset.UtcNow.DayOfYear / 7;
+
             var sqlTxts = "select value(c) from c";
             List<LessonCount> scount = new();
             List<LessonCount> tcount = new();
 
-            List<string> schools = await CommonFind.FindSchoolIds(cosmosClient, $"select c.id from c where c.areaId='{areaId}'", "Base");
+            StringBuilder sqlTxt = new("select c.id from c");
+            if (!string.IsNullOrEmpty($"{areaId}"))
+            {
+                sqlTxt.Append($" where c.areaId='{areaId}'");
+            }
+
+            List<string> schools = await CommonFind.FindSchoolIds(cosmosClient, sqlTxt.ToString(), "Base");
+
             foreach (var sId in schools)
             {
                 await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<LessonCount>(queryText:sqlTxts,requestOptions:new QueryRequestOptions() { PartitionKey =new PartitionKey($"LessonCount-{sId}-{year}")}))
@@ -448,11 +467,11 @@ namespace TEAMModelBI.Controllers.Census
                 int sweeks = days / 7;
                 //查询天数
                 int dayYear = 0;
-                if (sweeks > 0)
+                if (currentTime > 0)
                 {
-                    for (int i = 1; i <= sweeks; i++)
+                    for (int i = 0; i <= currentTime; i++)
                     {
-                        if (i == 1)
+                        if (i == 0)
                         {
                             var bsum = bmatrix.SubMatrix(dayYear, dd, 0, bmatrix.ColumnCount).ColumnSums().Sum();
                             dayYear += dd;
@@ -478,6 +497,35 @@ namespace TEAMModelBI.Controllers.Census
             return Ok(new { state = 200, weeks });
         }
 
+        /// <summary>
+        /// 统计所有区级的课例和活动
+        /// </summary>
+        /// <returns></returns>
+        [HttpPost("get-allarea")]
+        public async Task<IActionResult> GetAllArea()
+        {
+            var cosmosClient = _azureCosmos.GetCosmosClient();
+
+            List<AllAreaInfo> areaInfos = new();
+            await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Normal").GetItemQueryIterator<AllAreaInfo>(queryText: $"select c.id,c.name,c.standard,c.standardName from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base-Area") }))
+            {
+                areaInfos.Add(item);
+            }
+
+            foreach (var area in areaInfos)
+            {
+                List<string> schooId = await CommonFind.FindSchoolIds(cosmosClient,$"select c.id from c where c.areaId='{area.id}'","Base");
+                List<string> tecId = await CommonFind.FindRolesId(cosmosClient, schooId);
+
+                area.lessCount = await LessonStatisWay.GetAll(cosmosClient, schooId, tecId);
+                area.activityCount = await ActivityWay.GetAll(cosmosClient, schooId, tecId);
+            }
+
+
+            return Ok(new { state = 200 , areaInfos });
+        }
+
+
         /// <summary>
         /// 依据课例Id获取课例详情   数据管理工具——查询工具
         /// </summary>
@@ -505,7 +553,16 @@ namespace TEAMModelBI.Controllers.Census
 
 
 
+        public record AllAreaInfo
+        {
+            public string id { get; set; }
+            public string name { get; set; }
+            public string standard { get; set; }
+            public string standardName { get; set; }
+            public int lessCount { get; set; }
+            public int activityCount { get; set; }
 
+        }
 
         public record SchoolLen 
         {

+ 13 - 0
TEAMModelBI/Models/MonthStartEnd.cs

@@ -19,5 +19,18 @@
         public string tmdName { get; set; }
     }
 
+    /// <summary>
+    /// 学校信息显示
+    /// </summary>
+    public record RecSchool
+    {
+        public string id { get; set; }
+        public string name { get; set; }
+        public string picture { get; set; }
+        public int type { get; set; }
+        public int size { get; set; }
+        public int scale { get; set; }
+    }
+
 
 }

+ 0 - 1
TEAMModelBI/TEAMModelBI.csproj

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

+ 64 - 0
TEAMModelBI/Tool/CosmosBank/ActivityWay.cs

@@ -0,0 +1,64 @@
+using Azure.Cosmos;
+using System.Collections.Generic;
+using System.Text.Json;
+using System.Threading.Tasks;
+
+namespace TEAMModelBI.Tool.CosmosBank
+{
+    public class ActivityWay
+    {
+        public static List<string> types = new() { "Exam", "Survey", "Vote", "Homework" };
+
+        /// <summary>
+        /// 依据学校Id、教师Id统计活动总数
+        /// </summary>
+        /// <param name="cosmosClient"></param>
+        /// <param name="scIds"></param>
+        /// <param name="tecIds"></param>
+        /// <returns></returns>
+        public async static Task<int> GetAll(CosmosClient cosmosClient, List<string> scIds = null, List<string> tecIds = null)
+        {
+            int totals = 0;
+            string sqlTxt = "select count(c.id) as totals from c";
+            foreach (string type in types)
+            {
+                if (scIds.Count > 0)
+                {
+                    foreach (string sc in scIds)
+                    {
+                        await foreach (var itemTeac in cosmosClient.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{type}-{sc}") }))
+                        {
+                            using var json = await JsonDocument.ParseAsync(itemTeac.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();
+                                }
+                            }
+                        }
+                    }
+                }
+
+                if (tecIds.Count > 0)
+                {
+                    foreach (string sc in scIds)
+                    {
+                        await foreach (var itemTeac in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryStreamIterator(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{type}-{sc}") }))
+                        {
+                            using var json = await JsonDocument.ParseAsync(itemTeac.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;
+        }
+    }
+}

+ 61 - 0
TEAMModelBI/Tool/CosmosBank/LessonStatisWay.cs

@@ -0,0 +1,61 @@
+using Azure.Cosmos;
+using System.Collections.Generic;
+using System.Text.Json;
+using System.Threading.Tasks;
+
+namespace TEAMModelBI.Tool.CosmosBank
+{
+    public class LessonStatisWay
+    {
+        /// <summary>
+        /// 依据学校Id、教师Id统计课例总数
+        /// </summary>
+        /// <param name="cosmosClient"></param>
+        /// <param name="scIds"></param>
+        /// <param name="tecIds"></param>
+        /// <returns></returns>
+        public async static Task<int> GetAll(CosmosClient cosmosClient, List<string> scIds = null, List<string> tecIds = null)
+        {
+            int totals = 0;
+            string sqlTxt = "select count(c.id) as totals from c";
+            if (scIds.Count > 0)
+            {
+                foreach (string sc in scIds)
+                {
+                    await foreach (var itemTeac in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonRecord-{sc}") }))
+                    {
+                        using var json = await JsonDocument.ParseAsync(itemTeac.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();
+                            }
+                        }
+                    }
+                }
+            }
+            if (tecIds.Count > 0)
+            {
+                foreach (string sc in scIds)
+                {
+                    await foreach (var itemTeac in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryStreamIterator(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonRecord-{sc}") }))
+                    {
+                        using var json = await JsonDocument.ParseAsync(itemTeac.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;
+        }
+
+
+    }
+}

+ 51 - 0
TEAMModelBI/Tool/CosmosBank/ProductWay.cs

@@ -0,0 +1,51 @@
+using Azure.Cosmos;
+using System.Collections.Generic;
+using System.Text.Json;
+using System.Threading.Tasks;
+using TEAMModelBI.Models;
+
+namespace TEAMModelBI.Tool.CosmosBank
+{
+    public class ProductWay
+    {
+        /// <summary>
+        /// 依据学校信息统计版本数量
+        /// </summary>
+        /// <param name="cosmosClient"></param>
+        /// <param name="scIds"></param>
+        /// <returns></returns>
+        public async static Task<(int bCount, int sCount, int mCount)> GetVersionCount(CosmosClient cosmosClient, List<RecSchool> scIds) 
+        {
+            int totals = 0;
+
+            int basics = 0; //基础版数
+            int standard = 0; //标准版数
+            int major = 0;   //专业版数
+            foreach (var sc in scIds) 
+            {
+                string sqlTxt = $"select array_length(c.service) as totals from c where c.id='{sc.id}'";
+                await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryText: sqlTxt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("ProductSum") }))
+                {
+                    using var json = await JsonDocument.ParseAsync(item.ContentStream);
+                    if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetInt16() > 0)
+                    {
+                        foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
+                        {
+                            totals += obj.GetProperty("totals").GetInt32();
+                        }
+                    }
+                }
+
+                if (totals > 0)
+                    major += 1;
+                else if (sc.size >= 300 && sc.scale >= 500)
+                    standard += 1;
+                else basics += 1;
+
+            }
+
+            return (basics, standard, major);
+        }
+
+    }
+}