using Sln.Iot.Repository.dao;
using Sln.Iot.Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.CompilerServices.RuntimeHelpers;

namespace Sln.Iot.Repository.service
{
    public class ErrorService
    {
        private static readonly Lazy<ErrorService> lazy = new Lazy<ErrorService>(() => new ErrorService());

        public static ErrorService Instance
        {
            get
            {
                return lazy.Value;
            }
        }

        private SQLiteHelper<Vacuum1Error> _helperV1 = new SQLiteHelper<Vacuum1Error>();
        private SQLiteHelper<Vacuum2Error> _helperV2 = new SQLiteHelper<Vacuum2Error>();
        private SQLiteHelper<LiftError> _helperL = new SQLiteHelper<LiftError>();
        private SQLiteHelper<OvenError> _helperO = new SQLiteHelper<OvenError>();

        private SerilogHelper _log = SerilogHelper.Instance;

        #region 真空1错误操作

        /// <summary>
        /// 插入报错真空1
        /// </summary>
        /// <returns></returns>
        public bool InsertVacuum1Error(string Guid, string Message, string CNMessage, string code)
        {
            try
            {
                var res = _helperV1.Insert(new Vacuum1Error()
                {
                    ErrorGuid = Guid,
                    ErrorName = Message,
                    CNErrorName = CNMessage,
                    ErrorCode = code
                });
                return res == 0 ? false : true;
            }
            catch (Exception ex)
            {
                _log.Error("报警信息插入错误V1", ex);
                return false;
            }
        }

        /// <summary>
        /// 删除报错真空1
        /// </summary>
        /// <returns></returns>
        public string DeleteVacuum1Error(string Message)
        {
            try
            {
                var restr = _helperV1.QuerySingle(x => x.ErrorName == Message);
                if (restr == null)
                {
                    return string.Empty;
                }; 
                var res = _helperV1.SqlExcute($"DELETE FROM Vacuum1Error WHERE ErrorName = '{Message}'");
                if (res == 0)
                {
                    _log.Info("删除数据不成功，可能数据库中没有指定ErrorMessage的错误");
                }
                return restr.ErrorGuid;
            }
            catch (Exception ex)
            {
                _log.Error("报警信息删除错误V1", ex);
                return string.Empty;
            }
        }

        /// <summary>
        /// 获取报错列表
        /// </summary>
        /// <returns></returns>
        public List<Vacuum1Error> GetV1List()
        {
            try
            {
                var res = _helperV1.QueryAll();
                return res;
            }
            catch (Exception ex)
            {
                _log.Error("报警信息获取错误V1", ex);
                return new List<Vacuum1Error>();
            }
        }

        #endregion

        #region 真空2错误操作

        /// <summary>
        /// 插入报错真空2
        /// </summary>
        /// <returns></returns>
        public bool InsertVacuum2Error(string Guid, string Message, string CNMessage, string code)
        {
            try
            {
                var res = _helperV2.Insert(new Vacuum2Error()
                {
                    ErrorGuid = Guid,
                    ErrorName = Message,
                    CNErrorName = CNMessage,
                    ErrorCode = code
                });
                return res == 0 ? false : true;
            }
            catch (Exception ex)
            {
                _log.Error("报警信息插入错误V2", ex);
                return false;
            }
        }

        /// <summary>
        /// 删除报错真空2
        /// </summary>
        /// <returns></returns>
        public string DeleteVacuum2Error(string Message)
        {
            try
            {
                var restr = _helperV2.QuerySingle(x => x.ErrorName == Message);
                if(restr == null)
                {
                    return string.Empty;
                };
                var res = _helperV2.SqlExcute($"DELETE FROM Vacuum2Error WHERE ErrorName = '{Message}'");
                if (res == 0)
                {
                    _log.Info("删除数据不成功，可能数据库中没有指定ErrorMessage的错误");
                }
                return restr.ErrorGuid;
            }
            catch (Exception ex)
            {
                _log.Error("报警信息删除错误V2", ex);
                return string.Empty;
            }
        }

        /// <summary>
        /// 获取报错列表
        /// </summary>
        /// <returns></returns>
        public List<Vacuum2Error> GetV2List()
        {
            try
            {
                var res = _helperV2.QueryAll();
                return res;
            }
            catch (Exception ex)
            {
                _log.Error("报警信息获取错误V2", ex);
                return new List<Vacuum2Error>();
            }
        }

        #endregion

        #region 提升机错误操作

        /// <summary>
        /// 插入报错提升机
        /// </summary>
        /// <returns></returns>
        public bool InsertLiftError(string Guid, string Message, string CNMessage, string code)
        {
            try
            {
                var res = _helperL.Insert(new LiftError()
                {
                    ErrorGuid = Guid,
                    ErrorName = Message,
                    CNErrorName = CNMessage,
                    ErrorCode = code
                });
                return res == 0 ? false : true;
            }
            catch (Exception ex)
            {
                _log.Error("报警信息插入错误L", ex);
                return false;
            }
        }

        /// <summary>
        /// 删除报错提升机
        /// </summary>
        /// <returns></returns>
        public string DeleteliftError(string Message)
        {
            try
            {
                var restr = _helperL.QuerySingle(x => x.ErrorName == Message);
                if (restr == null)
                {
                    return string.Empty;
                }; 
                var res = _helperL.SqlExcute($"DELETE FROM LiftError WHERE ErrorName = '{Message}'");
                if (res == 0)
                {
                    _log.Info("删除数据不成功，可能数据库中没有指定ErrorMessage的错误");
                }
                return restr.ErrorGuid;
            }
            catch (Exception ex)
            {
                _log.Error("报警信息删除错误L", ex);
                return string.Empty;
            }
        }

        /// <summary>
        /// 获取报错列表提升机
        /// </summary>
        /// <returns></returns>
        public List<LiftError> GetLiftList()
        {
            try
            {
                var res = _helperL.QueryAll();
                return res;
            }
            catch (Exception ex)
            {
                _log.Error("报警信息获取错误L", ex);
                return new List<LiftError>();
            }
        }

        #endregion

        #region 烤箱错误操作

        /// <summary>
        /// 插入报错烤箱
        /// </summary>
        /// <returns></returns>
        public bool InsertOvenError(string Guid, string Message, string CNMessage, string code)
        {
            try
            {
                var res = _helperO.Insert(new OvenError()
                {
                    ErrorGuid = Guid,
                    ErrorName = Message,
                    CNErrorName = CNMessage,
                    ErrorCode = code
                });
                return res == 0 ? false : true;
            }
            catch (Exception ex)
            {
                _log.Error("报警信息插入错误O", ex);
                return false;
            }
        }

        /// <summary>
        /// 删除报错烤箱
        /// </summary>
        /// <returns></returns>
        public string DeleteOvenError(string Message)
        {
            try
            {
                var restr = _helperO.QuerySingle(x => x.ErrorName == Message);
                if (restr == null)
                {
                    return string.Empty;
                }; 
                var res = _helperO.SqlExcute($"DELETE FROM OvenError WHERE ErrorName = '{Message}'");
                if (res == 0)
                {
                    _log.Info("删除数据不成功，可能数据库中没有指定ErrorMessage的错误");
                }
                return restr.ErrorGuid;
            }
            catch (Exception ex)
            {
                _log.Error("报警信息删除错误O", ex);
                return string.Empty;
            }
        }

        /// <summary>
        /// 获取报错列表烤箱
        /// </summary>
        /// <returns></returns>
        public List<OvenError> GetOvenList()
        {
            try
            {
                var res = _helperO.QueryAll();
                return res;
            }
            catch (Exception ex)
            {
                _log.Error("报警信息获取错误O", ex);
                return new List<OvenError>();
            }
        }

        #endregion
    }
}
