189 lines
6.0 KiB
C#
189 lines
6.0 KiB
C#
using MyCode.Project.Domain.Message.Act.Common;
|
||
using MyCode.Project.Infrastructure.Cache;
|
||
using MyCode.Project.Infrastructure.Common;
|
||
using MyCode.Project.Infrastructure.Constant;
|
||
using MyCode.Project.Infrastructure.Enumeration;
|
||
using MyCode.Project.Infrastructure.Exceptions;
|
||
using MyCode.Project.Services;
|
||
using MyCode.Project.WebApi.App_Filter;
|
||
using System;
|
||
using System.IO;
|
||
using System.Web.Http;
|
||
|
||
namespace MyCode.Project.WebApi.Areas.Admin.Controllers
|
||
{
|
||
/// <summary>
|
||
/// 导出功能
|
||
/// </summary>
|
||
public class LxmZHMDReportController : BaseAdminController
|
||
{
|
||
#region 初始化
|
||
private readonly IReportService _reportService;
|
||
private readonly IMyCodeCacheService _myCodeCacheServie;
|
||
private readonly IQueueProcessService _queueProcessService;
|
||
|
||
|
||
public LxmZHMDReportController(IReportService reportService,
|
||
IMyCodeCacheService myCodeCacheService,
|
||
IQueueProcessService queueProcessService
|
||
)
|
||
{
|
||
|
||
_reportService = reportService;
|
||
_myCodeCacheServie = myCodeCacheService;
|
||
_queueProcessService = queueProcessService;
|
||
}
|
||
#endregion
|
||
|
||
#region Test(测试方法)
|
||
/// <summary>
|
||
/// 测试方法
|
||
/// </summary>
|
||
[AllowAnonymous]
|
||
[HttpGet]
|
||
public string Test()
|
||
{
|
||
|
||
return "test";
|
||
|
||
}
|
||
#endregion
|
||
|
||
#region ExportXls(导出报表)
|
||
/// <summary>
|
||
/// 导出报表,得到报表id,根据该ID可以建立websocket与下载
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
public dynamic ExportXls(ReportExportAct act)
|
||
{
|
||
var reportId = IdHelper.GetNewId();
|
||
|
||
var ansyAct = new AnsyReportExportAct()
|
||
{
|
||
Condition = act.Condition,
|
||
CurrentUser = CurrentLogin,
|
||
ReportId = reportId,
|
||
ReportType = act.ReportType
|
||
};
|
||
|
||
//ZIP导出
|
||
#region ZIP导出(没有特殊处理的方法,不包含旧的ZIP导出)
|
||
|
||
|
||
#endregion
|
||
#region ZIP导出(单独写的方法后续不动)
|
||
|
||
#endregion
|
||
|
||
ansyAct.ExportFileType = (int)ExportFileType.Xls;
|
||
|
||
//正常的导出
|
||
var filePath = _reportService.ReportExport(ansyAct);
|
||
|
||
var fileName = Path.GetFileName(filePath);
|
||
|
||
var res = CurrentHttpContext;
|
||
|
||
res.Response.Clear();
|
||
|
||
using (var fs = new FileStream(filePath, FileMode.Open))
|
||
{
|
||
byte[] bytes = new byte[(int)fs.Length];
|
||
fs.Read(bytes, 0, bytes.Length);//将流
|
||
|
||
res.Response.AppendHeader("Content-Disposition", $"attachment;filename={fileName};size={bytes.Length}");
|
||
|
||
res.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=GB2312";
|
||
|
||
res.Response.BinaryWrite(bytes);
|
||
res.Response.Flush();
|
||
res.Response.End();
|
||
}
|
||
|
||
return reportId;
|
||
}
|
||
#endregion
|
||
|
||
/// <summary>
|
||
/// 压缩文件导出处理
|
||
/// </summary>
|
||
/// <param name="act"></param>
|
||
/// <returns></returns>
|
||
public long ZIPExportXls(AnsyReportExportAct act)
|
||
{
|
||
string hasDownloadZipCacheKey = CacheKey.HasDownloadZipCacheKey + $@":{act.ReportType}";
|
||
var existDowing = _myCodeCacheServie.Exists(hasDownloadZipCacheKey);
|
||
|
||
if (existDowing) { throw new BaseException("当前有人正在导出该报表,为了保障性能,请1分钟后再导出"); }
|
||
|
||
//下载的标识
|
||
_myCodeCacheServie.Set($"{hasDownloadZipCacheKey}", 1, new TimeSpan(0, 1, 0));
|
||
|
||
//有这个才需要继续执行导出,没有的话,说明是去消息了
|
||
_myCodeCacheServie.Set($"{CacheKey.ReportZipFileCacheKey}{act.ReportId}", "", new TimeSpan(1, 0, 0));
|
||
|
||
_queueProcessService.AddQueue<IReportService>("ReportExportAnsy", DateTime.Now.AddHours(1), act);
|
||
|
||
return act.ReportId;
|
||
}
|
||
|
||
|
||
#region CancelExport(取消导出)
|
||
/// <summary>
|
||
/// 取消导出
|
||
/// </summary>
|
||
/// <param name="act"></param>
|
||
[AllowAnonymous]
|
||
[HttpPost]
|
||
public void CancelExport(IdAct act)
|
||
{
|
||
_myCodeCacheServie.Delete($"{CacheKey.ReportZipFileCacheKey}{act.Id}");
|
||
_myCodeCacheServie.Delete($"{CacheKey.HasDownloadZipCacheKey}");
|
||
}
|
||
#endregion
|
||
|
||
#region DownLoadExcel(开始下载)
|
||
/// <summary>
|
||
/// 开始下载
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[AllowAnonymous]
|
||
[IgnoreResultHandle]
|
||
[HttpGet]
|
||
public void DownLoadExcel(long id)
|
||
{
|
||
var res = CurrentHttpContext;
|
||
|
||
var zipFile = _myCodeCacheServie.Get<string>($"{CacheKey.ReportZipFileCacheKey}{id}");
|
||
|
||
if (string.IsNullOrWhiteSpace(zipFile)) { throw new BaseException("没有需要下载的文件"); }
|
||
|
||
//var zipFile = @"D:\App_File\download\exceltemp\202301\2301291545561350.zip";
|
||
|
||
//得到文件名
|
||
var fileName = Path.GetFileName(zipFile);
|
||
|
||
res.Response.Clear();
|
||
res.Response.Buffer = false;
|
||
|
||
using (var fs = new FileStream(zipFile, FileMode.Open))
|
||
{
|
||
byte[] bytes = new byte[(int)fs.Length];
|
||
fs.Read(bytes, 0, bytes.Length);//将流
|
||
|
||
res.Response.AppendHeader("Content-Disposition", $"attachment;filename={fileName};size={bytes.Length}");
|
||
res.Response.ContentType = "application/x-zip-compressed;charset=UTF-8";//输出流的MIME类型
|
||
|
||
res.Response.BinaryWrite(bytes);
|
||
res.Response.Flush();
|
||
|
||
res.Response.End();
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
|
||
}
|
||
}
|