using System.Collections.Generic;
using System.Threading.Tasks;
using GxPress.Common.AppOptions;
using GxPress.Common.Exceptions;
using GxPress.Common.Tools;
using GxPress.Entity;
using GxPress.Repository.Interface;
using GxPress.Request.ArticleGroup;
using Microsoft.Extensions.Options;
using Datory;
using Dapper;

namespace GxPress.Repository.Implement
{
    public class ArticleGroupRepository : IArticleGroupRepository
    {
        private readonly Repository<ArticleGroup> _repository;
        private readonly string _connectionString;
        private readonly string _databaseTypeStr;
        public ArticleGroupRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor)
        {
            _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<ArticleGroup>(database);
        }

        public IDatabase Database => _repository.Database;
        public string TableName => _repository.TableName;
        public List<TableColumn> TableColumns => _repository.TableColumns;
        public async Task<bool> ExistsAsync(int id)
        {
            return await _repository.ExistsAsync(id);
        }

        public async Task<bool> IsNameExistsAsync(string name)
        {
            return await _repository.ExistsAsync(Q.Where(nameof(ArticleGroup.Name), name));
        }

        public async Task<int> InsertAsync(ArticleGroup processGroup)
        {
            return await _repository.InsertAsync(processGroup);
        }

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

        public async Task<bool> UpdateAsync(ArticleGroup processGroup)
        {
            return await _repository.UpdateAsync(processGroup);
        }
        public async Task<IEnumerable<ArticleGroup>> GetListAsync(int userId)
        {
            string sql = $@"select * from ccpph.tede_article_group where Id not in(SELECT ArticleGroupId FROM ccpph.tede_article_group_user where UserId={userId})";
            var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
            var database = new Database(databaseType, _connectionString);
            var connection = database.GetConnection();
            var result = await connection.QueryAsync<ArticleGroup>(sql);
            return result;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<ArticleGroup> AddAsync(ArticleGroupAddRequest request)
        {
            var exist = await IsNameExistsAsync(request.Name);
            if (exist) throw new BusinessException("分组名不能重复");
            var group = new ArticleGroup
            {
                Name = request.Name,
                Sort = request.Sort
            };

            group.Id = await InsertAsync(group);
            return group;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(int id, ArticleGroupAddRequest request)
        {
            var group = await GetAsync(id);
            if (group == null) throw new BusinessException("该分组不存在");

            if (group.Name != request.Name)
            {
                var exist = await IsNameExistsAsync(request.Name);
                if (exist) throw new BusinessException("分组名不能重复");
            }

            group.Name = request.Name;
            group.Sort = request.Sort;

            return await UpdateAsync(group);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(int id)
        {
            var group = await GetAsync(id);
            if (group == null) throw new BusinessException("该文章组不存在");

            return await _repository.DeleteAsync(id);
        }
    }
}