using DS.Module.Core;
using DS.Module.ExcelModule;
using DS.Module.ExcelModule.Model;
using DS.WMS.Core.Code.Interface;
using Masuit.Tools.Files;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using NPOI.HPSF;
using NPOI.XSSF.UserModel;
using SharpCompress.Common;
using System.IO;
using System.Text;
using DS.Module.Core.Data;
using DS.WMS.Core.Code.Dtos;
using DS.WMS.Core.Op.Dtos;
using DS.WMS.Core.Op.Entity;
using DS.WMS.Core.Op.Interface;
using DS.WMS.Core.Sys.Dtos;
using DS.WMS.Core.Sys.Interface;
using Microsoft.AspNetCore.Authorization;
using SixLabors.ImageSharp.Drawing;
using System.IO;
using System.Web;
using DS.WMS.Core.Flow.Dtos;
using DS.WMS.Core.Flow.Entity;
using DS.WMS.Core;
using MathNet.Numerics;
using NPOI.SS.Formula.Functions;
using DS.Module.UserModule;
using SqlSugar;
using Newtonsoft.Json.Linq;
using DS.Module.Core.Extensions;
using LanguageExt.Common;
using DS.WMS.Core.TaskPlat.Dtos;
namespace DS.WMS.MainApi.Controllers
{
///
/// Excel服务 模块
///
public class ExcelController : ApiController
{
private readonly IServiceProvider _serviceProvider;
private readonly IExcelService _invokeService;
private readonly IUser user;
static readonly ApiFox api;
private readonly ISqlSugarClient db;
///
/// 构造函数
///
static ExcelController()
{
api = new ApiFox();
}
///
/// 构造函数
///
///
public ExcelController(IExcelService invokeService, IServiceProvider serviceProvider)
{
_invokeService = invokeService;
_serviceProvider = serviceProvider;
user = _serviceProvider.GetRequiredService();
db = _serviceProvider.GetRequiredService();
}
///
/// 根据用户列导出Excel数据
///
/// 请求数据
///
[HttpPost]
[Route("ExportExcelByColumn")]
public DataResult ExportExcelByColumn([FromBody] ExportByColumnReq req)
{
return _invokeService.ExportExcelByColumn(req);
}
///
/// 通用Excel导出
///
///
///
[HttpPost]
[Route("CommonExcelExport")]
[ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK)]
public async Task CommonExcelExportAsync([FromBody] ExportExcelReq req)
{
if (api.DefaultHeaders.Contains("Authorization"))
api.DefaultHeaders.Remove("Authorization");
api.DefaultHeaders.Add("Authorization", "Bearer " + user.GetToken());
var result = await api.PostAsync(req.Url, req.QueryRequest);
if (!result.Succeeded)
{
await new ApplicationException($"调用URL:{req.Url} 时返回了错误:" + result.Message).LogAsync(db);
}
var data = req.Url.Contains("GetSeaExportList") ?JsonConvert.SerializeObject(result.Data.ObjToDynamic()?.Data.list) : JsonConvert.SerializeObject(result.Data.Data);
var res1 = await _invokeService.ExportExcelStreamByColumnAsync(new ExportByColumnReq()
{
JsonDataStr = data,
ColumnSets = req.ColumnSets,
});
var dtstr = DateTime.Now.ToString("yyyyMMddHHmmssfff");
var filename = $"{dtstr}.xlsx";
byte[] bytes = new byte[res1.Length];
res1.ReadExactly(bytes, 0, bytes.Length);
string mimeType = "application/octet-stream";
var _r = new FileContentResult(bytes, mimeType)
{
FileDownloadName = filename
};
return _r;
}
//}
///
/// 根据用户列导出Excel文件流
///
/// 请求数据
///
[HttpPost]
[Route("ExportExcelStreamByColumn")]
[ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK)]
//[AllowAnonymous]
public async Task ExportExcelStreamByColumn([FromBody] ExportByColumnReq req)//
{
var result = _invokeService.ExportExcelStreamByColumn(req);
//HttpContext.Response.Headers.Add("Content-Length", result.Length.ToString());
//HttpContext.Response.Headers.Add("Content-Type", "application/octet-stream;charset=UTF-8");
var dtstr = DateTime.Now.ToString("yyyyMMddHHmmssfff");
var filename = $"{req.Title}-{dtstr}.xlsx";
byte[] bytes = new byte[result.Length];
result.ReadExactly(bytes, 0, bytes.Length);
string mimeType = "application/octet-stream";
var _r = new FileContentResult(bytes, mimeType)
{
FileDownloadName = filename
};
return _r;
//var path = Path.Combine("", "wwwroot/export/" + filename);
//using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write))
//{
// result.WriteTo(fileStream);
//}
//byte[] byteArr = System.IO.File.ReadAllBytes(path);
//string mimeType = "application/octet-stream";
//return new FileContentResult(byteArr, mimeType)
//{
// FileDownloadName = filename
//};
//return File(result, "application/octet-stream;charset=UTF-8",filename);
////return new FileStreamResult(result, "application/octet-stream;charset=UTF-8")
////{
//// FileDownloadName = filename
////};
//return File(result, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", filename);
//var bytes = new byte[result.Length];
//result.Read(bytes, 0, bytes.Length);
//result.Close();
//return new FileContentResult(bytes, "application/octet-stream");
//return new FileStreamResult(result, "application/octet-stream") { FileDownloadName = $"{req.Title}-{dtstr}.xlsx" };
//var provider = new Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider();
//var memi = provider.Mappings[".xlsx"];
//var fileName = Path.GetFileName(filename);
//return File(result, memi, fileName);
//var result = _invokeService.OutputExcel(req);
//return File(result, "application/octet-stream;charset=UTF-8", "UploadTemplate.xlsx");
}
}
}