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.Model.ApiModel.MatInventoryDetail;
using WCS.WebApi.Helper;

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

        public MatInventoryDetailController(IMatInventoryDetailService matInventoryDetailService)
        {
            _matInventoryDetailService = matInventoryDetailService;
        }

        [Route("getMatInventoryDetail")]
        [HttpPost(Name = "getMatInventoryDetail")]
        public async Task<ResponseBase> getMatInventoryDetail(GetMatInventoryDetailRequest request)
        {
            return await _matInventoryDetailService.getMatInventoryDetail(request);
        }

        [HttpPost("exportMatInventoryDetail")]
        public async Task<IActionResult> exportMatInventoryDetail([FromBody] GetMatInventoryDetailRequest request)
        {
            var result = await _matInventoryDetailService.exportMatInventoryDetail(request);
            var data = result.Data?.Lists;
            var columns = new[]
            {
                new ExportableColumn("序号","RowNumber"),
                new ExportableColumn("物料编码","MatCode"),
                new ExportableColumn("物料名称","MatName"),
                new ExportableColumn("规格","MatSpec"),
                new ExportableColumn("批次","MatBatch"),
                new ExportableColumn("数量","MatQty"),
                new ExportableColumn("库位","StoreCode"),
                new ExportableColumn("入库时间","InstoreTime"),
                new ExportableColumn("物料SN", "MatSN"),
            };
            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 };
            //}
        }

        [Route("getMatInventorySummary")]
        [HttpPost(Name = "getMatInventorySummary")]
        public async Task<ResponseBase> getMatInventorySummary(GetMatInventorySummaryRequest request)
        {
            return await _matInventoryDetailService.getMatInventorySummary(request);
        }

        [Route("compareMatInventoryDetail")]
        [HttpPost(Name = "compareMatInventoryDetail")]
        public async Task<ResponseBase> compareMatInventoryDetail(CompareMatInventoryDetailRequest request)
        {
            return await _matInventoryDetailService.compareMatInventoryDetail(request);
        }

    }
}
