Browse Source

修改 基类修改

集成Redis
郑勃旭 1 year ago
parent
commit
7f15f496a5
  1. 146
      WinIn.FasterZ.Wms.Be/WinIn.FasterZ.Wms/WinIn.FasterZ.Wms/src/WinIn.FasterZ.Wms.Application/AppBase/ZbxBase.cs
  2. 32
      WinIn.FasterZ.Wms.Be/WinIn.FasterZ.Wms/WinIn.FasterZ.Wms/src/WinIn.FasterZ.Wms.Application/Z_Business/StoreSaleOrder/StoreSaleOrderAppService.cs
  3. 10
      WinIn.FasterZ.Wms.Be/WinIn.FasterZ.Wms/WinIn.FasterZ.Wms/src/WinIn.FasterZ.Wms.EntityFrameworkCore/EntityFrameworkCore/WmsDbContext.cs

146
WinIn.FasterZ.Wms.Be/WinIn.FasterZ.Wms/WinIn.FasterZ.Wms/src/WinIn.FasterZ.Wms.Application/AppBase/ZbxBase.cs

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

32
WinIn.FasterZ.Wms.Be/WinIn.FasterZ.Wms/WinIn.FasterZ.Wms/src/WinIn.FasterZ.Wms.Application/Z_Business/StoreSaleOrder/StoreSaleOrderAppService.cs

@ -1,13 +1,17 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Volo.Abp;
using WinIn.FasterZ.Wms.Permissions; using WinIn.FasterZ.Wms.Permissions;
using WinIn.FasterZ.Wms.Z_Business.StoreSaleOrder.Dtos; using WinIn.FasterZ.Wms.Z_Business.StoreSaleOrder.Dtos;
using Volo.Abp.Application.Services; using Volo.Abp.Application.Services;
using WinIn.FasterZ.Wms.AppBase; using WinIn.FasterZ.Wms.AppBase;
using WinIn.FasterZ.Wms.Z_Business.StoreWorkOrder; using WinIn.FasterZ.Wms.Z_Business.StoreWorkOrder;
using WinIn.FasterZ.Wms.Z_Business.StoreWorkOrder.Dtos; using WinIn.FasterZ.Wms.Z_Business.StoreWorkOrder.Dtos;
using Omu.ValueInjecter;
using WinIn.FasterZ.Wms.Z_Business.StoreWorkOrderDetail.Dtos;
namespace WinIn.FasterZ.Wms.Z_Business.StoreSaleOrder; namespace WinIn.FasterZ.Wms.Z_Business.StoreSaleOrder;
@ -25,18 +29,36 @@ public class StoreSaleOrderAppService : ZbxBase<StoreSaleOrder, StoreSaleOrderDt
private readonly IStoreWorkOrderAppService _workOrderAppService; private readonly IStoreWorkOrderAppService _workOrderAppService;
public StoreSaleOrderAppService(IStoreSaleOrderRepository repository, IStoreWorkOrderAppService workOrderAppService) : base(repository) private IMapper _mapperssssss;
public StoreSaleOrderAppService(IStoreSaleOrderRepository repository, IStoreWorkOrderAppService workOrderAppService) : base(repository)
{ {
_repository = repository; _repository = repository;
_workOrderAppService = workOrderAppService; _workOrderAppService = workOrderAppService;
} }
/// <summary>
/// 测试多次修改 不 该数据已经被修改错误
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
[HttpPost("xcvxcv")] [HttpPost("xcvxcv")]
public async Task test(CreateUpdateStoreWorkOrderDto dto) public async Task test(CreateUpdateStoreWorkOrderDto dto)
{ {
dto.Qty = 1;
await _workOrderAppService.UpdateAsync(dto.Id, dto); await _workOrderAppService.UpdateAsync(dto.Id, dto);
dto.Qty = 4; var tttt =await _workOrderAppService.GetAsync(dto.Id);
await _workOrderAppService.UpdateAsync(dto.Id, dto); tttt.Qty += 5;
}
var config = new MapperConfiguration(cfg =>
{
// 动态创建映射关系
cfg.CreateMap(typeof(StoreWorkOrderDto), typeof(CreateUpdateStoreWorkOrderDto));
cfg.CreateMap(typeof(StoreWorkOrderDetailDto), typeof(CreateUpdateStoreWorkOrderDetailDto));
});
_mapperssssss = new AutoMapper.Mapper(config);
var ffff = _mapperssssss.Map<StoreWorkOrderDto, CreateUpdateStoreWorkOrderDto>(tttt);
await _workOrderAppService.UpdateAsync(ffff.Id, ffff);
//throw new UserFriendlyException("xzcvxcv");
}
} }

10
WinIn.FasterZ.Wms.Be/WinIn.FasterZ.Wms/WinIn.FasterZ.Wms/src/WinIn.FasterZ.Wms.EntityFrameworkCore/EntityFrameworkCore/WmsDbContext.cs

@ -1036,6 +1036,7 @@ namespace WinIn.FasterZ.Wms.EntityFrameworkCore
{ {
b.ToTable(WmsConsts.DbTablePrefix + "Store_WorkOrder", WmsConsts.DbSchema); b.ToTable(WmsConsts.DbTablePrefix + "Store_WorkOrder", WmsConsts.DbSchema);
b.ConfigureByConvention(); b.ConfigureByConvention();
b.Property(e => e.ConcurrencyStamp).IsConcurrencyToken(false);
b.HasMany(q => q.Details).WithOne().HasForeignKey(d => d.MasterId).IsRequired(); b.HasMany(q => q.Details).WithOne().HasForeignKey(d => d.MasterId).IsRequired();
/* Configure more properties here */ /* Configure more properties here */
@ -3220,8 +3221,7 @@ namespace WinIn.FasterZ.Wms.EntityFrameworkCore
//{ //{
// item?.GetType().GetProperty("ConcurrencyStamp")?.SetValue(item, Guid.NewGuid().ToString()); // item?.GetType().GetProperty("ConcurrencyStamp")?.SetValue(item, Guid.NewGuid().ToString());
//} //}
//return base.SaveChanges(); return base.SaveChanges();
return 1;
} }
public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = new CancellationToken()) public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = new CancellationToken())
@ -3232,8 +3232,7 @@ namespace WinIn.FasterZ.Wms.EntityFrameworkCore
//{ //{
// item?.GetType().GetProperty("ConcurrencyStamp")?.SetValue(item, Guid.NewGuid().ToString()); // item?.GetType().GetProperty("ConcurrencyStamp")?.SetValue(item, Guid.NewGuid().ToString());
//} //}
//return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken); return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
return Task.FromResult(1);
} }
public override Task<int> SaveChangesOnDbContextAsync(bool acceptAllChangesOnSuccess, public override Task<int> SaveChangesOnDbContextAsync(bool acceptAllChangesOnSuccess,
@ -3245,8 +3244,7 @@ namespace WinIn.FasterZ.Wms.EntityFrameworkCore
//{ //{
// item?.GetType().GetProperty("ConcurrencyStamp")?.SetValue(item, Guid.NewGuid().ToString()); // item?.GetType().GetProperty("ConcurrencyStamp")?.SetValue(item, Guid.NewGuid().ToString());
//} //}
//return base.SaveChangesOnDbContextAsync(acceptAllChangesOnSuccess, cancellationToken); return base.SaveChangesOnDbContextAsync(acceptAllChangesOnSuccess, cancellationToken);
return Task.FromResult(1);
} }
} }
} }

Loading…
Cancel
Save