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

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

        public static async Task<int> InsertToolLogAsync(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<ToolParseDataDTO>(queue_parse_tool_key);
                    if (data == null) break;


                    if (TestParseCore.InWhitelist(data.ip, data.oaid))
                    {
                        new DBContext.Table(connection, "tool_parse_logs_test")
                            .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);
                    }
                    else
                    {
                        string daily_table = $"tool_parse_logs_{data.create_time:yyyyMMdd}";
                        new DBContext.Table(connection, daily_table)
                            .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++;
                }
                transaction.Commit();
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                new LoggerLibrary("TkLogCore_error", "task_insert_parse_tool_logs")
                      .Info(ex.Message, ex.StackTrace)
                      .SaveAsync();
                throw;
            }
            finally
            {
                connection.Close();
            }
            return total;
        }

        public static async Task ParseLogAsync(ToolParseDataDTO response)
        {
            try
            {
                var ts = DateTime.Now - response.create_time;
                response.elapsedTime = (int)ts.TotalMilliseconds;

                _ = RedisKit.RPushAsync(queue_parse_tool_key, response);

                if (!TestParseCore.InWhitelist(response.ip, response.oaid))
                {
                    await SaveParseCacheAsync(response.channel.ToString(), 0, "tool",
                        response.success, response.message, response.reason,
                        response.deeplink_url);
                }
            }
            catch (Exception ex) { }
        }
    }

}
