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

namespace molilian.core
{
    public partial class TkLogCore
    {

        static string queue_parse_tb_key = "queue:parse_logs:tb";

        public static async Task<int> InsertParseTkLogAsync(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<TkDataDTO>(queue_parse_tb_key);
                    if (data == null) break;

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

                            if (data.success)
                            {
                                save_tk_parse_logs(data, "tk_dp_logs_success", connection, transaction);
                            }
                        }

                        ////每日分表
                        //if (save_dailys_log)
                        {
                            string daily_table = $"tk_parse_logs_{data.create_time:yyyyMMdd}";
                            save_tk_parse_logs(data, daily_table, connection, transaction);
                        }

                        if (data.elapsedTime > 1000)
                        {
                            save_tk_parse_logs(data, "tk_parse_logs_test", connection, transaction);
                        }

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

                        if (data.success)
                        {
                            save_tk_parse_logs(data, "tk_success_parse_logs", connection, transaction);
                        }
                        //data.id = save_tk_parse_logs(data, "tk_parse_logs", connection, transaction);

                        if (!string.IsNullOrEmpty(data.itemId)) TkOrderTrackingCore.SaveLinkSummary(data);
                    }

                    total++;
                }
                transaction.Commit();
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                new LoggerLibrary("TkLogCore_error", "task_insert_parse_tb_logs")
                      .Info(ex.Message, ex.StackTrace)
                      .SaveAsync();
                throw;
            }
            finally
            {
                connection.Close();
            }
            return total;
        }

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

                if (response.success || response.message.Equals("转链失败") || response.message.Equals("放弃转链"))
                {
                    await RiskControlCore.CallsIncrByAsync(response.channel, response.accountId);
                    await saveClientRequestTotalAsync(response.channel, response.ip, response.oaid);
                }
                if (response.success)
                {
                    _ = saveUnionCouponParseCacheAsync(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);
                }

                var account = await TkPoolCore.ALLGetOneAsync(response.accountId);
                if (!string.IsNullOrEmpty(response.itemId)) TkOrderTrackingCore.SaveLinkSummary(response, account);

                bool is_hide = false;
                if (account != null) is_hide = account.is_hide;

                if (!response.success && "nologin".Equals(response.message))
                {
                    switch (response.channel)
                    {
                        case TkChannelEnum.tb:
                            if (alimamaPlus != null)
                            {
                                (bool success, string message) = alimamaPlus.RenewCookie();
                                if (success) return;
                            }
                            TkPoolCore.Disabled(response.accountId, response.accountName, $"{response.rawContent}", is_hide);
                            break;
                    }
                }
                if (response.subCode == TkSubCodeEnum.ParseDeny && "接口风控ALL".Equals(response.reason))
                {
                    TkPoolCore.Disabled(response.accountId, response.accountName, "接口风控NONE", is_hide);
                    //todo  计数器+1
                }

                if (response.subCode == TkSubCodeEnum.Captcha || "霸下验证码".Equals(response.reason))
                {
                    TkPoolCore.Suspend(response.end_point, response.accountId, response.accountName, "霸下验证码");
                }
            }
            catch (Exception ex)
            {
                _ = new LoggerLibrary("unionParse", "database_error")
                .Info(response.rawContent)
                .Info(response.Convert2Json())
                .Info(ex.Message, ex.StackTrace)
                .SaveAsync();
            }
        }
        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("proxy_node", data.proxy_node)
                .Add("tkEndpoint", data.tkEndpoint)
                .Add("rawContent", data.rawContent)
                .Add("success", data.success)
                .Add("message", data.message)
                .Add("reason", data.reason)
                .Add("content", data.content)
                .Add("mktId", data.mktId)
                .Add("itemId", data.itemId)
                .Add("itemName", data.itemName)
                .Add("pic", data.pic)
                .Add("couponAmount", data.couponAmount)
                .Add("promotionPrice", data.promotionPrice)
                .Add("reservePrice", data.reservePrice)
                .Add("zkFinalPrice", data.zkFinalPrice)
                .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("retry_count", data.retry_count)
                .Add("subCode", data.subCode)
                .Add("ip", data.ip)
                .Add("oaid", data.oaid)
                .Add("riskStrategy", data.riskStrategy)
                .Add("launchScene", data.launchScene)
                .Add("item_url", data.item_url)
                .Add("item_deeplink_url", data.item_deeplink_url)

                .Add("create_time", data.create_time)
                .Add("parse_type", data.parse_type)
                .Add("remote_response", data.remote_response)
                .Add("commercial", data.commercial)
                .Add("with_middle_page", data.with_middle_page)
                .Create(DBContext.InsertType.NORMAL, transaction);
        }


    }

}
