1010 lines
34 KiB
C#
1010 lines
34 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Linq.Expressions;
|
|
using System.Threading.Tasks;
|
|
using APT.Infrastructure.Core;
|
|
namespace APT.Infrastructure.Api
|
|
{
|
|
|
|
/// <summary>
|
|
/// 基类接口
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
public abstract class APTApiControllerBase<T> : CommonApiController where T : MesEntityBase, new()
|
|
{
|
|
private IUnitOfWork _UnitOfWork;
|
|
/// <summary>
|
|
/// 单元操作
|
|
/// </summary>
|
|
protected IUnitOfWork UnitOfWork
|
|
{
|
|
get
|
|
{
|
|
if (_UnitOfWork == null)
|
|
{
|
|
_UnitOfWork = GetUnitOfWork<T>();
|
|
}
|
|
return _UnitOfWork;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 通过获取一条记录
|
|
/// </summary>
|
|
/// <param name="param">ID</param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<T> WitEntity(string id, params string[] param)
|
|
{
|
|
|
|
return SafeExecute(() =>
|
|
{
|
|
return base.GetEntity<T>(id, param);
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 通过获取一条记录
|
|
/// </summary>
|
|
/// <param name="param">ID</param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<T> WitEntity(Expression<Func<T, bool>> expression, params string[] param)
|
|
{
|
|
|
|
return SafeExecute(() =>
|
|
{
|
|
return base.GetEntity(expression, param);
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 通过获取一条记录
|
|
/// </summary>
|
|
/// <param name="param">ID</param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<T> WitEntity(Expression<Func<T, bool>> expression, BaseFilter filter, params string[] param)
|
|
{
|
|
|
|
return SafeExecute(() =>
|
|
{
|
|
return base.GetEntity(expression, filter, param);
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 排序后的所有记录
|
|
/// </summary>
|
|
/// <param name="orderBy">排序方式</param>
|
|
/// <param name="expression">条件</param>
|
|
/// <param name="fiter">分页参数</param>
|
|
/// <param name="param">导航属性</param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<IEnumerable<T>> WitActionOrderEntities(Action<IOrderable<T>> orderBy, Expression<Func<T, bool>> expression, BaseFilter fiter, params string[] param)
|
|
{
|
|
return SafeExecute<IEnumerable<T>>(() =>
|
|
{
|
|
return base.ActionOrderEntities(orderBy, expression, fiter, param);
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 获取所有记录
|
|
/// </summary>
|
|
/// <param name="expression"></param>
|
|
/// <param name="fiter"></param>
|
|
/// <param name="param"></param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<IEnumerable<T>> WitEntities(Expression<Func<T, bool>> expression, BaseFilter fiter, params string[] param)
|
|
{
|
|
return SafeExecute<IEnumerable<T>>(() =>
|
|
{
|
|
return base.GetEntities(expression, fiter, param);
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 分页查询
|
|
/// </summary>
|
|
/// <param name="expression">过滤条件</param>
|
|
/// <param name="fiter">过滤实体fiter</param>
|
|
/// <param name="param">导航属性</param>
|
|
/// <returns></returns>
|
|
protected PagedActionResult<T> WitPaged(Expression<Func<T, bool>> expression, BasePageFilter fiter, params string[] param)
|
|
{
|
|
return SafeGetPagedData<T>((result) =>
|
|
{
|
|
var page = base.GetPageEntities(expression, fiter, param);
|
|
result.Data = page.Data;
|
|
result.TotalCount = page.TotalCount;
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 排序后分页查询
|
|
/// </summary>
|
|
/// <param name="orderBy">排序条件</param>
|
|
/// <param name="expression">过滤条件</param>
|
|
/// <param name="fiter">过滤实体fiter</param>
|
|
/// <param name="param">导航属性</param>
|
|
/// <returns></returns>
|
|
protected async Task<PagedActionResult<T>> WitActionOrderPagedSync(Action<IOrderable<T>> orderBy, Expression<Func<T, bool>> expression, BasePageFilter fiter, params string[] param)
|
|
{
|
|
return SafeGetPagedData<T>(async (result) =>
|
|
{
|
|
var page = await base.ActionOrderPageEntitiesSync(orderBy, expression, fiter, param);
|
|
result.Data = page.Data;
|
|
result.TotalCount = page.TotalCount;
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 排序后分页查询
|
|
/// </summary>
|
|
/// <param name="orderBy">排序条件</param>
|
|
/// <param name="expression">过滤条件</param>
|
|
/// <param name="fiter">过滤实体fiter</param>
|
|
/// <param name="param">导航属性</param>
|
|
/// <returns></returns>
|
|
protected async Task<PagedActionResult<T>> WitOrderPagedSync(Expression<Func<T, bool>> expression, BasePageFilter fiter, params string[] param)
|
|
{
|
|
return SafeGetPagedData<T>(async (result) =>
|
|
{
|
|
var page = await base.GetOrderPageEntitiesSync(expression, fiter, param);
|
|
result.Data = page.Data;
|
|
result.TotalCount = page.TotalCount;
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 排序后分页查询
|
|
/// </summary>
|
|
/// <param name="orderBy">排序条件</param>
|
|
/// <param name="expression">过滤条件</param>
|
|
/// <param name="fiter">过滤实体fiter</param>
|
|
/// <param name="param">导航属性</param>
|
|
/// <returns></returns>
|
|
protected PagedActionResult<T> WitActionOrderPaged(Action<IOrderable<T>> orderBy, Expression<Func<T, bool>> expression, BasePageFilter fiter, params string[] param)
|
|
{
|
|
return SafeGetPagedData<T>((result) =>
|
|
{
|
|
var page = base.ActionOrderPageEntities(orderBy, expression, fiter, param);
|
|
result.Data = page.Data;
|
|
result.TotalCount = page.TotalCount;
|
|
});
|
|
}
|
|
|
|
protected PagedActionResult<T> WitActionOrderPaged<T>(Expression<Func<T, bool>> expression, BasePageFilter pageFilter, string[] selectField,
|
|
params string[] paths) where T : MesEntityBase, new()
|
|
{
|
|
return SafeGetPagedData<T>((result) =>
|
|
{
|
|
var page = base.GetOrderPageEntities(expression, pageFilter, selectField,paths);
|
|
result.Data = page.Data;
|
|
result.TotalCount = page.TotalCount;
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 排序后分页查询
|
|
/// </summary>
|
|
/// <param name="orderBy">排序条件</param>
|
|
/// <param name="expression">过滤条件</param>
|
|
/// <param name="fiter">过滤实体fiter</param>
|
|
/// <param name="param">导航属性</param>
|
|
/// <returns></returns>
|
|
protected PagedActionResult<T> WitOrderPaged(Expression<Func<T, bool>> expression, BasePageFilter fiter, params string[] param)
|
|
{
|
|
return SafeGetPagedData<T>((result) =>
|
|
{
|
|
var page = base.GetOrderPageEntities(expression, fiter, param);
|
|
result.Data = page.Data;
|
|
result.TotalCount = page.TotalCount;
|
|
});
|
|
}
|
|
|
|
protected JsonActionResult<IEnumerable<T>> WitOrderEntities(Expression<Func<T, bool>> expression, string sort, string order, BaseFilter fiter, params string[] param)
|
|
{
|
|
return SafeExecute<IEnumerable<T>>(() =>
|
|
{
|
|
return base.GetOrderEntities(expression, fiter, sort, order, param);
|
|
});
|
|
}
|
|
protected JsonActionResult<IEnumerable<T>> WitOrderEntities(Expression<Func<T, bool>> expression, BaseFilter fiter, params string[] param)
|
|
{
|
|
return SafeExecute<IEnumerable<T>>(() =>
|
|
{
|
|
return base.GetOrderEntities(expression, fiter, param);
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 提交事务
|
|
/// </summary>
|
|
/// <typeparam name="TResult">返回结果</typeparam>
|
|
/// <param name="execAction">数据处理</param>
|
|
/// <param name="comitAction">数据提交</param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<TResult> WitCommitTransaction<TResult>(Action execAction, Func<TResult> comitAction)
|
|
{
|
|
var result = new JsonActionResult<TResult>();
|
|
try
|
|
{
|
|
if (execAction != null)
|
|
{
|
|
execAction();
|
|
}
|
|
UnitOfWork.BeginTransaction();
|
|
result.Data = comitAction();
|
|
UnitOfWork.CommitTransaction();
|
|
}
|
|
catch (CodeException ex)
|
|
{
|
|
try
|
|
{
|
|
UnitOfWork.RollbackTransaction();
|
|
}
|
|
catch (Exception ex1)
|
|
{
|
|
LoggerManager.GetLogger().Info("rollback Error" + ex1.ToString());
|
|
}
|
|
|
|
result.IsSuccessful = false;
|
|
result.ErrorMessage = GetErrorMsg(ex);
|
|
result.Code = ex.Code;
|
|
LoggerManager.GetLogger().Error(result.ErrorMessage);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
try
|
|
{
|
|
UnitOfWork.RollbackTransaction();
|
|
}
|
|
catch (Exception ex1)
|
|
{
|
|
LoggerManager.GetLogger().Info("rollback Error" + ex1.ToString());
|
|
}
|
|
LoggerManager.GetLogger().Error(ex.ToString());
|
|
result.IsSuccessful = false;
|
|
result.ErrorMessage = ex.Message;
|
|
}
|
|
return result;
|
|
}
|
|
/// <summary>
|
|
/// 提交事务
|
|
/// </summary>
|
|
/// <param name="execAction">执行方法</param>
|
|
/// <param name="comitAction">提交方法</param>
|
|
protected void WitCommitTransaction(Action execAction, Action comitAction)
|
|
{
|
|
try
|
|
{
|
|
if (execAction != null)
|
|
{
|
|
execAction();
|
|
}
|
|
UnitOfWork.BeginTransaction();
|
|
comitAction();
|
|
UnitOfWork.CommitTransaction();
|
|
}
|
|
catch (CodeException ex)
|
|
{
|
|
try
|
|
{
|
|
UnitOfWork.RollbackTransaction();
|
|
}
|
|
catch (Exception ex1)
|
|
{
|
|
LoggerManager.GetLogger().Info("rollback Error" + ex1.ToString());
|
|
}
|
|
var exs = GetErrorMsg(ex);
|
|
LoggerManager.GetLogger().Info("rollback Error" + exs.ToString());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
try
|
|
{
|
|
UnitOfWork.RollbackTransaction();
|
|
}
|
|
catch (Exception ex1)
|
|
{
|
|
LoggerManager.GetLogger().Info("rollback Error" + ex1.ToString());
|
|
}
|
|
throw;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 提交事务
|
|
/// </summary>
|
|
/// <param name="comitAction">提交方法</param>
|
|
protected void WitCommitTransaction(Action comitAction)
|
|
{
|
|
try
|
|
{
|
|
|
|
UnitOfWork.BeginTransaction();
|
|
comitAction();
|
|
UnitOfWork.CommitTransaction();
|
|
}
|
|
catch (CodeException ex)
|
|
{
|
|
try
|
|
{
|
|
UnitOfWork.RollbackTransaction();
|
|
}
|
|
catch (Exception ex1)
|
|
{
|
|
LoggerManager.GetLogger().Info("rollback Error" + ex1.ToString());
|
|
}
|
|
var exs = GetErrorMsg(ex);
|
|
LoggerManager.GetLogger().Info("rollback Error" + exs.ToString());
|
|
}
|
|
catch
|
|
{
|
|
try
|
|
{
|
|
UnitOfWork.RollbackTransaction();
|
|
}
|
|
catch (Exception ex1)
|
|
{
|
|
LoggerManager.GetLogger().Info("rollback Error" + ex1.ToString());
|
|
}
|
|
throw;
|
|
}
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <typeparam name="TResult">返回类型</typeparam>
|
|
/// <param name="execAction">执行</param>
|
|
/// <param name="comitAction">返回</param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<TResult> WitSafeExecute<TResult>(Action execAction, Func<JsonActionResult<TResult>> comitAction)
|
|
{
|
|
var result = new JsonActionResult<TResult>();
|
|
try
|
|
{
|
|
if (execAction != null)
|
|
{
|
|
execAction();
|
|
}
|
|
result = comitAction();
|
|
}
|
|
catch (CodeException ex)
|
|
{
|
|
result.IsSuccessful = false;
|
|
result.ErrorMessage = GetErrorMsg(ex);
|
|
result.Code = ex.Code;
|
|
LoggerManager.GetLogger().Error(result.ErrorMessage);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LoggerManager.GetLogger().Error(ex.ToString());
|
|
result.IsSuccessful = false;
|
|
result.ErrorMessage = ex.Message;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
protected JsonActionResult<TResult> WitSafeExecute<TResult>(Action execAction, Func<TResult> comitAction)
|
|
{
|
|
var result = new JsonActionResult<TResult>();
|
|
try
|
|
{
|
|
if (execAction != null)
|
|
{
|
|
execAction();
|
|
}
|
|
result.Data = comitAction();
|
|
}
|
|
catch (CodeException ex)
|
|
{
|
|
result.IsSuccessful = false;
|
|
result.ErrorMessage = GetErrorMsg(ex);
|
|
result.Code = ex.Code;
|
|
LoggerManager.GetLogger().Error(result.ErrorMessage);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LoggerManager.GetLogger().Error(ex.ToString());
|
|
result.IsSuccessful = false;
|
|
result.ErrorMessage = ex.Message;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 统一提交
|
|
/// </summary>
|
|
/// <typeparam name="TResult"></typeparam>
|
|
/// <param name="execAction"></param>
|
|
/// <param name="comitAction"></param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<TResult> UnifiedCommit<TResult>(Action execAction, Func<TResult> comitAction)
|
|
{
|
|
var result = new JsonActionResult<TResult>();
|
|
try
|
|
{
|
|
if (execAction != null)
|
|
{
|
|
execAction();
|
|
UnitOfWork.SaveChanges();
|
|
}
|
|
result.Data = comitAction();
|
|
}
|
|
catch (CodeException ex)
|
|
{
|
|
result.IsSuccessful = false;
|
|
result.ErrorMessage = GetErrorMsg(ex);
|
|
result.Code = ex.Code;
|
|
LoggerManager.GetLogger().Error(result.ErrorMessage);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LoggerManager.GetLogger().Error(ex.ToString());
|
|
result.IsSuccessful = false;
|
|
result.ErrorMessage = ex.Message;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 统一提交
|
|
/// </summary>
|
|
/// <param name="comitAction">提交方法</param>
|
|
protected void UnifiedCommit(Action comitAction)
|
|
{
|
|
try
|
|
{
|
|
|
|
comitAction();
|
|
UnitOfWork.SaveChanges();
|
|
}
|
|
catch (CodeException ex)
|
|
{
|
|
var exs = GetErrorMsg(ex);
|
|
LoggerManager.GetLogger().Info("rollback Error" + exs.ToString());
|
|
throw;
|
|
}
|
|
catch (Exception ex1)
|
|
{
|
|
LoggerManager.GetLogger().Info("rollback Error" + ex1.ToString());
|
|
throw;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 基类接口
|
|
/// </summary>
|
|
/// <typeparam name="T">数据库类型</typeparam>
|
|
public abstract class APTApiController<T> : APTApiControllerBase<T> where T : MesEntityBase, new()
|
|
{
|
|
|
|
/// <summary>
|
|
/// 添加一条记录
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<bool> WitAdd(T entity)
|
|
{
|
|
return SafeExecute(() =>
|
|
{
|
|
return base.AddEntity(entity);
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 更新一条记录
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<bool> WitUpdate(T entity)
|
|
{
|
|
return SafeExecute(() =>
|
|
{
|
|
return base.UpdateEntity(entity);
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 按字段更新数据
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<bool> WitUpdate(T entity, params string[] updateField)
|
|
{
|
|
return SafeExecute(() =>
|
|
{
|
|
return base.UpdateEntity(entity,updateField);
|
|
});
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 根据条件删除
|
|
/// </summary>
|
|
/// <param name="expression">条件</param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<bool> WitDelete(Expression<Func<T, bool>> expression)
|
|
{
|
|
return SafeExecute(() =>
|
|
{
|
|
return base.DeleteEntity(expression);
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 删除实体
|
|
/// </summary>
|
|
/// <param name="entities">实体集合</param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<bool> WitDelete(IEnumerable<T> entities)
|
|
{
|
|
return SafeExecute(() =>
|
|
{
|
|
return base.DeleteEntity<T>(entities);
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 删除实体
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<bool> WitDelete(T entity)
|
|
{
|
|
return SafeExecute(() =>
|
|
{
|
|
return base.DeleteEntity(entity);
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 根据条件删除
|
|
/// </summary>
|
|
/// <param name="expression">条件</param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<bool> WitRealDelete(Expression<Func<T, bool>> expression)
|
|
{
|
|
return SafeExecute(() =>
|
|
{
|
|
return base.RealDeleteEntity(expression);
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 删除实体
|
|
/// </summary>
|
|
/// <param name="entities">实体集合</param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<bool> WitRealDelete(IEnumerable<T> entities)
|
|
{
|
|
return SafeExecute(() =>
|
|
{
|
|
return base.RealDeleteEntity<T>(entities);
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 删除实体
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<bool> WitRealDelete(T entity)
|
|
{
|
|
return SafeExecute(() =>
|
|
{
|
|
return base.RealDeleteEntity(entity);
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 批量新增数据
|
|
/// </summary>
|
|
/// <param name="entities"></param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<bool> WitBantchAdd(IEnumerable<T> entities)
|
|
{
|
|
return SafeExecute(() =>
|
|
{
|
|
return base.BantchAddEntity(entities);
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 批量更新数据(效率快)
|
|
/// </summary>
|
|
/// <param name="entities"></param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<bool> WitBantchUpdate(IEnumerable<T> entities)
|
|
{
|
|
return SafeExecute(() =>
|
|
{
|
|
return base.BantchUpdateEntity(entities);
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取条数
|
|
/// </summary>
|
|
/// <param name="expression"></param>
|
|
/// <param name="filter"></param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<int> WitCount(Expression<Func<T, bool>> expression, BaseFilter filter)
|
|
{
|
|
return SafeExecute(() =>
|
|
{
|
|
return base.GetCount(expression, filter);
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 通过ID删除
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<bool> WitDelete(string id)
|
|
{
|
|
return SafeExecute(() =>
|
|
{
|
|
return base.DeleteEntity<T>(id);
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 通过ID删除
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<bool> WitRealDelete(string id)
|
|
{
|
|
return SafeExecute(() =>
|
|
{
|
|
try
|
|
{
|
|
Guid temp = new Guid(id);
|
|
return base.RealDeleteEntity<T>(t => t.ID == temp);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (ex is DbUpdateException)
|
|
throw new Exception("该资料已被用,不允许执行此操作");
|
|
else if (ex is DomainException)
|
|
{
|
|
if (ex.Message.IndexOf("DbUpdateException", StringComparison.OrdinalIgnoreCase) > -1)
|
|
throw new Exception("该资料已被用,不允许执行此操作");
|
|
}
|
|
throw ex;
|
|
}
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 通过ID字符串删除
|
|
/// </summary>
|
|
/// <param name="ids"></param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<bool> WitBatchDelete(string ids)
|
|
{
|
|
var aryList = ids.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(i => new Guid(i)).ToList();
|
|
//var users = UserService.Query(i => i.NO == userName && i.PASSWORD == passWord).FirstOrDefault();
|
|
return SafeExecute(() =>
|
|
{
|
|
try
|
|
{
|
|
return base.BantchDeleteEntity<T>(aryList);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//if (ex is DbUpdateException)
|
|
// throw new Exception("存在资料已被用,不允许执行此操作");
|
|
//else if (ex is DomainException)
|
|
//{
|
|
// if (ex.Message.IndexOf("DbUpdateException", StringComparison.OrdinalIgnoreCase) > -1)
|
|
// throw new Exception("存在资料已被用,不允许执行此操作");
|
|
//}
|
|
throw ex;
|
|
}
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 通过ID数组删除
|
|
/// </summary>
|
|
/// <param name="ids"></param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<bool> WitBatchDelete(string[] ids)
|
|
{
|
|
//var users = UserService.Query(i => i.NO == userName && i.PASSWORD == passWord).FirstOrDefault();
|
|
return SafeExecute(() =>
|
|
{
|
|
var tmp = ids == null ? null : ids.Select(i => new Guid(i)).ToList();
|
|
return base.BantchDeleteEntity<T>(tmp);
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 通过ID字符串删除
|
|
/// </summary>
|
|
/// <param name="ids"></param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<bool> WitRealBatchDelete(string ids)
|
|
{
|
|
var aryList = ids.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(i => new Guid(i)).ToList();
|
|
//var users = UserService.Query(i => i.NO == userName && i.PASSWORD == passWord).FirstOrDefault();
|
|
return SafeExecute(() =>
|
|
{
|
|
return base.RealBantchDeleteEntity<T>(aryList);
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 通过ID数组删除
|
|
/// </summary>
|
|
/// <param name="ids"></param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<bool> WitRealBatchDelete(string[] ids)
|
|
{
|
|
//var users = UserService.Query(i => i.NO == userName && i.PASSWORD == passWord).FirstOrDefault();
|
|
return SafeExecute(() =>
|
|
{
|
|
var tmp = ids == null ? null : ids.Select(i => new Guid(i)).ToList();
|
|
return base.RealBantchDeleteEntity<T>(tmp);
|
|
});
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 树基类接口
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
public abstract class APTTreeApiController<T> : APTApiControllerBase<T> where T : TreeEntityBase<T>, new()
|
|
{
|
|
/// <summary>
|
|
/// 添加一条记录
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<bool> WitAdd(T entity)
|
|
{
|
|
return SafeExecute(() =>
|
|
{
|
|
return base.TreeAddEntity(entity);
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 更新一条记录
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<bool> WitUpdate(T entity)
|
|
{
|
|
return SafeExecute(() =>
|
|
{
|
|
return base.TreeUpdateEntity(entity);
|
|
});
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 根据条件删除
|
|
/// </summary>
|
|
/// <param name="expression">条件</param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<bool> WitDelete(Expression<Func<T, bool>> expression)
|
|
{
|
|
return SafeExecute(() =>
|
|
{
|
|
return base.TreeDeleteEntity(expression);
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 删除实体
|
|
/// </summary>
|
|
/// <param name="entities">实体集合</param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<bool> WitDelete(IEnumerable<T> entities)
|
|
{
|
|
return SafeExecute(() =>
|
|
{
|
|
return base.TreeDeleteEntity<T>(entities);
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 删除实体
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<bool> WitDelete(T entity)
|
|
{
|
|
return SafeExecute(() =>
|
|
{
|
|
return base.TreeDeleteEntity(entity);
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 根据条件删除
|
|
/// </summary>
|
|
/// <param name="expression">条件</param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<bool> WitRealDelete(Expression<Func<T, bool>> expression)
|
|
{
|
|
return SafeExecute(() =>
|
|
{
|
|
return base.TreeRealDeleteEntity(expression);
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 删除实体
|
|
/// </summary>
|
|
/// <param name="entities">实体集合</param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<bool> WitRealDelete(IEnumerable<T> entities)
|
|
{
|
|
return SafeExecute(() =>
|
|
{
|
|
return base.TreeRealDeleteEntity<T>(entities);
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 删除实体
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<bool> WitRealDelete(T entity)
|
|
{
|
|
return SafeExecute(() =>
|
|
{
|
|
return base.TreeRealDeleteEntity(entity);
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 批量新增数据
|
|
/// </summary>
|
|
/// <param name="entities"></param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<bool> WitBantchAdd(IEnumerable<T> entities)
|
|
{
|
|
return SafeExecute(() =>
|
|
{
|
|
return base.TreeBantchAddEntity(entities);
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 批量更新数据(效率快)
|
|
/// </summary>
|
|
/// <param name="entities"></param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<bool> WitBantchUpdate(IEnumerable<T> entities)
|
|
{
|
|
return SafeExecute(() =>
|
|
{
|
|
return base.TreeBantchUpdateEntity(entities);
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取条数
|
|
/// </summary>
|
|
/// <param name="expression"></param>
|
|
/// <param name="filter"></param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<int> WitCount(Expression<Func<T, bool>> expression, BaseFilter filter)
|
|
{
|
|
return SafeExecute(() =>
|
|
{
|
|
return base.GetCount(expression, filter);
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 通过ID删除
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<bool> WitDelete(string id)
|
|
{
|
|
return SafeExecute(() =>
|
|
{
|
|
return base.TreeDeleteEntity<T>(id);
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 通过ID删除
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<bool> WitRealDelete(string id)
|
|
{
|
|
return SafeExecute(() =>
|
|
{
|
|
try
|
|
{
|
|
return base.TreeRealDeleteEntity<T>(id);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (ex is DbUpdateException)
|
|
throw new Exception("该资料已被用,不允许执行此操作");
|
|
else if (ex is DomainException)
|
|
{
|
|
if (ex.Message.IndexOf("DbUpdateException", StringComparison.OrdinalIgnoreCase) > -1)
|
|
throw new Exception("该资料已被用,不允许执行此操作");
|
|
}
|
|
throw ex;
|
|
}
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 通过ID字符串删除
|
|
/// </summary>
|
|
/// <param name="ids"></param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<bool> WitBatchDelete(string ids)
|
|
{
|
|
return SafeExecute(() =>
|
|
{
|
|
try
|
|
{
|
|
var aryList = ids.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(t => new Guid(t)).ToList();
|
|
return base.TreeBantchDeleteEntity<T>(aryList);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (ex is DbUpdateException)
|
|
throw new Exception("存在资料已被用,不允许执行此操作");
|
|
else if (ex is DomainException)
|
|
{
|
|
if (ex.Message.IndexOf("DbUpdateException", StringComparison.OrdinalIgnoreCase) > -1)
|
|
throw new Exception("存在资料已被用,不允许执行此操作");
|
|
}
|
|
throw ex;
|
|
}
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 通过ID数组删除
|
|
/// </summary>
|
|
/// <param name="ids"></param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<bool> WitBatchDelete(string[] ids)
|
|
{
|
|
//var users = UserService.Query(i => i.NO == userName && i.PASSWORD == passWord).FirstOrDefault();
|
|
return SafeExecute(() =>
|
|
{
|
|
var aryList = ids == null ? null : ids.Select(t => new Guid(t)).ToList();
|
|
return base.TreeBantchDeleteEntity<T>(aryList);
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 通过ID字符串删除
|
|
/// </summary>
|
|
/// <param name="ids"></param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<bool> WitRealBatchDelete(string ids)
|
|
{
|
|
//var users = UserService.Query(i => i.NO == userName && i.PASSWORD == passWord).FirstOrDefault();
|
|
return SafeExecute(() =>
|
|
{
|
|
|
|
var aryList = string.IsNullOrEmpty(ids) ? null : ids.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(t => new Guid(t)).ToList();
|
|
return base.TreeRealBantchDeleteEntity<T>(aryList);
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 通过ID数组删除
|
|
/// </summary>
|
|
/// <param name="ids"></param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<bool> WitRealBatchDelete(string[] ids)
|
|
{
|
|
//var users = UserService.Query(i => i.NO == userName && i.PASSWORD == passWord).FirstOrDefault();
|
|
return SafeExecute(() =>
|
|
{
|
|
var aryList = ids == null ? null : ids.Select(t => new Guid(t)).ToList();
|
|
return base.TreeRealBantchDeleteEntity<T>(aryList);
|
|
});
|
|
}
|
|
/// <summary>
|
|
/// 获取树数据
|
|
/// </summary>
|
|
/// <param name="expression">lamble表达式</param>
|
|
/// <param name="filter">过滤实体</param>
|
|
/// <param name="isTracking"></param>
|
|
/// <param name="paths"></param>
|
|
/// <returns></returns>
|
|
protected JsonActionResult<IEnumerable<TreeNode<T>>> WitTreeOrderEntities(Expression<Func<T, bool>> expression, BaseFilter filter, bool isTracking = false,
|
|
params string[] paths)
|
|
{
|
|
return SafeExecute(() =>
|
|
{
|
|
return base.TreeOrderEntities<T>(expression, filter, isTracking, paths);
|
|
});
|
|
}
|
|
|
|
}
|
|
}
|