导出列表
This commit is contained in:
228
src/WMS.Web.Domain/Services/ExportExcelService.cs
Normal file
228
src/WMS.Web.Domain/Services/ExportExcelService.cs
Normal file
@@ -0,0 +1,228 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Npoi.Mapper;
|
||||
using System.IO;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using WMS.Web.Domain.IService;
|
||||
using WMS.Web.Domain.Options;
|
||||
using WMS.Web.Domain.Entitys;
|
||||
using WMS.Web.Domain.Values;
|
||||
using WMS.Web.Domain.Infrastructure;
|
||||
using WMS.Web.Core.Dto;
|
||||
|
||||
namespace WMS.Web.Domain.Services
|
||||
{
|
||||
public class ExportExcelService : IExportExcelService
|
||||
{
|
||||
private readonly IQiniuUploadService _qiniuUploadService;
|
||||
private readonly IServiceScopeFactory _serviceScopeFactory;
|
||||
private readonly ILogger<ExportExcelService> _logger;
|
||||
private readonly QiniuOptions _option;
|
||||
public ExportExcelService(IQiniuUploadService qiniuUploadService, IServiceScopeFactory serviceScopeFactory,
|
||||
ILogger<ExportExcelService> logger, IOptions<QiniuOptions> option)
|
||||
{
|
||||
_qiniuUploadService = qiniuUploadService;
|
||||
_serviceScopeFactory = serviceScopeFactory;
|
||||
_logger = logger;
|
||||
_option = option?.Value;
|
||||
}
|
||||
/// <summary>
|
||||
/// 列表页导出
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="dataList"></param>
|
||||
/// <param name="fileName"></param>
|
||||
/// <param name="userId"></param>
|
||||
/// <param name="companyId"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="mapper"></param>
|
||||
/// <returns></returns>
|
||||
public async Task Export<T>(List<T> dataList, string fileName, int userId, int companyId, FileDownLoadOrderType type, Mapper mapper = null, int? supplierId = null)
|
||||
{
|
||||
FileDownManager entity = new FileDownManager(userId, companyId, type, _option.Url + fileName, supplierId);
|
||||
using (var scope = _serviceScopeFactory.CreateScope())
|
||||
{
|
||||
var _fileDownManagerService = scope.ServiceProvider.GetRequiredService<IFileDownManagerRepositories>();
|
||||
entity = await _fileDownManagerService.Add(entity);
|
||||
List<List<T>> listz = new List<List<T>>();
|
||||
listz.Add(dataList);
|
||||
|
||||
string msg = await Upload(listz, fileName, userId, type);
|
||||
|
||||
entity.Finish(string.IsNullOrEmpty(msg) == true ? true : false, msg);
|
||||
await _fileDownManagerService.Edit(entity);
|
||||
return;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 全字段导出
|
||||
/// </summary>
|
||||
/// <typeparam name="Response"></typeparam>
|
||||
/// <typeparam name="Request"></typeparam>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="fileName"></param>
|
||||
/// <param name="userId"></param>
|
||||
/// <param name="companyId"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="mapper"></param>
|
||||
/// <returns></returns>
|
||||
public Task ExportAll<Response, Request>(Request request, string fileName, int userId, int companyId, FileDownLoadOrderType type, Mapper mapper = null, int? supplierId = null) where Request : PaginationBaseRequestDto
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
//FileDownManager entity = new FileDownManager(userId, companyId, type, _option.Url + fileName, supplierId);
|
||||
|
||||
//request.PageNo = 1;
|
||||
//request.PageSize = _option.PageSize;
|
||||
//List<List<Response>> listz = new List<List<Response>>();
|
||||
//string msg = "";
|
||||
//using (var scope = _serviceScopeFactory.CreateScope())
|
||||
//{
|
||||
// var _fileDownManagerService = scope.ServiceProvider.GetRequiredService<IFileDownManagerRepositories>();
|
||||
// entity = await _fileDownManagerService.Add(entity);
|
||||
// try
|
||||
// {
|
||||
// var _service = scope.ServiceProvider.GetRequiredService<IAllFielRepositories<Request>>();
|
||||
// _logger.LogInformation($"{DateTime.Now}--开始访问数据");
|
||||
// var (obj, total) = await _service.GetListAllField(request, companyId);
|
||||
// var list = (List<Response>)obj;
|
||||
// var page = Math.Ceiling(Convert.ToDecimal(total) / _option.PageSize);
|
||||
|
||||
// listz.Add(list);
|
||||
// for (int i = 1; i < page; i++)
|
||||
// {
|
||||
// request.PageNo++;
|
||||
// var (obj_f, total_f) = await _service.GetListAllField(request, companyId);
|
||||
// var list_f = (List<Response>)obj_f;
|
||||
// listz.Add(list_f);
|
||||
// }
|
||||
// _logger.LogInformation($"{DateTime.Now}--访问数据成功 总数:{total}");
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// msg = $"未找到数据 {ex.ToString()}";
|
||||
// _logger.LogError($"导出异常:{ex.ToString()}");
|
||||
// }
|
||||
|
||||
// if (!string.IsNullOrEmpty(msg))
|
||||
// {
|
||||
// entity.Finish(false, msg);
|
||||
// await _fileDownManagerService.Edit(entity);
|
||||
// return;
|
||||
// }
|
||||
|
||||
// msg = await Upload(listz, fileName, userId, type);
|
||||
|
||||
// entity.Finish(string.IsNullOrEmpty(msg) == true ? true : false, msg);
|
||||
// await _fileDownManagerService.Edit(entity);
|
||||
// return;
|
||||
//}
|
||||
}
|
||||
|
||||
public async Task ExportList<Response, Request>(Request request, string fileName, int userId, int companyId, FileDownLoadOrderType type, Mapper mapper = null, int? supplierId = null) where Request : PaginationBaseRequestDto
|
||||
{
|
||||
FileDownManager entity = new FileDownManager(userId, companyId, type, _option.Url + fileName, supplierId);
|
||||
|
||||
request.PageNo = 1;
|
||||
request.PageSize = _option.PageSize;
|
||||
List<List<Response>> listz = new List<List<Response>>();
|
||||
string msg = "";
|
||||
using (var scope = _serviceScopeFactory.CreateScope())
|
||||
{
|
||||
var _fileDownManagerService = scope.ServiceProvider.GetRequiredService<IFileDownManagerRepositories>();
|
||||
entity = await _fileDownManagerService.Add(entity);
|
||||
try
|
||||
{
|
||||
var _service = scope.ServiceProvider.GetRequiredService<IAllFielRepositories<Request>>();
|
||||
_logger.LogInformation($"{DateTime.Now}--开始访问数据");
|
||||
var (obj, total) = await _service.GetListField(request, companyId);
|
||||
var list = (List<Response>)obj;
|
||||
var page = Math.Ceiling(Convert.ToDecimal(total) / _option.PageSize);
|
||||
|
||||
listz.Add(list);
|
||||
for (int i = 1; i < page; i++)
|
||||
{
|
||||
request.PageNo++;
|
||||
var (obj_f, total_f) = await _service.GetListField(request, companyId);
|
||||
var list_f = (List<Response>)obj_f;
|
||||
listz.Add(list_f);
|
||||
}
|
||||
_logger.LogInformation($"{DateTime.Now}--访问数据成功 总数:{total}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
msg = "未找到数据";
|
||||
_logger.LogError($"导出数据查询异常 {ex.ToString()}");
|
||||
}
|
||||
|
||||
if (listz.Count <= 0)
|
||||
{
|
||||
entity.Finish(false, msg);
|
||||
await _fileDownManagerService.Edit(entity);
|
||||
return;
|
||||
}
|
||||
|
||||
msg = await Upload(listz, fileName, userId, type);
|
||||
|
||||
entity.Finish(string.IsNullOrEmpty(msg) == true ? true : false, msg);
|
||||
await _fileDownManagerService.Edit(entity);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<string> Upload<T>(List<List<T>> dataList, string fileName, int userId, FileDownLoadOrderType type, Mapper mapper = null)
|
||||
{
|
||||
if (mapper == null)
|
||||
mapper = new Mapper();
|
||||
|
||||
//第一个参数为导出Excel名称
|
||||
//第二个参数为Excel数据来源
|
||||
//第三个参数为导出的Sheet名称
|
||||
//overwrite参数如果是要覆盖已存在的Excel或者新建Excel则为true,如果在原有Excel上追加数据则为false
|
||||
//xlsx参数是用于区分导出的数据格式为xlsx还是xls
|
||||
MemoryStream stream = new MemoryStream();
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < dataList.Count; i++)
|
||||
{
|
||||
mapper.Put<T>(dataList[i], "sheet" + (i + 1), true);
|
||||
}
|
||||
mapper.Save(stream);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return "数据导入Execl异常";
|
||||
}
|
||||
|
||||
|
||||
//mapper.Save(stream, dataList, "sheet1", overwrite: true, xlsx: true);
|
||||
//,但是这里也踩到了一个坑,不过这个是 Npoi 的坑并不是 Npoi.Mapper 的坑,
|
||||
// 那就是 Workbook.Write(stream)的时候会将 stream 关闭,如果继续操作这个 Stream 会报流已关闭的错误
|
||||
//所以这里 是把流先转成byte[] 再转回使用的流
|
||||
try
|
||||
{
|
||||
var middleByte = stream.ToArray();
|
||||
using (MemoryStream streamUpload = new MemoryStream(middleByte))
|
||||
{
|
||||
var res = await _qiniuUploadService.Upload(fileName, streamUpload, true);
|
||||
if (!res.Success)
|
||||
{
|
||||
_logger.LogError($"{DateTime.Now}--上传千牛云失败 原因:{res.Message}");
|
||||
return res.Message;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return "数据上传云端异常";
|
||||
}
|
||||
|
||||
_logger.LogInformation($"{DateTime.Now}--导出成功");
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
73
src/WMS.Web.Domain/Services/QiniuUploadService.cs
Normal file
73
src/WMS.Web.Domain/Services/QiniuUploadService.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
using Newtonsoft.Json;
|
||||
using Qiniu.Http;
|
||||
using Qiniu.Storage;
|
||||
using Qiniu.Util;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WMS.Web.Core.Internal.Results;
|
||||
using WMS.Web.Domain.IService;
|
||||
using WMS.Web.Domain.Options;
|
||||
|
||||
namespace WMS.Web.Domain.Services
|
||||
{
|
||||
public class QiniuUploadService : IQiniuUploadService
|
||||
{
|
||||
private readonly QiniuOptions _option;
|
||||
public QiniuUploadService(IOptions<QiniuOptions> option)
|
||||
{
|
||||
_option = option?.Value;
|
||||
}
|
||||
/// <summary>
|
||||
/// 上传文件
|
||||
/// </summary>
|
||||
/// <param name="fileName"></param>
|
||||
/// <param name="stream"></param>
|
||||
/// <param name="isAutoDelte"></param>
|
||||
/// <returns></returns>
|
||||
public Task<Result<string>> Upload(string fileName, Stream stream, bool isAutoDelte = false)
|
||||
{
|
||||
// 生成(上传)凭证时需要使用此Mac
|
||||
// 这个示例单独使用了一个Settings类,其中包含AccessKey和SecretKey
|
||||
// 实际应用中,请自行设置您的AccessKey和SecretKey
|
||||
Mac mac = new Mac(_option.AccessKey, _option.SecretKey);
|
||||
|
||||
PutPolicy putPolicy = new PutPolicy();
|
||||
// 如果需要设置为"覆盖"上传(如果云端已有同名文件则覆盖),请使用 SCOPE = "BUCKET:KEY"
|
||||
putPolicy.Scope = _option.Bucket + ":" + fileName;
|
||||
// putPolicy.Scope = bucket;
|
||||
// 上传策略有效期(对应于生成的凭证的有效期)
|
||||
putPolicy.SetExpires(3600);
|
||||
// 上传到云端多少天后自动删除该文件,如果不设置(即保持默认默认)则不删除
|
||||
if (isAutoDelte)
|
||||
putPolicy.DeleteAfterDays = 7;
|
||||
string jstr = putPolicy.ToJsonString();
|
||||
string token = Auth.CreateUploadToken(mac, jstr);
|
||||
|
||||
Config config = new Config();
|
||||
// 空间对应的机房 华 东 ZONE_CN_East 华 北 ZONE_CN_North 华 南 ZONE_CN_Sout 北 美 ZONE_US_North 东南亚 ZONE_AS_Singapore
|
||||
config.Zone = Zone.ZONE_CN_South;
|
||||
// 是否使用https域名
|
||||
config.UseHttps = true;
|
||||
// 上传是否使用cdn加速
|
||||
config.UseCdnDomains = true;
|
||||
HttpResult result = null;
|
||||
try
|
||||
{
|
||||
FormUploader fu = new FormUploader(config);
|
||||
result = fu.UploadStream(stream, fileName, token, null);
|
||||
if (result.Code == 200)
|
||||
return Task.FromResult(Result<string>.ReSuccess(_option.Url + fileName));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex);
|
||||
return Task.FromResult(Result<string>.ReFailure("上传文件失败" + ex.ToString(), 0));
|
||||
}
|
||||
return Task.FromResult(Result<string>.ReFailure("上传文件失败" + JsonConvert.SerializeObject(result), 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user