using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using dodohold.core;
using Dataoke;
using System.Text.RegularExpressions;
using Org.BouncyCastle.Ocsp;
using System.Diagnostics;
using TencentCloud.Fmu.V20191213.Models;
using Microsoft.VisualBasic;
using System.Text.Json;
using static System.Runtime.InteropServices.JavaScript.JSType;
using System.Security.Cryptography;
using Google.Protobuf.WellKnownTypes;
using Microsoft.Extensions.FileSystemGlobbing.Internal;
using System.Threading.Channels;


namespace molilian.core
{
    public partial class JdUnionPlus
    {
        public static void DailyLogs(int intervalDay)
        {
            TkChannelEnum channelEnum = TkChannelEnum.jd;

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

            var accounts = JdPoolCore.List();
            using var conn = CenterHub.GetOpenConnection();



            int all_total_count = 0, all_success_count = 0, all_abandon_count = 0;
            foreach (var account in accounts)
            {
                int accountId = account.id;
                string accountName = $"{channelEnum}_{account.id}";
                //if (account.is_hide) continue;

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

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

                var success_count = TkLogCore.GetTotal($":parse_total:{accountName}:success:{log_date:yyyyMMdd}");
                var abandon_count = TkLogCore.GetTotal($":parse_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}%";
                }
                //all_total_count += total_count;
                //all_success_count += success_count;
                //all_abandon_count += abandon_count;

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


                var createArgs = new DBContext.Table(conn, "center_daily_logs")
                    .Add($"jd_parse_total_count", total_count)
                    .Add($"jd_parse_abandon_count", abandon_count)
                    .Add($"jd_parse_abandon_percentage", abandon_percentage)
                    .Add($"jd_parse_success_count", success_count)
                    .Add($"jd_parse_success_percentage", success_percentage)
                    .Add("last_time", DateTime.Now);

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

            all_total_count = TkLogCore.GetTotal($":parse_total:{channelEnum}:{log_date:yyyyMMdd}");
            all_success_count = TkLogCore.GetTotal($":parse_total:{channelEnum}:success:{log_date:yyyyMMdd}");
            all_abandon_count = TkLogCore.GetTotal($":parse_total:{channelEnum}:放弃转链:{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, "center_daily_logs")
            .Fields("id,log_date")
                 .Get<dynamic>("log_date=@log_date AND accountId=@accountId",
                 new { log_date, accountId = 0 });

            var createArgs2 = new DBContext.Table(conn, "center_daily_logs")
                .Add($"jd_parse_total_count", all_total_count)
                .Add($"jd_parse_abandon_count", all_abandon_count)
                .Add($"jd_parse_abandon_percentage", all_abandon_percentage)
                .Add($"jd_parse_success_count", all_success_count)
                .Add($"jd_parse_success_percentage", all_success_percentage)
                .Add("last_time", DateTime.Now);

            if (exist2 == null)
            {
                createArgs2.Add("channel", (int)channelEnum)
                    .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();
            }

        }

    }
}
