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

namespace molilian.core
{
    public partial class TkLogCore
    {

        static string queue_deeplink_parse_key = "queue:parse_logs:deeplink";

        public static int task_insert_parse_deeplink_logs(IDbConnection connection, IDbTransaction transaction, int limit, CSRedisClient redis)
        {
            int total = 0;

            for (int i = 0; i < limit; i++)
            {
                var data = redis.LPop<DeeplinkParseDataDTO>(queue_deeplink_parse_key);
                if (data == null) break;

                if (_test_oaid.Equals(data.oaid) ||
                     data.ip.Contains("127.0.0"))
                {
                    var test_data = data.Convert2Json().Convert2Object<TestDeeplinkParseDataDTO>();
                    connection.Insert(test_data);
                }
                else
                {
                    connection.Insert(data);
                    if (!data.success)
                    {
                        var success_data = data.Convert2Json().Convert2Object<FilDeeplinkParseDataDTO>();
                        connection.Insert(success_data);
                    }
                    else
                    {
                        var success_data = data.Convert2Json().Convert2Object<SuccessDeeplinkParseDataDTO>();
                        connection.Insert(success_data);
                    }
                }
                total++;
            }
            return total;
        }
        public static async Task ParseLogAsync(DeeplinkParseDataDTO response)
        {
            try
            {
                var ts = DateTime.Now - response.create_time;
                response.elapsedTime = (int)ts.TotalMilliseconds;
                _ = RedisHelper.RPushAsync(queue_deeplink_parse_key, response);

                if (!response.ip.Contains("127.0.0"))
                {
                    saveParseCache(response.channel_name, 0, "tool",
                        response.success, response.message, response.reason,
                        response.deeplink_url);
                }
            }
            catch (Exception ex) { }
        }

    }

}
