Files
tnb.server/EquipMgr/Tnb.EquipMgr/BaseMoldMaintainService.cs

89 lines
3.3 KiB
C#

using System.Linq.Expressions;
using JNPF.Common.Contracts;
using JNPF.Common.Enums;
using JNPF.Common.Extension;
using JNPF.FriendlyException;
using Mapster;
using SqlSugar;
using Tnb.EquipMgr.Entities.Dto;
namespace Tnb.EquipMgr
{
public class BaseMoldMaintainService
{
private readonly ISqlSugarClient _db;
public BaseMoldMaintainService(ISqlSugarClient db)
{
_db = db;
}
/// <summary>
/// 关联
/// </summary>
/// <typeparam name="TSrc">输入参数类型</typeparam>
/// <typeparam name="TDest">目标数据库表类型</typeparam>
/// <param name="input">输入参数</param>
/// <param name="mColumnName">主表属性名称</param>
/// <param name="name">次表属性名称</param>
/// <param name="deleleExp">删除条件</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
protected async Task Relevance<TSrc, TDest>(TSrc input, string mColumnName, string name, Expression<Func<TDest, bool>> deleleExp) where TDest : BaseEntity<string>, new()
where TSrc : BaseMoldMaintainInput
{
if (input == null)
{
throw new ArgumentNullException(nameof(input));
}
_ = await _db.Deleteable<TDest>().Where(deleleExp).ExecuteCommandAsync();
List<TDest> entities = new();
if (input.ids?.Count > 0)
{
foreach (string id in input.ids)
{
string pk = id;
TDest entity = new();
entity.PropertySetValue(mColumnName, input.id);
entity.PropertySetValue(name, pk);
entities.Add(entity);
}
}
int row = await _db.Insertable(entities).ExecuteCommandAsync();
if (row < 1)
{
throw Oops.Oh(ErrorCode.COM1000);
}
}
protected async Task Delete<T>(Expression<Func<T, bool>> delFilter) where T : BaseEntity<string>, new()
{
int row = await _db.Deleteable<T>().Where(delFilter).ExecuteCommandAsync();
if (row < 1)
{
throw Oops.Oh(ErrorCode.COM1002);
}
}
protected async Task<dynamic> GetListById<TRelaction, TDest, TOutput>(string masterId,
Expression<Func<TRelaction, bool>> masterFilterExp,
Expression<Func<TRelaction, string>> masterSelector,
Expression<Func<TDest, object>> srcMap,
Expression<Func<TOutput, object>> destMap
) where TDest : BaseEntity<string>, new()
{
TypeAdapterConfig config = new();
_ = config.ForType<TDest, TOutput>().Map(destMap, srcMap);
List<TOutput> list = new();
List<string> itemIds = await _db.Queryable<TRelaction>().Where(masterFilterExp).Select(masterSelector).ToListAsync();
if (itemIds?.Count > 0)
{
List<TDest> items = await _db.Queryable<TDest>().Where(it => itemIds.Contains(it.id)).ToListAsync();
list = items.Adapt<List<TOutput>>();
}
return list;
}
}
}