init commit
This commit is contained in:
210
20220330_Vote/Ewide.RoadFlow/Data/RoadFlowRepository.cs
Normal file
210
20220330_Vote/Ewide.RoadFlow/Data/RoadFlowRepository.cs
Normal file
@@ -0,0 +1,210 @@
|
||||
using Furion;
|
||||
using Furion.DependencyInjection;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RoadFlow.Data
|
||||
{
|
||||
public class RoadFlowRepository<T> : IRoadFlowRepository<T>, ITransient where T:RoadFlow.Model.BaseEntity,new()
|
||||
{
|
||||
protected readonly IMemoryCache _memoryCache;
|
||||
protected readonly ISqlSugarRepository repository;
|
||||
protected readonly SqlSugarClient db;
|
||||
protected readonly string CACHEKEY = "roadflow_cache_" + typeof(T).FullName;
|
||||
public RoadFlowRepository()
|
||||
{
|
||||
_memoryCache = App.GetService< IMemoryCache > ();
|
||||
repository = App.GetService<ISqlSugarRepository>();
|
||||
db = repository.Context;
|
||||
//db选择
|
||||
db.ChangeDatabase("118_3310_ewide");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有的数据对象并缓存
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual List<T> GetAll()
|
||||
{
|
||||
return _memoryCache.GetOrCreate(CACHEKEY, v =>
|
||||
{
|
||||
return db.Queryable<T>().ToList();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 按Id获取一个对象
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public virtual T GetOneById(string id)
|
||||
{
|
||||
List<T> list = GetAll();
|
||||
return list.Find(p => p.Id == id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 按条件获取一个对象
|
||||
/// </summary>
|
||||
/// <param name="predicate"></param>
|
||||
/// <returns></returns>
|
||||
public virtual T GetOneBy(Predicate<T> predicate)
|
||||
{
|
||||
return GetAll().Find(predicate);
|
||||
}
|
||||
/// <summary>
|
||||
/// 按条件获取列表
|
||||
/// </summary>
|
||||
/// <param name="predicate"></param>
|
||||
/// <returns></returns>
|
||||
public virtual List<T> GetListBy(Predicate<T> predicate)
|
||||
{
|
||||
return GetAll().FindAll(predicate);
|
||||
}
|
||||
/// <summary>
|
||||
/// 增加对象
|
||||
/// </summary>
|
||||
/// <param name="t"></param>
|
||||
/// <returns></returns>
|
||||
public virtual int Add(T t)
|
||||
{
|
||||
int rtn =db.Insertable<T>(t).ExecuteCommand();
|
||||
ClearCache();
|
||||
return rtn;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 增加多个对象
|
||||
/// </summary>
|
||||
/// <param name="ts"></param>
|
||||
/// <returns></returns>
|
||||
public virtual int AddRangeList(List<T> ts)
|
||||
{
|
||||
int rtn =db.Insertable<T>(ts).ExecuteCommand();
|
||||
ClearCache();
|
||||
return rtn;
|
||||
}
|
||||
/// <summary>
|
||||
/// 更新对象
|
||||
/// </summary>
|
||||
/// <param name="t"></param>
|
||||
/// <returns></returns>
|
||||
public virtual int Update(T t,bool clearCache=true)
|
||||
{
|
||||
int rtn =db.Updateable<T>(t).ExecuteCommand();
|
||||
if(clearCache)
|
||||
ClearCache();
|
||||
return rtn;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新多个对象
|
||||
/// </summary>
|
||||
/// <param name="ts"></param>
|
||||
/// <returns></returns>
|
||||
public virtual int Update(List<T> ts,bool trans=true)
|
||||
{
|
||||
int rslt = 0;
|
||||
|
||||
try
|
||||
{
|
||||
if(trans)
|
||||
db.BeginTran();
|
||||
foreach (var t in ts)
|
||||
{
|
||||
rslt += this.Update(t,false);
|
||||
}
|
||||
if(trans)
|
||||
db.CommitTran();
|
||||
ClearCache();
|
||||
}
|
||||
catch
|
||||
{
|
||||
db.RollbackTran();
|
||||
}
|
||||
return rslt;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除对象(物理)
|
||||
/// </summary>
|
||||
/// <param name="t"></param>
|
||||
/// <returns></returns>
|
||||
public virtual int Delete(T t)
|
||||
{
|
||||
int rtn =db.Deleteable<T>(t).ExecuteCommand();
|
||||
ClearCache();
|
||||
return rtn;
|
||||
}
|
||||
/// <summary>
|
||||
/// 删除一批对象(物理)
|
||||
/// </summary>
|
||||
/// <param name="ts"></param>
|
||||
/// <returns></returns>
|
||||
public virtual int Delete(List<T> ts)
|
||||
{
|
||||
int rtn = db.Deleteable<T>(ts).ExecuteCommand();
|
||||
ClearCache();
|
||||
return rtn;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 按条件删除对象
|
||||
/// </summary>
|
||||
/// <param name="expression"></param>
|
||||
/// <returns></returns>
|
||||
public virtual int DeleteBy(System.Linq.Expressions.Expression<Func<T, bool>> expression)
|
||||
{
|
||||
int rtn =db.Deleteable<T>(expression).ExecuteCommand();
|
||||
ClearCache();
|
||||
return rtn;
|
||||
}
|
||||
/// <summary>
|
||||
/// 清空缓存
|
||||
/// </summary>
|
||||
public virtual void ClearCache()
|
||||
{
|
||||
_memoryCache.Remove(CACHEKEY);
|
||||
}
|
||||
/// <summary>
|
||||
/// 清空缓存
|
||||
/// </summary>
|
||||
public virtual void ClearCache(string id)
|
||||
{
|
||||
_memoryCache.Remove(CACHEKEY + id);
|
||||
}
|
||||
|
||||
public virtual SqlSugarClient getClient()
|
||||
{
|
||||
// string str =this.db.Ado.Connection.ConnectionString;
|
||||
return this.db;
|
||||
|
||||
}
|
||||
|
||||
public virtual void BeginTrans()
|
||||
{
|
||||
this.db.Ado.BeginTran();
|
||||
}
|
||||
|
||||
public virtual void CommitTran()
|
||||
{
|
||||
this.db.Ado.CommitTran();
|
||||
}
|
||||
|
||||
public virtual void RollbackTran()
|
||||
{
|
||||
this.db.Ado.RollbackTran();
|
||||
}
|
||||
|
||||
public virtual string getCacheKey()
|
||||
{
|
||||
return this.CACHEKEY;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user