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

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

@ -1,7 +1,6 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Linq.Dynamic.Core; using System.Linq.Dynamic.Core;
@ -17,20 +16,12 @@ using Faster.Zheng.Winin.Localization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Localization; using Microsoft.Extensions.Localization;
using NPOI.POIFS.Macros;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel; using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel; using NPOI.XSSF.UserModel;
using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services; using Volo.Abp.Application.Services;
using Volo.Abp.Caching;
using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories; 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; namespace Faster.Zheng.Winin.AppBase;
@ -44,8 +35,12 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
private readonly IRepository<TEntity, TKey> _repository; private readonly IRepository<TEntity, TKey> _repository;
private readonly IMapper _mapper; private readonly IMapper _mapper;
private Func<TCreateInput, Entity> _mapFunc; 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) public ZbxBase(IRepository<TEntity, TKey> repository) : base(repository)
@ -92,7 +87,7 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
private async Task<long> GetCountAsync(Expression<Func<TEntity, bool>> expression, private async Task<long> GetCountAsync(Expression<Func<TEntity, bool>> expression,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
var count = await _repository.LongCountAsync(expression, cancellationToken: cancellationToken); var count = await _repository.LongCountAsync(expression, cancellationToken);
return count; return count;
} }
@ -203,8 +198,8 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
{ {
//return base.UpdateAsync(id, input); //return base.UpdateAsync(id, input);
await CheckUpdatePolicyAsync().ConfigureAwait(continueOnCapturedContext: false); await CheckUpdatePolicyAsync().ConfigureAwait(false);
TEntity entity = await GetEntityByIdAsync(id).ConfigureAwait(continueOnCapturedContext: false); var entity = await GetEntityByIdAsync(id).ConfigureAwait(false);
entity.FromObject(input!); entity.FromObject(input!);
#region 给所有字表的 Id和MasterId赋值 否则默认的会是000000-000-....的id 插入时会报错 #region 给所有字表的 Id和MasterId赋值 否则默认的会是000000-000-....的id 插入时会报错
@ -266,7 +261,7 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
#endregion #endregion
await Repository.UpdateAsync(entity, autoSave: true); await Repository.UpdateAsync(entity, true);
return await MapToGetOutputDtoAsync(entity); return await MapToGetOutputDtoAsync(entity);
} }
@ -280,7 +275,8 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
/// <param name="userId">用户ID</param> /// <param name="userId">用户ID</param>
/// <returns></returns> /// <returns></returns>
[HttpPost("api/[controller]/base/export-to-excel")] [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; //是否包含从表
@ -288,8 +284,8 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
var fileStream = new MemoryStream(); //文件流 var fileStream = new MemoryStream(); //文件流
IWorkbook workbook = new XSSFWorkbook(); IWorkbook workbook = new XSSFWorkbook();
ISheet sheet = workbook.CreateSheet(typeof(TEntityDto).Name); var sheet = workbook.CreateSheet(typeof(TEntityDto).Name);
int splitDetailsColumnNumber = 1;//分割主表和从表的列数量 var splitDetailsColumnNumber = 1; //分割主表和从表的列数量
var excelDetailsColor = SetExcelDetailsColor(workbook); //字表样式 var excelDetailsColor = SetExcelDetailsColor(workbook); //字表样式
// 获取主表的属性 创建主表 表头 // 获取主表的属性 创建主表 表头
@ -299,8 +295,10 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
#region 用户个性导出 主表 #region 用户个性导出 主表
//获取个性导出的字段 //获取个性导出的字段
var mainUserColumn = await _exportCustomUserSettingAppService.GetByUserIdAndExportTableNameAsync(userId, typeof(TEntity).Name); var mainUserColumn =
var mainNotShowUserColumn = mainUserColumn.Where(p => p.CustomUserSetting == Enum_ExportCustomUserSetting.No).Select(p => p.ExportColumnName).ToList(); 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()) if (mainUserColumn.Any())
{ {
mainProperties = mainProperties.Where(p => !mainNotShowUserColumn.Contains(p.Name)).ToArray(); mainProperties = mainProperties.Where(p => !mainNotShowUserColumn.Contains(p.Name)).ToArray();
@ -308,8 +306,8 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
#endregion #endregion
IRow headerRow = sheet.CreateRow(0); var headerRow = sheet.CreateRow(0);
for (int i = 0; i < mainProperties.Length; i++) for (var i = 0; i < mainProperties.Length; i++)
{ {
var englishName = mainProperties[i].Name; var englishName = mainProperties[i].Name;
//本地化 //本地化
@ -318,10 +316,13 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
} }
// 获取从表的属性 创建从表 表头 // 获取从表的属性 创建从表 表头
var detailProperties = typeof(TEntityDto).GetProperty("Details")?.PropertyType.GetGenericArguments()[0].GetProperties(); var detailProperties = typeof(TEntityDto).GetProperty("Details")?.PropertyType.GetGenericArguments()[0]
.GetProperties();
#region 用户个性导出 主表 #region 用户个性导出 主表
#endregion #endregion
if (detailProperties != null) if (detailProperties != null)
{ {
isHasDetail = true; isHasDetail = true;
@ -340,7 +341,9 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
var detailDtoName = mainAllProperties.First(p => p.Name == "Details"); var detailDtoName = mainAllProperties.First(p => p.Name == "Details");
var detailUserColumn = await _exportCustomUserSettingAppService.GetByUserIdAndExportTableNameAsync( var detailUserColumn = await _exportCustomUserSettingAppService.GetByUserIdAndExportTableNameAsync(
userId, detailDtoName.PropertyType.GenericTypeArguments.First().Name.Replace("Dto", "")); 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()) if (detailUserColumn.Any())
{ {
detailProperties = detailProperties.Where(p => !detailNotShowUserColumn.Contains(p.Name)).ToArray(); detailProperties = detailProperties.Where(p => !detailNotShowUserColumn.Contains(p.Name)).ToArray();
@ -348,7 +351,7 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
#endregion #endregion
for (int i = 0; i < detailProperties.Length; i++) for (var i = 0; i < detailProperties.Length; i++)
{ {
headerRow.CreateCell(mainProperties.Length + splitDetailsColumnNumber + i) headerRow.CreateCell(mainProperties.Length + splitDetailsColumnNumber + i)
.SetCellValue(detailProperties[i].Name); .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) foreach (var mainDto in data)
{ {
if (isHasDetail) if (isHasDetail)
@ -369,14 +372,14 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
//从表 //从表
var detailList = (IEnumerable<object>)mainAllProperties[detailsIndex].GetValue(mainDto); var detailList = (IEnumerable<object>)mainAllProperties[detailsIndex].GetValue(mainDto);
var startMainRowIndex = rowIndex; 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) if (isRedundance)
{ {
// 填充主表数据 // 填充主表数据
for (int i = 0; i < mainProperties.Length; i++) for (var i = 0; i < mainProperties.Length; i++)
{ {
var value = mainProperties[i].GetValue(mainDto); var value = mainProperties[i].GetValue(mainDto);
dataRow.CreateCell(i).SetCellValue(value?.ToString()); dataRow.CreateCell(i).SetCellValue(value?.ToString());
@ -387,7 +390,7 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
if (datailCount == 0) 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); var value = mainProperties[i].GetValue(mainDto);
dataRow.CreateCell(i).SetCellValue(value?.ToString()); dataRow.CreateCell(i).SetCellValue(value?.ToString());
@ -405,10 +408,11 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
{ {
//填充子表数据 //填充子表数据
var detailRow = sheet.GetRow(startMainRowIndex); 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); 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); var detailCell = detailRow.GetCell(mainProperties.Length + splitDetailsColumnNumber + i);
detailCell.CellStyle = excelDetailsColor; detailCell.CellStyle = excelDetailsColor;
} }
@ -419,9 +423,9 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
} }
else 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); var value = mainProperties[i].GetValue(mainDto);
dataRow.CreateCell(i).SetCellValue(value?.ToString()); dataRow.CreateCell(i).SetCellValue(value?.ToString());
@ -437,7 +441,7 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
if (isHasDetail) 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);
} }
@ -445,7 +449,7 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
else else
{ {
// 自动调整列宽 // 自动调整列宽
for (int i = 0; i < mainProperties.Length; i++) for (var i = 0; i < mainProperties.Length; i++)
{ {
sheet.AutoSizeColumn(i); sheet.AutoSizeColumn(i);
} }
@ -458,7 +462,8 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
fileStream.Position = 0; fileStream.Position = 0;
// 创建FileContentResult返回Excel文件 // 创建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"
}; };

Loading…
Cancel
Save