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

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

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

        /// <summary>
        /// 条件查询所有用户列表 用户名为模糊查询
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="userRole">用户角色</param>
        /// <param name="beginTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="isSelectByTime">是否</param>
        /// <returns></returns>
        public List<SysUserEntity> GetUserInfos(string userName = null, string userRole = null, DateTime? beginTime = null, DateTime? endTime = null, bool isSelectByTime = false)
        {
            try
            {
                List<SysUserEntity> entity = _repository.GetList(x => x.IsDeleted == false);

                if (!string.IsNullOrEmpty(userName))
                {
                    entity = entity.Where(x => x.UserName.Contains(userName)).ToList();
                }

                if (!string.IsNullOrEmpty(userRole))
                {
                    entity = entity.Where(x => x.UserRole == userRole).ToList();
                }

                if (isSelectByTime)
                {
                    entity = entity.Where(x => x.LastLoginTime >= beginTime && x.LastLoginTime <= endTime).ToList();
                }

                return entity;
            }
            catch (Exception ex)
            {
                log.Error("用户信息获取异常", ex);
                return null;
            }
        }

        /// <summary>
        /// 添加用户信息
        /// </summary>
        /// <param name="sysUserEntity"></param>
        /// <returns></returns>
        public bool InsertUserInfo(SysUserEntity sysUserEntity)
        {
            try
            {
                return _repository.Insert(sysUserEntity);
            }
            catch (Exception ex)
            {
                log.Error("用户信息插入异常", ex);
                return false;
            }
        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="sysUserEntity"></param>
        /// <returns></returns>
        public bool UpdateUserInfo(SysUserEntity sysUserEntity)
        {
            try
            {
                return _repository.Update(sysUserEntity);
            }
            catch(Exception ex)
            {
                log.Error("用户信息修改异常", ex);
                return false;
            }
        }

        /// <summary>
        /// 根据ID删除用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteUserInfoById(int id)
        {
            try
            {
                SysUserEntity entity = _repository.GetById(id);
                entity.IsDeleted = true;
                return _repository.Update(entity);
            }
            catch (Exception ex)
            {
                log.Error("用户信息删除异常", ex);
                return false;
            }
        }

        /// <summary>
        /// 根据用户名查询单条用户信息
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public List<SysUserEntity> GetUserInfoByUserName(string userName)
        {
            try
            {
                var list = _repository.GetList(x => x.UserName == userName && x.IsDeleted == false);
                return list;
            }
            catch (Exception ex)
            {
                log.Error("单条用户信息查询异常", ex);
                return null;
            }
        }
    }
}
