diff --git a/SolarPower/Controllers/DataBackFillController.cs b/SolarPower/Controllers/DataBackFillController.cs index 3c70fe4..485e194 100644 --- a/SolarPower/Controllers/DataBackFillController.cs +++ b/SolarPower/Controllers/DataBackFillController.cs @@ -15,6 +15,13 @@ namespace SolarPower.Controllers private readonly IPowerStationRepository powerStationRepository; private double carbonRate; + private enum tableType + { + hour = 0, + day = 1, + month = 2 + } + public DataBackFillController(IPowerStationRepository powerStationRepository) : base() { this.powerStationRepository = powerStationRepository; @@ -32,40 +39,15 @@ namespace SolarPower.Controllers try { - var targetDay = 7; //預計範圍天數 + var targetDay = 1; //預計範圍天數 - var now = DateTime.Now; + //var now = DateTime.Now; + var now = new DateTime(2021, 7, 26); var endDate = now.AddDays(-1); var endDateStr = endDate.ToString(dateTimeFormat); - var startDate = endDate.AddDays(-1 * targetDay); + //var startDate = endDate.AddDays(-1 * targetDay); + var startDate = now.AddDays(-1 * targetDay); var startDateStr = startDate.ToString(dateTimeFormat); - var timeArray = new List() - { - "00:05", - "01:05", - "02:05", - "03:05", - "04:05", - "05:05", - "06:05", - "07:05", - "08:05", - "09:05", - "10:05", - "11:05", - "12:05", - "13:05", - "14:05", - "15:05", - "16:05", - "17:05", - "18:05", - "19:05", - "20:05", - "21:05", - "22:05", - "23:05" - }; #region step1. 找出所有電站 var powerStations = await powerStationRepository.GetAllAsync(); @@ -76,184 +58,22 @@ namespace SolarPower.Controllers List pyrheliometerHistoriesHour = new List(); List TempHistoriesHour = new List(); - //逆變器歷史資料 - List inverterHistories = new List(); - - //5分鐘Sensor平均歷史資料 - List sensorAvgHistoryHour = new List(); - - #region 每小時歷史資料回填 - for (var i = 0; i < targetDay; i++) - { - var currentDateStr = startDate.AddDays(i).ToString(dateTimeFormat); - for (var j = 0; j < 24; j++) - { - var time = string.Format("{0}", j.ToString().PadLeft(2, '0')); - - var all_time = string.Format("{0} {1}", currentDateStr, time); - - foreach (var powerStation in powerStations) - { - #region step2. 電站歷史資料彙整(每小時) - var table_name = String.Format("s{1}01_station", powerStation.SiteDB, powerStation.Code); - var full_table_name = String.Format("`{0}`.`{1}`", powerStation.SiteDB, table_name); - var exist = await powerStationRepository.ExistTable(powerStation.SiteDB, table_name); - - if (!string.IsNullOrEmpty(exist)) - { - #region step2-1. 電站歷史資料 - var history = await powerStationRepository.GetPowerStationHistoryPerHour(all_time, full_table_name); - var lastmoneyhistorybyhour = await powerStationRepository.GetLastMoneyAndCarbonInHour(powerStation.Id, 0, all_time); - - if (history != null) - { - history.PowerStationId = powerStation.Id; - history.Timestamp = Convert.ToDateTime(history.Timestamp + ":00:00").ToString("yyyy-MM-dd HH:mm:ss"); - - carbonRate = Convert.ToDouble(await powerStationRepository.GetOneVariableByName("CarbonRate")); - - history.CARBON = history.KWH * carbonRate; - history.TODAYCARBON = lastmoneyhistorybyhour.TODAYCARBON + history.KWH * carbonRate; - history.TOTALCARBON = lastmoneyhistorybyhour.TOTALCARBON + history.KWH * carbonRate; - history.TODAYMONEY = lastmoneyhistorybyhour.TODAYMONEY + history.KWH * powerStation.PowerRate; - history.TOTALMONEY = lastmoneyhistorybyhour.TOTALMONEY + history.KWH * powerStation.PowerRate; - - powerStationHistoriesHour.Add(history); - } - #endregion - - #region step2-2. 取得該電站的當前這小時的日照度歷史資料 - //step2-2 1. 找出該電站所有日照計設備(包含共享 - var deviceInfos = await powerStationRepository.GetListPyrheliometerByPowerStationId(powerStation.Id, powerStation.SiteDB); - if (deviceInfos != null) - { - //step2-2 2. 計算該電站所有日照計設的每小時的平均在依照日照計數量平均 - var pyrheliometerHistory = await powerStationRepository.GetPyrheliometerHistoryPerHour(all_time, deviceInfos, 0); - - if (pyrheliometerHistory != null) - { - pyrheliometerHistory.Timestamp = Convert.ToDateTime(pyrheliometerHistory.Timestamp + ":00:00").ToString("yyyy-MM-dd HH:mm:ss"); - pyrheliometerHistory.PowerStationId = powerStation.Id; - pyrheliometerHistoriesHour.Add(pyrheliometerHistory); - } - } - #endregion - - #region step2-3. 取得該電站的當前這小時的溫度計歷史資料 - //step2-3 1. 找出該電站所有溫度計設備(包含共享 - var tempdeviceInfos = await powerStationRepository.GetListTempByPowerStationId(powerStation.Id, powerStation.SiteDB); - if (tempdeviceInfos != null) - { - //step2-3 2. 計算該電站所有溫度計設的每小時的平均在依照溫度計數量平均 - var tempHistory = await powerStationRepository.GetPyrheliometerHistoryPerHour(all_time, tempdeviceInfos, 1); - if (tempHistory != null) - { - tempHistory.Timestamp = Convert.ToDateTime(tempHistory.Timestamp + ":00:00").ToString("yyyy-MM-dd HH:mm:ss"); - tempHistory.PowerStationId = powerStation.Id; - TempHistoriesHour.Add(tempHistory); - } - } - #endregion - } - #endregion - - #region step3. 逆變器歷史資料彙整(每小時) - var controllers = await powerStationRepository.GetAllDeviceControllerId(powerStation.Id, powerStation.SiteDB); - var inverters = await powerStationRepository.InverterTable(controllers, powerStation.SiteDB); - var inverterIds = inverters.Where(x => x.Enabled == 1 && x.Status != 0).Select(x => x.InverterId).ToList(); - - var inverter_table_name = String.Format("s{0}01_inv", powerStation.Code); - var full_inverter_table_name = String.Format("`{0}`.`{1}`", powerStation.SiteDB, inverter_table_name); - var exist_inverter_table = await powerStationRepository.ExistTable(powerStation.SiteDB, inverter_table_name); - - if (!string.IsNullOrEmpty(exist_inverter_table)) - { - inverterHistories = await powerStationRepository.CalcInverterHisyortHourData(all_time, powerStation.SiteDB, full_inverter_table_name, inverterIds); - //取得日照計要找的欄位資訊 - var pyrheliometer = await powerStationRepository.GetFirstPyrheliometerInfo(powerStation.Id, powerStation.SiteDB); - var pyrheliometerValue = await powerStationRepository.GetFirstPyrheliometerValue(all_time, pyrheliometer.DBName, pyrheliometer.TableName, pyrheliometer.ColName); - foreach (var inverterHistory in inverterHistories) - { - inverterHistory.Irradiance = pyrheliometerValue; - inverterHistory.DC1KW = inverterHistory.DC1W / 1000; - inverterHistory.DC2KW = inverterHistory.DC2W / 1000; - inverterHistory.DC3KW = inverterHistory.DC3W / 1000; - inverterHistory.DC4KW = inverterHistory.DC4W / 1000; - inverterHistory.DC5KW = inverterHistory.DC5W / 1000; - - inverterHistory.DCKW = (inverterHistory.DC1W + inverterHistory.DC2W + inverterHistory.DC3W + inverterHistory.DC4W + inverterHistory.DC5W) / 1000; - inverterHistory.ACKW = (inverterHistory.AC1W + inverterHistory.AC2W + inverterHistory.AC3W) / 1000; - - inverterHistory.TIMESTAMP = Convert.ToDateTime(inverterHistory.TIMESTAMP + ":00:00").ToString("yyyy-MM-dd HH:mm:ss"); - inverterHistory.PowerStationId = powerStation.Id; - } - } - #endregion - - #region step4. 計算該電站所有sensoravg - var seneoravg_table_name = String.Format("s{0}01_sensoravg", powerStation.Code); - var full_seneoravg_table_name = String.Format("`{0}`.`{1}`", powerStation.SiteDB, seneoravg_table_name); - var exist_seneoravg_table = await powerStationRepository.ExistTable(powerStation.SiteDB, seneoravg_table_name); - if (!string.IsNullOrEmpty(exist_seneoravg_table)) - { - var sensorAvgHistory = await powerStationRepository.CalcSensorAvgHistory(all_time, full_seneoravg_table_name); - - if (sensorAvgHistory != null) - { - sensorAvgHistory.PowerStationId = powerStation.Id; - sensorAvgHistory.TIMESTAMP = Convert.ToDateTime(sensorAvgHistory.TIMESTAMP + ":00:00").ToString("yyyy-MM-dd HH:mm:ss"); - - sensorAvgHistoryHour.Add(sensorAvgHistory); - } - } - #endregion - } - } - } - - #region 刪除後新增該時間區間的電站歷史紀錄 - List history_properties = new List() + List powerStationHistoryDays = new List(); + List insertPowerStationHistoryMonths = new List(); + List pyrheliometerHistoryDays = new List(); + List insertPyrheliometerHistoryMonths = new List(); + List pyrheliometer_history_day_month_properties = new List() { "PowerStationId", - "TIMESTAMP", - "SITEID", - "SITETYPE", - "KWH", - "TODAYKWH", - "TOTALKWH", - "KWHKWP", - "PR", - "MP", - "SolarHour", - "MONEY", - "CARBON", - "TODAYMONEY", - "TOTALMONEY", - "TODAYCARBON", - "TOTALCARBON" - }; - await powerStationRepository.AddAfterPurgePowerStationHistoryHour(startDateStr, endDateStr, powerStationHistoriesHour, history_properties); - #endregion - - #region 刪除後新增該時間區間的日照溫度歷史紀錄 - List pyrheliometer_history_properties = new List() - { - "PowerStationId", - "TIMESTAMP", - "Irradiance" - }; - await powerStationRepository.AddAfterPurgePyrheliometerHistoryHour(startDateStr, endDateStr, pyrheliometerHistoriesHour, pyrheliometer_history_properties); - - List Temp_history_properties = new List() - { - "PowerStationId", - "TIMESTAMP", + "Timestamp", + "Irradiance", "Temperature" }; - await powerStationRepository.AddTempHistory(TempHistoriesHour, Temp_history_properties); - #endregion - #region 刪除後新增該時間區間的逆變器歷史紀錄 + //逆變器歷史資料 + List inverterHistoriesHour = new List(); + List inverterHistorDays = new List(); + List insertInverterHistoryMonths = new List(); List inverter_history_properties = new List() { "PowerStationId", @@ -313,69 +133,620 @@ namespace SolarPower.Controllers "TOTALKWH", "KWHKWP", }; - await powerStationRepository.AddAfterPurgeInverterHistoryHour(startDateStr, endDateStr, inverterHistories, inverter_history_properties); - #endregion - #region 刪除後新增該時間區間的SensorAvg歷史紀錄 - List sensoravg_history_properties = new List() + //5分鐘Sensor平均歷史資料 + List sensorAvgHistoryHour = new List(); + List sensorAvgHistoryDays = new List(); + List insertSensorAvgHistoryMonths = new List(); + + //#region step2. 每小時歷史資料回填 + //for (var i = 0; i < targetDay; i++) + //{ + // var currentDateStr = startDate.AddDays(i).ToString(dateTimeFormat); + // var temp_start_time = DateTime.Now; + // for (var j = 0; j < 24; j++) + // { + // var time = string.Format("{0}", j.ToString().PadLeft(2, '0')); + + // var all_time = string.Format("{0} {1}", currentDateStr, time); + + // var count = 0; + // foreach (var powerStation in powerStations) + // { + // if (count > 0) + // { + // break; + // } + + // #region step2-1. 電站歷史資料彙整(每小時) + // var table_name = String.Format("s{1}01_station", powerStation.SiteDB, powerStation.Code); + // var full_table_name = String.Format("`{0}`.`{1}`", powerStation.SiteDB, table_name); + // var exist = await powerStationRepository.ExistTable(powerStation.SiteDB, table_name); + + // if (!string.IsNullOrEmpty(exist)) + // { + // #region step2-1-1. 電站歷史資料 + // var history = await powerStationRepository.GetPowerStationHistoryPerHour(all_time, full_table_name); + // var lastmoneyhistorybyhour = await powerStationRepository.GetLastMoneyAndCarbonInHour(powerStation.Id, 0, all_time); + + // if (history != null) + // { + // history.PowerStationId = powerStation.Id; + // history.Timestamp = Convert.ToDateTime(history.Timestamp + ":00:00").ToString("yyyy-MM-dd HH:mm:ss"); + + // carbonRate = Convert.ToDouble(await powerStationRepository.GetOneVariableByName("CarbonRate")); + + // history.CARBON = history.KWH * carbonRate; + // history.TODAYCARBON = lastmoneyhistorybyhour.TODAYCARBON + history.KWH * carbonRate; + // history.TOTALCARBON = lastmoneyhistorybyhour.TOTALCARBON + history.KWH * carbonRate; + // history.TODAYMONEY = lastmoneyhistorybyhour.TODAYMONEY + history.KWH * powerStation.PowerRate; + // history.TOTALMONEY = lastmoneyhistorybyhour.TOTALMONEY + history.KWH * powerStation.PowerRate; + + // powerStationHistoriesHour.Add(history); + // } + // #endregion + + // #region step2-1-2. 取得該電站的當前這小時的日照度歷史資料 + // //step2-1-2 1. 找出該電站所有日照計設備(包含共享 + // var deviceInfos = await powerStationRepository.GetListPyrheliometerByPowerStationId(powerStation.Id, powerStation.SiteDB); + // if (deviceInfos != null) + // { + // //step2-1-2 2. 計算該電站所有日照計設的每小時的平均在依照日照計數量平均 + // var pyrheliometerHistory = await powerStationRepository.GetPyrheliometerHistoryPerHour(all_time, deviceInfos, 0); + + // if (pyrheliometerHistory != null) + // { + // pyrheliometerHistory.Timestamp = Convert.ToDateTime(pyrheliometerHistory.Timestamp + ":00:00").ToString("yyyy-MM-dd HH:mm:ss"); + // pyrheliometerHistory.PowerStationId = powerStation.Id; + // pyrheliometerHistoriesHour.Add(pyrheliometerHistory); + // } + // } + // #endregion + + // #region step2-1-3. 取得該電站的當前這小時的溫度計歷史資料 + // //step2-1-3 1. 找出該電站所有溫度計設備(包含共享 + // var tempdeviceInfos = await powerStationRepository.GetListTempByPowerStationId(powerStation.Id, powerStation.SiteDB); + // if (tempdeviceInfos != null) + // { + // //step2-1-3 2. 計算該電站所有溫度計設的每小時的平均在依照溫度計數量平均 + // var tempHistory = await powerStationRepository.GetPyrheliometerHistoryPerHour(all_time, tempdeviceInfos, 1); + // if (tempHistory != null) + // { + // tempHistory.Timestamp = Convert.ToDateTime(tempHistory.Timestamp + ":00:00").ToString("yyyy-MM-dd HH:mm:ss"); + // tempHistory.PowerStationId = powerStation.Id; + // TempHistoriesHour.Add(tempHistory); + // } + // } + // #endregion + // } + // #endregion + + // #region step2-2. 逆變器歷史資料彙整(每小時) + // var controllers = await powerStationRepository.GetAllDeviceControllerId(powerStation.Id, powerStation.SiteDB); + // var inverters = await powerStationRepository.InverterTable(controllers, powerStation.SiteDB); + // var inverterIds = inverters.Where(x => x.Enabled == 1 && x.Status != 0).Select(x => x.InverterId).ToList(); + + // var inverter_table_name = String.Format("s{0}01_inv", powerStation.Code); + // var full_inverter_table_name = String.Format("`{0}`.`{1}`", powerStation.SiteDB, inverter_table_name); + // var exist_inverter_table = await powerStationRepository.ExistTable(powerStation.SiteDB, inverter_table_name); + + // if (!string.IsNullOrEmpty(exist_inverter_table)) + // { + // inverterHistoriesHour = await powerStationRepository.CalcInverterHisyortHourData(all_time, powerStation.SiteDB, full_inverter_table_name, inverterIds); + // //取得日照計要找的欄位資訊 + // var pyrheliometer = await powerStationRepository.GetFirstPyrheliometerInfo(powerStation.Id, powerStation.SiteDB); + // var pyrheliometerValue = await powerStationRepository.GetFirstPyrheliometerValue(all_time, pyrheliometer.DBName, pyrheliometer.TableName, pyrheliometer.ColName); + // foreach (var inverterHistory in inverterHistoriesHour) + // { + // inverterHistory.Irradiance = pyrheliometerValue; + // inverterHistory.DC1KW = inverterHistory.DC1W / 1000; + // inverterHistory.DC2KW = inverterHistory.DC2W / 1000; + // inverterHistory.DC3KW = inverterHistory.DC3W / 1000; + // inverterHistory.DC4KW = inverterHistory.DC4W / 1000; + // inverterHistory.DC5KW = inverterHistory.DC5W / 1000; + + // inverterHistory.DCKW = (inverterHistory.DC1W + inverterHistory.DC2W + inverterHistory.DC3W + inverterHistory.DC4W + inverterHistory.DC5W) / 1000; + // inverterHistory.ACKW = (inverterHistory.AC1W + inverterHistory.AC2W + inverterHistory.AC3W) / 1000; + + // inverterHistory.TIMESTAMP = Convert.ToDateTime(inverterHistory.TIMESTAMP + ":00:00").ToString("yyyy-MM-dd HH:mm:ss"); + // inverterHistory.PowerStationId = powerStation.Id; + // } + // } + // #endregion + + // #region step2-3. 計算該電站所有sensoravg + // var seneoravg_table_name = String.Format("s{0}01_sensoravg", powerStation.Code); + // var full_seneoravg_table_name = String.Format("`{0}`.`{1}`", powerStation.SiteDB, seneoravg_table_name); + // var exist_seneoravg_table = await powerStationRepository.ExistTable(powerStation.SiteDB, seneoravg_table_name); + // if (!string.IsNullOrEmpty(exist_seneoravg_table)) + // { + // var sensorAvgHistory = await powerStationRepository.CalcSensorAvgHistory(all_time, full_seneoravg_table_name); + + // if (sensorAvgHistory != null) + // { + // sensorAvgHistory.PowerStationId = powerStation.Id; + // sensorAvgHistory.TIMESTAMP = Convert.ToDateTime(sensorAvgHistory.TIMESTAMP + ":00:00").ToString("yyyy-MM-dd HH:mm:ss"); + + // sensorAvgHistoryHour.Add(sensorAvgHistory); + // } + // } + // #endregion + // count++; + // } + // } + // var temp_end_time = DateTime.Now; + + // var runtime = temp_end_time.Subtract(temp_start_time).TotalSeconds; + // Logger.LogInformation("單一天執行小時回填運行時間{0}", runtime.ToString()); + //} + + //#region 刪除後新增該時間區間的電站歷史紀錄(小時資料表) + //List history_properties = new List() + //{ + // "PowerStationId", + // "TIMESTAMP", + // "SITEID", + // "SITETYPE", + // "KWH", + // "TODAYKWH", + // "TOTALKWH", + // "KWHKWP", + // "PR", + // "MP", + // "SolarHour", + // "MONEY", + // "CARBON", + // "TODAYMONEY", + // "TOTALMONEY", + // "TODAYCARBON", + // "TOTALCARBON" + //}; + //await powerStationRepository.AddAfterPurgePowerStationHistoryHour(startDateStr, endDateStr, powerStationHistoriesHour, history_properties); + //#endregion + + //#region 刪除後新增該時間區間的日照溫度歷史紀錄(小時資料表) + //List pyrheliometer_history_properties = new List() + //{ + // "PowerStationId", + // "TIMESTAMP", + // "Irradiance" + //}; + //await powerStationRepository.AddAfterPurgePyrheliometerHistory(startDateStr, endDateStr, (byte)tableType.hour, pyrheliometerHistoriesHour, pyrheliometer_history_properties); + + //List Temp_history_properties = new List() + //{ + // "PowerStationId", + // "TIMESTAMP", + // "Temperature" + //}; + //await powerStationRepository.AddTempHistory(TempHistoriesHour, Temp_history_properties); + //#endregion + + //#region 刪除後新增該時間區間的逆變器歷史紀錄(小時資料表) + + //await powerStationRepository.AddAfterPurgeInverterHistory(startDateStr, endDateStr, (byte)tableType.hour, inverterHistoriesHour, inverter_history_properties); + //#endregion + + //#region 刪除後新增該時間區間的SensorAvg歷史紀錄(小時資料表) + //List sensoravg_history_properties = new List() + //{ + // "PowerStationId", + // "TIMESTAMP", + // "SENSORAVG01", + // "SENSORAVG02", + // "SENSORAVG03", + // "SENSORAVG04", + // "SENSORAVG05", + // "SENSORAVG06", + // "SENSORAVG07", + // "SENSORAVG08", + // "SENSORAVG09", + // "SENSORAVG10", + // "SENSORAVG11", + // "SENSORAVG12", + // "SENSORAVG13", + // "SENSORAVG14", + // "SENSORAVG15", + // "SENSORAVG16", + // "SENSORAVG17", + // "SENSORAVG18", + // "SENSORAVG19", + // "SENSORAVG20", + // "SENSORAVG21", + // "SENSORAVG22", + // "SENSORAVG23", + // "SENSORAVG24", + // "SENSORAVG25", + // "SENSORAVG26", + // "SENSORAVG27", + // "SENSORAVG28", + // "SENSORAVG29", + // "SENSORAVG30", + // "SENSORAVG31", + // "SENSORAVG32", + // "SENSORAVG33", + // "SENSORAVG34", + // "SENSORAVG35", + // "SENSORAVG36", + // "SENSORAVG37", + // "SENSORAVG38", + // "SENSORAVG39", + // "SENSORAVG40", + // "SENSORAVG41", + // "SENSORAVG42", + // "SENSORAVG43", + // "SENSORAVG44", + // "SENSORAVG45", + // "SENSORAVG46", + // "SENSORAVG47", + // "SENSORAVG48", + // "SENSORAVG49", + // "SENSORAVG50", + //}; + //await powerStationRepository.AddAfterPurgeSensorAvgHistory(startDateStr, endDateStr, (byte)tableType.hour, sensorAvgHistoryHour, sensoravg_history_properties); + //#endregion + + //#endregion + + //#region step3. 每天歷史資料回填 + //for (var i = 0; i < targetDay; i++) + //{ + // var currentDateStr = startDate.AddDays(i).ToString(dateTimeFormat); + // var temp_start_time = DateTime.Now; + + // var count = 0; + // foreach (var powerStation in powerStations) + // { + // if (count > 0) + // { + // break; + // } + + // #region step3-1. 電站歷史資料彙整(每天) + // var table_name = String.Format("s{1}01_station", powerStation.SiteDB, powerStation.Code); + // var full_table_name = String.Format("`{0}`.`{1}`", powerStation.SiteDB, table_name); + // var exist = await powerStationRepository.ExistTable(powerStation.SiteDB, table_name); + // if (!string.IsNullOrEmpty(exist)) + // { + // var historyDay = await powerStationRepository.GetLastOnePowerStationHistoryByDay(currentDateStr, full_table_name); + // var moneyandcarbon = await powerStationRepository.GetMoneyAndCarbonWithHistoryHour(powerStation.Id, currentDateStr, 1); + // var lastmoneyhistory = await powerStationRepository.GetLastMoneyAndCarbonInHour(powerStation.Id, 1, ""); + // if (historyDay != null) + // { + // historyDay.PowerStationId = powerStation.Id; + // historyDay.CARBON = moneyandcarbon.CARBON; + // historyDay.MONEY = moneyandcarbon.MONEY; + // //historyDay.TODAYCARBON = lastmoneyhistory.TODAYCARBON; + // //historyDay.TODAYMONEY = lastmoneyhistory.TODAYMONEY ; + // historyDay.TOTALCARBON = lastmoneyhistory.TOTALCARBON + moneyandcarbon.CARBON; + // historyDay.TOTALMONEY = lastmoneyhistory.TOTALMONEY + moneyandcarbon.MONEY; + + // //日期轉換 + // historyDay.Timestamp = Convert.ToDateTime(historyDay.Timestamp + ":00:00").ToString("yyyy-MM-dd"); + // powerStationHistoryDays.Add(historyDay); + // } + // } + // #endregion + + // #region step3-2. 電站日照計溫度歷史資料彙整(每天) + // var pyrheliometerHistorDay = await powerStationRepository.CalcPyrheliometerHistoryDayDataByPowerStationId(currentDateStr, powerStation.Id); + // if (pyrheliometerHistorDay != null) + // { + // pyrheliometerHistoryDays.Add(pyrheliometerHistorDay); + // } + // #endregion + + // #region step3-3. 逆變器歷史資料彙整(每天) + // var inverterHistoriesDay = await powerStationRepository.CalcInverterHistoryDayDataByPowerStationId(currentDateStr, powerStation.SiteDB, powerStation.Id); + // if (inverterHistoriesDay != null) + // { + // foreach (var inverterHistoryDay in inverterHistoriesDay) + // { + // inverterHistoryDay.DC1KW = inverterHistoryDay.DC1W / 1000; + // inverterHistoryDay.DC2KW = inverterHistoryDay.DC2W / 1000; + // inverterHistoryDay.DC3KW = inverterHistoryDay.DC3W / 1000; + // inverterHistoryDay.DC4KW = inverterHistoryDay.DC4W / 1000; + // inverterHistoryDay.DC5KW = inverterHistoryDay.DC5W / 1000; + + // inverterHistoryDay.DCKW = (inverterHistoryDay.DC1W + inverterHistoryDay.DC2W + inverterHistoryDay.DC3W + inverterHistoryDay.DC4W + inverterHistoryDay.DC5W) / 1000; + // inverterHistoryDay.ACKW = (inverterHistoryDay.AC1W + inverterHistoryDay.AC2W + inverterHistoryDay.AC3W) / 1000; + + // inverterHistorDays.Add(inverterHistoryDay); + // } + // } + // #endregion + + // #region step3-4. 計算該電站所有sensoravg(每天) + // var sensorAvgHistoryDay = await powerStationRepository.CalcSensorAvgDayDataByPowerStationId(currentDateStr, powerStation.Id); + // if (sensorAvgHistoryDay != null) + // { + // sensorAvgHistoryDays.Add(sensorAvgHistoryDay); + // } + // #endregion + // count++; + // } + + // var temp_end_time = DateTime.Now; + + // var runtime = temp_end_time.Subtract(temp_start_time).TotalSeconds; + // Logger.LogInformation("單一天執行天回填運行時間{0}", runtime.ToString()); + //} + + //#region 刪除後新增該時間區間的電站歷史紀錄(天資料表) + //List history_properties_day = new List() + //{ + // "PowerStationId", + // "TIMESTAMP", + // "SITEID", + // "SITETYPE", + // "TODAYKWH", + // "TOTALKWH", + // "KWHKWP", + // "PR", + // "MP", + // "SolarHour", + // "MONEY", + // "CARBON", + // "TOTALMONEY", + // "TOTALCARBON" + //}; + //await powerStationRepository.AddAfterPurgePowerStationHistoryDay(startDateStr, endDateStr, powerStationHistoryDays, history_properties_day); + //#endregion + + //#region 刪除後新增該時間區間的日照溫度歷史紀錄(天資料表) + //await powerStationRepository.AddAfterPurgePyrheliometerHistory(startDateStr, endDateStr, (byte)tableType.day, pyrheliometerHistoryDays, pyrheliometer_history_day_month_properties); + //#endregion + + //#region 刪除後新增該時間區間的逆變器歷史紀錄(天資料表) + //await powerStationRepository.AddAfterPurgeInverterHistory(startDateStr, endDateStr, (byte)tableType.day, inverterHistorDays, inverter_history_properties); + //#endregion + + //#region 刪除後新增該時間區間的SensorAvg歷史紀錄(天資料表) + //await powerStationRepository.AddAfterPurgeSensorAvgHistory(startDateStr, endDateStr, (byte)tableType.day, sensorAvgHistoryDays, sensoravg_history_properties); + //#endregion + + //#endregion + + //#region step4. 每月歷史資料回填 + //var tempMonth = ""; + //for (var i = 0; i < targetDay; i++) + //{ + // var currentDateStr = startDate.AddDays(i).ToString("yyyy-MM"); + // if (string.Compare(tempMonth, currentDateStr) == 0) + // { + // continue; + // } + // tempMonth = currentDateStr; + + // foreach (var powerStation in powerStations) + // { + // #region step4-1. 電站歷史資料彙整(每月) + // var historyMonth = await powerStationRepository.ClacPowerStationHistoryMonthDataByPowerStationId(powerStation.Id, currentDateStr); + // var moneyandcarbonMon = await powerStationRepository.GetMoneyAndCarbonWithHistoryHour(powerStation.Id, currentDateStr, 0); + // var lastmoneyhistorymonth = await powerStationRepository.GetLastMoneyAndCarbonInHour(powerStation.Id, 2, ""); + // if (historyMonth != null) + // { + // historyMonth.Timestamp = Convert.ToDateTime(historyMonth.Timestamp).ToString("yyyy-MM-dd"); + // historyMonth.MONEY = moneyandcarbonMon.MONEY; + // historyMonth.CARBON = moneyandcarbonMon.CARBON; + // historyMonth.TOTALCARBON = lastmoneyhistorymonth.TOTALCARBON + moneyandcarbonMon.CARBON; + // historyMonth.TOTALMONEY = lastmoneyhistorymonth.TOTALMONEY + moneyandcarbonMon.MONEY; + + // insertPowerStationHistoryMonths.Add(historyMonth); + // } + // #endregion + + // #region step4-2. 電站日照計溫度歷史資料彙整(每月) + // var pyrheliometerHistoryMonth = await powerStationRepository.CalcPyrheliometerHistoryMonthDataByPowerStationId(currentDateStr, powerStation.Id); + // if (pyrheliometerHistoryMonth != null) + // { + // pyrheliometerHistoryMonth.Timestamp = Convert.ToDateTime(pyrheliometerHistoryMonth.Timestamp).ToString("yyyy-MM-dd"); + // insertPyrheliometerHistoryMonths.Add(pyrheliometerHistoryMonth); + // } + // #endregion + + // #region step4-3. 逆變器歷史資料彙整(每月) + // var inverterHistoriesMonth = await powerStationRepository.CalcInverterHistoryMonthDataByPowerStationId(currentDateStr, powerStation.SiteDB, powerStation.Id); + // if (inverterHistoriesMonth.Count > 0) + // { + // foreach (var inverterHistoryMonth in inverterHistoriesMonth) + // { + // inverterHistoryMonth.DC1KW = inverterHistoryMonth.DC1W / 1000; + // inverterHistoryMonth.DC2KW = inverterHistoryMonth.DC2W / 1000; + // inverterHistoryMonth.DC3KW = inverterHistoryMonth.DC3W / 1000; + // inverterHistoryMonth.DC4KW = inverterHistoryMonth.DC4W / 1000; + // inverterHistoryMonth.DC5KW = inverterHistoryMonth.DC5W / 1000; + + // inverterHistoryMonth.DCKW = (inverterHistoryMonth.DC1W + inverterHistoryMonth.DC2W + inverterHistoryMonth.DC3W + inverterHistoryMonth.DC4W + inverterHistoryMonth.DC5W) / 1000; + // inverterHistoryMonth.ACKW = (inverterHistoryMonth.AC1W + inverterHistoryMonth.AC2W + inverterHistoryMonth.AC3W) / 1000; + + // inverterHistoryMonth.TIMESTAMP = Convert.ToDateTime(inverterHistoryMonth.TIMESTAMP).ToString("yyyy-MM-dd"); + // insertInverterHistoryMonths.Add(inverterHistoryMonth); + // } + // } + // #endregion + + // #region step4-4. 計算該電站所有sensoravg(每月) + // var sensorAvgHistoryMonth = await powerStationRepository.CalcSensorAvgHistoryMonthDataByPowerStationId(currentDateStr, powerStation.Id); + // if (sensorAvgHistoryMonth != null) + // { + // sensorAvgHistoryMonth.TIMESTAMP = Convert.ToDateTime(sensorAvgHistoryMonth.TIMESTAMP).ToString("yyyy-MM-dd"); + // insertSensorAvgHistoryMonths.Add(sensorAvgHistoryMonth); + // } + // #endregion + // } + //} + + //#region 刪除後新增該時間區間的電站歷史紀錄(月資料表) + //List history_properties_month = new List() + //{ + // "PowerStationId", + // "TIMESTAMP", + // "SITEID", + // "SITETYPE", + // "MonthKWh", + // "TOTALKWH", + // "KWHKWP", + // "PR", + // "MP", + // "SolarHour", + // "MONEY", + // "CARBON", + // "TOTALMONEY", + // "TOTALCARBON" + //}; + //await powerStationRepository.AddAfterPurgePowerStationHistoryMonth(startDateStr, endDateStr, insertPowerStationHistoryMonths, history_properties_month); + //#endregion + + //#region 刪除後新增該時間區間的日照溫度歷史紀錄(月資料表) + //await powerStationRepository.AddAfterPurgePyrheliometerHistory(startDateStr, endDateStr, (byte)tableType.month, insertPyrheliometerHistoryMonths, pyrheliometer_history_day_month_properties); + //#endregion + + //#region 刪除後新增該時間區間的逆變器歷史紀錄(月資料表) + //await powerStationRepository.AddAfterPurgeInverterHistory(startDateStr, endDateStr, (byte)tableType.month, insertInverterHistoryMonths, inverter_history_properties); + //#endregion + + //#region 刪除後新增該時間區間的SensorAvg歷史紀錄(月資料表) + //await powerStationRepository.AddAfterPurgeSensorAvgHistory(startDateStr, endDateStr, (byte)tableType.month, insertSensorAvgHistoryMonths, sensoravg_history_properties); + //#endregion + + //#endregion + + //#region step5. 每15分鐘逆變器歷史資料回填 + + //var calcInverter15mins = new List(); + //for (var i = 0; i < targetDay; i++) + //{ + // var currentDateStr = startDate.AddDays(i).ToString("yyyy-MM-dd"); + + // var temp_start_time = DateTime.Now; + + // for (var j = 0; j < 24; j++) + // { + // var time = string.Format("{0}", j.ToString().PadLeft(2, '0')); + // var min_interval = new List() { "02", "17", "32", "47" }; + + // for (var k = 0; k < min_interval.Count(); k++) + // { + // if(i == 0 && j == 0 && k== 0) + // { //第一迴圈是跑開始日期的昨天最後一筆,故不執行 + // continue; + // } + + // var all_time = string.Format("{0} {1}:{2}", currentDateStr, time, min_interval[k]); + // var count = 0; + // foreach (var powerStation in powerStations) + // { + // if (count > 0) + // { + // break; + // } + + // var controllers = await powerStationRepository.GetAllDeviceControllerId(powerStation.Id, powerStation.SiteDB); + // var inverters = await powerStationRepository.InverterTable(controllers, powerStation.SiteDB); + // var inverterIds = inverters.Where(x => x.Enabled == 1 && x.Status != 0).Select(x => x.InverterId).ToList(); + + // var table_name = String.Format("s{1}01_inv", powerStation.SiteDB, powerStation.Code); + // var full_table_name = String.Format("`{0}`.`{1}`", powerStation.SiteDB, table_name); + // var exist = await powerStationRepository.ExistTable(powerStation.SiteDB, table_name); + + // if (!string.IsNullOrEmpty(exist)) + // { + // var calcInverter15min = await powerStationRepository.CalcInverterHisyort15minData(all_time, powerStation.SiteDB, full_table_name, inverterIds); + // if (calcInverter15min.Count() > 0) + // { + // foreach (var inverterHistory in calcInverter15min) + // { + // inverterHistory.TIMESTAMP = Convert.ToDateTime(inverterHistory.TIMESTAMP + ":00").ToString("yyyy-MM-dd HH:mm:ss"); + // inverterHistory.PowerStationId = powerStation.Id; + + // calcInverter15mins.Add(inverterHistory); + // } + // } + // } + + // count++; + // } + // } + // } + + // var temp_end_time = DateTime.Now; + + // var runtime = temp_end_time.Subtract(temp_start_time).TotalSeconds; + // Logger.LogInformation("單一天執行15回填運行時間{0}", runtime.ToString()); + //} + + ////需再針對最後一天的最後一個時段補上資料 + //var last_all_time = string.Format("{0} {1}", endDate.AddDays(1).ToString(dateTimeFormat), "00:02"); + //foreach (var powerStation in powerStations) + //{ + // var controllers = await powerStationRepository.GetAllDeviceControllerId(powerStation.Id, powerStation.SiteDB); + // var inverters = await powerStationRepository.InverterTable(controllers, powerStation.SiteDB); + // var inverterIds = inverters.Where(x => x.Enabled == 1 && x.Status != 0).Select(x => x.InverterId).ToList(); + + // var table_name = String.Format("s{1}01_inv", powerStation.SiteDB, powerStation.Code); + // var full_table_name = String.Format("`{0}`.`{1}`", powerStation.SiteDB, table_name); + // var exist = await powerStationRepository.ExistTable(powerStation.SiteDB, table_name); + + // if (!string.IsNullOrEmpty(exist)) + // { + // var calcInverter15min = await powerStationRepository.CalcInverterHisyort15minData(last_all_time, powerStation.SiteDB, full_table_name, inverterIds); + // if (calcInverter15min.Count() > 0) + // { + // foreach (var inverterHistory in calcInverter15min) + // { + // inverterHistory.TIMESTAMP = Convert.ToDateTime(inverterHistory.TIMESTAMP + ":00").ToString("yyyy-MM-dd HH:mm:ss"); + // inverterHistory.PowerStationId = powerStation.Id; + + // calcInverter15mins.Add(inverterHistory); + // } + // } + // } + //} + + //List inverter_history_15min_properties = new List() + // { + // "PowerStationId", + // "INVERTERID", + // "TIMESTAMP", + // "KWH", + // "TODAYKWH", + // "KWHKWP", + // }; + + //await powerStationRepository.AddAfterPurgeInverterHistory15min(startDateStr, endDateStr, calcInverter15mins, inverter_history_15min_properties); + //#endregion + + var calcInverter15mins = new List(); + //抓出inverter row data + for (var i = 0; i < targetDay; i++) { - "PowerStationId", - "TIMESTAMP", - "SENSORAVG01", - "SENSORAVG02", - "SENSORAVG03", - "SENSORAVG04", - "SENSORAVG05", - "SENSORAVG06", - "SENSORAVG07", - "SENSORAVG08", - "SENSORAVG09", - "SENSORAVG10", - "SENSORAVG11", - "SENSORAVG12", - "SENSORAVG13", - "SENSORAVG14", - "SENSORAVG15", - "SENSORAVG16", - "SENSORAVG17", - "SENSORAVG18", - "SENSORAVG19", - "SENSORAVG20", - "SENSORAVG21", - "SENSORAVG22", - "SENSORAVG23", - "SENSORAVG24", - "SENSORAVG25", - "SENSORAVG26", - "SENSORAVG27", - "SENSORAVG28", - "SENSORAVG29", - "SENSORAVG30", - "SENSORAVG31", - "SENSORAVG32", - "SENSORAVG33", - "SENSORAVG34", - "SENSORAVG35", - "SENSORAVG36", - "SENSORAVG37", - "SENSORAVG38", - "SENSORAVG39", - "SENSORAVG40", - "SENSORAVG41", - "SENSORAVG42", - "SENSORAVG43", - "SENSORAVG44", - "SENSORAVG45", - "SENSORAVG46", - "SENSORAVG47", - "SENSORAVG48", - "SENSORAVG49", - "SENSORAVG50", - }; - await powerStationRepository.AddAfterPurgeSensorAvgHistoryHour(startDateStr, endDateStr, sensorAvgHistoryHour, sensoravg_history_properties); - #endregion + var currentDateStr = startDate.AddDays(i).ToString("yyyy-MM-dd"); - #endregion + var count = 0; + foreach (var powerStation in powerStations) + { + if (count > 0) + { + break; + } + + var controllers = await powerStationRepository.GetAllDeviceControllerId(powerStation.Id, powerStation.SiteDB); + var inverters = await powerStationRepository.InverterTable(controllers, powerStation.SiteDB); + var inverterIds = inverters.Where(x => x.Enabled == 1 && x.Status != 0).Select(x => x.InverterId).ToList(); + + var table_name = String.Format("s{1}01_inv", powerStation.SiteDB, powerStation.Code); + var full_table_name = String.Format("`{0}`.`{1}`", powerStation.SiteDB, table_name); + var exist = await powerStationRepository.ExistTable(powerStation.SiteDB, table_name); + + if (!string.IsNullOrEmpty(exist)) + { + var inverter_row_data = await powerStationRepository.GetAllInverterRowData(currentDateStr, full_table_name); + } + } + } + + apiResult.Code = "0000"; } catch (Exception exception) { diff --git a/SolarPower/Controllers/PowerStationController.cs b/SolarPower/Controllers/PowerStationController.cs index 096a854..bb70699 100644 --- a/SolarPower/Controllers/PowerStationController.cs +++ b/SolarPower/Controllers/PowerStationController.cs @@ -58,7 +58,7 @@ namespace SolarPower.Controllers var companyId = 0; var powerStation = await powerStationRepository.GetOneAsync(powerStationId); - if(powerStation == null) + if (powerStation == null) { companyId = myUser.CompanyId; } @@ -66,10 +66,53 @@ namespace SolarPower.Controllers { companyId = powerStation.CompanyId; } - var userSelectItemLists = await userRepository.GetUserSelectOptionListAsync(companyId); + + var margeUserSelectItemLists = new List(); + + if (companyId == 1) + { + var userSelectItemLists = await userRepository.GetUserSelectOptionListAsync(companyId); + + foreach (var userSelectItem in userSelectItemLists) + { + userSelectItem.CanBeSelected = 1; + } + + margeUserSelectItemLists.AddRange(userSelectItemLists); + + } + else + { + var platformUserSelectItemLists = await userRepository.GetUserSelectOptionListAsync(1); + + if (IsPlatformLayer(myUser.Role.Layer)) + { + foreach (var userSelectItem in platformUserSelectItemLists) + { + userSelectItem.CanBeSelected = 1; + } + } + else + { + foreach (var userSelectItem in platformUserSelectItemLists) + { + userSelectItem.CanBeSelected = 0; + } + } + + margeUserSelectItemLists.AddRange(platformUserSelectItemLists); + + var userSelectItemLists = await userRepository.GetUserSelectOptionListAsync(companyId); + foreach (var userSelectItem in userSelectItemLists) + { + userSelectItem.CanBeSelected = 1; + } + + margeUserSelectItemLists.AddRange(userSelectItemLists); + } apiResult.Code = "0000"; - apiResult.Data = userSelectItemLists; + apiResult.Data = margeUserSelectItemLists; } catch (Exception exception) { @@ -259,7 +302,7 @@ namespace SolarPower.Controllers var tempSerialNumber = GetLastSerialNumber(currentSerialNumber); - var codeFormat = "s{0}{1}{2}"; + var codeFormat = "{0}{1}{2}"; powerStation = new PowerStation() { @@ -1294,7 +1337,7 @@ namespace SolarPower.Controllers "Type", "UpperLimit", }; - await powerStationRepository.AddException(Exception, properties,powerStation.SiteDB); + await powerStationRepository.AddException(Exception, properties, powerStation.SiteDB); apiResult.Code = "0000"; apiResult.Msg = "新增成功"; @@ -1321,7 +1364,7 @@ namespace SolarPower.Controllers "Type", "UpperLimit", }; - await powerStationRepository.UpdateException(Exception, properties,powerStation.SiteDB); + await powerStationRepository.UpdateException(Exception, properties, powerStation.SiteDB); apiResult.Code = "0000"; apiResult.Msg = "儲存成功"; } @@ -1350,7 +1393,7 @@ namespace SolarPower.Controllers PowerStation powerStation = null; powerStation = await powerStationRepository.GetOneAsync(stationId); apiResult.Code = "0000"; - exceptionTable = await powerStationRepository.ExceptionTable(stationId,powerStation.SiteDB); + exceptionTable = await powerStationRepository.ExceptionTable(stationId, powerStation.SiteDB); foreach (ExceptionTable a in exceptionTable) { a.Function = @" @@ -1394,7 +1437,7 @@ namespace SolarPower.Controllers PowerStation powerStation = null; powerStation = await powerStationRepository.GetOneAsync(id.PowerStationId); apiResult.Code = "0000"; - Exception = await powerStationRepository.OneException(id.SelectedId,powerStation.SiteDB); + Exception = await powerStationRepository.OneException(id.SelectedId, powerStation.SiteDB); apiResult.Data = Exception; } catch (Exception exception) @@ -1421,7 +1464,7 @@ namespace SolarPower.Controllers { PowerStation powerStation = null; powerStation = await powerStationRepository.GetOneAsync(id.PowerStationId); - Exception = await powerStationRepository.OneException(id.SelectedId,powerStation.SiteDB); + Exception = await powerStationRepository.OneException(id.SelectedId, powerStation.SiteDB); if (Exception == null) { @@ -1474,7 +1517,7 @@ namespace SolarPower.Controllers else { - if(!IsPlatformLayer(myUser.Role.Layer) && powerStation.CompanyId != myUser.CompanyId) + if (!IsPlatformLayer(myUser.Role.Layer) && powerStation.CompanyId != myUser.CompanyId) { apiResult.Code = "9993"; apiResult.Msg = errorCode.GetString(apiResult.Code); @@ -1696,7 +1739,7 @@ namespace SolarPower.Controllers { updata = new UpdataPowerStationImage() { - Id=origMainDisplay.Id, + Id = origMainDisplay.Id, IsMainDisplay = 0, UpdatedBy = myUser.Id }; @@ -1988,14 +2031,14 @@ namespace SolarPower.Controllers apiResult.Code = "0000"; solaramount = await powerStationRepository.GetSolarByCity(myUser, cityid); - foreach(var solar in solaramount) + foreach (var solar in solaramount) { solar.MainDisplay = Path.Combine(stationImageFilePath, solar.Id.ToString()) + "/" + solar.MainDisplay; List deviceControllerid = await powerStationRepository.GetAllDeviceControllerId(solar.Id, solar.SiteDB); var InverterTable = await powerStationRepository.InverterTable(deviceControllerid, solar.SiteDB); solar.InverterAmount = InverterTable.Count; } - + apiResult.Data = solaramount; @@ -2021,7 +2064,7 @@ namespace SolarPower.Controllers powerStation = await powerStationRepository.GetOneAsync(post.PowerStationId); var tableName = powerStation.SiteDB + ".controller"; - try + try { for (var i = 0; i < post.Count; i++) { @@ -2064,7 +2107,7 @@ namespace SolarPower.Controllers /// /// /// - public async Task DeviceControllerControllerTable (int stationId) + public async Task DeviceControllerControllerTable(int stationId) { List deviceControllerTable = new List(); ApiResult> apiResult = new ApiResult>(); @@ -2113,7 +2156,7 @@ namespace SolarPower.Controllers powerStation = await powerStationRepository.GetOneAsync(post.PowerStationId); var deviceController = await powerStationRepository.GetOneWithCustomDBNameAndTableAsync(post.SelectedId, powerStation.SiteDB, "controller"); - + @@ -2171,7 +2214,7 @@ namespace SolarPower.Controllers var tableName = powerStation.SiteDB + ".inverter"; try { - if(post.Id == 0) + if (post.Id == 0) { var Num = await powerStationRepository.GetCurrentSerialNumber(tableName, "ControllerId =" + post.ControllerId); var Newnum = GetLastSerialNumber(Num); @@ -2212,7 +2255,7 @@ namespace SolarPower.Controllers } else { - if(inverter1 == null) + if (inverter1 == null) { apiResult.Msg = "找不到資料"; apiResult.Code = "0001"; @@ -2279,7 +2322,7 @@ namespace SolarPower.Controllers { powerStation = await powerStationRepository.GetOneAsync(stationId); - List deviceControllerid = await powerStationRepository.GetAllDeviceControllerId(stationId,powerStation.SiteDB); + List deviceControllerid = await powerStationRepository.GetAllDeviceControllerId(stationId, powerStation.SiteDB); List InverterTable = new List(); InverterTable = await powerStationRepository.InverterTable(deviceControllerid, powerStation.SiteDB); @@ -2312,7 +2355,7 @@ namespace SolarPower.Controllers a.EnabledName = "無資料"; break; } - if(a.PyrheliometerName == null) + if (a.PyrheliometerName == null) { a.PyrheliometerName = "無設備"; } @@ -2387,7 +2430,7 @@ namespace SolarPower.Controllers try { var powerStation = await powerStationRepository.GetOneAsync(stationId); - var PowerstationOption = await powerStationRepository.GetPowerstationOptionAsync(powerStation.SiteDB,stationId); + var PowerstationOption = await powerStationRepository.GetPowerstationOptionAsync(powerStation.SiteDB, stationId); apiResult.Code = "0000"; apiResult.Data = PowerstationOption; @@ -2634,7 +2677,7 @@ namespace SolarPower.Controllers List ALLpowerStationImages = new List(); try { - foreach(var ones in powerStationId) + foreach (var ones in powerStationId) { var powerStation = await powerStationRepository.GetOneAsync(ones); diff --git a/SolarPower/Controllers/StationReportController.cs b/SolarPower/Controllers/StationReportController.cs index 2fb27a3..a417625 100644 --- a/SolarPower/Controllers/StationReportController.cs +++ b/SolarPower/Controllers/StationReportController.cs @@ -1181,7 +1181,7 @@ namespace SolarPower.Controllers { Directory.CreateDirectory(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "upload", "report", Datename)); } - var n = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "upload", "report", "FIC太陽能監控平台" + "_" + name + "報表" + "_" + postObject.Userid + Datename + ".xlsx"); + var n = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "upload", "report", Datename, "FIC太陽能監控平台" + "_" + name + "報表" + "_" + postObject.Userid + Datename + ".xlsx"); FileStream FS = new FileStream(n, FileMode.Create, FileAccess.Write); workbook.Write(FS); FS.Close(); diff --git a/SolarPower/Controllers/UserController.cs b/SolarPower/Controllers/UserController.cs index f560519..06c4d1e 100644 --- a/SolarPower/Controllers/UserController.cs +++ b/SolarPower/Controllers/UserController.cs @@ -24,14 +24,17 @@ namespace SolarPower.Controllers private readonly IUserRepository userRepository; private readonly ISendEmailService sendEmailService; private readonly IPowerStationRepository powerStationRepository; + private readonly IRoleRepository roleRepository; private string logoPath = "/upload/company_logo/"; public UserController(IUserRepository userRepository, ISendEmailService sendEmailService, - IPowerStationRepository powerStationRepository) : base() + IPowerStationRepository powerStationRepository, + IRoleRepository roleRepository) : base() { this.userRepository = userRepository; this.sendEmailService = sendEmailService; this.powerStationRepository = powerStationRepository; + this.roleRepository = roleRepository; } public IActionResult Index() @@ -302,7 +305,7 @@ namespace SolarPower.Controllers #region 新增使用者 //判斷帳號 是否已存在 var exist = await userRepository.GetOneByAccountAsync(post.Account); - if(exist != null) + if (exist != null) { apiResult.Code = "9986"; apiResult.Msg = errorCode.GetString(apiResult.Code); @@ -520,7 +523,9 @@ namespace SolarPower.Controllers return apiResult; } - var companyPowerStation = await userRepository.GetCompanyPowerStationAsync(user.CompanyId, user.Id); + var role = await roleRepository.GetOneAsync(user.RoleId); + + var companyPowerStation = await userRepository.GetCompanyPowerStationAsync(user.CompanyId, user.Id, IsPlatformLayer(role.Layer)); apiResult.Code = "0000"; apiResult.Data = companyPowerStation; @@ -556,8 +561,8 @@ namespace SolarPower.Controllers if (!IsPlatformLayer(myUser.Role.Layer)) { //如果身分為公司管理員 或 公司使用者,就只能改自己公司的資料 - - if(user.CompanyId != myUser.CompanyId) + + if (user.CompanyId != myUser.CompanyId) { apiResult.Code = "9993"; apiResult.Msg = errorCode.GetString(apiResult.Code); diff --git a/SolarPower/DBSchema/solar_power_schema.sql b/SolarPower/DBSchema/solar_power_schema.sql index 5f67d1d..7ad62a7 100644 --- a/SolarPower/DBSchema/solar_power_schema.sql +++ b/SolarPower/DBSchema/solar_power_schema.sql @@ -456,7 +456,7 @@ DELETE FROM `city`; /*!40000 ALTER TABLE `city` DISABLE KEYS */; INSERT INTO `city` (`Id`, `Name`, `ZipCode`, `Priority`) VALUES (1, '基隆市', '020', 1), - (2, '台北市', '021', 2), + (2, '臺北市', '021', 2), (3, '新北市', '022', 3), (4, '桃園縣', '030', 4), (5, '新竹市', '031', 5), @@ -464,13 +464,13 @@ INSERT INTO `city` (`Id`, `Name`, `ZipCode`, `Priority`) VALUES (7, '宜蘭縣', '033', 19), (8, '花蓮縣', '034', 18), (9, '苗栗縣', '037', 7), - (10, '台中市', '040', 8), + (10, '臺中市', '040', 8), (11, '彰化縣', '041', 9), (12, '南投縣', '049', 10), (13, '雲林縣', '050', 11), (14, '嘉義市', '051', 12), (15, '嘉義縣', '052', 13), - (16, '台南市', '060', 14), + (16, '臺南市', '060', 14), (17, '高雄市', '070', 15), (18, '屏東縣', '080', 16), (19, '臺東縣', '089', 17), diff --git a/SolarPower/Models/Overview.cs b/SolarPower/Models/Overview.cs index da8af3b..640e473 100644 --- a/SolarPower/Models/Overview.cs +++ b/SolarPower/Models/Overview.cs @@ -8,6 +8,7 @@ namespace SolarPower.Models { public class Overview { + public string Wx { get; set; } public double Today_kwh { get; set; } //今日總發電量 public double Total_kwh { get; set; } //累計總發電量 public double Today_irradiance { get; set; } //即時平均日照度 diff --git a/SolarPower/Models/PowerStation.cs b/SolarPower/Models/PowerStation.cs index 595b4e3..ba42f78 100644 --- a/SolarPower/Models/PowerStation.cs +++ b/SolarPower/Models/PowerStation.cs @@ -134,6 +134,7 @@ namespace SolarPower.Models.PowerStation public string line_token { get; set; }//line_token public double Estimate_kwh { get; set; }//預估發電度數 public double EstimateEfficacy { get; set; }//預估發電效能 + public string Wx { get; set; } //天氣預估 public string CreatorName { get; set; } //創建者名稱 } diff --git a/SolarPower/Models/User.cs b/SolarPower/Models/User.cs index 4fcddf6..033f58f 100644 --- a/SolarPower/Models/User.cs +++ b/SolarPower/Models/User.cs @@ -132,6 +132,7 @@ namespace SolarPower.Models.User public class UserSelectItemList { + public byte CanBeSelected { get; set; } public string Text { get; set; } public string Value { get; set; } } diff --git a/SolarPower/Quartz/Jobs/CalcAvgPowerStationJob.cs b/SolarPower/Quartz/Jobs/CalcAvgPowerStationJob.cs index bc0ab28..67352f5 100644 --- a/SolarPower/Quartz/Jobs/CalcAvgPowerStationJob.cs +++ b/SolarPower/Quartz/Jobs/CalcAvgPowerStationJob.cs @@ -421,8 +421,16 @@ namespace SolarPower.Quartz.Jobs historyDay.MONEY = moneyandcarbon.MONEY; //historyDay.TODAYCARBON = lastmoneyhistory.TODAYCARBON; //historyDay.TODAYMONEY = lastmoneyhistory.TODAYMONEY ; - historyDay.TOTALCARBON = lastmoneyhistory.TOTALCARBON + moneyandcarbon.CARBON; - historyDay.TOTALMONEY = lastmoneyhistory.TOTALMONEY + moneyandcarbon.MONEY; + if(lastmoneyhistory != null) + { + historyDay.TOTALCARBON = lastmoneyhistory.TOTALCARBON + moneyandcarbon.CARBON; + historyDay.TOTALMONEY = lastmoneyhistory.TOTALMONEY + moneyandcarbon.MONEY; + } + else + { + historyDay.TOTALCARBON = moneyandcarbon.CARBON; + historyDay.TOTALMONEY = moneyandcarbon.MONEY; + } //日期轉換 historyDay.Timestamp = Convert.ToDateTime(historyDay.Timestamp + ":00:00").ToString("yyyy-MM-dd"); @@ -530,8 +538,16 @@ namespace SolarPower.Quartz.Jobs historyMonth.Timestamp = Convert.ToDateTime(historyMonth.Timestamp).ToString("yyyy-MM-dd"); historyMonth.MONEY = moneyandcarbonMon.MONEY; historyMonth.CARBON = moneyandcarbonMon.CARBON; - historyMonth.TOTALCARBON = lastmoneyhistorymonth.TOTALCARBON + moneyandcarbonMon.CARBON; - historyMonth.TOTALMONEY = lastmoneyhistorymonth.TOTALMONEY + moneyandcarbonMon.MONEY; + if(lastmoneyhistorymonth != null) + { + historyMonth.TOTALCARBON = lastmoneyhistorymonth.TOTALCARBON + moneyandcarbonMon.CARBON; + historyMonth.TOTALMONEY = lastmoneyhistorymonth.TOTALMONEY + moneyandcarbonMon.MONEY; + } + else + { + historyMonth.TOTALCARBON = moneyandcarbonMon.CARBON; + historyMonth.TOTALMONEY = moneyandcarbonMon.MONEY; + } insertPowerStationHistoryMonths.Add(historyMonth); } @@ -546,8 +562,16 @@ namespace SolarPower.Quartz.Jobs { historyMonth.MONEY = historyMonth.MONEY + moneyandcarbonMon.MONEY; historyMonth.CARBON = historyMonth.CARBON + moneyandcarbonMon.CARBON; - historyMonth.TOTALCARBON = lastmoneyhistorymonth.TOTALCARBON + moneyandcarbonMon.CARBON; - historyMonth.TOTALMONEY = lastmoneyhistorymonth.TOTALMONEY + moneyandcarbonMon.MONEY; + if (lastmoneyhistorymonth != null) + { + historyMonth.TOTALCARBON = lastmoneyhistorymonth.TOTALCARBON + moneyandcarbonMon.CARBON; + historyMonth.TOTALMONEY = lastmoneyhistorymonth.TOTALMONEY + moneyandcarbonMon.MONEY; + } + else + { + historyMonth.TOTALCARBON = moneyandcarbonMon.CARBON; + historyMonth.TOTALMONEY = moneyandcarbonMon.MONEY; + } updatePowerStationHistoryMonths.Add(historyMonth); } logger.LogInformation("【CalcAvgPowerStationJob】【計算完成電站[{0}]在{1}月份的歷史資料總和】", powerStation.Code, dateNowMonth); diff --git a/SolarPower/Quartz/Jobs/CalcInverter15minJob.cs b/SolarPower/Quartz/Jobs/CalcInverter15minJob.cs index cdda9ca..20d3211 100644 --- a/SolarPower/Quartz/Jobs/CalcInverter15minJob.cs +++ b/SolarPower/Quartz/Jobs/CalcInverter15minJob.cs @@ -85,8 +85,6 @@ namespace SolarPower.Quartz.Jobs var inverterIds = inverters.Where(x => x.Enabled == 1 && x.Status != 0).Select(x => x.InverterId).ToList(); logger.LogInformation("【CalcInverter15minJob】【取得成功電站[{0}]在{1}的逆變器設備資訊】", powerStation.Code, dateNowTime); - var calcInverter15min = new InverterHistory(); - #region step2-1. 計算該電站逆變器每15min 的值 var table_name = String.Format("s{1}01_inv", powerStation.SiteDB, powerStation.Code); var full_table_name = String.Format("`{0}`.`{1}`", powerStation.SiteDB, table_name); @@ -95,15 +93,17 @@ namespace SolarPower.Quartz.Jobs if (!string.IsNullOrEmpty(exist)) { logger.LogInformation("【CalcInverter15minJob】【開始計算電站[{0}]在{1}的每15分鐘逆變器資訊】", powerStation.Code, dateNowTime); - calcInverter15mins = await powerStationRepository.CalcInverterHisyort15minData(dateNowTime, powerStation.SiteDB, full_table_name, inverterIds); + var calcInverter15min = await powerStationRepository.CalcInverterHisyort15minData(dateNowTime, powerStation.SiteDB, full_table_name, inverterIds); logger.LogInformation("【CalcInverter15minJob】【計算完成電站[{0}]在{1}的每15分鐘逆變器資訊】", powerStation.Code, dateNowTime); logger.LogInformation("【CalcInverter15minJob】【計算結果】 - {0}", System.Text.Json.JsonSerializer.Serialize(calcInverter15mins)); - if (calcInverter15mins.Count() > 0) + if (calcInverter15min.Count() > 0) { - foreach (var inverterHistory in calcInverter15mins) + foreach (var inverterHistory in calcInverter15min) { inverterHistory.TIMESTAMP = Convert.ToDateTime(inverterHistory.TIMESTAMP + ":00").ToString("yyyy-MM-dd HH:mm:ss"); inverterHistory.PowerStationId = powerStation.Id; + + calcInverter15mins.Add(inverterHistory); } } } @@ -115,7 +115,7 @@ namespace SolarPower.Quartz.Jobs } #endregion - #region step3. 將 inverter INSERT 到 inverter_history_hour 資料表 + #region step3. 將 inverter INSERT 到 inverter_history_15min 資料表 List inverter_history_properties = new List() { "PowerStationId", diff --git a/SolarPower/Quartz/Jobs/SendEmailJob.cs b/SolarPower/Quartz/Jobs/SendEmailJob.cs index 19fc0ff..e5fb2df 100644 --- a/SolarPower/Quartz/Jobs/SendEmailJob.cs +++ b/SolarPower/Quartz/Jobs/SendEmailJob.cs @@ -103,7 +103,7 @@ namespace SolarPower.Quartz.Jobs foreach(var attachment in attachments) { var directoryBase = Directory.GetCurrentDirectory(); - var filePath = @$"{directoryBase}/wwwroot{attachment}"; + var filePath = @$"{directoryBase}\wwwroot{attachment}"; var data = new Attachment(filePath); MyMail.Attachments.Add(data); diff --git a/SolarPower/Repository/Implement/OverviewRepository.cs b/SolarPower/Repository/Implement/OverviewRepository.cs index 350cb7f..8eaf39e 100644 --- a/SolarPower/Repository/Implement/OverviewRepository.cs +++ b/SolarPower/Repository/Implement/OverviewRepository.cs @@ -107,10 +107,18 @@ namespace SolarPower.Repository.Implement { try { - var sql = @$"SELECT * FROM power_station ps WHERE ps.Id IN @PowerStationIds + var dateNow = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); + + var sql = @$"SELECT + w.Wx, + ps.* + FROM power_station ps + LEFT JOIN city c ON ps.CityId = c.Id + LEFT JOIN weather_forecast w ON c.Name = w.LocationName AND w.StartTime < @DateNow AND w.EndTime > @DateNow + WHERE ps.Id IN @PowerStationIds "; - result = (await conn.QueryAsync(sql, new { PowerStationIds = powerStationIds })).ToList(); + result = (await conn.QueryAsync(sql, new { PowerStationIds = powerStationIds, DateNow = dateNow })).ToList(); } catch (Exception exception) { diff --git a/SolarPower/Repository/Implement/PowerStationRepository.cs b/SolarPower/Repository/Implement/PowerStationRepository.cs index 86de4f2..b67b99f 100644 --- a/SolarPower/Repository/Implement/PowerStationRepository.cs +++ b/SolarPower/Repository/Implement/PowerStationRepository.cs @@ -2263,7 +2263,7 @@ namespace SolarPower.Repository.Implement LEFT JOIN {db_name}.device d ON sd.DeviceId = d.Id WHERE sd.PowerStationId = @PowerStationId AND d.`Type` = 'PYR' AND d.Deleted = 0 AND d.Enabled = 1 AND d.Status != 0 ) temp - ORDER BY temp.ColName LIMIT 1 + ORDER BY temp.ColName "; result = (await conn.QueryAsync(sql, new { PowerStationId = powerStationId })).ToList(); @@ -2813,7 +2813,7 @@ namespace SolarPower.Repository.Implement var sql = $@"SELECT d.* FROM {db_name}.device d LEFT JOIN {db_name}.controller c ON d.ControllerId = c.Id AND c.Deleted = 0 - WHERE d.Enabled = 1 AND d.`Status` != 0 AND d.`Type` = 'PYR' + WHERE d.Deleted = 0 AND d.Enabled = 1 AND d.`Status` != 0 AND d.`Type` = 'PYR' AND d.PowerStationId = @PowerStationId ORDER BY d.ColName"; @@ -4352,11 +4352,12 @@ namespace SolarPower.Repository.Implement using (IDbConnection conn = this._databaseHelper.GetConnection()) { + conn.Open(); using (var trans = conn.BeginTransaction()) { try { - var purge_sql = $"DELET FROM power_station_history_hour WHERE TIMESTAMP BETWEEN @StartDate AND @EndDate"; + var purge_sql = $"DELETE FROM power_station_history_hour WHERE DATE_FORMAT(TIMESTAMP, '%Y-%m-%d') BETWEEN @StartDate AND @EndDate"; await conn.ExecuteAsync(purge_sql, new { StartDate = startDate, EndDate = endDate}, trans); var insert_sql = GenerateInsertQueryWithCustomTable(properties, "power_station_history_hour"); @@ -4378,20 +4379,21 @@ namespace SolarPower.Repository.Implement } } - public async Task AddAfterPurgePyrheliometerHistoryHour(string startDate, string endDate, List entity, List properties) + public async Task AddAfterPurgePowerStationHistoryDay(string startDate, string endDate, List entity, List properties) { int count; using (IDbConnection conn = this._databaseHelper.GetConnection()) { + conn.Open(); using (var trans = conn.BeginTransaction()) { try { - var purge_sql = $"DELET FROM sensor_history_hour WHERE TIMESTAMP BETWEEN @StartDate AND @EndDate"; + var purge_sql = $"DELETE FROM power_station_history_day WHERE DATE_FORMAT(TIMESTAMP, '%Y-%m-%d') BETWEEN @StartDate AND @EndDate"; await conn.ExecuteAsync(purge_sql, new { StartDate = startDate, EndDate = endDate }, trans); - var insert_sql = GenerateInsertQueryWithCustomTable(properties, "sensor_history_hour"); + var insert_sql = GenerateInsertQueryWithCustomTable(properties, "power_station_history_day"); count = await conn.ExecuteAsync(insert_sql, entity, trans); trans.Commit(); @@ -4410,20 +4412,21 @@ namespace SolarPower.Repository.Implement } } - public async Task AddAfterPurgeInverterHistoryHour(string startDate, string endDate, List entity, List properties) + public async Task AddAfterPurgePowerStationHistoryMonth(string startDate, string endDate, List entity, List properties) { int count; using (IDbConnection conn = this._databaseHelper.GetConnection()) { + conn.Open(); using (var trans = conn.BeginTransaction()) { try { - var purge_sql = $"DELET FROM inverter_history_hour WHERE TIMESTAMP BETWEEN @StartDate AND @EndDate"; + var purge_sql = $"DELETE FROM power_station_history_month WHERE DATE_FORMAT(TIMESTAMP, '%Y-%m-%d') BETWEEN @StartDate AND @EndDate"; await conn.ExecuteAsync(purge_sql, new { StartDate = startDate, EndDate = endDate }, trans); - var insert_sql = GenerateInsertQueryWithCustomTable(properties, "inverter_history_hour"); + var insert_sql = GenerateInsertQueryWithCustomTable(properties, "power_station_history_month"); count = await conn.ExecuteAsync(insert_sql, entity, trans); trans.Commit(); @@ -4442,20 +4445,164 @@ namespace SolarPower.Repository.Implement } } - public async Task AddAfterPurgeSensorAvgHistoryHour(string startDate, string endDate, List entity, List properties) + public async Task AddAfterPurgePyrheliometerHistory(string startDate, string endDate, byte type, List entity, List properties) { int count; using (IDbConnection conn = this._databaseHelper.GetConnection()) { + conn.Open(); using (var trans = conn.BeginTransaction()) { try { - var purge_sql = $"DELET FROM sensoravg_history_hour WHERE TIMESTAMP BETWEEN @StartDate AND @EndDate"; + var table_name = ""; + switch (type) + { + case 0: //小時 + table_name = "sensor_history_hour"; + break; + case 1: + table_name = "sensor_history_day"; + break; + case 2: + table_name = "sensor_history_month"; + break; + } + + var purge_sql = $"DELETE FROM {table_name} WHERE DATE_FORMAT(TIMESTAMP, '%Y-%m-%d') BETWEEN @StartDate AND @EndDate"; await conn.ExecuteAsync(purge_sql, new { StartDate = startDate, EndDate = endDate }, trans); - var insert_sql = GenerateInsertQueryWithCustomTable(properties, "sensoravg_history_hour"); + var insert_sql = GenerateInsertQueryWithCustomTable(properties, table_name); + count = await conn.ExecuteAsync(insert_sql, entity, trans); + + trans.Commit(); + + } + catch (Exception exception) + { + trans.Rollback(); + throw exception; + } + finally + { + conn.Close(); + } + } + } + } + + public async Task AddAfterPurgeInverterHistory(string startDate, string endDate, byte type, List entity, List properties) + { + int count; + + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + conn.Open(); + using (var trans = conn.BeginTransaction()) + { + try + { + var table_name = ""; + switch (type) + { + case 0: //小時 + table_name = "inverter_history_hour"; + break; + case 1: + table_name = "inverter_history_day"; + break; + case 2: + table_name = "inverter_history_month"; + break; + } + + var purge_sql = $"DELETE FROM {table_name} WHERE DATE_FORMAT(TIMESTAMP, '%Y-%m-%d') BETWEEN @StartDate AND @EndDate"; + await conn.ExecuteAsync(purge_sql, new { StartDate = startDate, EndDate = endDate }, trans); + + var insert_sql = GenerateInsertQueryWithCustomTable(properties, table_name); + count = await conn.ExecuteAsync(insert_sql, entity, trans); + + trans.Commit(); + + } + catch (Exception exception) + { + trans.Rollback(); + throw exception; + } + finally + { + conn.Close(); + } + } + } + } + + public async Task AddAfterPurgeInverterHistory15min(string startDate, string endDate, List entity, List properties) + { + int count; + + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + conn.Open(); + using (var trans = conn.BeginTransaction()) + { + try + { + var table_name = "inverter_history_15min"; + + var purge_sql = $"DELETE FROM {table_name} WHERE DATE_FORMAT(TIMESTAMP, '%Y-%m-%d') BETWEEN @StartDate AND @EndDate"; + await conn.ExecuteAsync(purge_sql, new { StartDate = startDate, EndDate = endDate }, trans); + + var insert_sql = GenerateInsertQueryWithCustomTable(properties, table_name); + count = await conn.ExecuteAsync(insert_sql, entity, trans); + + trans.Commit(); + + } + catch (Exception exception) + { + trans.Rollback(); + throw exception; + } + finally + { + conn.Close(); + } + } + } + } + + public async Task AddAfterPurgeSensorAvgHistory(string startDate, string endDate, byte type, List entity, List properties) + { + int count; + + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + conn.Open(); + using (var trans = conn.BeginTransaction()) + { + try + { + var table_name = ""; + switch (type) + { + case 0: //小時 + table_name = "sensoravg_history_hour"; + break; + case 1: + table_name = "sensoravg_history_day"; + break; + case 2: + table_name = "sensoravg_history_month"; + break; + } + + var purge_sql = $"DELETE FROM {table_name} WHERE DATE_FORMAT(TIMESTAMP, '%Y-%m-%d') BETWEEN @StartDate AND @EndDate"; + await conn.ExecuteAsync(purge_sql, new { StartDate = startDate, EndDate = endDate }, trans); + + var insert_sql = GenerateInsertQueryWithCustomTable(properties, table_name); count = await conn.ExecuteAsync(insert_sql, entity, trans); trans.Commit(); @@ -4509,5 +4656,29 @@ namespace SolarPower.Repository.Implement } return result; } + + public async Task> GetAllInverterRowData(string date, string table_name) + { + List result; + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + try + { + var sql = @$"SELECT FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') AS TIMESTAMP, + inv.WH / 1000 AS KWH, + inv.TODAYKWH, + inv.TOTALKWH + FROM {table_name} inv + WHERE FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') = $Date"; + + result = (await conn.QueryAsync(sql, new { Date = date })).ToList(); + } + catch (Exception exception) + { + throw exception; + } + return result; + } + } } } diff --git a/SolarPower/Repository/Implement/UserRepository.cs b/SolarPower/Repository/Implement/UserRepository.cs index ba98624..69f3ee3 100644 --- a/SolarPower/Repository/Implement/UserRepository.cs +++ b/SolarPower/Repository/Implement/UserRepository.cs @@ -300,6 +300,7 @@ namespace SolarPower.Repository.Implement try { var sql = $"SELECT Id AS Value, Name AS Text FROM `{tableName}` WHERE Deleted = 0"; + if (companyId != 0) { sql += @" AND CompanyId=@companyId"; @@ -420,7 +421,7 @@ namespace SolarPower.Repository.Implement } } - public async Task> GetCompanyPowerStationAsync(int companyId, int userId) + public async Task> GetCompanyPowerStationAsync(int companyId, int userId, bool isPlatformLayer) { List result; using (IDbConnection conn = this._databaseHelper.GetConnection()) @@ -436,7 +437,13 @@ namespace SolarPower.Repository.Implement END AS EscrowName FROM power_station ps LEFT JOIN company c ON ps.CompanyId = c.Id - WHERE ps.CompanyId = @CompanyId AND ps.Id NOT IN (SELECT psop.PowerStationId FROM power_station_operation_personnel psop WHERE psop.UserId = @UserId AND psop.Deleted = 0)"; + WHERE ps.Id NOT IN (SELECT psop.PowerStationId FROM power_station_operation_personnel psop WHERE psop.UserId = @UserId AND psop.Deleted = 0)"; + + if (!isPlatformLayer) + { + sql += " AND ps.CompanyId = @CompanyId"; + } + result = (await conn.QueryAsync(sql, new { CompanyId = companyId, UserId = userId })).ToList(); } diff --git a/SolarPower/Repository/Interface/IPowerStationRepository.cs b/SolarPower/Repository/Interface/IPowerStationRepository.cs index eb06bfb..51e7d95 100644 --- a/SolarPower/Repository/Interface/IPowerStationRepository.cs +++ b/SolarPower/Repository/Interface/IPowerStationRepository.cs @@ -563,9 +563,13 @@ namespace SolarPower.Repository.Interface Task> GetPowerStationHistoryByDateRange(string startDate, string endDate); Task AddAfterPurgePowerStationHistoryHour(string startDate, string endDate, List entity, List properties); - Task AddAfterPurgePyrheliometerHistoryHour(string startDate, string endDate, List entity, List properties); - Task AddAfterPurgeInverterHistoryHour(string startDate, string endDate, List entity, List properties); - Task AddAfterPurgeSensorAvgHistoryHour(string startDate, string endDate, List entity, List properties); + Task AddAfterPurgePowerStationHistoryDay(string startDate, string endDate, List entity, List properties); + Task AddAfterPurgePowerStationHistoryMonth(string startDate, string endDate, List entity, List properties); + Task AddAfterPurgePyrheliometerHistory(string startDate, string endDate, byte type, List entity, List properties); + Task AddAfterPurgeInverterHistory(string startDate, string endDate, byte type, List entity, List properties); + Task AddAfterPurgeInverterHistory15min(string startDate, string endDate, List entity, List properties); + Task AddAfterPurgeSensorAvgHistory(string startDate, string endDate, byte type, List entity, List properties); Task CheckShowMoney(int userid); + Task> GetAllInverterRowData(string date, string table_name); } } diff --git a/SolarPower/Repository/Interface/IUserRepository.cs b/SolarPower/Repository/Interface/IUserRepository.cs index a49cf00..77be130 100644 --- a/SolarPower/Repository/Interface/IUserRepository.cs +++ b/SolarPower/Repository/Interface/IUserRepository.cs @@ -101,7 +101,7 @@ namespace SolarPower.Repository.Interface /// /// /// - Task> GetCompanyPowerStationAsync(int companyId, int userId); + Task> GetCompanyPowerStationAsync(int companyId, int userId, bool isPlatformLayer); Task DeleteOneGetEmail(IdAndTypeByEmail post); } diff --git a/SolarPower/Views/AnalysisStationInfo/Index.cshtml b/SolarPower/Views/AnalysisStationInfo/Index.cshtml index 7ae5a1b..4937d55 100644 --- a/SolarPower/Views/AnalysisStationInfo/Index.cshtml +++ b/SolarPower/Views/AnalysisStationInfo/Index.cshtml @@ -65,6 +65,7 @@
+ @**@
@@ -734,5 +735,20 @@ }); } } + + function DataBackFill() { + var url = "/DataBackFill/BackFillHistory" + + $.ajax({ + url: url, + type: 'post', + beforSend: function (XMLHttpRequest) { + + }, + success: function (rel) { + alert("123") + }, + }); + } } \ No newline at end of file diff --git a/SolarPower/Views/MapOverview/Index.cshtml b/SolarPower/Views/MapOverview/Index.cshtml index a22411a..9ca1ebf 100644 --- a/SolarPower/Views/MapOverview/Index.cshtml +++ b/SolarPower/Views/MapOverview/Index.cshtml @@ -268,13 +268,12 @@ '
即時平均日照度:' + item.today_irradiance + '
'+ '
發電小時:' + item.solarHour + '
' + '
裝置容量:' + item.generatingCapacity + '
' + - '
天氣:' + + '
'+ + '
天氣:' + item.wx + '
'+ '' + '' + '' + ''; - if (item.coordinate != null) { var split = item.coordinate.split(','); if (split[0] == undefined || split[0] == null) { diff --git a/SolarPower/Views/PowerStation/PowerStationEdit.cshtml b/SolarPower/Views/PowerStation/PowerStationEdit.cshtml index 43c1c55..a39bb8f 100644 --- a/SolarPower/Views/PowerStation/PowerStationEdit.cshtml +++ b/SolarPower/Views/PowerStation/PowerStationEdit.cshtml @@ -765,7 +765,12 @@ $("#power_station_operation_personnel").empty(); $.each(rel.data, function (index, val) { - $("#power_station_operation_personnel").append($("