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;
}
///
/// 关联
///
/// 输入参数类型
/// 目标数据库表类型
/// 输入参数
/// 主表属性名称
/// 次表属性名称
/// 删除条件
///
///
protected async Task Relevance(TSrc input, string mColumnName, string name, Expression> deleleExp) where TDest : BaseEntity, new()
where TSrc : BaseMoldMaintainInput
{
if (input == null)
{
throw new ArgumentNullException(nameof(input));
}
_ = await _db.Deleteable().Where(deleleExp).ExecuteCommandAsync();
List 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(Expression> delFilter) where T : BaseEntity, new()
{
int row = await _db.Deleteable().Where(delFilter).ExecuteCommandAsync();
if (row < 1)
{
throw Oops.Oh(ErrorCode.COM1002);
}
}
protected async Task GetListById(string masterId,
Expression> masterFilterExp,
Expression> masterSelector,
Expression> srcMap,
Expression> destMap
) where TDest : BaseEntity, new()
{
TypeAdapterConfig config = new();
_ = config.ForType().Map(destMap, srcMap);
List list = new();
List itemIds = await _db.Queryable().Where(masterFilterExp).Select(masterSelector).ToListAsync();
if (itemIds?.Count > 0)
{
List items = await _db.Queryable().Where(it => itemIds.Contains(it.id)).ToListAsync();
list = items.Adapt>();
}
return list;
}
}
}