using Dm;
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 VacuumInjectionPlcDataService
    {
        private static readonly Lazy<VacuumInjectionPlcDataService> lazy = new Lazy<VacuumInjectionPlcDataService>(() => new VacuumInjectionPlcDataService());

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

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

        private SerilogHelper _log = SerilogHelper.Instance;

        /// <summary>
        /// 插入数据
        /// </summary>
        /// <param name="GUID"></param>
        /// <param name=""></param>
        /// <returns></returns>
        public bool InsertData(string guid, UnitsProcess1Entity data)
        {
            try
            {
                if (guid.IsNullOrEmpty())
                {
                    return false;
                }
                var res = _helper.Insert(new VacuumInjectionPlcData()
                {
                    GUID = guid,
                    GlueAmountSetValue = data.GlueAmountSetValue1,
                    GluePushSpeedSetValue = data.GluePushSpeedSetValue1,
                    BarrelA1TempActValue = data.BarrelA1TempActValue1,
                    BarrelA2TempActValue = data.BarrelA2TempActValue1,
                    BarrelB1TempActValue = data.BarrelB1TempActValue1,
                    BarrelB2TempActValue = data.BarrelB2TempActValue1,
                    PumpAPressureActValue = data.PumpAPressureActValue1,
                    PumpBPressureActValue = data.PumpBPressureActValue1,
                    VacuumDegreeActValue = data.VacuumDegreeActValue1,
                    PressureHoldTimeSetValue = data.PressureHoldTimeSetValue1
                });
                return res == 0 ? false : true;
            }
            catch (Exception ex)
            {
                _log.Error("插入真空箱传感器数据失败", ex);
                return false;
            }
        }

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

                UnitsProcess1Entity entity = new UnitsProcess1Entity()
                {
                    GlueAmountSetValue1 = res.GlueAmountSetValue,
                    GluePushSpeedSetValue1 = res.GluePushSpeedSetValue,
                    BarrelA1TempActValue1 = res.BarrelA1TempActValue,
                    BarrelA2TempActValue1 = res.BarrelA2TempActValue,
                    BarrelB1TempActValue1 = res.BarrelB1TempActValue,
                    BarrelB2TempActValue1 = res.BarrelB2TempActValue,
                    PumpAPressureActValue1 = res.PumpAPressureActValue,
                    PumpBPressureActValue1 = res.PumpBPressureActValue,
                    VacuumDegreeActValue1 = res.VacuumDegreeActValue,
                    PressureHoldTimeSetValue1 = res.PressureHoldTimeSetValue
                };

                _helper.Delete(guid);

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