206 lines
7.1 KiB
C#
206 lines
7.1 KiB
C#
using Hncore.Infrastructure.Data;
|
|
using Hncore.Infrastructure.DDD;
|
|
using Hncore.Infrastructure.EF;
|
|
using Hncore.Infrastructure.EntitiesExtension;
|
|
using Hncore.Infrastructure.WebApi;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Linq.Expressions;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Hncore.Infrastructure.Service
|
|
{
|
|
|
|
public interface IFindService
|
|
{
|
|
|
|
}
|
|
|
|
public class ServiceBase<TEntity> : IFindService where TEntity : class
|
|
{
|
|
IHttpContextAccessor m_HttpContextAccessor;
|
|
|
|
public HttpContext HttpContext
|
|
{
|
|
get => m_HttpContextAccessor.HttpContext;
|
|
}
|
|
public ManageUserInfo ManagerInfo
|
|
{
|
|
get => HttpContext.Request.GetManageUserInfo();
|
|
}
|
|
protected DbContextBase m_DbContextBase { get; set; }
|
|
public ServiceBase(DbContextBase dbContext, IHttpContextAccessor httpContextAccessor)
|
|
{
|
|
m_DbContextBase = dbContext;
|
|
m_HttpContextAccessor = httpContextAccessor;
|
|
}
|
|
|
|
|
|
public virtual async Task<TEntity> GetById(object id)
|
|
{
|
|
return await m_DbContextBase.Set<TEntity>().FindByIdAsync(id);
|
|
}
|
|
|
|
public virtual async Task<TEntity> Add(TEntity entity, bool autoSave = true)
|
|
{
|
|
var ret = await m_DbContextBase.Set<TEntity>().AddAsync(entity);
|
|
if (autoSave)
|
|
await m_DbContextBase.SaveChangesAsync();
|
|
return ret.Entity;
|
|
}
|
|
public virtual async Task Adds(IEnumerable<TEntity> entitys, bool autoSave = true)
|
|
{
|
|
await m_DbContextBase.Set<TEntity>().AddRangeAsync(entitys);
|
|
if (autoSave)
|
|
await m_DbContextBase.SaveChangesAsync();
|
|
}
|
|
public virtual async Task<bool> DeleteById(object id, bool autoSave = true)
|
|
{
|
|
var entity = await this.GetById(id);
|
|
return await Delete(entity, autoSave);
|
|
}
|
|
|
|
public virtual async Task<bool> Delete(TEntity entity, bool autoSave = true)
|
|
{
|
|
//if (entity is ITenant)
|
|
//{
|
|
// var tenantId = HttpContext.Request.GetManageUserInfo()?.TenantId;
|
|
// if (tenantId.HasValue && tenantId > 0 && (entity as ITenant).TenantId != tenantId)
|
|
// {
|
|
// return false;
|
|
// }
|
|
//}
|
|
|
|
if (entity is ISoftDelete)
|
|
{
|
|
(entity as ISoftDelete).DeleteTag = 1;
|
|
m_DbContextBase.Set<TEntity>().Update(entity);
|
|
}
|
|
else
|
|
{
|
|
m_DbContextBase.Set<TEntity>().Remove(entity);
|
|
}
|
|
if (autoSave)
|
|
await m_DbContextBase.SaveChangesAsync();
|
|
return true;
|
|
}
|
|
|
|
public virtual async Task<bool> Deletes(IEnumerable<TEntity> entitys, bool autoSave = true)
|
|
{
|
|
if (entitys == null || entitys.Count() == 0)
|
|
return false;
|
|
|
|
var entity = entitys.FirstOrDefault();
|
|
|
|
if (entity is ISoftDelete)
|
|
{
|
|
foreach(var item in entitys) { (entity as ISoftDelete).DeleteTag = 1; }
|
|
m_DbContextBase.Set<TEntity>().UpdateRange(entity);
|
|
}
|
|
else
|
|
{
|
|
m_DbContextBase.Set<TEntity>().RemoveRange(entity);
|
|
}
|
|
if (autoSave)
|
|
await m_DbContextBase.SaveChangesAsync();
|
|
return true;
|
|
}
|
|
|
|
|
|
public virtual async Task<bool> Update(TEntity entity, bool autoSave = true)
|
|
{
|
|
//if (entity is ITenant)
|
|
//{
|
|
// var tenantId = HttpContext.Request.GetManageUserInfo()?.TenantId;
|
|
// if (tenantId.HasValue && tenantId > 0 && (entity as ITenant).TenantId != tenantId)
|
|
// {
|
|
// return false;
|
|
// }
|
|
//}
|
|
m_DbContextBase.Set<TEntity>().Update(entity);
|
|
if (autoSave)
|
|
return (await m_DbContextBase.SaveChangesAsync()) > 0;
|
|
return true;
|
|
}
|
|
|
|
public virtual async Task<bool> Update(IEnumerable<TEntity> list, bool autoSave = true)
|
|
{
|
|
m_DbContextBase.Set<TEntity>().UpdateRange(list);
|
|
if (autoSave)
|
|
await m_DbContextBase.SaveChangesAsync();
|
|
return true;
|
|
}
|
|
public virtual IQueryable<TEntity> Query(bool noTracking = false)
|
|
{
|
|
var ret = m_DbContextBase.Set<TEntity>().AsQueryable();
|
|
if (noTracking)
|
|
{
|
|
ret = ret.AsNoTracking();
|
|
}
|
|
return ret;
|
|
}
|
|
public async virtual Task<List<TEntity>> GetAll(bool noTracking = false)
|
|
{
|
|
var ret = m_DbContextBase.Set<TEntity>().AsQueryable();
|
|
if (noTracking)
|
|
{
|
|
ret = ret.AsNoTracking();
|
|
}
|
|
return await ret.ToListAsync();
|
|
}
|
|
public virtual IQueryable<TEntity> Query(Expression<Func<TEntity, bool>> exp=null, bool noTracking = false)
|
|
{
|
|
var ret = m_DbContextBase.Set<TEntity>().AsQueryable();
|
|
if (exp != null)
|
|
{
|
|
ret = ret.Where(exp);
|
|
}
|
|
if (noTracking)
|
|
{
|
|
ret = ret.AsNoTracking();
|
|
}
|
|
return ret;
|
|
}
|
|
public virtual async Task<PageData<TEntity>> Page(int page, int limit, Expression<Func<TEntity, bool>> exp = null, bool noTracking = false)
|
|
{
|
|
var ret = await Query(exp, noTracking).ListPagerAsync(limit, page, true);
|
|
return ret;
|
|
}
|
|
|
|
public virtual async Task<PageData<TEntity>> PageDesc<TOrderKey>(int page, int limit, Expression<Func<TEntity, bool>> exp = null, bool noTracking = false, Expression<Func<TEntity, TOrderKey>> order = null)
|
|
{
|
|
|
|
var query = Query(exp, noTracking);
|
|
if (order != null)
|
|
query = query.OrderByDescending(order);
|
|
|
|
var ret = await query.ListPagerAsync(limit, page, true);
|
|
return ret;
|
|
}
|
|
|
|
public virtual async Task<PageData<TEntity>> PageAsc<TOrderKey>(int page, int limit, Expression<Func<TEntity, bool>> exp = null, bool noTracking = false, Expression<Func<TEntity, TOrderKey>> order = null)
|
|
{
|
|
|
|
var query = Query(exp, noTracking);
|
|
if (order != null)
|
|
query = query.OrderBy(order);
|
|
|
|
var ret = await query.ListPagerAsync(limit, page, true);
|
|
return ret;
|
|
}
|
|
|
|
public virtual bool Exist(Expression<Func<TEntity, bool>> expr)
|
|
{
|
|
return this.Query(expr).Count() > 0;
|
|
}
|
|
|
|
public async Task<bool> Save()
|
|
{
|
|
return (await m_DbContextBase.SaveChangesAsync()) > 0;
|
|
}
|
|
}
|
|
}
|