diff --git a/SolarPower/Controllers/PowerStationController.cs b/SolarPower/Controllers/PowerStationController.cs index de08b78..2044657 100644 --- a/SolarPower/Controllers/PowerStationController.cs +++ b/SolarPower/Controllers/PowerStationController.cs @@ -3161,5 +3161,53 @@ namespace SolarPower.Controllers return apiResult; } + public async Task>> CheckStationStatus (int Id) + { + ApiResult> apiResult = new ApiResult>(); + List status = new List(); + try + { + var powerStation = await powerStationRepository.GetOneAsync(Id); + status = await powerStationRepository.CheckStationStatus(powerStation.Code, powerStation.SiteDB); + if(status.Count > 0) + { + apiResult.Code = "0099"; + apiResult.Data = status; + } + else + { + await powerStationRepository.StationStatus(Id,1); + apiResult.Msg = "電站啟用成功"; + apiResult.Code = "0000"; + } + + } + catch (Exception exception) + { + apiResult.Code = "9999"; + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + + return apiResult; + } + + public async Task> StationUnStatus(int Id) + { + ApiResult apiResult = new ApiResult(); + try + { + await powerStationRepository.StationStatus(Id, 0); + apiResult.Msg = "電站停用成功"; + apiResult.Code = "0000"; + + } + catch (Exception exception) + { + apiResult.Code = "9999"; + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + + return apiResult; + } } } diff --git a/SolarPower/DBSchema/solar_power_schema.sql b/SolarPower/DBSchema/solar_power_schema.sql index fb525eb..7bb5ae4 100644 --- a/SolarPower/DBSchema/solar_power_schema.sql +++ b/SolarPower/DBSchema/solar_power_schema.sql @@ -2191,7 +2191,8 @@ ALTER TABLE `power_station` CHANGE COLUMN `today_irradiance` `today_irradiance` DECIMAL(5,2) UNSIGNED NOT NULL DEFAULT '0.00' COMMENT '今日日照度' AFTER `total_carbon`, CHANGE COLUMN `avg_irradiance` `avg_irradiance` DECIMAL(5,2) UNSIGNED NOT NULL DEFAULT '0.00' COMMENT '平均日照度' AFTER `today_irradiance`; - +ALTER TABLE `power_station` + ADD COLUMN `Status` TINYINT(4) UNSIGNED NOT NULL DEFAULT 0 COMMENT '是否啟用, 0:否 1:是' AFTER `AreaId`; /*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */; /*!40014 SET FOREIGN_KEY_CHECKS=IFNULL(@OLD_FOREIGN_KEY_CHECKS, 1) */; diff --git a/SolarPower/Models/PowerStation.cs b/SolarPower/Models/PowerStation.cs index 8e975ea..205fc60 100644 --- a/SolarPower/Models/PowerStation.cs +++ b/SolarPower/Models/PowerStation.cs @@ -30,6 +30,7 @@ namespace SolarPower.Models.PowerStation public int Id { get; set; } public byte HealthStatus { get; set; } //狀態 + public int Status { get; set; }//停用啟用 public int CompanyId { get; set; } public int CityId { get; set; } //縣市 public string CityName { get; set; } //縣市名稱 diff --git a/SolarPower/Repository/Implement/PowerStationRepository.cs b/SolarPower/Repository/Implement/PowerStationRepository.cs index 069627f..795e9d6 100644 --- a/SolarPower/Repository/Implement/PowerStationRepository.cs +++ b/SolarPower/Repository/Implement/PowerStationRepository.cs @@ -13,6 +13,7 @@ using System.Text.Json; using SolarPower.Models; using SolarPower.Models.Role; using SolarPower.Models.Company; +using MySql.Data.MySqlClient; namespace SolarPower.Repository.Implement { @@ -5426,5 +5427,286 @@ namespace SolarPower.Repository.Implement } } } + + public async Task> CheckStationStatus (string code , string DBname) + { + List result; + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + try + { + List devices = new List + { + new PowerstationOption{Text="日照計",Value="PYR"}, + new PowerstationOption{Text="模組溫度計",Value="MTR"} + }; + // devices.Add(new PowerstationOption { Text = "電表", Value = "PWR" }); + var sql2 = ""; + foreach (var device in devices) + { + sql2 += @$"#檢查是否存在設備({device.Text})# + set @qry1:= concat('insert power_station_enable_check(siteID, check_type, check_item, isError, check_Date, error_reason) + select ', siteID ,', ''device'', ''device_Type_{device.Value}'', + case when count(*) > 0 then 0 else 1 end isError, + now(), + case when count(*) > 0 then Null else ''電站無正常啟用{device.Text}'' end error_reason + from {DBname}.device b + join power_station a on a.Id = b.PowerStationId + where b.Deleted = 0 and b.Enabled = 1 and b.Status = 1 and b.Type = ''{device.Value}'' and a.`Code` = ', siteID ); + prepare stmt from @qry1 ; + execute stmt ;"; + } + + List tables = new List + { + "inv", + "station", + "sensor", + "sensoravg", + }; + // tables.Add("meter"); + var sql3 = ""; + foreach (var table in tables) + { + sql3 += @$"# 檢查資料表_{table}是否存在# + set @qry1:= concat('insert power_station_enable_check(siteID, check_type, check_item, isError, check_Date, error_reason) + select ', siteID ,', ''{table}'', ''{table}_Table'', + case when count(*) > 0 then 0 else 1 end isError, + now(), + case when count(*) > 0 then Null else ''_{table}原始資料表不存在'' end error_reason + from information_schema.SCHEMATA,information_schema.TABLES + where SCHEMA_NAME = ''{DBname}'' AND Table_schema = ''{DBname}'' AND TABLE_NAME = ''s{code}01_{table}'''); + prepare stmt from @qry1; + execute stmt;"; + } + + + + var sql = $@"DROP PROCEDURE IF EXISTS `sp_power_station_enable_check` ; + + CREATE PROCEDURE `sp_power_station_enable_check`( siteID varchar(20) ) + BEGIN + declare _siteDB varchar(20); + declare _siteNo int(2); + + SELECT SiteDB into _siteDB + from power_station where `code` = siteID; + + #clear check result + set @qry1:= concat('delete from power_station_enable_check where `siteID` = ', siteID ); + prepare stmt from @qry1 ; + execute stmt ; + #檢查電站缺少的欄位(住址)# + SET @qry1:= concat('insert power_station_enable_check(siteID, check_type, check_item, isError, check_Date, error_reason) + select ', siteID ,', ''station'', ''address_isnull'', case when ISNULL(a.Address) = 0 then 0 else 1 end isError, now() ,case when ISNULL(a.Address) = 0 then Null else ''電站住址為空'' end error_reason + from power_station a + where a.`Code` = ', siteID ); + prepare stmt from @qry1 ; + execute stmt ; + + #檢查電站缺少的欄位(名稱)# + set @qry1:= concat('insert power_station_enable_check(siteID, check_type, check_item, isError, check_Date, error_reason) + select ', siteID ,', ''station'', ''name_isnull'', case when ISNULL(a.Name) = 0 then 0 else 1 end isError, now(),case when ISNULL(a.Name) = 0 then Null else ''電站名稱為空'' end error_reason + from power_station a + where a.`Code` = ', siteID ); + prepare stmt from @qry1 ; + execute stmt ; + + #檢查電站缺少的欄位(編號)# + set @qry1:= concat('insert power_station_enable_check(siteID, check_type, check_item, isError, check_Date, error_reason) + select ', siteID ,', ''station'', ''code_isnull'', case when ISNULL(a.Code) = 0 then 0 else 1 end isError, now(),case when ISNULL(a.Code) = 0 then Null else ''電站編號為空'' end error_reason + from power_station a + where a.`Code` = ', siteID ); + prepare stmt from @qry1 ; + execute stmt ; + + #檢查電站缺少的欄位(裝置容量-不得為 0)# + set @qry1:= concat('insert power_station_enable_check(siteID, check_type, check_item, isError, check_Date, error_reason) + select ', siteID ,', ''station'', ''generatingCapacity_isnullorempty'', case when a.GeneratingCapacity > 0 and ISNULL(a.GeneratingCapacity) = 0 then 0 else 1 end isError, now(),case when a.GeneratingCapacity > 0 and ISNULL(a.GeneratingCapacity) = 0 then Null else ''裝置容量為 0 或 不存在'' end error_reason + from power_station a + where a.`Code` = ', siteID ); + prepare stmt from @qry1 ; + execute stmt ; + + -- #檢查子資料庫是否存在電站# + -- set @qry1:= concat('insert power_station_enable_check(siteID, check_type, check_item, isError, check_Date, error_reason) + -- select ', siteID ,', ''station'', ''subStation_isnull'', case when count(*) > 0 then 0 else 1 end isError, now(),case when count(*) > 0 then Null else ''子資料庫電站不存在'' end error_reason + -- from {DBname}.power_station a + -- where a.`Code` = ', siteID ); + -- prepare stmt from @qry1 ; + -- execute stmt ; + + #檢查電站缺少的欄位(座標)# + set @qry1:= concat('insert power_station_enable_check(siteID, check_type, check_item, isError, check_Date, error_reason) + select ', siteID ,', ''station'', ''coordinate_isnull'', case when ISNULL(a.Coordinate) = 0 then 0 else 1 end isError, now(),case when ISNULL(a.Coordinate) = 0 then Null else ''電站座標為空'' end error_reason + from power_station a + where a.`Code` = ', siteID ); + prepare stmt from @qry1 ; + execute stmt ; + + #檢查電站歸屬公司狀態(刪除)# + set @qry1:= concat('insert power_station_enable_check(siteID, check_type, check_item, isError, check_Date, error_reason) + select ', siteID ,', ''company'', ''company_deleted'', + case when b.Deleted = 0 then 0 else 1 end isError, + now(), + case when b.Deleted = 0 then Null else ''公司已被刪除'' end error_reason + from power_station a + join company b on a.CompanyId = b.Id + where a.`Code` = ', siteID ); + prepare stmt from @qry1 ; + execute stmt ; + + #檢查電站歸屬公司狀態(凍結)# + set @qry1:= concat('insert power_station_enable_check(siteID, check_type, check_item, isError, check_Date, error_reason) + select ', siteID ,', ''company'', ''company_status'', + case when b.Status = 1 then 0 else 1 end isError, + now(), + case when b.Status = 1 then Null else ''公司已被凍結'' end error_reason + from power_station a + join company b on a.CompanyId = b.Id + where a.`Code` = ', siteID ); + prepare stmt from @qry1 ; + execute stmt ; + + #檢查電站歸屬公司狀態(存在)# + set @qry1:= concat('insert power_station_enable_check(siteID, check_type, check_item, isError, check_Date, error_reason) + select ', siteID ,', ''company'', ''company_isnull'', + case when count(*) > 0 then 0 else 1 end isError, + now(), + case when count(*) > 0 then Null else ''公司不存在'' end error_reason + from power_station a + join company b on a.CompanyId = b.Id + where a.`Code` = ', siteID ); + prepare stmt from @qry1 ; + execute stmt ; + + #檢查電站有無運維人員# + set @qry1:= concat('insert power_station_enable_check(siteID, check_type, check_item, isError, check_Date, error_reason) + select ', siteID ,', ''station'', ''operation_personnel_count'', + case when count(*) > 0 then 0 else 1 end isError, + now(), + case when count(*) > 0 then Null else ''電站無運維人員'' end error_reason + from power_station_operation_personnel b + join power_station a on a.Id = b.PowerStationId + where b.Deleted = 0 and a.`Code` = ', siteID ); + prepare stmt from @qry1 ; + execute stmt ; + " + sql2 + $@" + #檢查設備DBName是否填寫# + set @qry1:= concat('insert power_station_enable_check(siteID, check_type, check_item, isError, check_Date, error_reason) + select ', siteID ,', ''device'', ''device_DBName'', + case when count(*) > 0 then 1 else 0 end isError, + now(), + case when count(*) > 0 then ''有設備DBName欄位未填寫'' else Null end error_reason + from {DBname}.device b + join power_station a on a.Id = b.PowerStationId + where b.Deleted = 0 and b.Enabled = 1 and b.Status = 1 and b.DBName Is Null and a.`Code` = ', siteID ); + prepare stmt from @qry1 ; + execute stmt ; + + #檢查設備TableName是否填寫# + set @qry1:= concat('insert power_station_enable_check(siteID, check_type, check_item, isError, check_Date, error_reason) + select ', siteID ,', ''device'', ''device_TableName'', + case when count(*) > 0 then 1 else 0 end isError, + now(), + case when count(*) > 0 then ''有設備TableName欄位未填寫'' else Null end error_reason + from {DBname}.device b + join power_station a on a.Id = b.PowerStationId + where b.Deleted = 0 and b.Enabled = 1 and b.Status = 1 and b.TableName Is Null and a.`Code` = ', siteID ); + prepare stmt from @qry1 ; + execute stmt ; + + #檢查設備ColName是否填寫# + set @qry1:= concat('insert power_station_enable_check(siteID, check_type, check_item, isError, check_Date, error_reason) + select ', siteID ,', ''device'', ''device_ColName'', + case when count(*) > 0 then 1 else 0 end isError, + now(), + case when count(*) > 0 then ''有設備ColName欄位未填寫'' else Null end error_reason + from {DBname}.device b + join power_station a on a.Id = b.PowerStationId + where b.Deleted = 0 and b.Enabled = 1 and b.Status = 1 and b.ColName Is Null and a.`Code` = ', siteID ); + prepare stmt from @qry1 ; + execute stmt ; + + " + sql3 + @$" + # 檢查是否有控制器# + set @qry1:= concat('insert power_station_enable_check(siteID, check_type, check_item, isError, check_Date, error_reason) + + select ', siteID ,', ''controller'', ''controller_isnull'', + case when count(*) > 0 then 0 else 1 end isError, + now(), + case when count(*) > 0 then Null else ''電站無控制器'' end error_reason + from {DBname}.power_station a + join {DBname}.controller b on a.id = b.PowerStationId + where a.`Code` = ', siteID, ' and b.Deleted = 0' ); + prepare stmt from @qry1; + execute stmt; + + # 檢查是否有逆變器# + set @qry1:= concat('insert power_station_enable_check(siteID, check_type, check_item, isError, check_Date, error_reason) + + select ', siteID ,', ''inverter'', ''inverter_isnull'', + case when count(*) > 0 then 0 else 1 end isError, + now(), + case when count(*) > 0 then Null else ''電站無逆變器'' end error_reason + from {DBname}.power_station a + join {DBname}.controller b on a.id = b.PowerStationId + join {DBname}.inverter c on b.id = c.ControllerId + where a.`Code` = ', siteID, ' and c.Deleted = 0 and c.Enabled = 1 and c.Status = 1' ); + prepare stmt from @qry1; + execute stmt; + + # 回傳結果 + set @qry1:= concat('select error_reason from power_station_enable_check + where siteID = ', siteID ,' and isError = 1'); + + prepare stmt from @qry1 ; + execute stmt; + END ; + + + call sp_power_station_enable_check('{code}'); "; + result = (await conn.QueryAsync(sql)).ToList(); + + } + catch (Exception exception) + { + throw exception; + } + return result; + } + } + + public async Task StationStatus(int stationId , int status) + { + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + conn.Open(); + using (var trans = conn.BeginTransaction()) + { + try + { + var sql = @$"UPDATE power_station + SET Status = {status} + WHERE Id = {stationId} "; + + await conn.ExecuteAsync(sql, trans); + + trans.Commit(); + } + catch (Exception exception) + { + trans.Rollback(); + throw exception; + } + finally + { + conn.Close(); + } + } + } + } + + } } diff --git a/SolarPower/Repository/Interface/IPowerStationRepository.cs b/SolarPower/Repository/Interface/IPowerStationRepository.cs index a27d8f6..e97c1e5 100644 --- a/SolarPower/Repository/Interface/IPowerStationRepository.cs +++ b/SolarPower/Repository/Interface/IPowerStationRepository.cs @@ -603,5 +603,7 @@ namespace SolarPower.Repository.Interface Task> GetShareDevicePowerstationName(int Id, string DBname); Task DropShareDevice(int powerstationId, string DBname); Task DeleteALLPowerStationOperationPersonnel(int stationId); + Task> CheckStationStatus(string code, string DBname); + Task StationStatus(int stationId, int status); } } diff --git a/SolarPower/Views/PowerStation/Index.cshtml b/SolarPower/Views/PowerStation/Index.cshtml index b95d9e7..3799128 100644 --- a/SolarPower/Views/PowerStation/Index.cshtml +++ b/SolarPower/Views/PowerStation/Index.cshtml @@ -140,6 +140,7 @@ 裝置容量(kWp) 逆變器數量 台電掛表日 + 狀態 功能 @@ -219,6 +220,7 @@ var tablocation = ""; var ids = new Array(0); var localurl = this.location.href; + var index = 0; $(function () { $('#collapse').trigger("click"); Cityes(); @@ -255,7 +257,7 @@ sidebar += "
"; sidebar += "
"; - sidebar += "