|
|
@ -11,12 +11,11 @@ using AutoMapper; |
|
|
|
using Microsoft.AspNetCore.Authorization; |
|
|
|
using Microsoft.AspNetCore.Mvc; |
|
|
|
using Microsoft.EntityFrameworkCore; |
|
|
|
using Microsoft.EntityFrameworkCore.Infrastructure; |
|
|
|
using Microsoft.Extensions.Caching.Distributed; |
|
|
|
using Microsoft.Extensions.Localization; |
|
|
|
using NPOI.SS.UserModel; |
|
|
|
using NPOI.XSSF.UserModel; |
|
|
|
using Omu.ValueInjecter; |
|
|
|
using Volo.Abp; |
|
|
|
using Volo.Abp.Application.Dtos; |
|
|
|
using Volo.Abp.Application.Services; |
|
|
|
using Volo.Abp.Caching; |
|
|
@ -29,7 +28,6 @@ using WinIn.FasterZ.Store.Enums; |
|
|
|
using WinIn.FasterZ.Wms.AppBase.Extensions; |
|
|
|
using WinIn.FasterZ.Wms.AppBaseBusiness.ExportCustomUserSetting; |
|
|
|
using WinIn.FasterZ.Wms.Localization; |
|
|
|
using Mapper = AutoMapper.Mapper; |
|
|
|
|
|
|
|
namespace WinIn.FasterZ.Wms.AppBase; |
|
|
|
|
|
|
@ -43,12 +41,14 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto, |
|
|
|
#region 定义
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// redis缓存时间 分钟
|
|
|
|
/// redis缓存时间 分钟
|
|
|
|
/// </summary>
|
|
|
|
private const int CacheMinute = 30; |
|
|
|
|
|
|
|
private readonly IRepository<TEntity, TKey> _repository; |
|
|
|
|
|
|
|
private IMapper _mapper; |
|
|
|
|
|
|
|
private IStringLocalizer<WmsResource> Localizer => |
|
|
|
LazyServiceProvider.LazyGetRequiredService<IStringLocalizer<WmsResource>>(); |
|
|
|
|
|
|
@ -57,7 +57,6 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto, |
|
|
|
|
|
|
|
private IDistributedCache<TEntity> Cache => |
|
|
|
LazyServiceProvider.LazyGetRequiredService<IDistributedCache<TEntity>>(); |
|
|
|
private IMapper _mapper; |
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
@ -129,8 +128,8 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto, |
|
|
|
{ |
|
|
|
//判断是否是List集合
|
|
|
|
if (propertyInfo.Name == "Details" |
|
|
|
&& propertyInfo.PropertyType.IsGenericType |
|
|
|
&& propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(List<>)) |
|
|
|
&& propertyInfo.PropertyType.IsGenericType |
|
|
|
&& propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(List<>)) |
|
|
|
{ |
|
|
|
var listProperty = typeof(TEntity).GetProperty("Details"); |
|
|
|
|
|
|
@ -211,101 +210,74 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto, |
|
|
|
|
|
|
|
await CheckUpdatePolicyAsync().ConfigureAwait(true); |
|
|
|
var entity = await GetEntityByIdAsync(id).ConfigureAwait(true); |
|
|
|
if (entity.GetType().GetProperty("ConcurrencyStamp").GetValue(entity).ToString() != |
|
|
|
input.GetType().GetProperty("ConcurrencyStamp").GetValue(input).ToString()) |
|
|
|
{ |
|
|
|
throw new UserFriendlyException($"您操作的数据已经被修改:\r\n" + |
|
|
|
$"已经由【{Newtonsoft.Json.JsonConvert.SerializeObject(entity)}】\r\n" + |
|
|
|
$"变更为【{Newtonsoft.Json.JsonConvert.SerializeObject(input)}】\r\n"); |
|
|
|
} |
|
|
|
|
|
|
|
var entity2 = input!.ToObject<TEntity>(); |
|
|
|
Type inputDetailDtoType = null; |
|
|
|
Type entityDetailType = null; |
|
|
|
bool inputDetailDtoTypeFlag = false; |
|
|
|
bool entityDetailTypeFlag = false; |
|
|
|
|
|
|
|
Type t1 = null; |
|
|
|
Type t2 = null; |
|
|
|
Type t3 = typeof(TUpdateInput); |
|
|
|
var t3 = typeof(TUpdateInput); |
|
|
|
|
|
|
|
#region 给所有字表的 Id和MasterId赋值 否则默认的会是000000-000-....的id 插入时会报错
|
|
|
|
var entityProperties = entity.GetType().GetProperties(); |
|
|
|
var inputProperties = input.GetType().GetProperties(); |
|
|
|
|
|
|
|
var propertyInfos = entity.GetType().GetProperties(); |
|
|
|
var dtolist = input.GetType().GetProperties(); |
|
|
|
foreach (var propertyInfo in dtolist) |
|
|
|
//InputDto的
|
|
|
|
foreach (var propertyInfo in inputProperties) |
|
|
|
{ |
|
|
|
if (propertyInfo.Name == "Details" |
|
|
|
&& propertyInfo.PropertyType.IsGenericType |
|
|
|
&& propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(List<>)) |
|
|
|
&& propertyInfo.PropertyType.IsGenericType |
|
|
|
&& propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(List<>)) |
|
|
|
{ |
|
|
|
var listProperty = typeof(TUpdateInput).GetProperty("Details"); |
|
|
|
if (listProperty != null) |
|
|
|
{ |
|
|
|
t1 = listProperty.PropertyType.GetGenericArguments()[0]; |
|
|
|
inputDetailDtoType = listProperty.PropertyType.GetGenericArguments()[0]; |
|
|
|
inputDetailDtoTypeFlag = true; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
foreach (var propertyInfo in propertyInfos) |
|
|
|
if (inputDetailDtoTypeFlag == true) |
|
|
|
{ |
|
|
|
//判断是否是List集合
|
|
|
|
if (propertyInfo.Name == "Details" |
|
|
|
&& propertyInfo.PropertyType.IsGenericType |
|
|
|
&& propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(List<>)) |
|
|
|
//实体的
|
|
|
|
foreach (var propertyInfo in entityProperties) |
|
|
|
{ |
|
|
|
var listProperty = typeof(TEntity).GetProperty("Details"); |
|
|
|
// 获取 List 的元素类型
|
|
|
|
if (listProperty != null) |
|
|
|
//判断是否是List集合
|
|
|
|
if (propertyInfo.Name == "Details" |
|
|
|
&& propertyInfo.PropertyType.IsGenericType |
|
|
|
&& propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(List<>)) |
|
|
|
{ |
|
|
|
var listItemType = listProperty.PropertyType.GetGenericArguments()[0]; |
|
|
|
t2 = 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);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
var listProperty = typeof(TEntity).GetProperty("Details"); |
|
|
|
// 获取 List 的元素类型
|
|
|
|
if (listProperty != null) |
|
|
|
{ |
|
|
|
entityDetailType = listProperty.PropertyType.GetGenericArguments()[0]; |
|
|
|
entityDetailTypeFlag = true; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
var config = new MapperConfiguration(cfg => |
|
|
|
if (inputDetailDtoTypeFlag == true && entityDetailTypeFlag==true) |
|
|
|
{ |
|
|
|
// 动态创建映射关系
|
|
|
|
cfg.CreateMap(typeof(TEntityDto), typeof(TEntity)); |
|
|
|
cfg.CreateMap(t1, t2); |
|
|
|
cfg.CreateMap(typeof(TUpdateInput), typeof(TEntity)); |
|
|
|
}); |
|
|
|
_mapper = new Mapper(config); |
|
|
|
var tt = _mapper.Map(input, entity); |
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
//await Repository.UpdateAsync(entity, true);
|
|
|
|
//await Repository.UpdateAsync(tt, true);
|
|
|
|
var config = new MapperConfiguration(cfg => |
|
|
|
{ |
|
|
|
// 动态创建映射关系
|
|
|
|
cfg.CreateMap(typeof(TEntityDto), typeof(TEntity)); |
|
|
|
cfg.CreateMap(typeof(TUpdateInput), typeof(TEntity)); |
|
|
|
cfg.CreateMap(inputDetailDtoType, entityDetailType); |
|
|
|
}); |
|
|
|
_mapper = new Mapper(config); |
|
|
|
} |
|
|
|
|
|
|
|
//await Cache.SetAsync(entity.Id.ToString(), entity, GetCacheTime());
|
|
|
|
await ReMoveCaCheAsync(id).ConfigureAwait(false); |
|
|
|
await Repository.UpdateAsync(entity, true); |
|
|
|
return await MapToGetOutputDtoAsync(entity); |
|
|
|
} |
|
|
@ -579,6 +551,16 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto, |
|
|
|
|
|
|
|
#region 私有处理
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 清楚缓存
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="id"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
private async Task ReMoveCaCheAsync(TKey id) |
|
|
|
{ |
|
|
|
await Cache.RemoveAsync(id?.ToString()); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 按表达式条件获取分页列表
|
|
|
|
/// </summary>
|
|
|
@ -713,15 +695,15 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto, |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 获取缓存redis时间
|
|
|
|
/// 获取缓存redis时间
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
private static DistributedCacheEntryOptions GetCacheTime() |
|
|
|
{ |
|
|
|
Random random = new Random(); |
|
|
|
var random = new Random(); |
|
|
|
//解决雪崩 添加随机缓存时间
|
|
|
|
var time = CacheMinute + random.Next(10, 30); |
|
|
|
return new DistributedCacheEntryOptions() |
|
|
|
return new DistributedCacheEntryOptions |
|
|
|
{ |
|
|
|
AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(time) |
|
|
|
}; |
|
|
|