using System;
|
using System.Collections.Generic;
|
using System.Linq.Expressions;
|
using HH.WCS.Mobox3.RiDong.models;
|
using HH.WCS.Mobox3.RiDong.util;
|
using SqlSugar;
|
|
namespace HH.WCS.Mobox3.RiDong.generalMethod;
|
|
/// <summary>
|
/// 数据库连接帮助类
|
/// </summary>
|
public static class AdoSqlMethod<T> where T : class, new()
|
{
|
|
/// <summary>
|
/// 查询一条数据
|
/// </summary>
|
/// <returns></returns>
|
public static T QueryFirst()
|
{
|
var db = new SqlHelper<object>().GetInstance();
|
|
// 返回数据
|
return db.Queryable<T>().First();
|
}
|
|
/// <summary>
|
/// 根据条件查询一条数据
|
/// </summary>
|
/// <param name="condition">条件</param>
|
/// <returns></returns>
|
public static T QueryFirst(Expression<Func<T, bool>> condition)
|
{
|
var db = new SqlHelper<object>().GetInstance();
|
|
// 返回数据
|
return db.Queryable<T>().First(condition);
|
}
|
|
/// <summary>
|
/// 根据条件查询多条数据
|
/// </summary>
|
/// <param name="condition"></param>
|
public static List<T> QueryList(Expression<Func<T, bool>> condition)
|
{
|
var db = new SqlHelper<object>().GetInstance();
|
|
// 返回数据
|
return db.Queryable<T>().Where(condition).ToList();
|
}
|
|
/// <summary>
|
/// 根据条件查询对应数据数量
|
/// </summary>
|
/// <param name="condition">条件</param>
|
/// <returns></returns>
|
public static int QueryCount(Expression<Func<T, bool>> condition)
|
{
|
var db = new SqlHelper<object>().GetInstance();
|
|
// 返回数据
|
return db.Queryable<T>().Count(condition);
|
}
|
|
/// <summary>
|
/// 根据条件修改一条数据(事务)
|
/// </summary>
|
/// <param name="sqlSugarClient">数据库上下文连接</param>
|
/// <param name="model">需要进行修改的对象</param>
|
/// <param name="condition">修改条件</param>
|
/// <returns></returns>
|
public static bool UpdateFirstTran(SqlSugarClient sqlSugarClient, T model, Expression<Func<T, object>> condition)
|
{
|
// 修改数据
|
var executeCommand = sqlSugarClient.Updateable(model).UpdateColumns(condition).ExecuteCommand();
|
|
if (executeCommand > 0)
|
{
|
return true;
|
}
|
else
|
{
|
return false;
|
}
|
}
|
|
/// <summary>
|
/// 根据条件修改一条数据
|
/// </summary>
|
/// <param name="model">需要进行修改的对象</param>
|
/// <param name="condition">修改条件</param>
|
/// <returns></returns>
|
public static bool UpdateFirst(T model, Expression<Func<T, object>> condition)
|
{
|
var sqlSugarClient = new SqlHelper<object>().GetInstance();
|
|
// 修改数据
|
var executeCommand = sqlSugarClient.Updateable(model).UpdateColumns(condition).ExecuteCommand();
|
|
if (executeCommand > 0)
|
{
|
return true;
|
}
|
else
|
{
|
return false;
|
}
|
}
|
|
/// <summary>
|
/// 根据条件修改一条数据的状态(sql语句)
|
/// </summary>
|
/// <param name="model">需要进行修改的对象</param>
|
/// <param name="sql">修改条件</param>
|
/// <returns></returns>
|
public static bool UpdateFirstOutBoundStart(T model, string sql)
|
{
|
var sqlSugarClient = new SqlHelper<object>().GetInstance();
|
|
// 修改数据
|
var executeCommand = sqlSugarClient.Ado.ExecuteCommand(sql, model);
|
|
if (executeCommand > 0)
|
{
|
return true;
|
}
|
else
|
{
|
return false;
|
}
|
}
|
|
/// <summary>
|
/// 根据条件修改多条数据(事务)
|
/// </summary>
|
/// <param name="sqlSugarClient">数据库上下文连接</param>
|
/// <param name="models"></param>
|
/// <param name="condition"></param>
|
/// <returns></returns>
|
public static int UpdateListTran(SqlSugarClient sqlSugarClient, List<T> models,
|
Expression<Func<T, object>> condition)
|
{
|
// 修改数据
|
return sqlSugarClient.Updateable(models).UpdateColumns(condition).ExecuteCommand();
|
}
|
|
/// <summary>
|
/// 根据条件修改多条数据
|
/// </summary>
|
/// <param name="models"></param>
|
/// <param name="condition"></param>
|
/// <returns></returns>
|
public static int UpdateList(List<T> models, Expression<Func<T, object>> condition)
|
{
|
var sqlSugarClient = new SqlHelper<object>().GetInstance();
|
// 修改数据
|
return sqlSugarClient.Updateable(models).UpdateColumns(condition).ExecuteCommand();
|
}
|
|
/// <summary>
|
/// 新增一条数据(事务)
|
/// </summary>
|
/// <param name="sqlSugarClient">数据库上下文连接</param>
|
/// <param name="model">需要新增的数据</param>
|
/// <returns></returns>
|
public static bool AddFirstTran(SqlSugarClient sqlSugarClient, T model)
|
{
|
var executeCommand = sqlSugarClient.Insertable(model).ExecuteCommand();
|
|
if (executeCommand > 0)
|
{
|
return true;
|
}
|
else
|
{
|
return false;
|
}
|
}
|
|
|
/// <summary>
|
/// 新增多条出库单数据(事务)
|
/// </summary>
|
/// <param name="sqlSugarClient">数据库上下文连接</param>
|
/// <param name="models">需要新增的数据</param>
|
/// <returns></returns>
|
public static bool AddOutboundOrderAndDetailsListTran(SqlSugarClient sqlSugarClient, List<OutboundOrder> models)
|
{
|
int executeCommand = 0;
|
foreach (var outboundOrder in models)
|
{
|
executeCommand = sqlSugarClient.Insertable(outboundOrder).ExecuteCommand();
|
sqlSugarClient.Insertable(outboundOrder.Details).ExecuteCommand();
|
}
|
|
if (executeCommand > 0)
|
{
|
return true;
|
}
|
else
|
{
|
return false;
|
}
|
}
|
|
/// <summary>
|
/// 新增一条数据(事务)
|
/// </summary>
|
/// <param name="model">需要新增的数据</param>
|
/// <returns></returns>
|
public static bool AddFirst(T model)
|
{
|
var sqlSugarClient = new SqlHelper<object>().GetInstance();
|
|
var executeCommand = sqlSugarClient.Insertable(model).ExecuteCommand();
|
|
if (executeCommand > 0)
|
{
|
return true;
|
}
|
else
|
{
|
return false;
|
}
|
}
|
|
|
/// <summary>
|
/// 新增多条数据(事务)
|
/// </summary>
|
/// <param name="sqlSugarClient">数据库上下文连接</param>
|
/// <param name="models">需要新增的数据集合</param>
|
/// <returns></returns>
|
public static int AddListTran(SqlSugarClient sqlSugarClient, List<T> models)
|
{
|
return sqlSugarClient.Insertable(models).ExecuteCommand();
|
}
|
|
/// <summary>
|
/// 新增多条数据
|
/// </summary>
|
/// <param name="models">需要新增的数据集合</param>
|
/// <returns></returns>
|
public static int AddList(List<T> models)
|
{
|
var sqlSugarClient = new SqlHelper<object>().GetInstance();
|
return sqlSugarClient.Insertable(models).ExecuteCommand();
|
}
|
|
/// <summary>
|
/// 删除一条数据(事务)
|
/// </summary>
|
/// <param name="sqlSugarClient">数据库上下文连接</param>
|
/// <param name="model">需要删除的对象</param>
|
/// <returns></returns>
|
public static bool DeleteFirstTran(SqlSugarClient sqlSugarClient, T model)
|
{
|
var executeCommand = sqlSugarClient.Deleteable(model).ExecuteCommand();
|
|
if (executeCommand > 0)
|
{
|
return true;
|
}
|
else
|
{
|
return false;
|
}
|
}
|
|
/// <summary>
|
/// 删除一条数据(事务)
|
/// </summary>
|
/// <param name="model">需要删除的对象</param>
|
/// <returns></returns>
|
public static bool DeleteFirst(T model)
|
{
|
var sqlSugarClient = new SqlHelper<object>().GetInstance();
|
|
var executeCommand = sqlSugarClient.Deleteable(model).ExecuteCommand();
|
|
if (executeCommand > 0)
|
{
|
return true;
|
}
|
else
|
{
|
return false;
|
}
|
}
|
|
/// <summary>
|
/// 删除多条数据(事务)
|
/// </summary>
|
/// <param name="sqlSugarClient">数据库上下文连接</param>
|
/// <param name="models">需要删除的对象集合</param>
|
/// <returns></returns>
|
public static int DeleteListTran(SqlSugarClient sqlSugarClient, List<T> models)
|
{
|
return sqlSugarClient.Deleteable(models).ExecuteCommand();
|
}
|
|
/// <summary>
|
/// 删除多条数据(事务)
|
/// </summary>
|
/// <param name="models">需要删除的对象集合</param>
|
/// <returns></returns>
|
public static int DeleteList(List<T> models)
|
{
|
var sqlSugarClient = new SqlHelper<object>().GetInstance();
|
return sqlSugarClient.Deleteable(models).ExecuteCommand();
|
}
|
|
/// <summary>
|
/// 根据条件删除数据(事务)
|
/// </summary>
|
/// <param name="sqlSugarClient">数据库上下文连接</param>
|
/// <param name="condition">条件</param>
|
/// <returns></returns>
|
public static int DeleteListTran(SqlSugarClient sqlSugarClient, Expression<Func<T, bool>> condition)
|
{
|
return sqlSugarClient.Deleteable<T>().Where(condition).ExecuteCommand();
|
}
|
|
/// <summary>
|
/// 根据条件删除数据(事务)
|
/// </summary>
|
/// <param name="condition">条件</param>
|
/// <returns></returns>
|
public static int DeleteList(Expression<Func<T, bool>> condition)
|
{
|
var sqlSugarClient = new SqlHelper<object>().GetInstance();
|
return sqlSugarClient.Deleteable<T>().Where(condition).ExecuteCommand();
|
}
|
|
/// <summary>
|
/// 倒序查询
|
/// </summary>
|
/// <param name="orderBy"></param>
|
/// <returns></returns>
|
public static List<T> QueryFirstByDecs(Expression<Func<T, object>> orderBy)
|
{
|
var db = new SqlHelper<object>().GetInstance();
|
|
// 返回数据
|
return db.Queryable<T>().OrderByDescending(orderBy).Take(2).ToList();
|
}
|
|
/// <summary>
|
/// 获取数据库上下文连接
|
/// </summary>
|
/// <returns></returns>
|
public static SqlSugarClient QuerySqlSugarClient()
|
{
|
return new SqlHelper<object>().GetInstance();
|
}
|
}
|