From 87ee098932a450f837628117dde0d2b373cb6894 Mon Sep 17 00:00:00 2001 From: "boxu.zheng" Date: Thu, 23 Nov 2023 16:45:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=9F=BA=E7=B1=BB=E4=B8=AD?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AppBase/ZbxBase.cs | 151 +++++++++++++----- ...StoreWorkOrderEfCoreQueryableExtensions.cs | 2 +- .../appsettings.json | 2 +- 3 files changed, 113 insertions(+), 42 deletions(-) diff --git a/WinIn.FasterZ.Wms.Be/WinIn.FasterZ.Wms/WinIn.FasterZ.Wms/src/WinIn.FasterZ.Wms.Application/AppBase/ZbxBase.cs b/WinIn.FasterZ.Wms.Be/WinIn.FasterZ.Wms/WinIn.FasterZ.Wms/src/WinIn.FasterZ.Wms.Application/AppBase/ZbxBase.cs index fd06ed423..e2c81efd1 100644 --- a/WinIn.FasterZ.Wms.Be/WinIn.FasterZ.Wms/WinIn.FasterZ.Wms/src/WinIn.FasterZ.Wms.Application/AppBase/ZbxBase.cs +++ b/WinIn.FasterZ.Wms.Be/WinIn.FasterZ.Wms/WinIn.FasterZ.Wms/src/WinIn.FasterZ.Wms.Application/AppBase/ZbxBase.cs @@ -1,39 +1,36 @@ -using Volo.Abp.Domain.Entities.Auditing; -using Volo.Abp.Identity; - -namespace WinIn.FasterZ.Wms.AppBase; - -using System; +using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Linq.Dynamic.Core; using System.Linq.Expressions; -using System.Reflection; using System.Threading; using System.Threading.Tasks; - +using AutoMapper; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Localization; - using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; - +using Omu.ValueInjecter; using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Services; +using Volo.Abp.Caching; using Volo.Abp.Domain.Entities; +using Volo.Abp.Domain.Entities.Auditing; using Volo.Abp.Domain.Repositories; - -using WinIn.FasterZ.Store.AppBase; using WinIn.FasterZ.Store.AppBase.Filters; using WinIn.FasterZ.Store.AppBase.TableColumnTypeDto; 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; public class ZbxBase : CrudAppService where TEntityDto : IEntityDto { + #region 定义 + + /// + /// redis缓存时间 分钟 + /// + private const int CacheMinute = 30; + private readonly IRepository _repository; - protected IStringLocalizer Localizer => + private IStringLocalizer Localizer => LazyServiceProvider.LazyGetRequiredService>(); - protected ExportCustomUserSettingAppService ExportCustomUserSettingAppService => + private ExportCustomUserSettingAppService ExportCustomUserSettingAppService => LazyServiceProvider.LazyGetRequiredService(); + private IDistributedCache Cache => + LazyServiceProvider.LazyGetRequiredService>(); + + private IMapper _mapper; + + #endregion + /// - /// 构造方法 + /// 构造方法 /// /// - public ZbxBase(IRepository repository) : base(repository) + protected ZbxBase(IRepository repository) : base(repository) { _repository = repository; } @@ -172,6 +183,7 @@ public class ZbxBase /// [HttpPut("api/[controller]/base/update-by-id")] - public override async Task UpdateAsync(TKey id, TUpdateInput input) + public new async Task UpdateAsync(TKey id, TEntityDto input) { //return base.UpdateAsync(id, input); await CheckUpdatePolicyAsync().ConfigureAwait(false); var entity = await GetEntityByIdAsync(id).ConfigureAwait(false); - entity.FromObject(input!); + + + + Type t1 = null; + Type t2 = null; #region 给所有字表的 Id和MasterId赋值 否则默认的会是000000-000-....的id 插入时会报错 var propertyInfos = entity.GetType().GetProperties(); + var dtolist = input.GetType().GetProperties(); + foreach (var propertyInfo in dtolist) + { + if (propertyInfo.Name == "Details" + && propertyInfo.PropertyType.IsGenericType + && propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(List<>)) + { + var listProperty = typeof(TEntityDto).GetProperty("Details"); + if (listProperty != null) + { + t1 = listProperty.PropertyType.GetGenericArguments()[0]; + } + } + } + foreach (var propertyInfo in propertyInfos) { //判断是否是List集合 @@ -212,33 +243,33 @@ public class ZbxBase)) { var listProperty = typeof(TEntity).GetProperty("Details"); - // 获取 List 的元素类型 if (listProperty != null) { var listItemType = listProperty.PropertyType.GetGenericArguments()[0]; + t2= listProperty.PropertyType.GetGenericArguments()[0]; // 获取元素类型的 ID 属性 - var detailIdProperty = listItemType.GetProperty("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 (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) { @@ -258,10 +289,20 @@ public class ZbxBase + { + // 动态创建映射关系 + cfg.CreateMap(typeof(TEntityDto), typeof(TEntity)); + cfg.CreateMap(t1,t2); + }); + _mapper = new Mapper(config); + var tt = _mapper.Map(input, entity); + #endregion await Repository.UpdateAsync(entity, true); + await Cache.SetAsync(entity.Id.ToString(), entity, GetCacheTime()); return await MapToGetOutputDtoAsync(entity); } @@ -456,7 +497,7 @@ public class ZbxBase 255 * 256)//excel列有最大宽度限制 + if (colWidth > 255 * 256) //excel列有最大宽度限制 { colWidth = 6000; } @@ -470,7 +511,7 @@ public class ZbxBase 255 * 256)//excel列有最大宽度限制 + if (colWidth > 255 * 256) //excel列有最大宽度限制 { colWidth = 6000; } @@ -516,6 +557,20 @@ public class ZbxBase + /// 【基础】-【获取 增 改 查基础的Dto数据类型】 + /// + /// + [HttpPost("api/[controller]/base/get-cache-by-id/{id}")] + public virtual async Task GetCacheByIdAsync(TKey id) + { + var entity = await Cache.GetOrAddAsync( + $"{typeof(TEntityDto).Name}:{id}".ToString(), + async () => await GetEntityByIdAsync(id), GetCacheTime); + var dto = ObjectMapper.Map(entity!); + return dto; + } + #endregion #region 私有处理 @@ -565,7 +620,7 @@ public class ZbxBase + /// 获取缓存redis时间 + /// + /// + private static DistributedCacheEntryOptions GetCacheTime() + { + Random random = new Random(); + //解决雪崩 添加随机缓存时间 + var time = CacheMinute + random.Next(10, 30); + return new DistributedCacheEntryOptions() + { + AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(time) + }; + } + + #region Excel导出的样式设置 /// diff --git a/WinIn.FasterZ.Wms.Be/WinIn.FasterZ.Wms/WinIn.FasterZ.Wms/src/WinIn.FasterZ.Wms.EntityFrameworkCore/Z_Business/StoreWorkOrder/StoreWorkOrderEfCoreQueryableExtensions.cs b/WinIn.FasterZ.Wms.Be/WinIn.FasterZ.Wms/WinIn.FasterZ.Wms/src/WinIn.FasterZ.Wms.EntityFrameworkCore/Z_Business/StoreWorkOrder/StoreWorkOrderEfCoreQueryableExtensions.cs index a2f2c13be..da6ff8b0b 100644 --- a/WinIn.FasterZ.Wms.Be/WinIn.FasterZ.Wms/WinIn.FasterZ.Wms/src/WinIn.FasterZ.Wms.EntityFrameworkCore/Z_Business/StoreWorkOrder/StoreWorkOrderEfCoreQueryableExtensions.cs +++ b/WinIn.FasterZ.Wms.Be/WinIn.FasterZ.Wms/WinIn.FasterZ.Wms/src/WinIn.FasterZ.Wms.EntityFrameworkCore/Z_Business/StoreWorkOrder/StoreWorkOrderEfCoreQueryableExtensions.cs @@ -13,7 +13,7 @@ public static class StoreWorkOrderEfCoreQueryableExtensions } return queryable - // .Include(x => x.xxx) // TODO: AbpHelper generated + .Include(x => x.Details) // TODO: AbpHelper generated ; } } diff --git a/WinIn.FasterZ.Wms.Be/WinIn.FasterZ.Wms/WinIn.FasterZ.Wms/src/WinIn.FasterZ.Wms.HttpApi.Host/appsettings.json b/WinIn.FasterZ.Wms.Be/WinIn.FasterZ.Wms/WinIn.FasterZ.Wms/src/WinIn.FasterZ.Wms.HttpApi.Host/appsettings.json index 28b726b35..9f24edab8 100644 --- a/WinIn.FasterZ.Wms.Be/WinIn.FasterZ.Wms/WinIn.FasterZ.Wms/src/WinIn.FasterZ.Wms.HttpApi.Host/appsettings.json +++ b/WinIn.FasterZ.Wms.Be/WinIn.FasterZ.Wms/WinIn.FasterZ.Wms/src/WinIn.FasterZ.Wms.HttpApi.Host/appsettings.json @@ -15,7 +15,7 @@ "KeyPrefix": "DY_AuthSiteCenter" }, "AuthServer": { - "Authority": "http://192.168.0.240:60080", + "Authority": "http://dev.ccwin-in.com:60080", "RequireHttpsMetadata": "false", "SwaggerClientId": "Wms_Swagger" },