using Sln.Iot.Repository.dao;
using Sln.Iot.Serilog;
using SQLitePCL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sln.Iot.Repository.service
{
    /// <summary>
    /// 托盘绑定服务类
    /// </summary>
    public class TrayBindingService
    {
        private static readonly Lazy<TrayBindingService> lazy = new Lazy<TrayBindingService>(() => new TrayBindingService());

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

        private SQLiteHelper<TrayRfidBinding> _helper = SQLiteHelper<TrayRfidBinding>.Instance;

        private SerilogHelper _log = SerilogHelper.Instance;

        /// <summary>
        /// 绑定信息根据托盘号删除
        /// </summary>
        /// <returns></returns>
        public bool TrayCodeDelete(string trayCode)
        {
            try
            {
                int res = _helper.DeleteRange(trayCode);
                return res != 0;
            }
            catch(Exception ex)
            {
                _log.Error("托盘码删除错误", ex);
                return false;
            }
        }

        /// <summary>
        /// 托盘二维码重新绑定
        /// </summary>
        /// <param name="trayCode">托盘码</param>
        /// <param name="prodCode">产品码</param>
        /// <returns></returns>
        public bool TrayBindingRefresh(string trayCode, string[] prodCode)
        {
            try
            {
                //先删除原来的
                _helper.DeleteRange(trayCode);
                //再绑定（插入）新的
                var entities = new List<TrayRfidBinding>();
                foreach (string code in prodCode)
                {
                    if (string.IsNullOrWhiteSpace(code))
                    {
                        continue;
                    }
                    TrayRfidBinding entity = new TrayRfidBinding()
                    {
                        GUID = Guid.NewGuid().ToString("N"),
                        TrayCode = trayCode,
                        ProductionCode = code
                    };
                    entities.Add(entity);
                }
                _helper.InsertRange(entities);
                return true;
            }
            catch(Exception ex)
            {
                _log.Error("托盘二维码重新绑定错误", ex);
                return false;
            }
        }

        /// <summary>
        /// 存储对应托盘码的时间节点
        /// </summary>
        /// <param name="timeString">时间字符串</param>
        /// <param name="trayCode">托盘码</param>
        /// <param name="timeCount">时间参数编号</param>
        /// <returns></returns>
        public bool UpDateTime(string timeString, string trayCode, string timeCount)
        {
            try
            {
                int res = _helper.SqlExcute($"update TrayRfidBinding set time{timeCount} = '{timeString}' where TrayCode = '{trayCode}'");
                if(res <= 0 || res > 6)
                {
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                _log.Error("时间更新错误", ex);
                return false;
            }
        }

        /// <summary>
        /// 获取一个时间
        /// </summary>
        /// <param name="trayCode"></param>
        /// <returns></returns>
        public string GetTime2ByTrayCode(string trayCode)
        {
            string time2 = string.Empty;
            try
            {
                var res = _helper.Query(t => t.TrayCode == trayCode).FirstOrDefault();
                if (res != null)
                {
                    time2 = res.Time2;
                }
                return time2;
            }
            catch (Exception ex)
            {
                _log.Error("根据托盘码获取time2错误", ex);
                return time2;
            }
        }
    }
}
