1. inv、sensor_avg 自動加入欄位

2. 修改逆變器歸檔sql
This commit is contained in:
Kai 2021-11-16 13:53:29 +08:00
parent dcca7c4afc
commit c6ea225956
3 changed files with 160 additions and 17 deletions

View File

@ -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 = "電站啟用成功";

View File

@ -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<InverterHistory>(sql, new { StartDateTime = stratDateTime, EndDateTime = dateTime, InverterIds = inverterIds })).ToList();
result = (await conn.QueryAsync<InverterHistory>(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<InverterHistory>(sql, new { DateTime = dateTime, InverterIds = inverterIds })).ToList();
result = (await conn.QueryAsync<InverterHistory>(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<int> 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<int>(sql);
}
catch (Exception exception)
{
throw exception;
}
finally
{
conn.Close();
}
return resule;
}
}
public async Task<int> 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<int>(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();
}
}
}
}
}

View File

@ -608,5 +608,9 @@ namespace SolarPower.Repository.Interface
Task UpdateInvStatus(string siteDB, List<string> codes, List<string> inverterIds = null, byte status = 0);
Task<List<InvStatusErr>> GetInvStatusErr(List<string> codes);
Task<Check4table> Check4table(string code, string dbname);
Task<int> CheckInvTableColumn(string dbname, string code);
Task<int> CheckSensorAvgTableColumn(string dbname, string code);
Task AlertColInvTableColumn(string dbname, string code);
Task AlertColSensoravgTableColumn(string dbname, string code);
}
}