using System;
using System.Data;
using Aitex.Core.RT.DBCore;

namespace MECF.Framework.Common.DBCore
{
	public class RuntimeDataRecorder
	{
		public static void UpdateElapseTimeSystem(int minutes)
		{
			string cmdText = $"Select  \"guid\",\"elapse_minutes\" from \"runtime_data2\" where \"ispm\"='false';";
			DataSet dataSet = DB.ExecuteDataSet(cmdText);
			if (dataSet != null && dataSet.Tables != null && dataSet.Tables[0].Rows.Count > 0)
			{
				cmdText = "";
				for (int i = 0; i < dataSet.Tables[0].Rows.Count; i++)
				{
					long num = Convert.ToInt64(dataSet.Tables[0].Rows[i]["elapse_minutes"].ToString()) + minutes;
					string arg = dataSet.Tables[0].Rows[i]["guid"].ToString();
					cmdText += $"Update \"runtime_data2\" set  \"elapse_minutes\"='{num}' where \"guid\"='{arg}';";
				}
				if (!string.IsNullOrEmpty(cmdText))
				{
					DB.InsertSql(cmdText);
				}
			}
		}

		public static void UpdateElapseTimePM(string pmName, int second)
		{
			string cmdText = $"Select \"Process_Seconds\" from \"runtime_data2\" where \"Module\"='{pmName}';";
			DataSet dataSet = DB.ExecuteDataSet(cmdText);
			if (dataSet != null && dataSet.Tables != null && dataSet.Tables.Count == 1 && dataSet.Tables[0].Rows.Count == 1)
			{
                long num = Convert.ToInt64(dataSet.Tables[0].Rows[0]["Process_Seconds"].ToString()) + second;
                cmdText = $"Update \"runtime_data2\" set  \"Process_Seconds\"='{num}' where \"Module\"='{pmName}';";
                DB.ExecuteNonQuery(cmdText);
            }
			else
			{
				cmdText = $"INSERT INTO \"runtime_data2\" ( \"Module\", \"Process_Seconds\")VALUES ('{pmName}',{second});";
                DB.ExecuteNonQuery(cmdText);
            }
		}

        public static void ResetElapseTimePM(string pmName)
        {
            string cmdText = $"Update \"runtime_data2\" set  \"Process_Seconds\"='{0}' where \"Module\"='{pmName}';";
            DB.ExecuteNonQuery(cmdText);
        }

    }
}
