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

namespace molilian.core
{
    public partial class TkLogCore
    {
        static string queue_cps_key = "queue:cps_logs";


        public static async Task<int> InsertCpsLogAsync(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<UnionCpsDTO>(queue_cps_key);
                    if (data == null) break;

                    if (TestParseCore.InWhitelist(data.ip, data.oaid))
                    {
                        var test_data = data.Convert2Json().Convert2Object<TestUnionCpsDTO>();
                        connection.Insert(test_data);
                    }
                    else
                    {
                        //按日保存数据
                        string daily_table = $"tk_cps_logs_{data.create_time:yyyyMMdd}";
                        save_cps_parse_logs(data, daily_table, connection, transaction);
                    }
                    total++;
                }
                transaction.Commit();
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                new LoggerLibrary("TkLogCore_error", "task_insert_parse_cps_logs")
                      .Info(ex.Message, ex.StackTrace)
                      .SaveAsync();
                throw;
            }
            finally
            {
                connection.Close();
            }
            return total;
        }

        public static int task_insert_parse_cps_logs(int limit, CSRedisClient 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 = redis.LPop<UnionCpsDTO>(queue_cps_key);
                    if (data == null) break;

                    if (TestParseCore.InWhitelist(data.ip, data.oaid))
                    {
                        var test_data = data.Convert2Json().Convert2Object<TestUnionCpsDTO>();
                        connection.Insert(test_data);
                    }
                    else
                    {
                        //按日保存数据
                        string daily_table = $"tk_cps_logs_{data.create_time:yyyyMMdd}";
                        save_cps_parse_logs(data, daily_table, connection, transaction);
                    }
                    total++;
                }
                transaction.Commit();
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                new LoggerLibrary("TkLogCore_error", "task_insert_parse_cps_logs")
                      .Info(ex.Message, ex.StackTrace)
                      .SaveAsync();
                throw;
            }
            finally
            {
                connection.Close();
            }
            return total;
        }

        private static int save_cps_parse_logs(UnionCpsDTO data, string tablename, IDbConnection connection, IDbTransaction transaction)
        {
            return new DBContext.Table(connection, tablename)
                .Add("end_point", data.end_point)
                .Add("channel", data.channel)
                .Add("accountId", data.accountId)
                .Add("accountName", data.accountName)
                .Add("proxy_node", data.proxy_node)
                .Add("rawContent", data.rawContent)
                .Add("rawContent2", data.rawContent2)
                .Add("success", data.success)
                .Add("message", data.message)
                .Add("reason", data.reason)
                .Add("deeplink_url", data.deeplink_url)
                .Add("create_time", data.create_time)
                .Add("elapsedTime", data.elapsedTime)
                .Add("ip", data.ip)
                .Add("oaid", data.oaid)
                .Add("token", data.token)
                .Add("category", data.category)
                .Add("pid", data.pid)
                .Add("title", data.title)
                .Add("sub_title", data.sub_title)
                .Add("itemId", data.itemId)
                .Add("couponAmount", data.couponAmount)
                .Create(DBContext.InsertType.NORMAL, transaction);
        }


        public static async Task CpsLogAsync(UnionCpsDTO response, AlimamaPlus? alimamaPlus = null)
        {
            try
            {
                var ts = DateTime.Now - response.create_time;
                response.elapsedTime = (int)ts.TotalMilliseconds;
                _ = RedisHelper.RPushAsync(queue_cps_key, response);

                if (response.success)
                {
                    await RiskControlCore.CallsIncrByAsync(TkChannelEnum.cps, response.accountId);
                    await saveClientRequestTotalAsync(response.channel, response.ip, response.oaid);
                }

                if (!TestParseCore.InWhitelist(response.ip, response.oaid))
                {
                    saveCpsCache(response.channel, response.accountId,
                        response.success, response.message, response.reason);
                }
            }
            catch (Exception ex)
            {
                _ = new LoggerLibrary("unionCps", "database_error")
                .Info(response.rawContent)
                .Info(response.Convert2Json())
                .Info(ex.Message, ex.StackTrace)
                .SaveAsync();
            }
        }

        private static void saveCpsCache(string channel, int accountId, bool success, string message, string reason)
        {
            saveAccountCpsCache("all", success, message, reason);
            saveAccountCpsCache($"{channel}", success, message, reason);
            if (accountId != 0)
            {
                saveAccountCpsCache($"{channel}_{accountId}", success, message, reason);
            }
        }

        private static void saveAccountCpsCache(string flagName, bool success, string message, string reason)
        {
            SaveStatsAccountCache("cps_total", flagName, success, message, reason);
        }




    }

}
