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 : 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 GetById(object id) { return await m_DbContextBase.Set().FindByIdAsync(id); } public virtual async Task Add(TEntity entity, bool autoSave = true) { var ret = await m_DbContextBase.Set().AddAsync(entity); if (autoSave) await m_DbContextBase.SaveChangesAsync(); return ret.Entity; } public virtual async Task Adds(IEnumerable entitys, bool autoSave = true) { await m_DbContextBase.Set().AddRangeAsync(entitys); if (autoSave) await m_DbContextBase.SaveChangesAsync(); } public virtual async Task DeleteById(object id, bool autoSave = true) { var entity = await this.GetById(id); return await Delete(entity, autoSave); } public virtual async Task 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().Update(entity); } else { m_DbContextBase.Set().Remove(entity); } if (autoSave) await m_DbContextBase.SaveChangesAsync(); return true; } public virtual async Task Deletes(IEnumerable 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().UpdateRange(entity); } else { m_DbContextBase.Set().RemoveRange(entity); } if (autoSave) await m_DbContextBase.SaveChangesAsync(); return true; } public virtual async Task 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().Update(entity); if (autoSave) return (await m_DbContextBase.SaveChangesAsync()) > 0; return true; } public virtual async Task Update(IEnumerable list, bool autoSave = true) { m_DbContextBase.Set().UpdateRange(list); if (autoSave) await m_DbContextBase.SaveChangesAsync(); return true; } public virtual IQueryable Query(bool noTracking = false) { var ret = m_DbContextBase.Set().AsQueryable(); if (noTracking) { ret = ret.AsNoTracking(); } return ret; } public async virtual Task> GetAll(bool noTracking = false) { var ret = m_DbContextBase.Set().AsQueryable(); if (noTracking) { ret = ret.AsNoTracking(); } return await ret.ToListAsync(); } public virtual IQueryable Query(Expression> exp=null, bool noTracking = false) { var ret = m_DbContextBase.Set().AsQueryable(); if (exp != null) { ret = ret.Where(exp); } if (noTracking) { ret = ret.AsNoTracking(); } return ret; } public virtual async Task> Page(int page, int limit, Expression> exp = null, bool noTracking = false) { var ret = await Query(exp, noTracking).ListPagerAsync(limit, page, true); return ret; } public virtual async Task> PageDesc(int page, int limit, Expression> exp = null, bool noTracking = false, Expression> 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> PageAsc(int page, int limit, Expression> exp = null, bool noTracking = false, Expression> 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> expr) { return this.Query(expr).Count() > 0; } public async Task Save() { return (await m_DbContextBase.SaveChangesAsync()) > 0; } } }