異常同步 更新 site_id SQL 語法

This commit is contained in:
cesar liu 2022-08-18 18:28:23 +08:00
parent a189bcc2f4
commit ff213eba6e
2 changed files with 58 additions and 51 deletions

View File

@ -34,7 +34,7 @@ namespace solarApp.Service
{
DateTime dt_start = DateTime.Now;
conn.Open();
#region trigger
#region Normal time
dt_start = DateTime.Now;
string sql = $@" update`err_main` a join alarmorion_orionalarmrecord b on a.id = b.id set
a.`sourcestate` = b.`sourcestate`,
@ -43,19 +43,19 @@ namespace solarApp.Service
a.`normaltime` = b.`normaltime`,
a.`acktime` = b.`acktime`,
a.`lastupdate` = b.`lastupdate`
where a.sourcestate =1 and a.site_id is not null "; // b.datestamp >= '{ System.DateTime.Now.AddDays(-90).ToString("yyyy-mm-dd")}' ;";
where a.sourcestate = 1 and b.datestamp >= '{ System.DateTime.Now.AddDays(-60).ToString("yyyy-MM-dd")}'";// and a.site_id is not null "; // ;";
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);
arclog.insert_log("0", "syncError s0 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")}'";
sql = $@"select * from err_main
where site_id is null and datestamp >= '{System.DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd")}'";
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")}'";
//sql = $@"select * from err_main
// where site_id is null 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;
@ -74,16 +74,17 @@ namespace solarApp.Service
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);
sql = "";
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)
{ // //{item.uuidHash}, '{Convert.ToBase64String(item.uuid)}'
sql += sql_header + $@" VALUES ({item.id}, {item.timestamp}, '{DateTime.Parse(item.datestamp).ToString("yyyy-MM-dd")}', 0, 0
, {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
@ -94,35 +95,38 @@ namespace solarApp.Service
SUBSTRING_INDEX(`value`, '@', 1) # @string --> e:7
, ':', -1)
where facetName = 4 and alarm in ({sb.ToString()});";
int rowCT = conn.Execute(sql);
TimeSpan duration = DateTime.Now - dt_start;
arclog.insert_log("0", "syncError s3 errValue", duration.TotalSeconds, "facetvalue4", "err_main", "0", "", rowCT.ToString(), conn, null);
rowCT = conn.Execute(sql);
duration = DateTime.Now - dt_start;
arclog.insert_log("0", "syncError s2 errValue", duration.TotalSeconds, "facetvalue4", "err_main", "0", "", rowCT.ToString(), conn, null);
#endregion
#region update device_id
#region update site_id, 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(`value`, ':', -1) # : string
,'S', -1)
where facetName = 8 and alarm in ({sb.ToString()});";
,'S', -1),
a.site_id = substring(
SUBSTRING_INDEX(SUBSTRING_INDEX(`value`, ':', -1) # : string
,'S', -1)
, 1, 9)
where facetName = 8 and errDevice is null and a.datestamp >= '{System.DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd")}';";
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);
arclog.insert_log("0", "syncError s3 errDevice", duration.TotalSeconds, "facetValue8", "err_main", "0", "", rowCT.ToString(), conn, null);
#endregion
#region update site_id,
#region update
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()});";
where errDevice is not null and datestamp >= '{System.DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd")}';";
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);
arclog.insert_log("0", "syncError s4 site_id", duration.TotalSeconds, "v_company_inv", "err_main", "0", "", rowCT.ToString(), conn, null);
#endregion
#region
@ -152,7 +156,7 @@ namespace solarApp.Service
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);
arclog.insert_log("0", "syncError s5 site health", duration.TotalSeconds, "temp_healthstatus", "err_main", "0", "", rowCT.ToString(), conn, null);
#endregion
@ -169,7 +173,7 @@ namespace solarApp.Service
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);
arclog.insert_log("0", "syncError s6 inv health", duration.TotalSeconds, "", "err_main", "0", "", rowCT.ToString(), conn, null);
#endregion
}

View File

@ -115,10 +115,12 @@ namespace solarApp
//異常處理 每 5分鐘跑一次
if ((DateTime.Now.Minute % 5) == 0 && autoTask == false)
{
lbMsgTitle.Text = DateTime.Now.ToString() + " timer start";
btSyncErr.PerformClick();
{
autoTask = true;
lbMsgTitle.Text = "異常處理 " + DateTime.Now.ToString() + " timer start";
btSyncErr.PerformClick();
lbMsgTitle.Text = "異常處理 done" + DateTime.Now.ToString() ;
autoTask = false;
//bt_archive_Click.PerformClick();
}
@ -136,24 +138,24 @@ namespace solarApp
//水位管理
if (System.DateTime.Now.Hour == 03 && DateTime.Now.Minute == 30
&& (DateTime.Now.Second >= 0 && DateTime.Now.Second <= 10)
&& autoTask == false)
{
int i = 0;
var site_list = stationSvc.get_station_list();
//if (System.DateTime.Now.Hour == 03 && DateTime.Now.Minute == 30
// && (DateTime.Now.Second >= 0 && DateTime.Now.Second <= 10)
// && autoTask == false)
//{
// int i = 0;
// var site_list = stationSvc.get_station_list();
archiveLowData archiveData = new archiveLowData();
autoTask = true;
foreach (var item in site_list)
{
archiveData.archiveData(item.SiteID.Substring(0, 9));
// archiveLowData archiveData = new archiveLowData();
// autoTask = true;
// foreach (var item in site_list)
// {
// archiveData.archiveData(item.SiteID.Substring(0, 9));
i++;
}
autoTask = false;
lbMsgTitle.Text = System.DateTime.Now.ToString() + " 水位管理 - 資料搬移完成!";
}
// i++;
// }
// autoTask = false;
// lbMsgTitle.Text = System.DateTime.Now.ToString() + " 水位管理 - 資料搬移完成!";
//}
}
private void rb_site_CheckedChanged(object sender, EventArgs e)
@ -714,8 +716,9 @@ namespace solarApp
private void btSyncErr_Click(object sender, EventArgs e)
{
autoTask = true;
svc.syncErrData();
autoTask = false;
}
//test for email format