using MadRunFabric.Common;
using MadRunFabric.Common.Options;
using MadRunFabric.Model;
using MadRunFabric.Model.TestUserTypeApi;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using TestUserTypeApi.IRepositories;
using System.Linq;

namespace TestUserTypeApi.Repositories
{
    public class TestusertypeRepository:BaseRepository<TestPager, string>, ItestusertypeRepository
    {
        protected readonly ILogger<BaseRepository<TestPager, string>> _logger;
        protected readonly IMongoCollection<TestPager> _collection_TestPager;
        protected readonly IMongoCollection<TestTypes> _collection_TestTypes;

        public TestusertypeRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<TestPager, string>> logger) : base(settings, logger)
        {
            _collection_TestTypes= _context.GetCollection<TestTypes>();
            _collection_TestPager = _context.GetCollection<TestPager>();
            _logger = logger;
        }
        /// <summary>
        ///获取数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IEnumerable<object> GetUserInfo(int pageIndex, int pageSize)
        {
            
               var query = from stud in _collection_TestPager.AsQueryable()
                         select new
                         {
                              stud.id, //编号
                              stud.testtitle,//题目
                              stud.testoptions,//选项
                              stud.standardkey,//参考答案
                              stud.testtype,//试卷类型
                              stud.createdatatime,//创建时间
                              stud.testcode,//序号
                         };
            var list = query.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            return list;
        }
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="tp"></param>
        /// <returns></returns>
        public int AddTestpager(TestPager tp)
        {
            _collection_TestPager.InsertOne(tp);
            return 1;
        }
        /// <summary>
        /// 根据id获取试卷类型
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public object GetUsertesttype(string id)
        {
            var query = from stud in _collection_TestPager.AsQueryable() where stud.id == id
                        select new
                        {
                            stud.id, //编号
                            stud.testtitle,//题目
                            stud.testoptions,//选项
                            stud.standardkey,//参考答案
                            stud.testtype,//试卷类型
                            stud.createdatatime,//创建时间
                            stud.testcode,//序号
                        };
            var model = query.FirstOrDefault();
            if (model != null)
            {
                return model;
            }
            return query;
        }
    }
}
