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

namespace GxPress.Repository.Implement.Missive
{
    public class MissiveAnalyzeRepository : IMissiveAnalyzeRepository
    {
        private readonly Repository<MissiveAnalyze> _repository;
        private readonly IMapper _mapper;
        private readonly string _connectionString;
        private readonly string _databaseTypeStr;
        public MissiveAnalyzeRepository(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<MissiveAnalyze>(database);
            _mapper = mapper;
        }
        public IDatabase Database => _repository.Database;
        public string TableName => _repository.TableName;
        public List<TableColumn> TableColumns => _repository.TableColumns;
        public async Task<int> InsertAsync(MissiveAnalyze missiveAnalyze)
        {
            return await _repository.InsertAsync(missiveAnalyze);
        }

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

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

        public async Task<bool> UpdateAsync(MissiveAnalyze missiveAnalyze)
        {
            return await _repository.UpdateAsync(missiveAnalyze);
        }

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

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

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

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

        /// <summary>
        /// 设置 点赞 转发 收藏
        /// </summary>
        /// /// <param name="request"></param>
        /// <returns></returns>
        public async Task<bool> SetMissiveAnalyzeAsync(MissiveAnalyzeRequest request)
        {
            var topicAnalyze = await _repository.GetAsync(Q
                .Where(nameof(MissiveAnalyze.AnalyzeType), request.MissiveAnalyzeType)
                .Where(nameof(MissiveAnalyze.MissiveId), request.MissiveId)
                .Where(nameof(MissiveAnalyze.UserId), request.UserId));
            if (topicAnalyze != null && topicAnalyze.Id > 0)
                return await _repository.DeleteAsync(topicAnalyze.Id);
            var missiveAnalyze = new MissiveAnalyze
            {
                AnalyzeType = request.MissiveAnalyzeType,
                MissiveId = request.MissiveId,
                UserId = request.UserId
            };
            return await InsertAsync(missiveAnalyze) > 0;
        }
       
        public async Task<int> CountAsync(SqlKata.Query query)
        {
            return await _repository.CountAsync(query);
        }
    }
}
