using LFlow.Base.Interfaces;
using LFlow.Base.Utils;
using SqlSugar;
namespace LFlow.Base.Default;
///
/// 默认的增删改查仓储
///
///
///
public abstract class DefaultCurdRepo : IRepo where T : class, IDataModel, new()
{
private readonly ISqlSugarClient _client;
public DefaultCurdRepo(ISqlSugarClient client)
{
_client = client;
}
///
/// 根据ID删除对象
///
///
///
///
public virtual int DeleteById(K id)
{
if (Get(id) != null)
{
return _client.Deleteable().In(id).ExecuteCommand();
}
else
{
throw new Exception("删除的对象不存在");
}
}
///
/// 根据ID获取对象
///
///
///
public virtual T Get(K id)
{
return _client.Queryable().InSingle(id);
}
///
/// 批量查询(分页)
///
///
///
///
///
public List GetAll(int pageIndex, int pageSize, ref int pageTotal)
{
return _client.Queryable().ToPageList(pageIndex, pageSize, ref pageTotal);
}
///
/// 报错或是更新对象
///
///
///
///
public virtual T SaveOrUpdate(T entity, bool isUpdate)
{
if (isUpdate)
{
_client.Updateable(entity).ExecuteCommand();
}
else
{
_client.Insertable(entity).ExecuteCommand();
}
return entity;
}
///
/// 搜索
///
///
///
public virtual List Search(T whereObj)
{
return _client.Queryable().Where(whereObj.ToWhereExp()).ToList();
}
///
/// 查找ID
///
///
///
public virtual List WhereSearchId(T whereObj)
{
return _client.Queryable().Where(whereObj.ToWhereExp()).Select(x => (K)Convert.ChangeType(x.ID, typeof(K))).ToList();
}
}