using CloudGaming.DtoModel.Other;

using HuanMeng.DotNetCore.QuartzExtend;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CloudGaming.Code.Monitor
{
    /// <summary>
    /// 游戏控制器
    /// </summary>
    [QuartzTrigger("GameMonitorProcessor", "* 05 4 * * ?")]
    public class GameMonitorProcessor : AppJobBase
    {
        public GameMonitorProcessor(IServiceScopeFactory scopeFactory) : base(scopeFactory)
        {
        }

        public override async Task AppConfigProcessLoop(AppConfig appConfig, AppMonitorInfo appMonitorInfo, IServiceProvider serviceProvider, CloudGamingBase cloudGamingBase)
        {
            var dao = cloudGamingBase.Dao;
            var now = DateTime.Now.AddDays(-1);
            int day = int.Parse(now.ToString("yyyyMMdd"));
            var nowDay = DateOnly.FromDateTime(now);
            var gamePlay = dao.DaoPhone.Context.T_Game_PlayGameLog.Where(it => it.CreateDay == day).AsQueryable();
            //启动游戏次数
            var gamePlayCount = await gamePlay.GroupBy(log => log.Channel).ToDictionaryAsync(it => it.Key ?? "", it => it.Count());
            //启动游戏人数
            var gamePlayLog = await gamePlay.GroupBy(log => log.Channel).Select(group => new
            {
                Channel = group.Key,
                UserCount = group.Select(log => log.UserId).Distinct().Count()
            }).ToDictionaryAsync(it => it.Channel ?? "", it => it.UserCount);
            // 游玩时间
            var gamePlaySeconds = await gamePlay.GroupBy(log => log.Channel).Select(group => new
            {
                Channel = group.Key,
                PlayGameTotalSeconds = group.Sum(it => it.PlayGameTotalSeconds)
            }).ToDictionaryAsync(it => it.Channel ?? "", it => it.PlayGameTotalSeconds);

            // 获取当前统计数据
            var userStatistics = await dao.DaoExt.Context.T_Statistics_Game
                .Where(it => it.LoginDay == day)
                .ToDictionaryAsync(it => it.Channel ?? "");
            // 更新或创建统计记录
            void UpdateStatistics(string channel, int playGameCount, int playGameTimeCount, int startGameCount)
            {
                if (!userStatistics.TryGetValue(channel, out var statisticsUser))
                {
                    statisticsUser = new T_Statistics_Game()
                    {
                        PlayGameCount = 0,
                        Channel = channel,
                        CreatedAt = DateTime.Now,
                        PlayGameTimeCount = 0,
                        StartGameCount = 0,
                        LoginDate = nowDay,
                        LoginDay = day,
                        UpdatedAt = DateTime.Now,
                    };
                    dao.DaoExt.Context.T_Statistics_Game.Add(statisticsUser);
                    userStatistics.Add(channel, statisticsUser);
                }
                statisticsUser.PlayGameCount = playGameCount;
                statisticsUser.PlayGameTimeCount = playGameTimeCount > 0 ? playGameTimeCount / 60 / 60 : playGameTimeCount;
                statisticsUser.StartGameCount = startGameCount;
                statisticsUser.UpdatedAt = DateTime.Now;
            }

            foreach (var item in gamePlayCount)
            {
                UpdateStatistics(item.Key, item.Value, gamePlaySeconds.GetValueOrDefault(item.Key, 0), gamePlayLog.GetValueOrDefault(item.Key, 0));
            }

            await dao.DaoExt.Context.SaveChangesAsync();
        }
    }
}
