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

namespace GxPress.Repository.Implement.Visit
{
    public class VisitRepository : IVisitRepository
    {
        private readonly Repository<Entity.Visit.Visit> _repository;
        private readonly IMapper _mapper;
        private readonly string _connectionString;
        private readonly string _databaseTypestr;
        public VisitRepository(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.Visit.Visit>(database);
            _mapper = mapper;
        }
        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<PagedList<VisitResult>> GetVisitResultAsync(VisitPageSearchRequest request)
        {
            var resultData = new PagedList<VisitResult>();
            string sql = $"SELECT a.*,b.Name,b.AvatarUrl,c.Name FROM ccpph.tede_visit a inner join ccpph.tede_user b on a.UserId=b.Id inner join ccpph.tede_department c on c.Id=b.DepartmentId where a.TypeId={request.TypeId} and a.SourceId={request.SouceId} order by a. CreatedDate desc limit {(request.Page - 1) * request.PerPage},{request.PerPage}";
            var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypestr, DatabaseType.MySql);
            var database = new Database(databaseType, _connectionString);
            var connection = database.GetConnection();
            var result =
            await connection
                    .QueryAsync<VisitResult, User, Department, VisitResult>(sql,
                        (visitResult, user, department) =>
                        {
                            visitResult.DepartmentName = department.Name;
                            visitResult.Name = user.Name;
                            visitResult.AvatarUrl = StringUtils.AddDomainMin(user.AvatarUrl);
                            return visitResult;
                        }, splitOn: "Name");
            var sqlCount = $"SELECT count(1) FROM ccpph.tede_visit a inner join ccpph.tede_user b on a.UserId=b.Id inner join ccpph.tede_department c on c.Id=b.DepartmentId where a.TypeId={request.TypeId} and a.SourceId={request.SouceId}";
            resultData.Items = result;
            resultData.Total = await connection.ExecuteScalarAsync<int>(sqlCount);
            return resultData;
        }
        /// <summary>
        /// 添加访客
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public Task<int> InsertAsync(Entity.Visit.Visit request)
        {

            return _repository.InsertAsync(request);
        }

        public Task<bool> ExistsAsync(SqlKata.Query query)
        {
            return _repository.ExistsAsync(query);
        }

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

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