diff --git a/solarApp/Service/procArchiveLog.cs b/solarApp/Service/procArchiveLog.cs new file mode 100644 index 0000000..2dbef06 --- /dev/null +++ b/solarApp/Service/procArchiveLog.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Text; +using MySql.Data.MySqlClient; + +namespace solarApp.Service +{ + public class procArchiveLog + { + /// + /// insert Log + /// + /// 動作名稱 + /// 運行時間 + /// 來源Table + /// 目的地table + /// 異常資訊 + /// 連線資訊 + /// command物件 + public void insert_log(string action_name, double duration_sec, string src_table, string dest_table, string errMessage, MySqlConnection myCon , MySqlCommand cmd) { + + 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`(`action_name`, `action_time`, `duration_sec`, `src_table`, `dest_table`, `result`, `err_txt`) + VALUES ('" + action_name + "', '" + nowTime + "', " + duration_sec + ", '" + // action_time, duration_sec + src_table + "', '"+ dest_table + "', b'"+ procResult + "', '" + // src_table, dest_table, result + errMessage + "');"; // err_txt + cmd.CommandText = sql; + cmd.ExecuteNonQuery(); + } + } +} diff --git a/solarApp/Service/procInvSvc.cs b/solarApp/Service/procInvSvc.cs index c75b243..5f050c7 100644 --- a/solarApp/Service/procInvSvc.cs +++ b/solarApp/Service/procInvSvc.cs @@ -7,6 +7,7 @@ using solarApp.Model; using System.Configuration; using System.Threading.Tasks; using Microsoft.Extensions.Logging; +//using solarApp.Service.proArchiveLog; namespace solarApp.Service { @@ -570,16 +571,16 @@ namespace solarApp.Service //建立日報表 bool insert_report_invDay() { bool result = false; - + procArchiveLog arclog = new procArchiveLog(); using (MySqlConnection conn = new MySqlConnection(Connection1)) { conn.Open(); string sql = @$"delete from report_invday where powerstationid = {_powerStationID} and left(report_date, 10) = '{_date1}'"; conn.Execute(sql); - + DateTime dt_start = DateTime.Now; #region get SQL - try - { + //try + //{ if (_logger != null) { _logger.LogInformation("【Proc_inv_日報 】開始執行[{0}]在{1}inv_get_SQLStatement", _siteID, _date1); @@ -598,8 +599,22 @@ ALTER TABLE `temp_invDay{_powerStationID}_s1` ADD INDEX `temp_indexs1` (report_d MySqlCommand cmd = new MySqlCommand(); cmd.Connection = conn; cmd.CommandTimeout = 720; - cmd.CommandText = sql; - cmd.ExecuteNonQuery(); + + try + { + cmd.CommandText = sql; + cmd.ExecuteNonQuery(); + + //select LAST_INSERT_ID() + TimeSpan duration = DateTime.Now - dt_start; + arclog.insert_log("report_day step1", duration.TotalSeconds, "inverter_history_hour", "temp_invDay", "", conn, cmd); + + } + catch (Exception ex) + { + TimeSpan duration = DateTime.Now - dt_start; + arclog.insert_log("report_day step1", duration.TotalSeconds, "inverter_history_hour", "temp_invDay", ex.Message, conn, cmd); + } #endregion #region 先新增 inv_no01 @@ -627,9 +642,22 @@ from temp_invDay{_powerStationID}_s1 a join ) d on a.powerStationid = d.powerStationid and a.`report_date` = d.report_date where a.inverterid = '{inv01}' # right(a.inverterid, 4) = '0001' GROUP BY left(a.report_date, 13) - order by a.report_date ;"; - cmd.CommandText = sql; - cmd.ExecuteNonQuery(); + order by a.report_date ;"; + DateTime dt_start2 = DateTime.Now; + try + { + cmd.CommandText = sql; + cmd.ExecuteNonQuery(); + + TimeSpan duration = DateTime.Now - dt_start2 ; + arclog.insert_log("report_day step2 inv_no01", duration.TotalSeconds, "inverter_history_hour", "report_invday", "", conn, cmd); + } + catch (Exception ex) + { + TimeSpan duration = DateTime.Now - dt_start2 ; + arclog.insert_log("report_day step2 inv_no01", duration.TotalSeconds, "inverter_history_hour", "report_invday", ex.Message, conn, cmd); + } + // conn.Execute(sql); #endregion @@ -644,7 +672,8 @@ from temp_invDay{_powerStationID}_s1 a join StringBuilder sb_column = new StringBuilder(); StringBuilder sb_select = new StringBuilder(); StringBuilder sb_update_columns = new StringBuilder(); - + + DateTime dt_start3 = DateTime.Now; for (int i = 0; i < ds.Count; i++) { if (i == 0) continue; @@ -686,35 +715,45 @@ set a.inv_02 = b.inv_02 "); #region insert report_invDay sb_column.Clear(); sql = sb.ToString() + sb_select.ToString() + sb_update_columns.ToString(); - - cmd.CommandText = sql; - cmd.ExecuteNonQuery(); - //conn.Execute(sql); + + try + { + cmd.CommandText = sql; + cmd.ExecuteNonQuery(); - sql = @$"DROP TABLE IF EXISTS temp_invDay{_powerStationID}_s1; "; - //conn.Execute(ss); - cmd.CommandText = sql; - cmd.ExecuteNonQuery(); + sql = @$"DROP TABLE IF EXISTS temp_invDay{_powerStationID}_s1; "; + //conn.Execute(ss); + cmd.CommandText = sql; + cmd.ExecuteNonQuery(); + + if (_logger != null) + _logger.LogInformation("【Proc_inv_日報 table 】執行完成[{0}]在{1} inv_get_SQLStatement", _siteID, _date1); + + TimeSpan duration = DateTime.Now - dt_start3; + arclog.insert_log("report_day step3 update inv02", duration.TotalSeconds, "inverter_history_hour", "report_invday", "", conn, cmd); + } + catch (Exception ex) + { + TimeSpan duration = DateTime.Now - dt_start3 ; + arclog.insert_log("report_day step3 update inv02", duration.TotalSeconds, "inverter_history_hour", "report_invday", ex.Message, conn, cmd); + if (_logger != null) + { + _logger.LogError("【Proc_inv_日報 table】執行失敗[{0}]在{1}inv_get_SQLStatement", _siteID, _date1); + _logger.LogError("【Proc_inv_日報 table】執行失敗[{0}]在{1}inv_get_SQLStatement - [Exception]:{2}", _siteID, _date1, ex.ToString()); + } + throw ex; + } + #endregion - if (_logger != null) - _logger.LogInformation("【Proc_inv_日報 table 】執行完成[{0}]在{1} inv_get_SQLStatement", _siteID, _date1); - - } - catch (Exception ex) - { - if (_logger != null) - { - _logger.LogError("【Proc_inv_日報 table】執行失敗[{0}]在{1}inv_get_SQLStatement", _siteID, _date1); - _logger.LogError("【Proc_inv_日報 table】執行失敗[{0}]在{1}inv_get_SQLStatement - [Exception]:{2}", _siteID, _date1, ex.ToString()); - } - throw ex; - } + //} + //catch (Exception ex) + //{ + + //} #endregion - - conn.Close(); - } + } //using (MySqlConnection conn = new MySqlConnection(Connection1)) result = true; return result;