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

namespace HighWayIot.Repository.service
{
    public class ZxReaderSettingService
    {
        private static readonly Lazy<ZxReaderSettingService> lazy = new Lazy<ZxReaderSettingService>(() => new ZxReaderSettingService());

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

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

        /// <summary>
        /// 查询所有RFID设备信息
        /// </summary>
        /// <returns></returns>
        public List<ZxReaderSettingEntity> GetReaderInfos(Expression<Func<ZxReaderSettingEntity, bool>> expression = null)
        {
            try
            {
                List<ZxReaderSettingEntity> entity;
                if (expression != null)
                {
                    entity = _repository.GetList(expression);
                }
                else
                {
                    entity = _repository.GetList();
                }
                return entity;
            }
            catch (Exception ex)
            {
                log.Error("RFID设备信息获取异常", ex);
                return null;
            }
        }

        /// <summary>
        /// 根据IP查询读写器工位信息
        /// </summary>
        /// <returns></returns>
        public string GetWorkstateNoByIp(string ip)
        {
            try
            {
                ZxReaderSettingEntity entity;
                entity = _repository.GetFirst(x => x.RfidIp == ip);
                
                return entity.WorkstationNo;
            }
            catch (Exception ex)
            {
                log.Error("RFID设备信息获取异常", ex);
                return null;
            }
        }

        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateReaderInfo(ZxReaderSettingEntity entity)
        {
            try
            {
                return _repository.Update(entity);
            }
            catch (Exception ex)
            {
                log.Error("RFID信息修改异常", ex);
                return false;
            }
        }

        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateRangeReaderInfo(List<ZxReaderSettingEntity> entitys)
        {
            try
            {
                return _repository.UpdateRange(entitys);
            }
            catch (Exception ex)
            {
                log.Error("RFID集合信息修改异常", ex);
                return false;
            }
        }
    }
}
