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

namespace GxPress.Repository.Implement.MeetingSummaryTodo
{
    public class MeetingSummaryTodoRepository : IMeetingSummaryTodoRepository
    {
        private readonly Repository<Entity.WorkMeeting.MeetingSummaryTodo> _repository;
        private readonly IMapper _mapper;
        private readonly IDistributedCache _cache;
        private readonly string _connectionString;
        private readonly string _databaseTypeStr;
        public MeetingSummaryTodoRepository(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);
            _repository = new Repository<Entity.WorkMeeting.MeetingSummaryTodo>(database);
            _mapper = mapper;
            _cache = cache;
        }

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

        public async Task<Entity.WorkMeeting.MeetingSummaryTodo> GetAsync(int id)
        {
            return await _repository.GetAsync(id);
        }

        public async Task<Entity.WorkMeeting.MeetingSummaryTodo> GetAsync(SqlKata.Query query)
        {
            return await _repository.GetAsync(query);
        }
        public async Task<IEnumerable<Entity.WorkMeeting.MeetingSummaryTodo>> GetAllAsync(SqlKata.Query query)
        {
            return await _repository.GetAllAsync(query);
        }
        public async Task<bool> DeleteAsync(SqlKata.Query query)
        {
            return await _repository.DeleteAsync(query) > 0;
        }

        public async Task<int> InsertAsync(Entity.WorkMeeting.MeetingSummaryTodo flowAttachment)
        {
            return await _repository.InsertAsync(flowAttachment);
        }

        public async Task<bool> UpdateAsync(Entity.WorkMeeting.MeetingSummaryTodo flowAttachment)
        {
            return await _repository.UpdateAsync(flowAttachment);
        }
        public async Task<bool> UpdateAsync(SqlKata.Query query)
        {
            return await _repository.UpdateAsync(query) > 0;
        }
        /// <summary>
        /// 添加工作流附件
        /// </summary>
        /// <param name="flowAttachments"></param>
        /// <returns></returns>
        public async Task<bool> InsertsAsync(List<Entity.WorkMeeting.MeetingSummaryTodo> flowAttachments)
        {
            if (flowAttachments.Count == 0)
                return false;
            var sql = "INSERT INTO `tede_meeting_summary_todo`(`Guid`,`CreatedDate`,`LastModifiedDate`,`MeetingSummaryId`,`UserId`,`IsAdmin`,`TypeId`)VALUES";
            foreach (var item in flowAttachments)
            {
                var guId = Guid.NewGuid().ToString();
                var createdDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                var IsAdmin = item.IsAdmin ? 1 : 0;
                sql += $"('{guId}','{createdDate}','{createdDate}',{item.MeetingSummaryId},{item.UserId},{IsAdmin},{item.TypeId}),";
            }
            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();
            var items = await connection.ExecuteScalarAsync<int>(sql);
            return items > 0;
        }
    }
}