using DataSharing.Share.Dtos.Common;
using MapsterMapper;
using Microsoft.Extensions.Logging;
using XF.Domain.Dependency;
using XF.Domain.Repository;

namespace DataSharing.RawData
{
    public class DsUserTokenInfoService : IDsUserTokenInfoService, IScopeDependency
    {
        private readonly IMapper _mapper;
        private readonly ILogger<DsUserTokenInfoService> _logger;
        private readonly IRepository<DsUserTokenInfo> _dsUserTokenInfoRepository;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="logger"></param>
        /// <param name="dsUserTokenInfoRepository"></param>
        public DsUserTokenInfoService(IMapper mapper,
            ILogger<DsUserTokenInfoService> logger,
            IRepository<DsUserTokenInfo> dsUserTokenInfoRepository)
        {
            _mapper = mapper;
            _logger = logger;
            _dsUserTokenInfoRepository = dsUserTokenInfoRepository;
        }

        /// <summary>
        /// 验证密钥
        /// </summary>
        /// <param name="Code"></param>
        /// <returns></returns>
        public async Task<CheckAccountDto> CheckAccountAsync(string Code, CancellationToken cancellationToken)
        {
            CheckAccountDto accountDto = new() { IsCheckAccount = false };
            var data = await _dsUserTokenInfoRepository.GetAsync(p => p.AccountId == Code && p.State == "1", cancellationToken);
            if (data is not null)
            {
                accountDto.PlatformSource = data.PlatformSource;
                accountDto.IsCheckAccount = true;
                accountDto.PlatformSourceName=data.PlatformSourceName;
                return accountDto;
            }

            accountDto.Message = "授权账号或密码验证不通过";
            return accountDto;
        }
    }
}
