using HTCoreServiceApp.DataHandle;
using HTCoreServiceApp.Models;
using SqlSugar;
using System;
using System.Diagnostics.CodeAnalysis;

namespace HTCoreServiceApp.Common
{
	public class LogHelper
	{
		public static void LogWrite(string message)
		{
			Console.WriteLine(message);
			var logmodel = new HTServiceLog()
			{
				EventTime = DateTime.Now,
				Message = message
			};
			try
			{
                //string ConnectionStrings = AESKeyEncrypt.DecryptDES(ConfigHelper.ReadAppSettings("ConnectionStrings", "SqlServer"));
                var db = new SqlSugarClient(StaticLibrary.DBConfig);
				//var db = StaticLibrary.DBContext;
				db.Insertable(logmodel).ExecuteCommand();
				//db.HT_Service_Log.Add(logmodel);
				//db.SaveChanges();
				db.Dispose();
			}
			catch (Exception ex) 
			{ 
				Console.WriteLine(ex.ToString());
			}
		}
		public static void SetDriverLogTask(HTLogSetDriver log)
		{
			var task1 = Task.Run(() =>
						SetDriverLog(log)
						);
		}
		public static void SetDriverLog(HTLogSetDriver log)
		{
			try
			{
				
				//string ConnectionStrings = AESKeyEncrypt.DecryptDES(ConfigHelper.ReadAppSettings("ConnectionStrings", "SqlServer"));
				var db = new SqlSugarClient(StaticLibrary.DBConfig);
				//var db = StaticLibrary.DBContext;
				db.Insertable(log).ExecuteCommand();
				//db.HT_Service_Log.Add(logmodel);
				//db.SaveChanges();
				db.Dispose();
			}
			catch (Exception ex)
			{
				LogWrite("SetDriver写入日志出错：" + ex.ToString());
				Console.WriteLine(ex.ToString());
			}
		}
	}
}
