You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1005 lines
34 KiB
1005 lines
34 KiB
using System.Data;
|
|
using System.Linq.Expressions;
|
|
using System.Reflection;
|
|
using AgileObjects.AgileMapper;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using SqlSugar;
|
|
using Tiobon.Core.Common;
|
|
using Tiobon.Core.Common.DB.Dapper;
|
|
using Tiobon.Core.Common.DB.Dapper.Extensions;
|
|
using Tiobon.Core.Common.Enums;
|
|
using Tiobon.Core.Common.Extensions;
|
|
using Tiobon.Core.Common.Helper;
|
|
using Tiobon.Core.Common.UserManager;
|
|
using Tiobon.Core.IRepository.Base;
|
|
using Tiobon.Core.IServices.BASE;
|
|
using Tiobon.Core.Model;
|
|
|
|
namespace Tiobon.Core.Services.BASE;
|
|
|
|
public class BaseServices<TEntity> : IBaseServices<TEntity> where TEntity : class, new()
|
|
{
|
|
public BaseServices(IBaseRepository<TEntity> BaseDal = null)
|
|
{
|
|
this.BaseDal = BaseDal;
|
|
}
|
|
|
|
//public IBaseRepository<TEntity> baseDal = new BaseRepository<TEntity>();
|
|
public IBaseRepository<TEntity> BaseDal { get; set; } //通过在子类的构造函数中注入,这里是基类,不用构造函数
|
|
|
|
public ISqlSugarClient Db => BaseDal.Db;
|
|
|
|
public async Task<TEntity> QueryById(object objId)
|
|
{
|
|
return await BaseDal.QueryById(objId);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据ID查询一条数据
|
|
/// </summary>
|
|
/// <param name="objId">id(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件</param>
|
|
/// <param name="blnUseCache">是否使用缓存</param>
|
|
/// <returns>数据实体</returns>
|
|
public async Task<TEntity> QueryById(object objId, bool blnUseCache = false)
|
|
{
|
|
return await BaseDal.QueryById(objId, blnUseCache);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据ID查询数据
|
|
/// </summary>
|
|
/// <param name="lstIds">id列表(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件</param>
|
|
/// <returns>数据实体列表</returns>
|
|
public async Task<List<TEntity>> QueryByIDs(object[] lstIds)
|
|
{
|
|
return await BaseDal.QueryByIDs(lstIds);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 写入实体数据
|
|
/// </summary>
|
|
/// <param name="entity">博文实体类</param>
|
|
/// <returns></returns>
|
|
public async Task<long> Add(TEntity entity)
|
|
{
|
|
return await BaseDal.Add(entity);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量插入实体(速度快)
|
|
/// </summary>
|
|
/// <param name="listEntity">实体集合</param>
|
|
/// <returns>影响行数</returns>
|
|
public async Task<List<long>> Add(List<TEntity> listEntity)
|
|
{
|
|
return await BaseDal.Add(listEntity);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新实体数据
|
|
/// </summary>
|
|
/// <param name="entity">博文实体类</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> Update(TEntity entity)
|
|
{
|
|
return await BaseDal.Update(entity);
|
|
}
|
|
/// <summary>
|
|
/// 更新实体数据
|
|
/// </summary>
|
|
/// <param name="entity">博文实体类</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> Update(List<TEntity> entity)
|
|
{
|
|
return await BaseDal.Update(entity);
|
|
}
|
|
|
|
public async Task<bool> Update(TEntity entity, string where)
|
|
{
|
|
return await BaseDal.Update(entity, where);
|
|
}
|
|
|
|
public async Task<bool> Update(object operateAnonymousObjects)
|
|
{
|
|
return await BaseDal.Update(operateAnonymousObjects);
|
|
}
|
|
|
|
public async Task<bool> Update(TEntity entity, List<string> lstColumns = null, List<string> lstIgnoreColumns = null, string where = "")
|
|
{
|
|
return await BaseDal.Update(entity, lstColumns, lstIgnoreColumns, where);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 根据实体删除一条数据
|
|
/// </summary>
|
|
/// <param name="entity">博文实体类</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> Delete(TEntity entity)
|
|
{
|
|
return await BaseDal.Delete(entity);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除指定ID的数据
|
|
/// </summary>
|
|
/// <param name="id">主键ID</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> DeleteById(object id)
|
|
{
|
|
return await BaseDal.DeleteById(id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除指定ID集合的数据(批量删除)
|
|
/// </summary>
|
|
/// <param name="ids">主键ID集合</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> DeleteByIds(object[] ids)
|
|
{
|
|
return await BaseDal.DeleteByIds(ids);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 查询所有数据
|
|
/// </summary>
|
|
/// <returns>数据列表</returns>
|
|
public async Task<List<TEntity>> Query()
|
|
{
|
|
return await BaseDal.Query();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询数据列表
|
|
/// </summary>
|
|
/// <param name="where">条件</param>
|
|
/// <returns>数据列表</returns>
|
|
public async Task<List<TEntity>> Query(string where)
|
|
{
|
|
return await BaseDal.Query(where);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询数据列表
|
|
/// </summary>
|
|
/// <param name="whereExpression">whereExpression</param>
|
|
/// <returns>数据列表</returns>
|
|
public async Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression)
|
|
{
|
|
return await BaseDal.Query(whereExpression);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 按照特定列查询数据列表
|
|
/// </summary>
|
|
/// <typeparam name="TResult"></typeparam>
|
|
/// <param name="expression"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<TResult>> Query<TResult>(Expression<Func<TEntity, TResult>> expression)
|
|
{
|
|
return await BaseDal.Query(expression);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 按照特定列查询数据列表带条件排序
|
|
/// </summary>
|
|
/// <typeparam name="TResult"></typeparam>
|
|
/// <param name="whereExpression">过滤条件</param>
|
|
/// <param name="expression">查询实体条件</param>
|
|
/// <param name="orderByFileds">排序条件</param>
|
|
/// <returns></returns>
|
|
public async Task<List<TResult>> Query<TResult>(Expression<Func<TEntity, TResult>> expression, Expression<Func<TEntity, bool>> whereExpression, string orderByFileds)
|
|
{
|
|
return await BaseDal.Query(expression, whereExpression, orderByFileds);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询一个列表
|
|
/// </summary>
|
|
/// <param name="whereExpression">条件表达式</param>
|
|
/// <param name="strOrderByFileds">排序字段,如name asc,age desc</param>
|
|
/// <returns>数据列表</returns>
|
|
public async Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true)
|
|
{
|
|
return await BaseDal.Query(whereExpression, orderByExpression, isAsc);
|
|
}
|
|
|
|
public async Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, string orderByFileds)
|
|
{
|
|
return await BaseDal.Query(whereExpression, orderByFileds);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询一个列表
|
|
/// </summary>
|
|
/// <param name="where">条件</param>
|
|
/// <param name="orderByFileds">排序字段,如name asc,age desc</param>
|
|
/// <returns>数据列表</returns>
|
|
public async Task<List<TEntity>> Query(string where, string orderByFileds)
|
|
{
|
|
return await BaseDal.Query(where, orderByFileds);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据sql语句查询
|
|
/// </summary>
|
|
/// <param name="sql">完整的sql语句</param>
|
|
/// <param name="parameters">参数</param>
|
|
/// <returns>泛型集合</returns>
|
|
public async Task<List<TEntity>> QuerySql(string sql, SugarParameter[] parameters = null)
|
|
{
|
|
return await BaseDal.QuerySql(sql, parameters);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据sql语句查询
|
|
/// </summary>
|
|
/// <param name="sql">完整的sql语句</param>
|
|
/// <param name="parameters">参数</param>
|
|
/// <returns>DataTable</returns>
|
|
public async Task<DataTable> QueryTable(string sql, SugarParameter[] parameters = null)
|
|
{
|
|
return await BaseDal.QueryTable(sql, parameters);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询前N条数据
|
|
/// </summary>
|
|
/// <param name="whereExpression">条件表达式</param>
|
|
/// <param name="top">前N条</param>
|
|
/// <param name="orderByFileds">排序字段,如name asc,age desc</param>
|
|
/// <returns>数据列表</returns>
|
|
public async Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, int top, string orderByFileds)
|
|
{
|
|
return await BaseDal.Query(whereExpression, top, orderByFileds);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询前N条数据
|
|
/// </summary>
|
|
/// <param name="where">条件</param>
|
|
/// <param name="top">前N条</param>
|
|
/// <param name="orderByFileds">排序字段,如name asc,age desc</param>
|
|
/// <returns>数据列表</returns>
|
|
public async Task<List<TEntity>> Query(string where, int top, string orderByFileds)
|
|
{
|
|
return await BaseDal.Query(where, top, orderByFileds);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 分页查询
|
|
/// </summary>
|
|
/// <param name="whereExpression">条件表达式</param>
|
|
/// <param name="pageIndex">页码(下标0)</param>
|
|
/// <param name="pageSize">页大小</param>
|
|
/// <param name="orderByFileds">排序字段,如name asc,age desc</param>
|
|
/// <returns>数据列表</returns>
|
|
public async Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, int pageIndex, int pageSize, string orderByFileds)
|
|
{
|
|
return await BaseDal.Query(whereExpression, pageIndex, pageSize, orderByFileds);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 分页查询
|
|
/// </summary>
|
|
/// <param name="where">条件</param>
|
|
/// <param name="pageIndex">页码(下标0)</param>
|
|
/// <param name="pageSize">页大小</param>
|
|
/// <param name="orderByFileds">排序字段,如name asc,age desc</param>
|
|
/// <returns>数据列表</returns>
|
|
public async Task<List<TEntity>> Query(string where, int pageIndex, int pageSize, string orderByFileds)
|
|
{
|
|
return await BaseDal.Query(where, pageIndex, pageSize, orderByFileds);
|
|
}
|
|
|
|
public async Task<PageModel<TEntity>> QueryPage(Expression<Func<TEntity, bool>> whereExpression, int pageIndex = 1, int pageSize = 20, string orderByFileds = null)
|
|
{
|
|
return await BaseDal.QueryPage(whereExpression, pageIndex, pageSize, orderByFileds);
|
|
}
|
|
public async Task<List<TResult>> QueryMuch<T, T2, T3, TResult>(Expression<Func<T, T2, T3, object[]>> joinExpression, Expression<Func<T, T2, T3, TResult>> selectExpression, Expression<Func<T, T2, T3, bool>> whereLambda = null) where T : class, new()
|
|
{
|
|
return await BaseDal.QueryMuch(joinExpression, selectExpression, whereLambda);
|
|
}
|
|
|
|
public async Task<PageModel<TEntity>> QueryPage(PaginationModel pagination)
|
|
{
|
|
var express = DynamicLinqFactory.CreateLambda<TEntity>(pagination.Conditions);
|
|
return await QueryPage(express, pagination.PageIndex, pagination.PageSize, pagination.OrderByFileds);
|
|
}
|
|
|
|
#region 分表
|
|
|
|
public async Task<List<long>> AddSplit(TEntity entity)
|
|
{
|
|
return await BaseDal.AddSplit(entity);
|
|
}
|
|
|
|
public async Task<bool> UpdateSplit(TEntity entity, DateTime dateTime)
|
|
{
|
|
return await BaseDal.UpdateSplit(entity, dateTime);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据实体删除一条数据
|
|
/// </summary>
|
|
/// <param name="entity">博文实体类</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> DeleteSplit(TEntity entity, DateTime dateTime)
|
|
{
|
|
return await BaseDal.DeleteSplit(entity, dateTime);
|
|
}
|
|
|
|
public async Task<TEntity> QueryByIdSplit(object objId)
|
|
{
|
|
return await BaseDal.QueryByIdSplit(objId);
|
|
}
|
|
|
|
public async Task<PageModel<TEntity>> QueryPageSplit(Expression<Func<TEntity, bool>> whereExpression, DateTime beginTime, DateTime endTime, int pageIndex = 1, int pageSize = 20, string orderByFields = null)
|
|
{
|
|
return await BaseDal.QueryPageSplit(whereExpression, beginTime, endTime, pageIndex, pageSize, orderByFields);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 增删改查基础服务
|
|
/// </summary>
|
|
/// <typeparam name="TEntity"></typeparam>
|
|
/// <typeparam name="TEntityDto"></typeparam>
|
|
/// <typeparam name="TInsertDto"></typeparam>
|
|
/// <typeparam name="TEditDto"></typeparam>
|
|
public class BaseServices<TEntity, TEntityDto, TInsertDto, TEditDto> : IBaseServices<TEntity, TEntityDto, TInsertDto, TEditDto> where TEntity : class, new()
|
|
{
|
|
public BaseServices(IBaseRepository<TEntity> BaseDal = null)
|
|
{
|
|
this.BaseDal = BaseDal;
|
|
}
|
|
|
|
//public IBaseRepository<TEntity> baseDal = new BaseRepository<TEntity>();
|
|
public IBaseRepository<TEntity> BaseDal { get; set; } //通过在子类的构造函数中注入,这里是基类,不用构造函数
|
|
|
|
public ISqlSugarClient Db => BaseDal.Db;
|
|
|
|
/// <summary>
|
|
/// 根据ID查询实体数据是否存在
|
|
/// </summary>
|
|
/// <param name="objId"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> AnyAsync(object objId)
|
|
{
|
|
var data = await BaseDal.AnyAsync(objId);
|
|
return data;
|
|
}
|
|
|
|
public async Task<TEntityDto> QueryById(object objId)
|
|
{
|
|
var data = await BaseDal.QueryById(objId);
|
|
return Mapper.Map(data).ToANew<TEntityDto>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据ID查询一条数据
|
|
/// </summary>
|
|
/// <param name="objId">id(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件</param>
|
|
/// <param name="blnUseCache">是否使用缓存</param>
|
|
/// <returns>数据实体</returns>
|
|
public async Task<TEntityDto> QueryById(object objId, bool blnUseCache = false)
|
|
{
|
|
var data = await BaseDal.QueryById(objId, blnUseCache);
|
|
return Mapper.Map(data).ToANew<TEntityDto>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据ID查询数据
|
|
/// </summary>
|
|
/// <param name="lstIds">id列表(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件</param>
|
|
/// <returns>数据实体列表</returns>
|
|
public async Task<List<TEntityDto>> QueryByIDs(object[] lstIds)
|
|
{
|
|
var data = await BaseDal.QueryByIDs(lstIds);
|
|
return Mapper.Map(data).ToANew<List<TEntityDto>>();
|
|
}
|
|
|
|
#region 新增
|
|
|
|
/// <summary>
|
|
/// 写入实体数据
|
|
/// </summary>
|
|
/// <param name="entity">实体类</param>
|
|
/// <returns></returns>
|
|
public async Task<long> Add(TInsertDto entity)
|
|
{
|
|
HttpRequest request = UserContext.Context.Request;
|
|
var api = request.Path.ObjToString().TrimEnd('/').ToLower();
|
|
var ip = GetUserIp(UserContext.Context);
|
|
|
|
var entity1 = Mapper.Map(entity).ToANew<TEntity>();
|
|
BasePoco ent = entity1 as BasePoco;
|
|
|
|
ent.CreateIP = ip;
|
|
ent.CreateProg = api;
|
|
|
|
#region 检查是否存在相同值
|
|
CheckOnly(entity1);
|
|
#endregion
|
|
|
|
return await BaseDal.Add(entity1);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量插入实体(速度快)
|
|
/// </summary>
|
|
/// <param name="listEntity">实体集合</param>
|
|
/// <returns>影响行数</returns>
|
|
public async Task<List<long>> Add(List<TInsertDto> listEntity)
|
|
{
|
|
var userId = UserContext.Current.User_Id;
|
|
HttpRequest request = UserContext.Context.Request;
|
|
var api = request.Path.ObjToString().TrimEnd('/').ToLower();
|
|
var ip = GetUserIp(UserContext.Context);
|
|
var list = Mapper.Map(listEntity).ToANew<List<TEntity>>();
|
|
list.ForEach(entity =>
|
|
{
|
|
BasePoco ent = entity as BasePoco;
|
|
ent.CreateIP = ip;
|
|
ent.CreateProg = api;
|
|
|
|
|
|
#region 检查是否存在相同值
|
|
CheckOnly(entity);
|
|
#endregion
|
|
});
|
|
return await BaseDal.Add(list);
|
|
}
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// 更新实体数据
|
|
/// </summary>
|
|
/// <param name="entity">博文实体类</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> Update(long Id, TEditDto editModel)
|
|
{
|
|
HttpRequest request = UserContext.Context.Request;
|
|
var api = request.Path.ObjToString().TrimEnd('/').ToLower();
|
|
var ip = GetUserIp(UserContext.Context);
|
|
|
|
if (editModel == null || !await BaseDal.AnyAsync(Id))
|
|
return false;
|
|
|
|
var entity = await BaseDal.QueryById(Id);
|
|
ConvertTEditDto2TEntity(editModel, entity);
|
|
BasePoco ent = entity as BasePoco;
|
|
ent.UpdateIP = ip;
|
|
ent.UpdateProg = api;
|
|
CheckOnly(entity, Id);
|
|
return await BaseDal.Update(entity);
|
|
}
|
|
public async Task<bool> Update(Dictionary<long, TEditDto> editModels)
|
|
{
|
|
HttpRequest request = UserContext.Context.Request;
|
|
var api = request.Path.ObjToString().TrimEnd('/').ToLower();
|
|
var ip = GetUserIp(UserContext.Context);
|
|
|
|
List<TEntity> entities = new List<TEntity>();
|
|
foreach (var keyValuePairs in editModels)
|
|
{
|
|
if (keyValuePairs.Value == null || !BaseDal.Any(keyValuePairs.Key))
|
|
continue;
|
|
|
|
var entity = await BaseDal.QueryById(keyValuePairs.Key);
|
|
|
|
ConvertTEditDto2TEntity(keyValuePairs.Value, entity);
|
|
BasePoco ent = entity as BasePoco;
|
|
ent.UpdateIP = ip;
|
|
ent.UpdateProg = api;
|
|
CheckOnly(entity, keyValuePairs.Key);
|
|
entities.Add(entity);
|
|
}
|
|
|
|
return await BaseDal.Update(entities);
|
|
}
|
|
/// <summary>
|
|
/// 更新实体数据
|
|
/// </summary>
|
|
/// <param name="entity">博文实体类</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> Update(List<TEntity> listEntity)
|
|
{
|
|
HttpRequest request = UserContext.Context.Request;
|
|
var api = request.Path.ObjToString().TrimEnd('/').ToLower();
|
|
var ip = GetUserIp(UserContext.Context);
|
|
listEntity.ForEach(entity =>
|
|
{
|
|
BasePoco ent = entity as BasePoco;
|
|
ent.UpdateIP = ip;
|
|
ent.UpdateProg = api;
|
|
});
|
|
return await BaseDal.Update(listEntity);
|
|
}
|
|
|
|
public async Task<bool> Update(TEntity entity, string where)
|
|
{
|
|
HttpRequest request = UserContext.Context.Request;
|
|
var api = request.Path.ObjToString().TrimEnd('/').ToLower();
|
|
var ip = GetUserIp(UserContext.Context);
|
|
BasePoco ent = entity as BasePoco;
|
|
ent.UpdateIP = ip;
|
|
ent.UpdateProg = api;
|
|
return await BaseDal.Update(entity, where);
|
|
}
|
|
|
|
public async Task<bool> Update(object operateAnonymousObjects)
|
|
{
|
|
return await BaseDal.Update(operateAnonymousObjects);
|
|
}
|
|
|
|
public async Task<bool> Update(TEntity entity, List<string> lstColumns = null, List<string> lstIgnoreColumns = null, string where = "")
|
|
{
|
|
return await BaseDal.Update(entity, lstColumns, lstIgnoreColumns, where);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 根据实体删除一条数据
|
|
/// </summary>
|
|
/// <param name="entity">博文实体类</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> Delete(TEntity entity)
|
|
{
|
|
return await BaseDal.Delete(entity);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除指定ID的数据
|
|
/// </summary>
|
|
/// <param name="id">主键ID</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> DeleteById(object id)
|
|
{
|
|
return await BaseDal.DeleteById(id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除指定ID的数据
|
|
/// </summary>
|
|
/// <param name="id">主键ID</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> DeleteById1(object id)
|
|
{
|
|
var entity = await BaseDal.QueryById(id);
|
|
BasePoco ent = entity as BasePoco;
|
|
ent.IsEnable = 0;
|
|
return await BaseDal.Update(entity);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除指定ID集合的数据(批量删除)
|
|
/// </summary>
|
|
/// <param name="ids">主键ID集合</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> DeleteByIds(object[] ids)
|
|
{
|
|
return await BaseDal.DeleteByIds(ids);
|
|
}
|
|
/// <summary>
|
|
/// 删除指定ID集合的数据(批量删除)
|
|
/// </summary>
|
|
/// <param name="ids">主键ID集合</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> DeleteByIds1(long[] ids)
|
|
{
|
|
HttpRequest request = UserContext.Context.Request;
|
|
var api = request.Path.ObjToString().TrimEnd('/').ToLower();
|
|
var ip = GetUserIp(UserContext.Context);
|
|
|
|
List<TEntity> entities = new List<TEntity>();
|
|
foreach (var id in ids)
|
|
{
|
|
if (id == null || !BaseDal.Any(id))
|
|
continue;
|
|
|
|
var entity = await BaseDal.QueryById(id);
|
|
|
|
BasePoco ent = entity as BasePoco;
|
|
ent.UpdateIP = ip;
|
|
ent.UpdateProg = api;
|
|
ent.IsEnable = 0;
|
|
entities.Add(entity);
|
|
}
|
|
|
|
return await BaseDal.Update(entities);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据表达式,删除实体
|
|
/// </summary>
|
|
/// <param name="whereExpression">表达式</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> Delete(Expression<Func<TEntity, bool>> whereExpression)
|
|
{
|
|
return await BaseDal.Delete(whereExpression);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询所有数据
|
|
/// </summary>
|
|
/// <returns>数据列表</returns>
|
|
public async Task<List<TEntity>> Query()
|
|
{
|
|
return await BaseDal.Query();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询数据列表
|
|
/// </summary>
|
|
/// <param name="where">条件</param>
|
|
/// <returns>数据列表</returns>
|
|
public async Task<List<TEntity>> Query(string where)
|
|
{
|
|
return await BaseDal.Query(where);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询数据列表
|
|
/// </summary>
|
|
/// <param name="whereExpression">whereExpression</param>
|
|
/// <returns>数据列表</returns>
|
|
public async Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression)
|
|
{
|
|
return await BaseDal.Query(whereExpression);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 按照特定列查询数据列表
|
|
/// </summary>
|
|
/// <typeparam name="TResult"></typeparam>
|
|
/// <param name="expression"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<TResult>> Query<TResult>(Expression<Func<TEntity, TResult>> expression)
|
|
{
|
|
return await BaseDal.Query(expression);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 按照特定列查询数据列表带条件排序
|
|
/// </summary>
|
|
/// <typeparam name="TResult"></typeparam>
|
|
/// <param name="whereExpression">过滤条件</param>
|
|
/// <param name="expression">查询实体条件</param>
|
|
/// <param name="orderByFileds">排序条件</param>
|
|
/// <returns></returns>
|
|
public async Task<List<TResult>> Query<TResult>(Expression<Func<TEntity, TResult>> expression, Expression<Func<TEntity, bool>> whereExpression, string orderByFileds)
|
|
{
|
|
return await BaseDal.Query(expression, whereExpression, orderByFileds);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询一个列表
|
|
/// </summary>
|
|
/// <param name="whereExpression">条件表达式</param>
|
|
/// <param name="strOrderByFileds">排序字段,如name asc,age desc</param>
|
|
/// <returns>数据列表</returns>
|
|
public async Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true)
|
|
{
|
|
return await BaseDal.Query(whereExpression, orderByExpression, isAsc);
|
|
}
|
|
|
|
public async Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, string orderByFileds)
|
|
{
|
|
return await BaseDal.Query(whereExpression, orderByFileds);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询一个列表
|
|
/// </summary>
|
|
/// <param name="where">条件</param>
|
|
/// <param name="orderByFileds">排序字段,如name asc,age desc</param>
|
|
/// <returns>数据列表</returns>
|
|
public async Task<List<TEntity>> Query(string where, string orderByFileds)
|
|
{
|
|
return await BaseDal.Query(where, orderByFileds);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据sql语句查询
|
|
/// </summary>
|
|
/// <param name="sql">完整的sql语句</param>
|
|
/// <param name="parameters">参数</param>
|
|
/// <returns>泛型集合</returns>
|
|
public async Task<List<TEntity>> QuerySql(string sql, SugarParameter[] parameters = null)
|
|
{
|
|
return await BaseDal.QuerySql(sql, parameters);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据sql语句查询
|
|
/// </summary>
|
|
/// <param name="sql">完整的sql语句</param>
|
|
/// <param name="parameters">参数</param>
|
|
/// <returns>DataTable</returns>
|
|
public async Task<DataTable> QueryTable(string sql, SugarParameter[] parameters = null)
|
|
{
|
|
return await BaseDal.QueryTable(sql, parameters);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询前N条数据
|
|
/// </summary>
|
|
/// <param name="whereExpression">条件表达式</param>
|
|
/// <param name="top">前N条</param>
|
|
/// <param name="orderByFileds">排序字段,如name asc,age desc</param>
|
|
/// <returns>数据列表</returns>
|
|
public async Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, int top, string orderByFileds)
|
|
{
|
|
return await BaseDal.Query(whereExpression, top, orderByFileds);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询前N条数据
|
|
/// </summary>
|
|
/// <param name="where">条件</param>
|
|
/// <param name="top">前N条</param>
|
|
/// <param name="orderByFileds">排序字段,如name asc,age desc</param>
|
|
/// <returns>数据列表</returns>
|
|
public async Task<List<TEntity>> Query(string where, int top, string orderByFileds)
|
|
{
|
|
return await BaseDal.Query(where, top, orderByFileds);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 分页查询
|
|
/// </summary>
|
|
/// <param name="whereExpression">条件表达式</param>
|
|
/// <param name="pageIndex">页码(下标0)</param>
|
|
/// <param name="pageSize">页大小</param>
|
|
/// <param name="orderByFileds">排序字段,如name asc,age desc</param>
|
|
/// <returns>数据列表</returns>
|
|
public async Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, int pageIndex, int pageSize, string orderByFileds)
|
|
{
|
|
return await BaseDal.Query(whereExpression, pageIndex, pageSize, orderByFileds);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 分页查询
|
|
/// </summary>
|
|
/// <param name="where">条件</param>
|
|
/// <param name="pageIndex">页码(下标0)</param>
|
|
/// <param name="pageSize">页大小</param>
|
|
/// <param name="orderByFileds">排序字段,如name asc,age desc</param>
|
|
/// <returns>数据列表</returns>
|
|
public async Task<List<TEntity>> Query(string where, int pageIndex, int pageSize, string orderByFileds)
|
|
{
|
|
return await BaseDal.Query(where, pageIndex, pageSize, orderByFileds);
|
|
}
|
|
|
|
public async Task<PageModel<TEntity>> QueryPage(Expression<Func<TEntity, bool>> whereExpression, int pageIndex = 1, int pageSize = 20, string orderByFileds = null)
|
|
{
|
|
return await BaseDal.QueryPage(whereExpression, pageIndex, pageSize, orderByFileds);
|
|
}
|
|
|
|
public async Task<ServicePageResult<TEntityDto>> QueryFilterPage(QueryBody body)
|
|
{
|
|
var data = await BaseDal.QueryFilterPage(body);
|
|
|
|
return new ServicePageResult<TEntityDto>(body.pageNum, data.result.DT_TablePageInfoT1.TotalCount, body.pageSize, Mapper.Map(data.result.DT_TableDataT1).ToANew<List<TEntityDto>>());
|
|
|
|
}
|
|
|
|
public async Task<List<TResult>> QueryMuch<T, T2, T3, TResult>(Expression<Func<T, T2, T3, object[]>> joinExpression, Expression<Func<T, T2, T3, TResult>> selectExpression, Expression<Func<T, T2, T3, bool>> whereLambda = null) where T : class, new()
|
|
{
|
|
return await BaseDal.QueryMuch(joinExpression, selectExpression, whereLambda);
|
|
}
|
|
|
|
public async Task<PageModel<TEntity>> QueryPage(PaginationModel pagination)
|
|
{
|
|
var express = DynamicLinqFactory.CreateLambda<TEntity>(pagination.Conditions);
|
|
return await QueryPage(express, pagination.PageIndex, pagination.PageSize, pagination.OrderByFileds);
|
|
}
|
|
|
|
#region 分表
|
|
|
|
public async Task<List<long>> AddSplit(TEntity entity)
|
|
{
|
|
return await BaseDal.AddSplit(entity);
|
|
}
|
|
|
|
public async Task<bool> UpdateSplit(TEntity entity, DateTime dateTime)
|
|
{
|
|
return await BaseDal.UpdateSplit(entity, dateTime);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据实体删除一条数据
|
|
/// </summary>
|
|
/// <param name="entity">博文实体类</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> DeleteSplit(TEntity entity, DateTime dateTime)
|
|
{
|
|
return await BaseDal.DeleteSplit(entity, dateTime);
|
|
}
|
|
|
|
public async Task<TEntity> QueryByIdSplit(object objId)
|
|
{
|
|
return await BaseDal.QueryByIdSplit(objId);
|
|
}
|
|
|
|
public async Task<PageModel<TEntity>> QueryPageSplit(Expression<Func<TEntity, bool>> whereExpression, DateTime beginTime, DateTime endTime, int pageIndex = 1, int pageSize = 20, string orderByFields = null)
|
|
{
|
|
return await BaseDal.QueryPageSplit(whereExpression, beginTime, endTime, pageIndex, pageSize, orderByFields);
|
|
}
|
|
|
|
#endregion
|
|
|
|
public static string GetUserIp(HttpContext context)
|
|
{
|
|
string realIP = null;
|
|
string forwarded = null;
|
|
string remoteIpAddress = context.Connection.RemoteIpAddress.ToString();
|
|
if (context.Request.Headers.ContainsKey("X-Real-IP"))
|
|
{
|
|
realIP = context.Request.Headers["X-Real-IP"].ToString();
|
|
if (realIP != remoteIpAddress)
|
|
{
|
|
remoteIpAddress = realIP;
|
|
}
|
|
}
|
|
if (context.Request.Headers.ContainsKey("X-Forwarded-For"))
|
|
{
|
|
forwarded = context.Request.Headers["X-Forwarded-For"].ToString();
|
|
if (forwarded != remoteIpAddress)
|
|
{
|
|
remoteIpAddress = forwarded;
|
|
}
|
|
}
|
|
remoteIpAddress = remoteIpAddress.Replace("::ffff:", null);
|
|
return remoteIpAddress;
|
|
}
|
|
|
|
|
|
#region 辅助方法
|
|
/// <summary>
|
|
/// 转换TEditDto2TEntity
|
|
/// </summary>
|
|
/// <param name="pTargetObjSrc"></param>
|
|
/// <param name="pTargetObjDest"></param>
|
|
/// <returns></returns>
|
|
protected void ConvertTEditDto2TEntity(TEditDto source, TEntity dest)
|
|
{
|
|
foreach (PropertyInfo mItem in typeof(TEditDto).GetProperties())
|
|
{
|
|
if (dest.HasField(mItem.Name))
|
|
dest.SetValueForField(mItem.Name, mItem.GetValue(source, null));
|
|
}
|
|
//dest.SetValueForField(DbConsts.ColunmName_LastModificationTime, DateTimeHelper.Now());
|
|
//if (_currentUserId != default)
|
|
//{
|
|
// //dest.SetValueForField(DbConsts.ColunmName_LastModifierId, _currentUserId);
|
|
// dest.SetValueForField(DbConsts.ColunmName_LastModifier, _currentUserName);
|
|
//}
|
|
|
|
//if (_currentTenantId != null)
|
|
//{
|
|
// dest.SetValueForField(DbConsts.ColunmName_TenantId, _currentTenantId);
|
|
//}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取根据ID查询的条件
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
protected QueryFilter QueryFilterById(Guid id)
|
|
{
|
|
return new QueryFilter
|
|
{
|
|
PageIndex = 1,
|
|
PageSize = 1,
|
|
Sorting = string.Empty,
|
|
Conditions = string.Empty
|
|
};
|
|
}
|
|
|
|
#region 检查表中是否已经存在相同代码的数据
|
|
|
|
public static void CheckOnly(TEntity entity, long? id = null)
|
|
{
|
|
Type entityType = typeof(TEntity);
|
|
var tableName = entityType.GetEntityTableName();
|
|
|
|
var names = new List<string>();
|
|
var values = new List<object>();
|
|
var descriptions = new List<string>();
|
|
entity.GetOnlyList(out names, out values, out descriptions);
|
|
for (int i = 0; i < names.Count; i++)
|
|
{
|
|
CheckCodeExist(tableName, names[i], values[i], id != null ? ModifyType.Edit : ModifyType.Add, descriptions[i]);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 检查表中是否已经存在相同代码的数据
|
|
/// </summary>
|
|
/// <param name="tableName">表名</param>
|
|
/// <param name="fieldName">字段名</param>
|
|
/// <param name="fieldValue">字段值</param>
|
|
/// <param name="modifyType">ModifyType.Add,ModifyType.Edit</param>
|
|
/// <param name="rowid">ModifyType.Edit时修改记录的ROW_ID值</param>
|
|
/// <param name="promptName">判断栏位的提示名称</param>
|
|
public static void CheckCodeExist(string tableName, string fieldName, object fieldValue, ModifyType modifyType, string promptName, long? rowid = null)
|
|
{
|
|
try
|
|
{
|
|
CheckCodeExist(tableName, fieldName, fieldValue, modifyType, rowid, promptName, null);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检查表中是否已经存在相同代码的数据
|
|
/// </summary>
|
|
/// <param name="tableName">表名</param>
|
|
/// <param name="fieldName">字段名</param>
|
|
/// <param name="fieldValue">字段值</param>
|
|
/// <param name="whereCondition">条件</param>
|
|
/// <param name="modifyType">ModifyType.Add,ModifyType.Edit</param>
|
|
/// <param name="rowid">ModifyType.Edit时修改记录的ROW_ID值</param>
|
|
/// <param name="promptName">判断栏位的提示名称</param>
|
|
/// <param name="whereCondition">Where后的条件,如:IS_ALCON='Y'</param>
|
|
public static bool CheckCodeExist(string tableName, string fieldName, object fieldValue, ModifyType modifyType, long? rowid, string promptName, string whereCondition)
|
|
{
|
|
try
|
|
{
|
|
bool result = false;
|
|
if (modifyType == ModifyType.Add)
|
|
{
|
|
string sql = string.Empty;
|
|
sql = "SELECT COUNT(*) FROM " + tableName + " WHERE " + fieldName + "='" + fieldValue + "' AND IsEnable=1";
|
|
|
|
if (!string.IsNullOrEmpty(whereCondition))
|
|
sql += " AND " + whereCondition;
|
|
|
|
int count = Convert.ToInt32(DbAccess.ExecuteScalar(sql));
|
|
if (count > 0)
|
|
{
|
|
result = true;
|
|
throw new Exception(string.Format("{0}【{1}】已经存在!", promptName, fieldValue));
|
|
}
|
|
else
|
|
result = false;
|
|
|
|
}
|
|
else if (modifyType == ModifyType.Edit)
|
|
{
|
|
string sql = string.Empty;
|
|
sql = "SELECT COUNT(*) FROM " + tableName + " WHERE " + fieldName + "='" + fieldValue + "' AND IsEnable=1 AND ID!='" + rowid.Value + "'";
|
|
|
|
if (!string.IsNullOrEmpty(whereCondition))
|
|
sql += " AND " + whereCondition;
|
|
|
|
int count = Convert.ToInt32(DbAccess.ExecuteScalar(sql));
|
|
if (count > 0)
|
|
{
|
|
result = true;
|
|
throw new Exception(string.Format("{0}【{1}】已经存在!", promptName, fieldValue));
|
|
}
|
|
else
|
|
result = false;
|
|
}
|
|
return result;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#endregion
|
|
} |