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.ArticleLabel;
using GxPress.Result.App.ArticleLabel;
using Microsoft.Extensions.Options;
using SqlKata;

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

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

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

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

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

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

        public async Task<int> InsertAsync(Entity.ArticleLabel.ArticleLabel articleLabel)
        {
            return await _repository.InsertAsync(articleLabel);
        }

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

        public async Task<bool> UpdateAsync(Query query)
        {
            return await _repository.UpdateAsync(query) > 0;
        }
        /// <summary>
        /// 根据文章ID获取文章标签
        /// </summary>
        /// <param name="articleId"></param>
        /// <returns></returns>
        public async Task<IEnumerable<ArticleInnerLabelResult>> GetArticleLabelsAsync(int articleId)
        {
            var sql = $"select a.*,b.LabelName from ccpph.tede_article_inner_label a inner join ccpph.tede_article_label b  on a.ArticleLabelId=b.Id where a.ArticleId={articleId}";
            var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
            var database = new Database(databaseType, _connectionString);
            var connection = database.GetConnection();
            var items = await connection
                .QueryAsync<ArticleInnerLabelResult, Entity.ArticleLabel.ArticleLabel, ArticleInnerLabelResult>(sql,
                    (articleInnerLabelResult, articleLabel) =>
                    {
                        articleInnerLabelResult.LabelName = articleLabel != null ? articleLabel.LabelName : "";
                        return articleInnerLabelResult;
                    },
                    splitOn: "LabelName");
            return items;
        }
    }
}