using CCDCount.DLL.SqlDataClass;
using CCDCount.MODEL.ShuLiModel;
using CCDCount.MODEL.SqlDataModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CCDCount.DLL
{
    public class FaultLog
    {
        public static event Action<ErrorMesDataEventModelClass> FaultError;
        static ErrorMesSqliteDataClass errorMesSqliteDataClass = null;
        static string dbPath = $"{AppDomain.CurrentDomain.BaseDirectory}DATA\\FaultMessage\\FaultMessage_{DateTime.Now:yyyyMMdd}.db";
        static int userid = 0;

        static FaultLog()
        {
            errorMesSqliteDataClass = new ErrorMesSqliteDataClass(dbPath);
        }

        public static void SetUserID(int id)
        {
            userid = id;
        }

        /// <summary>
        /// 记录错误信息
        /// </summary>
        /// <param name="Message"></param>
        /// <param name="userid"></param>
        public static void RecordErrorMessage(string Message)
        {
            Task.Run(() =>
            {
                ErroeMesDataModelClass errorMessage = new ErroeMesDataModelClass
                {
                    Message = Message,
                    DateTime = DateTime.Now,
                    Category = "系统",
                    MessageType = "异常",
                    UserID = userid
                };
                errorMesSqliteDataClass.InsertErrMessage(errorMessage);
                FaultError?.Invoke(new ErrorMesDataEventModelClass(errorMessage));
            });
        }

        /// <summary>
        /// 记录错误信息
        /// </summary>
        /// <param name="Message"></param>
        /// <param name="userid"></param>
        public static void RecordErrorMessage(string Message,string path)
        {
            Task.Run(() =>
            {
                ErroeMesDataModelClass errorMessage = new ErroeMesDataModelClass
                {
                    Message = Message,
                    DateTime = DateTime.Now,
                    Category = "系统",
                    MessageType = "异常",
                    Path = path,
                    UserID = userid
                };
                errorMesSqliteDataClass.InsertErrMessage(errorMessage);
                FaultError?.Invoke(new ErrorMesDataEventModelClass(errorMessage));
            });
        }

        /// <summary>
        /// 记录Log信息
        /// </summary>
        /// <param name="Message"></param>
        /// <param name="LogLevel"></param>
        public static void RecordLogMessage(string Message,int LogLevel)
        {
            Task.Run(() =>
            {
                ErroeMesDataModelClass errorMessage = new ErroeMesDataModelClass
                {
                    Message = Message,
                    DateTime = DateTime.Now,
                    Category = "系统",
                    MessageType = "日志",
                    UserID = userid,
                    LogLevel = LogLevel
                };
                errorMesSqliteDataClass.InsertErrMessage(errorMessage);
            });
        }

        /// <summary>
        /// 记录更改信息
        /// </summary>
        /// <param name="Message"></param>
        /// <param name="userid"></param>
        public static void RecordValueChangeMessage(string Message)
        {
            Task.Run(() =>
            {
                ErroeMesDataModelClass errorMessage = new ErroeMesDataModelClass
                {
                    Message = Message,
                    DateTime = DateTime.Now,
                    Category = "系统",
                    MessageType = "值变更",
                    UserID = userid
                };
                errorMesSqliteDataClass.InsertErrMessage(errorMessage);
            });
        }

        /// <summary>
        /// 记录值保存信息
        /// </summary>
        /// <param name="Message"></param>
        /// <param name="userid"></param>
        public static void RecordValueSaveMessage(string Message)
        {
            Task.Run(() =>
            {
                ErroeMesDataModelClass errorMessage = new ErroeMesDataModelClass
                {
                    Message = Message,
                    DateTime = DateTime.Now,
                    Category = "系统",
                    MessageType = "值保存",
                    UserID = userid
                };
                errorMesSqliteDataClass.InsertErrMessage(errorMessage);
            });
        }

        /// <summary>
        /// 读取全部信息
        /// </summary>
        /// <returns></returns>
        public static List<ErroeMesDataModelClass> GetAllErrMessage()
        {
            return errorMesSqliteDataClass.GetAllErrMessage();
        }
    }
}
