using HighWayIot.Log4net;
using HighWayIot.Repository.domain;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace HighWayIot.Repository.service
{
    /// <summary>
    /// 配方同步接口
    /// </summary>
    public class ZxMesPlanTransferService
    {
        private static readonly Lazy<ZxMesPlanTransferService> lazy = new Lazy<ZxMesPlanTransferService>(() => new ZxMesPlanTransferService());

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

        private LogHelper log = LogHelper.Instance;
        Repository<ZxMesPlanTransferEntity> _repository => new Repository<ZxMesPlanTransferEntity>("sqlserver");

        /// <summary>
        /// 查询所有物料类别信息
        /// </summary>
        /// <returns></returns>
        public List<ZxMesPlanTransferEntity> GetRecipeInfos()
        {
            try
            {
                List<ZxMesPlanTransferEntity> entity = _repository.GetList();
                return entity;
            }
            catch (Exception ex)
            {
                log.Error("配方同步信息获取异常", ex);
                return null;
            }
        }

        /// <summary>
        /// 查询所有物料类别信息
        /// </summary>
        /// <returns></returns>
        public List<ZxMesPlanTransferEntity> GetRecipeInfos(Expression<Func<ZxMesPlanTransferEntity, bool>> whereExpression)
        {
            try
            {
                List<ZxMesPlanTransferEntity> entity = _repository.GetList(whereExpression);
                return entity;
            }
            catch (Exception ex)
            {
                log.Error("配方同步信息获取异常", ex);
                return null;
            }
        }

        /// <summary>
        /// 更新基准数据标识为True
        /// </summary>
        /// <returns></returns>
        public bool SetFlagTrue()
        {
            try
            {
                var result = _repository.Update(x => new ZxMesPlanTransferEntity() { RequestFlag = true }, x => x.RequestFlag == false);
                return result;
            }
            catch (Exception ex)
            {
                log.Error("配方同步基准信息修改异常", ex);
                return false;
            }
        }

        /// <summary>
        /// 获取更新基准数据标识状态
        /// </summary>
        /// <returns></returns>
        public bool GetFlagState()
        {
            try
            {
                var result = _repository.GetById("syncsignal").RequestFlag;
                return result ?? true;
            }
            catch (Exception ex)
            {
                log.Error("配方同步基准信息标识获取异常", ex);
                return true;
            }
        }

        ///// <summary>
        ///// 有更无插
        ///// </summary>
        ///// <returns></returns>
        //public int UpdateOrAddInfos(List<ZxMesPlanTransferEntity> lists)
        //{
        //    try
        //    {
        //        return _repository.Context.Storageable(lists).WhereColumns(x => x.RecipeName).ExecuteCommand();
        //    }
        //    catch (Exception ex)
        //    {
        //        log.Error($"更新配方数据失败", ex);
        //        return 0;
        //    }
        //}
    }
}
