using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using HH.WCS.Mobox3.Template.Entity;
using SqlSugar;
namespace HH.WCS.Mobox3.Template.Util.Helper
{
///
/// 数据库连接帮助类
///
public static class AdoSqlHelper 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 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 SqlSugarClient QuerySqlSugarClient()
{
return new SqlHelper().GetInstance();
}
///
/// 按照时间倒序查询
///
///
///
///
public static T QueryFirstByDecs(Expression> condition, Expression> orderBy)
{
var db = new SqlHelper().GetInstance();
// 返回数据
return db.Queryable().OrderByDescending(orderBy).First(condition);
}
#region 特定查询
///
/// 分配入库货位
///
///
public static Location QueryInputLocation(string itemCode)
{
var areaCodeList = new List
{
"1","2","3","4"
};
var db = new SqlHelper().GetInstance();
var listMaxCol = db.Queryable()
.LeftJoin((l, lc) => l.S_CODE == lc.S_LOC_CODE)
.LeftJoin((l, lc, ci) => lc.S_CNTR_CODE == ci.S_CNTR_CODE)
.Where((l, lc, ci) => l.N_CURRENT_NUM > 0
&& areaCodeList.Contains(l.S_GROUP)
&& ci.S_ITEM_CODE == itemCode)
.OrderByDescending((l, lc, ci) => l.N_COL)
.Take(1)
.PartitionBy((l, lc, ci) => new { l.S_GROUP, l.N_ROW })
.ToList();
// 有同种物料所占据的排
if (listMaxCol.Count > 0)
{
foreach (var item in listMaxCol)
{
//排有锁也排除(锁表示后面可能要改)
if (db.Queryable().Count(a =>
a.S_GROUP == item.S_GROUP && a.N_ROW == item.N_ROW && a.N_LOCK_STATE != 0) > 0)
{
continue;
}
// 没锁
var end = db.Queryable().OrderBy(a => a.N_COL).Where(a =>
a.S_GROUP == item.S_GROUP && a.N_ROW == item.N_ROW &&
a.N_CURRENT_NUM == 0 &&
a.N_COL >= item.N_COL).First();
if (end != null)
{
return end;
}
}
}
Location location = db.Queryable().OrderBy(a => a.S_GROUP).First(p => p.N_COL == 1 && p.N_LOCK_STATE == 0);
if (location != null)
{
return location;
}
return null;
}
#endregion
}
}