using IRaCIS.Application.Contracts;
using IRaCIS.Core.Domain.Share;
using IRaCIS.Core.Infrastructure;
using System.Text.RegularExpressions;

using Microsoft.AspNetCore.Mvc;
using Panda.DynamicWebApi.Attributes;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Identity.Client;
using static IRaCIS.Core.Domain.Share.StaticData;
using IRaCIS.Core.Application.ViewModel;
using Medallion.Threading;
using EasyCaching.Core;
using IRaCIS.Core.Application.Contracts;
using LoginReturnDTO = IRaCIS.Application.Contracts.LoginReturnDTO;

namespace IRaCIS.Application.Services
{
    [ApiExplorerSettings(GroupName = "Management")]
    public class UserService : BaseService, IUserService
    {
        private readonly IRepository<User> _userRepository;
	
		private readonly IMailVerificationService _mailVerificationService;
        private readonly IRepository<VerificationCode> _verificationCodeRepository;
        private readonly IRepository<Doctor> _doctorRepository;
        private readonly IRepository<TrialUser> _userTrialRepository;
        private readonly IRepository<UserLog> _userLogRepository;
		private readonly IRepository<UserPassWordLog> _userPassWordLogRepository;
		private readonly IDistributedLockProvider _distributedLockProvider;
        private readonly IEasyCachingProvider _cache;
		private readonly IReadingImageTaskService _readingImageTaskService;
		private readonly IOptionsMonitor<ServiceVerifyConfigOption> _verifyConfig;


        public UserService(IRepository<User> userRepository,

            IMailVerificationService mailVerificationService,
            IRepository<VerificationCode> verificationCodeRepository,
            IRepository<Doctor> doctorRepository,
            IEasyCachingProvider cache,
			IReadingImageTaskService readingImageTaskService,
			IRepository<TrialUser> userTrialRepository,
            IOptionsMonitor<ServiceVerifyConfigOption> verifyConfig,
            IRepository<UserLog> userLogRepository,
				IRepository<UserPassWordLog> userPassWordLogRepository
,
            IDistributedLockProvider distributedLockProvider)
        {
            _userLogRepository = userLogRepository;
			this._userPassWordLogRepository = userPassWordLogRepository;
			_verifyConfig = verifyConfig;
            _cache = cache;
			this._readingImageTaskService = readingImageTaskService;
			_userRepository = userRepository;
            _mailVerificationService = mailVerificationService;
            _verificationCodeRepository = verificationCodeRepository;
            _doctorRepository = doctorRepository;
            _userTrialRepository = userTrialRepository;
            _userLogRepository = userLogRepository;
            _distributedLockProvider = distributedLockProvider;
        }



        private async Task VerifyUserNameAsync(Guid? userId, string userName)
        {
            if (await _userRepository.WhereIf(userId != null, t => t.Id != userId).AnyAsync(t => t.UserName == userName))
            {
                //---用户名已经存在。
                throw new BusinessValidationFailedException(_localizer["User_UsernameExist"]);
            }
        }

        private async Task VerifyUserPhoneAsync(Guid? userId, Guid userTypeId, string phone)
        {
            if (await _userRepository.WhereIf(userId != null, t => t.Id != userId).AnyAsync(t => (t.Phone == phone && t.UserTypeId == userTypeId)))
            {
                //---该用户类型中已存在具有相同的电话的用户。
                throw new BusinessValidationFailedException(_localizer["User_PhoneDup"]);
            }
        }


        private async Task VerifyUserEmailAsync(Guid? userId, Guid userTypeId, string email)
        {
            if (await _userRepository.WhereIf(userId != null, t => t.Id != userId).AnyAsync(t => (t.EMail == email && t.UserTypeId == userTypeId)))
            {
                //---该用户类型中已存在具有相同邮箱的用户。
                throw new BusinessValidationFailedException(_localizer["User_EmailDup"]);
            }
        }

        private async Task VerifyUserPwdAsync(Guid userId, string newPwd, string? oldPwd = null)
        {
			//var dbUser = (await _userRepository.FirstOrDefaultAsync(t => t.Id == userId)).IfNullThrowException();

			if (oldPwd != null && oldPwd == newPwd)
			{
				//---新密码与旧密码相同。
				throw new BusinessValidationFailedException(_localizer["User_NewOldPwdSame"]);
			}


			var dbUser = (await _userRepository.Where(t => t.Id == userId).FirstOrDefaultAsync()).IfNullThrowException();

			if (oldPwd != null && dbUser.Password != oldPwd)
			{
				//---旧密码验证失败。
				throw new BusinessValidationFailedException(_localizer["User_OldPwdInvalid"]);
			}

			if (dbUser.Password == newPwd)
			{
				//---新密码与旧密码相同。
				throw new BusinessValidationFailedException(_localizer["User_NewOldPwdSame"]);
			}

            var passWordList = await _userPassWordLogRepository.Where(x => x.UserId == userId).OrderByDescending(x => x.CreateTime).Take(2).ToListAsync();
            if (passWordList.Any(x => x.PassWord == newPwd))
            {
				throw new BusinessValidationFailedException(_localizer["User_PassWordRepeat"]);
			}
            if (oldPwd != null)
            {
				await _userPassWordLogRepository.AddAsync(new UserPassWordLog()
				{

					CreateTime = DateTime.Now,
					PassWord = oldPwd,
					UserId = userId,
				});
			}
          

            await _userRepository.BatchUpdateNoTrackingAsync(x => x.Id == userId, x => new User()
            {
                LastChangePassWordTime = DateTime.Now,
            });
            await _userPassWordLogRepository.SaveChangesAsync();


			await Task.CompletedTask;


        }


        /// <summary>发送验证码  修改邮箱(已经登陆修改) New </summary>

        [HttpGet("{email}")]
        public async Task<IResponseOutput> SendVerificationCode(string email)
        {

            //检查手机或者邮箱是否有效
            if (!Regex.IsMatch(email, @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"))
            {
                //---Please input a legal email
                return ResponseOutput.NotOk(_localizer["User_LegalEmail"]);
            }


            //验证码 6位
            int verificationCode = new Random().Next(100000, 1000000);

            await _mailVerificationService.SendMailEditEmail(_userInfo.Id, _userInfo.RealName, email, verificationCode);

            return ResponseOutput.Ok();

        }


        [HttpPut("{newEmail}/{verificationCode}")]
        [UnitOfWork]
        public async Task<IResponseOutput> SetNewEmail(string newEmail, string verificationCode)
        {


            var verificationRecord = await _verificationCodeRepository
                .FirstOrDefaultAsync(t => t.UserId == _userInfo.Id && t.Code == verificationCode && t.CodeType == 0);

            //检查数据库是否存在该验证码
            if (verificationRecord == null)
            {

                //---验证码错误。
                return ResponseOutput.NotOk(_localizer["User_VerificationCodeError"]);

            }
            else
            {
                //检查验证码是否失效
                if (verificationRecord.ExpirationTime < DateTime.Now)
                {
                    //---验证码已经过期。
                    return ResponseOutput.NotOk(_localizer["User_VerificationCodeExpired"]);

                }
                else //验证码正确 并且 没有超时 
                {


                    await VerifyUserEmailAsync(_userInfo.Id, _userInfo.UserTypeId, newEmail);


                    await _userRepository.UpdatePartialNowNoQueryAsync(_userInfo.Id, u => new User()
                    {
                        EMail = newEmail
                    });

                    //删除验证码历史记录
                    await _verificationCodeRepository.BatchDeleteNoTrackingAsync(t => t.UserId == _userInfo.Id && t.CodeType == 0);

                    await _userLogRepository.AddAsync(new UserLog() { IP = _userInfo.IP, LoginUserId = _userInfo.Id, OptUserId = _userInfo.Id, OptType = UserOptType.UpdateUser }, true);

                    return ResponseOutput.Ok();

                }
            }
        }


        [HttpPut("{newPhone}")]
        public async Task<IResponseOutput> SetNewPhone(string newPhone)
        {

            await VerifyUserPhoneAsync(_userInfo.Id, _userInfo.UserTypeId, newPhone);

            await _userRepository.UpdatePartialNowNoQueryAsync(_userInfo.Id, u => new User()
            {
                Phone = newPhone
            });

            await _userLogRepository.AddAsync(new UserLog() { IP = _userInfo.IP, LoginUserId = _userInfo.Id, OptUserId = _userInfo.Id, OptType = UserOptType.UpdateUser }, true);

            return ResponseOutput.Ok();
        }


        [HttpPut("{newUserName}")]
        public async Task<IResponseOutput> SetNewUserName(string newUserName)
        {
            await VerifyUserNameAsync(_userInfo.Id, newUserName);


            await _userRepository.UpdatePartialNowNoQueryAsync(_userInfo.Id, u => new User()
            {
                UserName = newUserName
            });

            await _userLogRepository.AddAsync(new UserLog() { IP = _userInfo.IP, LoginUserId = _userInfo.Id, OptUserId = _userInfo.Id, OptType = UserOptType.UpdateUser }, true);
            return ResponseOutput.Ok();
        }




        [HttpGet]
        public async Task<IResponseOutput> InitSetUserNameAndPwd(Guid userId, string newUserName, string newPWd)
        {


            await VerifyUserPwdAsync(userId, newPWd);

            await VerifyUserNameAsync(userId, newUserName);

            await _userRepository.UpdatePartialFromQueryAsync(userId, u => new User()
            {
                UserName = newUserName,

                Password = newPWd,

                IsFirstAdd = false,

                EmailToken = String.Empty

            }, true);

            await _userLogRepository.AddAsync(new UserLog() { IP = _userInfo.IP, LoginUserId = _userInfo.Id, OptUserId = _userInfo.Id, OptType = UserOptType.UpdateUser }, true);

            return ResponseOutput.Ok();
        }


        /// <summary>
        /// 重置密码为 默认密码
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet("{userId:guid}")]
        [UnitOfWork]
        public async Task<IResponseOutput> ResetPassword(Guid userId)
        {

            var pwd = "123456";

            if (_hostEnvironment.EnvironmentName != "Development")
            {
                pwd = "Extimaging." + new Random().Next(100, 1000);

            }

            try
            {
                await _mailVerificationService.AdminResetPwdSendEmailAsync(userId, pwd);
            }
            catch (Exception )
            {
                //---请检查邮箱地址或者联系维护人员, 邮件发送失败, 未能创建账户成功
                throw new BusinessValidationFailedException(_localizer["User_CreateFailed"]);
            }


            await _userRepository.UpdatePartialNowNoQueryAsync(userId, u => new User()
            {
                Password = MD5Helper.Md5(pwd),
                IsFirstAdd = true
            });

            await _userLogRepository.AddAsync(new UserLog() { IP = _userInfo.IP, LoginUserId = _userInfo.Id, OptUserId=userId,  OptType = UserOptType.ResetPassword }, true);

            return ResponseOutput.Ok();
        }



        /// <summary>
        ///  重置密码发邮件  (未登陆修改)
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet("{email}")]
        public async Task<IResponseOutput> AnonymousSendVerificationCode(string email)
        {

            //检查手机或者邮箱是否有效
            if (!Regex.IsMatch(email, @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"))
            {

                //---请输入一个正确的邮箱。
                return ResponseOutput.NotOk(_localizer["User_InvalidEmail"]);

            }

            ////查找改邮箱或者手机的用户
            var exist = await _userRepository.AnyAsync(t => t.EMail == email);

            if (!exist)
            {
                //---邮箱错误。
                return ResponseOutput.NotOk(_localizer["User_EmailError"]);

            }


            //验证码 6位
            int verificationCode = new Random().Next(100000, 1000000);

            await _mailVerificationService.AnolymousSendEmailForResetAccount(email, verificationCode);

            return ResponseOutput.Ok();

        }

        /// <summary>
        /// 验证验证码,没问题就返回用户所有的账户
        /// </summary>
        /// <param name="email"></param>
        /// <param name="verifyCode"></param>
        /// <returns></returns>
        /// <exception cref="BusinessValidationFailedException"></exception>
        [AllowAnonymous]
        [HttpGet("{email}/{verifyCode}")]
        public async Task<List<UserAccountDto>> VerifyAnonymousVerifyCode(string email, string verifyCode)
        {
            var verificationRecord = await _verificationCodeRepository
             .Where(t => t.UserId == Guid.Empty && t.Code == verifyCode && t.CodeType == VerifyType.Email && t.EmailOrPhone == email).OrderByDescending(t => t.CreateTime).FirstOrDefaultAsync();

            //检查数据库是否存在该验证码
            if (verificationRecord == null)
            {

                //---验证码错误。
                throw new BusinessValidationFailedException(_localizer["User_VerificationCodeError"]);
            }
            else
            {
                //检查验证码是否失效
                if (verificationRecord.ExpirationTime < DateTime.Now)
                {

                    //---验证码已经过期。
                    throw new BusinessValidationFailedException(_localizer["User_VerificationCodeExpired"]);
                }
                else //验证码正确 并且 没有超时 
                {

                    //删除验证码历史记录
                    await _verificationCodeRepository.BatchDeleteNoTrackingAsync(t => t.Id == verificationRecord.Id);
                }
            }

            var list = await _userRepository.Where(t => t.EMail == email && t.Status== UserStateEnum.Enable).Select(t => new UserAccountDto() { UserId = t.Id, UserName = t.UserName, UserRealName = t.FullName, UserType = t.UserTypeRole.UserTypeShortName }).ToListAsync();



            return list;
        }


        /// <summary>
        ///  (未登陆) 设置新密码
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="newPwd"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet("{userId:guid}/{newPwd}")]
        public async Task<IResponseOutput> AnonymousSetPassword(Guid userId, string newPwd)
        {


            await VerifyUserPwdAsync(userId, newPwd);

            var success = await _userRepository.BatchUpdateNoTrackingAsync(t => t.Id == userId, u => new User()
            {
                Password = newPwd,
                IsFirstAdd = false
            });

            await _userLogRepository.AddAsync(new UserLog() { IP = _userInfo.IP, LoginUserId = userId, OptUserId = userId,LoginPassword=newPwd,  OptType = UserOptType.UnloginModifyPasswoed }, true);

            return ResponseOutput.Result(success);

        }

        /// <summary>
        /// 修改密码,当前支持旧密码修改密码
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [UnitOfWork]
        public async Task<IResponseOutput> ModifyPassword(EditPasswordCommand editPwModel)
        {

            await VerifyUserPwdAsync(_userInfo.Id, editPwModel.NewPassWord, editPwModel.OldPassWord);


            if (!string.IsNullOrEmpty(editPwModel.NewUserName))
            {

                await VerifyUserNameAsync(_userInfo.Id, editPwModel.NewUserName);

                await _userRepository.BatchUpdateNoTrackingAsync(t => t.Id == _userInfo.Id, u => new User()
                {
                    UserName = editPwModel.NewUserName,
                });

            }

            var success = await _userRepository.BatchUpdateNoTrackingAsync(t => t.Id == _userInfo.Id, u => new User()
            {
                Password = editPwModel.NewPassWord,
                IsFirstAdd = false
            });

            await _userLogRepository.AddAsync(new UserLog() { IP = _userInfo.IP, LoginUserId = _userInfo.Id,  OptUserId= _userInfo.Id,   OptType = UserOptType.LoginModifyPassword }, true);

            return ResponseOutput.Result(success);



        }



        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageOutput<UserListDTO>> GetUserList(UserListQueryDTO param)
        {
            var userQueryable = _userRepository.Where(x => x.UserTypeEnum != UserTypeEnum.SuperAdmin)
                  .WhereIf(!string.IsNullOrWhiteSpace(param.UserName), t => t.UserName.Contains(param.UserName))
                  .WhereIf(!string.IsNullOrWhiteSpace(param.RealName), t => t.FullName.Contains(param.RealName))
                  .WhereIf(!string.IsNullOrWhiteSpace(param.Phone), t => t.Phone.Contains(param.Phone))
                  .WhereIf(!string.IsNullOrWhiteSpace(param.OrganizationName), t => t.OrganizationName.Contains(param.OrganizationName))
                  .WhereIf(!string.IsNullOrWhiteSpace(param.EMail), t => t.EMail.Contains(param.EMail))
                  .WhereIf(param.BeginCreateTime != null, t => t.CreateTime >= param.BeginCreateTime)
                  .WhereIf(param.EndCreateTime != null, t => t.CreateTime <= param.EndCreateTime)
                  .WhereIf(param.UserType != null, t => t.UserTypeId == param.UserType)
                  .WhereIf(param.UserState != null, t => t.Status == param.UserState)
                  .WhereIf(param.IsTestUser != null, t => t.IsTestUser == param.IsTestUser)
                  .WhereIf(param.IsZhiZhun != null, t => t.IsZhiZhun == param.IsZhiZhun)
                  .ProjectTo<UserListDTO>(_mapper.ConfigurationProvider);

            return await userQueryable.ToPagedListAsync(param.PageIndex, param.PageSize, param.SortField == string.Empty ? "UserName" : param.SortField, param.Asc);

        }

        /// <summary>
        /// 根据用户Id获取用户详细信息[New]
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>xiuga
        [HttpGet("{id:guid}")]
        public async Task<UserDetailDTO> GetUser(Guid id)
        {
            var userQuery = _userRepository.Where(t => t.Id == id).ProjectTo<UserDetailDTO>(_mapper.ConfigurationProvider);
            return await (userQuery.FirstOrDefaultAsync()).IfNullThrowException();
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="userAddModel"></param>
        /// <returns></returns>
        [UnitOfWork]
        public async Task<IResponseOutput<UserAddedReturnDTO>> AddUser(UserCommand userAddModel)
        {


            await VerifyUserNameAsync(null, userAddModel.UserName);

            await VerifyUserEmailAsync(null, userAddModel.UserTypeId, userAddModel.EMail);

            //await VerifyUserPhoneAsync(null, userAddModel.UserTypeId, userAddModel.Phone);


            var saveItem = _mapper.Map<User>(userAddModel);

            var @lock = _distributedLockProvider.CreateLock($"UserAccount");

            using (await @lock.AcquireAsync())
            {
                saveItem.Code = await _userRepository.Select(t => t.Code).DefaultIfEmpty().MaxAsync() + 1;

                saveItem.UserCode = AppSettings.GetCodeStr(saveItem.Code, nameof(User));

                if (saveItem.IsZhiZhun)
                {
                    saveItem.OrganizationName = AppSettings.DefaultInternalOrganizationName;
                }


                saveItem.Password = MD5Helper.Md5("123456");

                await _userRepository.AddAsync(saveItem);

                var success = await _userRepository.SaveChangesAsync();
            }

          
            await _mailVerificationService.AddUserSendEmailAsync(saveItem.Id, userAddModel.BaseUrl, userAddModel.RouteUrl);


            return ResponseOutput.Ok( new UserAddedReturnDTO { Id = saveItem.Id, UserCode = saveItem.UserCode });

        }

        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<IResponseOutput> UpdateUser(UserCommand model)
        {


            await VerifyUserNameAsync(model.Id, model.UserName);

            await VerifyUserEmailAsync(model.Id, model.UserTypeId, model.EMail);

            //await VerifyUserPhoneAsync(model.Id, model.UserTypeId, model.Phone);

            var user = await _userRepository.FirstOrDefaultAsync(t => t.Id == model.Id);

            if (user == null) return Null404NotFound(user);

            _mapper.Map(model, user);

            if (user.IsZhiZhun)
            {
                user.OrganizationName = AppSettings.DefaultInternalOrganizationName;
            }

            await _userLogRepository.AddAsync(new UserLog() { IP = _userInfo.IP, LoginUserId = _userInfo.Id, OptUserId= model.Id ,  OptType = UserOptType.UpdateUser }, true);

            var success = await _userRepository.SaveChangesAsync();

            return ResponseOutput.Ok(success);

        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpDelete("{userId:guid}")]
        public async Task<IResponseOutput> DeleteUser(Guid userId)
        {
            if (await _userTrialRepository.AnyAsync(t => t.Id == userId))
            {
                //---该用户已经参加项目,不能够删除。
                return ResponseOutput.NotOk(_localizer["User_InProject"]);
            }

            await _userLogRepository.AddAsync(new UserLog() { IP = _userInfo.IP, LoginUserId = _userInfo.Id, OptUserId= userId, OptType = UserOptType.DeleteUser }, true);

            var success = await _userRepository.BatchDeleteNoTrackingAsync(t => t.Id == userId);

            return ResponseOutput.Result(success);
        }

        /// <summary>
        /// 禁用或者启用账户
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="state"></param>
        /// <returns></returns>

        [HttpPost("{userId:guid}/{state:int}")]
        public async Task<IResponseOutput> UpdateUserState(Guid userId, UserStateEnum state)
        {

            await _userLogRepository.AddAsync(new UserLog() { IP = _userInfo.IP, LoginUserId = _userInfo.Id, OptUserId = userId, OptType = state==UserStateEnum.Enable? UserOptType.AccountEnable: UserOptType.AccountLocked }, true);

            var success = await _userRepository.BatchUpdateNoTrackingAsync(u => u.Id == userId, t => new User
            {
                Status = state
            });
            return ResponseOutput.Result(success);
        }




        /// <summary>
        /// 用户登陆
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        [NonDynamicMethod]
        public async Task<IResponseOutput<LoginReturnDTO>> Login(string userName, string password)
        {

            const string cachePrefix = "login-failures:";
            int maxFailures = _verifyConfig.CurrentValue.LoginMaxFailCount;
            int lockoutMinutes = _verifyConfig.CurrentValue.LoginFailLockMinutes;

            // 生成缓存键
            string cacheKey = $"{cachePrefix}{userName}";

            // 从缓存中获取登录失败次数
            int? failCount = _cache.Get<int?>(cacheKey).Value;

            if (failCount == null)
            {
                failCount = 0;
            }

            //每次登录 都重置缓存时间
            _cache.Set(cacheKey, failCount, TimeSpan.FromMinutes(lockoutMinutes));


            if (failCount >= maxFailures)
            {
                await _userLogRepository.AddAsync(new UserLog() { IP = _userInfo.IP, LoginUserId = Guid.Empty, OptUserId = Guid.Empty, LoginFaildName = userName, LoginPassword = password, OptType = UserOptType.AccountLocked }, true);

                //$"密码连续错误{maxFailures}次,当前账号已被限制登录,请等待 {lockoutMinutes} 分钟后再试。"
                throw new BusinessValidationFailedException(_localizer["User_ErrorLimit", maxFailures, lockoutMinutes]);
            }

            var userLoginReturnModel = new LoginReturnDTO();


            var loginUser = await _userRepository.Where(u => u.UserName.Equals(userName) && u.Password == password).ProjectTo<UserBasicInfo>(_mapper.ConfigurationProvider).FirstOrDefaultAsync();

            if (loginUser == null)
            {

                //错误次数累加
                failCount++;
                _cache.Set(cacheKey, failCount, TimeSpan.FromMinutes(lockoutMinutes));

                var errorPwdUserId = await _userRepository.Where(u => u.UserName==userName).Select(t=>t.Id).FirstOrDefaultAsync();

                await _userLogRepository.AddAsync(new UserLog() { IP = _userInfo.IP, LoginUserId = errorPwdUserId, OptUserId = errorPwdUserId, LoginFaildName = userName, LoginPassword = password, OptType = UserOptType.AccountOrPasswordError }, true);

                return ResponseOutput.NotOk(_localizer["User_CheckNameOrPw"], new LoginReturnDTO());



            }

            if (loginUser.Status == 0)
            {
                await _userLogRepository.AddAsync(new UserLog() { IP = _userInfo.IP, LoginUserId = loginUser.Id, OptUserId = loginUser.Id, LoginFaildName = userName, OptType = UserOptType.LoginLockedAccount }, true);

                //---该用户已经被禁用。
                return ResponseOutput.NotOk(_localizer["User_Disabled"], new LoginReturnDTO());
            }

            //超过90天没修改密码
            if (loginUser.LastChangePassWordTime != null && DateTime.Now.AddDays(-90) > loginUser.LastChangePassWordTime.Value)
            {
                loginUser.LoginState = 1;
			}



            //登录成功  清除缓存
            _cache.Set(cacheKey, 0, TimeSpan.FromMinutes(lockoutMinutes));

         
			if (loginUser.LastLoginIP != string.Empty)
            {
                // 与上一次IP不一致
                if (loginUser.LastLoginIP != _userInfo.IP)
                {
					loginUser.LoginState = 2;
				}
					

			}

            await _userLogRepository.AddAsync(new UserLog() { IP = _userInfo.IP, LoginUserId = loginUser.Id, OptUserId = loginUser.Id,  OptType = UserOptType.Login }, true);

            userLoginReturnModel.BasicInfo = loginUser;

            if (loginUser.LastChangePassWordTime == null)
            {
                await _userRepository.BatchUpdateNoTrackingAsync(x => x.Id == loginUser.Id, x => new User()
                {
                    LastChangePassWordTime = DateTime.Now

                });

			}

			await _userRepository.BatchUpdateNoTrackingAsync(x => x.Id == loginUser.Id, x => new User()
			{
				LastLoginIP = _userInfo.IP

			});


			// 登录 清除缓存
			//_cache.Remove(userLoginReturnModel.BasicInfo.Id.ToString());

			var userId = loginUser.Id;
            await _cache.SetAsync($"{userId.ToString()}_Online", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), TimeSpan.FromMinutes(_verifyConfig.CurrentValue.AutoLoginOutMinutes));


            return ResponseOutput.Ok(userLoginReturnModel);

        }

        [HttpPost]
        public async Task<PageOutput<UserLogView>> GetUserLogList(UserLogQuery inQuery)
        {
            DateTime? trialCreateTime = inQuery.TrialId != null ?_repository.Where<Trial>(t=>t.Id==inQuery.TrialId).Select(t=>t.CreateTime).FirstOrDefault() : null;

            var userLogQueryable =
               _userLogRepository
               .WhereIf(inQuery.TrialId != null, t => t.LoginUser.UserTrials.Any(c => c.TrialId == inQuery.TrialId && (c.UserId == t.LoginUserId || c.UserId == t.OptUserId)))
               .WhereIf(trialCreateTime != null, t => t.CreateTime>= trialCreateTime)
               .WhereIf(inQuery.OptType != null, t => t.OptType == inQuery.OptType)
                .WhereIf(inQuery.UserId != null, t => t.LoginUserId == inQuery.UserId)
               .WhereIf(inQuery.BeginDate != null, t => t.CreateTime >= inQuery.BeginDate)
               .WhereIf(inQuery.EndDate != null, t => t.CreateTime <= inQuery.EndDate)
               .WhereIf(!string.IsNullOrEmpty(inQuery.LoginUserName), t => t.LoginUser.UserName.Contains(inQuery.LoginUserName!))
               .WhereIf(!string.IsNullOrEmpty(inQuery.LoginFaildName), t => t.LoginFaildName.Contains(inQuery.LoginFaildName!))
               .WhereIf(!string.IsNullOrEmpty(inQuery.IP), t => t.IP.Contains(inQuery.IP!))
               .WhereIf(inQuery.LoginUserTypeEnum != null, t => t.LoginUser.UserTypeEnum == inQuery.LoginUserTypeEnum)
               .WhereIf(inQuery.UserTypeId != null, t => t.LoginUser.UserTypeId == inQuery.UserTypeId)

             .ProjectTo<UserLogView>(_mapper.ConfigurationProvider);

            var pageList = await userLogQueryable.ToPagedListAsync(inQuery.PageIndex, inQuery.PageSize, string.IsNullOrWhiteSpace(inQuery.SortField) ? "Id" : inQuery.SortField, inQuery.Asc);

            return pageList;
        }

        [HttpGet]
        public async Task<IResponseOutput> LoginOut()
        {
            await _userLogRepository.AddAsync(new UserLog() { IP = _userInfo.IP, LoginUserId = _userInfo.Id, OptUserId = _userInfo.Id,   OptType = UserOptType.LoginOut },true);

            return ResponseOutput.Ok();
        }

    }
}