Parcourir la source

[BI]IOT增量API 追加IOT累加項目

jeff il y a 1 an
Parent
commit
450196b682
1 fichiers modifiés avec 120 ajouts et 99 suppressions
  1. 120 99
      TEAMModelBI/Controllers/BITmid/TmidController.cs

+ 120 - 99
TEAMModelBI/Controllers/BITmid/TmidController.cs

@@ -587,104 +587,7 @@ namespace TEAMModelBI.Controllers.BITmid
                 {
                 {
                     return BadRequest();
                     return BadRequest();
                 }
                 }
-                //起始終止日期換算
-                long dateTimeFromSec = 0;
-                long dateTimeToSec = 0;
-                DateTimeOffset dateTimeFrom;
-                DateTimeOffset dateTimeTo;
-                List<string> dateFromList = dateFromStr.Split('-').ToList();
-                int dateFromYear = Convert.ToInt32(dateFromList[0], 10);
-                int dateFromMonth = (dateUnit.Equals("day") || dateUnit.Equals("month")) ? Convert.ToInt32(dateFromList[1], 10) : 1;
-                int dateFromDay = (dateUnit.Equals("day")) ? Convert.ToInt32(dateFromList[2], 10) : 1;
-                List<string> dateToList = dateToStr.Split('-').ToList();
-                int dateToYear = Convert.ToInt32(dateToList[0], 10);
-                int dateToMonth = (dateUnit.Equals("day") || dateUnit.Equals("month")) ? Convert.ToInt32(dateToList[1], 10) : 1;
-                int dateToDay = (dateUnit.Equals("day")) ? Convert.ToInt32(dateToList[2], 10) : 1;
-                switch (dateUnit)
-                {
-                    case "year":
-                        dateTimeFrom = new DateTimeOffset(dateFromYear, 1, 1, 0, 0, 0, TimeSpan.Zero);
-                        dateTimeTo = new DateTimeOffset(dateToYear, 12, 31, 23, 59, 59, TimeSpan.Zero);
-                        break;
-                    case "month":
-                        dateTimeFrom = new DateTimeOffset(dateFromYear, dateFromMonth, 1, 0, 0, 0, TimeSpan.Zero);
-                        dateTimeTo = new DateTimeOffset(dateToYear, dateToMonth, DateTime.DaysInMonth(dateToYear, dateToMonth), 23, 59, 59, TimeSpan.Zero);
-                        break;
-                    case "day":
-                        dateTimeFrom = new DateTimeOffset(dateFromYear, dateFromMonth, dateFromDay, 0, 0, 0, TimeSpan.Zero);
-                        dateTimeTo = new DateTimeOffset(dateToYear, dateToMonth, dateToDay, 23, 59, 59, TimeSpan.Zero);
-                        break;
-                    default: //同"month"
-                        dateTimeFrom = new DateTimeOffset(dateFromYear, dateFromMonth, 1, 0, 0, 0, TimeSpan.Zero);
-                        dateTimeTo = new DateTimeOffset(dateToYear, dateToMonth, DateTime.DaysInMonth(dateToYear, dateToMonth), 23, 59, 59, TimeSpan.Zero);
-                        break;
-                }
-                dateTimeFromSec = dateTimeFrom.ToUnixTimeSeconds();
-                dateTimeToSec = dateTimeTo.ToUnixTimeSeconds();
-                //資料取得
-                    //基本資料
-                Dictionary<string, TmidStics> tmidDic = new();
-                foreach (string id in tmids)
-                {
-                    if(!tmidDic.ContainsKey(id))
-                    {
-                        TmidStics tmidStics = new TmidStics() { id = id, iot = new() };
-                        tmidDic.Add(id, tmidStics);
-                    }
-                }
-                    //IOT資料
-                var cosmosClientIes5 = _azureCosmos.GetCosmosClient(); //CosmosDB IES5
-                List<TmidAnalysisCal> iotResult = new List<TmidAnalysisCal>();
-                Dictionary<string, List<TmidAnalysisCal>> iotDic = new Dictionary<string, List<TmidAnalysisCal>>();
-                if (dateUnit.ToLower().Equals("year"))
-                {
-                    iotDic["HiTeach-year"] = await getTMIDIotData(cosmosClientIes5, tmids, "HiTeach", "year", 0, 0, 0, dateTimeFromSec, dateTimeToSec);
-                    iotDic["HiTeachCC-year"] = await getTMIDIotData(cosmosClientIes5, tmids, "HiTeachCC", "year", 0, 0, 0, dateTimeFromSec, dateTimeToSec);
-                }
-                else if (dateUnit.ToLower().Equals("month"))
-                {
-                    iotDic["HiTeach-month"] = await getTMIDIotData(cosmosClientIes5, tmids, "HiTeach", "month", 0, 0, 0, dateTimeFromSec, dateTimeToSec);
-                    iotDic["HiTeachCC-month"] = await getTMIDIotData(cosmosClientIes5, tmids, "HiTeachCC", "month", 0, 0, 0, dateTimeFromSec, dateTimeToSec);
-                }
-                else if (dateUnit.ToLower().Equals("day"))
-                {
-                    iotDic["HiTeach-day"] = await getTMIDIotData(cosmosClientIes5, tmids, "HiTeach", "day", 0, 0, 0, dateTimeFromSec, dateTimeToSec);
-                    iotDic["HiTeachCC-day"] = await getTMIDIotData(cosmosClientIes5, tmids, "HiTeachCC", "day", 0, 0, 0, dateTimeFromSec, dateTimeToSec);
-                }
-                //IOT資料整理
-                List<string> iotDicKeyList = new List<string>() { "HiTeach-year", "HiTeachCC-year", "HiTeach-month", "HiTeachCC-month", "HiTeach-day", "HiTeachCC-day" };
-                foreach (string iotDicKey in iotDicKeyList)
-                {
-                    if (iotDic.ContainsKey(iotDicKey))
-                    {
-                        foreach (TmidAnalysisCal iotCalData in iotDic[iotDicKey])
-                        {
-                            string id = iotCalData.tmid;
-                            TmidStics tmidStics = tmidDic[id];
-                            switch (iotDicKey)
-                            {
-                                case "HiTeach-year":
-                                    tmidStics.iot.hiteach.year = iotCalData;
-                                    break;
-                                case "HiTeachCC-year":
-                                    tmidStics.iot.hiteachcc.year = iotCalData;
-                                    break;
-                                case "HiTeach-month":
-                                    tmidStics.iot.hiteach.month = iotCalData;
-                                    break;
-                                case "HiTeachCC-month":
-                                    tmidStics.iot.hiteachcc.month = iotCalData;
-                                    break;
-                                case "HiTeach-day":
-                                    tmidStics.iot.hiteach.day = iotCalData;
-                                    break;
-                                case "HiTeachCC-day":
-                                    tmidStics.iot.hiteachcc.day = iotCalData;
-                                    break;
-                            }
-                        }
-                    }
-                }
+                Dictionary<string, TmidStics> tmidDic = await GetTmidIotCore(tmids, dateFromStr, dateToStr, dateUnit);
                 //輸出
                 //輸出
                 List<object> data = new();
                 List<object> data = new();
                 foreach (KeyValuePair<string, TmidStics> dicItem in tmidDic)
                 foreach (KeyValuePair<string, TmidStics> dicItem in tmidDic)
@@ -699,6 +602,108 @@ namespace TEAMModelBI.Controllers.BITmid
                 return BadRequest();
                 return BadRequest();
             }
             }
         }
         }
+        private async Task<Dictionary<string, TmidStics>> GetTmidIotCore(List<string> tmids, string dateFromStr, string dateToStr, string dateUnit)
+        {
+            //起始終止日期換算
+            long dateTimeFromSec = 0;
+            long dateTimeToSec = 0;
+            DateTimeOffset dateTimeFrom;
+            DateTimeOffset dateTimeTo;
+            List<string> dateFromList = dateFromStr.Split('-').ToList();
+            int dateFromYear = Convert.ToInt32(dateFromList[0], 10);
+            int dateFromMonth = (dateUnit.Equals("day") || dateUnit.Equals("month")) ? Convert.ToInt32(dateFromList[1], 10) : 1;
+            int dateFromDay = (dateUnit.Equals("day")) ? Convert.ToInt32(dateFromList[2], 10) : 1;
+            List<string> dateToList = dateToStr.Split('-').ToList();
+            int dateToYear = Convert.ToInt32(dateToList[0], 10);
+            int dateToMonth = (dateUnit.Equals("day") || dateUnit.Equals("month")) ? Convert.ToInt32(dateToList[1], 10) : 1;
+            int dateToDay = (dateUnit.Equals("day")) ? Convert.ToInt32(dateToList[2], 10) : 1;
+            switch (dateUnit)
+            {
+                case "year":
+                    dateTimeFrom = new DateTimeOffset(dateFromYear, 1, 1, 0, 0, 0, TimeSpan.Zero);
+                    dateTimeTo = new DateTimeOffset(dateToYear, 12, 31, 23, 59, 59, TimeSpan.Zero);
+                    break;
+                case "month":
+                    dateTimeFrom = new DateTimeOffset(dateFromYear, dateFromMonth, 1, 0, 0, 0, TimeSpan.Zero);
+                    dateTimeTo = new DateTimeOffset(dateToYear, dateToMonth, DateTime.DaysInMonth(dateToYear, dateToMonth), 23, 59, 59, TimeSpan.Zero);
+                    break;
+                case "day":
+                    dateTimeFrom = new DateTimeOffset(dateFromYear, dateFromMonth, dateFromDay, 0, 0, 0, TimeSpan.Zero);
+                    dateTimeTo = new DateTimeOffset(dateToYear, dateToMonth, dateToDay, 23, 59, 59, TimeSpan.Zero);
+                    break;
+                default: //同"month"
+                    dateTimeFrom = new DateTimeOffset(dateFromYear, dateFromMonth, 1, 0, 0, 0, TimeSpan.Zero);
+                    dateTimeTo = new DateTimeOffset(dateToYear, dateToMonth, DateTime.DaysInMonth(dateToYear, dateToMonth), 23, 59, 59, TimeSpan.Zero);
+                    break;
+            }
+            dateTimeFromSec = dateTimeFrom.ToUnixTimeSeconds();
+            dateTimeToSec = dateTimeTo.ToUnixTimeSeconds();
+            //資料取得
+            Dictionary<string, TmidStics> tmidDic = new();
+            foreach (string id in tmids)
+            {
+                if (!tmidDic.ContainsKey(id))
+                {
+                    TmidStics tmidStics = new TmidStics() { id = id, iot = new() };
+                    tmidDic.Add(id, tmidStics);
+                }
+            }
+            //IOT資料
+            var cosmosClientIes5 = _azureCosmos.GetCosmosClient(); //CosmosDB IES5
+            List<TmidAnalysisCal> iotResult = new List<TmidAnalysisCal>();
+            Dictionary<string, List<TmidAnalysisCal>> iotDic = new Dictionary<string, List<TmidAnalysisCal>>();
+            if (dateUnit.ToLower().Equals("year"))
+            {
+                iotDic["HiTeach-year"] = await getTMIDIotData(cosmosClientIes5, tmids, "HiTeach", "year", 0, 0, 0, dateTimeFromSec, dateTimeToSec);
+                iotDic["HiTeachCC-year"] = await getTMIDIotData(cosmosClientIes5, tmids, "HiTeachCC", "year", 0, 0, 0, dateTimeFromSec, dateTimeToSec);
+            }
+            else if (dateUnit.ToLower().Equals("month"))
+            {
+                iotDic["HiTeach-month"] = await getTMIDIotData(cosmosClientIes5, tmids, "HiTeach", "month", 0, 0, 0, dateTimeFromSec, dateTimeToSec);
+                iotDic["HiTeachCC-month"] = await getTMIDIotData(cosmosClientIes5, tmids, "HiTeachCC", "month", 0, 0, 0, dateTimeFromSec, dateTimeToSec);
+            }
+            else if (dateUnit.ToLower().Equals("day"))
+            {
+                iotDic["HiTeach-day"] = await getTMIDIotData(cosmosClientIes5, tmids, "HiTeach", "day", 0, 0, 0, dateTimeFromSec, dateTimeToSec);
+                iotDic["HiTeachCC-day"] = await getTMIDIotData(cosmosClientIes5, tmids, "HiTeachCC", "day", 0, 0, 0, dateTimeFromSec, dateTimeToSec);
+            }
+            //IOT資料整理
+            List<string> iotDicKeyList = new List<string>() { "HiTeach-year", "HiTeachCC-year", "HiTeach-month", "HiTeachCC-month", "HiTeach-day", "HiTeachCC-day" };
+            foreach (string iotDicKey in iotDicKeyList)
+            {
+                if (iotDic.ContainsKey(iotDicKey))
+                {
+                    foreach (TmidAnalysisCal iotCalData in iotDic[iotDicKey])
+                    {
+                        string id = iotCalData.tmid;
+                        TmidStics tmidStics = tmidDic[id];
+                        switch (iotDicKey)
+                        {
+                            case "HiTeach-year":
+                                tmidStics.iot.hiteach.year = iotCalData;
+                                break;
+                            case "HiTeachCC-year":
+                                tmidStics.iot.hiteachcc.year = iotCalData;
+                                break;
+                            case "HiTeach-month":
+                                tmidStics.iot.hiteach.month = iotCalData;
+                                break;
+                            case "HiTeachCC-month":
+                                tmidStics.iot.hiteachcc.month = iotCalData;
+                                break;
+                            case "HiTeach-day":
+                                tmidStics.iot.hiteach.day = iotCalData;
+                                break;
+                            case "HiTeachCC-day":
+                                tmidStics.iot.hiteachcc.day = iotCalData;
+                                break;
+                        }
+                    }
+                }
+            }
+            return tmidDic;
+        }
+
 
 
         /// <summary>
         /// <summary>
         /// 取得TMID使用產品資料
         /// 取得TMID使用產品資料
@@ -1021,7 +1026,23 @@ namespace TEAMModelBI.Controllers.BITmid
                     geoRow.tmids = geoRow.tmids.Union(schRow.tmids).ToList();
                     geoRow.tmids = geoRow.tmids.Union(schRow.tmids).ToList();
                 }
                 }
             }
             }
-            return new { dateFrom = dateFromStr, dateTo = dateToStr, tmids, tmid, sch, geo };
+            //IOT累加 ※目前只放入tLesson、tGreen
+            Dictionary<string, int> iotResult = new Dictionary<string, int>();
+            iotResult.Add("tLesson", 0);
+            iotResult.Add("tGreen", 0);
+            Dictionary<string, TmidStics> iotDic = await GetTmidIotCore(tmids.ToList(), dateFromStr, dateToStr, "month");
+            foreach (KeyValuePair<string, TmidStics> iot in iotDic) 
+            {
+                string tid = iot.Key;
+                TmidStics iotCalData = iot.Value;
+                if(iotCalData.iot.hiteach.month != null)
+                {
+                    iotResult["tLesson"] += iotCalData.iot.hiteach.month.tLesson;
+                    iotResult["tGreen"] += iotCalData.iot.hiteach.month.tGreen;
+                }                
+            }
+
+            return new { dateFrom = dateFromStr, dateTo = dateToStr, tmids, tmid, sch, geo, iot = iotResult };
         }
         }
 
 
         //取得TMID服務授權週期
         //取得TMID服務授權週期