/// <summary> /// 根据ID主键修改实体有值的部分 /// </summary> /// <typeparam name="TDto"></typeparam> /// <typeparam name="TEntity"></typeparam> /// <param name="DtoModel"></param> /// <param name="ErrorMsg"></param> /// <param name="IgnorIntDefault">是否忽略int的默认值0</param> /// <param name="UpdateByKey">是否根据主键更新,默认是,否则按Dto模型配置的来更新</param> /// <returns></returns> public async Task <AttrResultModel> UpdateHasValueFieldAsync <TDto, TEntity>(TDto DtoModel, string ErrorMsg, string PrimaryKey = "", bool IgnorIntDefault = true, bool UpdateByKey = true) where TDto : AttrBaseModel where TEntity : AttrEntityBase, new() { var rm = AttrResultModel.Success(); return(await TryCatch(async() => { var entity = _mapper.Map <TEntity>(DtoModel); //校验名称是否重复 if (await Repo.CheckFieldRepeat(DtoModel, 1)) { // 执行更新 if (UpdateByKey) { rm.Result = await Repo.UpdateHasValueField(entity, PrimaryKey, IgnorIntDefault); } else { rm.Result = await Repo.UpdateHasValueFieldByDto(DtoModel, entity, IgnorIntDefault); } } return rm; }, ErrorMsg)); }
/// <summary> /// 异常通用处理方法 /// </summary> /// <param name="func"></param> /// <param name="ErrorMsg"></param> /// <param name="IsShowExceptionMsg">前端是否弹出异常捕获的信息,默认不显示</param> /// <returns></returns> private async Task <AttrResultModel> TryCatch(Func <Task <AttrResultModel> > func, string ErrorMsg, bool IsShowExceptionMsg = false) { var rm = AttrResultModel.Success(); try { rm = await func.Invoke(); } catch (AttrSqlException ex) { //Dto模型配置问题暴露配置错误原因 rm.Code = ResultCode.AttributeError; rm.Msg = ex.Message; } catch (Exception ex) { rm.Code = ResultCode.UnknownError; rm.Msg = string.IsNullOrEmpty(ErrorMsg) ? ex.Message : ErrorMsg; if (IsShowExceptionMsg) { rm.Msg = $"{ErrorMsg}:{ex.Message}:{ex.StackTrace}"; } } return(rm); }
/// <summary> /// 事务处理 /// </summary> /// <param name="func"></param> /// <returns></returns> public async Task <AttrResultModel> TransactionRun(Func <Task <AttrResultModel> > func) { var rm = AttrResultModel.Success(); return(await TryCatch(async() => { rm = await Repo.TransactionRun(func); return rm; }, "事务执行出错")); }
/// <summary> /// 根据Dto模型字段特性更新指定表 /// </summary> /// <typeparam name="TDto"></typeparam> /// <param name="dto"></param> /// <param name="ErrorMsg"></param> /// <param name="IgnorIntDefault"></param> /// <returns></returns> public async Task <AttrResultModel> UpdateAsync <TDto>(TDto dto, string ErrorMsg = "", bool IgnorIntDefault = true) where TDto : AttrBaseModel, new() { var rm = AttrResultModel.Success(); return(await TryCatch(async() => { // 执行更新 rm.Result = await Repo.UpdateDtoAttributeField(dto, IgnorIntDefault); return rm; }, ErrorMsg)); }
public async Task <AttrResultModel> UpdateHasValueFieldAsync <TEntity>(TEntity entity, string ErrorMsg, string PrimaryKey = "", bool IgnorIntDefault = true) where TEntity : AttrEntityBase, new() { var rm = AttrResultModel.Success(); return(await TryCatch(async() => { rm.Result = await Repo.UpdateHasValueField(entity, PrimaryKey, IgnorIntDefault); return rm; }, ErrorMsg)); }
public async Task <AttrResultModel> SoftDeleteAsync <TEntity>(TEntity entity, string softDeleteField, int value = 0, string PrimaryKey = "", bool IngnorIntDefault = true, string ErrorMsg = "") where TEntity : AttrEntityBase, new() { var rm = AttrResultModel.Success(); return(await TryCatch(async() => { rm.Result = await Repo.SoftDelete(entity, softDeleteField, value, PrimaryKey, IngnorIntDefault); return rm; }, ErrorMsg)); }
public async Task <AttrResultModel> DeleteAsync <TEntity>(TEntity entity, string[] condition, string ErrorMsg) where TEntity : AttrEntityBase, new() { var rm = AttrResultModel.Success(); return(await TryCatch(async() => { rm.Result = await Repo.Delete(entity, condition); return rm; }, ErrorMsg)); }
/// <summary> /// 执行指定的更新语句 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="entity"></param> /// <param name="sql"></param> /// <param name="ErrorMsg"></param> /// <returns></returns> public async Task <AttrResultModel> ExecUpdateSql <TEntity>(TEntity entity, string sql, string ErrorMsg) where TEntity : AttrEntityBase, new() { var rm = AttrResultModel.Success(); return(await TryCatch(async() => { rm.Result = await Repo.ExecUpdateSql(entity, sql); return rm; }, ErrorMsg)); }
/// <summary> /// 根据条件删除 /// </summary> /// <typeparam name="TDto"></typeparam> /// <typeparam name="TEntity"></typeparam> /// <typeparam name="TRepo"></typeparam> /// <param name="DtoModel"></param> /// <param name="ErrorMsg"></param> /// <param name="condition"></param> /// <returns></returns> public async Task <AttrResultModel> DeleteAsync <TDto, TEntity>(TDto DtoModel, string[] condition, string ErrorMsg) where TDto : AttrBaseModel where TEntity : AttrEntityBase, new() { var rm = AttrResultModel.Success(); return(await TryCatch(async() => { var entity = _mapper.Map <TEntity>(DtoModel); rm.Result = await Repo.Delete(entity, condition); return rm; }, ErrorMsg)); }
/// <summary> /// 执行指定的sql查询语句 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <typeparam name="TResult"></typeparam> /// <param name="sql"></param> /// <param name="ErrorMsg"></param> /// <returns></returns> public async Task <AttrResultModel> ExecQuerySql <TResult>(string sql, string ErrorMsg = "") where TResult : AttrBaseResult, new() { var rm = AttrResultModel.Success(); return(await TryCatch(async() => { var result = await Repo.ExecQuerySql <TResult>(sql); rm.Result = result; rm.Total = result.Total; return rm; }, ErrorMsg)); }
/// <summary> /// 通用查询,按照模型指定的where条件,查询指定单表的全部数据 /// </summary> /// <typeparam name="TPageSearch"></typeparam> /// <typeparam name="TEntity"></typeparam> /// <typeparam name="TRepo"></typeparam> /// <param name="pageSearch"></param> /// <param name="tableName"></param> /// <param name="ErrorMsg"></param> /// <returns></returns> public async Task <AttrResultModel> GetAll <TPageSearch, TEntity>(TPageSearch pageSearch, string tableName, string ErrorMsg = "") where TPageSearch : AttrPageSearch where TEntity : AttrEntityBase, new() { var rm = AttrResultModel.Success(); return(await TryCatch(async() => { var result = await Repo.GetAll <TPageSearch, TEntity>(pageSearch, tableName); rm.Result = result; rm.Total = result.Total; return rm; }, ErrorMsg)); }
/// <summary> /// 直接新增实体,不需要映射 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <typeparam name="TRepo"></typeparam> /// <param name="entity"></param> /// <returns></returns> public async Task <AttrResultModel> InsertEntityAsync <TEntity>(TEntity entity) where TEntity : AttrEntityBase, new() { var rm = AttrResultModel.Success(); return(await TryCatch(async() => { var rows = await Repo.Insert(entity); if (rows == 0) { rm.Code = ResultCode.UnknownError; rm.Msg = "创建数据失败!"; } return rm; }, "新增失败!")); }
public async Task <AttrResultModel> InsertReturnKey <TEntity>(TEntity Entity) where TEntity : AttrEntityBase, new() { var rm = AttrResultModel.Success(); return(await TryCatch(async() => { var rows = await Repo.InsertReturnKey(Entity); if (rows == 0) { rm.Code = ResultCode.UnknownError; rm.Msg = "创建数据失败!"; } rm.Result = rows; return rm; }, "创建实体失败!", true)); }
/// <summary> /// 通用查询,按照指定的Dto结果集模型获取多表查询的数据 /// </summary> /// <typeparam name="TPageSearch"></typeparam> /// <typeparam name="TResultDto"></typeparam> /// <param name="pageSearch"></param> /// <param name="ErrorMsg"></param> /// <param name="IngnorIntDefault"></param> /// <param name="whereSql"></param> /// <param name="usingCache"></param> /// <returns></returns> public async Task <AttrResultModel> GetSpecifyResultDto <TPageSearch, TResultDto>(TPageSearch pageSearch, string ErrorMsg = "", bool IngnorIntDefault = true, Func <string> whereSql = null, bool usingCache = true) where TPageSearch : AttrPageSearch where TResultDto : AttrBaseResult, new() { var rm = AttrResultModel.Success(); return(await TryCatch(async() => { var result = await Repo.GetSpecifyResultDto <TResultDto, TPageSearch>(pageSearch, IngnorIntDefault, whereSql, usingCache); rm.Result = result; rm.Total = result.Total; return rm; }, ErrorMsg)); }
/// <summary> /// 批量新增实体(非参数化,建议在大批量插入数据时使用) /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="Entities"></param> /// <param name="ErrorMsg"></param> /// <returns></returns> public async Task <AttrResultModel> NonPatameterBatchInsertAsync <TEntity>(TEntity[] Entities, string ErrorMsg) where TEntity : AttrEntityBase, new() { var rm = AttrResultModel.Success(); return(await TryCatch(async() => { if (Entities?.Length <= 0) { rm.Code = ResultCode.UnknownError; rm.Msg = "新增实体不能为空"; } else { // 执行添加 rm.Result = await Repo.NonPatameterBatchInsert(Entities); } return rm; }, ErrorMsg)); }
/// <summary> /// 批量新增(参数化) /// </summary> /// <param name="entity"></param> /// <returns></returns> internal async Task <int> BatchInsert <TEntity>(TEntity[] entities) where TEntity : AttrEntityBase, new() { int result = 0; if (entities?.Count() > 0) { await this.TryCatch(async() => { //默认事务新增 if (Tran == null) { await TransactionRun(async() => { foreach (var item in entities) { string sql = item.InsertEntity(); result += await DbExtend.ExecuteNonQuery(Context, sql.ToString(), item, Tran); } if (result == entities.Length) { return(AttrResultModel.Success()); } else { return(AttrResultModel.Error("批量新增失败!")); } }); } else { foreach (var item in entities) { string sql = item.InsertEntity(); result += await DbExtend.ExecuteNonQuery(Context, sql.ToString(), item, Tran); } } return(""); }); } return(result); }
internal async Task <AttrResultModel> TransactionRun(Func <Task <AttrResultModel> > func) { AttrResultModel result = AttrResultModel.Success(); if (Context.State != ConnectionState.Open) { Context.Open(); } using (Context) { using (Tran = Context.BeginTransaction()) { try { result = await func.Invoke(); if (result.Code != 0) { Tran.Rollback(); Context.Close(); throw new Exception($"{result.Msg}"); } else { Tran.Commit(); } } catch (Exception ex) { if (Context.State == ConnectionState.Open) { Tran.Rollback(); } result.Code = ResultCode.UnknownError; result.Msg = ex.Message; } } return(result); } }
public async Task <AttrResultModel> OrderQuery([FromBody] OrderPageSearch pageSearch) { //pageSearch.Index = 1; //pageSearch.Size = 10; //对于查询而言,可不创建实体,可以直接通过DebugQuerySql来获取最终生成的sql文本 var result = AttrResultModel.Success(); string sql = client.DebugQuerySql <OrderSearchResultDto, OrderPageSearch>(pageSearch); try { result = await client.GetSpecifyResultDto <OrderPageSearch, OrderSearchResultDto>(pageSearch); //若需要遍历查询结果 var list = ((AttrPageResult <OrderSearchResultDto>)result.Result).Rows.ToList(); } catch (Exception ex) { result.Code = ResultCode.UnknownError; result.Msg = ex.Message; } return(result); }
/// <summary> /// 通过Dto新增数据并返回自增主键 /// </summary> /// <typeparam name="TDto"></typeparam> /// <param name="model"></param> /// <returns></returns> public async Task <AttrResultModel> InsertDtoModelAsync <TDto>(TDto model, string ErrorMsg = "") where TDto : AttrBaseModel { var rm = AttrResultModel.Success(); return(await TryCatch(async() => { //校验名称是否重复 if (await Repo.CheckFieldRepeat(model, 0)) { // 执行添加 var key = await Repo.Insert(model); if (key <= 0) { rm.Code = ResultCode.UnknownError; rm.Msg = "创建数据失败!"; } rm.Result = key; } return rm; }, ErrorMsg, true)); }
/// <summary> /// 新增模型 /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="TEntity"></typeparam> /// <typeparam name="TRepo"></typeparam> /// <param name="DtoModel"></param> /// <param name="ErrorMsg">出错类型</param> /// <returns></returns> public async Task <AttrResultModel> InsertAsync <TDto, TEntity>(TDto DtoModel, string ErrorMsg) where TDto : AttrBaseModel where TEntity : AttrEntityBase, new() { var rm = AttrResultModel.Success(); return(await TryCatch(async() => { var entity = _mapper.Map <TEntity>(DtoModel); //校验名称是否重复 if (await Repo.CheckFieldRepeat(DtoModel, 0)) { // 执行添加 var rows = await Repo.Insert(entity); if (rows == 0) { rm.Code = ResultCode.UnknownError; rm.Msg = "创建数据失败!"; } } return rm; }, ErrorMsg, true)); }
/// <summary> /// 批量添加模型 /// </summary> /// <typeparam name="TDto"></typeparam> /// <typeparam name="TEntity"></typeparam> /// <param name="DtoModel"></param> /// <param name="ErrorMsg"></param> /// <returns></returns> public async Task <AttrResultModel> BatchInsertAsync <TDto, TEntity>(TDto[] DtoModel, string ErrorMsg) where TDto : AttrBaseModel where TEntity : AttrEntityBase, new() { var rm = AttrResultModel.Success(); return(await TryCatch(async() => { if (DtoModel?.Length <= 0) { rm.Code = ResultCode.UnknownError; rm.Msg = "新增实体不能为空"; } else { var entity = _mapper.Map <TEntity[]>(DtoModel); // 执行添加 rm.Result = await Repo.BatchInsert(entity); } return rm; }, ErrorMsg)); }