Ver Fonte

处理课例统计存储

Li há 3 anos atrás
pai
commit
c176236b30

+ 1 - 1
TEAMModelBI/Controllers/BISchool/LessonController.cs

@@ -101,7 +101,7 @@ namespace TEAMModelBI.Controllers.BISchool
             foreach (var item in unixsT)
             {
                 //await SetBILeesonStats(cosmosClient, item, num.GetInt32(), type: type.GetInt32(), schoolId: schoolId.GetString());
-                await BILeeson.SetCosmosDBStats(cosmosClient, _azureRedis, _dingDing, item, num.GetInt32(), type: type.GetInt32(), schoolId: schoolId.GetString());
+                //await BILeeson.SetCosmosDBStats(cosmosClient, _azureRedis, _dingDing, item, num.GetInt32(), type: type.GetInt32(), schoolId: schoolId.GetString());
                 await BILeeson.SetTableStats(_azureStorage, _azureRedis, _dingDing, item, num.GetInt32(), type: type.GetInt32(), schoolId: schoolId.GetString());
             }
 

+ 326 - 128
TEAMModelOS.SDK/Models/Service/BI/BILeeson.cs

@@ -1,4 +1,5 @@
 using Azure.Cosmos;
+using StackExchange.Redis;
 using System;
 using System.Collections.Generic;
 using System.Linq;
@@ -272,190 +273,387 @@ namespace TEAMModelOS.SDK.Models.Service.BI
                 DateTime expireDay = DateTime.UtcNow.AddDays(8);   //8天后到期
                 DateTime expireYear = DateTime.UtcNow.AddDays(396);   //一年后到期
 
+                ////保存当天的统计  小时
+                //SortedSetEntry[] dayCnt = null;
+                ////保一年的统计   天
+                //SortedSetEntry[] yearCnt = null;
                 string LessType = "Open";
                 if (type == 1)
                     LessType = "Lesson";
+                try
+                {
+                    await redisClient.SortedSetIncrementAsync($"BIStats:Less:All:{LessType}:{dateDay}", $"{hour}", num);//一天24小时课例数 有上传 base.josn  小时为单位
+                    await redisClient.SortedSetIncrementAsync($"BIStats:Less:All:{LessType}:{year}", $"{days}", num);//一年的课例数量 有上传 base.josn 小时为单位
 
-                await redisClient.SortedSetIncrementAsync($"BIStats:Less:All:{LessType}:{dateDay}", $"{hour}", num);//一天24小时课例数 有上传 base.josn  小时为单位
-                await redisClient.SortedSetIncrementAsync($"BIStats:Less:All:{LessType}:{year}", $"{days}", num);//一年的课例数量 有上传 base.josn 小时为单位
+                    var expDay = await _azureRedis.GetRedisClient(8).KeyTimeToLiveAsync($"BIStats:Less:All:{LessType}:{dateDay}");
+                    if (expDay == null)
+                        await _azureRedis.GetRedisClient(8).KeyExpireAsync($"BIStats:Less:All:{LessType}:{dateDay}", expireDay);  //设置八天后到期
+                    var expYear = await _azureRedis.GetRedisClient(8).KeyTimeToLiveAsync($"BIStats:Less:All:{LessType}:{year}");
+                    if (expYear == null)
+                        await _azureRedis.GetRedisClient(8).KeyExpireAsync($"BIStats:Less:All:{LessType}:{year}", expireYear);  //设置一年后到期
 
-                var expDay = await _azureRedis.GetRedisClient(8).KeyTimeToLiveAsync($"BIStats:Less:All:{LessType}:{dateDay}");
-                if (expDay == null)
-                    await _azureRedis.GetRedisClient(8).KeyExpireAsync($"BIStats:Less:All:{LessType}:{dateDay}", expireDay);  //设置八天后到期
-                var expYear = await _azureRedis.GetRedisClient(8).KeyTimeToLiveAsync($"BIStats:Less:All:{LessType}:{year}");
-                if (expYear == null)
-                    await _azureRedis.GetRedisClient(8).KeyExpireAsync($"BIStats:Less:All:{LessType}:{year}", expireYear);  //设置一年后到期
+                    ////保存当天的统计  小时
+                    //dayCnt = redisClient.SortedSetRangeByScoreWithScores($"BIStats:Less:All:{LessType}:{dateDay}");
 
-                //保存当天的统计  小时
-                var dayCnt = redisClient.SortedSetRangeByScoreWithScores($"BIStats:Less: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;
-                    }
+                    ////保一年的统计   天
+                    //yearCnt = redisClient.SortedSetRangeByScoreWithScores($"BIStats:Less:All:{LessType}:{year}");
+                }
+                catch { }
+
+                double[] daHour = new double[23];
+                daHour[hour] = 1;
+                string hourStats = string.Join(',', daHour);
 
-                    string hoursStats = string.Join(",", daHour);
-                    LessStats lessStats = table.Get<LessStats>("LessonHour", $"{dateDay}");
-                    if (lessStats != null)
+                double[] daYear = new double[yearDays];
+                daYear[hour] = 1;
+                string yearStats = string.Join(',', daYear);
+
+                LessStats lessHour = table.Get<LessStats>("LessonHour", $"{dateDay}");
+                if (lessHour != null)
+                {
+                    if (type == 1)
                     {
-                        if (type == 1)
-                            lessStats.lesson = hoursStats;
+                        if (lessHour.lesson != null)
+                        {
+                            double[] tempLess = Array.ConvertAll<string, double>(lessHour.lesson.Split(','), s => double.Parse(s));
+                            tempLess[hour] = tempLess[hour] + 1;
+                            string strLess = string.Join(',', tempLess);
+                            lessHour.lesson = strLess;
+                        }
                         else
-                            lessStats.open = hoursStats;
+                            lessHour.lesson = hourStats;
                     }
                     else
                     {
-                        lessStats = new() { PartitionKey = "LessonHour", RowKey = $"{dateDay}" };
-                        if (type == 1)
-                            lessStats.lesson = hoursStats;
+                        if (lessHour.open != null)
+                        {
+                            double[] tempOpen = Array.ConvertAll<string, double>(lessHour.open.Split(','), s => double.Parse(s));
+                            tempOpen[hour] = tempOpen[hour] + 1;
+                            string sOpen = string.Join(',', tempOpen);
+                            lessHour.open = sOpen;
+                        }
                         else
-                            lessStats.open = hoursStats;
+                            lessHour.open = hourStats;
                     }
-                    try
-                    {
-                        await table.SaveOrUpdate<LessStats>(lessStats);
-                    }
-                    catch {}
                 }
+                else
+                {
+                    lessHour = new() { PartitionKey = "LessonHour", RowKey = $"{dateDay}" };
+                    if (type == 1)
+                        lessHour.lesson = hourStats;
+                    else
+                        lessHour.open = hourStats;
+                }
+                await table.SaveOrUpdate<LessStats>(lessHour);
 
-                //保一年的统计   天
-                var yearCnt = redisClient.SortedSetRangeByScoreWithScores($"BIStats:Less:All:{LessType}:{year}");
-                if (yearCnt != null && yearCnt.Length > 0)
+                LessStats lessYear = table.Get<LessStats>("LessonYear", $"{year}");
+                if (lessYear != null)
                 {
-                    double[] daYear = new double[yearDays];
-                    foreach (var item in yearCnt)
-                    {
-                        double val = ((double)item.Score);
-                        int key = ((int)item.Element);
-                        daYear[key] = val;
-                    }
-                    string tempStats = string.Join(",", daYear);
-                    LessStats lessStats = table.Get<LessStats>("LessonYear", $"{year}");
-                    if (lessStats != null)
+                    if (type == 1)
                     {
-                        if (type == 1)
-                            lessStats.lesson = tempStats;
+                        if (lessYear.lesson != null)
+                        {
+                            double[] tempLess = Array.ConvertAll<string, double>(lessYear.lesson.Split(','), s => double.Parse(s));
+                            tempLess[hour] = tempLess[hour] + 1;
+                            string sLess = string.Join(',', tempLess);
+                            lessYear.lesson = sLess;
+                        }
                         else
-                            lessStats.open = tempStats;
+                            lessYear.lesson = yearStats;
                     }
                     else
                     {
-                        lessStats = new() { PartitionKey = "LessonYear", RowKey = $"{year}" };
-                        if (type == 1)
-                            lessStats.lesson = tempStats;
+                        if (lessYear.open != null)
+                        {
+                            double[] tempOpen = Array.ConvertAll<string, double>(lessYear.open.Split(','), s => double.Parse(s));
+                            tempOpen[days] = tempOpen[days] + 1;
+                            string sOpen = string.Join(',', tempOpen);
+                            lessYear.open = sOpen;
+                        }
                         else
-                            lessStats.open = tempStats;
+                            lessYear.open = yearStats;
                     }
-                    try
-                    {
-                        await table.SaveOrUpdate<LessStats>(lessStats);
-                    }
-                    catch { }
                 }
+                else
+                {
+                    lessYear = new() { PartitionKey = "LessonYear", RowKey = $"{year}" };
+                    if (type == 1)
+                        lessYear.lesson = yearStats;
+                    else
+                        lessYear.open = yearStats;
+                }
+                try
+                {
+                    await table.SaveOrUpdate<LessStats>(lessYear);
+                }
+                catch { }
+
+                //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;
+                //    }
+
+                //    string hoursStats = string.Join(",", daHour);
+                //    LessStats lessStats = table.Get<LessStats>("LessonHour", $"{dateDay}");
+                //    if (lessStats != null)
+                //    {
+                //        if (type == 1)
+                //            lessStats.lesson = hoursStats;
+                //        else
+                //            lessStats.open = hoursStats;
+                //    }
+                //    else
+                //    {
+                //        lessStats = new() { PartitionKey = "LessonHour", RowKey = $"{dateDay}" };
+                //        if (type == 1)
+                //            lessStats.lesson = hoursStats;
+                //        else
+                //            lessStats.open = hoursStats;
+                //    }
+                //    try
+                //    {
+                //        await table.SaveOrUpdate<LessStats>(lessStats);
+                //    }
+                //    catch {}
+                //}
+
+                //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;
+                //    }
+                //    string tempStats = string.Join(",", daYear);
+                //    LessStats lessStats = table.Get<LessStats>("LessonYear", $"{year}");
+                //    if (lessStats != null)
+                //    {
+                //        if (type == 1)
+                //            lessStats.lesson = tempStats;
+                //        else
+                //            lessStats.open = tempStats;
+                //    }
+                //    else
+                //    {
+                //        lessStats = new() { PartitionKey = "LessonYear", RowKey = $"{year}" };
+                //        if (type == 1)
+                //            lessStats.lesson = tempStats;
+                //        else
+                //            lessStats.open = tempStats;
+                //    }
+                //    try
+                //    {
+                //        await table.SaveOrUpdate<LessStats>(lessStats);
+                //    }
+                //    catch { }
+                //}
 
                 //保存学校课例数据
                 if (!string.IsNullOrEmpty(schoolId))
                 {
-                    await redisClient.SortedSetIncrementAsync($"BIStats:Less:{schoolId}:{LessType}:{dateDay}", $"{hour}", num);//一天24小时课例数 有上传 base.josn  小时为单位
-                    await redisClient.SortedSetIncrementAsync($"BIStats:Less:{schoolId}:{LessType}:{year}", $"{days}", num);//一年的课例数量 有上传 base.josn 小时为单位
+                    ////保存学校当天的统计  小时
+                    //SortedSetEntry[] dayScCnt = null;
+                    ////保学校一年的统计   天
+                    //SortedSetEntry[] scYearCnt = null;
+                    try
+                    {
+                        await redisClient.SortedSetIncrementAsync($"BIStats:Less:{schoolId}:{LessType}:{dateDay}", $"{hour}", num);//一天24小时课例数 有上传 base.josn  小时为单位
+                        await redisClient.SortedSetIncrementAsync($"BIStats:Less:{schoolId}:{LessType}:{year}", $"{days}", num);//一年的课例数量 有上传 base.josn 小时为单位
 
-                    var scExpDay = await _azureRedis.GetRedisClient(8).KeyTimeToLiveAsync($"BIStats:Less:{schoolId}:{LessType}:{dateDay}");
-                    if (scExpDay == null)
-                        await _azureRedis.GetRedisClient(8).KeyExpireAsync($"BIStats:Less:{schoolId}:{LessType}:{dateDay}", expireDay);  //设置八天后到期
-                    var scExpYear = await _azureRedis.GetRedisClient(8).KeyTimeToLiveAsync($"BIStats:Less:{schoolId}:{LessType}:{year}");
-                    if (scExpYear == null)
-                        await _azureRedis.GetRedisClient(8).KeyExpireAsync($"BIStats:Less:{schoolId}:{LessType}:{year}", expireYear);  //设置一年后到期
+                        var scExpDay = await _azureRedis.GetRedisClient(8).KeyTimeToLiveAsync($"BIStats:Less:{schoolId}:{LessType}:{dateDay}");
+                        if (scExpDay == null)
+                            await _azureRedis.GetRedisClient(8).KeyExpireAsync($"BIStats:Less:{schoolId}:{LessType}:{dateDay}", expireDay);  //设置八天后到期
+                        var scExpYear = await _azureRedis.GetRedisClient(8).KeyTimeToLiveAsync($"BIStats:Less:{schoolId}:{LessType}:{year}");
+                        if (scExpYear == null)
+                            await _azureRedis.GetRedisClient(8).KeyExpireAsync($"BIStats:Less:{schoolId}:{LessType}:{year}", expireYear);  //设置一年后到期
 
-                    //保存学校当天的统计  小时
-                    var dayScCnt = redisClient.SortedSetRangeByScoreWithScores($"BIStats:Less:{schoolId}:{LessType}:{dateDay}");
-                    if (dayScCnt != null && dayScCnt.Length > 0)
-                    {
-                        double[] daHourSc = new double[23];
-                        foreach (var item in dayScCnt)
-                        {
-                            double val = ((double)item.Score);
-                            int key = ((int)item.Element);
-                            daHourSc[key] = val;
-                        }
+                        ////保存学校当天的统计  小时
+                        //dayScCnt = redisClient.SortedSetRangeByScoreWithScores($"BIStats:Less:{schoolId}:{LessType}:{dateDay}");
+
+                        ////保学校一年的统计   天
+                        //scYearCnt = redisClient.SortedSetRangeByScoreWithScores($"BIStats:Less:{schoolId}:{LessType}:{year}");
+                    }catch { }
+
+                    double[] daScHour = new double[23];
+                    daScHour[hour] = 1;
+                    string hourScStats = string.Join(',', daScHour);
 
-                        string hoursStatsSc = string.Join(",", daHourSc);
-                        LessStats lessStatsSc = table.Get<LessStats>("LessonHour", $"{dateDay}-{schoolId}");
-                        if (lessStatsSc != null)
+                    double[] daScYear = new double[yearDays];
+                    daScYear[hour] = 1;
+                    string strYearSc = string.Join(',', daScYear);
+
+                    LessStats lessScHour = table.Get<LessStats>("LessonHour", $"{dateDay}-{schoolId}");
+                    if (lessScHour != null)
+                    {
+                        if (type == 1)
                         {
-                            if (type == 1)
-                                lessStatsSc.lesson = hoursStatsSc;
+                            if (lessScHour.lesson != null)
+                            {
+                                double[] tempLess = Array.ConvertAll<string, double>(lessScHour.lesson.Split(','), s => double.Parse(s));
+                                tempLess[hour] = tempLess[hour] + 1;
+                                string strLess = string.Join(',', tempLess);
+                                lessScHour.lesson = strLess;
+                            }
                             else
-                                lessStatsSc.open = hoursStatsSc;
+                                lessScHour.lesson = hourScStats;
                         }
                         else
                         {
-                            lessStatsSc = new() { PartitionKey = "LessonHour", RowKey = $"{dateDay}-{schoolId}" };
-                            if (type == 1)
-                                lessStatsSc.lesson = hoursStatsSc;
+                            if (lessScHour.open != null)
+                            {
+                                double[] tempOpen = Array.ConvertAll<string, double>(lessScHour.open.Split(','), s => double.Parse(s));
+                                tempOpen[hour] = tempOpen[hour] + 1;
+                                string sOpen = string.Join(',', tempOpen);
+                                lessScHour.open = sOpen;
+                            }
                             else
-                                lessStatsSc.open = hoursStatsSc;
-                            lessStatsSc.time = dateDay;
-                            lessStatsSc.school = schoolId;
-                        }
-
-                        try
-                        {
-                            await table.SaveOrUpdate<LessStats>(lessStatsSc);
+                                lessScHour.open = hourScStats;
                         }
-                        catch { }
                     }
-
-                    //保学校一年的统计   天
-                    var scYearCnt = redisClient.SortedSetRangeByScoreWithScores($"BIStats:Less:{schoolId}:{LessType}:{year}");
-                    if (scYearCnt != null && scYearCnt.Length > 0)
+                    else
                     {
-                        double[] daYearSc = new double[yearDays];
-                        foreach (var item in scYearCnt)
-                        {
-                            double val = ((double)item.Score);
-                            int key = ((int)item.Element);
-                            daYearSc[key] = val;
-                        }
+                        lessScHour = new() { PartitionKey = "LessonHour", RowKey = $"{dateDay}-{schoolId}" };
+                        if (type == 1)
+                            lessScHour.lesson = hourScStats;
+                        else
+                            lessScHour.open = hourScStats;
+                    }
+                    try
+                    {
+                        await table.SaveOrUpdate<LessStats>(lessScHour);
+                    }
+                    catch { }
 
-                        string tempStatsSc = string.Join(",", daYearSc);
-                        LessStats lessStatsSc = table.Get<LessStats>("LessonYear", $"{year}-{schoolId}");
-                        if (lessStatsSc != null)
+                    LessStats lessScYear = table.Get<LessStats>("LessonYear", $"{year}-{schoolId}");
+                    if (lessScYear != null)
+                    {
+                        if (type == 1)
                         {
-                            if (type == 1)
-                                lessStatsSc.lesson = tempStatsSc;
+                            if (lessScYear.lesson != null)
+                            {
+                                double[] tempLess = Array.ConvertAll<string, double>(lessScYear.lesson.Split(','), s => double.Parse(s));
+                                tempLess[hour] = tempLess[hour] + 1;
+                                string sLess = string.Join(',', tempLess);
+                                lessScYear.lesson = sLess;
+                            }
                             else
-                                lessStatsSc.open = tempStatsSc;
+                                lessScYear.lesson = strYearSc;
                         }
                         else
                         {
-                            lessStatsSc = new() { PartitionKey = "LessonYear", RowKey = $"{year}-{schoolId}" };
-                            if (type == 1)
-                                lessStatsSc.lesson = tempStatsSc;
+                            if (lessScYear.open != null)
+                            {
+                                double[] tempOpen = Array.ConvertAll<string, double>(lessScYear.open.Split(','), s => double.Parse(s));
+                                tempOpen[days] = tempOpen[days] + 1;
+                                string sOpen = string.Join(',', tempOpen);
+                                lessScYear.open = sOpen;
+                            }
                             else
-                                lessStatsSc.open = tempStatsSc;
-
-                            lessStatsSc.time = $"{year}";
-                            lessStatsSc.school = schoolId;
-                        }
-                        try
-                        {
-                            await table.SaveOrUpdate<LessStats>(lessStatsSc);
+                                lessScYear.open = strYearSc;
                         }
-                        catch { }
                     }
+                    else
+                    {
+                        lessScYear = new() { PartitionKey = "LessonYear", RowKey = $"{year}" };
+                        if (type == 1)
+                            lessScYear.lesson = strYearSc;
+                        else
+                            lessScYear.open = strYearSc;
+                    }
+                    try
+                    {
+                        await table.SaveOrUpdate<LessStats>(lessScYear);
+                    }
+                    catch { }
+
+                    //if (dayScCnt != null && dayScCnt.Length > 0)
+                    //{
+                    //    double[] daHourSc = new double[23];
+                    //    foreach (var item in dayScCnt)
+                    //    {
+                    //        double val = ((double)item.Score);
+                    //        int key = ((int)item.Element);
+                    //        daHourSc[key] = val;
+                    //    }
+
+                    //    string hoursStatsSc = string.Join(",", daHourSc);
+                    //    LessStats lessStatsSc = table.Get<LessStats>("LessonHour", $"{dateDay}-{schoolId}");
+                    //    if (lessStatsSc != null)
+                    //    {
+                    //        if (type == 1)
+                    //            lessStatsSc.lesson = hoursStatsSc;
+                    //        else
+                    //            lessStatsSc.open = hoursStatsSc;
+                    //    }
+                    //    else
+                    //    {
+                    //        lessStatsSc = new() { PartitionKey = "LessonHour", RowKey = $"{dateDay}-{schoolId}" };
+                    //        if (type == 1)
+                    //            lessStatsSc.lesson = hoursStatsSc;
+                    //        else
+                    //            lessStatsSc.open = hoursStatsSc;
+                    //        lessStatsSc.time = dateDay;
+                    //        lessStatsSc.school = schoolId;
+                    //    }
+
+                    //    try
+                    //    {
+                    //        await table.SaveOrUpdate<LessStats>(lessStatsSc);
+                    //    }
+                    //    catch { }
+                    //}
+
+                    //if (scYearCnt != null && scYearCnt.Length > 0)
+                    //{
+                    //    double[] daYearSc = new double[yearDays];
+                    //    foreach (var item in scYearCnt)
+                    //    {
+                    //        double val = ((double)item.Score);
+                    //        int key = ((int)item.Element);
+                    //        daYearSc[key] = val;
+                    //    }
+
+                    //    string tempStatsSc = string.Join(",", daYearSc);
+                    //    LessStats lessStatsSc = table.Get<LessStats>("LessonYear", $"{year}-{schoolId}");
+                    //    if (lessStatsSc != null)
+                    //    {
+                    //        if (type == 1)
+                    //            lessStatsSc.lesson = tempStatsSc;
+                    //        else
+                    //            lessStatsSc.open = tempStatsSc;
+                    //    }
+                    //    else
+                    //    {
+                    //        lessStatsSc = new() { PartitionKey = "LessonYear", RowKey = $"{year}-{schoolId}" };
+                    //        if (type == 1)
+                    //            lessStatsSc.lesson = tempStatsSc;
+                    //        else
+                    //            lessStatsSc.open = tempStatsSc;
+
+                    //        lessStatsSc.time = $"{year}";
+                    //        lessStatsSc.school = schoolId;
+                    //    }
+                    //    try
+                    //    {
+                    //        await table.SaveOrUpdate<LessStats>(lessStatsSc);
+                    //    }
+                    //    catch { }
+                    //}
                 }
 
                 Monitor.Enter(unix);  //锁对象  结束
-                //slimlock.Release(); // 对可同时访问资源或资源池的线程数加以限制 结束    
-
+                //slimlock.Release(); // 对可同时访问资源或资源池的线程数加以限制 结束
             }
             catch (Exception ex)
             {
-                await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-SetBITableStats  课例统计异常{unix}|{num}|{type}|{schoolId} ;错误信息:{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
+                await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-SetBITableStats  BI课例统计异常参数:{unix}|{num}|{type}|{schoolId} ;错误信息:{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
             }
         }
     }