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

namespace GxPress.Service.Implement.PlatformData
{
    public partial class PlatformDataService
    {
        /// <summary>
        /// 内容数据
        /// </summary>
        /// <returns></returns>
        public async Task<List<PlatformContentDataInfoResult>> GetPlatformContentDataInfoResults()
        {
            var connectionString = ConfigHelper.GetValue("Database:ConnectionString");
            var database = new Database(DatabaseType.MySql, connectionString);
            var connection = database.GetConnection();
            var nowTime = DateTime.Now.ToString("yyyy-MM-dd");
            var sql = $@"SELECT 
                            (SELECT 
                                    COUNT(1)
                                FROM
                                    tede_media) AS SumContentCount,
                            (SELECT 
                                    COUNT(1)
                                FROM
                                    tede_media
                                WHERE
                                    CreatedDate > '{nowTime}') AS NewContentCount,
                            (SELECT 
                                    COUNT(1)
                                FROM
                                    tede_media
                                WHERE
                                IsChecked=1 and IsDelete=0) AS OnlineContentCount,
                                (SELECT 
                                    COUNT(1)
                                FROM
                                    tede_media
                                WHERE
                                FreeProportion>0) AS PayContentCount";
            var model = await connection.QueryFirstAsync<PlatformContentDataResult>(sql);
            var result = new List<PlatformContentDataInfoResult>();
            for (int i = 0; i < 4; i++)
            {
                if (i == 0)
                {
                    var dto = new PlatformContentDataInfoResult();
                    dto.Type = "内容总量(条)";
                    dto.Value = model.SumContentCount;
                    result.Add(dto);
                }
                if (i == 1)
                {
                    var dto = new PlatformContentDataInfoResult();
                    dto.Type = "今日新增内容量(条)";
                    dto.Value = model.NewContentCount;
                    result.Add(dto);
                }
                if (i == 2)
                {
                    var dto = new PlatformContentDataInfoResult();
                    dto.Type = "在线出版量(条)";
                    dto.Value = model.OnlineContentCount;
                    result.Add(dto);
                }
                if (i == 3)
                {
                    var dto = new PlatformContentDataInfoResult();
                    dto.Type = "付费内容量(条)";
                    dto.Value = model.PayContentCount;
                    result.Add(dto);
                }
                result[i].Item = await GetPlatformContentDataInfoItemResults(i);
            }
            return result;
        }

        public async Task<List<PlatformContentDataInfoItemResult>> GetPlatformContentDataInfoItemResults(int typeId)
        {
            var query = Q.NewQuery();
            if (typeId == 1)
            {
                var nowTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
                query.WhereDate(nameof(Entity.tede2.Media.Media.CreatedDate), ">=", nowTime);
                query.WhereDate(nameof(Entity.tede2.Media.Media.CreatedDate), "<=", DateTime.Now);
            }
            if (typeId == 2)
            {
                query.Where(nameof(Entity.tede2.Media.Media.IsChecked), true);
            }
            if (typeId == 3)
            {
                query.Where(nameof(Entity.tede2.Media.Media.FreeProportion), ">", 0);
            }
            var result = new List<PlatformContentDataInfoItemResult>();
            foreach (ResourceTypeConst item in Enum.GetValues(typeof(ResourceTypeConst)))
            {
                query.Where(nameof(Entity.tede2.Media.Media.MediaType), item.GetHashCode());
                var model = new PlatformContentDataInfoItemResult();
                model.Type = item.GetDescriptionOriginal();
                model.Value = await mediaRepository.CountAsync(query);
                result.Add(model);
            }
            foreach (AttachTypeConst item in Enum.GetValues(typeof(AttachTypeConst)))
            {
                var model = new PlatformContentDataInfoItemResult();
                model.Type = item.GetDescriptionOriginal();
                query.Where(nameof(Entity.tede2.Media.Media.AttachType), item.GetHashCode());
                model.Value = await mediaRepository.CountAsync(query);
                result.Add(model);
            }
            return result;
        }
    }
}