模具保养代码提交

This commit is contained in:
DEVICE8\12494
2023-05-18 08:52:54 +08:00
parent 4fa3124aea
commit 926d37924a
10 changed files with 100 additions and 97 deletions

View File

@@ -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<ToolMoldMaintainGroup> _repository;
private readonly IUserManager _userManager;
private readonly ISqlSugarClient _db;
public ToolMoldMaintainGroupService(ISqlSugarRepository<ToolMoldMaintainGroup> repository, IUserManager userManager)
public ToolMoldMaintainGroupService(ISqlSugarRepository<ToolMoldMaintainGroup> repository) : base(repository.AsSugarClient())
{
_repository = repository;
_userManager = userManager;
_db = repository.AsSugarClient();
}
@@ -87,7 +81,7 @@ namespace Tnb.EquipMgr
/// <returns></returns>
[HttpPost]
public async Task RelevanceMaintianGroupAndItem(MoldMaintainGroupItemInput input) =>
await Relevance<MoldMaintainGroupItemInput, ToolMoldMaintainGroupItem>(input, nameof(ToolMoldMaintainGroupItem.item_id), it => it.item_group_id == input.item_group_id);
await Relevance<MoldMaintainGroupItemInput, ToolMoldMaintainGroupItem>(input, nameof(ToolMoldMaintainGroupItem.item_group_id), nameof(ToolMoldMaintainGroupItem.item_id), it => it.item_group_id == input.item_group_id);
/// <summary>
/// 关联项目组与模具
@@ -96,7 +90,7 @@ namespace Tnb.EquipMgr
/// <returns></returns>
[HttpPost]
public async Task RelevanceMaintianGroupAndMold(MoldMaintainGroupItemRelationInput input) =>
await Relevance<MoldMaintainGroupItemRelationInput, ToolMoldMaintainGroupRelation>(input, nameof(ToolMoldMaintainGroupRelation.mold_id), it => it.item_group_id == input.item_group_id);
await Relevance<MoldMaintainGroupItemRelationInput, ToolMoldMaintainGroupRelation>(input, nameof(ToolMoldMaintainGroupRelation.item_group_id), nameof(ToolMoldMaintainGroupRelation.mold_id), it => it.item_group_id == input.item_group_id);
/// <summary>
/// 删除项目组与模具检查项信息
/// </summary>
@@ -110,7 +104,7 @@ namespace Tnb.EquipMgr
{
await Delete<ToolMoldMaintainGroupItem>(it => it.item_group_id == input.item_group_id && input.ids.Contains(it.item_id));
}
else
else
{
await Delete<ToolMoldMaintainGroupRelation>(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<T>().Where(deleteExp).ExecuteCommandAsync();
}
private async Task Relevance<TSrc, TDest>(TSrc input, string name, Expression<Func<TDest, bool>> deleleExp) where TDest : BaseEntity<string>, new()
where TSrc : BaseInput
{
await _db.Deleteable<TDest>().Where(deleleExp).ExecuteCommandAsync();
var itemGroupId = nameof(ToolMoldMaintainGroupRelation.item_group_id);
if (input == null) throw new ArgumentNullException(nameof(input));
var entities = new List<TDest>();
if (input.ids?.Count > 0)
{
foreach (var id in input.ids)
{
var pk = id;
TDest entity = new();
entity.id = SnowflakeIdHelper.NextId();
if (!PropertySet<TDest>.ValueFactories.TryGetValue(itemGroupId, out Action<object, object> setGroupIdAction))
{
setGroupIdAction = PropertySet<TDest>.CreateSetPropertyValueAction(itemGroupId);
PropertySet<TDest>.ValueFactories.Add(itemGroupId, setGroupIdAction);
}
setGroupIdAction(entity, input.item_group_id);
if (!PropertySet<TDest>.ValueFactories.TryGetValue(name, out Action<object, object> setAction))
{
setAction = PropertySet<TDest>.CreateSetPropertyValueAction(name);
PropertySet<TDest>.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<T>
{
public static Dictionary<string, Action<object, object>> ValueFactories = new Dictionary<string, Action<object, object>>(StringComparer.OrdinalIgnoreCase);
public static Action<object, object> 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<Action<object, object>>(setPropertyValue, target, propertyValue).Compile();
}
}