| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 | using DW5S.Entity;using Microsoft.EntityFrameworkCore;using Microsoft.EntityFrameworkCore.ChangeTracking;using Microsoft.EntityFrameworkCore.Query;using System;using System.Collections.Generic;using System.Linq;using System.Linq.Expressions;using System.Text;using System.Threading.Tasks;namespace DW5S.Repostory{    public interface IRepository<T, IDType> where T : BaseEntity<IDType>    {        IQueryable<T> AsQueryable();        Task<T> GetByIdAsync(object id);        Task<IEnumerable<T>> GetAllAsync();        Task<IEnumerable<T>> GetAllAsync<TKey>(Expression<Func<T, TKey>> keySelector = null, bool asc = true);        Task<IEnumerable<T>> FindAsync(Expression<Func<T, bool>> predicate);        Task<IEnumerable<T>> FindAsync<TKey>(Expression<Func<T, bool>> predicate, Expression<Func<T, TKey>> keySelector = null, bool asc = true);        Task<IEnumerable<Res>> GetDistinctAsync<Res>(Expression<Func<T, Res>> selector);        Task<T> FirstOrDefaultAsync(Expression<Func<T, bool>> predicate = null);        Task<T> AddOrUpdateAsync(T entity);        Task AddOrUpdateAsync(IEnumerable<T> entitis);        /// <summary>        /// 修改某些字段        /// expression  s => s.SetProperty(b => b.Enable, true)        /// </summary>        /// <param name="predicate"></param>        /// <param name="expression">ddd</param>        /// <returns></returns>        Task<int> UpdatePropAsync(Expression<Func<T, bool>> predicate, Expression<Func<SetPropertyCalls<T>, SetPropertyCalls<T>>> expression);        Task<int> DeleteAsync(T entity);        Task<int> DeleteAsync(IEnumerable<T> entitis);        Task<int> DeleteAsync(Expression<Func<T, bool>> predicate = null);        Task<bool> AnyAsync(Expression<Func<T, bool>> predicate = null);        T MaxBy<TResult>(Expression<Func<T, TResult>> selector, Expression<Func<T, bool>> predicate = null);    }    public class Repository<T, IDType> : IRepository<T, IDType> where T : BaseEntity<IDType>    {        protected readonly OracleContext ctx;        protected readonly DbSet<T> dbSet;        public Repository(OracleContext ctx)        {            this.ctx = ctx;            this.dbSet = ctx.Set<T>();        }        public IQueryable<T> AsQueryable()        {            return dbSet.AsQueryable();        }        public async Task<T> GetByIdAsync(object id)        {            return await dbSet.FindAsync(id);        }        public async Task<IEnumerable<T>> GetAllAsync()        {            return await dbSet.ToListAsync();        }        public async Task<IEnumerable<T>> GetAllAsync<TKey>(Expression<Func<T, TKey>> keySelector = null, bool asc = true)        {            if (keySelector == null)            {                return await dbSet.ToListAsync();            }            else            {                if (asc)                    return await dbSet.OrderBy(keySelector).ToListAsync();                else                    return await dbSet.OrderByDescending(keySelector).ToListAsync();            }        }        public async Task<IEnumerable<T>> FindAsync(Expression<Func<T, bool>> predicate)        {            return await dbSet.Where(predicate).ToListAsync();        }        public async Task<IEnumerable<T>> FindAsync<TKey>(Expression<Func<T, bool>> predicate, Expression<Func<T, TKey>> keySelector = null, bool asc = true)        {            if (keySelector == null)            {                return await dbSet.Where(predicate).ToListAsync();            }            else            {                if (asc)                    return await dbSet.Where(predicate).OrderBy(keySelector).ToListAsync();                else                    return await dbSet.Where(predicate).OrderByDescending(keySelector).ToListAsync();            }        }        public async Task<T> FirstOrDefaultAsync(Expression<Func<T, bool>> predicate = null)        {            if (predicate != null)                return await dbSet.Where(predicate).FirstOrDefaultAsync();            return await dbSet.FirstOrDefaultAsync();        }        public async Task<IEnumerable<Res>> GetDistinctAsync<Res>(Expression<Func<T, Res>> selector)        {            var res = await dbSet.Select(selector).Distinct().ToListAsync();            return res;        }        public async Task AddOrUpdateAsync(IEnumerable<T> entity)        {            if (entity.First().Id.Equals(0))            {                foreach (var item in entity)                {                    item.CreateTime = DateTime.Now;                    item.UpdateTime = DateTime.Now;                }                await dbSet.AddRangeAsync(entity);            }            else            {                foreach (var item in entity)                {                    item.UpdateTime = DateTime.Now;                }                dbSet.UpdateRange(entity);            }        }        public async Task<T> AddOrUpdateAsync(T entity)        {            if (entity.Id.Equals(0))            {                entity.CreateTime = DateTime.Now;                entity.UpdateTime = DateTime.Now;                var res = await dbSet.AddAsync(entity);                return res.Entity;            }            else            {                entity.UpdateTime = DateTime.Now;                var res = dbSet.Update(entity);                return res.Entity;            }        }        public async Task<int> DeleteAsync(T entity)        {            return await dbSet.Where(p => p.Id.Equals(entity.Id)).ExecuteDeleteAsync();        }        public async Task<int> DeleteAsync(IEnumerable<T> entities)        {            var ids = entities.Select(p => p.Id);            return await DeleteAsync(p => ids.Contains(p.Id));        }        public async Task<int> DeleteAsync(Expression<Func<T, bool>> predicate = null)        {            return await dbSet.Where(predicate).ExecuteDeleteAsync();        }        public async Task<bool> AnyAsync(Expression<Func<T, bool>> predicate = null)        {            return await dbSet.AnyAsync(predicate);        }        public T MaxBy<TResult>(Expression<Func<T, TResult>> selector, Expression<Func<T, bool>> predicate = null)        {            if (predicate == null)                return dbSet.MaxBy(selector);            else                return dbSet.Where(predicate).MaxBy(selector);        }        public async Task<int> UpdatePropAsync(Expression<Func<T, bool>> predicate, Expression<Func<SetPropertyCalls<T>, SetPropertyCalls<T>>> expression)        {            return await dbSet.Where(predicate)                .ExecuteUpdateAsync(expression);        }    }}
 |