Files
tnb.server/EquipMgr/Tnb.EquipMgr/BaseMoldMaintainService.cs
2023-05-18 08:52:54 +08:00

61 lines
2.4 KiB
C#

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, TDest>(TSrc input,string masterTableName, string name, Expression<Func<TDest, bool>> deleleExp) where TDest : BaseEntity<string>, new()
where TSrc : BaseMoldMaintainInput
{
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(masterTableName, out Action<object, object>? setGroupIdAction))
{
setGroupIdAction = PropertySet<TDest>.CreateSetPropertyValueAction(masterTableName);
PropertySet<TDest>.ValueFactories.Add(masterTableName, 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);
}
}
}