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