diff --git a/Src/Asp.NetCore2/SqlSugar/Interface/ISimpleClient.cs b/Src/Asp.NetCore2/SqlSugar/Interface/ISimpleClient.cs index fef916dc0..c331dd9ae 100644 --- a/Src/Asp.NetCore2/SqlSugar/Interface/ISimpleClient.cs +++ b/Src/Asp.NetCore2/SqlSugar/Interface/ISimpleClient.cs @@ -12,7 +12,7 @@ namespace SqlSugar RepositoryType CopyNew(IServiceProvider serviceProvider) where RepositoryType : ISugarRepository; RepositoryType CopyNew() where RepositoryType : ISugarRepository; SimpleClient Change() where ChangeType : class, new(); - RepositoryType ChangeRepository() where RepositoryType : ISugarRepository ; + RepositoryType ChangeRepository() where RepositoryType : ISugarRepository; RepositoryType ChangeRepository(IServiceProvider serviceProvider) where RepositoryType : ISugarRepository; IDeleteable AsDeleteable(); IInsertable AsInsertable(List insertObjs); @@ -26,7 +26,9 @@ namespace SqlSugar IUpdateable AsUpdateable(); IUpdateable AsUpdateable(T[] updateObjs); int Count(Expression> whereExpression); + int Count(List conditionalModels); bool Delete(Expression> whereExpression); + bool Delete(List conditionalModels); bool Delete(T deleteObj); bool Delete(List deleteObjs); bool DeleteById(dynamic id); @@ -34,12 +36,20 @@ namespace SqlSugar T GetById(dynamic id); List GetList(); List GetList(Expression> whereExpression); + List GetList(List conditionalList); + List GetList(Expression> whereExpression, List orderByModels); + List GetList(List conditionalList, List orderByModels); List GetPageList(Expression> whereExpression, PageModel page); + List GetPageList(Expression> whereExpression, PageModel page, List orderByModels); List GetPageList(Expression> whereExpression, PageModel page, Expression> orderByExpression = null, OrderByType orderByType = OrderByType.Asc); List GetPageList(List conditionalList, PageModel page); + List GetPageList(List conditionalList, PageModel page, List orderByModels); List GetPageList(List conditionalList, PageModel page, Expression> orderByExpression = null, OrderByType orderByType = OrderByType.Asc); T GetSingle(Expression> whereExpression); + T GetSingle(List conditionalModels); T GetFirst(Expression> whereExpression); + T GetFirst(List conditionalModels); + T GetFirst(List conditionalModels, List orderByModels); bool Insert(T insertObj); bool InsertOrUpdate(T data); bool InsertOrUpdate(List datas); @@ -53,6 +63,7 @@ namespace SqlSugar bool IsAny(Expression> whereExpression); + bool IsAny(List conditionalModels); bool Update(Expression> columns, Expression> whereExpression); bool UpdateSetColumnsTrue(Expression> columns, Expression> whereExpression); bool Update(T updateObj); @@ -105,12 +116,12 @@ namespace SqlSugar Task DeleteByIdAsync(dynamic id, CancellationToken cancellationToken); Task DeleteByIdsAsync(dynamic[] ids, CancellationToken cancellationToken); Task GetByIdAsync(dynamic id, CancellationToken cancellationToken); - Task> GetListAsync( CancellationToken cancellationToken); + Task> GetListAsync(CancellationToken cancellationToken); Task> GetListAsync(Expression> whereExpression, CancellationToken cancellationToken); Task> GetPageListAsync(Expression> whereExpression, PageModel page, CancellationToken cancellationToken); - Task> GetPageListAsync(Expression> whereExpression, PageModel page, Expression> orderByExpression = null, OrderByType orderByType = OrderByType.Asc, CancellationToken cancellationToken=default); + Task> GetPageListAsync(Expression> whereExpression, PageModel page, Expression> orderByExpression = null, OrderByType orderByType = OrderByType.Asc, CancellationToken cancellationToken = default); Task> GetPageListAsync(List conditionalList, PageModel page, CancellationToken cancellationToken); - Task> GetPageListAsync(List conditionalList, PageModel page, Expression> orderByExpression = null, OrderByType orderByType = OrderByType.Asc, CancellationToken cancellationToken=default); + Task> GetPageListAsync(List conditionalList, PageModel page, Expression> orderByExpression = null, OrderByType orderByType = OrderByType.Asc, CancellationToken cancellationToken = default); Task GetSingleAsync(Expression> whereExpression, CancellationToken cancellationToken); Task GetFirstAsync(Expression> whereExpression, CancellationToken cancellationToken); Task InsertAsync(T insertObj, CancellationToken cancellationToken); diff --git a/Src/Asp.NetCore2/SqlSugar/SimpleClient.cs b/Src/Asp.NetCore2/SqlSugar/SimpleClient.cs index fa6c4b06e..63d157c7c 100644 --- a/Src/Asp.NetCore2/SqlSugar/SimpleClient.cs +++ b/Src/Asp.NetCore2/SqlSugar/SimpleClient.cs @@ -9,16 +9,16 @@ using System.Threading.Tasks; namespace SqlSugar { - - public partial class SimpleClient : ISugarRepository,ISimpleClient where T : class, new() + + public partial class SimpleClient : ISugarRepository, ISimpleClient where T : class, new() { #region Interface - public ISqlSugarClient Context { get; set; } + public virtual ISqlSugarClient Context { get; set; } - public ITenant AsTenant() + public virtual ITenant AsTenant() { - var result= this.Context as ITenant; - if (result == null&& this.Context is SqlSugarProvider) + var result = this.Context as ITenant; + if (result == null && this.Context is SqlSugarProvider) { result = (this.Context as SqlSugarProvider).Root as ITenant; } @@ -28,7 +28,7 @@ namespace SqlSugar } return result; } - public ISqlSugarClient AsSugarClient() + public virtual ISqlSugarClient AsSugarClient() { return this.Context; } @@ -45,21 +45,21 @@ namespace SqlSugar { return this.Context.GetSimpleClient(); } - public SimpleClient CopyNew() + public SimpleClient CopyNew() { SimpleClient sm = new SimpleClient(); sm.Context = this.Context.CopyNew(); return sm; } - public RepositoryType CopyNew() where RepositoryType : ISugarRepository + public virtual RepositoryType CopyNew() where RepositoryType : ISugarRepository { Type type = typeof(RepositoryType); var isAnyParamter = type.GetConstructors().Any(z => z.GetParameters().Any()); object o = null; if (isAnyParamter) { - object[] pars= type.GetConstructors().First().GetParameters() - .Select(it=>(object)null).ToArray(); + object[] pars = type.GetConstructors().First().GetParameters() + .Select(it => (object)null).ToArray(); o = Activator.CreateInstance(type, pars); } else @@ -77,13 +77,13 @@ namespace SqlSugar } return result; } - public RepositoryType CopyNew(IServiceProvider serviceProvider) where RepositoryType : ISugarRepository + public virtual RepositoryType CopyNew(IServiceProvider serviceProvider) where RepositoryType : ISugarRepository { - var instance = handleDependencies(typeof(RepositoryType), serviceProvider,true); + var instance = handleDependencies(typeof(RepositoryType), serviceProvider, true); return (RepositoryType)instance; } - private object handleDependencies(Type type, IServiceProvider serviceProvider,bool needNewCopy = false) + private object handleDependencies(Type type, IServiceProvider serviceProvider, bool needNewCopy = false) { ConstructorInfo constructorInfo = null; var newInstanceType = type; @@ -129,7 +129,7 @@ namespace SqlSugar } return instance; } - private ISugarRepository setContext(ISugarRepository sugarRepository,bool needNewCopy) + private ISugarRepository setContext(ISugarRepository sugarRepository, bool needNewCopy) { if (sugarRepository.Context != null) { @@ -169,67 +169,67 @@ namespace SqlSugar } return false; } - public RepositoryType ChangeRepository() where RepositoryType : ISugarRepository + public virtual RepositoryType ChangeRepository() where RepositoryType : ISugarRepository { Type type = typeof(RepositoryType); var isAnyParamter = type.GetConstructors().Any(z => z.GetParameters().Any()); object o = null; if (isAnyParamter) { - o=Activator.CreateInstance(type, new string[] { null }); + o = Activator.CreateInstance(type, new string[] { null }); } - else + else { o = Activator.CreateInstance(type); } - var result= (RepositoryType)o; + var result = (RepositoryType)o; if (result.Context == null) { result.Context = this.Context; } return result; } - public RepositoryType ChangeRepository(IServiceProvider serviceProvider) where RepositoryType : ISugarRepository + public virtual RepositoryType ChangeRepository(IServiceProvider serviceProvider) where RepositoryType : ISugarRepository { var instance = handleDependencies(typeof(RepositoryType), serviceProvider, false); return (RepositoryType)instance; } - public ISugarQueryable AsQueryable() + public virtual ISugarQueryable AsQueryable() { return Context.Queryable(); } - public IInsertable AsInsertable(T insertObj) + public virtual IInsertable AsInsertable(T insertObj) { return Context.Insertable(insertObj); } - public IInsertable AsInsertable(T[] insertObjs) + public virtual IInsertable AsInsertable(T[] insertObjs) { return Context.Insertable(insertObjs); } - public IInsertable AsInsertable(List insertObjs) + public virtual IInsertable AsInsertable(List insertObjs) { return Context.Insertable(insertObjs); } - public IUpdateable AsUpdateable(T updateObj) + public virtual IUpdateable AsUpdateable(T updateObj) { return Context.Updateable(updateObj); } - public IUpdateable AsUpdateable(T[] updateObjs) + public virtual IUpdateable AsUpdateable(T[] updateObjs) { return Context.Updateable(updateObjs); } - public IUpdateable AsUpdateable(List updateObjs) + public virtual IUpdateable AsUpdateable(List updateObjs) { return Context.Updateable(updateObjs); } - public IUpdateable AsUpdateable() + public virtual IUpdateable AsUpdateable() { return Context.Updateable(); } - public IDeleteable AsDeleteable() + public virtual IDeleteable AsDeleteable() { return Context.Deleteable(); - } + } #endregion #region Method @@ -250,7 +250,7 @@ namespace SqlSugar { return Context.Queryable().Single(whereExpression); } - public virtual T GetFirst(Expression> whereExpression) + public virtual T GetFirst(Expression> whereExpression) { return Context.Queryable().First(whereExpression); } @@ -297,7 +297,7 @@ namespace SqlSugar return this.Context.Insertable(insertObj).ExecuteCommand() > 0; } - public virtual bool InsertOrUpdate(T data) + public virtual bool InsertOrUpdate(T data) { return this.Context.Storageable(data).ExecuteCommand() > 0; } @@ -324,11 +324,11 @@ namespace SqlSugar } public virtual Task InsertReturnSnowflakeIdAsync(T insertObj) { - return this.Context.Insertable(insertObj).ExecuteReturnSnowflakeIdAsync(); + return this.Context.Insertable(insertObj).ExecuteReturnSnowflakeIdAsync(); } public virtual Task> InsertReturnSnowflakeIdAsync(List insertObjs) { - return this.Context.Insertable(insertObjs).ExecuteReturnSnowflakeIdListAsync(); + return this.Context.Insertable(insertObjs).ExecuteReturnSnowflakeIdListAsync(); } public virtual T InsertReturnEntity(T insertObj) @@ -362,7 +362,7 @@ namespace SqlSugar } public virtual bool UpdateSetColumnsTrue(Expression> columns, Expression> whereExpression) { - return this.Context.Updateable().SetColumns(columns,true).Where(whereExpression).ExecuteCommand() > 0; + return this.Context.Updateable().SetColumns(columns, true).Where(whereExpression).ExecuteCommand() > 0; } public virtual bool Delete(T deleteObj) { @@ -411,28 +411,28 @@ namespace SqlSugar public virtual async Task> GetPageListAsync(Expression> whereExpression, PageModel page) { RefAsync count = 0; - var result =await Context.Queryable().Where(whereExpression).ToPageListAsync(page.PageIndex, page.PageSize, count); + var result = await Context.Queryable().Where(whereExpression).ToPageListAsync(page.PageIndex, page.PageSize, count); page.TotalCount = count; return result; } public virtual async Task> GetPageListAsync(Expression> whereExpression, PageModel page, Expression> orderByExpression = null, OrderByType orderByType = OrderByType.Asc) { RefAsync count = 0; - var result =await Context.Queryable().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(whereExpression).ToPageListAsync(page.PageIndex, page.PageSize, count); + var result = await Context.Queryable().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(whereExpression).ToPageListAsync(page.PageIndex, page.PageSize, count); page.TotalCount = count; return result; } public virtual async Task> GetPageListAsync(List conditionalList, PageModel page) { RefAsync count = 0; - var result =await Context.Queryable().Where(conditionalList).ToPageListAsync(page.PageIndex, page.PageSize, count); + var result = await Context.Queryable().Where(conditionalList).ToPageListAsync(page.PageIndex, page.PageSize, count); page.TotalCount = count; return result; } public virtual async Task> GetPageListAsync(List conditionalList, PageModel page, Expression> orderByExpression = null, OrderByType orderByType = OrderByType.Asc) { RefAsync count = 0; - var result =await Context.Queryable().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(conditionalList).ToPageListAsync(page.PageIndex, page.PageSize, count); + var result = await Context.Queryable().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(conditionalList).ToPageListAsync(page.PageIndex, page.PageSize, count); page.TotalCount = count; return result; } @@ -456,7 +456,7 @@ namespace SqlSugar } public virtual async Task InsertAsync(T insertObj) { - return await this.Context.Insertable(insertObj).ExecuteCommandAsync() > 0; + return await this.Context.Insertable(insertObj).ExecuteCommandAsync() > 0; } public virtual Task InsertReturnIdentityAsync(T insertObj) { @@ -496,7 +496,7 @@ namespace SqlSugar } public virtual async Task UpdateSetColumnsTrueAsync(Expression> columns, Expression> whereExpression) { - return await this.Context.Updateable().SetColumns(columns,true).Where(whereExpression).ExecuteCommandAsync() > 0; + return await this.Context.Updateable().SetColumns(columns, true).Where(whereExpression).ExecuteCommandAsync() > 0; } public virtual async Task DeleteAsync(T deleteObj) { @@ -532,9 +532,9 @@ namespace SqlSugar return this.Context.Insertable(insertObjs).ExecuteReturnSnowflakeIdListAsync(); } - public virtual Task GetByIdAsync(dynamic id,CancellationToken cancellationToken) + public virtual Task GetByIdAsync(dynamic id, CancellationToken cancellationToken) { - this.Context.Ado.CancellationToken= cancellationToken; + this.Context.Ado.CancellationToken = cancellationToken; return Context.Queryable().InSingleAsync(id); } public virtual Task> GetListAsync(CancellationToken cancellationToken) @@ -582,7 +582,7 @@ namespace SqlSugar page.TotalCount = count; return result; } - public virtual async Task> GetPageListAsync(List conditionalList, PageModel page, Expression> orderByExpression = null, OrderByType orderByType = OrderByType.Asc, CancellationToken cancellationToken=default) + public virtual async Task> GetPageListAsync(List conditionalList, PageModel page, Expression> orderByExpression = null, OrderByType orderByType = OrderByType.Asc, CancellationToken cancellationToken = default) { this.Context.Ado.CancellationToken = cancellationToken; RefAsync count = 0; @@ -691,6 +691,73 @@ namespace SqlSugar this.Context.Ado.CancellationToken = cancellationToken; return await this.Context.Deleteable().In(ids).ExecuteCommandAsync() > 0; } + + public int Count(List conditionalModels) + { + return Context.Queryable().Where(conditionalModels).Count(); + } + + public bool Delete(List conditionalModels) + { + return Context.Deleteable().Where(conditionalModels).ExecuteCommandHasChange(); + } + + public List GetList(Expression> whereExpression, List orderByModels) + { + return Context.Queryable().Where(whereExpression).OrderBy(orderByModels).ToList(); + } + + public List GetList(List conditionalList) + { + return Context.Queryable().Where(conditionalList).ToList(); + } + + public List GetList(List conditionalList, List orderByModels) + { + return Context.Queryable().Where(conditionalList).OrderBy(orderByModels).ToList(); + } + + public List GetPageList(Expression> whereExpression, PageModel page, List orderByModels) + { + var total = 0; + var list = Context.Queryable().Where(whereExpression).OrderBy(orderByModels).ToPageList(page.PageIndex, page.PageSize, ref total); + page.TotalCount = total; + return list; + } + + public List GetPageList(List conditionalList, PageModel page, List orderByModels) + { + var total = 0; + var list = Context.Queryable().Where(conditionalList).OrderBy(orderByModels).ToPageList(page.PageIndex, page.PageSize, ref total); + page.TotalCount = total; + return list; + } + + public T GetSingle(List conditionalModels) + { + return Context.Queryable().Where(conditionalModels).Single(); + } + + public T GetFirst(List conditionalModels) + { + return Context.Queryable().Where(conditionalModels).First(); + } + + public bool IsAny(List conditionalModels) + { + return Context.Queryable().Where(conditionalModels).Any(); + } + + public T GetFirst(Expression> whereExpression, List orderByModels) + { + return Context.Queryable().Where(whereExpression).OrderBy(orderByModels).First(); + } + + public T GetFirst(List conditionalModels, List orderByModels) + { + return Context.Queryable().Where(conditionalModels).OrderBy(orderByModels).First(); + } + #endregion }