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

namespace GxPress.Repository.Implement.UserMiddle
{
    /// <summary>
    /// 用户中间件
    /// </summary>
    public class UserMiddleRepository : IUserMiddleRepository
    {
        private readonly Repository<Entity.UserMiddle.UserMiddle> _repository;
        private readonly IMapper _mapper;
        public UserMiddleRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
        {
            var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
            var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
            _repository = new Repository<Entity.UserMiddle.UserMiddle>(database);
            _mapper = mapper;
        }
        public IDatabase Database => _repository.Database;
        public string TableName => _repository.TableName;
        public List<TableColumn> TableColumns => _repository.TableColumns;
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="userMiddle"></param>
        /// <returns></returns>
        public async Task<int> InsertAsync(Entity.UserMiddle.UserMiddle userMiddle)
        {
            return await _repository.InsertAsync(userMiddle);
        }

        public async Task<bool> UpdateAsync(Entity.UserMiddle.UserMiddle userMiddle)
        {
            return await _repository.UpdateAsync(userMiddle);
        }

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

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

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

        public async Task<int> CountAsync(SqlKata.Query query)
        {
            return await _repository.CountAsync(query);
        }
        public async Task<bool> ExistsAsync(SqlKata.Query query)
        {
            return await _repository.ExistsAsync(query);
        }

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