Ver Fonte

BI统计新数据结构,和单个学校查询信息。待完善没有数据的更新操作

Li há 2 anos atrás
pai
commit
2b3e46349b

Diff do ficheiro suprimidas por serem muito extensas
+ 22 - 8
TEAMModelBI/Controllers/BITest/TestController.cs


+ 185 - 3
TEAMModelBI/Controllers/Census/SchoolController.cs

@@ -2,9 +2,11 @@
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
 using Microsoft.Extensions.Options;
+using NUnit.Framework.Constraints;
 using Pipelines.Sockets.Unofficial.Arenas;
 using System;
 using System.Collections.Generic;
+using System.Diagnostics;
 using System.Linq;
 using System.Text;
 using System.Text.Json;
@@ -497,15 +499,111 @@ namespace TEAMModelBI.Controllers.Census
             if (!jsonElement.TryGetProperty("scId", out JsonElement scId)) return BadRequest();
             var cosmosClinet = _azureCosmos.GetCosmosClient();
 
-            StatsInfo statsInfo = new();
-            var resStats = cosmosClinet.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{scId}", new PartitionKey(""));
+            School school = new();
+            ScStats scStats = new();
 
+            var respSc = await cosmosClinet.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{scId}", new PartitionKey($"base-{scId}"));
+            if (respSc.Status == 200)
+            {
+                using var fileJson = await JsonDocument.ParseAsync(respSc.ContentStream);
+                school = fileJson.ToObject<School>();
+            }
+            else
+                return Ok(new { state = RespondCode.NotFound, msg = $"未找到id:{scId};相关的学校,请检查" });
+
+            try
+            {
+
+                StatsInfo statsInfo = new();
+                var scDataStats = await cosmosClinet.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{scId}", new PartitionKey("Stats"));
+                if (scDataStats.Status == 200)
+                {
+                    using var fileJson = await JsonDocument.ParseAsync(scDataStats.ContentStream);
+                    statsInfo = fileJson.ToObject<StatsInfo>();
+                }
+
+                bool isnew = false;
+                DateTimeOffset dateTime = DateTimeOffset.UtcNow;
+                long now = dateTime.ToUnixTimeMilliseconds();
+                if (!string.IsNullOrEmpty(statsInfo.id)) 
+                {
+                    scStats.id= statsInfo.id;
+                    scStats.name = statsInfo.name;
+                    scStats.picture = statsInfo.picture;
+                    scStats.tch = statsInfo.tch;
+                    scStats.dayTch = statsInfo.dayTch;
+                    scStats.weekTch = statsInfo.weekTch;
+                    scStats.monthTch = statsInfo.monthTch;
+                    scStats.stu = statsInfo.stu;
+                    scStats.dayStu = statsInfo.dayStu;
+                    scStats.weekStu = statsInfo.weekStu;
+                    scStats.monthStu = statsInfo.monthStu;
+                    scStats.room = statsInfo.room;
+                    scStats.witRoom = statsInfo.witRoom;
+                    scStats.size = statsInfo.size;
+                    scStats.scCreateTime = statsInfo.scCreateTime;
+                    scStats.upTime = statsInfo.upTime;
+                    scStats.lessStats.open = statsInfo.lesson.open;
+                    scStats.lessStats.less = statsInfo.lesson.less;
+                    scStats.lessStats.lastDay = statsInfo.lesson.lastDay;
+                    scStats.lessStats.day = statsInfo.lesson.day;
+                    scStats.lessStats.lastWeek = statsInfo.lesson.lastWeek;
+                    scStats.lessStats.week = statsInfo.lesson.week;
+                    scStats.lessStats.lastTerm = statsInfo.lesson.lastTerm;
+                    scStats.lessStats.term = statsInfo.lesson.term;
+                    scStats.lessStats.lastDayInter = statsInfo.lesson.lastDayInter;
+                    scStats.lessStats.dayInter = statsInfo.lesson.dayInter;
+                    scStats.lessStats.lastMonthInter = statsInfo.lesson.lastMonthInter;
+                    scStats.lessStats.monthInter = statsInfo.lesson.monthInter;
+                    scStats.lessStats.lastYearInter = statsInfo.lesson.lastYearInter;
+                    scStats.lessStats.yearInter= TimeHelper.GetYearMonth(statsInfo.lesson.yearInter, dateTime.Year, dateTime.Month);
+                    scStats.lessStats.LastYear = TimeHelper.GetYearMonth(statsInfo.lesson.LastYear, dateTime.Year, dateTime.Month);
+                    scStats.lessStats.year = TimeHelper.GetYearMonth(statsInfo.lesson.year, dateTime.Year, dateTime.Month);
+                    scStats.actStats.cnt = statsInfo.activity.cnt;
+                    scStats.actStats.exam = statsInfo.activity.exam;
+                    scStats.actStats.survey = statsInfo.activity.survey;
+                    scStats.actStats.vote = statsInfo.activity.vote;
+                    scStats.actStats.homework = statsInfo.activity.homework;
+                    scStats.actStats.lastDay = statsInfo.activity.lastDay;
+                    scStats.actStats.dayCnt = statsInfo.activity.dayCnt;
+                    scStats.actStats.lastWeek = statsInfo.activity.lastWeek;
+                    scStats.actStats.week = statsInfo.activity.week;
+                    scStats.actStats.lastTerm = statsInfo.activity.lastTerm;
+                    scStats.actStats.term = statsInfo.activity.term;
+                    scStats.actStats.LastYear = TimeHelper.GetYearMonth(statsInfo.activity.LastYear ,dateTime.Year,dateTime.Month);
+                    scStats.actStats.year = TimeHelper.GetYearMonth(statsInfo.activity.year, dateTime.Year, dateTime.Month);
+                    scStats.srStats.learnTime = statsInfo.study.learnTime;
+                    scStats.srStats.onlineTime = statsInfo.study.onlineTime;
+                    scStats.srStats.offlineTime = statsInfo.study.offlineTime;
+                    scStats.srStats.classTime = statsInfo.study.classTime;
+                    scStats.srStats.currency = statsInfo.study.currency;
+                    scStats.srStats.notStarted = statsInfo.study.notStarted;
+                    scStats.srStats.ongoing = statsInfo.study.ongoing;
+                    scStats.srStats.finish = statsInfo.study.finish;
+                }
+
+                if(statsInfo.upTime > 0 && !isnew) {
+                    long timedis = 15 * 60 * 1000;//15分钟
+                    if (now - statsInfo.upTime < timedis)
+                        isnew = true;
+                }
+                if (statsInfo.upTime == 0 && isnew)
+                    statsInfo.upTime = now;
+                if (statsInfo.upTime == 0 && isnew)
+                    statsInfo.upTime = DateTimeOffset.UtcNow.Add(new TimeSpan(-1, 0, 0, 0)).ToUnixTimeMilliseconds();
 
 
 
 
 
-            return Ok(new { state = RespondCode.Ok});
+
+                return Ok(new { state = RespondCode.Ok });
+            }
+            catch (Exception ex)
+            {
+                await _dingDing.SendBotMsg($"BI,{_option.Location}  /school/set-scstats   \n    {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
+                return BadRequest();
+            }
         }
 
 
@@ -545,5 +643,89 @@ namespace TEAMModelBI.Controllers.Census
             public long dataCount { get; set; }
         }
 
+        /// <summary>
+        /// 前端显示学校统计数据
+        /// </summary>
+        public record ScStats
+        {
+            public string id { get; set; }
+            public string name { get; set; }
+            public string picture { get; set; }
+            public int tch { get; set; }
+            public int dayTch { get; set; }
+            public int weekTch { get; set; }
+            public int monthTch { get; set; }
+            public int stu { get; set; }
+            public int dayStu { get; set; }
+            public int weekStu { get; set; }
+            public int monthStu { get; set; }
+            public int room { get; set; }
+            public int witRoom { get; set; }
+            public int size { get; set; }
+            public long scCreateTime { get; set; }
+            public long upTime { get; set; }
+            public ScLessStats lessStats { get; set; }
+            public ScActStats actStats { get; set; }
+
+            public ScSRStats srStats { get; set; }
+        }
+
+        /// <summary>
+        /// 课例活动数据
+        /// </summary>
+        public record ScLessStats
+        {
+            public int open { get; set; }
+            public int less { get; set; }
+            public int lastDay { get; set; }
+            public int day { get; set; }
+            public int lastWeek { get; set; }
+            public int week { get; set; }
+            public int lastTerm { get; set; }
+            public int term { get; set; }
+            public int lastDayInter { get; set; }
+            public int dayInter { get; set; }
+            public int lastMonthInter { get; set; }
+            public int monthInter { get; set; }
+            public int lastYearInter { get; set; }
+            public List<YearMonth> yearInter { get; set; } = new List<YearMonth>();//12个月
+            public List<YearMonth> LastYear { get; set; } = new List<YearMonth>();//12个月
+            public List<YearMonth> year { get; set; } = new List<YearMonth>();    //12个月
+        }
+
+        /// <summary>
+        /// 学校活动数据
+        /// </summary>
+        public record ScActStats 
+        {
+            public int cnt { get; set; }
+            public int exam { get; set; }
+            public int survey { get; set; }
+            public int vote { get; set; }
+            public int homework { get; set; }
+            public int lastDay { get; set; }
+            public int dayCnt { get; set; }
+            public int lastWeek { get; set; }
+            public int week { get; set; }
+            public int lastTerm { get; set; }
+            public int term { get; set; }
+            public List<YearMonth> LastYear { get; set; } = new List<YearMonth>(); //12个月
+            public List<YearMonth> year { get; set; } = new List<YearMonth>();    //12个月
+        }
+
+        /// <summary>
+        /// 研修统计数据
+        /// </summary>
+        public record ScSRStats 
+        { 
+            public int learnTime { get; set; }
+            public int onlineTime { get; set; }
+            public int offlineTime { get; set; }
+            public int classTime { get; set; }
+            public int currency { get; set; }
+            public int notStarted { get; set; }
+            public int ongoing { get; set; }
+            public int finish { get; set; }
+        }
     }
 }

+ 7 - 0
TEAMModelBI/Models/ItemInfo.cs

@@ -46,4 +46,11 @@ namespace TEAMModelBI.Models
         public string code { get; set; }
         public string value { get; set; }
     }
+
+    public class YearMonth 
+    {
+        public string id { get; set; }
+
+        public double cnt { get; set; }
+    }
 }

+ 30 - 1
TEAMModelBI/Tool/TimeHelper.cs

@@ -1,4 +1,5 @@
-using System;
+using DocumentFormat.OpenXml.Bibliography;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Threading.Tasks;
@@ -417,5 +418,33 @@ namespace TEAMModelBI.Tool
             return days;
         }
 
+
+        public static List<YearMonth> GetYearMonth(List<double> doubles, int year = 0,int month = 0) 
+        {
+            List<YearMonth> months = new();
+            DateTimeOffset dateTime = DateTimeOffset.Now;
+            if (year == 0) 
+                year = dateTime.Year;
+            if(month == 0)
+                month = dateTime.Month;
+
+            List<int> isLeapYear = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? isLeapYear = leapY : isLeapYear = flatY;
+            double[] array = doubles.ToArray();
+            double ser = doubles.Sum();
+            int moth = 1;
+            int primaryD = 0;
+            for (int i = 0; i < isLeapYear.Count; i++)
+            {
+                if (month < moth)
+                    break;
+                double[] temp = new double[isLeapYear[i]];
+                Array.ConstrainedCopy(array, primaryD, temp, 0, isLeapYear[i]);
+                months.Add(new YearMonth() { id = $"{year}-{moth}", cnt = temp.Sum() });
+                moth += 1;
+                primaryD += isLeapYear[i];
+            }
+
+            return months;
+        }
     }
 }

+ 13 - 2
TEAMModelOS.SDK/Models/Cosmos/BI/StatsInfo.cs

@@ -15,6 +15,7 @@ namespace TEAMModelOS.SDK.Models.Cosmos.BI
         public StatsInfo()
         {
             pk = "Stats";
+            code = "Stats";
         }
 
         /// <summary>
@@ -85,12 +86,12 @@ namespace TEAMModelOS.SDK.Models.Cosmos.BI
         /// <summary>
         /// 学校创建时间
         /// </summary>
-        public long scCreateDte { get; set; }
+        public long scCreateTime { get; set; }
 
         /// <summary>
         /// 学校统计数据更新时间
         /// </summary>
-        public long upDate { get; set; }
+        public long upTime { get; set; }
 
         /// <summary>
         /// 课例活动
@@ -173,6 +174,16 @@ namespace TEAMModelOS.SDK.Models.Cosmos.BI
         /// </summary>
         public int monthInter { get; set; }
 
+        /// <summary>
+        /// 去年互动
+        /// </summary>
+        public int lastYearInter { get; set; }
+
+        /// <summary>
+        /// 今年互动
+        /// </summary>
+        public List<double> yearInter { get; set; } = new List<double>();
+
         /// <summary>
         /// 去年每天的数据  366天
         /// </summary>