using Nirvana.Common;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace Nirvana.Data
{
///
/// 仓储实现类
///
public abstract class Repository : IRepository where T : class, new()
{
public object Add(T entity)
{
using (var dbClient = ReadDbContext.GetInstance())
{
var addresult = dbClient.Insertable(entity).ExecuteCommand();
return addresult > 0;
}
}
public bool AddList(List TModels)
{
using (var dbClient = ReadDbContext.GetInstance())
{
return dbClient.Insertable(TModels.ToArray()).ExecuteCommand() > 0;
}
}
public bool Delete(Expression> predicate)
{
using (var dbClient = ReadDbContext.GetInstance())
{
var deleteresult = dbClient.Deleteable(predicate).ExecuteCommand();
return deleteresult > 0;
}
}
public List GetList(Expression> predicate)
{
using (var dbClient = ReadDbContext.GetInstance())
{
return dbClient.Queryable().Where(predicate).ToList();
}
}
///
/// 获取列表
///
///
///
public async Task> GetListAsync(Expression> predicate)
{
using (var dbClient = ReadDbContext.GetInstance())
{
return await dbClient.Queryable().Where(predicate).ToListAsync();
}
}
public T GetModel(Expression> predicate)
{
using (var dbClient = ReadDbContext.GetInstance())
{
return dbClient.Queryable().Where(predicate).First();
}
}
public PageData GetPageList(int pageIndex, int pageSize, string strwhere, string strorder)
{
List query;
var rows = 0;
using (var dbClient = ReadDbContext.GetInstance())
{
query = dbClient.Queryable().Where(strwhere).OrderBy(strorder).ToPageList(pageIndex, pageSize, ref rows);
}
return new PageData
{
CurrentPage = pageIndex,
Items = query,
TotalNum = rows,
TotalPageCount = Convert.ToInt32(Math.Ceiling(rows * 1.0 / pageSize))
};
}
///
/// 分页查询
///
///
///
public async Task> GetPageListAsync(ParamQuery param)
{
RefAsync totalnum = 0;
using (var dbClient = ReadDbContext.GetInstance())
{
var temquery = dbClient.Queryable();
string sorts = string.Format("{0} {1}", param.sort, param.order);
var query = await temquery.OrderBy(sorts)
.ToPageListAsync(param.page, param.pagesize, totalnum);
return new ParamReturnData
{
page = param.page,
items = query,
totalnum = totalnum,
pagesize = param.pagesize
};
}
}
///
/// 获取分页结果
///
///
///
public async Task> GetPageListAsync(QueryParams param)
{
RefAsync totalnum = 0;
using (var dbClient = ReadDbContext.GetInstance())
{
var temquery = dbClient.Queryable();
if (param.queryParam != null && param.queryParam.Count > 0)
{
List conModels = new List();
param.queryParam.ForEach(x =>
{
if (!string.IsNullOrEmpty(x.Value))
{
conModels.Add(new ConditionalModel()
{
FieldName = x.Name,
ConditionalType = (ConditionalType)x.Type,
FieldValue = x.Value.Trim()
});
}
});
if (conModels.Count > 0)
{
temquery = temquery.Where(conModels);
}
}
string sorts = string.Format("{0} {1}", param.sort, param.order);
var query = await temquery.OrderBy(sorts)
.ToPageListAsync(param.offset, param.limit, totalnum);
return new PageParms
{
page = param.offset,
Items = query,
totalnum = totalnum,
limit = param.limit
};
}
}
public PageData GetPageList(int pageIndex, int pageSize, Expression> whereLambda, params MutilOrderExpress[] orderBy)
{
List query;
var rows = 0;
using (var dbClient = ReadDbContext.GetInstance())
{
var tempquery = dbClient.Queryable()
.Where(whereLambda);
orderBy.ToList().ForEach((item) =>
{
tempquery = item.OrderBy.HasValue
? tempquery.OrderBy(item.Expression, item.OrderBy.Value)
: tempquery.OrderBy(item.Expression);
});
query = tempquery
.ToPageList(pageIndex, pageSize, ref rows);
}
return new PageData
{
CurrentPage = pageIndex,
Items = query,
TotalNum = rows,
TotalPageCount = pageSize == 0 ? 0 : Convert.ToInt32(Math.Ceiling(rows * 1.0 / pageSize))
};
}
public bool IsExist(Expression> predicate)
{
using (var dbClient = ReadDbContext.GetInstance())
{
return dbClient.Queryable().Any(predicate);
}
}
public bool Update(T entity)
{
using (var dbClient = ReadDbContext.GetInstance())
{
var updateresult = dbClient
.Updateable(entity)
.ExecuteCommand();
return updateresult > 1;
}
}
public bool UpdateList(List TModels)
{
using (var dbClient = ReadDbContext.GetInstance())
{
return dbClient.Updateable(TModels).ExecuteCommand() > 0;
}
}
///
/// 生成随机的唯一ID
///
///
public long GenWorkId(long workerId = 1)
{
return new IdWorker(workerId).nextid();
}
///
/// 生成随机的唯一ID
///
///
///
public static long genunionid(long workerId = 1)
{
return new IdWorker(workerId).nextid();
}
List IRepository.GetListAsync(Expression> predicate)
{
throw new NotImplementedException();
}
}
}