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.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;
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
@ -628,8 +643,21 @@ from temp_invDay{_powerStationID}_s1 a join
where a.inverterid = '{inv01}' # right(a.inverterid, 4) = '0001'
GROUP BY left(a.report_date, 13)
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
@ -645,6 +673,7 @@ from temp_invDay{_powerStationID}_s1 a join
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;
@ -687,22 +716,26 @@ set a.inv_02 = b.inv_02 ");
sb_column.Clear();
sql = sb.ToString() + sb_select.ToString() + sb_update_columns.ToString();
try
{
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
//conn.Execute(sql);
sql = @$"DROP TABLE IF EXISTS temp_invDay{_powerStationID}_s1; ";
//conn.Execute(ss);
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
#endregion
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);
@ -710,11 +743,17 @@ set a.inv_02 = b.inv_02 ");
}
throw ex;
}
#endregion
//}
//catch (Exception ex)
//{
//}
#endregion
conn.Close();
}
} //using (MySqlConnection conn = new MySqlConnection(Connection1))
result = true;
return result;