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 power_stationID, string action_name, double duration_sec, string src_table, string dest_table, string procResult, string errMessage, string note,  MySqlConnection myCon , MySqlCommand cmd = null) {

            MySqlCommand _cmd = new MySqlCommand();
            if (cmd == null) {
                _cmd.Connection = myCon;
                _cmd.CommandTimeout = 720;
            }
            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
                                errMsg + "', '"+ note +"');"; // err_txt
            _cmd.CommandText = sql;
            _cmd.ExecuteNonQuery();
        }
    }
}
