From c6ea225956e0071cad90407d96e9c2d524cffecc Mon Sep 17 00:00:00 2001 From: Kai Date: Tue, 16 Nov 2021 13:53:29 +0800 Subject: [PATCH] =?UTF-8?q?1.=20inv=E3=80=81sensor=5Favg=20=E8=87=AA?= =?UTF-8?q?=E5=8B=95=E5=8A=A0=E5=85=A5=E6=AC=84=E4=BD=8D=202.=20=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E9=80=86=E8=AE=8A=E5=99=A8=E6=AD=B8=E6=AA=94sql?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/PowerStationController.cs | 14 ++ .../Implement/PowerStationRepository.cs | 159 ++++++++++++++++-- .../Interface/IPowerStationRepository.cs | 4 + 3 files changed, 160 insertions(+), 17 deletions(-) diff --git a/SolarPower/Controllers/PowerStationController.cs b/SolarPower/Controllers/PowerStationController.cs index 5482d63..0fdab4a 100644 --- a/SolarPower/Controllers/PowerStationController.cs +++ b/SolarPower/Controllers/PowerStationController.cs @@ -3289,6 +3289,20 @@ namespace SolarPower.Controllers } else { + //檢查資料表是否有該欄位 + var check_inv_col = await powerStationRepository.CheckInvTableColumn(powerStation.SiteDB, powerStation.Code); + var check_sensor_avg_col = await powerStationRepository.CheckSensorAvgTableColumn(powerStation.SiteDB, powerStation.Code); + + if (check_inv_col != 1) + { + await powerStationRepository.AlertColInvTableColumn(powerStation.SiteDB, powerStation.Code); + } + + if (check_sensor_avg_col != 1) + { + await powerStationRepository.AlertColSensoravgTableColumn(powerStation.SiteDB, powerStation.Code); + } + await powerStationRepository.StationStatus(Id, 1); await powerStationRepository.StationStatus(Id, 1, powerStation.SiteDB + "."); apiResult.Msg = "電站啟用成功"; diff --git a/SolarPower/Repository/Implement/PowerStationRepository.cs b/SolarPower/Repository/Implement/PowerStationRepository.cs index bfd4377..f748416 100644 --- a/SolarPower/Repository/Implement/PowerStationRepository.cs +++ b/SolarPower/Repository/Implement/PowerStationRepository.cs @@ -2890,19 +2890,20 @@ namespace SolarPower.Repository.Implement var sql = $@"SELECT s.TIMESTAMP, s.INVERTERID, - a.KWH, + s.KWH, s.TODAYKWH, i.Capacity, - a.KWH/(i.Capacity/4) AS KWHKWP + s.KWH/(i.Capacity/4) AS KWHKWP FROM (SELECT MAX(FROM_UNIXTIME(sub_inv.TIMESTAMP/1000, '%Y-%m-%d %H:%i')) AS TIMESTAMP, sub_inv.INVERTERID, + SUM(sub_inv.WH)/1000 AS KWH, MAX(sub_inv.TODAYKWH) AS TODAYKWH FROM {table_name} sub_inv WHERE FROM_UNIXTIME(sub_inv.TIMESTAMP/1000, '%Y-%m-%d %H:%i') BETWEEN @StartDateTime AND @EndDateTime AND sub_inv.INVERTERID IN @InverterIds GROUP BY sub_inv.INVERTERID) s - LEFT JOIN ( + /*LEFT JOIN ( SELECT MAX(FROM_UNIXTIME(sub_inv.TIMESTAMP/1000, '%Y-%m-%d %H:%i')) AS TIMESTAMP, sub_inv.INVERTERID, SUM(sub_inv.WH)/1000 AS KWH @@ -2910,10 +2911,10 @@ namespace SolarPower.Repository.Implement WHERE FROM_UNIXTIME(sub_inv.TIMESTAMP/1000, '%Y-%m-%d %H:%i') BETWEEN @StartDateTime AND @EndDateTime AND sub_inv.INVERTERID IN @InverterIds GROUP BY sub_inv.INVERTERID) a - ON s.TIMESTAMP = a.TIMESTAMP AND s.INVERTERID = a. INVERTERID + ON s.TIMESTAMP = a.TIMESTAMP AND s.INVERTERID = a. INVERTERID*/ LEFT JOIN {db_name}.inverter i ON s.INVERTERID = REPLACE(i.InverterId, 's', '');"; - result = (await conn.QueryAsync(sql, new { StartDateTime = stratDateTime, EndDateTime = dateTime, InverterIds = inverterIds })).ToList(); + result = (await conn.QueryAsync(sql, new { StartDateTime = stratDateTime, EndDateTime = dateTime, InverterIds = inverterIds }, commandTimeout: 300)).ToList(); } catch (Exception exception) { @@ -3008,14 +3009,14 @@ namespace SolarPower.Repository.Implement AVG(s.RA3) AS RA3, AVG(s.RA4) AS RA4, AVG(s.RA5) AS RA5, - a.KWH, + SUM(s.WH)/1000 AS KWH, MAX(s.TODAYKWH) AS TODAYKWH, MAX(s.TOTALKWH) AS TOTALKWH, i.Capacity, - (a.KWH/i.Capacity) AS KWHKWP + ((SUM(s.WH)/1000) / i.Capacity) AS KWHKWP FROM {table_name} s -- 取得該時間區間的KWH - LEFT JOIN ( + /*LEFT JOIN ( SELECT FROM_UNIXTIME(inv.TIMESTAMP/1000, '%Y-%m-%d %H') AS TIMESTAMP, inv.INVERTERID, @@ -3024,26 +3025,26 @@ namespace SolarPower.Repository.Implement WHERE DATE_FORMAT(FROM_UNIXTIME(inv.TIMESTAMP/1000), '%Y-%m-%d %H') = @DateTime AND inv.INVERTERID IN @InverterIds GROUP BY FROM_UNIXTIME(inv.TIMESTAMP/1000, '%Y-%m-%d %H'), inv.INVERTERID) a - ON FROM_UNIXTIME(s.TIMESTAMP/1000, '%Y-%m-%d %H') = a.TIMESTAMP AND s.INVERTERID = a. INVERTERID + ON FROM_UNIXTIME(s.TIMESTAMP/1000, '%Y-%m-%d %H') = a.TIMESTAMP AND s.INVERTERID = a. INVERTERID*/ -- 取得整點值PR LEFT JOIN ( SELECT - FROM_UNIXTIME(inv.TIMESTAMP/1000, '%Y-%m-%d %H') AS TIMESTAMP, + inv.CrdTime AS TIMESTAMP, inv.INVERTERID, inv.PR FROM {table_name} inv - WHERE DATE_FORMAT(FROM_UNIXTIME(inv.TIMESTAMP/1000), '%Y-%m-%d %H:%i') = CONCAT(@DateTime, ':55') + WHERE left(inv.CrdTime, 16) = CONCAT(@DateTime, ':55') AND inv.INVERTERID IN @InverterIds - GROUP BY FROM_UNIXTIME(inv.TIMESTAMP/1000, '%Y-%m-%d %H'), inv.INVERTERID) inv_pr - ON FROM_UNIXTIME(s.TIMESTAMP/1000, '%Y-%m-%d %H') = inv_pr.TIMESTAMP AND s.INVERTERID = inv_pr. INVERTERID + GROUP BY left(inv.CrdTime, 13), inv.INVERTERID) inv_pr + ON left(s.CrdTime, 13) = inv_pr.TIMESTAMP AND s.INVERTERID = inv_pr. INVERTERID -- 取得逆變器容量 LEFT JOIN {db_name}.inverter i ON s.INVERTERID = i.InverterId - WHERE FROM_UNIXTIME(s.TIMESTAMP/1000, '%Y-%m-%d %H') = @DateTime + WHERE left(s.CrdTime, 13) = @DateTime AND s.INVERTERID IN @InverterIds - GROUP BY DATE_FORMAT(FROM_UNIXTIME(s.TIMESTAMP/1000), '%Y-%m-%d %H'), s.INVERTERID + GROUP BY left(s.CrdTime, 13), s.INVERTERID "; - result = (await conn.QueryAsync(sql, new { DateTime = dateTime, InverterIds = inverterIds })).ToList(); + result = (await conn.QueryAsync(sql, new { DateTime = dateTime, InverterIds = inverterIds }, commandTimeout: 300)).ToList(); } catch (Exception exception) { @@ -4467,7 +4468,7 @@ namespace SolarPower.Repository.Implement inv.RA5 FROM {table_name} inv LEFT JOIN (select * from {sensor_table_name} where TIMESTAMP between @start_timestamp and @end_timestamp)sen - ON FROM_UNIXTIME(inv.TIMESTAMP/1000, '%Y-%m-%d %H:%i') = FROM_UNIXTIME(sen.TIMESTAMP/1000, '%Y-%m-%d %H:%i') + ON inv.CrdTime = sen.CrdTime -- LEFT JOIN {sensor_table_name} sen ON FROM_UNIXTIME(inv.TIMESTAMP/1000, '%Y-%m-%d %H:%i') = FROM_UNIXTIME(sen.TIMESTAMP/1000, '%Y-%m-%d %H:%i') LEFT JOIN {entity.SiteDB}.inverter i ON inv.INVERTERID = i.InverterId WHERE inv.`TIMESTAMP` between @start_timestamp and @end_timestamp @@ -5837,5 +5838,129 @@ namespace SolarPower.Repository.Implement return resule; } } + + public async Task CheckInvTableColumn (string dbname, string code) + { + + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + conn.Open(); + + var resule = 0; + try + { + var sql = $@"SELECT count(*) FROM information_schema.columns WHERE table_schema='{dbname}' AND table_name = 's{code}01_inv' AND column_name = 'CrdTime'"; + resule = await conn.QueryFirstOrDefaultAsync(sql); + + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + + return resule; + } + } + + public async Task CheckSensorAvgTableColumn(string dbname, string code) + { + + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + conn.Open(); + + var resule = 0; + try + { + var sql = $@"SELECT count(*) FROM information_schema.columns WHERE table_schema='{dbname}' AND table_name = 's{code}01_sensoravg' AND column_name = 'CrdTime'"; + resule = await conn.QueryFirstOrDefaultAsync(sql); + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + + return resule; + } + } + + public async Task AlertColInvTableColumn(string dbname, string code) + { + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + conn.Open(); + + try + { + var sql = $@"ALTER TABLE {dbname}.s{code}01_inv ADD COLUMN CrdTime datetime; + use {dbname}; + create trigger trg01_s{code}01_inv + before insert on {dbname}.s{code}01_inv + FOR EACH ROW + begin + set new.CrdTime = FROM_UNIXTIME(new.`TIMESTAMP`/1000,'%Y-%m-%d %H:%i'); + end; + use solar_master; + + update {dbname}.s{code}01_inv set crdtime = FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i'); + + CREATE INDEX IDX02_s{code}01 ON {dbname}.s{code}01_inv(Inverterid, CrdTime);"; + + await conn.ExecuteAsync(sql); + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + + } + } + + public async Task AlertColSensoravgTableColumn(string dbname, string code) + { + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + conn.Open(); + + try + { + var sql = $@"ALTER TABLE {dbname}.s{code}01_sensoravg ADD COLUMN CrdTime datetime; + use {dbname}; + create trigger trg01_s{code}01_sensoravg + before insert on {dbname}.s{code}01_sensoravg + FOR EACH ROW + begin + set new.CrdTime = FROM_UNIXTIME(new.`TIMESTAMP`/1000,'%Y-%m-%d %H:%i'); + end; + use solar_master; + + update {dbname}.s{code}01_sensoravg set crdtime = FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i'); + + CREATE INDEX IDX{code}01_sensoravg ON {dbname}.s{code}01_sensoravg(CrdTime);"; + await conn.ExecuteAsync(sql); + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + + } + } } } diff --git a/SolarPower/Repository/Interface/IPowerStationRepository.cs b/SolarPower/Repository/Interface/IPowerStationRepository.cs index 2672704..5aed3fe 100644 --- a/SolarPower/Repository/Interface/IPowerStationRepository.cs +++ b/SolarPower/Repository/Interface/IPowerStationRepository.cs @@ -608,5 +608,9 @@ namespace SolarPower.Repository.Interface Task UpdateInvStatus(string siteDB, List codes, List inverterIds = null, byte status = 0); Task> GetInvStatusErr(List codes); Task Check4table(string code, string dbname); + Task CheckInvTableColumn(string dbname, string code); + Task CheckSensorAvgTableColumn(string dbname, string code); + Task AlertColInvTableColumn(string dbname, string code); + Task AlertColSensoravgTableColumn(string dbname, string code); } }