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


        [HttpPost]
        public ActionResult list([FromBody] JsonElement form)
        {
            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 name = form.Read<string>("keyword");
            string query_type = form.Read<string>("query_type", "tbPaidTime");
            var tkStatus = form.PathReadArray<int>("tkStatus[]");
            int maxTotal = form.Read("maxTotal", 0);

            var time = form.PathReadArray<string>("query_date[]");

            string filter = string.Empty;
            if (!string.IsNullOrEmpty(name))
            {
                filter += $" AND accountId IN (SELECT ID FROM tk_pool WHERE company=@name)";
            }
            if (tkStatus.Any())
            {
                filter += $" AND tkStatus IN @tkStatus";
            }

            DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
            if (time.Count == 2)
            {
                if (DateTime.TryParse(time[0], out stime) && DateTime.TryParse(time[1], out etime))
                {
                    etime = etime.AddDays(1).AddSeconds(-1);

                    filter += query_type switch
                    {
                        "tkEarningTime" or "tkCreateTime" => $" AND {query_type} BETWEEN @stime AND @etime",
                        _ => $" AND tbPaidTime BETWEEN @stime AND @etime",
                    };
                }
            }
            filter = filter.StringTrimStart(" AND ");

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

            var result = new DBContext.Table("tk_order_details")
                .Where(filter, new { name, stime, etime, tkStatus })
                .Page(size, page)
                .Order(orderBy)
                .PageList<TkOrderDetailDTO>(getTotal);


            //foreach (var item in result.List)
            //{
            //    if (string.IsNullOrEmpty(name))
            //    {
            //        item.accountName = "-";
            //    }
            //}

            if (maxTotal != 0 && result.Count > maxTotal)
            {
                result.Count = maxTotal;
                result.PageCount = maxTotal / size;
            }
            return new APIResult(new { data = result });
        }

    }
}