/*******************************************************************************
* Copyright © 2020 WaterCloud.Framework 版权所有
* Author: WaterCloud
* Description: WaterCloud快速开发平台
* Website:
*********************************************************************************/
using Chloe;
using WaterCloud.Code;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading.Tasks;
namespace WaterCloud.DataBase
{
///
/// 仓储实现
///
public class RepositoryBase : IRepositoryBase
{
private IDbContext _context;
public RepositoryBase(IDbContext context)
{
_context = context;
}
public IDbContext GetDbContext()
{
return _context;
}
public RepositoryBase(string ConnectStr, string providerName)
{
_context = DBContexHelper.Contex(ConnectStr, providerName);
}
public IRepositoryBase BeginTrans()
{
if (_context.Session.CurrentTransaction == null)
{
_context.Session.BeginTransaction();
}
return this;
}
public void Commit()
{
try
{
if (_context.Session.CurrentTransaction != null)
{
_context.Session.CommitTransaction();
}
}
catch (Exception)
{
this.Rollback();
throw;
}
}
public void Rollback()
{
if (_context.Session.CurrentTransaction != null)
{
_context.Session.RollbackTransaction();
}
}
public async Task Insert(TEntity entity) where TEntity : class
{
try
{
return await _context.InsertAsync(entity);
}
catch (Exception)
{
this.Rollback();
throw;
}
}
public async Task Insert(List entitys) where TEntity : class
{
try
{
await _context.InsertRangeAsync(entitys);
return 1;
}
catch (Exception)
{
this.Rollback();
throw;
}
}
public async Task Update(TEntity entity) where TEntity : class
{
try
{
TEntity newentity = _context.QueryByKey(entity);
_context.TrackEntity(newentity);
PropertyInfo[] newprops = newentity.GetType().GetProperties();
PropertyInfo[] props = entity.GetType().GetProperties();
foreach (PropertyInfo prop in props)
{
if (prop.GetValue(entity, null) != null)
{
PropertyInfo item = newprops.Where(a => a.Name == prop.Name).FirstOrDefault();
if (item != null)
{
item.SetValue(newentity, prop.GetValue(entity, null), null);
if (prop.GetValue(entity, null).ToString() == " ")
item.SetValue(newentity, null, null);
}
}
}
return await _context.UpdateAsync(newentity);
}
catch (Exception)
{
this.Rollback();
throw;
}
}
public async Task Update(Expression> predicate, Expression> content) where TEntity : class
{
try
{
return await _context.UpdateAsync(predicate, content);
}
catch (Exception)
{
this.Rollback();
throw;
}
}
public async Task Delete(TEntity entity) where TEntity : class
{
try
{
return await _context.DeleteAsync(entity);
}
catch (Exception)
{
this.Rollback();
throw;
}
}
public async Task Delete(Expression> predicate) where TEntity : class
{
try
{
return await _context.DeleteAsync(predicate);
}
catch (Exception)
{
this.Rollback();
throw;
}
}
public async Task FindEntity(object keyValue) where TEntity : class
{
return await _context.QueryByKeyAsync(keyValue);
}
public async Task FindEntity(Expression> predicate) where TEntity : class
{
return _context.Query().FirstOrDefault(predicate);
}
public IQuery IQueryable() where TEntity : class
{
return _context.Query();
}
public IQuery IQueryable(Expression> predicate) where TEntity : class
{
return _context.Query().Where(predicate);
}
public async Task> FindList(string strSql) where TEntity : class
{
return await _context.SqlQueryAsync(strSql);
}
public async Task> FindList(string strSql, DbParam[] dbParameter) where TEntity : class
{
return await _context.SqlQueryAsync(strSql, dbParameter);
}
public async Task> FindList(Pagination pagination) where TEntity : class, new()
{
var tempData = _context.Query();
pagination.records = tempData.Count();
tempData = tempData.OrderBy(pagination.sort);
tempData = tempData.TakePage(pagination.page, pagination.rows);
return tempData.ToList();
}
public async Task> FindList(Expression> predicate, Pagination pagination) where TEntity : class, new()
{
var tempData = _context.Query().Where(predicate);
pagination.records = tempData.Count();
tempData = tempData.OrderBy(pagination.sort);
tempData = tempData.TakePage(pagination.page, pagination.rows);
return tempData.ToList();
}
public async Task> OrderList(IQuery query, Pagination pagination)
{
var tempData = query;
pagination.records = tempData.Count();
tempData = tempData.OrderBy(pagination.sort);
tempData = tempData.TakePage(pagination.page, pagination.rows);
return tempData.ToList();
}
public async Task> OrderList(IQuery query, SoulPage pagination)
{
var tempData = query;
List filterSos = pagination.getFilterSos();
if (filterSos != null && filterSos.Count > 0)
{
tempData = tempData.GenerateFilter("u", filterSos);
}
pagination.count = tempData.Count();
if (pagination.order == "desc")
{
tempData = tempData.OrderBy(pagination.field + " " + pagination.order);
}
else
{
tempData = tempData.OrderBy(pagination.field);
}
tempData = tempData.TakePage(pagination.page, pagination.rows);
return tempData.ToList();
}
public async Task> CheckCacheList(string cacheKey, long old = 0) where TEntity : class
{
var cachedata = await CacheHelper.Get>(cacheKey);
if (cachedata == null || cachedata.Count() == 0)
{
cachedata = _context.Query().ToList();
await CacheHelper.Set(cacheKey, cachedata);
}
return cachedata;
}
public async Task CheckCache(string cacheKey, object keyValue, long old = 0) where TEntity : class
{
var cachedata = await CacheHelper.Get(cacheKey + keyValue);
if (cachedata == null)
{
cachedata = await _context.QueryByKeyAsync(keyValue);
if (cachedata != null)
{
await CacheHelper.Set(cacheKey + keyValue, cachedata);
}
}
return cachedata;
}
}
}