add 日報表 log: log_archive

This commit is contained in:
cesar liu 2022-03-28 00:45:37 +08:00
parent 393f885a8d
commit c520a6dcab
2 changed files with 105 additions and 34 deletions

View File

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
namespace solarApp.Service
{
public class procArchiveLog
{
/// <summary>
/// insert Log
/// </summary>
/// <param name="action_name">動作名稱</param>
/// <param name="duration_sec">運行時間</param>
/// <param name="src_table">來源Table</param>
/// <param name="dest_table">目的地table</param>
/// <param name="errMessage">異常資訊</param>
/// <param name="myCon">連線資訊</param>
/// <param name="cmd">command物件</param>
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();
}
}
}

View File

@ -7,6 +7,7 @@ using solarApp.Model;
using System.Configuration; using System.Configuration;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
//using solarApp.Service.proArchiveLog;
namespace solarApp.Service namespace solarApp.Service
{ {
@ -570,16 +571,16 @@ namespace solarApp.Service
//建立日報表 //建立日報表
bool insert_report_invDay() { bool insert_report_invDay() {
bool result = false; bool result = false;
procArchiveLog arclog = new procArchiveLog();
using (MySqlConnection conn = new MySqlConnection(Connection1)) using (MySqlConnection conn = new MySqlConnection(Connection1))
{ {
conn.Open(); conn.Open();
string sql = @$"delete from report_invday where powerstationid = {_powerStationID} and left(report_date, 10) = '{_date1}'"; string sql = @$"delete from report_invday where powerstationid = {_powerStationID} and left(report_date, 10) = '{_date1}'";
conn.Execute(sql); conn.Execute(sql);
DateTime dt_start = DateTime.Now;
#region get SQL #region get SQL
try //try
{ //{
if (_logger != null) if (_logger != null)
{ {
_logger.LogInformation("【Proc_inv_日報 】開始執行[{0}]在{1}inv_get_SQLStatement", _siteID, _date1); _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(); MySqlCommand cmd = new MySqlCommand();
cmd.Connection = conn; cmd.Connection = conn;
cmd.CommandTimeout = 720; 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 #endregion
#region inv_no01 #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 ) d on a.powerStationid = d.powerStationid and a.`report_date` = d.report_date
where a.inverterid = '{inv01}' # right(a.inverterid, 4) = '0001' where a.inverterid = '{inv01}' # right(a.inverterid, 4) = '0001'
GROUP BY left(a.report_date, 13) GROUP BY left(a.report_date, 13)
order by a.report_date ;"; order by a.report_date ;";
cmd.CommandText = sql; DateTime dt_start2 = DateTime.Now;
cmd.ExecuteNonQuery(); 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); // conn.Execute(sql);
#endregion #endregion
@ -644,7 +672,8 @@ from temp_invDay{_powerStationID}_s1 a join
StringBuilder sb_column = new StringBuilder(); StringBuilder sb_column = new StringBuilder();
StringBuilder sb_select = new StringBuilder(); StringBuilder sb_select = new StringBuilder();
StringBuilder sb_update_columns = new StringBuilder(); StringBuilder sb_update_columns = new StringBuilder();
DateTime dt_start3 = DateTime.Now;
for (int i = 0; i < ds.Count; i++) for (int i = 0; i < ds.Count; i++)
{ {
if (i == 0) continue; if (i == 0) continue;
@ -686,35 +715,45 @@ set a.inv_02 = b.inv_02 ");
#region insert report_invDay #region insert report_invDay
sb_column.Clear(); sb_column.Clear();
sql = sb.ToString() + sb_select.ToString() + sb_update_columns.ToString(); sql = sb.ToString() + sb_select.ToString() + sb_update_columns.ToString();
cmd.CommandText = sql; try
cmd.ExecuteNonQuery(); {
//conn.Execute(sql); cmd.CommandText = sql;
cmd.ExecuteNonQuery();
sql = @$"DROP TABLE IF EXISTS temp_invDay{_powerStationID}_s1; "; sql = @$"DROP TABLE IF EXISTS temp_invDay{_powerStationID}_s1; ";
//conn.Execute(ss); //conn.Execute(ss);
cmd.CommandText = sql; cmd.CommandText = sql;
cmd.ExecuteNonQuery(); 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 #endregion
if (_logger != null) //}
_logger.LogInformation("【Proc_inv_日報 table 】執行完成[{0}]在{1} inv_get_SQLStatement", _siteID, _date1); //catch (Exception ex)
//{
}
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;
}
#endregion #endregion
conn.Close(); conn.Close();
} } //using (MySqlConnection conn = new MySqlConnection(Connection1))
result = true; result = true;
return result; return result;