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 { /// /// 基类接口 /// /// public abstract class APTApiControllerBase : CommonApiController where T : MesEntityBase, new() { private IUnitOfWork _UnitOfWork; /// /// 单元操作 /// protected IUnitOfWork UnitOfWork { get { if (_UnitOfWork == null) { _UnitOfWork = GetUnitOfWork(); } return _UnitOfWork; } } /// /// 通过获取一条记录 /// /// ID /// protected JsonActionResult WitEntity(string id, params string[] param) { return SafeExecute(() => { return base.GetEntity(id, param); }); } /// /// 通过获取一条记录 /// /// ID /// protected JsonActionResult WitEntity(Expression> expression, params string[] param) { return SafeExecute(() => { return base.GetEntity(expression, param); }); } /// /// 通过获取一条记录 /// /// ID /// protected JsonActionResult WitEntity(Expression> expression, BaseFilter filter, params string[] param) { return SafeExecute(() => { return base.GetEntity(expression, filter, param); }); } /// /// 排序后的所有记录 /// /// 排序方式 /// 条件 /// 分页参数 /// 导航属性 /// protected JsonActionResult> WitActionOrderEntities(Action> orderBy, Expression> expression, BaseFilter fiter, params string[] param) { return SafeExecute>(() => { return base.ActionOrderEntities(orderBy, expression, fiter, param); }); } /// /// 获取所有记录 /// /// /// /// /// protected JsonActionResult> WitEntities(Expression> expression, BaseFilter fiter, params string[] param) { return SafeExecute>(() => { return base.GetEntities(expression, fiter, param); }); } /// /// 分页查询 /// /// 过滤条件 /// 过滤实体fiter /// 导航属性 /// protected PagedActionResult WitPaged(Expression> expression, BasePageFilter fiter, params string[] param) { return SafeGetPagedData((result) => { var page = base.GetPageEntities(expression, fiter, param); result.Data = page.Data; result.TotalCount = page.TotalCount; }); } /// /// 排序后分页查询 /// /// 排序条件 /// 过滤条件 /// 过滤实体fiter /// 导航属性 /// protected async Task> WitActionOrderPagedSync(Action> orderBy, Expression> expression, BasePageFilter fiter, params string[] param) { return SafeGetPagedData(async (result) => { var page = await base.ActionOrderPageEntitiesSync(orderBy, expression, fiter, param); result.Data = page.Data; result.TotalCount = page.TotalCount; }); } /// /// 排序后分页查询 /// /// 排序条件 /// 过滤条件 /// 过滤实体fiter /// 导航属性 /// protected async Task> WitOrderPagedSync(Expression> expression, BasePageFilter fiter, params string[] param) { return SafeGetPagedData(async (result) => { var page = await base.GetOrderPageEntitiesSync(expression, fiter, param); result.Data = page.Data; result.TotalCount = page.TotalCount; }); } /// /// 排序后分页查询 /// /// 排序条件 /// 过滤条件 /// 过滤实体fiter /// 导航属性 /// protected PagedActionResult WitActionOrderPaged(Action> orderBy, Expression> expression, BasePageFilter fiter, params string[] param) { return SafeGetPagedData((result) => { var page = base.ActionOrderPageEntities(orderBy, expression, fiter, param); result.Data = page.Data; result.TotalCount = page.TotalCount; }); } protected PagedActionResult WitActionOrderPaged(Expression> expression, BasePageFilter pageFilter, string[] selectField, params string[] paths) where T : MesEntityBase, new() { return SafeGetPagedData((result) => { var page = base.GetOrderPageEntities(expression, pageFilter, selectField,paths); result.Data = page.Data; result.TotalCount = page.TotalCount; }); } /// /// 排序后分页查询 /// /// 排序条件 /// 过滤条件 /// 过滤实体fiter /// 导航属性 /// protected PagedActionResult WitOrderPaged(Expression> expression, BasePageFilter fiter, params string[] param) { return SafeGetPagedData((result) => { var page = base.GetOrderPageEntities(expression, fiter, param); result.Data = page.Data; result.TotalCount = page.TotalCount; }); } protected JsonActionResult> WitOrderEntities(Expression> expression, string sort, string order, BaseFilter fiter, params string[] param) { return SafeExecute>(() => { return base.GetOrderEntities(expression, fiter, sort, order, param); }); } protected JsonActionResult> WitOrderEntities(Expression> expression, BaseFilter fiter, params string[] param) { return SafeExecute>(() => { return base.GetOrderEntities(expression, fiter, param); }); } /// /// 提交事务 /// /// 返回结果 /// 数据处理 /// 数据提交 /// protected JsonActionResult WitCommitTransaction(Action execAction, Func comitAction) { var result = new JsonActionResult(); 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; } /// /// 提交事务 /// /// 执行方法 /// 提交方法 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; } } /// /// 提交事务 /// /// 提交方法 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; } } /// /// /// /// 返回类型 /// 执行 /// 返回 /// protected JsonActionResult WitSafeExecute(Action execAction, Func> comitAction) { var result = new JsonActionResult(); 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 WitSafeExecute(Action execAction, Func comitAction) { var result = new JsonActionResult(); 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; } /// /// 统一提交 /// /// /// /// /// protected JsonActionResult UnifiedCommit(Action execAction, Func comitAction) { var result = new JsonActionResult(); 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; } /// /// 统一提交 /// /// 提交方法 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; } } } /// /// 基类接口 /// /// 数据库类型 public abstract class APTApiController : APTApiControllerBase where T : MesEntityBase, new() { /// /// 添加一条记录 /// /// /// protected JsonActionResult WitAdd(T entity) { return SafeExecute(() => { return base.AddEntity(entity); }); } /// /// 更新一条记录 /// /// /// protected JsonActionResult WitUpdate(T entity) { return SafeExecute(() => { return base.UpdateEntity(entity); }); } /// /// 按字段更新数据 /// /// /// protected JsonActionResult WitUpdate(T entity, params string[] updateField) { return SafeExecute(() => { return base.UpdateEntity(entity,updateField); }); } /// /// 根据条件删除 /// /// 条件 /// protected JsonActionResult WitDelete(Expression> expression) { return SafeExecute(() => { return base.DeleteEntity(expression); }); } /// /// 删除实体 /// /// 实体集合 /// protected JsonActionResult WitDelete(IEnumerable entities) { return SafeExecute(() => { return base.DeleteEntity(entities); }); } /// /// 删除实体 /// /// /// protected JsonActionResult WitDelete(T entity) { return SafeExecute(() => { return base.DeleteEntity(entity); }); } /// /// 根据条件删除 /// /// 条件 /// protected JsonActionResult WitRealDelete(Expression> expression) { return SafeExecute(() => { return base.RealDeleteEntity(expression); }); } /// /// 删除实体 /// /// 实体集合 /// protected JsonActionResult WitRealDelete(IEnumerable entities) { return SafeExecute(() => { return base.RealDeleteEntity(entities); }); } /// /// 删除实体 /// /// /// protected JsonActionResult WitRealDelete(T entity) { return SafeExecute(() => { return base.RealDeleteEntity(entity); }); } /// /// 批量新增数据 /// /// /// protected JsonActionResult WitBantchAdd(IEnumerable entities) { return SafeExecute(() => { return base.BantchAddEntity(entities); }); } /// /// 批量更新数据(效率快) /// /// /// protected JsonActionResult WitBantchUpdate(IEnumerable entities) { return SafeExecute(() => { return base.BantchUpdateEntity(entities); }); } /// /// 获取条数 /// /// /// /// protected JsonActionResult WitCount(Expression> expression, BaseFilter filter) { return SafeExecute(() => { return base.GetCount(expression, filter); }); } /// /// 通过ID删除 /// /// /// protected JsonActionResult WitDelete(string id) { return SafeExecute(() => { return base.DeleteEntity(id); }); } /// /// 通过ID删除 /// /// /// protected JsonActionResult WitRealDelete(string id) { return SafeExecute(() => { try { Guid temp = new Guid(id); return base.RealDeleteEntity(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; } }); } /// /// 通过ID字符串删除 /// /// /// protected JsonActionResult 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(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; } }); } /// /// 通过ID数组删除 /// /// /// protected JsonActionResult 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(tmp); }); } /// /// 通过ID字符串删除 /// /// /// protected JsonActionResult 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(aryList); }); } /// /// 通过ID数组删除 /// /// /// protected JsonActionResult 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(tmp); }); } } /// /// 树基类接口 /// /// public abstract class APTTreeApiController : APTApiControllerBase where T : TreeEntityBase, new() { /// /// 添加一条记录 /// /// /// protected JsonActionResult WitAdd(T entity) { return SafeExecute(() => { return base.TreeAddEntity(entity); }); } /// /// 更新一条记录 /// /// /// protected JsonActionResult WitUpdate(T entity) { return SafeExecute(() => { return base.TreeUpdateEntity(entity); }); } /// /// 根据条件删除 /// /// 条件 /// protected JsonActionResult WitDelete(Expression> expression) { return SafeExecute(() => { return base.TreeDeleteEntity(expression); }); } /// /// 删除实体 /// /// 实体集合 /// protected JsonActionResult WitDelete(IEnumerable entities) { return SafeExecute(() => { return base.TreeDeleteEntity(entities); }); } /// /// 删除实体 /// /// /// protected JsonActionResult WitDelete(T entity) { return SafeExecute(() => { return base.TreeDeleteEntity(entity); }); } /// /// 根据条件删除 /// /// 条件 /// protected JsonActionResult WitRealDelete(Expression> expression) { return SafeExecute(() => { return base.TreeRealDeleteEntity(expression); }); } /// /// 删除实体 /// /// 实体集合 /// protected JsonActionResult WitRealDelete(IEnumerable entities) { return SafeExecute(() => { return base.TreeRealDeleteEntity(entities); }); } /// /// 删除实体 /// /// /// protected JsonActionResult WitRealDelete(T entity) { return SafeExecute(() => { return base.TreeRealDeleteEntity(entity); }); } /// /// 批量新增数据 /// /// /// protected JsonActionResult WitBantchAdd(IEnumerable entities) { return SafeExecute(() => { return base.TreeBantchAddEntity(entities); }); } /// /// 批量更新数据(效率快) /// /// /// protected JsonActionResult WitBantchUpdate(IEnumerable entities) { return SafeExecute(() => { return base.TreeBantchUpdateEntity(entities); }); } /// /// 获取条数 /// /// /// /// protected JsonActionResult WitCount(Expression> expression, BaseFilter filter) { return SafeExecute(() => { return base.GetCount(expression, filter); }); } /// /// 通过ID删除 /// /// /// protected JsonActionResult WitDelete(string id) { return SafeExecute(() => { return base.TreeDeleteEntity(id); }); } /// /// 通过ID删除 /// /// /// protected JsonActionResult WitRealDelete(string id) { return SafeExecute(() => { try { return base.TreeRealDeleteEntity(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; } }); } /// /// 通过ID字符串删除 /// /// /// protected JsonActionResult WitBatchDelete(string ids) { return SafeExecute(() => { try { var aryList = ids.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(t => new Guid(t)).ToList(); return base.TreeBantchDeleteEntity(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; } }); } /// /// 通过ID数组删除 /// /// /// protected JsonActionResult 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(aryList); }); } /// /// 通过ID字符串删除 /// /// /// protected JsonActionResult 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(aryList); }); } /// /// 通过ID数组删除 /// /// /// protected JsonActionResult 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(aryList); }); } /// /// 获取树数据 /// /// lamble表达式 /// 过滤实体 /// /// /// protected JsonActionResult>> WitTreeOrderEntities(Expression> expression, BaseFilter filter, bool isTracking = false, params string[] paths) { return SafeExecute(() => { return base.TreeOrderEntities(expression, filter, isTracking, paths); }); } } }