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.EquipMgr.Entities.Dto;
using Tnb.EquipMgr.Entities;
using Mapster;
using JNPF.Common.Contracts;
using JNPF.Common.Extension;
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();
var entities = new List();
if (input.ids?.Count > 0)
{
foreach (var id in input.ids)
{
var pk = id;
TDest entity = new();
entity.PropertySetValue(mColumnName, input.id);
entity.PropertySetValue(name, pk);
entities.Add(entity);
}
}
var row = await _db.Insertable(entities).ExecuteCommandAsync();
if (row < 1) throw Oops.Oh(ErrorCode.COM1000);
}
protected async Task Delete(Expression> delFilter) where T : BaseEntity, new()
{
var 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()
{
var config = new TypeAdapterConfig();
config.ForType().Map(destMap, srcMap);
var list = new List();
var itemIds = await _db.Queryable().Where(masterFilterExp).Select(masterSelector).ToListAsync();
if (itemIds?.Count > 0)
{
var items = await _db.Queryable().Where(it=>itemIds.Contains(it.id)).ToListAsync();
list = items.Adapt>();
}
return list;
}
}
}