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.Entity.Middle;
using GxPress.Repository.Interface;
using Microsoft.Extensions.Options;
using SqlKata;

namespace GxPress.Repository.Implement
{
    public class FolderUserRepository : IFolderUserRepository
    {
        private readonly Repository<FolderUser> _repository;
        private readonly IMapper _mapper;
        private readonly string _connectionString;
        private readonly string _databaseTypeStr;
        public FolderUserRepository(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<FolderUser>(database);
            _mapper = mapper;
        }

        public IDatabase Database => _repository.Database;
        public string TableName => _repository.TableName;
        public List<TableColumn> TableColumns => _repository.TableColumns;
        public async Task<int> InsertAsync(FolderUser folderUser)
        {
            return await _repository.InsertAsync(folderUser);
        }
        /// <summary>
        /// 插入文件夹共享用户
        /// </summary>
        /// <param name="folderUsers"></param>
        /// <returns></returns>
        public async Task<bool> InsertsAsync(List<FolderUser> folderUsers)
        {
            if (folderUsers.Count == 0)
                return false;
            string sql = $@"INSERT INTO `tede_folder_user`(`Guid`,`CreatedDate`,`LastModifiedDate`,`MiddleId`,`UserId`,`UserName`) VALUES";
            foreach (var item in folderUsers)
            {
                var guid = Guid.NewGuid().ToString();
                var createdDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                sql += $"('{guid}','{createdDate}','{createdDate}',{item.MiddleId},{item.UserId},'{item.UserName}'),";
            }
            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 result = await connection.ExecuteAsync(sql);
            return result > 0;
        }

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

        public async Task<IEnumerable<FolderUser>> GetAllAsync(int middleId)
        {
            return await _repository.GetAllAsync(Q.Where(nameof(FolderUser.MiddleId), middleId));
        }

        public async Task<IEnumerable<FolderUser>> GetAllAsync(Query query)
        {
            return await _repository.GetAllAsync(query);
        }
    }
}
