using System;
using System.Threading.Tasks;
using Dapper;
using Datory;
using GxPress.Common.Tools;
using GxPress.Result.DataCenter;

namespace GxPress.Service.Implement.PlatformData
{
    public partial class PlatformDataService
    {
        /// <summary>
        ///  今日访问数据(次)
        /// </summary>
        /// <returns></returns>
        public async Task<PlatformAccumulativeVisitResult> GetPlatformTodayVisitResult()
        {
            var nowTime = DateTime.Now.ToString("yyyy-MM-dd");
            var sql = $@"SELECT 
                                (SELECT 
                                        COUNT(1)
                                    FROM
                                        tede_visit
                                    WHERE
                                        CreatedDate > '{nowTime}') AS VisitCount,
                                (SELECT 
                                        COUNT(1)
                                    FROM
                                        tede_analyze
                                    WHERE
                                        AnalyzeType = 3
                                            AND CreatedDate > '{nowTime}') AS RetransmissionCount,
                                (SELECT 
                                        COUNT(1)
                                    FROM
                                        tede_analyze
                                    WHERE
                                        AnalyzeType = 4
                                            AND CreatedDate > '{nowTime}') AS CollctionCount,
                                (SELECT 
                                        COUNT(1)
                                    FROM
                                        tede_comment
                                    WHERE
                                        CreatedDate > '{nowTime}') AS CommentCount";
            var connectionString = ConfigHelper.GetValue("Database:ConnectionString");
            var database = new Database(DatabaseType.MySql, connectionString);
            var connection = database.GetConnection();
            return await connection.QueryFirstAsync<PlatformAccumulativeVisitResult>(sql);
        }
    }
}