From 926d37924ad5ee458cf684420404326600c02ac5 Mon Sep 17 00:00:00 2001 From: "DEVICE8\\12494" Date: Thu, 18 May 2023 08:52:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A8=A1=E5=85=B7=E4=BF=9D=E5=85=BB=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...{BaseInput.cs => BaseMoldMaintainInput.cs} | 2 +- .../Dto/MoldMaintainDelInput.cs | 2 +- .../Dto/MoldMaintainGroupItemRelationInput.cs | 2 +- .../Dto/MoldMaintainItemInput.cs | 2 +- .../Dto/RelevanceMoldInput.cs | 6 +- .../Entity/ToolMoldMaintainRuleRelation.cs | 15 +---- .../Tnb.EquipMgr/BaseMoldMaintainService.cs | 60 +++++++++++++++++ .../ToolMoldMaintainGroupService.cs | 67 ++----------------- .../ToolMoldMaintainRuleService.cs | 18 ++--- EquipMgr/Tnb.EquipMgr/Utils/PropertySet.cs | 23 +++++++ 10 files changed, 100 insertions(+), 97 deletions(-) rename EquipMgr/Tnb.EquipMgr.Entities/Dto/{BaseInput.cs => BaseMoldMaintainInput.cs} (87%) create mode 100644 EquipMgr/Tnb.EquipMgr/BaseMoldMaintainService.cs create mode 100644 EquipMgr/Tnb.EquipMgr/Utils/PropertySet.cs diff --git a/EquipMgr/Tnb.EquipMgr.Entities/Dto/BaseInput.cs b/EquipMgr/Tnb.EquipMgr.Entities/Dto/BaseMoldMaintainInput.cs similarity index 87% rename from EquipMgr/Tnb.EquipMgr.Entities/Dto/BaseInput.cs rename to EquipMgr/Tnb.EquipMgr.Entities/Dto/BaseMoldMaintainInput.cs index b826fe23..e34be422 100644 --- a/EquipMgr/Tnb.EquipMgr.Entities/Dto/BaseInput.cs +++ b/EquipMgr/Tnb.EquipMgr.Entities/Dto/BaseMoldMaintainInput.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Tnb.EquipMgr.Entities.Dto { - public class BaseInput + public class BaseMoldMaintainInput { public string item_group_id { get; set; } public List ids { get; set; } diff --git a/EquipMgr/Tnb.EquipMgr.Entities/Dto/MoldMaintainDelInput.cs b/EquipMgr/Tnb.EquipMgr.Entities/Dto/MoldMaintainDelInput.cs index 0d078291..4bedda98 100644 --- a/EquipMgr/Tnb.EquipMgr.Entities/Dto/MoldMaintainDelInput.cs +++ b/EquipMgr/Tnb.EquipMgr.Entities/Dto/MoldMaintainDelInput.cs @@ -9,7 +9,7 @@ namespace Tnb.EquipMgr.Entities.Dto /// /// 模具保养删除参数 /// - public class MoldMaintainDelInput : BaseInput + public class MoldMaintainDelInput : BaseMoldMaintainInput { /// /// 删除的表名 diff --git a/EquipMgr/Tnb.EquipMgr.Entities/Dto/MoldMaintainGroupItemRelationInput.cs b/EquipMgr/Tnb.EquipMgr.Entities/Dto/MoldMaintainGroupItemRelationInput.cs index 4e796a9e..0618d131 100644 --- a/EquipMgr/Tnb.EquipMgr.Entities/Dto/MoldMaintainGroupItemRelationInput.cs +++ b/EquipMgr/Tnb.EquipMgr.Entities/Dto/MoldMaintainGroupItemRelationInput.cs @@ -9,7 +9,7 @@ namespace Tnb.EquipMgr.Entities.Dto /// /// 关联保养组与模具输入参数 /// - public class MoldMaintainGroupItemRelationInput :BaseInput + public class MoldMaintainGroupItemRelationInput :BaseMoldMaintainInput { } } diff --git a/EquipMgr/Tnb.EquipMgr.Entities/Dto/MoldMaintainItemInput.cs b/EquipMgr/Tnb.EquipMgr.Entities/Dto/MoldMaintainItemInput.cs index 7802dba7..8820e89a 100644 --- a/EquipMgr/Tnb.EquipMgr.Entities/Dto/MoldMaintainItemInput.cs +++ b/EquipMgr/Tnb.EquipMgr.Entities/Dto/MoldMaintainItemInput.cs @@ -9,7 +9,7 @@ namespace Tnb.EquipMgr.Entities.Dto /// /// 模具保养项输入参数 /// - public class MoldMaintainGroupItemInput : BaseInput + public class MoldMaintainGroupItemInput : BaseMoldMaintainInput { } diff --git a/EquipMgr/Tnb.EquipMgr.Entities/Dto/RelevanceMoldInput.cs b/EquipMgr/Tnb.EquipMgr.Entities/Dto/RelevanceMoldInput.cs index c64c5312..9c2ea796 100644 --- a/EquipMgr/Tnb.EquipMgr.Entities/Dto/RelevanceMoldInput.cs +++ b/EquipMgr/Tnb.EquipMgr.Entities/Dto/RelevanceMoldInput.cs @@ -9,15 +9,11 @@ namespace Tnb.EquipMgr.Entities.Dto /// /// 模具规则定义,关键模具输入参数 /// - public class RelevanceMoldInput + public class RelevanceMoldInput : BaseMoldMaintainInput { /// /// 规则Id /// public string? rule_id { get; set; } - /// - /// 模具Id - /// - public string? mold_id { get; set;} } } diff --git a/EquipMgr/Tnb.EquipMgr.Entities/Entity/ToolMoldMaintainRuleRelation.cs b/EquipMgr/Tnb.EquipMgr.Entities/Entity/ToolMoldMaintainRuleRelation.cs index f27e7d3a..d2818d09 100644 --- a/EquipMgr/Tnb.EquipMgr.Entities/Entity/ToolMoldMaintainRuleRelation.cs +++ b/EquipMgr/Tnb.EquipMgr.Entities/Entity/ToolMoldMaintainRuleRelation.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Text; using JNPF.Common.Security; using SqlSugar; +using Tnb.Common.Contracts; namespace Tnb.EquipMgr.Entities { @@ -10,20 +11,8 @@ namespace Tnb.EquipMgr.Entities ///模具保养规则与模具关联 /// [SugarTable("tool_mold_maintain_rule_relation")] - public partial class ToolMoldMaintainRuleRelation + public partial class ToolMoldMaintainRuleRelation:BaseEntity { - public ToolMoldMaintainRuleRelation(){ - - - } - /// - /// Desc:主键 - /// Default: - /// Nullable:False - /// - [SugarColumn(IsPrimaryKey=true)] - public string id {get;set;} = SnowflakeIdHelper.NextId(); - /// /// Desc:保养规则id /// Default: diff --git a/EquipMgr/Tnb.EquipMgr/BaseMoldMaintainService.cs b/EquipMgr/Tnb.EquipMgr/BaseMoldMaintainService.cs new file mode 100644 index 00000000..8fc11254 --- /dev/null +++ b/EquipMgr/Tnb.EquipMgr/BaseMoldMaintainService.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using System.Threading.Tasks; +using JNPF.Common.Enums; +using JNPF.Common.Security; +using JNPF.FriendlyException; +using SqlSugar; +using Tnb.Common.Contracts; +using Tnb.EquipMgr.Entities.Dto; +using Tnb.EquipMgr.Entities; +using Tnb.EquipMgr.Utils; + +namespace Tnb.EquipMgr +{ + public class BaseMoldMaintainService + { + private readonly ISqlSugarClient _db; + public BaseMoldMaintainService(ISqlSugarClient db) + { + _db = db; + } + + protected async Task Relevance(TSrc input,string masterTableName, string name, Expression> deleleExp) where TDest : BaseEntity, new() + where TSrc : BaseMoldMaintainInput + { + + await _db.Deleteable().Where(deleleExp).ExecuteCommandAsync(); + //var itemGroupId = nameof(ToolMoldMaintainGroupRelation.item_group_id); + if (input == null) throw new ArgumentNullException(nameof(input)); + var entities = new List(); + if (input.ids?.Count > 0) + { + foreach (var id in input.ids) + { + var pk = id; + TDest entity = new(); + entity.id = SnowflakeIdHelper.NextId(); + if (!PropertySet.ValueFactories.TryGetValue(masterTableName, out Action? setGroupIdAction)) + { + setGroupIdAction = PropertySet.CreateSetPropertyValueAction(masterTableName); + PropertySet.ValueFactories.Add(masterTableName, setGroupIdAction); + } + setGroupIdAction(entity, input.item_group_id); + if (!PropertySet.ValueFactories.TryGetValue(name, out Action? setAction)) + { + setAction = PropertySet.CreateSetPropertyValueAction(name); + PropertySet.ValueFactories.Add(name, setAction); + } + setAction(entity, pk); + entities.Add(entity); + } + } + var row = await _db.Insertable(entities).ExecuteCommandAsync(); + if (row < 1) throw Oops.Oh(ErrorCode.COM1000); + } + } +} diff --git a/EquipMgr/Tnb.EquipMgr/ToolMoldMaintainGroupService.cs b/EquipMgr/Tnb.EquipMgr/ToolMoldMaintainGroupService.cs index 2391aa23..5a94a02f 100644 --- a/EquipMgr/Tnb.EquipMgr/ToolMoldMaintainGroupService.cs +++ b/EquipMgr/Tnb.EquipMgr/ToolMoldMaintainGroupService.cs @@ -14,9 +14,6 @@ using JNPF.DynamicApiController; using JNPF.FriendlyException; using Mapster; using Microsoft.AspNetCore.Mvc; -using NPOI.SS.Formula.Functions; -using Qiniu.Util; -using Spire.Pdf.Widget; using SqlSugar; using Tnb.Common.Contracts; using Tnb.EquipMgr.Entities; @@ -29,15 +26,12 @@ namespace Tnb.EquipMgr [ApiDescriptionSettings(Tag = ModuleConsts.Tag, Area = ModuleConsts.Area, Order = 700)] [Route("api/[area]/[controller]/[action]")] - public class ToolMoldMaintainGroupService : IToolMoldMaintainGroupService, IDynamicApiController, ITransient + public class ToolMoldMaintainGroupService : BaseMoldMaintainService, IToolMoldMaintainGroupService, IDynamicApiController, ITransient { private readonly ISqlSugarRepository _repository; - private readonly IUserManager _userManager; private readonly ISqlSugarClient _db; - public ToolMoldMaintainGroupService(ISqlSugarRepository repository, IUserManager userManager) + public ToolMoldMaintainGroupService(ISqlSugarRepository repository) : base(repository.AsSugarClient()) { - _repository = repository; - _userManager = userManager; _db = repository.AsSugarClient(); } @@ -87,7 +81,7 @@ namespace Tnb.EquipMgr /// [HttpPost] public async Task RelevanceMaintianGroupAndItem(MoldMaintainGroupItemInput input) => - await Relevance(input, nameof(ToolMoldMaintainGroupItem.item_id), it => it.item_group_id == input.item_group_id); + await Relevance(input, nameof(ToolMoldMaintainGroupItem.item_group_id), nameof(ToolMoldMaintainGroupItem.item_id), it => it.item_group_id == input.item_group_id); /// /// 关联项目组与模具 @@ -96,7 +90,7 @@ namespace Tnb.EquipMgr /// [HttpPost] public async Task RelevanceMaintianGroupAndMold(MoldMaintainGroupItemRelationInput input) => - await Relevance(input, nameof(ToolMoldMaintainGroupRelation.mold_id), it => it.item_group_id == input.item_group_id); + await Relevance(input, nameof(ToolMoldMaintainGroupRelation.item_group_id), nameof(ToolMoldMaintainGroupRelation.mold_id), it => it.item_group_id == input.item_group_id); /// /// 删除项目组与模具检查项信息 /// @@ -110,7 +104,7 @@ namespace Tnb.EquipMgr { await Delete(it => it.item_group_id == input.item_group_id && input.ids.Contains(it.item_id)); } - else + else { await Delete(it => it.item_group_id == input.item_group_id && input.ids.Contains(it.mold_id)); } @@ -125,57 +119,6 @@ namespace Tnb.EquipMgr await _db.Deleteable().Where(deleteExp).ExecuteCommandAsync(); } - private async Task Relevance(TSrc input, string name, Expression> deleleExp) where TDest : BaseEntity, new() - where TSrc : BaseInput - { - - await _db.Deleteable().Where(deleleExp).ExecuteCommandAsync(); - var itemGroupId = nameof(ToolMoldMaintainGroupRelation.item_group_id); - if (input == null) throw new ArgumentNullException(nameof(input)); - var entities = new List(); - if (input.ids?.Count > 0) - { - foreach (var id in input.ids) - { - var pk = id; - TDest entity = new(); - entity.id = SnowflakeIdHelper.NextId(); - if (!PropertySet.ValueFactories.TryGetValue(itemGroupId, out Action setGroupIdAction)) - { - setGroupIdAction = PropertySet.CreateSetPropertyValueAction(itemGroupId); - PropertySet.ValueFactories.Add(itemGroupId, setGroupIdAction); - } - setGroupIdAction(entity, input.item_group_id); - if (!PropertySet.ValueFactories.TryGetValue(name, out Action setAction)) - { - setAction = PropertySet.CreateSetPropertyValueAction(name); - PropertySet.ValueFactories.Add(name, setAction); - } - setAction(entity, pk); - entities.Add(entity); - } - } - var row = await _db.Insertable(entities).ExecuteCommandAsync(); - if (row < 1) throw Oops.Oh(ErrorCode.COM1000); - } - - } - - public class PropertySet - { - public static Dictionary> ValueFactories = new Dictionary>(StringComparer.OrdinalIgnoreCase); - - public static Action CreateSetPropertyValueAction(string propertyName) - { - var property = typeof(T).GetProperty(propertyName); - var target = Expression.Parameter(typeof(object)); - var propertyValue = Expression.Parameter(typeof(object)); - var castTarget = Expression.Convert(target, typeof(T)); - var castPropertyValue = Expression.Convert(propertyValue, property.PropertyType); - var setPropertyValue = Expression.Call(castTarget, property.GetSetMethod(), castPropertyValue); - return Expression.Lambda>(setPropertyValue, target, propertyValue).Compile(); - } - } diff --git a/EquipMgr/Tnb.EquipMgr/ToolMoldMaintainRuleService.cs b/EquipMgr/Tnb.EquipMgr/ToolMoldMaintainRuleService.cs index fbc88461..57133460 100644 --- a/EquipMgr/Tnb.EquipMgr/ToolMoldMaintainRuleService.cs +++ b/EquipMgr/Tnb.EquipMgr/ToolMoldMaintainRuleService.cs @@ -23,15 +23,11 @@ namespace Tnb.EquipMgr /// [ApiDescriptionSettings(Tag = ModuleConsts.Tag, Area = ModuleConsts.Area, Order = 700)] [Route("api/[area]/[controller]/[action]")] - public class ToolMoldMaintainRuleService : IToolMoldMaintainRuleService, IDynamicApiController, ITransient + public class ToolMoldMaintainRuleService : BaseMoldMaintainService, IToolMoldMaintainRuleService, IDynamicApiController, ITransient { - private readonly ISqlSugarRepository _repository; - private readonly IUserManager _userManager; private readonly ISqlSugarClient _db; - public ToolMoldMaintainRuleService(ISqlSugarRepository repository, IUserManager userManager) + public ToolMoldMaintainRuleService(ISqlSugarRepository repository, IUserManager userManager) : base(repository.AsSugarClient()) { - _repository = repository; - _userManager = userManager; _db = repository.AsSugarClient(); } /// @@ -41,12 +37,8 @@ namespace Tnb.EquipMgr /// /// [HttpPost] - public async Task RelevanceMold(RelevanceMoldInput input) - { - if (input is null) throw new ArgumentNullException(nameof(input)); - var entity = input.Adapt(); - var row = await _db.Insertable(entity).ExecuteCommandAsync(); - if (row < 1) throw Oops.Oh(ErrorCode.COM1000); - } + public async Task RelevanceMold(RelevanceMoldInput input) => + await Relevance(input, nameof(ToolMoldMaintainRuleRelation.rule_id), nameof(ToolMoldMaintainRuleRelation.mold_id), it => it.rule_id == input.rule_id); + } } diff --git a/EquipMgr/Tnb.EquipMgr/Utils/PropertySet.cs b/EquipMgr/Tnb.EquipMgr/Utils/PropertySet.cs new file mode 100644 index 00000000..81464052 --- /dev/null +++ b/EquipMgr/Tnb.EquipMgr/Utils/PropertySet.cs @@ -0,0 +1,23 @@ +using System.Linq.Expressions; + +namespace Tnb.EquipMgr.Utils +{ + public class PropertySet + { + public static Dictionary> ValueFactories = new Dictionary>(StringComparer.OrdinalIgnoreCase); + + public static Action CreateSetPropertyValueAction(string propertyName) + { + var property = typeof(T).GetProperty(propertyName); + var target = Expression.Parameter(typeof(object)); + var propertyValue = Expression.Parameter(typeof(object)); + var castTarget = Expression.Convert(target, typeof(T)); + var castPropertyValue = Expression.Convert(propertyValue, property!.PropertyType); + var setPropertyValue = Expression.Call(castTarget, property.GetSetMethod()!, castPropertyValue); + return Expression.Lambda>(setPropertyValue, target, propertyValue).Compile(); + } + + } + + +}