Browse Source

优化依据区级ID分析该去所有学校课例接口

Li 2 years ago
parent
commit
3d2d05f2cb

+ 37 - 15
TEAMModelBI/Controllers/Census/LessonSticsController.cs

@@ -19,6 +19,7 @@ using MathNet.Numerics.LinearAlgebra.Double;
 using TEAMModelBI.Tool.CosmosBank;
 using TEAMModelOS.SDK.Context.BI;
 using TEAMModelOS.SDK.Models.Service.BI;
+using TEAMModelOS.SDK.Context.Constant;
 
 namespace TEAMModelBI.Controllers.Census
 {
@@ -104,29 +105,40 @@ namespace TEAMModelBI.Controllers.Census
         }
 
         /// <summary>
-        /// 统计所有课例数量
+        /// 依据区级ID分析该去所有学校课例
         /// </summary>
+        /// <param name="jsonElement"></param>
         /// <returns></returns>
-        [HttpPost("get-total")]
-        public async Task<IActionResult> GetAllCount(JsonElement jsonElement)
+        [ProducesDefaultResponseType]
+        [HttpPost("get-schoolan")]
+        public async Task<IActionResult> GetSchoolsAn(JsonElement jsonElement)
         {
-            try
-            {
-                jsonElement.TryGetProperty("site", out JsonElement site);
+            if (!jsonElement.TryGetProperty("areaId", out JsonElement areaId)) return BadRequest();
+            jsonElement.TryGetProperty("site", out JsonElement site);
+            var (lWeekS, lWeekE) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "lastterm");
+            var (monthS, monthE) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "month");
 
-                var cosmosClient = _azureCosmos.GetCosmosClient();
-                if ($"{site}".Equals(BIConst.Global))
-                    cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
-                string lessonSql = $"select COUNT(c.id) AS totals from c where c.pk='LessonRecord'";
-                long total = await CommonFind.FindTotals(cosmosClient, lessonSql, new List<string>() { "School","Teacher" });
+            var cosmosClient = _azureCosmos.GetCosmosClient();
+            if ($"{site}".Equals(BIConst.Global))
+                cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
 
-                return Ok(new { state = 200, total });
+            List<AreaSchools> areaSchools = new();
+            await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<AreaSchools>(queryText: $"select c.id,c.name,c.picture from c where c.areaId='{areaId}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base")}))
+            {
+                areaSchools.Add(item);
             }
-            catch (Exception ex)
+            if (areaSchools.Count > 0)
             {
-                await _dingDing.SendBotMsg($"BI,{_option.Location}  /lesson/get-total \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
-                return BadRequest();
+                foreach (var item in areaSchools)
+                {
+                    item.allCnt = await CommonFind.GetSqlValueCount(cosmosClient,"School",$"select value(count(c.id)) from c ",$"LessonRecord-{item.id}");
+                    item.weekCnt = await CommonFind.GetSqlValueCount(cosmosClient, "School", $"select value(count(c.id)) from c where c.startTime >= {lWeekS} and c.startTime <= {lWeekE}", $"LessonRecord-{item.id}");
+                    item.monthCnt = await CommonFind.GetSqlValueCount(cosmosClient, "School", $"select value(count(c.id)) from c where  c.startTime >= {monthS} and c.startTime <= {monthE}", $"LessonRecord-{item.id}");
+                }
+                //areaSchools = areaSchools.Where(w => w.allCnt != 0 && w.weekCnt != 0 && w.monthCnt != 0).ToList();
             }
+
+            return Ok(new { state = RespondCode.Ok, areaSchools });
         }
 
         /// <summary>
@@ -602,6 +614,16 @@ namespace TEAMModelBI.Controllers.Census
 
 
 
+        public record AreaSchools 
+        {
+            public string id { get; set; }
+            public string name { get; set; }
+            public string picture { get; set; }
+            public int allCnt { get; set; }
+            public int weekCnt { get; set; }
+            public int monthCnt { get; set; }
+        }
+
 
         public record AllAreaInfo
         {

+ 1 - 2
TEAMModelBI/Controllers/Census/StudyController.cs

@@ -81,8 +81,7 @@ namespace TEAMModelBI.Controllers.Census
 
             foreach (var schoolId in schoolIds)
             {
-                await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Teacher")
-                 .GetItemQueryIterator<TeacherTrain>(queryText: $"select value(c) from c  ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"TeacherTrain-{schoolId}") }))
+                await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<TeacherTrain>(queryText: $"select value(c) from c  ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"TeacherTrain-{schoolId}") }))
                 {
                     trains.Add(item);
                 }

+ 23 - 19
TEAMModelBI/Tool/CommonFind.cs

@@ -292,7 +292,7 @@ namespace TEAMModelBI.Tool
         }
 
         /// <summary>
-        /// 通过SQL 语句返回实体信息
+        /// 通过SQL 语句返回实体信息  带有Ids
         /// </summary>
         /// <typeparam name="T"></typeparam>
         /// <param name="cosmosClient"></param>
@@ -333,33 +333,37 @@ namespace TEAMModelBI.Tool
         /// <param name="containers"></param>
         /// <param name="sqlTxt"></param>
         /// <param name="code"></param>
-        /// <param name="ids"></param>
         /// <returns></returns>
-        public static async Task<List<T>> GetObject<T>(CosmosClient cosmosClient, List<string> containers, string sqlTxt, string code = null, List<string> ids = null)
+        public static async Task<List<T>> GetObject<T>(CosmosClient cosmosClient, List<string> containers, string sqlTxt, string code = null)
         {
             List<T> temps = new();
             foreach (var container in containers)
             {
-                if (ids.Count > 0)
-                {
-                    foreach (var item in ids)
-                    {
-                        string sql = $"{sqlTxt} c.id='{item}'";
-                        await foreach (var items in cosmosClient.GetContainer("TEAMModelOS", container).GetItemQueryIterator<T>(queryText: sql, requestOptions: string.IsNullOrEmpty(code) ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey(code) }))
-                        {
-                            temps.Add(items);
-                        }
-                    }
-                }
-                else
+                await foreach (var items in cosmosClient.GetContainer("TEAMModelOS", container).GetItemQueryIterator<T>(queryText: sqlTxt, requestOptions: string.IsNullOrEmpty(code) ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey(code) }))
                 {
-                    await foreach (var items in cosmosClient.GetContainer("TEAMModelOS", container).GetItemQueryIterator<T>(queryText: sqlTxt, requestOptions: string.IsNullOrEmpty(code) ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey(code) }))
-                    {
-                        temps.Add(items);
-                    }
+                    temps.Add(items);
                 }
             }
             return temps;
         }
+
+        /// <summary>
+        /// 通过SQL 语句返回实体信息
+        /// </summary>
+        /// <typeparam name="T"></typeparam>
+        /// <param name="cosmosClient"></param>
+        /// <param name="containers"></param>
+        /// <param name="sqlTxt"></param>
+        /// <param name="code"></param>
+        /// <returns></returns>
+        public static async Task<List<T>> GetObject<T>(CosmosClient cosmosClient, string containers, string sqlTxt, string code = null)
+        {
+            List<T> temps = new();
+            await foreach (var items in cosmosClient.GetContainer("TEAMModelOS", containers).GetItemQueryIterator<T>(queryText: sqlTxt, requestOptions: string.IsNullOrEmpty(code) ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey(code) }))
+            {
+                temps.Add(items);
+            }
+            return temps;
+        }
     }
 }

+ 11 - 0
TEAMModelBI/Tool/TimeHelper.cs

@@ -261,6 +261,11 @@ namespace TEAMModelBI.Tool
                 switch (type)
                 {
                     //今年开始、结束
+                    case "yearMonth":
+                        tempStrart = new DateTime(year, 1, 1);
+                        tempEnd = new DateTime(year, month, DateTime.DaysInMonth(year, month), 23, 59, 59);
+                        break;
+                    //今年开始、结束
                     case "year":
                         tempStrart = new DateTime(year, 1, 1);
                         tempEnd = new DateTime(year, 12, DateTime.DaysInMonth(year, 12), 23, 59, 59);
@@ -326,6 +331,12 @@ namespace TEAMModelBI.Tool
                             }
                         }
 
+                        break;
+                    //本月当天
+                    case "monthDay":
+                        tempStrart = new DateTime(year, month, 1);
+                        tempEnd = new DateTime(year, month, day, 23, 59, 59);
+
                         break;
                     //本月
                     case "month":