using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper;
using GxPress.Common.AppOptions;
using GxPress.Common.Tools;
using GxPress.Entity.Topic;
using GxPress.Repository.Interface;
using Microsoft.Extensions.Options;
using Datory;
using SqlKata;
using Dapper;
using System;
using System.Linq;

namespace GxPress.Repository.Implement
{
    public class TopicAddresseeRepository : ITopicAddresseeRepository
    {
        private readonly Repository<TopicAddressee> _repository;
        private readonly IMapper _mapper;
        private readonly string _connectionString;
        private readonly string _databaseTypeStr;
        public TopicAddresseeRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
        {
            _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);
            _repository = new Repository<TopicAddressee>(database);
            _mapper = mapper;
        }

        public IDatabase Database => _repository.Database;
        public string TableName => _repository.TableName;
        public List<TableColumn> TableColumns => _repository.TableColumns;
        public async Task<int> InsertAsync(TopicAddressee request)
        {
            return await _repository.InsertAsync(request);
        }

        /// <summary>
        /// 添加话题收件人
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<bool> InsertAsync(IEnumerable<TopicAddressee> request)
        {
            var sql = "INSERT INTO `tede_topic_addressee`(`Guid`,`CreatedDate`,`LastModifiedDate`,`TopicId`,`UserId`,`UserName`,`IsRead`,`IsAdmin`,`IsUpload`)VALUES";
            if (request.Count() == 0)
                return true;
            foreach (var item in request)
            {
                var isAdmin = item.IsAdmin ? 1 : 0;
                sql += $"('{Guid.NewGuid().ToString()}','{DateTime.Now.ToString()}','{DateTime.Now.ToString()}',{item.TopicId},{item.UserId},'',0,{isAdmin},0),";
            }
            sql = sql.Remove(sql.Length - 1, 1);
            var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
            var database = new Database(databaseType, _connectionString);
            var connection = database.GetConnection();
            return await connection.ExecuteScalarAsync<int>(sql) > 0;
        }
        public async Task<IEnumerable<TopicAddressee>> GetAllAsync(Query query)
        {
            return await _repository.GetAllAsync(query);
        }

        public async Task<bool> DeleteAsync(Query query)
        {
            return await _repository.DeleteAsync(query) > 0;
        }
        /// <summary>
        /// 根据topicId删除
        /// </summary>
        /// <param name="topicId"></param>
        /// <returns></returns>
        public async Task<bool> DeletByTopicIdAsync(int topicId)
        {
            return await _repository.DeleteAsync(Q.Where(nameof(Entity.Topic.TopicAddressee.TopicId), topicId)) > 0;
        }

        public async Task<bool> UpdateAsync(Query query)
        {
            return await _repository.UpdateAsync(query) > 0;
        }
    }
}
