using Infrastructure.Attribute; using Infrastructure.Model; using SqlSugar; using System; using System.Collections.Generic; using System.Linq.Expressions; using ZR.Model; using ZR.Repository; using ZR.Repository.DbProvider; namespace ZR.Service { /// /// 基础服务定义 /// /// public class BaseService : BaseRepository where T : class,new()//, IBaseService where T : class, new() { //private readonly IBaseRepository BaseRepository; //public BaseService(IBaseRepository baseRepository) //{ // BaseRepository = baseRepository; //} #region 事务 ///// ///// 启用事务 ///// //public void BeginTran() //{ // Context.Ado.BeginTran(); //} ///// ///// 提交事务 ///// //public void CommitTran() //{ // Context.Ado.CommitTran(); //} ///// ///// 回滚事务 ///// //public void RollbackTran() //{ // Context.Ado.RollbackTran(); //} #endregion #region 添加操作 /// /// 添加一条数据 /// /// T /// public int Add(T parm) { return base.Add(parm);// Context.Insertable(parm).RemoveDataCache().ExecuteCommand(); } /// /// 添加 /// /// /// 插入列 /// 忽略null列 /// //public int Add(T parm, Expression> iClumns = null, bool ignoreNull = true) //{ // return Add(parm); //} /// /// 批量添加数据 /// /// List /// public int Add(List parm) { return Context.Insertable(parm).RemoveDataCache().ExecuteCommand(); } /// /// 添加或更新数据,不推荐使用了 /// /// List /// public T Saveable(T parm, Expression> uClumns = null, Expression> iColumns = null) { var command = Context.Saveable(parm); if (uClumns != null) { command = command.UpdateIgnoreColumns(uClumns); } if (iColumns != null) { command = command.InsertIgnoreColumns(iColumns); } return command.ExecuteReturnEntity(); } /// /// 批量添加或更新数据 /// /// List /// public List Saveable(List parm, Expression> uClumns = null, Expression> iColumns = null) { var command = Context.Saveable(parm); if (uClumns != null) { command = command.UpdateIgnoreColumns(uClumns); } if (iColumns != null) { command = command.InsertIgnoreColumns(iColumns); } return command.ExecuteReturnList(); } #endregion #region 查询操作 /// /// 根据条件查询数据是否存在 /// /// 条件表达式树 /// //public bool Any(Expression> where) //{ // return base.Context.Any(where); //} /// /// 根据条件合计字段 /// /// 条件表达式树 /// public TResult Sum(Expression> where, Expression> field) { return base.Context.Queryable().Where(where).Sum(field); } /// /// 根据主值查询单条数据 /// /// 主键值 /// 泛型实体 //public T GetId(object pkValue) //{ // return base.Context.Queryable().InSingle(pkValue); //} /// /// 根据主键查询多条数据 /// /// /// public List GetIn(object[] ids) { return Context.Queryable().In(ids).ToList(); } /// /// 根据条件取条数 /// /// 条件表达式树 /// public int GetCount(Expression> where) { return Context.Queryable().Count(where); } /// /// 查询所有数据(无分页,请慎用) /// /// public List GetAll(bool useCache = false, int cacheSecond = 3600) { return Context.Queryable().WithCacheIF(useCache, cacheSecond).ToList(); } /// /// 获得一条数据 /// /// Expression> /// public T GetFirst2(Expression> where) { return base.GetFirst(where);// Context.Queryable().Where(where).First(); } /// /// 获得一条数据 /// /// string /// //public T GetFirst(string parm) //{ // return Context.Queryable().Where(parm).First(); //} /// /// 根据条件查询分页数据 /// /// /// /// public PagedInfo GetPages(Expression> where, PagerInfo parm) { var source = Context.Queryable().Where(where); return source.ToPage(parm); } public PagedInfo GetPages(Expression> where, PagerInfo parm, Expression> order, string orderEnum = "Asc") { var source = Context.Queryable().Where(where).OrderByIF(orderEnum == "Asc", order, OrderByType.Asc).OrderByIF(orderEnum == "Desc", order, OrderByType.Desc); return source.ToPage(parm); } /// /// 根据条件查询数据 /// /// 条件表达式树 /// public List GetWhere(Expression> where, bool useCache = false, int cacheSecond = 3600) { var query = Context.Queryable().Where(where).WithCacheIF(useCache, cacheSecond); return query.ToList(); } /// /// 根据条件查询数据 /// /// 条件表达式树 /// public List GetWhere(Expression> where, Expression> order, string orderEnum = "Asc", bool useCache = false, int cacheSecond = 3600) { var query = Context.Queryable().Where(where).OrderByIF(orderEnum == "Asc", order, OrderByType.Asc).OrderByIF(orderEnum == "Desc", order, OrderByType.Desc).WithCacheIF(useCache, cacheSecond); return query.ToList(); } #endregion #region 修改操作 ///// ///// 修改一条数据 ///// ///// T ///// //public int Update(T parm) //{ // return Context.Updateable(parm).RemoveDataCache().ExecuteCommand(); //} ///// ///// 批量修改 ///// ///// T ///// //public int Update(List parm) //{ // return Context.Updateable(parm).RemoveDataCache().ExecuteCommand(); //} ///// ///// 按查询条件更新 ///// ///// ///// ///// //public int Update(Expression> where, Expression> columns) //{ // return Context.Updateable().SetColumns(columns).Where(where).RemoveDataCache().ExecuteCommand(); //} #endregion #region 删除操作 ///// ///// 删除一条或多条数据 ///// ///// string ///// //public int Delete(object id) //{ // return Context.Deleteable(id).RemoveDataCache().ExecuteCommand(); //} ///// ///// 删除一条或多条数据 ///// ///// string ///// //public int Delete(object[] ids) //{ // return Context.Deleteable().In(ids).RemoveDataCache().ExecuteCommand(); //} ///// ///// 根据条件删除一条或多条数据 ///// ///// 过滤条件 ///// //public int Delete(Expression> where) //{ // return Context.Deleteable().Where(where).RemoveDataCache().ExecuteCommand(); //} public int DeleteTable() { return Context.Deleteable().RemoveDataCache().ExecuteCommand(); } #endregion } }