using Rhea.Common; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace Tiger.Business.SqlSugarHepler { /// /// /// public class BizSqlsugar { #region 私有变量 /// /// 最大插入数 /// private const int MaxCount = 1000; /// /// T100接口URL /// public static string t100Url { get; set; } = @"http://172.18.8.11/wstopprd/ws/r/awsp920"; #endregion 私有变量 /// /// 循环插入 /// /// /// /// public static int InsertDataTable(List input, DbClient db) where T : class, new() { int count = input.Count(); int result = 0; if (input.Count() > MaxCount) { int round = (count / MaxCount) + ((count % MaxCount) > 0 ? 1 : 0); List addLst = new List(); for (int i = 0; i < round; i++) { int start = i * MaxCount; int end = i == (round - 1) ? count : (start + MaxCount); for (int j = start; j < end; j++) { addLst.Add(input[j]); } result += db.Fastest().BulkCopy(addLst); addLst.Clear(); } } else { //result += db.Insertable(input).ExecuteCommand(); result += db.Fastest().BulkCopy(input); } return result; } /// /// 循环更新 /// /// /// /// /// public static int UpdateDataTable(List input, DbClient db) where T : class, new() { int res = 0; foreach (var item in input) { res += db.Updateable(item).ExecuteCommand(); } return res; } #region 事务 /// /// 同步事务 /// /// /// /// public static void CreateTran(Action action, DbClient _dbContext) { if (action == null) { throw new Exception("方法CreateTran的参数不能为空!"); } string bizErrorMsg = string.Empty; Exception error = null; var res = _dbContext.Ado.UseTran(() => { try { action.Invoke(_dbContext); _dbContext.Ado.CommitTran(); } catch (Exception ex) { _dbContext.Ado.RollbackTran(); if (ex is Exception) { bizErrorMsg = ex.Message; } else { error = ex; } } finally { _dbContext.Ado.Close(); } }); if (!string.IsNullOrWhiteSpace(bizErrorMsg)) { throw new Exception(bizErrorMsg); } if (error != null) { throw error; } if (!res.IsSuccess) { throw new Exception(res.ErrorMessage); } } /// /// 同步事务 /// /// /// /// public static void CreateTran(Action action, DbClient _dbContext) { if (action == null) { throw new Exception("方法CreateTran的参数不能为空!"); } string bizErrorMsg = string.Empty; Exception error = null; var res = _dbContext.Ado.UseTran(() => { try { action.Invoke(); _dbContext.Ado.CommitTran(); } catch (Exception ex) { _dbContext.Ado.RollbackTran(); if (ex is Exception) { bizErrorMsg = ex.Message; } else { error = ex; } } finally { _dbContext.Ado.Close(); } }); if (!string.IsNullOrWhiteSpace(bizErrorMsg)) { throw new Exception(bizErrorMsg); } if (error != null) { throw error; } if (!res.IsSuccess) { throw new Exception(res.ErrorMessage); } } /// /// 同步带返回类型的事务 /// /// /// /// /// /// public static T CreateTran(Func action, DbClient _dbContext) { if (action == null) { throw new Exception("方法CreateTran的参数不能为空!"); } var res = _dbContext.Ado.UseTran(() => { return action.Invoke(); }, (exception) => { throw exception; }); if (!res.IsSuccess) { throw new Exception(res.ErrorMessage); } return res.Data; } /// /// 异步带返回类型的事务 /// /// /// /// /// /// public static async Task CreateTranAsync(Func action, DbClient _dbContext) { if (action == null) { throw new Exception("方法CreateTran的参数不能为空!"); } var res = await _dbContext.Ado.UseTranAsync(() => { return Task.FromResult(action.Invoke()); }, (exception) => { throw exception; }); if (!res.IsSuccess) { throw new Exception(res.ErrorMessage); } return res.Data; } #endregion 事务 } }