using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper;
using Dapper;
using GxPress.Common.AppOptions;
using GxPress.Common.Tools;
using GxPress.Entity.Topic;
using GxPress.Repository.Interface.Job;
using GxPress.Result.Job;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Options;
using GxPress.Entity;
using Datory;

namespace GxPress.Repository.Implement.Job
{
    public class JobRepository : IJobRepository
    {
        private readonly Repository<Entity.Missive.Missive> _repository;
        private readonly IMapper _mapper;
        private readonly IDistributedCache _cache;
        private readonly string _connectionString;
        private readonly string _databaseTypeStr;
        public JobRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper, IDistributedCache cache)
        {
            _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
            _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
            var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
            var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
            _mapper = mapper;
            _cache = cache;
        }

        public IDatabase Database => _repository.Database;
        public string TableName => _repository.TableName;
        public List<TableColumn> TableColumns => _repository.TableColumns;

        /// <summary>
        /// 执行话题
        /// </summary>
        /// <returns></returns>
        public async Task<IEnumerable<JobTopicResult>> ExecuteTopic()
        {
            string sql = "select a.Id,a.Title,a.Content,b.UserId,c.Name,a.CreatedDate,d.AvatarUrl from tede_topic a inner join  tede_topic_addressee b on a.Id=b.TopicId inner join tede_user c on c.Id=b.UserId inner join tede_user d on d.Id=a.UserId where b.IsUpload=0 order by CreatedDate desc";
            var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
            var database = new Database(databaseType, _connectionString);
            var connection = database.GetConnection();
            var result = await connection.QueryAsync<JobTopicResult, Entity.Topic.Topic, TopicAddressee, User, User, JobTopicResult>(sql, (jobTopicResult, topic, topicAddressee, user, userDto) =>
              {
                  jobTopicResult.Id = topic.Id;
                  jobTopicResult.Title = topic.Title;
                  jobTopicResult.Content = topic.Content;
                  jobTopicResult.UserId = topicAddressee.UserId;
                  jobTopicResult.Name = user.Name;
                  jobTopicResult.CreatedDate = topic.CreatedDate;
                  jobTopicResult.AvatarUrl = userDto.AvatarUrl;
                  jobTopicResult.TopicAddresseeId = topicAddressee.Id;
                  return jobTopicResult;
              }, splitOn: "Id,UserId,Name,AvatarUrl");
            return result;
        }
    }
}