|
@ -11,6 +11,7 @@ using AutoMapper; |
|
|
using Microsoft.AspNetCore.Authorization; |
|
|
using Microsoft.AspNetCore.Authorization; |
|
|
using Microsoft.AspNetCore.Mvc; |
|
|
using Microsoft.AspNetCore.Mvc; |
|
|
using Microsoft.EntityFrameworkCore; |
|
|
using Microsoft.EntityFrameworkCore; |
|
|
|
|
|
using Microsoft.EntityFrameworkCore.Infrastructure; |
|
|
using Microsoft.Extensions.Caching.Distributed; |
|
|
using Microsoft.Extensions.Caching.Distributed; |
|
|
using Microsoft.Extensions.Localization; |
|
|
using Microsoft.Extensions.Localization; |
|
|
using NPOI.SS.UserModel; |
|
|
using NPOI.SS.UserModel; |
|
@ -56,7 +57,6 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto, |
|
|
|
|
|
|
|
|
private IDistributedCache<TEntity> Cache => |
|
|
private IDistributedCache<TEntity> Cache => |
|
|
LazyServiceProvider.LazyGetRequiredService<IDistributedCache<TEntity>>(); |
|
|
LazyServiceProvider.LazyGetRequiredService<IDistributedCache<TEntity>>(); |
|
|
|
|
|
|
|
|
private IMapper _mapper; |
|
|
private IMapper _mapper; |
|
|
|
|
|
|
|
|
#endregion
|
|
|
#endregion
|
|
@ -205,17 +205,18 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto, |
|
|
/// <param name="input"></param>
|
|
|
/// <param name="input"></param>
|
|
|
/// <returns></returns>
|
|
|
/// <returns></returns>
|
|
|
[HttpPut("api/[controller]/base/update-by-id")]
|
|
|
[HttpPut("api/[controller]/base/update-by-id")]
|
|
|
public new async Task<TEntityDto> UpdateAsync(TKey id, TEntityDto input) |
|
|
public new async Task<TEntityDto> UpdateAsync(TKey id, TUpdateInput input) |
|
|
{ |
|
|
{ |
|
|
//return base.UpdateAsync(id, input);
|
|
|
//return base.UpdateAsync(id, input);
|
|
|
|
|
|
|
|
|
await CheckUpdatePolicyAsync().ConfigureAwait(false); |
|
|
await CheckUpdatePolicyAsync().ConfigureAwait(true); |
|
|
var entity = await GetEntityByIdAsync(id).ConfigureAwait(false); |
|
|
var entity = await GetEntityByIdAsync(id).ConfigureAwait(true); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var entity2 = input!.ToObject<TEntity>(); |
|
|
|
|
|
|
|
|
Type t1 = null; |
|
|
Type t1 = null; |
|
|
Type t2 = null; |
|
|
Type t2 = null; |
|
|
|
|
|
Type t3 = typeof(TUpdateInput); |
|
|
|
|
|
|
|
|
#region 给所有字表的 Id和MasterId赋值 否则默认的会是000000-000-....的id 插入时会报错
|
|
|
#region 给所有字表的 Id和MasterId赋值 否则默认的会是000000-000-....的id 插入时会报错
|
|
|
|
|
|
|
|
@ -227,7 +228,7 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto, |
|
|
&& propertyInfo.PropertyType.IsGenericType |
|
|
&& propertyInfo.PropertyType.IsGenericType |
|
|
&& propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(List<>)) |
|
|
&& propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(List<>)) |
|
|
{ |
|
|
{ |
|
|
var listProperty = typeof(TEntityDto).GetProperty("Details"); |
|
|
var listProperty = typeof(TUpdateInput).GetProperty("Details"); |
|
|
if (listProperty != null) |
|
|
if (listProperty != null) |
|
|
{ |
|
|
{ |
|
|
t1 = listProperty.PropertyType.GetGenericArguments()[0]; |
|
|
t1 = listProperty.PropertyType.GetGenericArguments()[0]; |
|
@ -247,7 +248,7 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto, |
|
|
if (listProperty != null) |
|
|
if (listProperty != null) |
|
|
{ |
|
|
{ |
|
|
var listItemType = listProperty.PropertyType.GetGenericArguments()[0]; |
|
|
var listItemType = listProperty.PropertyType.GetGenericArguments()[0]; |
|
|
t2= listProperty.PropertyType.GetGenericArguments()[0]; |
|
|
t2 = listProperty.PropertyType.GetGenericArguments()[0]; |
|
|
|
|
|
|
|
|
// 获取元素类型的 ID 属性
|
|
|
// 获取元素类型的 ID 属性
|
|
|
//var detailIdProperty = listItemType.GetProperty("Id");
|
|
|
//var detailIdProperty = listItemType.GetProperty("Id");
|
|
@ -271,20 +272,20 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto, |
|
|
// }
|
|
|
// }
|
|
|
//}
|
|
|
//}
|
|
|
|
|
|
|
|
|
if (masterIdProperty != null) |
|
|
//if (masterIdProperty != null)
|
|
|
{ |
|
|
//{
|
|
|
// 获取 List 属性的值
|
|
|
// // 获取 List 属性的值
|
|
|
var list = (IList)listProperty.GetValue(entity); |
|
|
// var list = (IList)listProperty.GetValue(entity);
|
|
|
|
|
|
|
|
|
// 遍历 List 集合中的每个元素,给 ID 属性赋值
|
|
|
// // 遍历 List 集合中的每个元素,给 ID 属性赋值
|
|
|
if (list != null) |
|
|
// if (list != null)
|
|
|
{ |
|
|
// {
|
|
|
foreach (var item in list) |
|
|
// foreach (var item in list)
|
|
|
{ |
|
|
// {
|
|
|
masterIdProperty.SetValue(item, id); |
|
|
// masterIdProperty.SetValue(item, id);
|
|
|
} |
|
|
// }
|
|
|
} |
|
|
// }
|
|
|
} |
|
|
//}
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -293,16 +294,19 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto, |
|
|
{ |
|
|
{ |
|
|
// 动态创建映射关系
|
|
|
// 动态创建映射关系
|
|
|
cfg.CreateMap(typeof(TEntityDto), typeof(TEntity)); |
|
|
cfg.CreateMap(typeof(TEntityDto), typeof(TEntity)); |
|
|
cfg.CreateMap(t1,t2); |
|
|
cfg.CreateMap(t1, t2); |
|
|
|
|
|
cfg.CreateMap(typeof(TUpdateInput), typeof(TEntity)); |
|
|
}); |
|
|
}); |
|
|
_mapper = new Mapper(config); |
|
|
_mapper = new Mapper(config); |
|
|
var tt = _mapper.Map(input, entity); |
|
|
var tt = _mapper.Map(input, entity); |
|
|
|
|
|
|
|
|
#endregion
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
await Repository.UpdateAsync(entity, true); |
|
|
//await Repository.UpdateAsync(entity, true);
|
|
|
|
|
|
//await Repository.UpdateAsync(tt, true);
|
|
|
|
|
|
|
|
|
await Cache.SetAsync(entity.Id.ToString(), entity, GetCacheTime()); |
|
|
//await Cache.SetAsync(entity.Id.ToString(), entity, GetCacheTime());
|
|
|
|
|
|
await Repository.UpdateAsync(entity, true); |
|
|
return await MapToGetOutputDtoAsync(entity); |
|
|
return await MapToGetOutputDtoAsync(entity); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|