using Infrastructure.Model;
using SqlSugar;
using SqlSugar.IOC;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq.Expressions;
using ZR.Model;
using ZR.Repository;
namespace ZR.Service
{
///
/// 基础服务定义
///
///
public class BaseService : IBaseService where T : class, new()
{
public IBaseRepository baseRepository;
public BaseService(IBaseRepository repository)
{
this.baseRepository = repository ?? throw new ArgumentNullException(nameof(repository));
}
#region add
///
/// 插入指定列使用
///
///
///
///
///
public int Insert(T parm, Expression> iClumns = null, bool ignoreNull = true)
{
return baseRepository.Insert(parm, iClumns, ignoreNull);
}
///
/// 插入实体
///
///
///
public int Add(T t)
{
return baseRepository.Add(t);
}
public IInsertable Insertable(T t)
{
return baseRepository.Insertable(t);
}
//public int Insert(SqlSugarClient client, T t)
//{
// return client.Insertable(t).ExecuteCommand();
//}
//public long InsertBigIdentity(T t)
//{
// return base.Context.Insertable(t).ExecuteReturnBigIdentity();
//}
public int Insert(List t)
{
return baseRepository.Insert(t);
}
public long InsertReturnBigIdentity(T t)
{
return baseRepository.InsertReturnBigIdentity(t);
}
//public int InsertIgnoreNullColumn(List t)
//{
// return base.Context.Insertable(t).IgnoreColumns(true).ExecuteCommand();
//}
//public int InsertIgnoreNullColumn(List t, params string[] columns)
//{
// return base.Context.Insertable(t).IgnoreColumns(columns).ExecuteCommand();
//}
//public DbResult InsertTran(T t)
//{
// var result = base.Context.Ado.UseTran(() =>
// {
// base.Context.Insertable(t).ExecuteCommand();
// });
// return result;
//}
//public DbResult InsertTran(List t)
//{
// var result = base.Context.Ado.UseTran(() =>
// {
// base.Context.Insertable(t).ExecuteCommand();
// });
// return result;
//}
//public T InsertReturnEntity(T t)
//{
// return base.Context.Insertable(t).ExecuteReturnEntity();
//}
//public T InsertReturnEntity(T t, string sqlWith = SqlWith.UpdLock)
//{
// return base.Context.Insertable(t).With(sqlWith).ExecuteReturnEntity();
//}
//public bool ExecuteCommand(string sql, object parameters)
//{
// return base.Context.Ado.ExecuteCommand(sql, parameters) > 0;
//}
//public bool ExecuteCommand(string sql, params SugarParameter[] parameters)
//{
// return base.Context.Ado.ExecuteCommand(sql, parameters) > 0;
//}
//public bool ExecuteCommand(string sql, List parameters)
//{
// return base.Context.Ado.ExecuteCommand(sql, parameters) > 0;
//}
#endregion add
#region update
public int Update(T entity, bool ignoreNullColumns = false)
{
return baseRepository.Update(entity, ignoreNullColumns);
}
public int Update(T entity, Expression> expression, bool ignoreAllNull = false)
{
return baseRepository.Update(entity, expression, ignoreAllNull);
}
public int Update(T entity, Expression> expression, Expression> where)
{
return baseRepository.Update(entity, expression, where);
}
public int Update(SqlSugarClient client, T entity, Expression> expression, Expression> where)
{
return client.Updateable(entity).UpdateColumns(expression).Where(where).ExecuteCommand();
}
/////
/////
/////
/////
/////
///// 默认为true
/////
//public bool Update(T entity, List list = null, bool isNull = true)
//{
// if (list == null)
// {
// list = new List()
// {
// "Create_By",
// "Create_time"
// };
// }
// //base.Context.Updateable(entity).IgnoreColumns(c => list.Contains(c)).Where(isNull).ExecuteCommand()
// return baseRepository.Update(entity, list, isNull);
//}
//public bool Update(List entity)
//{
// var result = base.Context.Ado.UseTran(() =>
// {
// base.Context.Updateable(entity).ExecuteCommand();
// });
// return result.IsSuccess;
//}
public int Update(Expression> where, Expression> columns)
{
return baseRepository.Update(where, columns);
}
#endregion update
public DbResult UseTran(Action action)
{
var result = baseRepository.UseTran(action);
return result;
}
public DbResult UseTran(SqlSugarClient client, Action action)
{
var result = client.Ado.UseTran(() => action());
return result;
}
public bool UseTran2(Action action)
{
var result = baseRepository.UseTran2(action);
return result;
}
#region delete
///
/// 删除表达式
///
///
///
public int Delete(Expression> expression)
{
return baseRepository.Delete(expression);
}
///
/// 批量删除
///
///
///
public int Delete(object[] obj)
{
return baseRepository.Delete(obj);
}
public int Delete(object id)
{
return baseRepository.Delete(id);
}
public int DeleteTable()
{
return baseRepository.DeleteTable();
}
#endregion delete
#region query
public bool Any(Expression> expression)
{
return baseRepository.Any(expression);
}
public ISugarQueryable Queryable()
{
return baseRepository.Queryable();
}
public List GetList(Expression> expression)
{
return baseRepository.GetList(expression);
}
//public Task> QueryableToListAsync(Expression> expression)
//{
// return Context.Queryable().Where(expression).ToListAsync();
//}
//public string QueryableToJson(string select, Expression> expressionWhere)
//{
// var query = base.Context.Queryable().Select(select).Where(expressionWhere).ToList();
// return query.JilToJson();
//}
//public List QueryableToList(string tableName)
//{
// return Context.Queryable(tableName).ToList();
//}
//public (List, int) QueryableToPage(Expression> expression, int pageIndex = 0, int pageSize = 10)
//{
// int totalNumber = 0;
// var list = Context.Queryable().Where(expression).ToPageList(pageIndex, pageSize, ref totalNumber);
// return (list, totalNumber);
//}
//public (List, int) QueryableToPage(Expression> expression, string order, int pageIndex = 0, int pageSize = 10)
//{
// int totalNumber = 0;
// var list = Context.Queryable().Where(expression).OrderBy(order).ToPageList(pageIndex, pageSize, ref totalNumber);
// return (list, totalNumber);
//}
//public (List, int) QueryableToPage(Expression> expression, Expression> orderFiled, string orderBy, int pageIndex = 0, int pageSize = 10)
//{
// int totalNumber = 0;
// if (orderBy.Equals("DESC", StringComparison.OrdinalIgnoreCase))
// {
// var list = Context.Queryable().Where(expression).OrderBy(orderFiled, OrderByType.Desc).ToPageList(pageIndex, pageSize, ref totalNumber);
// return (list, totalNumber);
// }
// else
// {
// var list = Context.Queryable().Where(expression).OrderBy(orderFiled, OrderByType.Asc).ToPageList(pageIndex, pageSize, ref totalNumber);
// return (list, totalNumber);
// }
//}
//public List SqlQueryToList(string sql, object obj = null)
//{
// return Context.Ado.SqlQuery(sql, obj);
//}
///
/// 获得一条数据
///
/// Expression>
///
public T GetFirst(Expression> where)
{
return baseRepository.GetFirst(where);
}
///
/// 根据主值查询单条数据
///
/// 主键值
/// 泛型实体
public T GetId(object pkValue)
{
return baseRepository.GetId(pkValue);
}
///
/// 根据条件查询分页数据
///
///
///
///
public PagedInfo GetPages(Expression> where, PagerInfo parm)
{
var source = baseRepository.GetPages(where, parm);
return source;
}
public PagedInfo GetPages(Expression> where, PagerInfo parm, Expression> order, OrderByType orderEnum = OrderByType.Asc)
{
return baseRepository.GetPages(where, parm, order, orderEnum);
}
public PagedInfo GetPages(Expression> where, PagerInfo parm, Expression> order, string orderByType)
{
return baseRepository.GetPages(where, parm, order, orderByType == "desc" ? OrderByType.Desc : OrderByType.Asc);
}
///
/// 查询所有数据(无分页,请慎用)
///
///
public List GetAll(bool useCache = false, int cacheSecond = 3600)
{
return baseRepository.GetAll(useCache, cacheSecond);
}
public int Count(Expression> where)
{
return baseRepository.Count(where);
}
#endregion query
///
/// 此方法不带output返回值
/// var list = new List();
/// list.Add(new SugarParameter(ParaName, ParaValue)); input
///
///
///
///
public DataTable UseStoredProcedureToDataTable(string procedureName, List parameters)
{
return baseRepository.UseStoredProcedureToDataTable(procedureName, parameters);
}
///
/// 带output返回值
/// var list = new List();
/// list.Add(new SugarParameter(ParaName, ParaValue, true)); output
/// list.Add(new SugarParameter(ParaName, ParaValue)); input
///
///
///
///
public (DataTable, List) UseStoredProcedureToTuple(string procedureName, List parameters)
{
return baseRepository.UseStoredProcedureToTuple(procedureName, parameters);
}
//public string QueryableToJson(string select, Expression> expressionWhere)
//{
// throw new NotImplementedException();
//}
}
}