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

namespace molilian.core
{
    public partial class TkLogCore
    {
        static string promotion_img_key = "queue:promotion:img";

        public static int task_insert_promotion_img_logs(IDbConnection connection, IDbTransaction transaction, int limit, CSRedisClient redis)
        {
            int total = 0;

            for (int i = 0; i < limit; i++)
            {
                var data = redis.LPop<PromotionQueryDTO>(promotion_img_key);
                if (data == null) break;

                if (data.success)
                {
                    data.similarPromotion = string.Empty;
                    data.promotionImg = string.Empty;
                }

                //按日保存数据
                string daily_table = $"tk_promotion_logs_{DateTime.Now:yyyyMMdd}";
                save_tk_promotion_img_logs(data, daily_table, connection, transaction);

                total++;
            }
            return total;
        }

        private static int save_tk_promotion_img_logs(PromotionQueryDTO data, string tablename, IDbConnection connection, IDbTransaction transaction)
        { 
            try
            {
                return new DBContext.Table(connection, tablename)
                    .Add("accountId", data.accountId)
                    .Add("accountName", data.accountName)
                    .Add("img", data.img)
                    .Add("ip", data.ip)
                    .Add("oaid", data.oaid)
                    .Add("success", data.success)
                    .Add("message", data.message)
                    .Add("reason", data.reason)
                    .Add("url", data.url)
                    .Add("similarPromotion", data.similarPromotion)
                    .Add("promotionImg", data.promotionImg)

                    .Add("elapsedTime", data.elapsedTime)
                    .Add("elapsedTime2", data.elapsedTime2)
                    .Add("elapsedTime3", data.elapsedTime3)
                    .Add("elapsedTime4", data.elapsedTime4)
                    .Add("create_time", data.create_time)
                    .Add("end_point", data.end_point)
                    .Create(DBContext.InsertType.NORMAL, transaction);
            }
            catch (Exception ex) { return 0; }
        }

        public static async Task PromotionImgLogAsync(PromotionQueryDTO response)
        {
            try
            {
                var ts = DateTime.Now - response.create_time;
                response.elapsedTime = (int)ts.TotalMilliseconds;
                _ = RedisHelper.RPushAsync(promotion_img_key, response);

                //using var connection = DBContext.GetOpenConnection();
                //connection.Insert(response);
                savePromotionCache(response.accountId, response.accountName, response.success, response.message, response.reason);
            }
            catch (Exception ex) { }
        }





        private static void savePromotionCache(int accountId, string accountName, bool success, string message, string reason)
        {
            savePromotionAccountCache("all", success, message, reason);
            savePromotionAccountCache($"{accountId}", success, message, reason);
        }

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


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


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

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

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

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





    }

}
