using molilian.core;
using dodohold.core;
using Microsoft.AspNetCore.Mvc;
using Org.BouncyCastle.Ocsp;
using System.Text.Json;
using System.Runtime.InteropServices;
using System.Net;
using System.Threading.Channels;
using TencentCloud.Cdwch.V20200915.Models;
using COSXML.Network;
using System.Security.Cryptography;
using System.Threading;
using TencentCloud.Batch.V20170312.Models;

namespace molilian.api.Controllers
{
    [ApiController]
    [Route("[controller]_70160bd632/[action]")]
    public class JDTaskController : ControllerBase
    {

        protected IHttpContextAccessor _accessor;
        public JDTaskController(IHttpContextAccessor accessor)
        {
            _accessor = accessor;
        }


        [HttpGet]
        public async Task<ActionResult> GetHistoryOrders(int id, int sleep, int h = 0)
        {
            var list = await JdPoolCore.ListAsync();
            if (list == null) return new APIResult(new { success = false, message = "没有有效账号", });

            string message = string.Empty;
            int total = 0;
            foreach (var account in list)
            {
                if (id != 0 && id != account.id) continue;
                 try
                {
                    DateTime endTime = DateTime.Now.Date;
                    DateTime startTime = endTime.AddDays(-90);

                    var plus = new JdUnionPlus(account);

                    //for (int i = 89; i >= 0; i--)
                    //{
                    //    DateTime dayStartTime = startTime.AddDays(i);
                    //    DateTime dayEndTime = dayStartTime.AddDays(1);
                    //    total += await plus.GetHistoryOrders(sleep, dayStartTime, dayEndTime);
                    //}

                    if (h == 1)
                    {
                        for (int i = 24 * 90 - 1; i >= 0; i--)
                        {
                            DateTime hourStartTime = startTime.AddHours(i);
                            DateTime hourEndTime = hourStartTime.AddHours(1);
                            total += await plus.GetHistoryOrders(sleep, hourStartTime, hourEndTime);
                        }
                    }
                    else
                    {
                        for (int i = 89; i >= 0; i--)
                        {
                            DateTime dayStartTime = startTime.AddDays(i);
                            DateTime dayEndTime = dayStartTime.AddDays(1);
                            total += await plus.GetHistoryOrders(sleep, dayStartTime, dayEndTime);
                        }
                    }

                    //total += await plus.GetHistoryOrders(sleep, startTime, endTime);
                }
                catch (Exception ex)
                {
                    message = $"【JD历史订单接口异常】[{account.id}]{account.company}\n{ex.Message}\n{ex.StackTrace}";
                    NotifyCore.Notify(new NifyMessage
                    {
                        message = message,
                        priority = NifyMessagePriority.high,
                        tags = ["red_circle"]
                    });
                    continue;
                }
            }
            return new APIResult(new { success = true, message = "ok", total });
        }

        [HttpGet]
        public async Task<ActionResult> GetIncyOrders(int sleep = 2000)
        {
            var list = await JdPoolCore.ListAsync();
            if (list == null) return new APIResult(new { success = false, message = "没有有效账号", });
            string message = string.Empty;
            int total = 0;
            foreach (var account in list)
            {
                if (!account.enable_sync_order) continue;

                try
                {
                    var plus = new JdUnionPlus(account);
                    total += await plus.GetIncyOrders(sleep);
                }
                catch (Exception ex)
                {
                    message = $"【JD新增订单接口异常】[{account.id}]{account.company}\n{ex.Message}\n{ex.StackTrace}";
                    NotifyCore.Notify(new NifyMessage
                    {
                        message = message,
                        priority = NifyMessagePriority.high,
                        tags = ["red_circle"]
                    });
                    continue;
                }
            }
            return new APIResult(new { success = true, message = "ok", total });
        }


    }
}