using Rhea.Common;
|
using SqlSugar;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Threading.Tasks;
|
|
namespace Tiger.Business.SqlSugarHepler
|
{
|
/// <summary>
|
///
|
/// </summary>
|
public class BizSqlsugar
|
{
|
#region 私有变量
|
|
/// <summary>
|
/// 最大插入数
|
/// </summary>
|
private const int MaxCount = 1000;
|
|
/// <summary>
|
/// T100接口URL
|
/// </summary>
|
public static string t100Url { get; set; } = @"http://172.18.8.11/wstopprd/ws/r/awsp920";
|
|
#endregion 私有变量
|
|
/// <summary>
|
/// 循环插入
|
/// </summary>
|
/// <typeparam name="T"></typeparam>
|
/// <param name="input"></param>
|
/// <param name="db"></param>
|
public static int InsertDataTable<T>(List<T> 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<T> addLst = new List<T>();
|
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<T>().BulkCopy(addLst);
|
addLst.Clear();
|
}
|
}
|
else
|
{
|
//result += db.Insertable(input).ExecuteCommand();
|
result += db.Fastest<T>().BulkCopy(input);
|
}
|
return result;
|
}
|
|
/// <summary>
|
/// 循环更新
|
/// </summary>
|
/// <typeparam name="T"></typeparam>
|
/// <param name="input"></param>
|
/// <param name="db"></param>
|
/// <returns></returns>
|
public static int UpdateDataTable<T>(List<T> input, DbClient db) where T : class, new()
|
{
|
int res = 0;
|
foreach (var item in input)
|
{
|
res += db.Updateable(item).ExecuteCommand();
|
}
|
return res;
|
}
|
|
#region 事务
|
|
/// <summary>
|
/// 同步事务
|
/// </summary>
|
/// <param name="action"></param>
|
/// <exception cref="Exception"></exception>
|
/// <exception cref="BizException"></exception>
|
public static void CreateTran(Action<ISqlSugarClient> 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);
|
}
|
}
|
|
/// <summary>
|
/// 同步事务
|
/// </summary>
|
/// <param name="action"></param>
|
/// <param name="_dbContext"></param>
|
/// <exception cref="Exception"></exception>
|
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);
|
}
|
}
|
|
/// <summary>
|
/// 同步带返回类型的事务
|
/// </summary>
|
/// <typeparam name="T"></typeparam>
|
/// <param name="action"></param>
|
/// <param name="_dbContext"></param>
|
/// <returns></returns>
|
/// <exception cref="Exception"></exception>
|
public static T CreateTran<T>(Func<T> 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;
|
}
|
|
/// <summary>
|
/// 异步带返回类型的事务
|
/// </summary>
|
/// <typeparam name="T"></typeparam>
|
/// <param name="action"></param>
|
/// <param name="_dbContext"></param>
|
/// <returns></returns>
|
/// <exception cref="Exception"></exception>
|
public static async Task<T> CreateTranAsync<T>(Func<T> 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 事务
|
}
|
}
|