郑渤旭[Irelia] 2 years ago
parent
commit
bd126d3c43
  1. 115
      Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Application/AppBase/ZbxBase.cs

115
Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Application/AppBase/ZbxBase.cs

@ -1,7 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Linq.Dynamic.Core;
@ -17,20 +16,12 @@ using Faster.Zheng.Winin.Localization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Localization;
using NPOI.POIFS.Macros;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Caching;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.Localization;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
using static OpenIddict.Abstractions.OpenIddictConstants;
using static Volo.Abp.UI.Navigation.DefaultMenuNames.Application;
namespace Faster.Zheng.Winin.AppBase;
@ -44,8 +35,12 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
private readonly IRepository<TEntity, TKey> _repository;
private readonly IMapper _mapper;
private Func<TCreateInput, Entity> _mapFunc;
protected IStringLocalizer<WininResource> _localizer => LazyServiceProvider.LazyGetRequiredService<IStringLocalizer<WininResource>>();
protected ExportCustomUserSettingAppService _exportCustomUserSettingAppService => LazyServiceProvider.LazyGetRequiredService<ExportCustomUserSettingAppService>();
protected IStringLocalizer<WininResource> _localizer =>
LazyServiceProvider.LazyGetRequiredService<IStringLocalizer<WininResource>>();
protected ExportCustomUserSettingAppService _exportCustomUserSettingAppService =>
LazyServiceProvider.LazyGetRequiredService<ExportCustomUserSettingAppService>();
public ZbxBase(IRepository<TEntity, TKey> repository) : base(repository)
@ -92,13 +87,13 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
private async Task<long> GetCountAsync(Expression<Func<TEntity, bool>> expression,
CancellationToken cancellationToken = default)
{
var count = await _repository.LongCountAsync(expression, cancellationToken: cancellationToken);
var count = await _repository.LongCountAsync(expression, cancellationToken);
return count;
}
/// <summary>
/// 【基础】-【新增】
/// 【基础】-【新增】
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
@ -182,7 +177,7 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
}
/// <summary>
/// 【基础】-【删除】
/// 【基础】-【删除】
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
@ -193,7 +188,7 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
}
/// <summary>
/// 【基础】-【修改】
/// 【基础】-【修改】
/// </summary>
/// <param name="id"></param>
/// <param name="input"></param>
@ -203,8 +198,8 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
{
//return base.UpdateAsync(id, input);
await CheckUpdatePolicyAsync().ConfigureAwait(continueOnCapturedContext: false);
TEntity entity = await GetEntityByIdAsync(id).ConfigureAwait(continueOnCapturedContext: false);
await CheckUpdatePolicyAsync().ConfigureAwait(false);
var entity = await GetEntityByIdAsync(id).ConfigureAwait(false);
entity.FromObject(input!);
#region 给所有字表的 Id和MasterId赋值 否则默认的会是000000-000-....的id 插入时会报错
@ -266,13 +261,13 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
#endregion
await Repository.UpdateAsync(entity, autoSave: true);
await Repository.UpdateAsync(entity, true);
return await MapToGetOutputDtoAsync(entity);
}
/// <summary>
/// 【基础】-【导出Excel】【有筛选条件】
/// 【基础】-【导出Excel】【有筛选条件】
/// </summary>
/// <param name="sfsRequestInputBase">查询条件</param>
/// <param name="isRedundance">是否冗余主表数据</param>
@ -280,27 +275,30 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
/// <param name="userId">用户ID</param>
/// <returns></returns>
[HttpPost("api/[controller]/base/export-to-excel")]
public virtual async Task<IActionResult> ExportToExcelAsync(SfsRequestInputBase sfsRequestInputBase,bool isRedundance, Guid userId,bool isDetailExport = true)
public virtual async Task<IActionResult> ExportToExcelAsync(SfsRequestInputBase sfsRequestInputBase,
bool isRedundance, Guid userId, bool isDetailExport = true)
{
var isHasDetail = false;//是否包含从表
var isHasDetail = false; //是否包含从表
var data = (await GetPageListByFilterAsync(sfsRequestInputBase,true)).Items;
var data = (await GetPageListByFilterAsync(sfsRequestInputBase, true)).Items;
var fileStream = new MemoryStream();//文件流
var fileStream = new MemoryStream(); //文件流
IWorkbook workbook = new XSSFWorkbook();
ISheet sheet = workbook.CreateSheet(typeof(TEntityDto).Name);
int splitDetailsColumnNumber = 1;//分割主表和从表的列数量
var excelDetailsColor = SetExcelDetailsColor(workbook);//字表样式
var sheet = workbook.CreateSheet(typeof(TEntityDto).Name);
var splitDetailsColumnNumber = 1; //分割主表和从表的列数量
var excelDetailsColor = SetExcelDetailsColor(workbook); //字表样式
// 获取主表的属性 创建主表 表头
var mainAllProperties = typeof(TEntityDto).GetProperties();
var mainProperties= mainAllProperties.Where(p=>p.Name!= "Details").ToArray();//去除details属性否则导出时会带出来
var mainProperties = mainAllProperties.Where(p => p.Name != "Details").ToArray(); //去除details属性否则导出时会带出来
#region 用户个性导出 主表
//获取个性导出的字段
var mainUserColumn = await _exportCustomUserSettingAppService.GetByUserIdAndExportTableNameAsync(userId, typeof(TEntity).Name);
var mainNotShowUserColumn = mainUserColumn.Where(p => p.CustomUserSetting == Enum_ExportCustomUserSetting.No).Select(p => p.ExportColumnName).ToList();
var mainUserColumn =
await _exportCustomUserSettingAppService.GetByUserIdAndExportTableNameAsync(userId, typeof(TEntity).Name);
var mainNotShowUserColumn = mainUserColumn.Where(p => p.CustomUserSetting == Enum_ExportCustomUserSetting.No)
.Select(p => p.ExportColumnName).ToList();
if (mainUserColumn.Any())
{
mainProperties = mainProperties.Where(p => !mainNotShowUserColumn.Contains(p.Name)).ToArray();
@ -308,31 +306,34 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
#endregion
IRow headerRow = sheet.CreateRow(0);
for (int i = 0; i < mainProperties.Length; i++)
var headerRow = sheet.CreateRow(0);
for (var i = 0; i < mainProperties.Length; i++)
{
var englishName=mainProperties[i].Name;
var englishName = mainProperties[i].Name;
//本地化
var localizerName = _localizer[typeof(TEntity).Name+ englishName];
var localizerName = _localizer[typeof(TEntity).Name + englishName];
headerRow.CreateCell(i).SetCellValue(localizerName);
}
// 获取从表的属性 创建从表 表头
var detailProperties = typeof(TEntityDto).GetProperty("Details")?.PropertyType.GetGenericArguments()[0].GetProperties();
var detailProperties = typeof(TEntityDto).GetProperty("Details")?.PropertyType.GetGenericArguments()[0]
.GetProperties();
#region 用户个性导出 主表
#endregion
if (detailProperties != null)
{
isHasDetail = true;
if (!isDetailExport)//是否要导出子表
if (!isDetailExport) //是否要导出子表
{
isHasDetail = false;
}
if (isHasDetail)
{
headerRow.CreateCell(mainProperties.Length ).SetCellValue("---分割---");
headerRow.CreateCell(mainProperties.Length).SetCellValue("---分割---");
#region 用户个性导出 从表
@ -340,7 +341,9 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
var detailDtoName = mainAllProperties.First(p => p.Name == "Details");
var detailUserColumn = await _exportCustomUserSettingAppService.GetByUserIdAndExportTableNameAsync(
userId, detailDtoName.PropertyType.GenericTypeArguments.First().Name.Replace("Dto", ""));
var detailNotShowUserColumn = detailUserColumn.Where(p => p.CustomUserSetting == Enum_ExportCustomUserSetting.No).Select(p => p.ExportColumnName).ToList();
var detailNotShowUserColumn = detailUserColumn
.Where(p => p.CustomUserSetting == Enum_ExportCustomUserSetting.No).Select(p => p.ExportColumnName)
.ToList();
if (detailUserColumn.Any())
{
detailProperties = detailProperties.Where(p => !detailNotShowUserColumn.Contains(p.Name)).ToArray();
@ -348,7 +351,7 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
#endregion
for (int i = 0; i < detailProperties.Length; i++)
for (var i = 0; i < detailProperties.Length; i++)
{
headerRow.CreateCell(mainProperties.Length + splitDetailsColumnNumber + i)
.SetCellValue(detailProperties[i].Name);
@ -359,7 +362,7 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
}
// 填充数据行
int rowIndex = 1;
var rowIndex = 1;
foreach (var mainDto in data)
{
if (isHasDetail)
@ -369,14 +372,14 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
//从表
var detailList = (IEnumerable<object>)mainAllProperties[detailsIndex].GetValue(mainDto);
var startMainRowIndex = rowIndex;
for (int datailCount = 0; datailCount < detailList.Count(); datailCount++)
for (var datailCount = 0; datailCount < detailList.Count(); datailCount++)
{
IRow dataRow = sheet.CreateRow(rowIndex);
var dataRow = sheet.CreateRow(rowIndex);
if (isRedundance)
{
// 填充主表数据
for (int i = 0; i < mainProperties.Length; i++)
for (var i = 0; i < mainProperties.Length; i++)
{
var value = mainProperties[i].GetValue(mainDto);
dataRow.CreateCell(i).SetCellValue(value?.ToString());
@ -387,7 +390,7 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
if (datailCount == 0)
{
// 填充主表数据
for (int i = 0; i < mainProperties.Length; i++)
for (var i = 0; i < mainProperties.Length; i++)
{
var value = mainProperties[i].GetValue(mainDto);
dataRow.CreateCell(i).SetCellValue(value?.ToString());
@ -405,10 +408,11 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
{
//填充子表数据
var detailRow = sheet.GetRow(startMainRowIndex);
for (int i = 0; i < detailProperties.Length; i++)
for (var i = 0; i < detailProperties.Length; i++)
{
var value = detailProperties[i].GetValue(detail);
detailRow.CreateCell(mainProperties.Length + splitDetailsColumnNumber + i).SetCellValue(value?.ToString());
detailRow.CreateCell(mainProperties.Length + splitDetailsColumnNumber + i)
.SetCellValue(value?.ToString());
var detailCell = detailRow.GetCell(mainProperties.Length + splitDetailsColumnNumber + i);
detailCell.CellStyle = excelDetailsColor;
}
@ -419,9 +423,9 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
}
else
{
IRow dataRow = sheet.CreateRow(rowIndex);
var dataRow = sheet.CreateRow(rowIndex);
// 填充主表数据
for (int i = 0; i < mainProperties.Length; i++)
for (var i = 0; i < mainProperties.Length; i++)
{
var value = mainProperties[i].GetValue(mainDto);
dataRow.CreateCell(i).SetCellValue(value?.ToString());
@ -437,15 +441,15 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
if (isHasDetail)
{
// 自动调整列宽
for (int i = 0; i < mainProperties.Length + detailProperties.Length; i++)
for (var i = 0; i < mainProperties.Length + detailProperties.Length; i++)
{
sheet.AutoSizeColumn(i+splitDetailsColumnNumber);
sheet.AutoSizeColumn(i + splitDetailsColumnNumber);
}
}
else
{
// 自动调整列宽
for (int i = 0; i < mainProperties.Length; i++)
for (var i = 0; i < mainProperties.Length; i++)
{
sheet.AutoSizeColumn(i);
}
@ -454,13 +458,14 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
#endregion
// 保存Excel文件到MemoryStream
workbook.Write(fileStream,true);
workbook.Write(fileStream, true);
fileStream.Position = 0;
// 创建FileContentResult返回Excel文件
var fileContentResult = new FileContentResult(fileStream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
var fileContentResult = new FileContentResult(fileStream.ToArray(),
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
{
FileDownloadName = typeof(TEntityDto).Name+".xlsx"
FileDownloadName = typeof(TEntityDto).Name + ".xlsx"
};
await Task.CompletedTask;
@ -504,7 +509,7 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
}
/// <summary>
/// 设置排序
/// 设置排序
/// </summary>
/// <param name="entities"></param>
/// <param name="sorting"></param>
@ -540,7 +545,7 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
}
/// <summary>
/// 导出设置子表单元格样式
/// 导出设置子表单元格样式
/// </summary>
/// <param name="workbook"></param>
/// <returns></returns>

Loading…
Cancel
Save