|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722 |
- using Permission.Infrastructure.Core;
- using System;
- using System.Collections.Generic;
- using System.Data.Common;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Threading.Tasks;
- using Microsoft.EntityFrameworkCore;
- using System.Reflection;
- using Permission.Infrastructure.WebControls;
- using Permission.Entity.DbContext;
- using Permission.Infrastructure.Repositories;
-
- namespace Permission.Data.Repositories
- {
- /// <summary>
- /// 仓储数据访问基接口实现
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public class BaseRepository<T> : IBaseRepository<T> where T : BaseEntity
- {
- #region 数据上下文
-
- /// <summary>
- /// 数据上下文
- /// </summary>
- private readonly PermissionContext _context;
-
- /// <summary>
- /// 工作单元
- /// </summary>
- private IUnitOfWork _unitOfWork;
-
- public IQueryable<T> Entities => _context.Set<T>();
-
- public BaseRepository(PermissionContext context, IUnitOfWork unitOfWork)
- {
- _context = context;
- _unitOfWork = unitOfWork;
- }
-
- #endregion
-
- #region 单模型 CRUD 操作
-
- public virtual bool Save(T entity, bool isCommit = true)
- {
- _context.Add(entity);
- if (isCommit)
- {
- return _unitOfWork.SaveChanges() > 0;
- }
- else
- {
- return false;
- }
- }
-
- public virtual async Task<bool> SaveAsync(T entity, bool isCommit = true)
- {
- _context.Set<T>().Add(entity);
- if (isCommit)
- {
- return await Task.Run(() => _unitOfWork.SaveChanges() > 0);
- }
- else
- {
- return await Task.Run(() => false);
- }
- }
-
- public virtual bool Update(T entity, bool isCommit = true)
- {
- if (_context.Entry(entity).State == EntityState.Detached)
- {
- _context.Set<T>().Attach(entity);
- _context.Entry(entity).State = EntityState.Modified;
- }
- else
- {
- _context.Entry(entity).State = EntityState.Modified;
- }
-
- if (isCommit)
- {
-
- return _unitOfWork.SaveChanges() > 0;
- }
- else
- {
- return false;
- }
- }
-
- public virtual async Task<bool> UpdateAsync(T entity, bool isCommit = true)
- {
- //_context.Set<T>().Attach(entity);
- //_context.Entry<T>(entity).State = EntityState.Modified;
- if (_context.Entry(entity).State == EntityState.Detached)
- {
- _context.Set<T>().Attach(entity);
- _context.Entry(entity).State = EntityState.Modified;
- }
- else
- {
- _context.Entry(entity).State = EntityState.Modified;
- }
- if (isCommit)
- {
- return await Task.Run(() => _unitOfWork.SaveChanges() > 0);
- }
- else
- {
- return await Task.Run(() => false);
- }
- }
-
- public virtual bool SaveOrUpdate(T entity, bool isSave, bool isCommit = true)
- {
- return isSave ? Save(entity, isCommit) : Update(entity, isCommit);
- }
-
- public virtual async Task<bool> SaveOrUpdateAsync(T entity, bool isSave, bool isCommit = true)
- {
- return isSave ? await SaveAsync(entity, isCommit) : await UpdateAsync(entity, isCommit);
- }
-
- public virtual T Get(Expression<Func<T, bool>> predicate)
- {
- //return _context.Set<T>().AsNoTracking().SingleOrDefault(predicate);
- return _context.Set<T>().SingleOrDefault(predicate);
- }
-
- public virtual T Get(object key)
- {
- return _context.Set<T>().Find(key);
- }
-
- public virtual async Task<T> GetAsync(object key)
- {
- return await Task.Run(() => _context.Set<T>().Find(key));
- }
-
- public virtual async Task<T> GetAsync(Expression<Func<T, bool>> predicate)
- {
- return await Task.Run(() => _context.Set<T>().SingleOrDefault(predicate));
- }
-
- public virtual bool Delete(T entity, bool isCommit = true)
- {
- if (entity == null)
- {
- return false;
- }
- _context.Set<T>().Attach(entity);
- _context.Set<T>().Remove(entity);
- if (isCommit)
- {
- return _unitOfWork.SaveChanges() > 0;
- }
- else
- {
- return false;
- }
- }
-
- public virtual async Task<bool> DeleteAsync(T entity, bool isCommit = true)
- {
- if (entity == null)
- {
- return await Task.Run(() => false);
- }
- _context.Set<T>().Attach(entity);
- _context.Set<T>().Remove(entity);
- if (isCommit)
- {
- return await Task.Run(() => _unitOfWork.SaveChanges() > 0);
- }
- else
- {
- return await Task.Run(() => false);
- }
- }
-
- #endregion
-
- #region 多模型 操作
-
- public virtual bool SaveList(List<T> entities, bool isCommit = true)
- {
- if (entities == null || !entities.Any())
- {
- return false;
- }
- entities.ToList().ForEach(item =>
- {
- _context.Set<T>().Add(item);
- });
- if (isCommit)
- {
- return _unitOfWork.SaveChanges() > 0;
- }
- else
- {
- return false;
- }
- }
-
- public virtual async Task<bool> SaveListAsync(List<T> entities, bool isCommit = true)
- {
- if (entities == null || !entities.Any())
- {
- return await Task.Run(() => false);
- }
- entities.ToList().ForEach(item =>
- {
- _context.Set<T>().Add(item);
- });
- if (isCommit)
- {
- return await Task.Run(() => _unitOfWork.SaveChanges() > 0);
- }
- else
- {
- return await Task.Run(() => false);
- }
- }
-
- public virtual bool SaveList<TEntity>(List<TEntity> entities, bool isCommit = true) where TEntity : class
- {
- if (entities == null || !entities.Any())
- {
- return false;
- }
- var tmp = _context.ChangeTracker.Entries<T>().ToList();
- foreach (var x in tmp)
- {
- var properties = typeof(T).GetTypeInfo().GetProperties();
- foreach (var y in properties)
- {
- var entry = x.Property(y.Name);
- entry.CurrentValue = entry.OriginalValue;
- entry.IsModified = false;
- y.SetValue(x.Entity, entry.OriginalValue);
- }
- x.State = EntityState.Unchanged;
- }
- entities.ToList().ForEach(item =>
- {
- _context.Set<TEntity>().Add(item);
- });
- if (isCommit)
- {
- return _unitOfWork.SaveChanges() > 0;
- }
- else
- {
- return false;
- }
- }
-
- public virtual async Task<bool> SaveListAsync<TEntity>(List<TEntity> entities, bool isCommit = true) where TEntity : class
- {
- if (entities == null || !entities.Any())
- {
- return await Task.Run(() => false);
- }
- var tmp = _context.ChangeTracker.Entries<T>().ToList();
- foreach (var x in tmp)
- {
- var properties = typeof(T).GetTypeInfo().GetProperties();
- foreach (var y in properties)
- {
- var entry = x.Property(y.Name);
- entry.CurrentValue = entry.OriginalValue;
- entry.IsModified = false;
- y.SetValue(x.Entity, entry.OriginalValue);
- }
- x.State = EntityState.Unchanged;
- }
- entities.ToList().ForEach(item =>
- {
- _context.Set<TEntity>().Add(item);
- });
- if (isCommit)
- {
- return await Task.Run(() => _unitOfWork.SaveChanges() > 0);
- }
- else
- {
- return await Task.Run(() => false);
- }
- }
-
- public virtual bool UpdateList(List<T> entities, bool isCommit = true)
- {
- if (entities == null || !entities.Any())
- {
- return false;
- }
- entities.ToList().ForEach(item =>
- {
- _context.Set<T>().Attach(item);
- _context.Entry<T>(item).State = EntityState.Modified;
- });
- if (isCommit)
- {
- return _unitOfWork.SaveChanges() > 0;
- }
- else
- {
- return false;
- }
- }
-
- public virtual async Task<bool> UpdateListAsync(List<T> entities, bool isCommit = true)
- {
- if (entities == null || !entities.Any())
- {
- return await Task.Run(() => false);
- }
- entities.ToList().ForEach(item =>
- {
- _context.Set<T>().Attach(item);
- _context.Entry<T>(item).State = EntityState.Modified;
- });
- if (isCommit)
- {
- return await Task.Run(() => _unitOfWork.SaveChanges() > 0);
- }
- else
- {
- return await Task.Run(() => false);
- }
- }
-
- public virtual bool UpdateList<TEntity>(List<TEntity> entities, bool isCommit = true) where TEntity : class
- {
- if (entities == null || !entities.Any())
- {
- return false;
- }
- entities.ToList().ForEach(item =>
- {
- _context.Set<TEntity>().Attach(item);
- _context.Entry<TEntity>(item).State = EntityState.Modified;
- });
- if (isCommit)
- {
- return _unitOfWork.SaveChanges() > 0;
- }
- else
- {
- return false;
- }
- }
-
- public virtual async Task<bool> UpdateListAsync<TEntity>(List<TEntity> entities, bool isCommit = true) where TEntity : class
- {
- if (entities == null || !entities.Any())
- {
- return await Task.Run(() => false);
- }
- entities.ToList().ForEach(item =>
- {
- _context.Set<TEntity>().Attach(item);
- _context.Entry<TEntity>(item).State = EntityState.Modified;
- });
- if (isCommit)
- {
- return await Task.Run(() => _unitOfWork.SaveChanges() > 0);
- }
- else
- {
- return await Task.Run(() => false);
- }
- }
-
- public virtual bool Delete(Expression<Func<T, bool>> predicate, bool isCommit = true)
- {
- IQueryable<T> entry = (predicate == null) ? _context.Set<T>().AsQueryable() : _context.Set<T>().Where(predicate);
- List<T> list = entry.ToList();
- if (list != null && list.Count == 0)
- {
- return false;
- }
- list.ForEach(item =>
- {
- _context.Set<T>().Attach(item);
- _context.Set<T>().Remove(item);
- });
- if (isCommit)
- {
- return _unitOfWork.SaveChanges() > 0;
- }
- else
- {
- return false;
- }
- }
-
- public virtual async Task<bool> DeleteAsync(Expression<Func<T, bool>> predicate, bool isCommit = true)
- {
- IQueryable<T> entry = (predicate == null) ? _context.Set<T>().AsQueryable() : _context.Set<T>().Where(predicate);
- List<T> list = entry.ToList();
- if (list != null && list.Count == 0)
- {
- return await Task.Run(() => false);
- }
- list.ForEach(item =>
- {
- _context.Set<T>().Attach(item);
- _context.Set<T>().Remove(item);
- });
- if (isCommit)
- {
- return await Task.Run(() => _unitOfWork.SaveChanges() > 0);
- }
- else
- {
- return await Task.Run(() => false);
- }
- }
-
- public virtual bool DeleteList(List<T> entities, bool isCommit = true)
- {
- if (entities == null || !entities.Any())
- {
- return false;
- }
- entities.ToList().ForEach(item =>
- {
- _context.Set<T>().Attach(item);
- _context.Set<T>().Remove(item);
- });
- if (isCommit)
- {
- return _unitOfWork.SaveChanges() > 0;
- }
- else
- {
- return false;
- }
- }
-
- public virtual async Task<bool> DeleteListAsync(List<T> entities, bool isCommit = true)
- {
- if (entities == null || !entities.Any())
- {
- return await Task.Run(() => false);
- }
- entities.ToList().ForEach(item =>
- {
- _context.Set<T>().Attach(item);
- _context.Set<T>().Remove(item);
- });
- if (isCommit)
- {
- return await Task.Run(() => _unitOfWork.SaveChanges() > 0);
- }
- else
- {
- return await Task.Run(() => false);
- }
- }
-
- public virtual bool DeleteList<TEntity>(List<TEntity> entities, bool isCommit = true) where TEntity : class
- {
- if (entities == null || !entities.Any())
- {
- return false;
- }
- entities.ToList().ForEach(item =>
- {
- _context.Set<TEntity>().Attach(item);
- _context.Set<TEntity>().Remove(item);
- });
- if (isCommit)
- {
- return _unitOfWork.SaveChanges() > 0;
- }
- else
- {
- return false;
- }
- }
-
- public virtual async Task<bool> DeleteListAsync<TEntity>(List<TEntity> entities, bool isCommit = true) where TEntity : class
- {
- if (entities == null || !entities.Any())
- {
- return await Task.Run(() => false);
- }
- entities.ToList().ForEach(item =>
- {
- _context.Set<TEntity>().Attach(item);
- _context.Set<TEntity>().Remove(item);
- });
- if (isCommit)
- {
- return await Task.Run(() => _unitOfWork.SaveChanges() > 0);
- }
- else
- {
- return await Task.Run(() => false);
- }
- }
-
- #endregion
-
- #region 获取多条数据操作
-
- public virtual IQueryable<T> LoadAll(Expression<Func<T, bool>> predicate)
- {
- return predicate != null ? _context.Set<T>().Where(predicate) : _context.Set<T>().AsQueryable<T>();
- }
-
- public virtual async Task<IQueryable<T>> LoadAllAsync(Expression<Func<T, bool>> predicate)
- {
- return predicate != null ? await Task.Run(() => _context.Set<T>().Where(predicate)) : await Task.Run(() => _context.Set<T>().AsQueryable<T>());
- }
-
- public virtual List<T> LoadListAll(Expression<Func<T, bool>> predicate)
- {
- return predicate != null ? _context.Set<T>().Where(predicate).ToList() : _context.Set<T>().AsQueryable<T>().ToList();
- }
-
- public virtual async Task<List<T>> LoadListAllAsync(Expression<Func<T, bool>> predicate)
- {
- return predicate != null ? await Task.Run(() => _context.Set<T>().Where(predicate).ToList()) : await Task.Run(() => _context.Set<T>().AsQueryable<T>().ToList());
- }
-
- public virtual IQueryable<T> LoadAllBySql(string sql, params DbParameter[] para)
- {
- return _context.Set<T>().FromSql(sql, para);
- }
-
- public virtual async Task<IQueryable<T>> LoadAllBySqlAsync(string sql, params DbParameter[] para)
- {
- return await Task.Run(() => _context.Set<T>().FromSql(sql, para));
- }
-
- public virtual List<T> LoadListAllBySql(string sql, params DbParameter[] para)
- {
- return _context.Set<T>().FromSql(sql, para).Cast<T>().ToList();
- }
-
- public virtual async Task<List<T>> LoadListAllBySqlAsync(string sql, params DbParameter[] para)
- {
- return await Task.Run(() => _context.Set<T>().FromSql(sql, para).Cast<T>().ToList());
- }
-
- public virtual List<TResult> QueryEntity<TEntity, TOrderBy, TResult>(Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, TOrderBy>> orderby, Expression<Func<TEntity, TResult>> selector, bool isAsc)
- where TEntity : class
- where TResult : class
- {
- IQueryable<TEntity> query = _context.Set<TEntity>();
- if (where != null)
- {
- query = query.Where(where);
- }
- if (orderby != null)
- {
- query = isAsc ? query.OrderBy(orderby) : query.OrderByDescending(orderby);
- }
- if (selector == null)
- {
- return query.Cast<TResult>().ToList();
- }
- return query.Select(selector).ToList();
- }
-
- public virtual async Task<List<TResult>> QueryEntityAsync<TEntity, TOrderBy, TResult>(Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, TOrderBy>> orderby, Expression<Func<TEntity, TResult>> selector, bool isAsc)
- where TEntity : class
- where TResult : class
- {
- IQueryable<TEntity> query = _context.Set<TEntity>();
- if (where != null)
- {
- query = query.Where(where);
- }
- if (orderby != null)
- {
- query = isAsc ? query.OrderBy(orderby) : query.OrderByDescending(orderby);
- }
- if (selector == null)
- {
- return await Task.Run(() => query.Cast<TResult>().ToList());
- }
- return await Task.Run(() => query.Select(selector).ToList());
- }
-
- public virtual List<object> QueryObject<TEntity, TOrderBy>(Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, TOrderBy>> orderby, Func<IQueryable<TEntity>, List<object>> selector, bool isAsc) where TEntity : class
- {
- IQueryable<TEntity> query = _context.Set<TEntity>();
- if (where != null)
- {
- query = query.Where(where);
- }
- if (orderby != null)
- {
- query = isAsc ? query.OrderBy(orderby) : query.OrderByDescending(orderby);
- }
- if (selector == null)
- {
- return query.ToList<object>();
- }
- return selector(query);
- }
-
- public virtual async Task<List<object>> QueryObjectAsync<TEntity, TOrderBy>(Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, TOrderBy>> orderby, Func<IQueryable<TEntity>, List<object>> selector, bool isAsc) where TEntity : class
- {
- IQueryable<TEntity> query = _context.Set<TEntity>();
- if (where != null)
- {
- query = query.Where(where);
- }
-
- if (orderby != null)
- {
- query = isAsc ? query.OrderBy(orderby) : query.OrderByDescending(orderby);
- }
- if (selector == null)
- {
- return await Task.Run(() => query.ToList<object>());
- }
- return await Task.Run(() => selector(query));
- }
-
- public virtual dynamic QueryDynamic<TEntity, TOrderBy>(Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, TOrderBy>> orderby, Func<IQueryable<TEntity>, List<object>> selector, bool isAsc) where TEntity : class
- {
- //List<object> list = QueryObject<TEntity, TOrderBy>
- // (where, orderby, selector, isAsc);
- //return Common.JsonHelper.JsonConvert.JsonClass(list);
- throw new NotImplementedException();
- }
-
- public virtual async Task<dynamic> QueryDynamicAsync<TEntity, TOrderBy>(Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, TOrderBy>> orderby, Func<IQueryable<TEntity>, List<object>> selector, bool isAsc) where TEntity : class
- {
- // List<object> list = QueryObject<TEntity, TOrderBy>
- //(where, orderby, selector, isAsc);
- // return await Task.Run(() => Common.JsonHelper.JsonConvert.JsonClass(list));
- throw new NotImplementedException();
- }
-
- #endregion
-
- #region 验证是否存在
-
- public virtual bool IsExist(Expression<Func<T, bool>> predicate)
- {
- var entry = _context.Set<T>().Where(predicate);
- return (entry.Any());
- }
-
- public virtual async Task<bool> IsExistAsync(Expression<Func<T, bool>> predicate)
- {
- var entry = _context.Set<T>().Where(predicate);
- return await Task.Run(() => entry.Any());
- }
-
- public virtual bool IsExist(string sql, params DbParameter[] para)
- {
- return _context.Database.ExecuteSqlCommand(sql, para) > 0;
- }
-
- public virtual async Task<bool> IsExistAsync(string sql, params DbParameter[] para)
- {
- return await Task.Run(() => _context.Database.ExecuteSqlCommand(sql, para) > 0);
- }
-
- #endregion
-
- public virtual Page<T> PageList(int pageindex, int pagesize, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy, Expression<Func<T, T>> selector, out int total, Expression<Func<T, bool>> where = null)
- {
- int row = (--pageindex) * pagesize;
-
- if (where != null)
- {
- total = _context.Set<T>().Where(where).Count();
-
- var data = orderBy(_context.Set<T>().Where(where))
- .Skip(row)
- .Take(pagesize)
- .Select(selector).ToList();
-
- var res = new Page<T>(data, pagesize, total);
- return res;
- }
- else
- {
- total = _context.Set<T>().Count();
- var data = orderBy(_context.Set<T>()).Skip(row)
- .Take(pagesize)
- .Select(selector).ToList();
-
- var res = new Page<T>(data, pagesize, total);
- return res;
- }
- }
-
- public virtual Page<T> PageList<S>(int pageindex, int pagesize, Expression<Func<T, bool>> predicate, Expression<Func<T, S>> orderPredicate, bool isAsc)
- {
- var data = _context.Set<T>().AsQueryable();
- if (predicate != null)
- {
- data = data.Where(predicate);
- }
- var count = data.Count();
- if (isAsc)
- {
- data = data.OrderBy(orderPredicate).Skip((pageindex - 1) * pagesize).Take(pagesize);
- return new Page<T>(data.ToList(), pagesize, count);
- }
- else
- {
- data = data.OrderByDescending(orderPredicate).Skip((pageindex - 1) * pagesize).Take(pagesize);
- return new Page<T>(data.ToList(), pagesize, count);
- }
- }
- }
- }
|