浏览代码

课例统计存储在 CosmosDB School表中的结构和相关 信息

Li 3 年之前
父节点
当前提交
5b0cf5d4a1

+ 152 - 121
TEAMModelBI/Controllers/BISchool/LessonController.cs

@@ -2,17 +2,19 @@
 using MathNet.Numerics.LinearAlgebra.Double;
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
+using StackExchange.Redis;
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text.Json;
+using System.Threading;
 using System.Threading.Tasks;
 using TEAMModelBI.Tool;
 using TEAMModelBI.Tool.Context;
 using TEAMModelOS.SDK.DI;
 using TEAMModelOS.SDK.Extension;
 using TEAMModelOS.SDK.Models;
-using TEAMModelOS.SDK.Models.Cosmos.BI.BICommon;
+using TEAMModelOS.SDK.Models.Cosmos.BI.BISchool;
 
 namespace TEAMModelBI.Controllers.BISchool
 {
@@ -31,25 +33,34 @@ namespace TEAMModelBI.Controllers.BISchool
             _azureRedis = azureRedis;
         }
 
-
-
         /// <summary>
         /// 测试 通过时间戳保存统计类型接口
         /// </summary>
         /// <param name="jsonElement"></param>
         /// <returns></returns>
         [HttpPost("set-bilessonstats")]
-        public async Task<IActionResult> SetBILeesonStats(JsonElement jsonElement) 
+        public async Task<IActionResult> SetBILeesonStats(JsonElement jsonElement)
         {
             if (!jsonElement.TryGetProperty("unix", out JsonElement unix)) return BadRequest();
             if (!jsonElement.TryGetProperty("num", out JsonElement num)) return BadRequest();
             jsonElement.TryGetProperty("schoolId", out JsonElement schoolId);
             jsonElement.TryGetProperty("type", out JsonElement type);
+            jsonElement.TryGetProperty("unixs", out JsonElement unixs);
+            var cosmosClient = _azureCosmos.GetCosmosClient();
 
+            var UUID = Guid.NewGuid().ToString();
+            List<long> unixsT = unixs.ToObject<List<long>>();
 
-            var cosmosClient = _azureCosmos.GetCosmosClient();
+            RedisValue token = Environment.MachineName;
+
+            foreach (var item in unixsT)
+            {
+                await SetBILeesonStats(cosmosClient, item, num.GetInt32(), type: type.GetInt32(), schoolId: schoolId.GetString());
+            }
+
+            //await BILeeson.SetBILeesonStats(cosmosClient, _azureRedis, unix.GetInt64(), num.GetInt32(), type: type.GetInt32(), schoolId: schoolId.GetString());
 
-            await SetBILeesonStats(cosmosClient, unix.GetInt64(), num.GetInt32(), type: -1, schoolId: schoolId.GetString());
+            //await SetBILeesonStats(cosmosClient, unix.GetInt64(), num.GetInt32(), type: -1, schoolId: schoolId.GetString());
 
             return Ok(new { state = 200 });
         }
@@ -63,10 +74,14 @@ namespace TEAMModelBI.Controllers.BISchool
         /// <param name="type">课例类型,0开课 1 课例</param>
         /// <param name="schoolId">学校ID,可不传</param>
         /// <returns></returns>
-        public static async Task SetBILeesonStats(CosmosClient cosmosClient,long unix, int num, int type = 0, string schoolId = null)
+        public static async Task SetBILeesonStats(CosmosClient cosmosClient, long unix, int num, int type = 0, string schoolId = null)
         {
+            //Monitor.TryEnter(unix);  //锁对象
+            SemaphoreSlim slimlock = new(1, 1);    //对可同时访问资源或资源池的线程数加以限制
+            await slimlock.WaitAsync();
+
             DateTimeOffset dateTime = DateTimeOffset.FromUnixTimeMilliseconds(unix);
-            int year, month, day, hour ,days;
+            int year, month, day, hour, days;
             year = dateTime.Year;
             month = dateTime.Month;
             day = dateTime.Day;
@@ -88,66 +103,68 @@ namespace TEAMModelBI.Controllers.BISchool
             if (openRes.Status == 200)
             {
                 using var json = await JsonDocument.ParseAsync(openRes.ContentStream);
-                LessonYear lessonYear = json.ToObject<LessonYear>();
-                if (type == 0) 
+                LessonStats lessonYear = json.ToObject<LessonStats>();
+                if (type == 0)
                 {
-                    if (lessonYear.openYear.Count == 0)
-                        lessonYear.openYear = yDays;
-                    else lessonYear.openYear[days] = lessonYear.openYear[days] + num;
+                    if (lessonYear.open.Count == 0)
+                        lessonYear.open = yDays;
+                    else lessonYear.open[days] = lessonYear.open[days] + num;
                 }
                 else if (type == 1)
                 {
-                    if (lessonYear.lessonYear.Count == 0)
-                        lessonYear.lessonYear = yDays;
-                    else lessonYear.lessonYear[days] = lessonYear.lessonYear[days] + num;
+                    if (lessonYear.lesson.Count == 0)
+                        lessonYear.lesson = yDays;
+                    else lessonYear.lesson[days] = lessonYear.lesson[days] + num;
                 }
 
-                await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<LessonYear>(lessonYear, lessonYear.id, new PartitionKey($"{lessonYear.code}"));
+                await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<LessonStats>(lessonYear, lessonYear.id, new PartitionKey($"{lessonYear.code}"));
             }
             else
             {
-                LessonYear lessonYear = new();
+                LessonStats lessonYear = new();
                 lessonYear.id = $"{year}";
                 lessonYear.code = "LessonYear";
+                lessonYear.pk = "LessonYear";
                 if (type == 0)
-                    lessonYear.openYear = yDays;
+                    lessonYear.open = yDays;
                 else if (type == 1)
-                    lessonYear.lessonYear = yDays;
+                    lessonYear.lesson = yDays;
 
-                await cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync<LessonYear>(lessonYear, new PartitionKey($"{lessonYear.code}"));
+                await cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync<LessonStats>(lessonYear, new PartitionKey($"{lessonYear.code}"));
             }
 
             var lessRes = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync($"{hourId}", new PartitionKey("LessonHour"));
             if (lessRes.Status == 200)
             {
                 using var json = await JsonDocument.ParseAsync(lessRes.ContentStream);
-                LessonHour lessonHour = json.ToObject<LessonHour>();
+                LessonStats lessonHour = json.ToObject<LessonStats>();
                 if (type == 0)
                 {
-                    if (lessonHour.openHour.Count == 0)
-                        lessonHour.openHour = yHours;
-                    else lessonHour.openHour[hour] = lessonHour.openHour[hour] + num;
+                    if (lessonHour.open.Count == 0)
+                        lessonHour.open = yHours;
+                    else lessonHour.open[hour] = lessonHour.open[hour] + num;
                 }
                 else if (type == 1)
                 {
-                    if (lessonHour.lessonHour.Count == 0)
-                        lessonHour.lessonHour = yHours;
-                    else lessonHour.lessonHour[hour] = lessonHour.lessonHour[hour] + num;
+                    if (lessonHour.lesson.Count == 0)
+                        lessonHour.lesson = yHours;
+                    else lessonHour.lesson[hour] = lessonHour.lesson[hour] + num;
                 }
 
-                await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<LessonHour>(lessonHour, lessonHour.id, new PartitionKey($"LessonHour"));
+                await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<LessonStats>(lessonHour, lessonHour.id, new PartitionKey($"LessonHour"));
             }
             else
             {
-                LessonHour lessonHour = new();
+                LessonStats lessonHour = new();
                 lessonHour.id = $"{hourId}";
                 lessonHour.code = "LessonHour";
+                lessonHour.pk = "LessonHour";
                 if (type == 0)
-                    lessonHour.openHour = yDays;
+                    lessonHour.open = yDays;
                 else if (type == 1)
-                    lessonHour.lessonHour = yDays;
+                    lessonHour.lesson = yDays;
 
-                await cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync<LessonHour>(lessonHour, new PartitionKey("LessonHour"));
+                await cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync<LessonStats>(lessonHour, new PartitionKey("LessonHour"));
             }
 
             //学校统计
@@ -157,71 +174,76 @@ namespace TEAMModelBI.Controllers.BISchool
                 if (openResSc.Status == 200)
                 {
                     using var jsonSc = await JsonDocument.ParseAsync(openResSc.ContentStream);
-                    LessonYear lessonYearSc = jsonSc.ToObject<LessonYear>();
+                    LessonStats lessonYearSc = jsonSc.ToObject<LessonStats>();
                     if (type == 0)
-                        if (lessonYearSc.openYear.Count == 0)
-                            lessonYearSc.openYear = yDays;
-                        else lessonYearSc.openYear[days] = lessonYearSc.openYear[days] + num;
+                        if (lessonYearSc.open.Count == 0)
+                            lessonYearSc.open = yDays;
+                        else lessonYearSc.open[days] = lessonYearSc.open[days] + num;
                     else if (type == 1)
                     {
-                        if (lessonYearSc.lessonYear.Count == 0)
-                            lessonYearSc.lessonYear = yDays;
-                        else lessonYearSc.lessonYear[days] = lessonYearSc.lessonYear[days] + num;
+                        if (lessonYearSc.lesson.Count == 0)
+                            lessonYearSc.lesson = yDays;
+                        else lessonYearSc.lesson[days] = lessonYearSc.lesson[days] + num;
                     }
 
-                    await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<LessonYear>(lessonYearSc, lessonYearSc.id, new PartitionKey($"LessonYear-{schoolId}"));
+                    await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<LessonStats>(lessonYearSc, lessonYearSc.id, new PartitionKey($"LessonYear-{schoolId}"));
                 }
                 else
                 {
-                    LessonYear lessonYearSc = new();
+                    LessonStats lessonYearSc = new();
                     lessonYearSc.id = $"{year}";
                     lessonYearSc.code = $"LessonYear-{schoolId}";
+                    lessonYearSc.pk = "LessonYear";
                     if (type == 0)
-                        lessonYearSc.openYear = yDays;
+                        lessonYearSc.open = yDays;
                     else if (type == 1)
-                        lessonYearSc.lessonYear = yDays;
+                        lessonYearSc.lesson = yDays;
 
-                    await cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync<LessonYear>(lessonYearSc, new PartitionKey($"LessonYear-{schoolId}"));
+                    await cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync<LessonStats>(lessonYearSc, new PartitionKey($"LessonYear-{schoolId}"));
                 }
 
                 var lessResSc = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync($"{hourId}", new PartitionKey($"LessonHour-{schoolId}"));
                 if (lessResSc.Status == 200)
                 {
                     using var jsonSc = await JsonDocument.ParseAsync(lessResSc.ContentStream);
-                    LessonHour lessonHourSc = jsonSc.ToObject<LessonHour>();
+                    LessonStats lessonHourSc = jsonSc.ToObject<LessonStats>();
                     if (type == 0)
-                        lessonHourSc.openHour[hour] = lessonHourSc.openHour[hour] + num;
+                        if (lessonHourSc.open.Count == 0)
+                            lessonHourSc.open = yDays;
+                        else lessonHourSc.open[days] = lessonHourSc.open[days] + num;
                     else if (type == 1)
                     {
-                        if (lessonHourSc.lessonHour.Count == 0)
-                            lessonHourSc.lessonHour = yHours;
-                        else lessonHourSc.lessonHour[hour] = lessonHourSc.lessonHour[hour] + num;
+                        if (lessonHourSc.lesson.Count == 0)
+                            lessonHourSc.lesson = yHours;
+                        else lessonHourSc.lesson[hour] = lessonHourSc.lesson[hour] + num;
                     }
-                    await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<LessonHour>(lessonHourSc, lessonHourSc.id, new PartitionKey($"LessonHour-{schoolId}"));
+                    await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<LessonStats>(lessonHourSc, lessonHourSc.id, new PartitionKey($"LessonHour-{schoolId}"));
                 }
                 else
                 {
-                    LessonHour lessonYearSc = new();
+                    LessonStats lessonYearSc = new();
                     lessonYearSc.id = $"{hourId}";
                     lessonYearSc.code = $"LessonHour-{schoolId}";
+                    lessonYearSc.pk = "LessonHour";
                     if (type == 0)
-                        lessonYearSc.openHour = yHours;
+                        lessonYearSc.open = yHours;
                     else if (type == 1)
-                        lessonYearSc.lessonHour = yHours;
+                        lessonYearSc.lesson = yHours;
 
-                    await cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync<LessonHour>(lessonYearSc, new PartitionKey($"LessonHour-{schoolId}"));
+                    await cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync<LessonStats>(lessonYearSc, new PartitionKey($"LessonHour-{schoolId}"));
                 }
             }
+            slimlock.Release();
+            //Monitor.Enter(unix);
         }
 
-
         /// <summary>
         /// 历史记录读取,并统计存储
         /// </summary>
         /// <param name="jsonElement"></param>
         /// <returns></returns>
         [HttpPost("get-alllesson")]
-        public async Task<IActionResult> GetAllLessonRecords(JsonElement jsonElement) 
+        public async Task<IActionResult> GetAllLessonRecords(JsonElement jsonElement)
         {
             jsonElement.TryGetProperty("site", out JsonElement site);
 
@@ -230,9 +252,8 @@ namespace TEAMModelBI.Controllers.BISchool
                 cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.GlobalSite);
 
             List<LessonRecord> allLesson = new();
-            List<LessonRecord> tchLesson = new();
 
-            await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS,"School").GetItemQueryIterator<LessonRecord>(queryText: $"select value(c) from c where c.pk='LessonRecord'",requestOptions:new QueryRequestOptions() { }))
+            await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<LessonRecord>(queryText: $"select value(c) from c where c.pk='LessonRecord'", requestOptions: new QueryRequestOptions() { }))
             {
                 allLesson.Add(item);
             }
@@ -242,7 +263,7 @@ namespace TEAMModelBI.Controllers.BISchool
                 allLesson.Add(item);
             }
 
-            (List<LessonYear> lessYears, List<LessonHour> lessHours) = await AllLessonStats(allLesson);
+            (List<LessonStats> lessYears, List<LessonStats> lessHours) = await AllLessonStats(allLesson, _azureRedis);
 
             foreach (var itemY in lessYears)
             {
@@ -250,13 +271,13 @@ namespace TEAMModelBI.Controllers.BISchool
                 if (response.Status == 200)
                 {
                     using var json = await JsonDocument.ParseAsync(response.ContentStream);
-                    LessonYear lessonYear = json.ToObject<LessonYear>();
-                    lessonYear.openYear = itemY.openYear;
-                    lessonYear.lessonYear = itemY.lessonYear;
-                    await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<LessonYear>(lessonYear, lessonYear.id, new PartitionKey($"{lessonYear.code}"));
+                    LessonStats lessonYear = json.ToObject<LessonStats>();
+                    lessonYear.open = itemY.open;
+                    lessonYear.lesson = itemY.lesson;
+                    await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<LessonStats>(lessonYear, lessonYear.id, new PartitionKey($"{lessonYear.code}"));
                 }
                 else
-                    await cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync<LessonYear>(itemY, new PartitionKey($"{itemY.code}"));
+                    await cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync<LessonStats>(itemY, new PartitionKey($"{itemY.code}"));
             }
 
 
@@ -266,13 +287,13 @@ namespace TEAMModelBI.Controllers.BISchool
                 if (response.Status == 200)
                 {
                     using var json = await JsonDocument.ParseAsync(response.ContentStream);
-                    LessonHour lessonHour = json.ToObject<LessonHour>();
-                    lessonHour.openHour = itemH.openHour;
-                    lessonHour.lessonHour = itemH.lessonHour;
-                    await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<LessonHour>(lessonHour, lessonHour.id, new PartitionKey($"{lessonHour.code}"));
+                    LessonStats lessonHour = json.ToObject<LessonStats>();
+                    lessonHour.open = itemH.open;
+                    lessonHour.lesson = itemH.lesson;
+                    await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<LessonStats>(lessonHour, lessonHour.id, new PartitionKey($"{lessonHour.code}"));
                 }
                 else
-                    await cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync<LessonHour>(itemH, new PartitionKey($"{itemH.code}"));
+                    await cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync<LessonStats>(itemH, new PartitionKey($"{itemH.code}"));
             }
 
             return Ok(new { state = 200, ycnt = lessYears.Count, hcnt = lessHours.Count, lessYears, lessHours });
@@ -286,7 +307,7 @@ namespace TEAMModelBI.Controllers.BISchool
         /// </summary>
         /// <param name="lessRecs"></param>
         /// <returns></returns>
-        public static async Task<(List<LessonYear> lessonYears, List<LessonHour> lessonHours)> AllLessonStats(List<LessonRecord> lessRecs)
+        public static async Task<(List<LessonStats> lessonYears, List<LessonStats> lessonHours)> AllLessonStats(List<LessonRecord> lessRecs, AzureRedisFactory azureRedis)
         {
             //统计所有
             List<LessCnt> allLess = lessRecs.Select(item => new LessCnt { hour = TimeHelper.GetDateTime(item.startTime).ToString("yyyyMMddHH"), cat = item.id, upload = item.upload, schoolId = item.school }).ToList();
@@ -294,62 +315,66 @@ namespace TEAMModelBI.Controllers.BISchool
             var openL = allLess.FindAll(f => f.upload == 0).GroupBy(g => g.hour).Select(s => new AllLess { key = s.Key, cnt = s.Count() }).ToList();
             var lessL = allLess.FindAll(s => s.upload == 1).GroupBy(g => g.hour).Select(s => new AllLess { key = s.Key, cnt = s.Count() }).ToList();
 
-            List<LessonYear> lessYears = new();
-            List<LessonHour> lessHours = new();
+            List<LessonStats> lessYears = new();
+            List<LessonStats> lessHours = new();
             //统计开课记录
             openL.ForEach(x =>
             {
                 var (year, month, day, days, hour) = TimeHelper.GetDateTime(x.key);
                 string id = $"{year}{month.ToString().PadLeft(2, '0')}{day.ToString().PadLeft(2, '0')}";
-                LessonHour finHour = lessHours.Find(f => f.id.Equals(id));
+                LessonStats finHour = lessHours.Find(f => f.id.Equals(id));
                 if (finHour != null)
                 {
-                    if (finHour.openHour.Count == 0)
+                    if (finHour.open.Count == 0)
                     {
                         double[] da = new double[23];
                         da[hour] = x.cnt;
                         List<double> tempDays = new(da);
-                        finHour.openHour = tempDays;
+                        finHour.open = tempDays;
                     }
-                    else finHour.openHour[hour] = x.cnt;
+                    else finHour.open[hour] = x.cnt;
+
+                    azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"BILesson:All:Open:{finHour.id}", $"{hour}", x.cnt);//一天24小时课例数 有上传 base.josn  小时为单位
                 }
                 else
                 {
                     //小时统计
-                    LessonHour lessonHour = new();
+                    LessonStats lessonHour = new();
                     lessonHour.id = id;
                     lessonHour.code = "LessonHour";
+                    lessonHour.pk = "LessonHour";
                     double[] daHours = new double[23];
                     daHours[hour] = x.cnt;
                     List<double> hourDays = new(daHours);
-                    lessonHour.openHour = hourDays;
+                    lessonHour.open = hourDays;
                     //lessonHour.ttl = 24 * 60 * 60 * 8; //设置过期时间 8天后自动删除
                     lessHours.Add(lessonHour);
                 }
 
-                LessonYear findLess = lessYears.Find(f => f.id.Equals($"{year}"));
+                LessonStats findLess = lessYears.Find(f => f.id.Equals($"{year}"));
                 if (findLess != null)
                 {
-                    if (findLess.openYear.Count == 0)
+                    if (findLess.open.Count == 0)
                     {
                         var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
                         double[] da = new double[yearDays];
                         da[days] = x.cnt;
                         List<double> tempDays = new(da);
-                        findLess.openYear = tempDays;
+                        findLess.open = tempDays;
                     }
-                    else findLess.openYear[days] = x.cnt;
+                    else findLess.open[days] = x.cnt;
                 }
                 else
                 {
-                    LessonYear lessonYear = new();
+                    LessonStats lessonYear = new();
                     lessonYear.id = $"{year}";
                     lessonYear.code = "LessonYear";
+                    lessonYear.pk = "LessonYear";
                     var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
                     double[] daDays = new double[yearDays];
                     daDays[days] = x.cnt;
                     List<double> yDays = new(daDays);
-                    lessonYear.openYear = yDays;
+                    lessonYear.open = yDays;
                     lessYears.Add(lessonYear);
                 }
             });
@@ -359,54 +384,56 @@ namespace TEAMModelBI.Controllers.BISchool
             {
                 var (year, month, day, days, hour) = TimeHelper.GetDateTime(x.key);
                 string id = $"{year}{month.ToString().PadLeft(2, '0')}{day.ToString().PadLeft(2, '0')}";
-                LessonHour finHour = lessHours.Find(f => f.id.Equals(id));
+                LessonStats finHour = lessHours.Find(f => f.id.Equals(id));
                 if (finHour != null)
                 {
-                    if (finHour.lessonHour.Count == 0)
+                    if (finHour.lesson.Count == 0)
                     {
                         double[] da = new double[23];
                         da[hour] = x.cnt;
                         List<double> tempDays = new(da);
-                        finHour.lessonHour = tempDays;
+                        finHour.lesson = tempDays;
                     }
-                    else finHour.lessonHour[hour] = x.cnt;
+                    else finHour.lesson[hour] = x.cnt;
                 }
                 else
                 {
                     //小时统计
-                    LessonHour lessonHour = new();
+                    LessonStats lessonHour = new();
                     lessonHour.id = id;
                     lessonHour.code = "LessonHour";
+                    lessonHour.pk = "LessonHour";
                     double[] daHours = new double[23];
                     daHours[hour] = x.cnt;
                     List<double> hourDays = new(daHours);
-                    lessonHour.lessonHour = hourDays;
+                    lessonHour.lesson = hourDays;
                     lessHours.Add(lessonHour);
                 }
 
-                LessonYear findLess = lessYears.Find(f => f.id.Equals($"{year}"));
+                LessonStats findLess = lessYears.Find(f => f.id.Equals($"{year}"));
                 if (findLess != null)
                 {
-                    if (findLess.lessonYear.Count == 0)
+                    if (findLess.lesson.Count == 0)
                     {
                         var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
                         double[] da = new double[yearDays];
                         da[days] = x.cnt;
                         List<double> tempDays = new(da);
-                        findLess.lessonYear = tempDays;
+                        findLess.lesson = tempDays;
                     }
-                    else findLess.lessonYear[days] = x.cnt;
+                    else findLess.lesson[days] = x.cnt;
                 }
                 else
                 {
-                    LessonYear lessonYear = new();
+                    LessonStats lessonYear = new();
                     lessonYear.id = $"{year}";
                     lessonYear.code = "LessonYear";
+                    lessonYear.pk = "LessonYear";
                     var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
                     double[] da = new double[yearDays];
                     da[days] = x.cnt;
                     List<double> tempDays = new(da);
-                    lessonYear.lessonYear = tempDays;
+                    lessonYear.lesson = tempDays;
                     lessYears.Add(lessonYear);
                 }
             });
@@ -426,55 +453,57 @@ namespace TEAMModelBI.Controllers.BISchool
                         var (year, month, day, days, hour) = TimeHelper.GetDateTime(f.key);
                         string id = $"{year}{month.ToString().PadLeft(2, '0')}{day.ToString().PadLeft(2, '0')}";
                         string codeHour = $"LessonHour-{f.schoolId}";
-                        LessonHour finHour = lessHours.Find(f => f.id.Equals(id) && f.code.Equals(codeHour));
+                        LessonStats finHour = lessHours.Find(f => f.id.Equals(id) && f.code.Equals(codeHour));
                         if (finHour != null)
                         {
-                            if (finHour.openHour.Count == 0)
+                            if (finHour.open.Count == 0)
                             {
                                 double[] da = new double[23];
                                 da[hour] = f.cnt;
                                 List<double> tempDays = new(da);
-                                finHour.openHour = tempDays;
+                                finHour.open = tempDays;
                             }
-                            else finHour.openHour[hour] = f.cnt;
+                            else finHour.open[hour] = f.cnt;
                         }
                         else
                         {
                             //小时统计
-                            LessonHour lessonHour = new();
+                            LessonStats lessonHour = new();
                             lessonHour.id = id;
                             lessonHour.code = codeHour;
+                            lessonHour.pk = "LessonHour";
                             double[] daHours = new double[23];
                             daHours[hour] = f.cnt;
                             List<double> hourDays = new(daHours);
-                            lessonHour.openHour = hourDays;
+                            lessonHour.open = hourDays;
                             //lessonHour.ttl = 24 * 60 * 60 * 8; //设置过期时间
                             lessHours.Add(lessonHour);
                         }
                         string codeYear = $"LessonYear-{f.schoolId}";
-                        LessonYear findLess = lessYears.Find(f => f.id.Equals($"{year}") && f.code.Equals(codeYear));
+                        LessonStats findLess = lessYears.Find(f => f.id.Equals($"{year}") && f.code.Equals(codeYear));
                         if (findLess != null)
                         {
-                            if (findLess.openYear.Count == 0)
+                            if (findLess.open.Count == 0)
                             {
                                 var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
                                 double[] da = new double[yearDays];
                                 da[days] = f.cnt;
                                 List<double> tempDays = new(da);
-                                findLess.openYear = tempDays;
+                                findLess.open = tempDays;
                             }
-                            else findLess.openYear[days] = f.cnt;
+                            else findLess.open[days] = f.cnt;
                         }
                         else
                         {
-                            LessonYear lessonYear = new();
+                            LessonStats lessonYear = new();
                             lessonYear.id = $"{year}";
                             lessonYear.code = codeYear;
+                            lessonYear.code = "LessonYear";
                             var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
                             double[] daDays = new double[yearDays];
                             daDays[days] = f.cnt;
                             List<double> yDays = new(daDays);
-                            lessonYear.openYear = yDays;
+                            lessonYear.open = yDays;
                             lessYears.Add(lessonYear);
                         }
                     }
@@ -489,54 +518,56 @@ namespace TEAMModelBI.Controllers.BISchool
                             var (year, month, day, days, hour) = TimeHelper.GetDateTime(x.key);
                             string id = $"{year}{month.ToString().PadLeft(2, '0')}{day.ToString().PadLeft(2, '0')}";
                             string codeHour = $"LessonHour-{x.schoolId}";
-                            LessonHour finHour = lessHours.Find(f => f.id.Equals(id) && f.code.Equals(codeHour));
+                            LessonStats finHour = lessHours.Find(f => f.id.Equals(id) && f.code.Equals(codeHour));
                             if (finHour != null)
                             {
-                                if (finHour.lessonHour.Count == 0)
+                                if (finHour.lesson.Count == 0)
                                 {
                                     double[] da = new double[23];
                                     da[hour] = x.cnt;
                                     List<double> tempDays = new(da);
-                                    finHour.lessonHour = tempDays;
+                                    finHour.lesson = tempDays;
                                 }
-                                else finHour.lessonHour[hour] = x.cnt;
+                                else finHour.lesson[hour] = x.cnt;
                             }
                             else
                             {
                                 //小时统计
-                                LessonHour lessonHour = new();
+                                LessonStats lessonHour = new();
                                 lessonHour.id = id;
                                 lessonHour.code = codeHour;
+                                lessonHour.pk = "LessonHour";
                                 double[] daHours = new double[23];
                                 daHours[hour] = x.cnt;
                                 List<double> hourDays = new(daHours);
-                                lessonHour.lessonHour = hourDays;
+                                lessonHour.lesson = hourDays;
                                 lessHours.Add(lessonHour);
                             }
                             string codeYear = $"LessonYear-{x.schoolId}";
-                            LessonYear findLess = lessYears.Find(f => f.id.Equals($"{year}") && f.code.Equals(codeYear));
+                            LessonStats findLess = lessYears.Find(f => f.id.Equals($"{year}") && f.code.Equals(codeYear));
                             if (findLess != null)
                             {
-                                if (findLess.lessonYear.Count == 0)
+                                if (findLess.lesson.Count == 0)
                                 {
                                     var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
                                     double[] da = new double[yearDays];
                                     da[days] = x.cnt;
                                     List<double> tempDays = new(da);
-                                    findLess.lessonYear = tempDays;
+                                    findLess.lesson = tempDays;
                                 }
-                                else findLess.lessonYear[days] = x.cnt;
+                                else findLess.lesson[days] = x.cnt;
                             }
                             else
                             {
-                                LessonYear lessonYear = new();
+                                LessonStats lessonYear = new();
                                 lessonYear.id = $"{year}";
                                 lessonYear.code = codeYear;
+                                lessonYear.pk = "LessonYear";
                                 var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
                                 double[] da = new double[yearDays];
                                 da[days] = x.cnt;
                                 List<double> tempDays = new(da);
-                                lessonYear.lessonYear = tempDays;
+                                lessonYear.lesson = tempDays;
                                 lessYears.Add(lessonYear);
                             }
                         }

+ 14 - 0
TEAMModelBI/Controllers/BITest/TestController.cs

@@ -1408,6 +1408,20 @@ namespace TEAMModelBI.Controllers.BITest
             return Ok(new { state = 200, linqTest,cosmosClient,site });
         }
 
+        [HttpPost("get-bizschool")]
+        public async Task<IActionResult> GetTable(JsonElement jsonElement) 
+        {            
+            jsonElement.TryGetProperty("partitionKey", out JsonElement partitionKey);
+            jsonElement.TryGetProperty("rowKey", out JsonElement rowKey);
+            var tableClient = _azureStorage.GetCloudTableClient();
+            var table = _azureStorage.GetCloudTableClient().GetTableReference("IESOpenApi");
+            BusinessSchool tempUser = table.Get<BusinessSchool>(partitionKey: $"{partitionKey}", rowKey: $"{rowKey}");
+
+
+
+            return Ok(new { state = 200, table = tempUser });
+        }
+
         public class linqTest
         {
             public string id{ get; set; }

+ 0 - 48
TEAMModelOS.SDK/Models/Cosmos/BI/BICommon/LessonYear.cs

@@ -1,48 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace TEAMModelOS.SDK.Models.Cosmos.BI.BICommon
-{
-
-    /// <summary>
-    /// 一年的的课例
-    /// </summary>
-    public class LessonYear: CosmosEntity
-    {
-        public LessonYear()
-        {
-            pk = "LessonYear";
-        }
-        /// <summary>
-        ///  Hiteach开课365/366天记录
-        /// </summary>
-        public List<double> openYear { get; set; } = new List<double>();
-        /// <summary>
-        /// 课例365/366天记录
-        /// </summary>
-        public List<double> lessonYear { get; set; } = new List<double>();
-    }
-
-    /// <summary>
-    /// 一天24小时的数据
-    /// </summary>
-    public class LessonHour : CosmosEntity
-    {
-        public LessonHour()
-        {
-            pk = "LessonHour";
-        }
-        /// <summary>
-        ///  Hiteach开课24小时
-        /// </summary>
-        public List<double> openHour { get; set; } = new List<double>();
-        /// <summary>
-        /// 课例24小时
-        /// </summary>
-        public List<double> lessonHour { get; set; } = new List<double>();
-
-    }
-}

+ 27 - 0
TEAMModelOS.SDK/Models/Cosmos/BI/BISchool/LessonStats.cs

@@ -0,0 +1,27 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace TEAMModelOS.SDK.Models.Cosmos.BI.BISchool
+{
+    public class LessonStats : CosmosEntity
+    {
+        public LessonStats()
+        {
+            //pk LessonYear 记录一年的数据  天    code LessonYear 一年所有  LessonYear-学校编码   学校一年所有
+            //pk LessonHour 记录一天的数据  小时   code LessonHour 一天的记录  LessonHour-学校编码   学校一天的记录
+        }
+
+        /// <summary>
+        ///  Hiteach开课记录
+        /// </summary>
+        public List<double> open { get; set; } = new List<double>();
+
+        /// <summary>
+        /// 课例记录
+        /// </summary>
+        public List<double> lesson { get; set; } = new List<double>();
+    }
+}

+ 213 - 0
TEAMModelOS.SDK/Models/Service/BI/BILeeson.cs

@@ -0,0 +1,213 @@
+using Azure.Cosmos;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Text.Json;
+using System.Threading.Tasks;
+using TEAMModelOS.SDK.DI;
+using TEAMModelOS.SDK.Extension;
+using TEAMModelOS.SDK.Models.Cosmos.BI.BISchool;
+
+namespace TEAMModelOS.SDK.Models.Service.BI
+{
+    public class BILeeson
+    {
+        /// <summary>
+        /// 通过时间戳保存开课记录和课例记录统计
+        /// </summary>
+        /// <param name="cosmosClient">cosmosDB连接</param>
+        /// <param name="azureRedis">redis</param>
+        /// <param name="unix">13位时间戳</param>
+        /// <param name="num">数量</param>
+        /// <param name="type">类型:0 开课记录  1 课例记录</param>
+        /// <param name="schoolId">学校编码</param>
+        /// <returns></returns>
+        public static async Task SetBILeesonStats(CosmosClient cosmosClient, AzureRedisFactory azureRedis, long unix, int num = 1, int type = 0, string schoolId = null)
+        {
+            DateTimeOffset dateTime = DateTimeOffset.FromUnixTimeMilliseconds(unix);
+            int year, month, day, hour, days;
+            year = dateTime.Year;
+            month = dateTime.Month;
+            day = dateTime.Day;
+            hour = dateTime.Hour;
+            days = dateTime.DayOfYear;
+            var dateDay = dateTime.ToString("yyyyMMdd"); //获取当天的日期
+            var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
+
+            string LessType = "Open";
+            if (type == 1)
+                LessType = "Lesson";
+
+            await azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"BILesson:All:{LessType}:{dateDay}", $"{hour}", num);//一天24小时课例数 有上传 base.josn  小时为单位
+            await azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"BILesson:All:{LessType}:{year}", $"{days}", num);//一年的课例数量 有上传 base.josn 小时为单位
+
+            //保存当天的统计  小时
+            var dayCnt = azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"BILesson:All:{LessType}:{dateDay}");
+            if (dayCnt != null && dayCnt.Length > 0)
+            {
+                double[] daHour = new double[23];
+                foreach (var item in dayCnt)
+                {
+                    double val = ((double)item.Score);
+                    int key = ((int)item.Element);
+                    daHour[key] = val;
+                }
+                List<double> lessHours = new(daHour);
+
+                var lessRes = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync($"{dateDay}", new PartitionKey("LessonHour"));
+                if (lessRes.Status == 200)
+                {
+                    using var json = await JsonDocument.ParseAsync(lessRes.ContentStream);
+                    LessonStats lessonStats = json.ToObject<LessonStats>();
+                    lessonStats.code = "LessonHour";
+                    lessonStats.pk = "LessonHour";
+                    if (type == 1)
+                        lessonStats.lesson = lessHours;
+                    else
+                        lessonStats.open = lessHours;
+                    await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<LessonStats>(lessonStats, lessonStats.id, new PartitionKey("LessonHour"));
+                }
+                else
+                {
+                    LessonStats lessonStats = new();
+                    lessonStats.id = $"{dateDay}";
+                    lessonStats.code = "LessonHour";
+                    lessonStats.pk = "LessonHour";
+                    if (type == 1)
+                        lessonStats.lesson = lessHours;
+                    else
+                        lessonStats.open = lessHours;
+
+                    await cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync<LessonStats>(lessonStats, new PartitionKey("LessonHour"));
+                }
+            }
+
+            //保一年的统计   天
+            var yearCnt = azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"BILesson:All:{LessType}:{year}");
+            if (yearCnt != null && yearCnt.Length > 0)
+            {
+                double[] daYear = new double[yearDays];
+                foreach (var item in yearCnt)
+                {
+                    double val = ((double)item.Score);
+                    int key = ((int)item.Element);
+                    daYear[key] = val;
+                }
+                List<double> lessYear = new(daYear);
+
+                var lessRes = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync($"{year}", new PartitionKey("LessonYear"));
+                if (lessRes.Status == 200)
+                {
+                    using var json = await JsonDocument.ParseAsync(lessRes.ContentStream);
+                    LessonStats lessonYear = json.ToObject<LessonStats>();
+                    lessonYear.code = "LessonYear";
+                    lessonYear.pk = "LessonYear";
+                    if (type == 1)
+                        lessonYear.lesson = lessYear;
+                    else
+                        lessonYear.open = lessYear;
+                    await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<LessonStats>(lessonYear, lessonYear.id, new PartitionKey("LessonYear"));
+                }
+                else
+                {
+                    LessonStats lessonYear = new();
+                    lessonYear.id = $"{year}";
+                    lessonYear.code = "LessonYear";
+                    lessonYear.pk = "LessonYear";
+                    if (type == 1)
+                        lessonYear.lesson = lessYear;
+                    else
+                        lessonYear.open = lessYear;
+                    await cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync<LessonStats>(lessonYear, new PartitionKey("LessonYear"));
+                }
+            }
+
+            if (!string.IsNullOrEmpty(schoolId))
+            {
+                await azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"BILesson:{schoolId}:{LessType}:{dateDay}", $"{hour}", num);//一天24小时课例数 有上传 base.josn  小时为单位
+                await azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"BILesson:{schoolId}:{LessType}:{year}", $"{days}", num);//一年的课例数量 有上传 base.josn 小时为单位
+
+                //保存当天的统计  小时
+                var dayScCnt = azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"BILesson:{schoolId}:{LessType}:{dateDay}");
+                if (dayScCnt != null && dayScCnt.Length > 0)
+                {
+                    double[] daHour = new double[23];
+                    foreach (var item in dayScCnt)
+                    {
+                        double val = ((double)item.Score);
+                        int key = ((int)item.Element);
+                        daHour[key] = val;
+                    }
+                    List<double> lessHours = new(daHour);
+
+                    var lessRes = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync($"{dateDay}", new PartitionKey($"LessonHour-{schoolId}"));
+                    if (lessRes.Status == 200)
+                    {
+                        using var json = await JsonDocument.ParseAsync(lessRes.ContentStream);
+                        LessonStats lessonStats = json.ToObject<LessonStats>();
+                        lessonStats.code = $"LessonHour-{schoolId}";
+                        lessonStats.pk = "LessonHour";
+                        if (type == 1)
+                            lessonStats.lesson = lessHours;
+                        else
+                            lessonStats.open = lessHours;
+                        await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<LessonStats>(lessonStats, lessonStats.id, new PartitionKey($"LessonHour-{schoolId}"));
+                    }
+                    else
+                    {
+                        LessonStats lessonStats = new();
+                        lessonStats.id = $"{dateDay}";
+                        lessonStats.code = $"LessonHour-{schoolId}";
+                        lessonStats.pk = "LessonHour";
+                        if (type == 1)
+                            lessonStats.lesson = lessHours;
+                        else
+                            lessonStats.open = lessHours;
+                        await cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync<LessonStats>(lessonStats, new PartitionKey($"LessonHour-{schoolId}"));
+                    }
+                }
+
+                //保一年的统计   天
+                var scYearCnt = azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"BILesson:{schoolId}:{LessType}:{year}");
+                if (scYearCnt != null && scYearCnt.Length > 0)
+                {
+                    double[] daYear = new double[yearDays];
+                    foreach (var item in scYearCnt)
+                    {
+                        double val = ((double)item.Score);
+                        int key = ((int)item.Element);
+                        daYear[key] = val;
+                    }
+                    List<double> lessYear = new(daYear);
+
+                    var lessRes = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync($"{year}", new PartitionKey($"LessonYear-{schoolId}"));
+                    if (lessRes.Status == 200)
+                    {
+                        using var json = await JsonDocument.ParseAsync(lessRes.ContentStream);
+                        LessonStats lessonYear = json.ToObject<LessonStats>();
+                        lessonYear.code = $"LessonYear-{schoolId}";
+                        lessonYear.pk = "LessonYear";
+                        if (type == 1)
+                            lessonYear.lesson = lessYear;
+                        else
+                            lessonYear.open = lessYear;
+                        await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<LessonStats>(lessonYear, lessonYear.id, new PartitionKey($"LessonYear-{schoolId}"));
+                    }
+                    else
+                    {
+                        LessonStats lessonYear = new();
+                        lessonYear.id = $"{year}";
+                        lessonYear.code = $"LessonYear-{schoolId}";
+                        lessonYear.pk = "LessonYear";
+                        if (type == 1)
+                            lessonYear.lesson = lessYear;
+                        else
+                            lessonYear.open = lessYear;
+                        await cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync<LessonStats>(lessonYear, new PartitionKey($"LessonYear-{schoolId}"));
+                    }
+                }
+            }
+        }
+    }
+}

+ 6 - 0
TEAMModelOS.SDK/TEAMModelOS.SDK.csproj

@@ -45,4 +45,10 @@
 	<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Abstractions" Version="1.1.0" />
 	<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" />
   </ItemGroup>
+
+
+
+  <ItemGroup>
+    <Folder Include="Models\Cosmos\BI\BICommon\" />
+  </ItemGroup>
 </Project>