using Microsoft.IdentityModel.Tokens;
using Sln.Iot.Model.Entity;
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;

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

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

        private SQLiteHelper<OvenPlcData> _helper = new SQLiteHelper<OvenPlcData>();

        private SerilogHelper _log = SerilogHelper.Instance;

        /// <summary>
        /// 插入数据
        /// </summary>
        /// <param name="GUID"></param>
        /// <param name=""></param>
        /// <returns></returns>
        public bool InsertData(string guid, int data)
        {
            try
            {
                if (guid.IsNullOrEmpty())
                {
                    return false;
                }
                var res = _helper.Insert(new OvenPlcData()
                {
                    GUID = guid,
                    PreheatOvenTempActValue = data,
                });
                return res == 0 ? false : true;
            }
            catch (Exception ex)
            {
                _log.Error("插入烤炉数据失败", ex);
                return false;
            }
        }

        /// <summary>
        /// 预固炉温度插入
        /// </summary>
        /// <returns></returns>
        public bool UpdateDataPreCure(string guid, UnitsProcess3Entity data)
        {
            try
            {
                if (guid.IsNullOrEmpty())
                {
                    return false;
                }
                var oldData = _helper.Query(x => x.GUID == guid).FirstOrDefault();
                if (oldData != null)
                {
                    oldData.PreCureOven1TempActValue = data.PreCureOven1TempActValue;
                    oldData.PreCureOven2TempActValue = data.PreCureOven2TempActValue;
                    oldData.PreCureOven3TempActValue = data.PreCureOven3TempActValue;
                    oldData.PreCureOven4TempActValue = data.PreCureOven4TempActValue;
                }
                else
                {
                    oldData = new OvenPlcData()
                    {
                        GUID = guid,
                        PreCureOven1TempActValue = data.PreCureOven1TempActValue,
                        PreCureOven2TempActValue = data.PreCureOven2TempActValue,
                        PreCureOven3TempActValue = data.PreCureOven3TempActValue,
                        PreCureOven4TempActValue = data.PreCureOven4TempActValue,
                    };
                }

                var res = _helper.Update(oldData);
                return res == 0 ? false : true;
            }
            catch (Exception ex)
            {
                _log.Error("更新预固炉温度数据失败", ex);
                return false;
            }
        }

        /// <summary>
        /// 固化炉温度插入
        /// </summary>
        /// <returns></returns>
        public bool UpdateDataCure(string guid, UnitsProcess3Entity data)
        {
            try
            {
                if (guid.IsNullOrEmpty())
                {
                    return false;
                }
                var oldData = _helper.Query(x => x.GUID == guid).FirstOrDefault();
                if (oldData != null)
                {
                    oldData.CureOven1TempActValue = data.CureOven1TempActValue;
                    oldData.CureOven2TempActValue = data.CureOven2TempActValue;
                    oldData.CureOven3TempActValue = data.CureOven3TempActValue;
                    oldData.CureOven4TempActValue = data.CureOven4TempActValue;
                }
                else
                {
                    oldData = new OvenPlcData()
                    {
                        GUID = guid,
                        CureOven1TempActValue = data.CureOven1TempActValue,
                        CureOven2TempActValue = data.CureOven2TempActValue,
                        CureOven3TempActValue = data.CureOven3TempActValue,
                        CureOven4TempActValue = data.CureOven4TempActValue,
                    };
                }

                var res = _helper.Update(oldData!);
                return res == 0 ? false : true;
            }
            catch (Exception ex)
            {
                _log.Error("更新固化炉炉温度数据失败", ex);
                return false;
            }
        }

        /// <summary>
        /// 获取之前暂存的数据
        /// </summary>
        /// <returns></returns>
        public UnitsProcess3Entity GetData(string guid)
        {
            try
            {
                if (guid.IsNullOrEmpty())
                {
                    return new UnitsProcess3Entity();
                }
                var res = _helper.QuerySingle(x => x.GUID == guid);
                if (res == null)
                {
                    return new UnitsProcess3Entity();
                }

                UnitsProcess3Entity entity = new UnitsProcess3Entity()
                {
                    PreheatOvenTempActValue = res.PreheatOvenTempActValue,
                    PreCureOven1TempActValue = res.PreCureOven1TempActValue,
                    PreCureOven2TempActValue = res.PreCureOven2TempActValue,
                    PreCureOven3TempActValue = res.PreCureOven3TempActValue,
                    PreCureOven4TempActValue = res.PreCureOven4TempActValue,
                    CureOven1TempActValue = res.CureOven1TempActValue,
                    CureOven2TempActValue = res.CureOven2TempActValue,
                    CureOven3TempActValue = res.CureOven3TempActValue,
                    CureOven4TempActValue = res.CureOven4TempActValue,
                };

                _helper.Delete(guid);

                return entity;
            }
            catch (Exception ex)
            {
                _log.Error("获取或删除隧道烤箱传感器数据失败", ex);
                return new UnitsProcess3Entity();
            }
        }
    }
}
