using molilian.core;
using dodohold.core;
using Microsoft.AspNetCore.Mvc;
using System.Text.Json;
using static molilian.core.ChartDataCore;
namespace molilian.api.Controllers
{
    [ApiController]
    [MyAuthorize("admin")]
    [Route("api/[controller]/[action]")]
    public class ReportController : ControllerBase
    {
        readonly IAuthorizationProvider provider = new AdminProvider();
        protected IHttpContextAccessor _accessor;
        public ReportController(IHttpContextAccessor accessor)
        {
            _accessor = accessor;
        }

        [HttpPost]
        public ActionResult daily_list([FromBody] JsonElement form)
        {
            var token = provider.Get(_accessor.HttpContext);

            int page = form.Read("current", 1);
            int size = form.Read("pageSize", 10);
            bool getTotal = form.Read("getTotal", true);

            string sort = form.Read<string>("sort");
            string order = form.Read<string>("order");

            string filter = string.Empty;

            var log_date = form.PathReadArray<string>("query_date[]");
            var accountName = form.Read("accountName", string.Empty);
            int channel = form.Read("channel", -1);

            if (!string.IsNullOrEmpty(accountName))
            {
                filter += $" AND accountName=@accountName";
            }
            else
            {
                filter += $" AND accountName!='all'";
            }
            DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
            if (log_date.Count == 2)
            {
                if (DateTime.TryParse(log_date[0], out stime) && DateTime.TryParse(log_date[1], out etime))
                {
                    etime = etime.AddDays(1).AddSeconds(-1);
                    if (etime > DateTime.Now) etime = DateTime.Now;
                    filter += $" AND log_date BETWEEN @stime AND @etime";
                }
            }
            if (channel != -1)
            {
                filter += $" AND channel=@channel";
            }
            filter = filter.StringTrimStart(" AND ");

            //排序
            string orderBy = "id DESC";
            if (!string.IsNullOrEmpty(order))
            {
                order = "descending".Equals(order) ? "DESC" : "ASC";
                orderBy = sort switch
                {
                    _ => $"{sort} {order}",
                };
            }

            using var conn = CenterHub.GetOpenConnection();
            var result = new DBContext.Table(conn, "center_daily_logs")
                .Where(filter, new { channel, accountName, stime, etime })
                .Page(size, page)
                .Order(orderBy)
                .PageList<DailyLogsDTO>(getTotal);


            var accounts = JdPoolCore.List();
            var hide_ids = accounts.Where(e => e.is_hide).Select(e => e.id).ToList();
            result.List = result.List.Where(e => e.channel != 1 || !hide_ids.Contains(e.accountId));
            return new APIResult(new { data = result });
        }


        [HttpPost]
        public ActionResult chartData([FromBody] JsonElement form)
        {
            var result = new ChartDataRes();

            var report_date = form.PathReadArray<string>("query_date[]");
            var accountName = form.Read("accountName", string.Empty);
            var isHourtrend = form.Read<bool>("isLeaf", false);
            var total_key = form.Read("total_key", "total");

            DateTime stime = DateTime.MinValue;
            if (!DateTime.TryParse(report_date[0], out stime)) return new APIResult(result);

            string _report_date = stime.ToString("yyyyMMdd");
            if (isHourtrend) _report_date = stime.ToString("yyyyMMddHH");



            if (string.IsNullOrEmpty(accountName)) accountName = "tb";

            switch (total_key)
            {
                case "tool_total":
                    result = GetToolData(total_key, accountName, _report_date);
                    break;
                case "total":
                case "parse_total":
                case "coupon_total":
                    result = GetTaobaoData(total_key, accountName, _report_date);
                    break;
                //case "coupon_total":
                //    result = GetTaobaoCouponData(total_key, accountName, _report_date);
                //    break;
                case "jd_parse_total":
                    result = GetJdData(total_key, accountName, _report_date);

                    break;
            }
            return new APIResult(result);
        }


    }
}