using CCDCount.DLL.SqlDataClass;
using CCDCount.MODEL.ShuLiClass;
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 FaultLogClass
    {
        public event EventHandler<EventArgs> ErroroCcurred;
        ErrorMesSqliteDataClass errorMesSqliteDataClass = null;

        public FaultLogClass(string dbPath)
        {
            errorMesSqliteDataClass = new ErrorMesSqliteDataClass(dbPath);
        }

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

        /// <summary>
        /// 记录更改信息
        /// </summary>
        /// <param name="Message"></param>
        /// <param name="userid"></param>
        public void RecordValueChangeMessage(string Message, int userid)
        {
            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 void RecordValueSaveMessage(string Message, int userid)
        {
            ErroeMesDataModelClass errorMessage = new ErroeMesDataModelClass
            {
                Message = Message,
                DateTime = DateTime.Now,
                Category = "系统",
                MessageType = "值保存",
                UserID = userid
            };
            errorMesSqliteDataClass.InsertErrMessage(errorMessage);
        }

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

        /// <summary>
        /// 对外通知事件
        /// </summary>
        private void OnErroroCcurred()
        {
            // 触发事件
            ErroroCcurred?.Invoke(this, null);
        }
    }
}
