using dodohold.core;
using CSRedis;
using System.Data;

namespace molilian.core
{
    public partial class TkLogCore
    {
        static string queue_cps_key = "queue:cps_logs";

        public static int task_insert_parse_cps_logs(IDbConnection connection, IDbTransaction transaction, int limit, CSRedisClient redis)
        {
            int total = 0;
            for (int i = 0; i < limit; i++)
            {
                var data = redis.LPop<UnionCpsDTO>(queue_cps_key);
                if (data == null) break;

                if (_test_oaid.Equals(data.oaid) ||
                     data.ip.Contains("127.0.0"))
                {
                    var test_data = data.Convert2Json().Convert2Object<TestUnionCpsDTO>();
                    connection.Insert(test_data);
                }
                else
                {
                    connection.Insert(data);
                    if (data.success)
                    {
                        var success_data = data.Convert2Json().Convert2Object<SuccessUnionCpsDTO>();
                        connection.Insert(success_data);
                    }
                }
                total++;
            }
            return total;
        }


        public static async Task CpsLogAsync(UnionCpsDTO response, AlimamaPlus? alimamaPlus = null)
        {
            try
            {
                var ts = DateTime.Now - response.create_time;
                response.elapsedTime = (int)ts.TotalMilliseconds;
                _ = RedisHelper.RPushAsync(queue_cps_key, response);

                if (response.success) saveClientRequestTotal(response.channel, response.ip, response.oaid);

                if (!response.ip.Contains("127.0.0"))
                {
                    saveCpsCache(response.channel, response.accountId,
                        response.success, response.message, response.reason);
                }
            }
            catch (Exception ex)
            {
                _ = new LoggerLibrary("unionCps", "database_error")
                .Info(response.rawContent)
                .Info(response.Convert2Json())
                .Info(ex.Message, ex.StackTrace)
                .SaveAsync();
            }
        }

        private static void saveCpsCache(string channel, int accountId, bool success, string message, string reason)
        {
            saveAccountCpsCache("all", success, message, reason);
            saveAccountCpsCache($"{channel}", success, message, reason);
            if (accountId != 0)
            {
                saveAccountCpsCache($"{channel}_{accountId}", success, message, reason);
            }
        }

        private static void saveAccountCpsCache(string flagName, bool success, string message, string reason)
        {
            RedisHelper.IncrBy($":cps_total:{flagName}:{DateTime.Now:yyyyMM}");
            RedisHelper.IncrBy($":cps_total:{flagName}:{DateTime.Now:yyyyMMdd}");
            RedisHelper.IncrBy($":cps_total:{flagName}:{DateTime.Now:yyyyMMddHH}");


            string result = success ? "success" : "fail";
            RedisHelper.IncrBy($":cps_total:{flagName}:{result}:{DateTime.Now:yyyyMM}");
            RedisHelper.IncrBy($":cps_total:{flagName}:{result}:{DateTime.Now:yyyyMMdd}");
            RedisHelper.IncrBy($":cps_total:{flagName}:{result}:{DateTime.Now:yyyyMMddHH}");


            if (!string.IsNullOrEmpty(message))
            {
                RedisHelper.SAdd($":cps_total:{flagName}:message:{DateTime.Now:yyyyMM}", message);
                RedisHelper.SAdd($":cps_total:{flagName}:message:{DateTime.Now:yyyyMMdd}", message);
                RedisHelper.SAdd($":cps_total:{flagName}:message:{DateTime.Now:yyyyMMddHH}", message);

                RedisHelper.IncrBy($":cps_total:{flagName}:{message}:{DateTime.Now:yyyyMM}");
                RedisHelper.IncrBy($":cps_total:{flagName}:{message}:{DateTime.Now:yyyyMMdd}");
                RedisHelper.IncrBy($":cps_total:{flagName}:{message}:{DateTime.Now:yyyyMMddHH}");
                RedisHelper.IncrBy($":cps_total:{flagName}:message:{message}:{DateTime.Now:yyyyMM}");
                RedisHelper.IncrBy($":cps_total:{flagName}:message:{message}:{DateTime.Now:yyyyMMdd}");
                RedisHelper.IncrBy($":cps_total:{flagName}:message:{message}:{DateTime.Now:yyyyMMddHH}");
            }

            if (!string.IsNullOrEmpty(reason))
            {
                RedisHelper.SAdd($":cps_total:{flagName}:reason:{DateTime.Now:yyyyMM}", reason);
                RedisHelper.SAdd($":cps_total:{flagName}:reason:{DateTime.Now:yyyyMMdd}", reason);
                RedisHelper.SAdd($":cps_total:{flagName}:reason:{DateTime.Now:yyyyMMddHH}", reason);

                RedisHelper.IncrBy($":cps_total:{flagName}:{reason}:{DateTime.Now:yyyyMM}");
                RedisHelper.IncrBy($":cps_total:{flagName}:{reason}:{DateTime.Now:yyyyMMdd}");
                RedisHelper.IncrBy($":cps_total:{flagName}:{reason}:{DateTime.Now:yyyyMMddHH}");
                RedisHelper.IncrBy($":cps_total:{flagName}:reason:{reason}:{DateTime.Now:yyyyMM}");
                RedisHelper.IncrBy($":cps_total:{flagName}:reason:{reason}:{DateTime.Now:yyyyMMdd}");
                RedisHelper.IncrBy($":cps_total:{flagName}:reason:{reason}:{DateTime.Now:yyyyMMddHH}");
            }

        }




    }

}
