using System;
using System.Threading.Tasks;
using MrWu.Debug;
using Server.DB.Redis;
using StackExchange.Redis;

namespace Server
{
    /// <summary>
    /// 活动统计
    /// </summary>
    public class ActivityStatistics
    {
        public const string Statistics = "Statistics";
        
        /// <summary>
        /// 活动
        /// </summary>
        public const string Name = "Activity";

        /// <summary>
        /// 全名
        /// </summary>
        private const string full_Name = Statistics + ":" + Name;

        /// <summary>
        /// 广告
        /// </summary>
        public const string Activity_AdGift = "AdGift";

        private const string Activity_AdGift_FullName = full_Name + ":" + Activity_AdGift + ":{0}:{1}";

        public const string Activity_Dole = "Dole";

        private const string Activity_Dole_FullName = full_Name + ":" + Activity_Dole + ":{0}:{1}";

        private static string GetStatisticsKey(string activityName, int plat)
        {
            switch (activityName)
            {
                case Activity_AdGift:
                    return string.Format(Activity_AdGift_FullName, DateTime.Now.Date.ToString("yyyyMMdd"), plat);
                case Activity_Dole:
                    return string.Format(Activity_Dole_FullName, DateTime.Now.Date.ToString("yyyyMMdd"), plat);
                default:
                    return null;
            }
        }

        /// <summary>
        /// 保存活动日志
        /// </summary>
        /// <param name="userid">玩家userid</param>
        /// <param name="money">活动获得的金额</param>
        /// <param name="plat">平台</param>
        /// <param name="activityName">活动名称</param>
        public static void SaveLog(int userid, long money, int plat, string activityName)
        {
            string key = GetStatisticsKey(activityName, plat);
            if (string.IsNullOrEmpty(key))
            {
                Debug.Error("未找到活动:" + activityName);
                return;
            }

            IBatch batch = redisManager.GetBiSaidb(RedisDictionary.Statistics).CreateBatch();
            batch.HashIncrementAsync(key, "Count");
            batch.HashIncrementAsync(key, "Money", money);
            batch.KeyExpireAsync(key, new TimeSpan(60, 0, 0, 0));
            batch.Execute();
        }
    }
}