2024-05-02 15:24:13 -07:00

138 lines
3.5 KiB
C#

using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Transactions;
using System;
using Church.Net.DAL.EF;
using System.Linq;
using Church.Net.Utility;
using System.Threading.Tasks;
using WebAPI.Logics.Interface;
using Church.Net.DAL.EFCoreDBF.Interface;
namespace WebAPI.Logics.Core
{
public class LogicBase<T> : ICrudLogic<T> where T : class, Church.Net.Entity.Interface.IEntity, new()
{
protected readonly LogicService logicService;
protected readonly ICrudDAL<T> crudDAL;
public LogicBase(
LogicService logicService,
ICrudDAL<T> crudDAL
)
{
this.logicService = logicService;
this.crudDAL = crudDAL;
}
public virtual bool CheckExist(T obj)
{
return this.crudDAL.CheckExist(obj);
}
public virtual void BeforeCreate(T entity)
{
}
public virtual int Create(T entity)
{
BeforeCreate(entity);
return this.crudDAL.Create(entity) + CreateDone(entity);
}
public virtual Task<int> CreateAsync(T entity)
{
BeforeCreate(entity);
return this.crudDAL.CreateAsync(entity);
}
public virtual int CreateDone(T entity)
{
return 0;
}
public virtual int CreateOrUpdate(T entity, out string id)
{
int result = 0;
if (this.crudDAL.CheckExist(entity))
{
result = this.Update(entity);
}
else
{
result = this.Create(entity);
}
id = entity.Id;
return result;
}
//public string CreateReturnId(T entity)
//{
// return this.crudDAL.CreateReturnId(entity);
//}
public virtual int Delete(T obj)
{
return this.crudDAL.Delete(obj);
}
public int Delete(Func<T, bool> filter = null)
{
return this.crudDAL.Delete(filter);
}
public T First(Func<T, bool> filter = null)
{
return this.crudDAL.First(filter);
}
public virtual IEnumerable<T> GetAll(Func<T, bool> filter = null)
{
return this.crudDAL.GetAll(filter);
}
//public IEnumerable<T> GetAllById(IEnumerable<string> Ids)
//{
// return this.crudDAL.GetAllById(Ids);
//}
public T GetById(string Id)
{
return this.crudDAL.GetById(Id);
}
public virtual void BeforeUpdate(T entity)
{
}
public virtual int Update(T entity)
{
BeforeUpdate(entity);
return this.crudDAL.Update(entity)+ UpdateDone(entity);
}
public virtual int UpdateDone(T entity)
{
return 0;
}
public int UpdateRange(IEnumerable<T> entities)
{
int result = 0;
foreach (var item in entities)
{
result += Update(item);
}
return result;// this.crudDAL.UpdateRange(entities);
}
public int CreateOrUpdateAll(IEnumerable<T> entities)
{
int result = 0;
foreach (var item in entities)
{
result += CreateOrUpdate(item,out string id);
}
return result;// this.crudDAL.UpdateRange(entities);
}
}
}