using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
using System.Data ;
using Mesnac.Codd.Session ;
using Mesnac.Action.ChemicalWeighing.Entity ;
using System.Windows.Forms ;
using Mesnac.Action.ChemicalWeighing.DBHelper ;
using System.Reflection ;
namespace Mesnac.Action.ChemicalWeighing.Warehouse
{
/// <summary>
/// 工艺管理辅助类
/// </summary>
public class WarehouseHelper
{
#region 数据获取
/// <summary>
/// 获取所有投料釜列表
/// </summary>
public static DataTable GetAllWarehouse ( )
{
DbHelper dbHelper = Mesnac . Basic . DataSourceFactory . Instance . GetDbHelper ( Mesnac . Basic . DataSourceFactory . MCDbType . Local ) ;
if ( dbHelper = = null )
{
throw new Exception ( Mesnac . Basic . LanguageHelper . DataBaseConnectError ) ;
}
dbHelper . ClearParameter ( ) ;
dbHelper . CommandType = CommandType . Text ;
string sqlstr = "select ID,Name,BarCode from Hw_Warehouse order by ID asc" ;
dbHelper . CommandText = sqlstr ;
DataTable dt = dbHelper . ToDataTable ( ) ;
return dt ;
}
/// <summary>
/// 获取所有的班次数据
/// </summary>
public static List < Pmt_Shiftime > GetShiftimeList ( )
{
DbHelper dbHelper = Mesnac . Basic . DataSourceFactory . Instance . GetDbHelper ( Mesnac . Basic . DataSourceFactory . MCDbType . Local ) ;
if ( dbHelper = = null )
{
throw new Exception ( Mesnac . Basic . LanguageHelper . DataBaseConnectError ) ;
}
dbHelper . ClearParameter ( ) ;
dbHelper . CommandType = CommandType . Text ;
string sqlstr = "select * from Pmt_Shiftime" ;
sqlstr = String . Format ( sqlstr , Mesnac . Basic . LanguageHelper . PleaseSelect ) ;
dbHelper . CommandText = sqlstr ;
DataTable table = dbHelper . ToDataTable ( ) ;
List < Pmt_Shiftime > lst = new List < Pmt_Shiftime > ( ) ;
foreach ( DataRow row in table . Rows )
{
Pmt_Shiftime pmtShiftime = new Pmt_Shiftime ( ) ;
pmtShiftime . Shift_id = Mesnac . Basic . DataProcessor . RowValue ( row , "Shift_id" , 0 ) ;
pmtShiftime . Shift_name = Mesnac . Basic . DataProcessor . RowValue ( row , "Shift_name" , String . Empty ) ;
pmtShiftime . Shift_st = Mesnac . Basic . DataProcessor . RowValue ( row , "Shift_st" , String . Empty ) ;
pmtShiftime . Shift_et = Mesnac . Basic . DataProcessor . RowValue ( row , "Shift_et" , String . Empty ) ;
pmtShiftime . Shift_class = Mesnac . Basic . DataProcessor . RowValue ( row , "Shift_class" , 0 ) ;
lst . Add ( pmtShiftime ) ;
}
return lst ;
}
# endregion
#region 判断某个物料是否存在(根据ID)
/// <summary>
/// 判断某个物料是否存在(根据ID)
/// </summary>
/// <param name="Id"></param>
/// <param name="name"></param>
/// <param name="mainId"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static bool IsExistsName ( string Id , string name , int mainId )
{
DbHelper dbHelper = Mesnac . Basic . DataSourceFactory . Instance . GetDbHelper ( Mesnac . Basic . DataSourceFactory . MCDbType . Local ) ;
if ( dbHelper = = null )
{
throw new Exception ( Mesnac . Basic . LanguageHelper . DataBaseConnectError ) ;
}
dbHelper . CommandType = CommandType . Text ;
dbHelper . ClearParameter ( ) ;
string sqlstr = "select count(ID) from Hw_WareHouse_Sub where MaterialID = @MaterialID and MainId=@MainId" ;
dbHelper . CommandText = sqlstr ;
dbHelper . AddParameter ( "@MaterialID" , Id ) ;
dbHelper . AddParameter ( "@MainId" , mainId ) ;
object result = dbHelper . ToScalar ( ) ;
if ( result ! = null & & result ! = System . DBNull . Value )
{
int intResult = 0 ;
int . TryParse ( result . ToString ( ) , out intResult ) ;
if ( intResult > 0 )
{
return true ;
}
}
return false ;
}
# endregion
#region 判断某个物料是否存在(根据ID)
/// <summary>
/// 判断某个物料是否存在(根据ID)
/// </summary>
/// <param name="Id"></param>
/// <param name="name"></param>
/// <param name="mainId"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static bool IsExistsUpdateName ( string MId , string name , int mainId , int Id )
{
DbHelper dbHelper = Mesnac . Basic . DataSourceFactory . Instance . GetDbHelper ( Mesnac . Basic . DataSourceFactory . MCDbType . Local ) ;
if ( dbHelper = = null )
{
throw new Exception ( Mesnac . Basic . LanguageHelper . DataBaseConnectError ) ;
}
dbHelper . CommandType = CommandType . Text ;
dbHelper . ClearParameter ( ) ;
string sqlstr = "select count(ID) from Hw_WareHouse_Sub where MaterialID = @MaterialID and MainId=@MainId and ID<>@ID" ;
dbHelper . CommandText = sqlstr ;
dbHelper . AddParameter ( "@MaterialID" , MId ) ;
dbHelper . AddParameter ( "@MainId" , mainId ) ;
dbHelper . AddParameter ( "@ID" , Id ) ;
object result = dbHelper . ToScalar ( ) ;
if ( result ! = null & & result ! = System . DBNull . Value )
{
int intResult = 0 ;
int . TryParse ( result . ToString ( ) , out intResult ) ;
if ( intResult > 0 )
{
return true ;
}
}
return false ;
}
# endregion
#region 根据物料id获取釜对应的material
/// <summary>
/// 根据物料id获取Pmt_material
/// </summary>
/// <param name="recipeId">物料id</param>
/// <returns>返回符合条件Pmt_Bin</returns>
public static DataTable GetMaterial ( int MId )
{
DbHelper dbHelper = Mesnac . Basic . DataSourceFactory . Instance . GetDbHelper ( Mesnac . Basic . DataSourceFactory . MCDbType . Local ) ;
if ( dbHelper = = null )
{
throw new Exception ( Mesnac . Basic . LanguageHelper . DataBaseConnectError ) ;
}
dbHelper . CommandType = CommandType . Text ;
string strSql = "SELECT ID,PId,MaterialID,MaterialName,SetWeight,SetError FROM Hw_WareHouse_Sub where MainId=@MainId" ;
dbHelper . CommandText = strSql ;
dbHelper . ClearParameter ( ) ;
dbHelper . AddParameter ( "@MainId" , MId ) ;
DataTable table = dbHelper . ToDataTable ( ) ;
if ( table ! = null & & table . Rows . Count > 0 )
{
return table ;
}
return null ;
}
# endregion
#region 根据物料Mainid获取釜对应的material
/// <summary>
/// 根据物料id获取釜对应物料
/// </summary>
/// <param name="recipeId">物料id</param>
/// <returns>返回符合条件物料</returns>
public static Hw_WareHouse_Sub GetWareHouseMaterial ( int MId )
{
DbHelper dbHelper = Mesnac . Basic . DataSourceFactory . Instance . GetDbHelper ( Mesnac . Basic . DataSourceFactory . MCDbType . Local ) ;
if ( dbHelper = = null )
{
throw new Exception ( Mesnac . Basic . LanguageHelper . DataBaseConnectError ) ;
}
Hw_WareHouse_Sub sb = new Hw_WareHouse_Sub ( ) ;
dbHelper . CommandType = CommandType . Text ;
string strSql = "SELECT * FROM Hw_WareHouse_Sub where MainId=@MainId" ;
dbHelper . CommandText = strSql ;
dbHelper . ClearParameter ( ) ;
dbHelper . AddParameter ( "@MainId" , MId ) ;
DataTable table = dbHelper . ToDataTable ( ) ;
if ( table ! = null & & table . Rows . Count > 0 )
{
foreach ( DataRow dr in table . Rows )
{
sb = ConvertDataRowToRecipeEntity ( dr ) ;
break ;
}
return sb ;
}
return null ;
}
# endregion
#region 根据物料id获取釜material
/// <summary>
/// 根据物料id获取釜对应物料
/// </summary>
/// <param name="recipeId">物料id</param>
/// <returns>返回符合条件物料</returns>
public static Hw_WareHouse_Sub GetWareHouseMaterialId ( int Id )
{
DbHelper dbHelper = Mesnac . Basic . DataSourceFactory . Instance . GetDbHelper ( Mesnac . Basic . DataSourceFactory . MCDbType . Local ) ;
if ( dbHelper = = null )
{
throw new Exception ( Mesnac . Basic . LanguageHelper . DataBaseConnectError ) ;
}
Hw_WareHouse_Sub sb = new Hw_WareHouse_Sub ( ) ;
dbHelper . CommandType = CommandType . Text ;
string strSql = "SELECT * FROM Hw_WareHouse_Sub where ID=@Id" ;
dbHelper . CommandText = strSql ;
dbHelper . ClearParameter ( ) ;
dbHelper . AddParameter ( "@Id" , Id ) ;
DataTable table = dbHelper . ToDataTable ( ) ;
if ( table ! = null & & table . Rows . Count > 0 )
{
foreach ( DataRow dr in table . Rows )
{
sb = ConvertDataRowToRecipeEntity ( dr ) ;
break ;
}
return sb ;
}
return null ;
}
# endregion
#region 判断是否存在条码
/// <summary>
/// 判断某个物料是否存在(根据ID)
/// </summary>
/// <param name="Id"></param>
/// <param name="name"></param>
/// <param name="mainId"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static bool IsExistsCode ( string code , int Id )
{
DbHelper dbHelper = Mesnac . Basic . DataSourceFactory . Instance . GetDbHelper ( Mesnac . Basic . DataSourceFactory . MCDbType . Local ) ;
if ( dbHelper = = null )
{
throw new Exception ( Mesnac . Basic . LanguageHelper . DataBaseConnectError ) ;
}
dbHelper . CommandType = CommandType . Text ;
dbHelper . ClearParameter ( ) ;
string sqlstr = "select count(ID) from Hw_Warehouse where BarCode=@BarCode and ID<>@ID" ;
dbHelper . CommandText = sqlstr ;
dbHelper . AddParameter ( "@BarCode" , code ) ;
dbHelper . AddParameter ( "@ID" , Id ) ;
object result = dbHelper . ToScalar ( ) ;
if ( result ! = null & & result ! = System . DBNull . Value )
{
int intResult = 0 ;
int . TryParse ( result . ToString ( ) , out intResult ) ;
if ( intResult > 0 )
{
return true ;
}
}
return false ;
}
# endregion
/// <summary>
/// 保存物料信息
/// </summary>
/// <param name="recipe"></param>
/// <returns></returns>
public static bool InsertWareHouseSub ( Hw_WareHouse_Sub sub )
{
//DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local);
//if (dbHelper == null)
//{
// throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError);
//}
//dbHelper.CommandType = CommandType.Text;
//string sqlstr = @"INSERT INTO Hw_WareHouse_Sub(MainId,MaterialID,MaterialName,Material_Code,SetWeight)VALUES(@MainId,@MaterialID,@MaterialName,@Material_Code,@SetWeight)";
//dbHelper.ClearParameter();
//dbHelper.CommandText = sqlstr;
//dbHelper.AddParameter("@ID", sub.ID);
//dbHelper.AddParameter("@MainId", sub.MainId);
//dbHelper.AddParameter("@MaterialID", sub.MaterialID);
//dbHelper.AddParameter("@MaterialName", sub.MaterialName);
//dbHelper.AddParameter("@Material_Code", sub.Material_Code);
//dbHelper.AddParameter("@SetWeight", sub.SetWeight);
//dbHelper.ExecuteNonQuery();
sub . ID = 0 ;
IFreeSql fsql = FreeHelper . Instance ;
var result = fsql . Insert < Hw_WareHouse_Sub > ( sub ) . ExecuteAffrows ( ) ;
if ( result > 0 )
{
return true ;
}
return false ;
}
/// <summary>
/// 更新物料信息
/// </summary>
/// <param name="recipe"></param>
/// <returns></returns>
public static bool UpdateWareHouseSub ( Hw_WareHouse_Sub sub )
{
DbHelper dbHelper = Mesnac . Basic . DataSourceFactory . Instance . GetDbHelper ( Mesnac . Basic . DataSourceFactory . MCDbType . Local ) ;
if ( dbHelper = = null )
{
throw new Exception ( Mesnac . Basic . LanguageHelper . DataBaseConnectError ) ;
}
dbHelper . CommandType = CommandType . Text ;
string sqlstr = sqlstr = @"UPDATE Hw_WareHouse_Sub set MainId = @MainId,MaterialID =@MaterialID,MaterialName=@MaterialName,Material_Code=@Material_Code,SetWeight=@SetWeight where ID = @ID" ;
dbHelper . ClearParameter ( ) ;
dbHelper . CommandText = sqlstr ;
dbHelper . AddParameter ( "@ID" , sub . ID ) ;
dbHelper . AddParameter ( "@MainId" , sub . MainId ) ;
dbHelper . AddParameter ( "@MaterialID" , sub . MaterialID ) ;
dbHelper . AddParameter ( "@MaterialName" , sub . MaterialName ) ;
dbHelper . AddParameter ( "@Material_Code" , sub . Material_Code ) ;
dbHelper . AddParameter ( "@SetWeight" , sub . SetWeight ) ;
dbHelper . ExecuteNonQuery ( ) ;
return true ;
}
/// <summary>
/// 更新釜信息
/// </summary>
/// <param name="recipe"></param>
/// <returns></returns>
public static bool UpdateWareHouse ( Hw_Warehouse wh )
{
DbHelper dbHelper = Mesnac . Basic . DataSourceFactory . Instance . GetDbHelper ( Mesnac . Basic . DataSourceFactory . MCDbType . Local ) ;
if ( dbHelper = = null )
{
throw new Exception ( Mesnac . Basic . LanguageHelper . DataBaseConnectError ) ;
}
dbHelper . CommandType = CommandType . Text ;
string sqlstr = sqlstr = @"UPDATE [dbo].[Hw_Warehouse] set Name = @Name,BarCode =@BarCode,UpdateTime=@UpdateTime where ID = @ID" ;
dbHelper . ClearParameter ( ) ;
dbHelper . CommandText = sqlstr ;
dbHelper . AddParameter ( "@ID" , wh . ID ) ;
dbHelper . AddParameter ( "@Name" , wh . Name ) ;
dbHelper . AddParameter ( "@BarCode" , wh . BarCode ) ;
dbHelper . AddParameter ( "@UpdateTime" , DateTime . Now . ToString ( ) ) ;
dbHelper . ExecuteNonQuery ( ) ;
return true ;
}
#region 把DataRow的数据封装至配方实体
/// 把DataRow的数据封装至配方实体
/// </summary>
/// <param name="dr">配方数据行</param>
/// <returns>返回封装号的配方实体对象</returns>
public static Hw_WareHouse_Sub ConvertDataRowToRecipeEntity ( DataRow dr )
{
if ( dr ! = null )
{
Hw_WareHouse_Sub recipe = new Hw_WareHouse_Sub ( ) ;
recipe . ID = Mesnac . Basic . DataProcessor . RowValue ( dr , "ID" , 0 ) ;
recipe . MainId = Mesnac . Basic . DataProcessor . RowValue ( dr , "MainId" , 0 ) ;
recipe . MaterialID = Mesnac . Basic . DataProcessor . RowValue ( dr , "MaterialID" , String . Empty ) ;
recipe . MaterialName = Mesnac . Basic . DataProcessor . RowValue ( dr , "MaterialName" , String . Empty ) ;
recipe . Material_Code = Mesnac . Basic . DataProcessor . RowValue ( dr , "Material_Code" , String . Empty ) ;
recipe . SetWeight = Convert . ToDouble ( Mesnac . Basic . DataProcessor . RowValue ( dr , "SetWeight" , 0 ) ) ;
return recipe ;
}
else
{
return null ;
}
}
# endregion
#region 设置(网格控件)的表头、字体和行样式
/// <summary>
/// 设置(网格控件)的表头、字体和行样式
/// </summary>
/// <param name="grid"></param>
public static void SetDataGridViewStyle ( DataGridView grid )
{
lock ( String . Empty )
{
try
{
if ( grid = = null )
{
ICSharpCode . Core . LoggingService < WarehouseHelper > . Warn ( "设置计划背景色失败: 网格控件为null" ) ;
return ;
}
grid . RowTemplate . Height = 28 ;
grid . DefaultCellStyle . Font = new System . Drawing . Font ( "宋体" , 12F , System . Drawing . FontStyle . Bold , System . Drawing . GraphicsUnit . Point , ( ( byte ) ( 134 ) ) ) ;
grid . ColumnHeadersHeight = 28 ;
grid . ColumnHeadersDefaultCellStyle . Font = new System . Drawing . Font ( "宋体" , 10F , System . Drawing . FontStyle . Regular , System . Drawing . GraphicsUnit . Point , ( ( byte ) ( 134 ) ) ) ;
grid . RowHeadersVisible = false ;
}
catch ( Exception ex )
{
ICSharpCode . Core . LoggingService < WarehouseHelper > . Error ( "【投料釜管理】设置( 网格控件) 的表头、字体和行样式SetDataGridViewStyle失败: " + ex . Message ) ;
}
}
}
# endregion
#region 根据物料ID删除物料
public static void DeleteMaterial ( int id )
{
DbHelper dbHelper = Mesnac . Basic . DataSourceFactory . Instance . GetDbHelper ( Mesnac . Basic . DataSourceFactory . MCDbType . Local ) ;
if ( dbHelper = = null )
{
throw new Exception ( Mesnac . Basic . LanguageHelper . DataBaseConnectError ) ;
}
dbHelper . CommandType = CommandType . Text ;
dbHelper . ClearParameter ( ) ;
string strSql = "DELETE FROM Hw_WareHouse_Sub WHERE ID = @ID" ;
dbHelper . CommandText = strSql ;
dbHelper . AddParameter ( "@ID" , id ) ;
dbHelper . ExecuteNonQuery ( ) ;
}
# endregion
#region 根据物料ID删除物料
public static void DeleteAllMaterial ( int id )
{
DbHelper dbHelper = Mesnac . Basic . DataSourceFactory . Instance . GetDbHelper ( Mesnac . Basic . DataSourceFactory . MCDbType . Local ) ;
if ( dbHelper = = null )
{
throw new Exception ( Mesnac . Basic . LanguageHelper . DataBaseConnectError ) ;
}
dbHelper . CommandType = CommandType . Text ;
dbHelper . ClearParameter ( ) ;
string strSql = "DELETE FROM Hw_WareHouse_Sub WHERE MainId = @ID" ;
dbHelper . CommandText = strSql ;
dbHelper . AddParameter ( "@ID" , id ) ;
dbHelper . ExecuteNonQuery ( ) ;
}
# endregion
}
}