using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using NPOI.SS.UserModel;
using SqlSugar;
using System.Xml.Linq;
using WCS.BLL.DbModels;
using WCS.BLL.Manager;
using WCS.BLL.Services.IService;
using WCS.BLL.Services.Service;
using WCS.DAL.Db;
using WCS.Model;
using WCS.Model.ApiModel;
using WCS.WebApi.Helper;

namespace WCS.WebApi.Controllers
{
    /// <summary>
    /// 接口记录
    /// </summary>
    [ApiController]
    [Route("[controller]")]
    public class InterfaceRecordController : ControllerBase
    {
        public IInterfaceRecordService _interfaceRecordService { get; set; }

        public InterfaceRecordController(IInterfaceRecordService interfaceRecordService)
        {
            _interfaceRecordService = interfaceRecordService;
        }

        [Route("getInterfaceRecord")]
        [HttpPost(Name = "getInterfaceRecord")]
        public async Task<ResponseBase> getInterfaceRecord(GetInterfaceRecordsRequest request)
        {
            return await _interfaceRecordService.getInterfaceRecord(request);
        }

        [HttpPost("exportInterfaceRecord")]
        public async Task<IActionResult> exportInterfaceRecord([FromBody] GetInterfaceRecordsRequest request)
        {
            var result = await _interfaceRecordService.exportInterfaceRecord(request);
            var data = result.Data?.Lists;
            var columns = new[]
            {
                new ExportableColumn("序号","RowNumber"),
                new ExportableColumn("接口地址","RequestUrl"),
                new ExportableColumn("设备IP","DeviceIp"),
                new ExportableColumn("请求参数","RequestBody"),
                new ExportableColumn("QueryString","QueryString"),
                new ExportableColumn("请求时间","RequestTime"),
                new ExportableColumn("返回参数","ResponseJson"),
                new ExportableColumn("返回时间","ResponseTime"),
                new ExportableColumn("耗时(ms)", "ExecutionTime"),
            };
            if (data == null)
            {
                return NotFound();
            }
            else
                return ExportExcelHelper.Export("导出数据", columns, data);

            // 导出到Excel
            //using (var stream = new MemoryStream())
            //{
            //    // 获取当前工作目录
            //    string currentPath = Directory.GetCurrentDirectory();
            //    // 创建文件路径
            //    string filePath = Path.Combine(currentPath, "data.xlsx");
            //    //MiniExcel.SaveAs(filePath, data);
            //    MiniExcel.SaveAs(stream, data);
            //    // 重置位置
            //    //stream.Position = 0;
            //    //// 通过接口下载文件
            //    //return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "data.xlsx");
            //    var isXlsx = true;
            //    var saveAsFileName = string.Format("{0}-{1:d}.{2}", "测试", DateTime.Now, (isXlsx ? "xlsx" : "xls")).Replace("/", "-");
            //    var contentType = isXlsx ? "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" : "application/vnd.ms-excel";
            //    return new FileContentResult(stream.ToArray(), contentType) { FileDownloadName = saveAsFileName };
            //}
        }
    }
}
