|
|
@ -1,6 +1,7 @@ |
|
|
|
using System; |
|
|
|
using System.Collections; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.IO; |
|
|
|
using System.Linq; |
|
|
|
using System.Linq.Dynamic.Core; |
|
|
|
using System.Linq.Expressions; |
|
|
@ -8,8 +9,11 @@ using System.Threading; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using AutoMapper; |
|
|
|
using Faster.Zheng.Winin.AppBase.Filters; |
|
|
|
using Faster.Zheng.Winin.Extensions; |
|
|
|
using Microsoft.AspNetCore.Mvc; |
|
|
|
using Microsoft.EntityFrameworkCore; |
|
|
|
using NPOI.SS.UserModel; |
|
|
|
using NPOI.XSSF.UserModel; |
|
|
|
using Volo.Abp.Application.Dtos; |
|
|
|
using Volo.Abp.Application.Services; |
|
|
|
using Volo.Abp.Domain.Entities; |
|
|
@ -17,6 +21,7 @@ using Volo.Abp.Domain.Repositories; |
|
|
|
using Volo.Abp.EntityFrameworkCore; |
|
|
|
using static Microsoft.EntityFrameworkCore.DbLoggerCategory; |
|
|
|
using static OpenIddict.Abstractions.OpenIddictConstants; |
|
|
|
using static Volo.Abp.UI.Navigation.DefaultMenuNames.Application; |
|
|
|
|
|
|
|
namespace Faster.Zheng.Winin.AppBase; |
|
|
|
|
|
|
@ -92,20 +97,7 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto, |
|
|
|
{ |
|
|
|
await CheckCreatePolicyAsync(); |
|
|
|
|
|
|
|
var mapperConfig = new MapperConfiguration(cfg => |
|
|
|
{ |
|
|
|
cfg.CreateMap<TCreateInput, TEntity>(); |
|
|
|
}); |
|
|
|
var _mapper = mapperConfig.CreateMapper(); |
|
|
|
var source = Expression.Parameter(typeof(TCreateInput), "source"); |
|
|
|
_mapFunc = Expression.Lambda<Func<TCreateInput, TEntity>>( |
|
|
|
Expression.Invoke(Expression.Constant(_mapper.Map<TCreateInput, TEntity>)), source) |
|
|
|
.Compile(); |
|
|
|
|
|
|
|
|
|
|
|
var tt= _mapper.Map<TCreateInput, TEntity>(input); |
|
|
|
|
|
|
|
var entity = await MapToEntityAsync(input); |
|
|
|
var entity = input!.ToObject<TEntity>(); |
|
|
|
|
|
|
|
//判断id是否是00000-0000 如果是则赋值
|
|
|
|
var mainId = (Guid)entity.GetType().GetProperty("Id")?.GetValue(entity)!; |
|
|
@ -197,9 +189,119 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto, |
|
|
|
/// <param name="input"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
[HttpPut("api/[controller]/base/update-by-id")]
|
|
|
|
public override Task<TEntityDto> UpdateAsync(TKey id, TUpdateInput input) |
|
|
|
public override async Task<TEntityDto> UpdateAsync(TKey id, TUpdateInput input) |
|
|
|
{ |
|
|
|
return base.UpdateAsync(id, input); |
|
|
|
//return base.UpdateAsync(id, input);
|
|
|
|
|
|
|
|
await CheckUpdatePolicyAsync().ConfigureAwait(continueOnCapturedContext: false); |
|
|
|
TEntity entity = await GetEntityByIdAsync(id).ConfigureAwait(continueOnCapturedContext: false); |
|
|
|
entity.FromObject(input!); |
|
|
|
|
|
|
|
#region 给所有字表的 Id和MasterId赋值 否则默认的会是000000-000-....的id 插入时会报错
|
|
|
|
|
|
|
|
var propertyInfos = entity.GetType().GetProperties(); |
|
|
|
foreach (var propertyInfo in propertyInfos) |
|
|
|
{ |
|
|
|
//判断是否是List集合
|
|
|
|
if (propertyInfo.Name == "Details" |
|
|
|
&& propertyInfo.PropertyType.IsGenericType |
|
|
|
&& propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(List<>)) |
|
|
|
{ |
|
|
|
var listProperty = typeof(TEntity).GetProperty("Details"); |
|
|
|
|
|
|
|
// 获取 List 的元素类型
|
|
|
|
if (listProperty != null) |
|
|
|
{ |
|
|
|
var listItemType = listProperty.PropertyType.GetGenericArguments()[0]; |
|
|
|
|
|
|
|
// 获取元素类型的 ID 属性
|
|
|
|
var detailIdProperty = listItemType.GetProperty("Id"); |
|
|
|
var masterIdProperty = listItemType.GetProperty("MasterId"); |
|
|
|
|
|
|
|
if (detailIdProperty != null) |
|
|
|
{ |
|
|
|
// 获取 List 属性的值
|
|
|
|
var list = (IList)listProperty.GetValue(entity); |
|
|
|
|
|
|
|
// 遍历 List 集合中的每个元素,给 ID 属性赋值
|
|
|
|
if (list != null) |
|
|
|
{ |
|
|
|
foreach (var item in list) |
|
|
|
{ |
|
|
|
if ((Guid)detailIdProperty.GetValue(item)! == Guid.Empty) |
|
|
|
{ |
|
|
|
detailIdProperty.SetValue(item, Guid.NewGuid()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (masterIdProperty != null) |
|
|
|
{ |
|
|
|
// 获取 List 属性的值
|
|
|
|
var list = (IList)listProperty.GetValue(entity); |
|
|
|
|
|
|
|
// 遍历 List 集合中的每个元素,给 ID 属性赋值
|
|
|
|
if (list != null) |
|
|
|
{ |
|
|
|
foreach (var item in list) |
|
|
|
{ |
|
|
|
masterIdProperty.SetValue(item, id); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
await Repository.UpdateAsync(entity, autoSave: true); |
|
|
|
|
|
|
|
return await MapToGetOutputDtoAsync(entity); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 导出Excel
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
/// <param name="data"></param>
|
|
|
|
/// <param name="filePath"></param>
|
|
|
|
[HttpPost("api/[controller]/base/export-to-excel")]
|
|
|
|
public void ExportToExcel<T>(IEnumerable<T> data, string filePath) |
|
|
|
{ |
|
|
|
IWorkbook workbook = new XSSFWorkbook(); |
|
|
|
ISheet sheet = workbook.CreateSheet("Sheet1"); |
|
|
|
|
|
|
|
// 获取泛型参数的类型
|
|
|
|
var type = typeof(T); |
|
|
|
var properties = type.GetProperties(); |
|
|
|
|
|
|
|
// 创建表头
|
|
|
|
IRow headerRow = sheet.CreateRow(0); |
|
|
|
for (int i = 0; i < properties.Length; i++) |
|
|
|
{ |
|
|
|
headerRow.CreateCell(i).SetCellValue(properties[i].Name); |
|
|
|
} |
|
|
|
|
|
|
|
// 填充数据行
|
|
|
|
int rowIndex = 1; |
|
|
|
foreach (var item in data) |
|
|
|
{ |
|
|
|
IRow dataRow = sheet.CreateRow(rowIndex); |
|
|
|
for (int i = 0; i < properties.Length; i++) |
|
|
|
{ |
|
|
|
var value = properties[i].GetValue(item); |
|
|
|
dataRow.CreateCell(i).SetCellValue(value?.ToString()); |
|
|
|
} |
|
|
|
rowIndex++; |
|
|
|
} |
|
|
|
|
|
|
|
// 保存Excel文件
|
|
|
|
using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write)) |
|
|
|
{ |
|
|
|
workbook.Write(fileStream,true); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
#endregion
|
|
|
|