using System;
using System.Threading;
using System.Data;
using StackExchange.Redis;
using CSharpUtil;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Diagnostics;
using DataTransfer.tongji;

namespace DataTransfer
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("言灵数据传输开始........");

            Thread t1 = new Thread(transfer);         
            t1.Start();

            //dataSav();
        }
        public static int mask = 0;
        public static int mask_lose = 0;
        public static int dataSavNum = 0;

        public static void dataSav()
        {
            Console.WriteLine("dataSav................");
            //DataToExcelText.TargetStatistics();
        }

        public static void transfer()
        {
            Console.WriteLine("transfer............");
            while (true)
            {
                try
                {                               
                    int hour = DateTime.Now.Hour;            
                    if (hour == 2 && mask == 0)//正式的是晚上2点进行数据传输
                    {
                        Console.WriteLine("数据传输的时间到了开始执行程序.......");
                        //进行数据传输
                        mask = 1;                        
                        //dataTransfer();                      
                    }

                    if (hour != 2 && mask != 0)
                    {
                        mask = 0;
                    }

                    #region------流失玩家指标分析
                    //【解释:次日、2日、3日、4日、5日、6日、7日的流失玩家的uid和该玩家的信息统计\\redis中保存一份，execl文件也存一份】
                    if (hour == 2 && mask_lose == 0)//正式的是晚上2点进行数据传输
                    {
                        Console.WriteLine("统计信息程序开始执行............");
                        //进行数据传输
                        mask_lose = 1;
                        UserStatistics.DataDeal();                     
                    }

                    if (hour != 2 && mask_lose != 0)
                    {
                        mask_lose = 0;
                    }
                    #endregion                   
                }
                catch (Exception ex)
                {                  
                    Console.WriteLine(ex.Message);  // 提示套接字监听异常     
                    break;
                }
            }                 
        } 

        public static void dataTransfer()
        {
            Console.WriteLine("进入dataTransfer方法..............");

            TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1);
            float curday = MathF.Floor((long)ts.TotalSeconds / 24 / 60 / 60);
            float day = curday - 1;
            Console.WriteLine("当前天是：" + day);

            var mem = Redis.Ins.GetDatabase(0);

            //int zoneid = 1;//暂时先这样

            HashEntry[] zoneidList = mem.HashGetAll(MemKey_Statistics.Zonelist());
            foreach (var item in zoneidList)
            {
                string[] sList = item.ToString().Split(':');
                int zoneid = int.Parse(sList[0]);

                Taskprogressrecord_RedisToSql(zoneid);

                if (mem.KeyExists(MemKey_Statistics.GamerunLoginUser(zoneid, day)))
                {
                    string[] list = mem.HashKeys(MemKey_Statistics.GamerunLoginUser(zoneid, day)).ToStringArray();
                    foreach (string k in list)
                    {
                        Console.WriteLine("uid：" + k);
                        redisToSql(k, zoneid);
                    }
                }
                else
                {
                    Console.WriteLine(day+"-没有玩家登录,以玩家为单位的数据不做传输");
                }
            }       
            
        }
       
        /// <summary>
        /// redis数据传输到mysql中
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="zoneid"></param>
        public static void redisToSql(string uid,int zoneid)
        {
            Console.WriteLine("以玩家为单位数据传输开始..........：" );
            Userbaseparams_RedisToSql(uid,zoneid);
            TaskcompleteTsrecord_RedisToSql(uid,zoneid);
        }
       
        const string Userbaseparams_TableName = "tab_userbaseparams";

        const string TaskcompleteTsrecord_TableName = "tab_taskcompleteTsrecord";

        const string Taskprogressrecord_TableName = "tab_taskprogressrecord";

        /// <summary>
        /// 界面内数值变化数据传输
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="zoneid"></param>
        public static void Userbaseparams_RedisToSql(string uid, int zoneid)
        {
            Console.WriteLine("玩家基础数值变化方法开始.........");
            var mem = Redis.Ins.GetDatabase(0);
            string key = MemKey_Statistics.UserBaseParams(uid, zoneid);

            if (mem.KeyExists(key))
            {
                long length = mem.ListLength(key);
                var ctxList = mem.ListRange(key, 0, length);

                int num = 0;
                foreach (var item in ctxList)
                {
                    num += 1;
                    JObject dic = JObject.Parse(item.ToString());
                    int cmd = int.Parse(dic["cmd"].ToString());
                    int type = int.Parse(dic["type"].ToString());
                    string curVal = dic["curVal"].ToString();
                    string changeVal = dic["changeVal"].ToString();
                    string endVal = dic["endVal"].ToString();
                    string desc = dic["desc"].ToString();
                    //Console.WriteLine("desc..........："+ desc);
                    string ts = dic["ts"].ToString();

                    //continue;

                    var sql = $" Insert Into {Userbaseparams_TableName} (`uid`, `zoneid`, `cmd`, `type`, `curVal`, `endVal`, `changeVal`, `desc`, `ts`) " +
                $"values('{uid}',{zoneid},{cmd},{type},'{curVal}','{endVal}','{changeVal}','{desc}','{ts}')";

                    var n = MysqlUtil.Ins.ExecuteSqlNonQuery(sql);
                    //Debug.Assert(n > 0, $"{sql} 执行失败!");

                    if (num >= length)
                    {
                        Console.WriteLine("总共执行条数：" + num);
                        mem.KeyDelete(key);
                        break;
                    }

                }
            }
        }

        /// <summary>
        /// 个人数据 包括以下两点：
        ///1.通过玩家ID查询指定玩家的任务进度，包括任务ID和任务STEP ID----【直接从玩家数据里取数据展示】
        ///2.可查询玩家每日任务进度变化---【单一记录玩家自己的任务信息就可以了,每次做任务时候要记录时间，展示的时候需要以天为单位展示任务进度】
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="zoneid"></param>
        public static void TaskcompleteTsrecord_RedisToSql(string uid, int zoneid)
        {            
            var mem = Redis.Ins.GetDatabase(0);
            string key = MemKey_Statistics.SelfTaskDataCollect(uid, zoneid);

            if (mem.KeyExists(key))
            {
                Console.WriteLine("玩家任务完成时间记录方法开始.........");
                long length = mem.ListLength(key);
                var ctxList = mem.ListRange(key, 0, length);

                int num = 0;
                foreach (var item in ctxList)
                {
                    num += 1;
            
                    string[] sList = item.ToString().Split('-');
                    string taskId = sList[0];                              
                    string time = sList[1];

                    var sql = $" Insert Into {TaskcompleteTsrecord_TableName} (`uid`, `zoneid`, `taskId`, `time`) " +
                $"values('{uid}',{zoneid},'{taskId}','{time}')";

                    var n = MysqlUtil.Ins.ExecuteSqlNonQuery(sql);
                    //Debug.Assert(n > 0, $"{sql} 执行失败!");

                    if (num >= length)
                    {
                        Console.WriteLine("总共执行条数：" + num);
                        mem.KeyDelete(key);
                        break;
                    }

                }
            }
        }

        /// <summary>
        /// 所有玩家最新任务id记录
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="zoneid"></param>
        public static void Taskprogressrecord_RedisToSql(int zoneid)
        {
            Console.WriteLine("玩家任务进度方式开始......");
            var mem = Redis.Ins.GetDatabase(0);
            string key = MemKey_Statistics.TaskDataCollect(zoneid);

            if (mem.KeyExists(key))
            {
                HashEntry[] ctxList = mem.HashGetAll(key);
                int length = ctxList.Length;
                int num = 0;
                foreach (var item in ctxList)
                {
                    num += 1;
                    string[] sList = item.ToString().Split(':');
                    string uid = sList[0];
                    string taskId = sList[1];

                    var sql = $" Insert Into {Taskprogressrecord_TableName} (`uid`, `zoneid`, `taskId`) " +
                $"values('{uid}',{zoneid},'{taskId}')";

                    var n = MysqlUtil.Ins.ExecuteSqlNonQuery(sql);
                    //Debug.Assert(n > 0, $"{sql} 执行失败!");

                    if (num >= length)
                    {
                        Console.WriteLine("总共执行条数：" + num);
                        mem.KeyDelete(key);
                        break;
                    }
                }
            }
            else
            {
                Console.WriteLine("玩家任务进度redis-key不存在所以不在数据传输处理");
            }          
        }  
    }
}
