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

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

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


                if (_test_oaid.Equals(data.oaid) || data.ip.Contains("127.0.0"))
                {
                    save_ks_parse_logs(data, "ks_parse_logs_test", connection, transaction);
                }
                else
                {
                    save_ks_parse_logs(data, "ks_parse_logs", connection, transaction);

                    if (save_dailys_log)
                    {
                        string daily_table = $"ks_parse_logs_{DateTime.Now:yyyyMMdd}";
                        save_ks_parse_logs(data, daily_table, connection, transaction);
                    }

                    if (data.success)
                    {
                        save_ks_parse_logs(data, "ks_parse_logs_success", connection, transaction);
                    }
                }
                total++;
            }
            return total;
        }

        public static async Task ParseLogAsync(KsDataDTO response)
        {
            try
            {
                var ts = DateTime.Now - response.create_time;
                response.elapsedTime = (int)ts.TotalMilliseconds;
                _ = RedisHelper.RPushAsync(queue_parse_ks_key, 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 (response.success || response.message.Equals("转链失败"))
                {
                    KsPoolCore.CallsIncrBy(response.accountId);
                }


                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(() =>
                    {
                        KsPoolCore.Disabled(response.accountId, response.accountName, $"{response.rawContent}\n{response.rawContent2}");
                    });
                }
                if ("没有匹配账号".Equals(response.reason))
                {
                    KsPoolCore.AccountExhausted();
                }
            }
            catch (Exception ex) { }
        }
        private static int save_ks_parse_logs(KsDataDTO 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);
        }

    }

}
