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

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

        public IDatabase Database => _repository.Database;
        public string TableName => _repository.TableName;
        public List<TableColumn> TableColumns => _repository.TableColumns;
        /// <summary>
        /// 根据ID和类型获取版本号
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<AppVersion> GetAsync(int id)
        {
            return await _repository.GetAsync(id);
        }

        /// <summary>
        /// 根据APP类型获取最新版本号
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<AppVersion> GetAppVersionAsync(AppVersionSearchRequest request)
        {
            var appVersion = await _repository.GetAsync(Q.Where(nameof(AppVersion.VersionType), request.VersionType)
                .OrderByDesc(nameof(AppVersion.CreatedDate)));
            if (appVersion == null)
                throw new BusinessException("暂无数据");
            return appVersion;
        }

        public async Task<int> InsertAsync(AppVersion appVersion)
        {
            return await _repository.InsertAsync(appVersion);
        }
        /// <summary>
        /// 修改版本数据
        /// </summary>
        /// <param name="appVersion"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(AppVersion appVersion)
        {
            var appVersionEntity = _repository.GetAsync(appVersion.Id);
            if (appVersionEntity == null)
                throw new BusinessException("版本数据不存在");
            return await _repository.UpdateAsync(appVersion);
        }

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

        /// <summary>
        /// 获取版本号
        /// </summary>
        /// <returns></returns>
        public async Task<IEnumerable<AppVersion>> GetAppVersionAsync()
        {
            return await _repository.GetAllAsync();
        }
        /// <summary>
        /// 获取最新的移动端信息
        /// </summary>
        /// <returns></returns>
        public async Task<IEnumerable<AppVersion>> GetAppVersionsAsync()
        {
            string sql = "SELECT * FROM ccpph.tede_app_version where Id in(select max(id) from tede_app_version group by VersionType)";
            var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
            var database = new Database(databaseType, _connectionString);
            var connection = database.GetConnection();
            var result = await connection.QueryAsync<AppVersion>(sql);
            return result;
        }
    }
}
