Browse Source

修改 基类修改

集成Redis
郑勃旭 1 year ago
parent
commit
7f15f496a5
  1. 108
      WinIn.FasterZ.Wms.Be/WinIn.FasterZ.Wms/WinIn.FasterZ.Wms/src/WinIn.FasterZ.Wms.Application/AppBase/ZbxBase.cs
  2. 28
      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

108
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.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;
@ -49,6 +47,8 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
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
@ -211,18 +210,26 @@ 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
@ -231,12 +238,16 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
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)
{
//实体的
foreach (var propertyInfo in entityProperties)
{
//判断是否是List集合
if (propertyInfo.Name == "Details"
@ -247,65 +258,26 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
// 获取 List 的元素类型
if (listProperty != null)
{
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);
// }
// }
//}
entityDetailType = listProperty.PropertyType.GetGenericArguments()[0];
entityDetailTypeFlag = true;
}
}
}
}
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(inputDetailDtoType, entityDetailType);
});
_mapper = new Mapper(config);
var tt = _mapper.Map(input, entity);
#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);
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>
@ -718,10 +700,10 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
/// <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)
};

28
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.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp;
using WinIn.FasterZ.Wms.Permissions;
using WinIn.FasterZ.Wms.Z_Business.StoreSaleOrder.Dtos;
using Volo.Abp.Application.Services;
using WinIn.FasterZ.Wms.AppBase;
using WinIn.FasterZ.Wms.Z_Business.StoreWorkOrder;
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;
@ -25,18 +29,36 @@ public class StoreSaleOrderAppService : ZbxBase<StoreSaleOrder, StoreSaleOrderDt
private readonly IStoreWorkOrderAppService _workOrderAppService;
private IMapper _mapperssssss;
public StoreSaleOrderAppService(IStoreSaleOrderRepository repository, IStoreWorkOrderAppService workOrderAppService) : base(repository)
{
_repository = repository;
_workOrderAppService = workOrderAppService;
}
/// <summary>
/// 测试多次修改 不 该数据已经被修改错误
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
[HttpPost("xcvxcv")]
public async Task test(CreateUpdateStoreWorkOrderDto dto)
{
dto.Qty = 1;
await _workOrderAppService.UpdateAsync(dto.Id, dto);
dto.Qty = 4;
await _workOrderAppService.UpdateAsync(dto.Id, dto);
var tttt =await _workOrderAppService.GetAsync(dto.Id);
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.ConfigureByConvention();
b.Property(e => e.ConcurrencyStamp).IsConcurrencyToken(false);
b.HasMany(q => q.Details).WithOne().HasForeignKey(d => d.MasterId).IsRequired();
/* Configure more properties here */
@ -3220,8 +3221,7 @@ namespace WinIn.FasterZ.Wms.EntityFrameworkCore
//{
// item?.GetType().GetProperty("ConcurrencyStamp")?.SetValue(item, Guid.NewGuid().ToString());
//}
//return base.SaveChanges();
return 1;
return base.SaveChanges();
}
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());
//}
//return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
return Task.FromResult(1);
return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
}
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());
//}
//return base.SaveChangesOnDbContextAsync(acceptAllChangesOnSuccess, cancellationToken);
return Task.FromResult(1);
return base.SaveChangesOnDbContextAsync(acceptAllChangesOnSuccess, cancellationToken);
}
}
}

Loading…
Cancel
Save