using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using dodohold.core;
using static dodohold.core.ZTOExpress.CreateOrderArgs;
using System.Net;
using System.Security.Cryptography;
using Spire.Pdf.Exporting.XPS.Schema;
using System.Xml.Linq;
using static QRCoder.PayloadGenerator;
using TencentCloud.Ssl.V20191205.Models;
using CSRedis;
using System.Data;
using TencentCloud.Omics.V20221128.Models;
using TencentCloud.Csip.V20221121.Models;


namespace molilian.core
{
    public class TkLogCore
    {
        static string queue_tb_key = "queue:logs:tb";
        static string queue_jd_key = "queue:logs:jd";

        static string queue_parse_tb_key = "queue:parse_logs:tb";
        static string queue_parse_jd_key = "queue:parse_logs:jd";
        static string queue_parse_dy_key = "queue:parse_logs:dy";
        static string queue_parse_tool_key = "queue:parse_logs:tool";

        static string queue_coupon_key = "queue:coupon_logs";
        static string promotion_img_key = "queue:promotion:img";


        public static int BatchInsertLogDB(int limit)
        {
            var result = EndPointCore.ProcessEndPointNodes<int>(node =>
            {
                if (!node.is_public_api) return 0;

                if (CenterHub.IsCenter)
                {
                    if (node.is_coupon_api) return 0;
                }
                else
                {
                    if (!node.is_coupon_api) return 0;
                }

                if (string.IsNullOrEmpty(node.redis_server)) return 0;

                var redis = RedisClientManager.GetRedisClient(node.redis_server);
                return BatchInsertLogDB(limit, redis);
            });
            return result.Sum();
        }

        private static int save_tk_log(TkDataDTO data, string tablename, IDbConnection connection, IDbTransaction transaction)
        {
            return new DBContext.Table(connection, tablename)
                  .Add("end_point", data.end_point)
                  .Add("channel", (int)data.channel)
                  .Add("accountId", data.accountId)
                  .Add("accountName", data.accountName)
                  .Add("rawContent", data.rawContent)
                  .Add("rawContent2", data.rawContent2)
                  .Add("success", data.success)
                  .Add("message", data.message)
                  .Add("reason", data.reason)
                  .Add("content", data.content)
                  .Add("couponAmount", data.couponAmount)
                  .Add("itemId", data.itemId)
                  .Add("itemName", data.itemName)
                  .Add("pic", data.pic)
                  .Add("promotionPrice", data.promotionPrice)
                  .Add("taoToken", data.taoToken)
                  .Add("shortLinkurl", data.shortLinkurl)
                  .Add("deeplink_url", data.deeplink_url)
                  .Add("num_iid", data.num_iid)
                  .Add("elapsedTime", data.elapsedTime)
                  .Add("subCode", data.subCode)
                  .Add("ip", data.ip)
                  .Add("oaid", data.oaid)
                  .Add("create_time", data.create_time)
                  .Create(DBContext.InsertType.NORMAL, transaction);
        }
        private static int save_tk_parse_logs(TkDataDTO data, string tablename, IDbConnection connection, IDbTransaction transaction)
        {
            return new DBContext.Table(connection, tablename)
                .Add("end_point", data.end_point)
                .Add("channel", (int)data.channel)
                .Add("linkType", (int)data.link_type)
                .Add("accountId", data.accountId)
                .Add("accountName", data.accountName)
                .Add("rawContent", data.rawContent)
                .Add("success", data.success)
                .Add("message", data.message)
                .Add("reason", data.reason)
                .Add("content", data.content)
                .Add("itemId", data.itemId)
                .Add("itemName", data.itemName)
                .Add("pic", data.pic)
                .Add("couponAmount", data.couponAmount)
                .Add("promotionPrice", data.promotionPrice)
                .Add("taoToken", data.taoToken)
                .Add("shortLinkurl", data.shortLinkurl)
                .Add("deeplink_url", data.deeplink_url)
                .Add("num_iid", data.num_iid)
                .Add("elapsedTime", data.elapsedTime)
                .Add("elapsedTime2", data.elapsedTime2)
                .Add("elapsedTime3", data.elapsedTime3)
                .Add("subCode", data.subCode)
                .Add("ip", data.ip)
                .Add("oaid", data.oaid)
                .Add("create_time", data.create_time)

                .Create(DBContext.InsertType.NORMAL, transaction);
        }
        private static int save_jd_parse_logs(JdDataDTO data, string tablename, IDbConnection connection, IDbTransaction transaction)
        {
            return new DBContext.Table(connection, tablename)
                .Add("end_point", data.end_point)
                .Add("channel", (int)data.channel)
                .Add("accountId", data.accountId)
                .Add("accountName", data.accountName)
                .Add("rawContent", data.rawContent)
                .Add("success", data.success)
                .Add("message", data.message)
                .Add("reason", data.reason)
                .Add("content", data.content)
                .Add("itemId", data.itemId)
                .Add("itemName", data.itemName)
                .Add("pic", data.pic)
                .Add("couponAmount", data.couponAmount)
                .Add("promotionPrice", data.promotionPrice)
                .Add("taoToken", data.taoToken)
                .Add("shortLinkurl", data.shortLinkurl)
                .Add("deeplink_url", data.deeplink_url)
                .Add("elapsedTime", data.elapsedTime)
                .Add("subCode", data.subCode)
                .Add("ip", data.ip)
                .Add("oaid", data.oaid)
                .Add("create_time", data.create_time)
                .Create(DBContext.InsertType.NORMAL, transaction);
        }
        public static int BatchInsertLogDB(int limit, CSRedisClient redis)
        {
            int total = 0;
            using var connection = DBContext.GetOpenConnection();
            connection.Open();
            using var transaction = connection.BeginTransaction();


            string cacheKey = ":lock_key:start_comparison_tk";
            bool start_comparison_tk = RedisHelper.Get<int>(cacheKey) > 0;


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

                    if ("3JDaxNfPy83okP3kLScwCkGiuMcyC4PcyntF424979CC9C51BCAD0C245B1C7BA2".Equals(data.oaid) ||
                         data.ip.Contains("127.0.0"))
                    {
                        save_tk_log(data, "tk_logs_test", connection, transaction);
                    }
                    else
                    {
                        data.id = save_tk_log(data, "tk_logs", connection, transaction);
                        if (data.elapsedTime > 1000)
                        {
                            save_tk_log(data, "tk_logs_test", connection, transaction);
                        }
                        if (data.success)
                        {
                            save_tk_log(data, "tk_success_logs", connection, transaction);
                        }
                        if (!string.IsNullOrEmpty(data.itemId)) TkOrderTrackingCore.SaveLinkSummary(data);
                    }
                    total++;
                }

                for (int i = 0; i < limit; i++)
                {
                    var data = redis.LPop<JdDataDTO>(queue_jd_key);
                    if (data == null) break;
                    new DBContext.Table(connection, "tk_logs")
                        .Add("end_point", data.end_point)
                        .Add("channel", (int)data.channel)
                        .Add("accountId", data.accountId)
                        .Add("accountName", data.accountName)
                        .Add("rawContent", data.rawContent)
                        .Add("rawContent2", data.rawContent2)
                        .Add("success", data.success)
                        .Add("message", data.message)
                        .Add("reason", data.reason)
                        .Add("shortLinkurl", data.shortLinkurl)
                        .Add("deeplink_url", data.deeplink_url)
                        .Add("elapsedTime", data.elapsedTime)
                        .Add("ip", data.ip)
                        .Add("oaid", data.oaid)
                        .Add("create_time", data.create_time)
                        .Create(DBContext.InsertType.NORMAL, transaction);
                    total++;
                }

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

                    if ("3JDaxNfPy83okP3kLScwCkGiuMcyC4PcyntF424979CC9C51BCAD0C245B1C7BA2".Equals(data.oaid) ||
                         data.ip.Contains("127.0.0"))
                    {
                        save_tk_parse_logs(data, "tk_parse_logs_test", connection, transaction);
                    }
                    else
                    {
                        data.id = save_tk_parse_logs(data, "tk_parse_logs", connection, transaction);
                        if (data.elapsedTime > 1000)
                        {
                            save_tk_parse_logs(data, "tk_parse_logs_test", connection, transaction);
                        }
                        if (data.success)
                        {
                            save_tk_parse_logs(data, "tk_success_parse_logs", connection, transaction);
                        }
                        if (!string.IsNullOrEmpty(data.itemId)) TkOrderTrackingCore.SaveLinkSummary(data);
                    }

                    if (data.reason.Contains("初步筛选2") && !data.rawContent.Contains("I:/kWqN5t623Hx"))
                    {
                        save_tk_parse_logs(data, "tk_parse_logs_test2", connection, transaction);
                    }

                    if (data.reason.Contains("初步筛选1.5"))
                    {
                        save_tk_parse_logs(data, "tk_parse_logs_multi_token", connection, transaction);
                    }
                    if (data.reason.Contains("霸下验证码"))
                    {
                        save_tk_parse_logs(data, "tk_parse_logs_captcha", connection, transaction);
                    }
                    total++;
                }


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


                    if ("3JDaxNfPy83okP3kLScwCkGiuMcyC4PcyntF424979CC9C51BCAD0C245B1C7BA2".Equals(data.oaid) ||
                         data.ip.Contains("127.0.0"))
                    {
                        save_jd_parse_logs(data, "jd_parse_logs_test", connection, transaction);
                    }
                    else
                    {
                        save_jd_parse_logs(data, "jd_parse_logs", connection, transaction);
                        if (data.elapsedTime > 1000)
                        {
                            save_jd_parse_logs(data, "jd_parse_logs_test", connection, transaction);
                        }
                        if (data.success)
                        {
                            save_jd_parse_logs(data, "jd_parse_logs_success", connection, transaction);
                        }
                    }
                    total++;
                }
                for (int i = 0; i < limit; i++)
                {
                    var data = redis.LPop<DyDataDTO>(queue_parse_dy_key);
                    if (data == null) break;
                    new DBContext.Table(connection, "tk_parse_logs")
                        .Add("end_point", data.end_point)
                        .Add("channel", (int)data.channel)
                        .Add("accountId", data.accountId)
                        .Add("accountName", data.accountName)
                        .Add("rawContent", data.rawContent)
                        .Add("success", data.success)
                        .Add("message", data.message)
                        .Add("reason", data.reason)
                        .Add("content", data.content)
                        .Add("itemId", data.itemId)
                        .Add("itemName", data.itemName)
                        .Add("pic", data.pic)
                        .Add("couponAmount", data.couponAmount)
                        .Add("promotionPrice", data.promotionPrice)
                        .Add("taoToken", data.taoToken)
                        .Add("shortLinkurl", data.shortLinkurl)
                        .Add("deeplink_url", data.deeplink_url)
                        .Add("elapsedTime", data.elapsedTime)
                        .Add("subCode", data.subCode)
                        .Add("ip", data.ip)
                        .Add("oaid", data.oaid)
                        .Add("create_time", data.create_time)
                        .Create(DBContext.InsertType.NORMAL, transaction);
                    total++;
                }
                for (int i = 0; i < limit; i++)
                {
                    var data = redis.LPop<ToolParseDataDTO>(queue_parse_tool_key);
                    if (data == null) break;
                    new DBContext.Table(connection, "tool_parse_logs")
                        .Add("end_point", data.end_point)
                        .Add("channel", (int)data.channel)
                        .Add("rawContent", data.rawContent)
                        .Add("success", data.success)
                        .Add("message", data.message)
                        .Add("reason", data.reason)
                        .Add("content", data.content)
                        .Add("taoToken", data.taoToken)
                        .Add("shortLinkurl", data.shortLinkurl)
                        .Add("deeplink_url", data.deeplink_url)
                        .Add("elapsedTime", data.elapsedTime)
                        .Add("ip", data.ip)
                        .Add("oaid", data.oaid)
                        .Add("create_time", data.create_time)
                        .Create(DBContext.InsertType.NORMAL, transaction);
                    total++;
                }


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

                    if ("3JDaxNfPy83okP3kLScwCkGiuMcyC4PcyntF424979CC9C51BCAD0C245B1C7BA2".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++;
                }

                for (int i = 0; i < limit; i++)
                {
                    var data = redis.LPop<PromotionQueryDTO>(promotion_img_key);
                    if (data == null) break;
                    connection.Insert(data);
                    total++;
                }
                transaction.Commit();
            }
            catch (Exception ex)
            {
                transaction.Rollback();

                _ = new LoggerLibrary("database_error", "parse_log")
                    .Info(ex.Message, ex.StackTrace)
                    .SaveAsync();

                NotifyCore.Notify(new NifyMessage
                {
                    message = $"【写入日志异常】\n{ex.Message}\n{ex.StackTrace}",
                    priority = NifyMessagePriority.high,
                    tags = ["red_circle"]
                });
            }
            finally
            {
                connection.Close();
            }
            return total;
        }

        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) { }
        }

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

                if (!response.ip.Contains("127.0.0"))
                {
                    saveCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
                }
            }
            catch (Exception ex) { }
        }

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

                if (!response.ip.Contains("127.0.0"))
                {
                    saveCache(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}\n{response.rawContent2}");
                            });
                            break;
                    }
                }
                if ("没有匹配账号".Equals(response.reason))
                {
                    TkPoolCore.AccountExhausted();
                }
            }
            catch (Exception ex)
            {
                _ = new LoggerLibrary("unionParse", "database_error")
                .Info(response.rawContent, response.rawContent2)
                .Info(response.Convert2Json())
                .Info(ex.Message, ex.StackTrace)
                .SaveAsync();
            }
        }

        public static async Task ParseLogAsync(TkDataDTO response, AlimamaPlus? alimamaPlus = null)
        {
#if DEBUG
            //return;
#endif
            try
            {
                var ts = DateTime.Now - response.create_time;
                response.elapsedTime = (int)ts.TotalMilliseconds;
                _ = RedisHelper.RPushAsync(queue_parse_tb_key, response);


                if (response.success)
                {
                    _ = saveUnionCouponParseCacheAsync(response);
                }
                if (!response.ip.Contains("127.0.0"))
                {
                    saveParseCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
                }

                if (!string.IsNullOrEmpty(response.itemId)) TkOrderTrackingCore.SaveLinkSummary(response);

                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("unionParse", "database_error")
                .Info(response.rawContent)
                .Info(response.Convert2Json())
                .Info(ex.Message, ex.StackTrace)
                .SaveAsync();
            }
        }
        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();
            }
        }
        public static async Task ParseLogAsync(JdDataDTO response)
        {
            try
            {
                var ts = DateTime.Now - response.create_time;
                response.elapsedTime = (int)ts.TotalMilliseconds;
                _ = RedisHelper.RPushAsync(queue_parse_jd_key, response);

                if (!response.ip.Contains("127.0.0"))
                {
                    saveParseCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
                }

                if (response.success || response.message.Equals("转链失败"))
                {
                    JdPoolCore.CallsIncrBy(response.accountId);
                }
                //CallsIncrBy

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

                if (!response.success && ("nologin".Equals(response.reason) ||
                    "方法不存在".Equals(response.reason) ||
                    "未登录".Equals(response.reason)))
                {
                    await Task.Run(() =>
                    {
                        JdPoolCore.Disabled(response.accountId, response.accountName, $"{response.rawContent}\n{response.rawContent2}");
                    });
                    //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}\n{response.rawContent2}");
                    //        //});
                    //        break;
                    //}
                }
                if ("没有匹配账号".Equals(response.reason))
                {
                    JdPoolCore.AccountExhausted();
                }
            }
            catch (Exception ex) { }
        }
        public static async Task ParseLogAsync(DyDataDTO response)
        {
            try
            {
                var ts = DateTime.Now - response.create_time;
                response.elapsedTime = (int)ts.TotalMilliseconds;
                _ = RedisHelper.RPushAsync(queue_parse_dy_key, response);

                if (!response.ip.Contains("127.0.0"))
                {
                    saveParseCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
                }

            }
            catch (Exception ex) { }
        }
        public static async Task ParseLogAsync(ToolParseDataDTO response)
        {
            try
            {
                var ts = DateTime.Now - response.create_time;
                response.elapsedTime = (int)ts.TotalMilliseconds;
                _ = RedisHelper.RPushAsync(queue_parse_tool_key, response);

                if (!response.ip.Contains("127.0.0"))
                {
                    saveParseCache(response.channel.ToString(), 0, "tool", response.success, response.message, response.reason);
                }
            }
            catch (Exception ex) { }
        }

        private static void saveCache(string channel, int accountId, string accountName, bool success, string message, string reason)
        {
            saveAccountCache("all", success, message, reason);
            saveAccountCache($"{channel}", success, message, reason);
            saveAccountCache($"{accountName}", success, message, reason);
            if (accountId != 0)
            {
                //todo 放着跑两天，要将读取的地方改成读取accountid
                saveAccountCache($"{channel}_{accountId}", success, message, reason);
            }
        }

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


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


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

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

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

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

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


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

        }

        private static async Task saveUnionCouponParseCacheAsync(TkDataDTO data)
        {
            string cacheKey = $":cache:parse:{data.ip}_{data.oaid}_{data.itemId}";
            await EndPointCore.ProcessEndPointNodesAsync(node =>
            {
                if (!node.is_coupon_api) return Task.CompletedTask;
                if (string.IsNullOrEmpty(node.redis_server)) return Task.CompletedTask;

                var redis = RedisClientManager.GetRedisClient(node.redis_server);
                redis.Set(cacheKey, 1, 2 * 86400);
                return Task.CompletedTask;
            });
        }
        private static void saveClientRequestTotal(TkChannelEnum channel, string ip, string oaid)
        {
            string cacheKey = $":cache:{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
            RedisHelper.IncrBy(cacheKey);
            RedisHelper.Expire(cacheKey, 86400);

            if (!string.IsNullOrEmpty(oaid))
            {
                cacheKey = $":cache:{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
                RedisHelper.IncrBy(cacheKey);
                RedisHelper.Expire(cacheKey, 86400);
            }
        }
        public static int getClientRequestTotalByOAID(TkChannelEnum channel, string oaid)
        {
            if (string.IsNullOrEmpty(oaid)) return 0;
            string cacheKey = $":cache:{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
            return RedisHelper.Get<int>(cacheKey);
        }
        public static int getClientRequestTotalByIp(TkChannelEnum channel, string ip)
        {
            if (string.IsNullOrEmpty(ip)) return 0;
            string cacheKey = $":cache:{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
            return RedisHelper.Get<int>(cacheKey);
        }

        private static void saveParseCache(string channel, int accountId, string accountName, bool success, string message, string reason)
        {
            saveParseAccountCache("all", success, message, reason);
            saveParseAccountCache($"{channel}", success, message, reason);
            saveParseAccountCache($"{accountName}", success, message, reason);
            if (accountId != 0)
            {
                //todo 放着跑两天，要将读取的地方改成读取accountid
                saveParseAccountCache($"{channel}_{accountId}", success, message, reason);
            }
        }

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


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


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

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

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

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

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

        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}");
            }
        }


        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}");
            }

        }

        public static int GetTotal(string keyname, bool all_node = true)
        {
            //:coupon_total:tb:20240706
            //:coupon_total:tb:success:20240706
            //:coupon_total:tb:放弃转链:20240706

            var result = EndPointCore.ProcessEndPointNodes<int>(node =>
            {
                if (!node.is_public_api) return 0;
                if (string.IsNullOrEmpty(node.redis_server)) return 0;

                if (!all_node)
                {
                    if (CenterHub.IsCenter)
                    {
                        if (node.is_coupon_api) { return 0; }
                    }
                    else
                    {
                        if (!node.is_coupon_api) { return 0; }
                    }
                }

#if DEBUG
                switch (node.name)
                {

                    case "bj":
                        node.redis_server = "101.200.46.46:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
                        break;
                    case "gz":
                        node.redis_server = "8.138.110.158:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
                        break;
                    case "coupon1":
                        node.redis_server = "c1api.molilian.com:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=coupon";
                        break;
                    default: return 0;
                }

#endif
                var redis = RedisClientManager.GetRedisClient(node.redis_server);
                int count = redis.Get<int>(keyname);
                return count;
            });
            return result.Sum();
        }

        public static string[] GetTotalKeys(string keyname, bool all_node = true)
        {

            var result = EndPointCore.ProcessEndPointNodes<string[]>(node =>
            {
                if (!node.is_public_api) return [];
                if (string.IsNullOrEmpty(node.redis_server)) return [];

                if (!all_node)
                {
                    if (CenterHub.IsCenter)
                    {
                        if (node.is_coupon_api) { return []; }
                    }
                    else
                    {
                        if (!node.is_coupon_api) { return []; }
                    }
                }
#if DEBUG
                switch (node.name)
                {

                    case "bj":
                        node.redis_server = "101.200.46.46:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
                        break;
                    case "gz":
                        node.redis_server = "8.138.110.158:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
                        break;
                    case "coupon1":
                        node.redis_server = "c1api.molilian.com:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=coupon";
                        break;
                    default: return [];
                }
#endif

                var redis = RedisClientManager.GetRedisClient(node.redis_server);

                string[] message_keys = redis.SMembers(keyname);
                return message_keys;
            });

            string[] message_keys = [];
            foreach (var arr in result)
            {
                message_keys = message_keys.Union(arr).ToArray();
            }
            return message_keys;
        }


        //if (item.total_count > 0) continue;
        //item.total_count = RedisHelper.Get<int>($":total:all:{item.report_date:yyyyMMdd}");
        //if (item.total_count == 0) continue;
        //item.success_count = RedisHelper.Get<int>($":total:all:success:{item.report_date:yyyyMMdd}");
        //item.abandon_count = RedisHelper.Get<int>($":total:all:放弃转链:{item.report_date:yyyyMMdd}");




    }

}
