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

namespace molilian.core
{
    public partial class TkLogCore
    {

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

        public static int task_insert_parse_tb_logs(IDbConnection connection, IDbTransaction transaction, int limit, CSRedisClient redis)
        {
            int total = 0;
            for (int i = 0; i < limit; i++)
            {
                var data = redis.LPop<TkDataDTO>(queue_parse_tb_key);
                if (data == null) break;

                if (_test_oaid.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 (save_dailys_log)
                    {
                        string daily_table = $"tk_parse_logs_{DateTime.Now: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.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++;
            }
            return total;
        }

        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,
                        response.deeplink_url);
                }


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


    }

}
