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;


namespace molilian.core
{
    public partial class ToolParsePlus
    {
        public static void DailyLogs(int intervalDay)
        {
            TkChannelEnum toolChannelEnum = TkChannelEnum.tool;
            string channel = toolChannelEnum.ToString();

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

            int all_total_count = 0;
            var all_success_count = 0;
            var all_abandon_count = 0;
            List<TkChannelEnum> arr = [TkChannelEnum.wemeet, TkChannelEnum.bdpan];
            foreach (var channelEnum in arr)
            {
                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}%";
            }


            using var conn = CenterHub.GetOpenConnection();
            var exist = 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($"tool_total_count", all_total_count)
                .Add($"tool_abandon_count", all_abandon_count)
                .Add($"tool_abandon_percentage", all_abandon_percentage)
                .Add($"tool_success_count", all_success_count)
                .Add($"tool_success_percentage", all_success_percentage)
                .Add("last_time", DateTime.Now);

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

        }

    }
}
