using MadRunFabric.Common;
using MadRunFabric.Common.Options;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
using SignTokenApi.IRepositories;
using MadRunFabric.Model;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace SignTokenApi.Repositories
{
    public class Sys_Login_LogsRepository : BaseRepository<Sys_Login_Logs, string>, ISys_Login_LogsRepository
    {
        protected readonly IMongoCollection<Sys_Login_Logs> m_collection;
        protected readonly ILogger<BaseRepository<Sys_Login_Logs, string>> _logger;
        public Sys_Login_LogsRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<Sys_Login_Logs, string>> logger) : base(settings, logger)
        {
            m_collection = _collection;
            _logger = logger;
        }


        public IEnumerable<object> GetLogs()
        {
            try
            {
                //可限制返回的文档中的字段或者重命名其中的字段
                var project = new BsonDocument{
                                { "_id",0},
                                { "username",1},
                                { "usercode",1}
                };
                return BsonSerializer.Deserialize<IEnumerable<object>>(_collection.Aggregate().Lookup("sys_user_account", "usercode", "usercode", "result").Project(project).ToList().ToJson());

            }
            catch (Exception ex)
            {
                _logger.LogError(ex.ToString());
                throw ex;
            }
        }

    
    }
}
