Browse Source

优化统计区域接口

Li 2 years ago
parent
commit
07301d38ea
1 changed files with 41 additions and 39 deletions
  1. 41 39
      TEAMModelBI/Controllers/Census/ActivitySticsController.cs

+ 41 - 39
TEAMModelBI/Controllers/Census/ActivitySticsController.cs

@@ -47,25 +47,25 @@ namespace TEAMModelBI.Controllers.Census
         /// <returns></returns>
         [ProducesDefaultResponseType]
         [HttpPost("get-count")]
-        public async Task<IActionResult> GetCount(JsonElement jsonElement) 
+        public async Task<IActionResult> GetCount(JsonElement jsonElement)
         {
             jsonElement.TryGetProperty("tmdId", out JsonElement tmdId);
-            if(!jsonElement.TryGetProperty("term", out JsonElement term)) return BadRequest();
+            if (!jsonElement.TryGetProperty("term", out JsonElement term)) return BadRequest();
             jsonElement.TryGetProperty("site", out JsonElement site);
             long start = 0, end = 0;
-            if (bool.Parse($"{term}") == true) 
+            if (bool.Parse($"{term}") == true)
             {
                 (start, end) = TimeHelper.GetTermStartOrEnd(DateTime.Now);
             }
             var cosmosClient = _azureCosmos.GetCosmosClient();
             if ($"{site}".Equals(BIConst.Global))
                 cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
-             List<object> activityCount = new List<object>();
+            List<object> activityCount = new List<object>();
 
             if (!string.IsNullOrEmpty($"{tmdId}"))
             {
                 List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"{tmdId}");
-                
+
                 foreach (var itemId in schoolIds)
                 {
                     School school = new();
@@ -92,7 +92,7 @@ namespace TEAMModelBI.Controllers.Census
                     activityCount.Add(tempCount);
                 }
             }
-            else 
+            else
             {
                 foreach (var type in StaticValue.activityTypes)
                 {
@@ -116,7 +116,7 @@ namespace TEAMModelBI.Controllers.Census
         /// </summary>
         /// <returns></returns>
         [HttpPost("get-allactivity")]
-        public async Task<IActionResult> GetAllActivity(JsonElement jsonElement) 
+        public async Task<IActionResult> GetAllActivity(JsonElement jsonElement)
         {
             try
             {
@@ -128,7 +128,7 @@ namespace TEAMModelBI.Controllers.Census
                 foreach (var type in StaticValue.activityTypes)
                 {
                     string querySql = $"SELECT Count(c.id) as totals FROM c where c.pk='{type}'  ";
-                    
+
                     long totals = await CommonFind.FindTotals(cosmosClient, querySql, new List<string>() { "Common" });
 
                     KeyValuePair<string, long> valuePair = new KeyValuePair<string, long>(type, totals);
@@ -192,7 +192,7 @@ namespace TEAMModelBI.Controllers.Census
         /// <param name="jsonElement"></param>
         /// <returns></returns>
         [HttpPost("get-termactivity")]
-        public async Task<IActionResult> GetTermActivity(JsonElement jsonElement) 
+        public async Task<IActionResult> GetTermActivity(JsonElement jsonElement)
         {
             jsonElement.TryGetProperty("tmdId", out JsonElement tmdId);
             jsonElement.TryGetProperty("site", out JsonElement site);
@@ -217,7 +217,7 @@ namespace TEAMModelBI.Controllers.Census
                     }
                     ActivityCount activityCount = new ActivityCount() { id = schoolId, name = school.name != null ? school.name : schoolId };
 
-                    foreach (var type in StaticValue.activityTypes) 
+                    foreach (var type in StaticValue.activityTypes)
                     {
                         string activitySql = $"SELECT COUNT(c.id) AS totals  FROM c WHERE c.pk='{type}' AND c.school='{school}' and c.createTime >= {start} and c.createTime <= {end}";
                         long totals = await CommonFind.FindTotals(cosmosClient, activitySql, new List<string>() { "Common" });
@@ -251,7 +251,7 @@ namespace TEAMModelBI.Controllers.Census
         /// <param name="jsonElement"></param>
         /// <returns></returns>
         [HttpPost("get-area")]
-        public async Task<IActionResult> GetAreaActovoty(JsonElement jsonElement) 
+        public async Task<IActionResult> GetAreaActovoty(JsonElement jsonElement)
         {
             if (!jsonElement.TryGetProperty("areaId", out JsonElement areaId)) return BadRequest();
             jsonElement.TryGetProperty("site", out JsonElement site);
@@ -277,7 +277,7 @@ namespace TEAMModelBI.Controllers.Census
                 ActivityCount tempCount = new ActivityCount() { id = school.id, name = school.name != null ? school.name : school.id };
                 foreach (var type in StaticValue.activityTypes)
                 {
-                    StringBuilder sqlTxt = new($"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" });
 
                     switch (type)
@@ -286,13 +286,13 @@ namespace TEAMModelBI.Controllers.Census
                             exemAreaCount += totals;
                             break;
                         case "Survey":
-                             surveyAreaCount += totals;
+                            surveyAreaCount += totals;
                             break;
                         case "Vote":
-                             voteAreaCount += totals;
+                            voteAreaCount += totals;
                             break;
                         case "Homework":
-                             homeworkAreaCount += totals;
+                            homeworkAreaCount += totals;
                             break;
                     }
 
@@ -310,7 +310,7 @@ namespace TEAMModelBI.Controllers.Census
         /// <param name="jsonElement"></param>
         /// <returns></returns>
         [HttpPost("get-areastics")]
-        public async Task<IActionResult> GetAreaStics(JsonElement jsonElement) 
+        public async Task<IActionResult> GetAreaStics(JsonElement jsonElement)
         {
             if (!jsonElement.TryGetProperty("areaId", out JsonElement areaId)) return BadRequest();
             jsonElement.TryGetProperty("site", out JsonElement site);
@@ -335,7 +335,7 @@ namespace TEAMModelBI.Controllers.Census
             {
                 schools.Add(item);
             }
-            
+
             int countArea = 0;//区级人员
             int appraiseArea = 0;//区级评审人员
             int areaSize = 0;   //区级空间
@@ -344,7 +344,7 @@ namespace TEAMModelBI.Controllers.Census
             long voteAreaCount = 0;   //投票活动
             long homeworkAreaCount = 0;  //作业活动
             long weekActivity = 0; //周活动数量
-            long termActivity=0;   //学期活动
+            long termActivity = 0;   //学期活动
             int basics = 0; //基础版数
             int standard = 0; //标准版数
             int major = 0;   //专业版数
@@ -359,7 +359,7 @@ namespace TEAMModelBI.Controllers.Census
             double teachCount = 0;    //课例教师
 
             List<SchoolLesson> schoolLessons = new();  //学校课例集合 
-            List<SchoolInfo> schoolInfos = new();            
+            List<SchoolInfo> schoolInfos = new();
             List<LessonRecord> records = new();//所有的课程记录
 
             List<string> scIds = schools.Select(x => x.id).ToList();
@@ -389,14 +389,14 @@ namespace TEAMModelBI.Controllers.Census
                 }
 
                 //查询学校的总学时
-                totalTime += await CommonFind.FindTotals(cosmosClient, "SELECT sum(c.totalTime) as totals FROM c", "Teacher", $"TeacherTrain-{school.id}");
+                totalTime += await CommonFind.GetSqlValueCount(cosmosClient, "Teacher", "SELECT value(sum(c.totalTime)) FROM c", $"TeacherTrain-{school.id}");
                 //查询学校参训人数
-                int tempCount = await CommonFind.FindTotals(cosmosClient, $"select array_length(c.members) as totals from c where c.type='yxtrain'", "School", $"GroupList-{school.id}");
+                int tempCount = await CommonFind.GetSqlValueCount(cosmosClient, "School", $"select value(array_length(c.members)) from c where c.type='yxtrain'", $"GroupList-{school.id}");
                 //学校学生人数
-                long stuCount = await CommonFind.FindTotals(cosmosClient, $"select count(c.id) as totals from c", "Student", $"Base-{school.id}");
+                long stuCount = await CommonFind.GetSqlValueCount(cosmosClient, "Student", $"select value(count(c.id)) from c", $"Base-{school.id}");
 
                 //查询是否有服务
-                int serCount = await CommonFind.FindTotals(cosmosClient, $"select array_length(c.service) as totals from c where c.id='{school.id}'", "School", "ProductSum");
+                int serCount = await CommonFind.GetSqlValueCount(cosmosClient, "School", $"select value(array_length(c.service)) from c where c.id='{school.id}'", "ProductSum");
                 if (serCount > 0)
                     major += 1;
                 else if (school.size >= 300 && school.scale >= 500)
@@ -410,7 +410,7 @@ namespace TEAMModelBI.Controllers.Census
                 }
 
                 //课例
-                int lessCount = await CommonFind.FindTotals(cosmosClient, "select count(c.id) totals from c", "School", $"LessonRecord-{school.id}");
+                int lessCount = await CommonFind.GetSqlValueCount(cosmosClient, "School", "select value(count(c.id)) from c", $"LessonRecord-{school.id}");
                 SchoolLesson schoolLesson = new()
                 {
                     id = school.id,
@@ -419,27 +419,28 @@ namespace TEAMModelBI.Controllers.Census
                     count = lessCount
                 };
 
-                SchoolInfo schoolInfo = new() { 
-                    id = school.id, 
+                SchoolInfo schoolInfo = new()
+                {
+                    id = school.id,
                     name = school.name,
                     picture = school.picture,
                     teacherCount = count,
-                    studentCount = stuCount, 
-                    appraiseCount = appraise, 
-                    trainCount = tempCount 
+                    studentCount = stuCount,
+                    appraiseCount = appraise,
+                    trainCount = tempCount
                 };
 
                 ActivityCount tempActivity = new() { id = school.id, name = school.name != null ? school.name : school.id };
                 foreach (var type in StaticValue.activityTypes)
                 {
-                    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" });
+                    long totals = await CommonFind.GetSqlValueCount(cosmosClient, "Common", $"select value(COUNT(c.id)) from c where c.pk='{type}' and c.school='{school.id}' ");
 
-                    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 weekActCount = await CommonFind.FindTotals(cosmosClient, weekSql, new List<string>() { "Common" });
+                    string weekSql = $"select value(count(c.id)) from c where c.pk='{type}' and c.school='{school.id}' and c.createTime>={weekStart}   and c.createTime<={weekEnd}";
+                    long weekActCount = await CommonFind.GetSqlValueCount(cosmosClient, "Common", weekSql);
                     weekActivity += weekActCount;
 
-                    string termSql = $"select count(c.id) as totals from c where c.pk='{type}' and c.school='{school.id}' and c.createTime>={termStart}   and c.createTime<={termEnd}";
-                    long termActCount = await CommonFind.FindTotals(cosmosClient, termSql, new List<string>() { "Common" });
+                    string termSql = $"select value(count(c.id)) from c where c.pk='{type}' and c.school='{school.id}' and c.createTime>={termStart}   and c.createTime<={termEnd}";
+                    long termActCount = await CommonFind.GetSqlValueCount(cosmosClient, "Common", termSql);
                     termActivity += termActCount;
 
                     switch (type)
@@ -487,6 +488,7 @@ namespace TEAMModelBI.Controllers.Census
             }
 
             return Ok(new { state = 200, schoolCount = schools.Count, countArea, weekActivity, termActivity, totalTime, appraiseArea, examAreaCount, surveyAreaCount, voteAreaCount, homeworkAreaCount, major, standard, basics, oeCount, dayLess, weekLess, monthLess, termLess, teachCount, allLess = records.Count, schools = schoolInfos, schoolLessons });
+
         }
 
         /// <summary>
@@ -535,10 +537,10 @@ namespace TEAMModelBI.Controllers.Census
             stuCount = await CommonFind.FindTotals(cosmosClient, $"SELECT count(c.id) as totals FROM c ", "Student", "Base");
             allSize = await CommonFind.FindTotals(cosmosClient, $"SELECT sum(c.size) as totals FROM c ", "School", "Base");
 
-            foreach (var area in areaInfos) 
+            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") })) 
+                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);
                 }
@@ -567,7 +569,7 @@ namespace TEAMModelBI.Controllers.Census
                 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" });
+                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} ";
                 termActivity += await CommonFind.FindTotals(cosmosClient, termSql, new List<string> { "Common" });
@@ -578,7 +580,7 @@ namespace TEAMModelBI.Controllers.Census
 
             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" });
+            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" });
 
             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" });
@@ -645,7 +647,7 @@ namespace TEAMModelBI.Controllers.Census
             public string standard { get; set; }
             public string standardName { get; set; }
             public int schoolCount { get; set; }
-            public int techCount { get; set;}
+            public int techCount { get; set; }
             public int stuCount { get; set; }
         }