using molilian.core;
using dodohold.core;
using Microsoft.AspNetCore.Mvc;
using System.Text.Json;
using static molilian.core.ChartDataCore;
using TencentCloud.Soe.V20180724.Models;
using TencentCloud.Mrs.V20200910.Models;
using System.Xml.Linq;
using System.Security.Cryptography;
using TencentCloud.Bi.V20220105.Models;
using static Microsoft.Extensions.Logging.EventSource.LoggingEventSource;
using molilian.core.PushDataReport;
using System.Threading;
using OfficeOpenXml.DataValidation.Exceptions;
using System.Text.RegularExpressions;
namespace molilian.api.Controllers
{
    [ApiController]
    [MyAuthorize("admin")]
    [Route("api/[controller]/[action]")]
    public class CustomReportController : ControllerBase
    {
        readonly IAuthorizationProvider provider = new AdminProvider();
        protected IHttpContextAccessor _accessor;
        public CustomReportController(IHttpContextAccessor accessor)
        {
            _accessor = accessor;
        }

        [HttpPost]
        public ActionResult 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;

            // 临时锁定所有口令推送数据
            //string filter = "type<>1";

            int type = form.Read("type", 0);
            if (type > 0) filter += $" AND type = @type";


            int status = form.Read("status", -1);
            if (status != -1) filter += $" AND status = @status";

            int is_settlement = form.Read("is_settlement", -1);
            if (is_settlement != -1) filter += $" AND is_settlement = @is_settlement";


            int platform = form.Read("platform", 0);
            if (platform > 0) filter += $" AND platform = @platform";

            var report_date = form.Read<DateTime>("report_date", DateTime.MinValue);
            if (report_date != DateTime.MinValue) filter += $" AND report_date = @report_date";


            filter = filter.StringTrimStart(" AND ");

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

            var result = new DBContext.Table("push_data_report")
               .Where(filter, new { type, report_date, is_settlement, status, platform })
               .Page(size, page)
               .Order(orderBy)
               .PageList<dynamic>(getTotal);

            return new APIResult(new { data = result });
        }

        [HttpPost]
        public ActionResult GetSqlTemplates([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 category = form.Read<string>("category");
            string keyword = form.Read<string>("keyword");

            string filter = "is_active = 1";

            if (!string.IsNullOrEmpty(category))
                filter += " AND category = @category";

            if (!string.IsNullOrEmpty(keyword))
                filter += " AND (name LIKE @keyword OR description LIKE @keyword)";

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

            var keywordParam = !string.IsNullOrEmpty(keyword) ? $"%{keyword}%" : "";

            var result = new DBContext.Table("sql_templates")
               .Where(filter, new { category, keyword = keywordParam })
               .Page(size, page)
               .Order(orderBy)
               .PageList<dynamic>(getTotal);

            return new APIResult(new { data = result });
        }

         


    }
}