| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- using SqlSugar;
- using System;
- using System.Collections.Generic;
- using System.Linq.Expressions;
- using System.Text;
- using System.Threading.Tasks;
- namespace System.Common
- {
- public class BaseRepository<T> : BaseContext, IRepository<T> where T : class, new()
- {
- //public SimpleClient<T> CurrentDb => new SimpleClient<T>(Db);
- /// <summary>
- /// 获取数量
- /// </summary>
- /// <param name="whereExpression"></param>
- /// <returns></returns>
- public async Task<int> GetCount(Expression<Func<T, bool>> whereExpression = null)
- {
- if (whereExpression != null)
- return await Db.Queryable<T>().With(SqlWith.NoLock).CountAsync(whereExpression);
- else
- return await Db.Queryable<T>().With(SqlWith.NoLock).CountAsync();
- }
- /// <summary>
- /// 获取所有
- /// </summary>
- /// <returns></returns>
- public async Task<List<T>> GetList()
- {
- return await Db.Queryable<T>().With(SqlWith.NoLock).ToListAsync();
- //return CurrentDb.GetList();
- }
- public async Task<List<T>> GetListALL(Expression<Func<T, bool>> whereExpression)
- {
- return await Db.Queryable<T>().With(SqlWith.NoLock).Where(whereExpression).ToListAsync();
- }
- public async Task<List<T>> GetListALL(List<IConditionalModel> conModels, string orderby)
- {
- return await Db.Queryable<T>().With(SqlWith.NoLock).Where(conModels).OrderBy(orderby).ToListAsync();
- }
- public async Task<List<T>> GetListALL(Expression<Func<T, bool>> whereExpression, Expression<Func<T, object>> orderExpression, OrderByType ordertype = OrderByType.Asc)
- {
- return await Db.Queryable<T>().With(SqlWith.NoLock).Where(whereExpression).OrderBy(orderExpression,ordertype).ToListAsync();
- }
- /// <summary>
- /// 分页获取所有
- /// </summary>
- /// <param name="conModels"></param>
- /// <param name="pagemodel"></param>
- /// <returns></returns>
- public async Task<PageData<T>> GetListByPage(List<IConditionalModel> conModels, PageModel pagemodel,string orderby)
- {
- var list= await Db.Queryable<T>().With(SqlWith.NoLock).Where(conModels).OrderBy(orderby).ToPageListAsync(pagemodel.PageIndex, pagemodel.PageSize, pagemodel.PageCount);
- PageData<T> pd = new PageData<T>();
- pd.Rows = list;
- pd.Totals = pagemodel.PageCount;
- return pd;// CurrentDb.GetPageList(conModels, pagemodel);
- }
- public async Task<PageData<T>> GetListByPage(List<IConditionalModel> conModels, PageModel pagemodel)
- {
- var list = await Db.Queryable<T>().With(SqlWith.NoLock).Where(conModels).ToPageListAsync(pagemodel.PageIndex, pagemodel.PageSize, pagemodel.PageCount);
- PageData<T> pd = new PageData<T>();
- pd.Rows = list;
- pd.Totals = pagemodel.PageCount;
- return pd;// CurrentDb.GetPageList(conModels, pagemodel);
- }
- public async Task<PageData<T>> GetListByPage(Expression<Func<T, bool>> whereExpression, PageModel pagemodel)
- {
- var list = await Db.Queryable<T>().With(SqlWith.NoLock).Where(whereExpression).ToPageListAsync(pagemodel.PageIndex, pagemodel.PageSize, pagemodel.PageCount);
- PageData<T> pd = new PageData<T>();
- pd.Rows = list;
- pd.Totals = pagemodel.PageCount;
- return pd;// CurrentDb.GetPageList(conModels, pagemodel);
- }
- public async Task<PageData<T>> GetListByPage(Expression<Func<T, bool>> whereExpression, PageModel pagemodel, string orderby)
- {
- var list = await Db.Queryable<T>().With(SqlWith.NoLock).Where(whereExpression).OrderBy(orderby).ToPageListAsync(pagemodel.PageIndex, pagemodel.PageSize, pagemodel.PageCount);
- PageData<T> pd = new PageData<T>();
- pd.Rows = list;
- pd.Totals = pagemodel.PageCount;
- return pd;// CurrentDb.GetPageList(conModels, pagemodel);
- }
- public async Task<T> GetSingle(Expression<Func<T, bool>> whereExpression)
- {
- return await Db.Queryable<T>().With(SqlWith.NoLock).Where(whereExpression).SingleAsync();
- //return CurrentDb.GetSingle(whereExpression);
- }
- /// <summary>
- /// 添加
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public async Task<int> Add(T obj)
- {
- return await Db.Insertable<T>(obj).ExecuteReturnIdentityAsync();
- //return CurrentDb.InsertReturnIdentity(obj);
- }
- /// <summary>
- /// 批量添加
- /// </summary>
- /// <param name="objlist"></param>
- /// <returns></returns>
- public async Task<bool> AddMany(List<T> objlist)
- {
- return await Db.Insertable<T>(objlist).ExecuteCommandIdentityIntoEntityAsync();
- }
- /// <summary>
- /// 根据条件删除
- /// </summary>
- /// <returns></returns>
- public async Task<bool> Delete(Expression<Func<T, bool>> whereExpression)
- {
- return await Db.Deleteable<T>(whereExpression).ExecuteCommandHasChangeAsync();
- //return CurrentDb.Delete(whereExpression);
- }
- /// <summary>
- /// 更新
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public async Task<bool> Update(T obj)
- {
- return await Db.Updateable<T>(obj).ExecuteCommandHasChangeAsync();
- //return CurrentDb.Update(obj);
- }
- }
- }
|