using dodohold.core;
using System.Text.Json;
using TencentCloud.Cdn.V20180606.Models;
using TencentCloud.Tse.V20201207.Models;
using static molilian.core.ChartDataCore;


namespace molilian.core
{
    public partial class AlimamaPlus
    {

        /// <summary>
        /// 更新API调用日志（1天一次）
        /// </summary>
        /// <param name="intervalDay"></param>
        public static void DailyLogs_bak(int intervalDay, string prefix = "", string channel = "all")
        {
            int channelId = (int)TkChannelEnum.tb;
            var db_prefix = channel switch
            {
                "tool" => "tool_",
                "jd" => "jd_parse_",
                _ => prefix,
            };

            DateTime log_date = DateTime.Now.AddDays(-intervalDay).Date;

            var accounts = TkPoolCore.List();
            using var conn = DBContext.GetOpenConnection();

            foreach (var account in accounts)
            {
                int accountId = account.id;
                string accountName = $"{TkChannelEnum.tb}_{account.id}";

                int total_count = TkLogCore.GetTotal($":{prefix}total:{accountName}:{log_date:yyyyMMdd}");
                if (total_count == 0) accountName = account.name;

                total_count = TkLogCore.GetTotal($":{prefix}total:{accountName}:{log_date:yyyyMMdd}");
                if (total_count == 0) continue;

                var success_count = TkLogCore.GetTotal($":{prefix}total:{accountName}:success:{log_date:yyyyMMdd}");
                var abandon_count = TkLogCore.GetTotal($":{prefix}total:{accountName}:放弃转链:{log_date:yyyyMMdd}");
                string success_percentage = string.Empty;
                string abandon_percentage = string.Empty;

                if (total_count > 0)
                {
                    success_percentage = $"{success_count / (double)total_count * 100:f2}%";
                    abandon_percentage = $"{abandon_count / (double)total_count * 100:f2}%";
                }

                var exist = new DBContext.Table("daily_logs")
                     .Fields("id,log_date")
                     .Get<dynamic>("log_date=@log_date AND channel=@channel AND accountId=@accountId", new { log_date, channel = channelId, accountId });


                var createArgs = new DBContext.Table(conn, "daily_logs")

                    .Add($"{db_prefix}total_count", total_count)
                    .Add($"{db_prefix}abandon_count", abandon_count)
                    .Add($"{db_prefix}abandon_percentage", abandon_percentage)
                    .Add($"{db_prefix}success_count", success_count)
                    .Add($"{db_prefix}success_percentage", success_percentage)
                    .Add("last_time", DateTime.Now);

                if (exist == null)
                {
                    createArgs.Add("channel", channelId)
                        .Add("accountId", accountId)
                        .Add("accountName", account.company)
                        .Add("log_date", log_date)
                        .Add("create_time", DateTime.Now)
                        .Create();
                }
                else
                {
                    createArgs.Where("id=@id", new { exist.id }).Update();
                }
            }

            int all_total_count = TkLogCore.GetTotal($":{prefix}total:{channel}:{log_date:yyyyMMdd}");
            var all_success_count = TkLogCore.GetTotal($":{prefix}total:{channel}:success:{log_date:yyyyMMdd}");
            var all_abandon_count = TkLogCore.GetTotal($":{prefix}total:{channel}:放弃转链:{log_date:yyyyMMdd}");

            string all_success_percentage = string.Empty;
            string all_abandon_percentage = string.Empty;
            if (all_total_count > 0)
            {
                all_success_percentage = $"{all_success_count / (double)all_total_count * 100:f2}%";
                all_abandon_percentage = $"{all_abandon_count / (double)all_total_count * 100:f2}%";
            }
            var exist2 = new DBContext.Table(conn, "daily_logs")
                 .Fields("id,log_date")
                 .Get<dynamic>("log_date=@log_date AND channel=@channel AND accountId=@accountId",
                 new { log_date, channel = channelId, accountId = 0 });

            var createArgs2 = new DBContext.Table(conn, "daily_logs")
                .Add($"{db_prefix}total_count", all_total_count)
                .Add($"{db_prefix}abandon_count", all_abandon_count)
                .Add($"{db_prefix}abandon_percentage", all_abandon_percentage)
                .Add($"{db_prefix}success_count", all_success_count)
                .Add($"{db_prefix}success_percentage", all_success_percentage)
                .Add("last_time", DateTime.Now);

            if (exist2 == null)
            {
                createArgs2.Add("channel", channelId)
                    .Add("accountId", 0)
                    .Add("accountName", "all")
                    .Add("log_date", log_date)
                    .Add("create_time", DateTime.Now)
                    .Create();
            }
            else
            {
                createArgs2.Where("id=@id", new { exist2.id }).Update();
            }

        }


        public static void AllDailyLogs(int intervalDay, string prefix = "", string channel = "all")
        {
            DailyLogs(true, intervalDay, prefix, channel);
        }


        public static void NodeDailyLogs(int intervalDay, string prefix = "", string channel = "all")
        {
            DailyLogs(false, intervalDay, prefix, channel);
        }

        public static void DailyLogs(bool all_node, int intervalDay, string prefix = "", string channel = "all")
        {
            int channelId = (int)TkChannelEnum.tb;
            var db_prefix = channel switch
            {
                "tool" => "tool_",
                "jd" => "jd_parse_",
                _ => prefix,
            };

            DateTime log_date = DateTime.Now.AddDays(-intervalDay).Date;

            var accounts = TkPoolCore.List();
            using var conn = all_node ? CenterHub.GetOpenConnection() : DBContext.GetOpenConnection();
            string tableName = all_node ? "center_daily_logs" : "daily_logs";
            string tableName2 = all_node ? "center_daily_chat_data" : "daily_chat_data";

            foreach (var account in accounts)
            {
                int accountId = account.id;
                string accountName = $"{TkChannelEnum.tb}_{account.id}";

                int total_count = TkLogCore.GetTotal($":{prefix}total:{accountName}:{log_date:yyyyMMdd}", all_node);
                if (total_count == 0) accountName = account.name;

                total_count = TkLogCore.GetTotal($":{prefix}total:{accountName}:{log_date:yyyyMMdd}", all_node);
                if (total_count == 0) continue;

                var success_count = TkLogCore.GetTotal($":{prefix}total:{accountName}:success:{log_date:yyyyMMdd}", all_node);
                var abandon_count = TkLogCore.GetTotal($":{prefix}total:{accountName}:放弃转链:{log_date:yyyyMMdd}", all_node);
                string success_percentage = string.Empty;
                string abandon_percentage = string.Empty;

                if (total_count > 0)
                {
                    success_percentage = $"{success_count / (double)total_count * 100:f2}%";
                    abandon_percentage = $"{abandon_count / (double)total_count * 100:f2}%";
                }

                var exist = new DBContext.Table(tableName)
                     .Fields("id,log_date")
                     .Get<dynamic>("log_date=@log_date AND channel=@channel AND accountId=@accountId", new { log_date, channel = channelId, accountId });


                var createArgs = new DBContext.Table(conn, tableName)

                    .Add($"{db_prefix}total_count", total_count)
                    .Add($"{db_prefix}abandon_count", abandon_count)
                    .Add($"{db_prefix}abandon_percentage", abandon_percentage)
                    .Add($"{db_prefix}success_count", success_count)
                    .Add($"{db_prefix}success_percentage", success_percentage)
                    .Add("last_time", DateTime.Now);

                if (exist == null)
                {
                    createArgs.Add("channel", channelId)
                        .Add("accountId", accountId)
                        .Add("accountName", account.company)
                        .Add("log_date", log_date)
                        .Add("create_time", DateTime.Now)
                        .Create();
                }
                else
                {
                    createArgs.Where("id=@id", new { exist.id }).Update();
                }
            }

            int all_total_count = TkLogCore.GetTotal($":{prefix}total:{channel}:{log_date:yyyyMMdd}", all_node);
            var all_success_count = TkLogCore.GetTotal($":{prefix}total:{channel}:success:{log_date:yyyyMMdd}", all_node);
            var all_abandon_count = TkLogCore.GetTotal($":{prefix}total:{channel}:放弃转链:{log_date:yyyyMMdd}", all_node);

            string all_success_percentage = string.Empty;
            string all_abandon_percentage = string.Empty;
            if (all_total_count > 0)
            {
                all_success_percentage = $"{all_success_count / (double)all_total_count * 100:f2}%";
                all_abandon_percentage = $"{all_abandon_count / (double)all_total_count * 100:f2}%";
            }
            var exist2 = new DBContext.Table(conn, tableName)
                 .Fields("id,log_date")
                 .Get<dynamic>("log_date=@log_date AND accountId=@accountId",
                 new { log_date, channel = channelId, accountId = 0 });

            var createArgs2 = new DBContext.Table(conn, tableName)
                .Add($"{db_prefix}total_count", all_total_count)
                .Add($"{db_prefix}abandon_count", all_abandon_count)
                .Add($"{db_prefix}abandon_percentage", all_abandon_percentage)
                .Add($"{db_prefix}success_count", all_success_count)
                .Add($"{db_prefix}success_percentage", all_success_percentage)
                .Add("last_time", DateTime.Now);

            if (exist2 == null)
            {
                createArgs2.Add("channel", 0)
                    .Add("accountId", 0)
                    .Add("accountName", "all")
                    .Add("log_date", log_date)
                    .Add("create_time", DateTime.Now)
                    .Create();
            }
            else
            {
                createArgs2.Where("id=@id", new { exist2.id }).Update();
            }

            //jd_parse_total
            //tool_total
            //total
            //coupon_total
            //parse_total

            var chatData = ChartDataCore.GetTaobaoData($"{prefix}total", "", $"{log_date:yyyyMMdd}");
            //tableName2
        }

        public static DBContext.Table FillLinkTypeData(DBContext.Table update, int data_type, DateTime report_date)
        {
            long req_other_aff_num = 0;//1
            long req_goods_num = 0;//2
            long req_live_num = 0;//3
            long req_video_num = 0;///4
            long req_shop_num = 0;//5
            long req_base_num = 0;//6
            long req_note_num = 0;//7
            int resp_deeplink_num = 0;

            try
            {
                string tableName = $"tk_parse_logs_{report_date:yyyyMMdd}";

                string exist_sql = @$"SELECT IF( EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_name = '{tableName}'), 1, 0) AS table_exists;";

                int exist_table = DBContext.ExecuteScalar<int>(exist_sql);
                if (exist_table == 1)
                {
                    var linkTypeTotal = new DBContext.Table(tableName)
                                    .Fields("linkType, count(*) AS total")
                                    .GroupBy("linkType")
                                    .Select<dynamic>();

                    foreach (var t in linkTypeTotal)
                    {
                        int linkType = t.linkType;
                        long total = t.total;
                        switch (linkType)
                        {
                            case 1: req_other_aff_num = total; break;
                            case 2: req_goods_num = total; break;
                            case 3: req_live_num = total; break;
                            case 4: req_video_num = total; break;
                            case 5: req_shop_num = total; break;
                            case 6: req_base_num = total; break;
                            case 7: req_note_num = total; break;
                        }
                    }
                }

                string total_key = data_type switch
                {
                    1 => "parse_total",
                    2 or _ => "coupon_total",
                };

                string[] deeplink_keys = ["home", "success", "fail"];
                string accountName = "all";
                foreach (var keyname in deeplink_keys)
                {
                    string cacheKey = $":{total_key}:dp_{keyname}:{accountName}:{report_date:yyyyMMdd}";
                    int value = TkLogCore.GetTotal(cacheKey);
                    if (value == 0) continue;
                    resp_deeplink_num += value;
                }
            }
            catch (Exception e)
            {
            }
            update.Add("req_other_aff_num", req_other_aff_num);
            update.Add("req_goods_num", req_goods_num);
            update.Add("req_live_num", req_live_num);
            update.Add("req_video_num", req_video_num);
            update.Add("req_shop_num", req_shop_num);
            update.Add("req_base_num", req_base_num);
            update.Add("req_note_num", req_note_num);
            update.Add("resp_deeplink_num", resp_deeplink_num);

            return update;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="data_type">1转链 2优惠券</param>
        /// <param name="intervalDay"></param>
        /// <param name="channel"></param>
        public static void ApiCallStatistics(int data_type, int intervalDay, TkChannelEnum channel)
        {
            DateTime log_date = DateTime.Now.AddDays(-intervalDay).Date;

            var endpoint = data_type == 1 ? EndPointCore.GetParseAdmin() : EndPointCore.GetCouponAdmin();
            if (endpoint == null || string.IsNullOrEmpty(endpoint.db_server)) return;

            using var conn = EndPointCore.GetDbConnection(endpoint.db_server);
            using var center_conn = CenterHub.GetOpenConnection();

            var exist_report_dailys = new DBContext.Table(conn, "v_tk_report_dailys")
                 .Get<dynamic>("report_date=@log_date", new { log_date });


            var record = new DBContext.Table(center_conn, "api_call_report_dailys")
                 .Fields("id, report_date, read_num")
                 .Get<dynamic>("report_date=@log_date AND data_type=@data_type", new { log_date, data_type });

            var update = new DBContext.Table(center_conn, "api_call_report_dailys")
                .Add("data_type", data_type)
                .Add("is_show", 0)
                .Add("report_date", log_date)
                .Add("last_time", DateTime.Now);

            if (exist_report_dailys != null)
            {
                update.Add("uclk_uv", exist_report_dailys.uclk_uv_56)
                    .Add("uclk_pv", exist_report_dailys.uclk_pv_56)
                    .Add("pay_ord_num", exist_report_dailys.pay_ord_num_56)
                    .Add("order_ord_num", exist_report_dailys.order_ord_num)
                    .Add("pay_ord_amt", exist_report_dailys.pay_ord_amt_56)
                    .Add("pay_tk_disp_tfee", exist_report_dailys.pay_tk_disp_tfee_56)
                    .Add("pay_ord_uv", exist_report_dailys.pay_ord_uv_56)
                    .Add("eff_ord_num", exist_report_dailys.eff_ord_num)
                    .Add("eff_ord_amt", exist_report_dailys.eff_ord_amt)
                    .Add("eff_tk_disp_tfee", exist_report_dailys.eff_tk_disp_tfee);
            }

            var exist = new DBContext.Table(center_conn, "center_daily_logs")
                 .Get<dynamic>("log_date=@log_date AND accountId=@accountId",
                 new { log_date, channel, accountId = 0 });
            if (exist != null)
            {
                if (data_type == 1)
                {
                    update.Add("total_count", exist.parse_total_count)
                        .Add("abandon_count", exist.parse_abandon_count)
                        .Add("success_count", exist.parse_success_count);
                }
                if (data_type == 2)
                {
                    update.Add("total_count", exist.coupon_total_count)
                        .Add("abandon_count", exist.coupon_abandon_count)
                        .Add("success_count", exist.coupon_success_count);
                }
            }

            if (data_type == 1)
            {
                //todo 只有转链需要处理，优惠券和京东（未上线）暂不处理
                update = FillLinkTypeData(update, data_type, log_date);
            }
            if (record == null)
            {
                update.Add("read_num", 0).Add("create_time", DateTime.Now).Create();
            }
            else
            {
                update.Add("read_num", record.read_num).Where("id=@id", new { record.id }).Update();
            }
        }


        //public static void ApiCallStatistics(int intervalDay, TkChannelEnum channel, string prefix = "")
        //{

        //    DateTime log_date = DateTime.Now.AddDays(-intervalDay).Date;

        //    var accounts = TkPoolCore.List();

        //    foreach (var account in accounts)
        //    {
        //        int accountId = account.id;
        //        string accountName = $"{channel}_{account.id}";

        //        int total_count = TkLogCore.GetTotal($":{prefix}total:{accountName}:{log_date:yyyyMMdd}");
        //        if (total_count == 0) continue;

        //        var success_count = TkLogCore.GetTotal($":{prefix}total:{accountName}:success:{log_date:yyyyMMdd}");
        //        var abandon_count = TkLogCore.GetTotal($":{prefix}total:{accountName}:放弃转链:{log_date:yyyyMMdd}");
        //        string success_percentage = string.Empty;
        //        string abandon_percentage = string.Empty;

        //        if (total_count > 0)
        //        {
        //            success_percentage = $"{success_count / (double)total_count * 100:f2}%";
        //            abandon_percentage = $"{abandon_count / (double)total_count * 100:f2}%";
        //        }

        //        var exist = new DBContext.Table("api_call_statistics")
        //             .Fields("id,log_date")
        //             .Get<dynamic>("log_date=@log_date AND channel=@channel AND accountId=@accountId",
        //             new { log_date, channel, accountId });

        //        var createArgs = new DBContext.Table("api_call_statistics")

        //            .Add($"{prefix}total_count", total_count)
        //            .Add($"{prefix}abandon_count", abandon_count)
        //            .Add($"{prefix}abandon_percentage", abandon_percentage)
        //            .Add($"{prefix}success_count", success_count)
        //            .Add($"{prefix}success_percentage", success_percentage)
        //            .Add("last_time", DateTime.Now);

        //        if (exist == null)
        //        {
        //            createArgs.Add("channel", channel)
        //                .Add("accountId", accountId)
        //                .Add("accountName", account.company)
        //                .Add("log_date", log_date)
        //                .Add("create_time", DateTime.Now)
        //                .Create();
        //        }
        //        else
        //        {
        //            createArgs.Where("id=@id", new { exist.id }).Update();
        //        }
        //    }


        //    int all_total_count = TkLogCore.GetTotal($":{prefix}total:{channel.ToString()}:{log_date:yyyyMMdd}");
        //    var all_success_count = TkLogCore.GetTotal($":{prefix}total:{channel.ToString()}:success:{log_date:yyyyMMdd}");
        //    var all_abandon_count = TkLogCore.GetTotal($":{prefix}total:{channel.ToString()}:放弃转链:{log_date:yyyyMMdd}");

        //    string all_success_percentage = string.Empty;
        //    string all_abandon_percentage = string.Empty;
        //    if (all_total_count > 0)
        //    {
        //        all_success_percentage = $"{all_success_count / (double)all_total_count * 100:f2}%";
        //        all_abandon_percentage = $"{all_abandon_count / (double)all_total_count * 100:f2}%";
        //    }

        //    var exist2 = new DBContext.Table("api_call_statistics")
        //         .Fields("id,log_date")
        //         .Get<dynamic>("log_date=@log_date AND channel=@channel AND accountId=@accountId",
        //         new { log_date, channel, accountId = 0 });

        //    var createArgs2 = new DBContext.Table("api_call_statistics")
        //        .Add($"{prefix}total_count", all_total_count)
        //        .Add($"{prefix}abandon_count", all_abandon_count)
        //        .Add($"{prefix}abandon_percentage", all_abandon_percentage)
        //        .Add($"{prefix}success_count", all_success_count)
        //        .Add($"{prefix}success_percentage", all_success_percentage)
        //        .Add("last_time", DateTime.Now);

        //    if (exist2 == null)
        //    {
        //        createArgs2.Add("channel", channel)
        //            .Add("accountId", 0)
        //            .Add("accountName", "all")
        //            .Add("log_date", log_date)
        //            .Add("create_time", DateTime.Now)
        //            .Create();
        //    }
        //    else
        //    {
        //        createArgs2.Where("id=@id", new { exist2.id }).Update();
        //    }

        //}

        /// <summary>
        /// 事实动态接口（5分钟一次）
        /// </summary>
        /// <param name="date"></param>
        public void SaveReportOverview()
        {
            DateTime date = DateTime.Now;
            TkReportDTO result = new TkReportDTO();
            try
            {
                string t = $"{date.Convert2UnixTimestamp(true)}";
                string dt = date.ToString("yyyy-MM-dd");

                string url = "https://pub.alimama.com/openapi/param2/1/gateway.unionpub/data.lens.report.overview.json?" +
                    $"t={t}&_tb_token_={_tb_token}&ruleEnum=pub_overview&startDate={dt}&endDate={dt}&fixPeriod=1d";

                var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
                            .AddHeaders("X-Requested-With", "XMLHttpRequest")
                            .AddHeaders("Cookie", _cookies);
#if DEBUG
#else
            client.Proxy = _proxy;
#endif
                if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
                var response = client.Request(url);

                var body = response.Body();
                var root = body.Convert2JsonElement();

                int resultCode = root.Read<int>("resultCode", 0);
                bool success = root.Read("success", false);
                if (!success)
                {
                    _ = new LoggerLibrary("report", "error")
                        .Info(url, body)
                        .SaveAsync();
                    throw new Exception(body);
                }

                var data = root.ElementRead("data").ElementRead("data");

                result.eff_ord_num = data.Read<int>("eff_ord_num", 0);
                result.eff_ord_amt = data.Read<decimal>("eff_ord_amt", 0);
                result.eff_tk_disp_tfee = data.Read<decimal>("eff_tk_disp_tfee", 0);

                result.uclk_uv_56 = data.Read<int>("uclk_uv_56", 0);
                result.uclk_pv_56 = data.Read<int>("uclk_pv_56", 0);
                result.uclt_uv_56 = data.Read<int>("uclt_uv_56", 0);
                result.ucart_uv_56 = data.Read<int>("ucart_uv_56", 0);
                result.dep_ord_num_56 = data.Read<int>("dep_ord_num_56", 0);
                result.dep_ord_dep_amt_56 = data.Read<decimal>("dep_ord_dep_amt_56", 0);
                result.pay_ord_num_56 = data.Read<int>("pay_ord_num_56", 0);
                result.pay_ord_amt_56 = data.Read<decimal>("pay_ord_amt_56", 0);
                result.pay_tk_disp_tfee_56 = data.Read<decimal>("pay_tk_disp_tfee_56", 0);
                result.pay_ord_uv_56 = data.Read<int>("pay_ord_uv_56", 0);
                result.sett_ord_num_56 = data.Read<int>("sett_ord_num_56", 0);
                result.sett_ord_amt_56 = data.Read<decimal>("sett_ord_amt_56", 0);
                result.sett_tk_disp_tfee_56 = data.Read<decimal>("sett_tk_disp_tfee_56", 0);

                DateTime report_date = date.Date;
                if (report_date == DateTime.Now.Date)
                {
#if DEBUG
#else
                    TkPoolCore.SaveIncomeAmt($"{_accountId}", result.eff_tk_disp_tfee);
#endif
                    //TkPoolCore.SaveIncomeAmt(_accountName, result.eff_tk_disp_tfee);
                    EachSaveReportOverview(report_date.AddHours(DateTime.Now.Hour), "tk_report_hourtrend", result);
                    EachSaveReportOverview(report_date, "tk_report", result);
                }
            }
            catch (Exception ex) { throw; }
        }
        private void EachSaveReportOverview(DateTime report_date, string tableName, TkReportDTO data)
        {
            var exist = new DBContext.Table(tableName)
                 .Fields("id,report_date")
                 .Get<dynamic>("report_date=@report_date AND accountId=@accountId",
                                 new { report_date, accountId = _accountId });

            var createArgs = new DBContext.Table(tableName)
                .Add("uclk_uv_56", data.uclk_uv_56)
                .Add("uclk_pv_56", data.uclk_pv_56)
                .Add("uclt_uv_56", data.uclt_uv_56)
                .Add("ucart_uv_56", data.ucart_uv_56)
                .Add("dep_ord_num_56", data.dep_ord_num_56)
                .Add("dep_ord_dep_amt_56", data.dep_ord_dep_amt_56)
                .Add("pay_ord_num_56", data.pay_ord_num_56)
                .Add("pay_ord_amt_56", data.pay_ord_amt_56)
                .Add("pay_tk_disp_tfee_56", data.pay_tk_disp_tfee_56)
                .Add("pay_ord_uv_56", data.pay_ord_uv_56)
                .Add("sett_ord_num_56", data.sett_ord_num_56)
                .Add("sett_ord_amt_56", data.sett_ord_amt_56)
                .Add("sett_tk_disp_tfee_56", data.sett_tk_disp_tfee_56)
                .Add("eff_ord_num", data.eff_ord_num)
                .Add("eff_ord_amt", data.eff_ord_amt)
                .Add("eff_tk_disp_tfee", data.eff_tk_disp_tfee);

            string data_suffix = report_date.ToString("yyyyMMdd");
            if ("tk_report_hourtrend".Equals(tableName))
            {
                data_suffix = report_date.ToString("yyyyMMddHH");
            }

            string accountName = $"{TkChannelEnum.tb}_{_accountId}";
            int total_count = TkLogCore.GetTotal($":total:{accountName}:{data_suffix}");
            int success_count = 0, abandon_count = 0;
            string success_percentage = string.Empty, abandon_percentage = string.Empty;
            if (total_count > 0)
            {
                success_count = TkLogCore.GetTotal($":total:{accountName}:success:{data_suffix}");
                abandon_count = TkLogCore.GetTotal($":total:{accountName}:放弃转链:{data_suffix}");
                if (total_count > 0)
                {
                    success_percentage = $"{success_count / (double)total_count * 100:f2}%";
                    abandon_percentage = $"{abandon_count / (double)total_count * 100:f2}%";
                }
            }
            createArgs.Add("total_count", total_count);
            createArgs.Add("success_count", success_count);
            createArgs.Add("abandon_count", abandon_count);
            createArgs.Add("success_percentage", success_percentage);
            createArgs.Add("abandon_percentage", abandon_percentage);

            int parse_total_count = TkLogCore.GetTotal($":parse_total:{accountName}:{data_suffix}");
            int parse_success_count = 0, parse_abandon_count = 0;
            string parse_success_percentage = string.Empty, parse_abandon_percentage = string.Empty;
            if (parse_total_count > 0)
            {
                parse_success_count = TkLogCore.GetTotal($":parse_total:{accountName}:success:{data_suffix}");
                parse_abandon_count = TkLogCore.GetTotal($":parse_total:{accountName}:放弃转链:{data_suffix}");
                if (parse_total_count > 0)
                {
                    parse_success_percentage = $"{parse_success_count / (double)parse_total_count * 100:f2}%";
                    parse_abandon_percentage = $"{parse_abandon_count / (double)parse_total_count * 100:f2}%";
                }
            }
            createArgs.Add("parse_total_count", parse_total_count);
            createArgs.Add("parse_success_count", parse_success_count);
            createArgs.Add("parse_abandon_count", parse_abandon_count);
            createArgs.Add("parse_success_percentage", parse_success_percentage);
            createArgs.Add("parse_abandon_percentage", parse_abandon_percentage);


            int coupon_total_count = TkLogCore.GetTotal($":coupon_total:{accountName}:{data_suffix}", false);
            int coupon_success_count = 0, coupon_abandon_count = 0;
            string coupon_success_percentage = string.Empty, coupon_abandon_percentage = string.Empty;
            if (coupon_total_count > 0)
            {
                coupon_success_count = TkLogCore.GetTotal($":coupon_total:{accountName}:success:{data_suffix}", false);
                coupon_abandon_count = TkLogCore.GetTotal($":coupon_total:{accountName}:放弃转链:{data_suffix}", false);
                if (coupon_total_count > 0)
                {
                    coupon_success_percentage = $"{coupon_success_count / (double)coupon_total_count * 100:f2}%";
                    coupon_abandon_percentage = $"{coupon_abandon_count / (double)coupon_total_count * 100:f2}%";
                }
            }
            createArgs.Add("coupon_total_count", coupon_total_count);
            createArgs.Add("coupon_success_count", coupon_success_count);
            createArgs.Add("coupon_abandon_count", coupon_abandon_count);
            createArgs.Add("coupon_success_percentage", coupon_success_percentage);
            createArgs.Add("coupon_abandon_percentage", coupon_abandon_percentage);


            int tool_total_count = TkLogCore.GetTotal($":tool_total:{accountName}:{data_suffix}");
            int tool_success_count = 0, tool_abandon_count = 0;
            string tool_success_percentage = string.Empty, tool_abandon_percentage = string.Empty;
            if (tool_total_count > 0)
            {
                tool_success_count = TkLogCore.GetTotal($":tool_total:{accountName}:success:{data_suffix}");
                tool_abandon_count = TkLogCore.GetTotal($":tool_total:{accountName}:放弃转链:{data_suffix}");
                if (tool_total_count > 0)
                {
                    tool_success_percentage = $"{tool_success_count / (double)tool_total_count * 100:f2}%";
                    tool_abandon_percentage = $"{tool_abandon_count / (double)tool_total_count * 100:f2}%";
                }
            }
            createArgs.Add("tool_total_count", tool_total_count);
            createArgs.Add("tool_success_count", tool_success_count);
            createArgs.Add("tool_abandon_count", tool_abandon_count);
            createArgs.Add("tool_success_percentage", tool_success_percentage);
            createArgs.Add("tool_abandon_percentage", tool_abandon_percentage);

            if (exist == null)
            {
                createArgs.Add("accountId", _accountId)
                    .Add("accountName", _company)
                    .Add("report_date", report_date)
                    .Add("create_time", DateTime.Now)
                    .Add("last_time", DateTime.Now)
                    .Create();
            }
            else
            {
                createArgs.Add("last_time", DateTime.Now)
                    .Where("id=@id", new { exist.id })
                    .Update();
            }
        }

        /// <summary>
        /// 修正点击数
        /// </summary>
        public void FixReport(DateTime date)
        {
            TkReportDTO result = new TkReportDTO();
            try
            {
                string t = $"{date.Convert2UnixTimestamp(true)}";
                string dt = date.ToString("yyyy-MM-dd");

                string url = "https://pub.alimama.com/openapi/param2/1/gateway.unionpub/data.lens.report.overview.json?" +
                    $"t={t}&_tb_token_={_tb_token}&ruleEnum=pub_overview&startDate={dt}&endDate={dt}&fixPeriod=1d";

                var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
                            .AddHeaders("X-Requested-With", "XMLHttpRequest")
                            .AddHeaders("Cookie", _cookies);
#if DEBUG
#else
            client.Proxy = _proxy;
#endif
                if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
                var response = client.Request(url);

                var body = response.Body();
                var root = body.Convert2JsonElement();

                int resultCode = root.Read<int>("resultCode", 0);
                bool success = root.Read("success", false);
                if (!success)
                {
                    _ = new LoggerLibrary("report", "error")
                        .Info(url, body)
                        .SaveAsync();
                    throw new Exception(body);
                }

                var data = root.ElementRead("data").ElementRead("data");

                result.eff_ord_num = data.Read<int>("eff_ord_num", 0);
                result.eff_ord_amt = data.Read<decimal>("eff_ord_amt", 0);
                result.eff_tk_disp_tfee = data.Read<decimal>("eff_tk_disp_tfee", 0);

                result.uclk_uv_56 = data.Read<int>("uclk_uv_56", 0);
                result.uclk_pv_56 = data.Read<int>("uclk_pv_56", 0);
                result.uclt_uv_56 = data.Read<int>("uclt_uv_56", 0);
                result.ucart_uv_56 = data.Read<int>("ucart_uv_56", 0);
                result.dep_ord_num_56 = data.Read<int>("dep_ord_num_56", 0);
                result.dep_ord_dep_amt_56 = data.Read<decimal>("dep_ord_dep_amt_56", 0);
                result.pay_ord_num_56 = data.Read<int>("pay_ord_num_56", 0);
                result.pay_ord_amt_56 = data.Read<decimal>("pay_ord_amt_56", 0);
                result.pay_tk_disp_tfee_56 = data.Read<decimal>("pay_tk_disp_tfee_56", 0);
                result.pay_ord_uv_56 = data.Read<int>("pay_ord_uv_56", 0);
                result.sett_ord_num_56 = data.Read<int>("sett_ord_num_56", 0);
                result.sett_ord_amt_56 = data.Read<decimal>("sett_ord_amt_56", 0);
                result.sett_tk_disp_tfee_56 = data.Read<decimal>("sett_tk_disp_tfee_56", 0);

                DateTime report_date = date.Date;
                EachSaveReportOverview(report_date, "tk_report", result);


                //var exist = new DBContext.Table("tk_report")
                //     .Fields("id,report_date")
                //     .Get<dynamic>("report_date=@report_date AND accountId=@accountId",
                //                     new { report_date, accountId = _accountId });
                //var createArgs = new DBContext.Table("tk_report")
                //    .Add("fix_uclk_pv_56", result.fix_uclk_pv_56);

                //if (exist == null)
                //{
                //    createArgs.Add("accountId", _accountId)
                //        .Add("accountName", _company)
                //        .Add("report_date", report_date)
                //        .Add("create_time", DateTime.Now)
                //        .Add("last_time", DateTime.Now)
                //        .Create();
                //}
                //else
                //{
                //    createArgs.Add("last_time", DateTime.Now)
                //        .Where("id=@id", new { exist.id })
                //        .Update();
                //}
            }
            catch (Exception ex) { }
        }


        /// <summary>
        /// 1小时更新一次
        /// </summary>
        public void SaveReportHourtrend(DateTime date)
        {
            string t = $"{DateTime.Now.Convert2UnixTimestamp(true)}";
            string dt = date.ToString("yyyy-MM-dd");

            string url = "https://pub.alimama.com/openapi/param2/1/gateway.unionpub/data.lens.report.hourtrend.dod.json?" +
                $"t={t}&_tb_token_={_tb_token}&ruleEnum=pub_homepage&startDate={dt}&endDate={dt}&fixPeriod=realTime";


            var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
                        .AddHeaders("X-Requested-With", "XMLHttpRequest")
                        .AddHeaders("Cookie", _cookies);
#if DEBUG
#else
            client.Proxy = _proxy;
#endif
            if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
            var response = client.Request(url);

            var body = response.Body();
            var root = body.Convert2JsonElement();

            int resultCode = root.Read<int>("resultCode", 0);
            bool success = root.Read("success", false);
            if (!success)
            {
                _ = new LoggerLibrary("report", "error")
                    .Info(url, body)
                    .SaveAsync();
                throw new Exception(body);
            }

            var data_list = root.ElementRead("data").ElementRead("data");
            foreach (var data in data_list.EnumerateArray())
            {
                string xAxis = data.Read<string>("xAxis");

                string _report_date = $"{dt} {xAxis}";
                EachSaveReport(_report_date, "tk_report_hourtrend", data);
                if ("23:00".Equals(xAxis))
                {
                    EachSaveReport(dt, "tk_report", data);
                }
            }
        }

        private void EachSaveReport(string _report_date, string tableName, JsonElement data)
        {
            _ = DateTime.TryParse(_report_date, out DateTime report_date);

            var exist = new DBContext.Table(tableName)
                 .Fields("id,report_date")
                 .Get<dynamic>("report_date=@report_date AND accountId=@accountId",
                                 new { report_date, accountId = _accountId });
            if (exist == null)
            {
                new DBContext.Table(tableName)
                    .Fill(data)
                    .Loader<int>("uclk_uv_56", 0)
                    .Loader<int>("uclk_pv_56", 0)
                    .Loader<int>("uclt_uv_56", 0)
                    .Loader<int>("ucart_uv_56", 0)
                    .Loader<int>("dep_ord_num_56", 0)
                    .Loader<decimal>("dep_ord_dep_amt_56", 0)
                    .Loader<int>("pay_ord_num_56", 0)
                    .Loader<decimal>("pay_ord_amt_56", 0)
                    .Loader<decimal>("pay_tk_disp_tfee_56", 0)
                    .Loader<int>("pay_ord_uv_56", 0)
                    .Loader<int>("sett_ord_num_56", 0)
                    .Loader<decimal>("sett_ord_amt_56", 0)
                    .Loader<decimal>("sett_tk_disp_tfee_56", 0)
                    .Add("accountId", _accountId)
                    .Add("accountName", _company)
                    .Add("report_date", report_date)
                    .Add("create_time", DateTime.Now)
                    .Add("last_time", DateTime.Now)
                    .Create();
            }
            else
            {
                new DBContext.Table(tableName)
                    .Fill(data)
                    .Loader<int>("uclk_uv_56", 0)
                    .Loader<int>("uclk_pv_56", 0)
                    .Loader<int>("uclt_uv_56", 0)
                    .Loader<int>("ucart_uv_56", 0)
                    .Loader<int>("dep_ord_num_56", 0)
                    .Loader<decimal>("dep_ord_dep_amt_56", 0)
                    .Loader<int>("pay_ord_num_56", 0)
                    .Loader<decimal>("pay_ord_amt_56", 0)
                    .Loader<decimal>("pay_tk_disp_tfee_56", 0)
                    .Loader<int>("pay_ord_uv_56", 0)
                    .Loader<int>("sett_ord_num_56", 0)
                    .Loader<decimal>("sett_ord_amt_56", 0)
                    .Loader<decimal>("sett_tk_disp_tfee_56", 0)
                    .Add("last_time", DateTime.Now)
                    .Where("id=@id", new { exist.id })
                    .Update();
            }
        }


        /// <summary>
        /// 警告消息
        /// </summary>
        public void GetViolationuWarning()
        {
            try
            {
                string url = "https://media.alimama.com/violationu2/warning_instance_page_v2.json?r=mx_208&toPage=1&pageSize=40&params=";
                var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
                            .AddHeaders("X-Requested-With", "XMLHttpRequest")
                            .AddHeaders("Cookie", _cookies);
#if DEBUG
#else
            client.Proxy = _proxy;
#endif
                if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
                var response = client.Request(url);

                var body = response.Body();
                var root = body.Convert2JsonElement();

                int resultCode = root.Read<int>("resultCode", 0);
                int totalCount = root.PathRead<int>("data.totalCount", 0);
                bool success = root.Read("success", false);

                if (!success)
                {
                    _ = new LoggerLibrary("violationu", "warning_error")
                        .Info(url, body)
                        .SaveAsync();
                    throw new Exception(body);
                }
                if (totalCount == 0) return;
                _ = new LoggerLibrary("violationu", "warning").Info(url, body).SaveAsync();

                var list = root.ElementRead("data").ElementRead("result").EnumerateArray();
                foreach (var data in list)
                {
                    var violation_commission = data.PathRead<decimal>("mediaInfo.订单虚假交易违规佣金", 0);
                    new DBContext.Table("alimama_warning")
                        .Fill(data)
                        .Loader<int>("filterRuleId", 0)
                        .Loader<long>("gmtCreate", 0)
                        .Loader<long>("id", 0, "ali_id")
                        .Loader<string>("idStr", string.Empty)
                        .Loader<int>("mediaDimension", 0)
                        .Loader<long>("mediaId", 0)
                        .Loader<int>("mediaInfo.abn_cnt", 0, "abn_cnt")
                        .Add("violation_commission", violation_commission)
                        .Loader<int>("mediaInfo.warning_cnt", 0, "warning_cnt")
                        .Loader<string>("mediaName", string.Empty)
                        .Loader<long>("memberId", 0)

                        .Loader<string>("noticeCategory", string.Empty)
                        .Loader<int>("noticeTemplateId", 0)
                        .Loader<string>("noticeTemplateName", string.Empty)

                        .Loader<string>("orderFile", string.Empty)
                        .Loader<string>("pid", string.Empty)
                        .Loader<int>("punishRuleId", 0)
                        .Loader<string>("riskType", string.Empty)
                        .Loader<string>("roleName", string.Empty)
                        .Loader<string>("ruleLink", string.Empty)
                        .Loader<bool>("showOrderDetail", false)
                        .Loader<int>("status", 0, "ali_status")
                        .Loader<string>("warningText", string.Empty)

                        .Add("accountName", _company)
                        .Add("status", true)
                        .Add("create_time", DateTime.Now)
                        .Add("last_time", DateTime.Now)
                        .Create(DBContext.InsertType.REPLACE);
                }
            }
            catch (Exception ex) { }
        }


    }
}
