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

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

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

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

        /// <summary>
        /// 根据EPC查询指定RFID标签DeviceNo信息
        /// </summary>
        /// <returns></returns>
        public string GetTagDeviceNoByEPC(string Epc)
        {
            try
            {
                ZxTagSettingEntity entity;
                entity = _repository.GetFirst(x => x.RfidEpc == Epc);
                
                return entity.DeviceNo;
            }
            catch (Exception ex)
            {
                log.Error("RFID标签信息获取异常", ex);
                return null;
            }
        }

        /// <summary>
        /// 修改标签信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateTagInfo(ZxTagSettingEntity 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 UpdateRangeTagInfo(List<ZxTagSettingEntity> entitys)
        {
            try
            {
                return _repository.UpdateRange(entitys);
            }
            catch (Exception ex)
            {
                log.Error("RFID标签集合信息修改异常", ex);
                return false;
            }
        }
    }
}
