using System.Collections.Generic;
using System.Threading.Tasks;
using GxPress.Common.AppOptions;
using GxPress.Common.Tools;
using GxPress.Entity;
using GxPress.Entity.Article;
using GxPress.Repository.Interface;
using GxPress.Request.BlacklistArticle;
using Microsoft.Extensions.Options;
using Datory;
using SqlKata;
namespace GxPress.Repository.Implement
{
    public class BlacklistArticleRepository : IBlacklistArticleRepository
    {
        private readonly Repository<BlacklistArticle> _repository;
        private readonly Repository<User> _userRepository;
        private readonly Repository<Article> _articleRepository;

        public BlacklistArticleRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor)
        {
            var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
            var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
            _repository = new Repository<BlacklistArticle>(database);
            _userRepository = new Repository<User>(database);
            _articleRepository = new Repository<Article>(database);
        }

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



        /// <summary>
        /// 查询用户屏蔽文章类型值
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<IEnumerable<BlacklistArticle>> GetListAsync(BlacklistArticleSearchRequest request)
        {
            return await _repository.GetAllAsync(Q.Where(nameof(BlacklistArticle.UserId), request.UserId));
        }

        public async Task<bool> InsertAsync(BlacklistArticle blacklistArticle)
        {
            return await _repository.InsertAsync(blacklistArticle) > 0;
        }

        public async Task<BlacklistArticle> GetAsync(Query query)
        {
            return await _repository.GetAsync(query);
        }

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

}