1
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Aspose.Cells.Drawing;
|
||||
@@ -12,6 +14,9 @@ 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;
|
||||
@@ -82,7 +87,7 @@ namespace Tnb.EquipMgr
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task RelevanceMaintianGroupAndItem(MoldMaintainGroupItemInput input) =>
|
||||
await Relevance<MoldMaintainGroupItemInput, ToolMoldMaintainGroupItem>(input);
|
||||
await Relevance<MoldMaintainGroupItemInput, ToolMoldMaintainGroupItem>(input, nameof(ToolMoldMaintainGroupItem.item_id), it => it.item_group_id == input.item_group_id);
|
||||
|
||||
/// <summary>
|
||||
/// 关联项目组与模具
|
||||
@@ -91,20 +96,64 @@ namespace Tnb.EquipMgr
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task RelevanceMaintianGroupAndMold(MoldMaintainGroupItemRelationInput input) =>
|
||||
await Relevance<MoldMaintainGroupItemRelationInput, ToolMoldMaintainGroupRelation>(input);
|
||||
await Relevance<MoldMaintainGroupItemRelationInput, ToolMoldMaintainGroupRelation>(input, nameof(ToolMoldMaintainGroupRelation.mold_id), it => it.item_group_id == input.item_group_id);
|
||||
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
private async Task Relevance<TSrc, TDest>(TSrc input) where TDest : BaseEntity<string>, new()
|
||||
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 entity = input.Adapt<TDest>();
|
||||
entity.id = SnowflakeIdHelper.NextId();
|
||||
var row = await _db.Insertable(entity).ExecuteCommandAsync();
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user