using dodohold.core;
using CSRedis;
using System.Data;
using YunhuiKit;
using static dodohold.core.ZTOExpress.CreateOrderArgs;
using System.Xml.Linq;

namespace molilian.core
{
    public partial class TkLogCore
    {
        static string queue_parse_jd_key = "queue:parse_logs:jd";


        public static async Task<int> InsertParseJDLogAsync(int limit, YunhuiKit.RedisClient redis)
        {
            int total = 0;
            using var connection = DBContext.GetOpenConnection();
            connection.Open();
            using var transaction = connection.BeginTransaction();
            try
            {
                for (int i = 0; i < limit; i++)
                {
                    var data = await redis.LPopAsync<JdDataDTO>(queue_parse_jd_key);
                    if (data == null) break;

                    if (TestParseCore.InWhitelist(data.ip, data.oaid))
                    {
                        save_jd_parse_logs(data, "jd_parse_logs_test", connection, transaction);
                    }
                    else
                    {
                        if ("dp".Equals(data.parse_type))
                        {
                            string daily_table = $"jd_dp_logs_{data.create_time:yyyyMMdd}";
                            save_jd_parse_logs(data, daily_table, connection, transaction);

                            if (data.success)
                            {
                                save_jd_parse_logs(data, "jd_dp_logs_success", connection, transaction);
                            }
                        }

                        //save_jd_parse_logs(data, "jd_parse_logs", connection, transaction);
                        //if (save_dailys_log)
                        {
                            string daily_table = $"jd_parse_logs_{data.create_time:yyyyMMdd}";
                            save_jd_parse_logs(data, daily_table, 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++;
                }
                transaction.Commit();
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                new LoggerLibrary("TkLogCore_error", "task_insert_parse_jd_logs")
                      .Info(ex.Message, ex.StackTrace)
                      .SaveAsync();
                throw;
            }
            finally
            {
                connection.Close();
            }
            return total;
        }

        public static int task_insert_parse_jd_logs(int limit, CSRedisClient redis)
        {
            int total = 0;
            using var connection = DBContext.GetOpenConnection();
            connection.Open();
            using var transaction = connection.BeginTransaction();
            try
            {
                for (int i = 0; i < limit; i++)
                {
                    var data = redis.LPop<JdDataDTO>(queue_parse_jd_key);
                    if (data == null) break;

                    if (TestParseCore.InWhitelist(data.ip, data.oaid))
                    {
                        save_jd_parse_logs(data, "jd_parse_logs_test", connection, transaction);
                    }
                    else
                    {
                        //save_jd_parse_logs(data, "jd_parse_logs", connection, transaction);

                        //if (save_dailys_log)
                        {
                            string daily_table = $"jd_parse_logs_{data.create_time:yyyyMMdd}";
                            save_jd_parse_logs(data, daily_table, 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++;
                }
                transaction.Commit();
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                new LoggerLibrary("TkLogCore_error", "task_insert_parse_jd_logs")
                      .Info(ex.Message, ex.StackTrace)
                      .SaveAsync();
                throw;
            }
            finally
            {
                connection.Close();
            }
            return total;
        }

        public static async Task ParseLogAsync(JdDataDTO response, bool is_hide)
        {
            try
            {
                var ts = DateTime.Now - response.create_time;
                response.elapsedTime = (int)ts.TotalMilliseconds;
                _ = RedisKit.RPushAsync(queue_parse_jd_key, response);

                if (!TestParseCore.InWhitelist(response.ip, response.oaid))
                {
                    await SaveParseCacheAsync(response.channel.ToString(), response.accountId,
                        response.accountName, response.success, response.message, response.reason,
                        response.deeplink_url);
                }

                if (response.success) await saveClientRequestTotalAsync(response.channel, response.ip, response.oaid);
                if (response.success || response.message.Equals("转链失败"))
                {
                    await RiskControlCore.CallsIncrByAsync(response.channel, response.accountId);
                    //saveClientRequestTotal(response.channel, response.ip, response.oaid);
                }

                //if (!response.success && ("nologin".Equals(response.reason) ||
                //    "方法不存在".Equals(response.reason) ||
                //    "未登录".Equals(response.reason)))
                //{
                //    await JdPoolCore.DisabledAsync(response.accountId, response.accountName, $"{response.rawContent}\n{response.rawContent2}");
                //}

                if ("没有匹配账号".Equals(response.reason))
                {
                    await JdPoolCore.AccountExhaustedAsync();
                }

                string failure_key = $":total:continuous_failure:{response.accountId}";
                string failure_lock_key = $":lock:continuous_failure:{response.accountId}";

                if (response.success) await RedisKit.DelAsync(failure_key);

                //连续错误发送报警
                if ("签名失败".Equals(response.reason) || "网络异常".Equals(response.reason))
                {
                    var total = await RedisKit.IncrByAsync(failure_key);
                    await RedisKit.ExpireAsync(failure_key, 3600);

                    if (total > 30)
                    {
                        //没有锁定记录才发送，
                        string lockfFlag = await RedisKit.GetAsync<string>(failure_lock_key);
                        if (string.IsNullOrEmpty(lockfFlag))
                        {
                            //发送锁，保存半小时（半小时内只发送1次）
                            await RedisKit.SetAsync(failure_lock_key, 1, 1800);

                            string message = $"【京东{response.accountName}】{EndPointCore.CurrentEndPoint}，连续的{response.reason}";
                            await RedisKit.ExpireAsync(failure_key, 3600);
                            NotifyCore.Notify(new NifyMessage
                            {
                                message = message,
                                priority = NifyMessagePriority.high,
                                tags = ["red_circle"]
                            });

                            if (!is_hide)
                            {
                                _ = NotifyCore.QYWeixinPushNotifyAsync(response.reason, message);
                            }
                        }
                    }
                }

                string nologin_key = $":total:continuous_nologin:{response.accountId}";
                string nologin_lock_key = $":lock:continuous_nologin:{response.accountId}";

                //连续错误发送报警
                if (!response.success && ("nologin".Equals(response.reason) ||
                    "方法不存在".Equals(response.reason) ||
                    "未登录".Equals(response.reason)))
                {
                    var total = await RedisKit.IncrByAsync(nologin_key);
                    await RedisKit.ExpireAsync(nologin_key, 3600);

                    if (total > 3)
                    {
                        //没有锁定记录才发送，
                        string lockfFlag = await RedisKit.GetAsync<string>(nologin_lock_key);
                        if (string.IsNullOrEmpty(lockfFlag))
                        {
                            await JdPoolCore.DisabledAsync(response.accountId, response.accountName, $"{response.rawContent}\n{response.rawContent2}");
                            await RedisKit.SetAsync(nologin_lock_key, 1, 60);
                            await RedisKit.ExpireAsync(nologin_key, 86400);
                        }
                    }
                    else
                    {
                        NotifyCore.Notify(new NifyMessage
                        {
                            message = $"【京东联盟{response.accountId}:{response.accountName}】cookie 掉线({total})\n\n{response.rawContent}",
                            priority = NifyMessagePriority.high,
                            tags = ["red_circle"]
                        });
                    }
                }
                else
                {
                    await RedisKit.DelAsync(nologin_key);
                }




            }
            catch (Exception ex) { }
        }

        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("proxy_name", data.proxy_name)
                .Add("proxy_node", data.proxy_node)
                .Add("curl_proxy", data.curl_proxy)
                .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("riskStrategy", data.riskStrategy)
                .Add("launchScene", data.launchScene)
                .Add("create_time", data.create_time)
                .Add("parse_type", data.parse_type)
                .Create(DBContext.InsertType.NORMAL, transaction);
        }

    }

}
