FIC_Solar/solarApp/Service/procSyncError.cs

186 lines
9.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
using Dapper;
using solarApp.Model;
using System.Configuration;
namespace solarApp.Service
{
public class procSyncError
{
string Connection1 = string.Empty;
public procSyncError(string Connection_parame = null)
{
if (!string.IsNullOrEmpty(Connection_parame))
{
Connection1 = Connection_parame;
}
else
{
Connection1 = ConfigurationManager.ConnectionStrings["mySql"].ConnectionString;
}
}
public bool syncErrData()
{
bool result = false;
procArchiveLog arclog = new procArchiveLog();
try
{
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
DateTime dt_start = DateTime.Now;
conn.Open();
#region trigger
dt_start = DateTime.Now;
string sql = $@" update`err_main` a join alarmorion_orionalarmrecord b on a.id = b.id set
a.`sourcestate` = b.`sourcestate`,
a.`ackstate` = b.`ackstate`,
a.`priority` = b.`priority`,
a.`normaltime` = b.`normaltime`,
a.`acktime` = b.`acktime`,
a.`lastupdate` = b.`lastupdate`
where a.sourcestate =1 and and a.site_id is not null "; // b.datestamp >= '{ System.DateTime.Now.AddDays(-90).ToString("yyyy-mm-dd")}' ;";
int rowCount = conn.Execute(sql);
TimeSpan durationSec = DateTime.Now - dt_start;
arclog.insert_log("0", "syncError s2 normaltime", durationSec.TotalSeconds, "orionAlarmRecord", "err_main", "0", "", rowCount.ToString(), conn, null);
#endregion
#region
sql = $@" select * from alarmorion_orionalarmrecord
where id not in (select id from err_main )
and datestamp >= '{System.DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd")}'";
var ds = conn.Query<errMain_model>(sql).AsList<errMain_model>();
StringBuilder sb = new StringBuilder();
bool isfirst = true;
foreach (var item in ds)
{
if (isfirst)
{
isfirst = false;
sb.Append(item.id);
}
else {
sb.Append($", {item.id}");
}
}
#endregion
if (ds.Count > 0)
{
#region err_main
string sql_header = "INSERT INTO `err_main`(`id`, `timestamp`, `datestamp`, `uuidHash`, `uuid`, `isOpen`, `sourceState`, `ackState`, `ackRequired`, `alarmClass`, `priority`, `normalTime`, `ackTime`, `userAccount`, `alarmTransition`, `lastUpdate`) ";
foreach (var item in ds)
{
sql = sql_header + $@" VALUES ({item.id}, {item.timestamp}, '{item.datestamp}', {item.uuidHash}, {item.uuid}, {item.isOpen}, {item.sourceState}
, {item.ackState}, {item.ackRequired}, {item.alarmClass}, {item.priority}, {item.normalTime}, {item.ackTime}
, '{item.userAccount}', {item.alarmTransition}, {item.lastUpdate}); ";
}
int rowCT = conn.Execute(sql);
TimeSpan duration = DateTime.Now - dt_start;
arclog.insert_log("0", "syncError s1", duration.TotalSeconds, "orionAlarmRecord", "err_main", "0", "", rowCT.ToString(), conn, null);
#endregion
#region update site_id, inverterID
dt_start = DateTime.Now;
sql = $@"update err_main a join alarmorion_orionalarmfacetvalue b on a.id = b.alarm
set err_valuekind = left(`value`, 1),
errValue = SUBSTRING_INDEX(
SUBSTRING_INDEX(`value`, '@', 1) # @前面所有string --> e:7
, ':', -1)
where facetName = 4 and alarm in ({sb.ToString()});";
rowCT = conn.Execute(sql);
duration = DateTime.Now - dt_start;
arclog.insert_log("0", "syncError s3 errValue", duration.TotalSeconds, "facetvalue4", "err_main", "0", "", rowCT.ToString(), conn, null);
#endregion
#region update device_id
dt_start = DateTime.Now;
sql = $@"update err_main a join alarmorion_orionalarmfacetvalue b on a.id = b.alarm
set err_valuekind = left(`value`, 1),
errDevice = SUBSTRING_INDEX(SUBSTRING_INDEX(new.`value`, ':', -1) # : 後面所有string
,'S', -1)
where facetName = 8 and alarm in ({sb.ToString()});";
rowCT = conn.Execute(sql);
duration = DateTime.Now - dt_start;
arclog.insert_log("0", "syncError s4 errDevice", duration.TotalSeconds, "facetValue8", "err_main", "0", "", rowCT.ToString(), conn, null);
#endregion
#region update site_id,
dt_start = DateTime.Now;
sql = $@"update err_main a join v_company_inv b on a.errDevice = b.INVERTERID set
site_id = substring(errDevice , 1, 9),
errDeviceBrand = b.brand,
errDeviceModel = b.Model
where errDevice is not null and id in ({sb.ToString()});";
rowCT = conn.Execute(sql);
duration = DateTime.Now - dt_start;
arclog.insert_log("0", "syncError s5 site_id", duration.TotalSeconds, "v_company_inv", "err_main", "0", "", rowCT.ToString(), conn, null);
#endregion
#region
dt_start = DateTime.Now;
sql = $@"drop table temp_healthStatus;
create table temp_healthStatus
select v.siteName, z.* from (
select ROW_NUMBER() OVER (PARTITION BY site_id ORDER BY HealthStatus desc) row_num,
site_id, alarmID, datestamp, HealthStatus
from (
select site_id, datestamp, sourceState, id alarmID,
case when alarmClass = 4 then 3 # Network 控制盒斷線 - 紅燈
when alarmClass != 4 then 2 end HealthStatus # inv, sensor 異常 黃燈
from err_main
where sourceState = 1 and left(site_id , 1) = '0'
and datestamp > '{ System.DateTime.Now.AddDays(-60).ToString("yyyy-mm-dd")}'
order by HealthStatus desc
limit 100
) x
)z join v_station v on z.site_id = v.siteid
where row_num = 1;";
conn.Execute(sql);
sql = $@"update power_station set healthstatus = 1;
update power_station a join temp_healthstatus b on a.code = b.site_id
set a.healthstatus = b.healthstatus;";
rowCT = conn.Execute(sql);
duration = DateTime.Now - dt_start;
arclog.insert_log("0", "syncError s6 site health", duration.TotalSeconds, "temp_healthstatus", "err_main", "0", "", rowCT.ToString(), conn, null);
#endregion
#region Inverter
dt_start = DateTime.Now;
sql = $@"update inv_status set status = 1;
update inv_status a join
(
select errDevice, 2 errStatus
from err_main -- 有資料 = 異常
where sourceState = 1 and left(errDevice, 1) = '0'
group by errDevice
)b on a.inverterid = b.errDevice
set status = errStatus;";
rowCT = conn.Execute(sql);
duration = DateTime.Now - dt_start;
arclog.insert_log("0", "syncError s7 inv health", duration.TotalSeconds, "", "err_main", "0", "", rowCT.ToString(), conn, null);
#endregion
}
conn.Close();
}
result = true;
}
catch (Exception ex)
{
throw ex;
}
return result;
}
}
}