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

namespace molilian.core
{
    public partial class TkLogCore
    {
        static string queue_coupon_key = "queue:coupon_logs";
        public static int task_insert_parse_coupon_logs(IDbConnection connection, IDbTransaction transaction, int limit, CSRedisClient redis)
        {
            int total = 0;
            for (int i = 0; i < limit; i++)
            {
                var data = redis.LPop<UnionCouponDTO>(queue_coupon_key);
                if (data == null) break;

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

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

                if (!response.ip.Contains("127.0.0"))
                {
                    saveCouponCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
                }
                if (!response.success && "nologin".Equals(response.message))
                {
                    switch (response.channel)
                    {
                        case TkChannelEnum.tb:
                            await Task.Run(() =>
                            {
                                if (alimamaPlus != null)
                                {
                                    (bool success, string message) = alimamaPlus.RenewCookie();
                                    if (success) return;
                                }
                                TkPoolCore.Disabled(response.accountId, response.accountName, $"{response.rawContent}");
                            }); break;
                    }
                }
            }
            catch (Exception ex)
            {
                _ = new LoggerLibrary("unionCoupon", "database_error")
                .Info(response.rawContent)
                .Info(response.Convert2Json())
                .Info(ex.Message, ex.StackTrace)
                .SaveAsync();
            }
        }
        private static void saveCouponCache(string channel, int accountId, string accountName, bool success, string message, string reason)
        {
            saveAccountCouponCache("all", success, message, reason);
            saveAccountCouponCache($"{channel}", success, message, reason);
            if (accountId != 0)
            {
                saveAccountCouponCache($"{channel}_{accountId}", success, message, reason);
            }
        }

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


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


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

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

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

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

        }




    }

}
