log 至DB機制增加 單引號處理

This commit is contained in:
cesar liu 2022-08-05 08:46:15 +08:00
parent 59cb127300
commit 673e108c62
4 changed files with 23 additions and 15 deletions

View File

@ -96,7 +96,7 @@ namespace SolarPower
#region
//services.AddHostedService<OperationScheduleBackgroundService>();
//if (envName == "Production")
if (!envName != "Production")
if (envName == "Production")
{
////添加Quartz服務
services.AddSingleton<IJobFactory, SingletonJobFactory>();

View File

@ -33,8 +33,7 @@ namespace solarApp.Service
public bool archiveData(string siteID)
{
bool result = false;
try
{
_siteID = siteID;
//_date1 = date1;
get_siteInfo();
@ -44,11 +43,7 @@ namespace solarApp.Service
moveData_inv();
moveData_sensor();
result = true;
}
catch (Exception ex)
{
throw ex;
}
return result;
}
@ -302,6 +297,8 @@ namespace solarApp.Service
// log
TimeSpan duration = DateTime.Now - dt_start1;
arclog.insert_log(_powerStationID, "move raw s1", duration.TotalSeconds, $"{_siteDB}.s{_siteID}_station", $"{_siteDB}_history.s{_siteID}_station", "0", "", rowCT.ToString(), conn, null);
dt_start1 = DateTime.Now;
//刪除原本的資料
@ -310,8 +307,10 @@ namespace solarApp.Service
rowCT = conn.Execute(for_delete_station, commandTimeout: 600);
//System.Diagnostics.Debug.WriteLine($"【ArchiveRowData】{_siteDB}.s{_siteID}_sensoravg搬移至{_siteDB}_history.s{_siteID}_sensoravg【搬移資料成功】於{dateTime} 共 {delete_station.ToString()} 筆");
duration = DateTime.Now - dt_start1;
arclog.insert_log(_powerStationID, "delete raw s2", duration.TotalSeconds, $"{_siteDB}.s{_siteID}_station", "", "0", "", rowCT.ToString(), conn, null);
#endregion
@ -322,6 +321,7 @@ namespace solarApp.Service
//System.Diagnostics.Debug.WriteLine("【ArchiveRowData】[搬移資料失敗】");
TimeSpan duration = DateTime.Now - dt_start1;
arclog.insert_log(_powerStationID, "move raw s1", duration.TotalSeconds, $"{_siteDB}.s{_siteID}_station", $"{_siteDB}_history.s{_siteID}_station", "1", ex.Message, "0", conn, null);
throw ex;
}
@ -344,14 +344,15 @@ namespace solarApp.Service
#region inv搬移
dt_start1 = DateTime.Now;
// System.Diagnostics.Debug.WriteLine($"【ArchiveRowData】開始執行[{_siteDB}.s{_siteID}_inv]的資料表向[{_siteDB}_history.s{_siteID}_inv]搬移");
string for_insert_inv = $@"INSERT {_siteDB}_history.s{_siteID01}_inv
SELECT * FROM {_siteDB}.s{_siteID01}_inv
string for_insert_inv = $@"INSERT {_siteDB}_history.s{_siteID01}_inv(`ID`, `TIMESTAMP`, `SITEID`, `SITETYPE`, `CONTROLLERID`, `INVERTERID`, `SN`, `AC1V`, `AC1A`, `AC1W`, `AC1F`, `AC1WH`, `AC2V`, `AC2A`, `AC2W`, `AC2F`, `AC2WH`, `AC3V`, `AC3A`, `AC3W`, `AC3F`, `AC3WH`, `ACRUNTIME`, `DC1V`, `DC1A`, `DC1W`, `DC1WH`, `DC2V`, `DC2A`, `DC2W`, `DC2WH`, `DC3V`, `DC3A`, `DC3W`, `DC3WH`, `DC4V`, `DC4A`, `DC4W`, `DC4WH`, `DC5V`, `DC5A`, `DC5W`, `DC5WH`, `DCRUNTIME`, `WH`, `TODAYKWH`, `TOTALKWH`, `PR`, `RA1`, `RA2`, `RA3`, `RA4`, `RA5`, `CrdTime`)
SELECT `ID`, `TIMESTAMP`, `SITEID`, `SITETYPE`, `CONTROLLERID`, `INVERTERID`, `SN`, `AC1V`, `AC1A`, `AC1W`, `AC1F`, `AC1WH`, `AC2V`, `AC2A`, `AC2W`, `AC2F`, `AC2WH`, `AC3V`, `AC3A`, `AC3W`, `AC3F`, `AC3WH`, `ACRUNTIME`, `DC1V`, `DC1A`, `DC1W`, `DC1WH`, `DC2V`, `DC2A`, `DC2W`, `DC2WH`, `DC3V`, `DC3A`, `DC3W`, `DC3WH`, `DC4V`, `DC4A`, `DC4W`, `DC4WH`, `DC5V`, `DC5A`, `DC5W`, `DC5WH`, `DCRUNTIME`, `WH`, `TODAYKWH`, `TOTALKWH`, `PR`, `RA1`, `RA2`, `RA3`, `RA4`, `RA5`, `CrdTime` FROM {_siteDB}.s{_siteID01}_inv
WHERE LEFT(crdTime, 7) < '2022-07' ";
int rowCT = conn.Execute(for_insert_inv, commandTimeout: 6000);
//System.Diagnostics.Debug.WriteLine($"【ArchiveRowData】{_siteDB}.s{_siteID}_inv的七月前資料寫入至{_siteDB}_history.s{_siteID}_inv【寫入成功】於{dateTime}");
// log
TimeSpan duration = DateTime.Now - dt_start1;
arclog.insert_log(_powerStationID, "move raw s1", duration.TotalSeconds, $"{_siteDB}.s{_siteID01}_inv", $"{_siteDB}_history.s{_siteID01}_inv", "0", "", rowCT.ToString(), conn, null);
//arclog.insert_log(_powerStationID, "sensor step1", duration.TotalSeconds, "sensor_history_hour", "insert sensor hour", "0", "", rowCT.ToString(), conn, cmd);
dt_start1 = DateTime.Now;
string for_delete_inv = $@"DELETE FROM {_siteDB}.s{_siteID01}_inv
@ -365,6 +366,7 @@ namespace solarApp.Service
{
TimeSpan duration = DateTime.Now - dt_start1;
arclog.insert_log(_powerStationID, "move raw s1", duration.TotalSeconds, $"{_siteDB}.s{_siteID01}_station", $"{_siteDB}_history.s{_siteID01}_station", "1", ex.Message, "0", conn, null);
//arclog.insert_log(_powerStationID, "sensor step1", duration.TotalSeconds, "sensor_history_hour", "insert sensor hour", "0", "", rowCT.ToString(), conn, cmd);
throw ex;
}
#endregion
@ -387,8 +389,8 @@ namespace solarApp.Service
#region sensoravg搬移
dt_start1 = DateTime.Now;
//System.Diagnostics.Debug.WriteLine($"【ArchiveRowData】開始執行[{_siteDB}.s{_siteID}_sensoravg]的資料表向[{_siteDB}_history.s{_siteID}_sensoravg]搬移");
string for_insert_sensoravg = $@"INSERT {_siteDB}_history.s{_siteID01}_sensoravg
SELECT * FROM {_siteDB}.s{_siteID01}_sensoravg
string for_insert_sensoravg = $@"INSERT {_siteDB}_history.s{_siteID01}_sensoravg(`ID`, `TIMESTAMP`, `SITEID`, `SITETYPE`, `CONTROLLERID`, `SENSORAVG01`, `SENSORAVG02`, `SENSORAVG03`, `SENSORAVG04`, `SENSORAVG05`, `SENSORAVG06`, `SENSORAVG07`, `SENSORAVG08`, `SENSORAVG09`, `SENSORAVG10`, `SENSORAVG11`, `SENSORAVG12`, `SENSORAVG13`, `SENSORAVG14`, `SENSORAVG15`, `SENSORAVG16`, `SENSORAVG17`, `SENSORAVG18`, `SENSORAVG19`, `SENSORAVG20`, `SENSORAVG21`, `SENSORAVG22`, `SENSORAVG23`, `SENSORAVG24`, `SENSORAVG25`, `SENSORAVG26`, `SENSORAVG27`, `SENSORAVG28`, `SENSORAVG29`, `SENSORAVG30`, `SENSORAVG31`, `SENSORAVG32`, `SENSORAVG33`, `SENSORAVG34`, `SENSORAVG35`, `SENSORAVG36`, `SENSORAVG37`, `SENSORAVG38`, `SENSORAVG39`, `SENSORAVG40`, `SENSORAVG41`, `SENSORAVG42`, `SENSORAVG43`, `SENSORAVG44`, `SENSORAVG45`, `SENSORAVG46`, `SENSORAVG47`, `SENSORAVG48`, `SENSORAVG49`, `SENSORAVG50`, `CrdTime`)
SELECT `ID`, `TIMESTAMP`, `SITEID`, `SITETYPE`, `CONTROLLERID`, `SENSORAVG01`, `SENSORAVG02`, `SENSORAVG03`, `SENSORAVG04`, `SENSORAVG05`, `SENSORAVG06`, `SENSORAVG07`, `SENSORAVG08`, `SENSORAVG09`, `SENSORAVG10`, `SENSORAVG11`, `SENSORAVG12`, `SENSORAVG13`, `SENSORAVG14`, `SENSORAVG15`, `SENSORAVG16`, `SENSORAVG17`, `SENSORAVG18`, `SENSORAVG19`, `SENSORAVG20`, `SENSORAVG21`, `SENSORAVG22`, `SENSORAVG23`, `SENSORAVG24`, `SENSORAVG25`, `SENSORAVG26`, `SENSORAVG27`, `SENSORAVG28`, `SENSORAVG29`, `SENSORAVG30`, `SENSORAVG31`, `SENSORAVG32`, `SENSORAVG33`, `SENSORAVG34`, `SENSORAVG35`, `SENSORAVG36`, `SENSORAVG37`, `SENSORAVG38`, `SENSORAVG39`, `SENSORAVG40`, `SENSORAVG41`, `SENSORAVG42`, `SENSORAVG43`, `SENSORAVG44`, `SENSORAVG45`, `SENSORAVG46`, `SENSORAVG47`, `SENSORAVG48`, `SENSORAVG49`, `SENSORAVG50`, `CrdTime` FROM {_siteDB}.s{_siteID01}_sensoravg
WHERE LEFT(crdTime, 7) < '2022-07'";
int rowCT = conn.Execute(for_insert_sensoravg, commandTimeout: 600);
// log

View File

@ -180,7 +180,7 @@ namespace solarApp.Service
conn.Open(); // 01 控制器編號
string sql = @" select id, CompanyId, `code` , SerialNumber, CONCAT(`code` ,'01') SiteID, SiteDB, `name` SiteName
from power_station
where deleted = 0 and `status`= 1 ";
where deleted = 0 and `status`= 1";
// id <> 14";and id > 24
List<station_list> ds = conn.Query<station_list>(sql).AsList<station_list>();
conn.Close();

View File

@ -26,12 +26,18 @@ namespace solarApp.Service
}
else _cmd = cmd;
string errMsg = string.Empty;
if (!string.IsNullOrEmpty(errMessage))
{
errMsg = errMessage.Replace("'", "''");
}
string nowTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
//string procResult = (string.IsNullOrEmpty(errMessage)) ? "0" : "1";
string sql = @"INSERT INTO `solar_master`.`log_archive`(power_stationID, `action_name`, `action_time`, `duration_sec`, `src_table`, `dest_table`, `result`, `err_txt`, `note`)
VALUES ("+ power_stationID + ", '" + action_name + "', '" + nowTime + "', " + duration_sec + ", '" + // action_time, duration_sec
src_table + "', '"+ dest_table + "', b'"+ procResult + "', '" + // src_table, dest_table, result
errMessage + "', '"+ note +"');"; // err_txt
errMsg + "', '"+ note +"');"; // err_txt
_cmd.CommandText = sql;
_cmd.ExecuteNonQuery();
}