using System.Collections.Generic;
using System.Threading.Tasks;
using Datory;
using GxPress.Common.AppOptions;
using GxPress.Common.Tools;
using GxPress.Entity;
using GxPress.Repository.Interface;
using Microsoft.Extensions.Options;

namespace GxPress.Repository.Implement
{
    public class UserLoginRepository : IUserLoginRepository
    {
        private readonly Repository<UserLogin> _repository;
        public UserLoginRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor)
        {
            var databaseType =
            StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
            //_connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
            var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
            _repository = new Repository<UserLogin>(database);
        }
        public IDatabase Database => _repository.Database;
        public string TableName => _repository.TableName;
        public List<TableColumn> TableColumns => _repository.TableColumns;
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<int> InsertAsync(UserLogin model)
        {
            return await _repository.InsertAsync(model);
        }
        public async Task<UserLogin> GetUserAsync(int userId)
        {
            return await _repository.GetAsync(Q.Where(nameof(UserLogin.UserId), userId).OrderByDesc(nameof(UserLogin.CreatedDate)));
        }
    }
}