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

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

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

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

        /// <summary>
        /// 添加日志
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool InsertLogInfo(SysLogEntity entity)
        {
            try
            {
                return _repository.Insert(entity);
            }
            catch (Exception ex)
            {
                log.Error("日志信息插入异常", ex);
                return false;
            }
        }
    }
}
