using DevExpress.XtraCharts.Native ;
using FreeSql.Internal.ObjectPool ;
using Mesnac.Action.ChemicalWeighing.DBHelper ;
using Mesnac.Action.ChemicalWeighing.Entity ;
using Mesnac.Action.ChemicalWeighing.Entity.material ;
using Mesnac.Action.ChemicalWeighing.Entity.station ;
using Mesnac.Action.ChemicalWeighing.Product.PptPlan ;
using Mesnac.Codd.Session ;
using System ;
using System.Collections.Generic ;
using System.Data ;
using System.Data.Common ;
using System.Data.SqlClient ;
using System.Linq ;
using System.Security.Claims ;
using System.Text ;
using System.Threading.Tasks ;
namespace Mesnac.Action.ChemicalWeighing.BinManage
{
public class BinHelper
{
#region 查询料仓信息
/// <summary>
/// 查询料仓信息
/// </summary>
/// <returns>料仓信息表</returns>
public static DataTable GetBin ( )
{
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 strSql = "SELECT Bin_Serial,Bin_Name,Bin_Code,Material_ID, Material_name,Station_Weight_Medium,Station_Weight_Low,Station_Weight_Advance,Station_Speed_Hight,Station_Speed_Medium,Station_Speed_Low,BinWeight,LimitWeight FROM Pmt_Bin LEFT JOIN xl_material ON Pmt_Bin.Material_ID=xl_material.ID ORDER BY Bin_Serial" ;
dbHelper . CommandText = strSql ;
DataTable table = dbHelper . ToDataTable ( ) ;
if ( table ! = null & & table . Rows . Count > 0 )
{
return table ;
}
return null ;
}
public static DataTable GetBinMaterial ( )
{
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 strSql = @ "select Bin_Serial, Bin_Name, Bin_Code+'(' + Material_name + ')' as Bin_Code,Material_ID, Material_name,Station_Weight_Medium,Station_Weight_Low,Station_Weight_Advance,Station_Speed_Hight,Station_Speed_Medium,Station_Speed_Low from
(
SELECT Bin_Serial , Bin_Name , Bin_Code , m . ID Material_ID , m . Material_name , Station_Weight_Medium , Station_Weight_Low , Station_Weight_Advance , Station_Speed_Hight , Station_Speed_Medium , Station_Speed_Low
FROM Pmt_Bin b inner JOIN xl_material m ON b . Material_ID = m . ID
) t ORDER BY Bin_Serial ";
dbHelper . CommandText = strSql ;
DataTable table = dbHelper . ToDataTable ( ) ;
if ( table ! = null & & table . Rows . Count > 0 )
{
return table ;
}
return null ;
}
/// <summary>
/// 查询料仓信息
/// </summary>
/// <returns>料仓信息表</returns>
public static DataTable GetBinDataTable ( )
{
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 strSql = "SELECT Bin_Serial,Bin_Name,Bin_Code,Material_ID, Material_name,Station_Weight_Medium,Station_Weight_Low,Station_Weight_Advance,Station_Speed_Hight,Station_Speed_Medium,Station_Speed_Low FROM Pmt_Bin LEFT JOIN xl_material ON Pmt_Bin.Material_ID=xl_material.ID ORDER BY Bin_Serial" ;
dbHelper . CommandText = strSql ;
DataTable table = dbHelper . ToDataTable ( ) ;
if ( table ! = null & & table . Rows . Count > 0 )
{
return table ;
}
return null ;
}
# endregion
#region 获取物料名与ID对象集合
/// <summary>
/// 获取所有物料对象集合
/// </summary>
/// <returns></returns>
public static List < xl_material > GetXlMaterialList ( )
{
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, Material_name FROM xl_material where IsEnable='是'" ;
dbHelper . CommandText = sqlstr ;
DataTable table = dbHelper . ToDataTable ( ) ;
List < xl_material > lst = new List < xl_material > ( ) ;
xl_material material = null ;
foreach ( DataRow row in table . Rows )
{
material = new xl_material ( ) ;
material . ID = Mesnac . Basic . DataProcessor . RowValue ( row , "ID" , String . Empty ) ;
material . Material_name = Mesnac . Basic . DataProcessor . RowValue ( row , "Material_name" , String . Empty ) ;
lst . Add ( material ) ;
}
return lst ;
}
# endregion
#region 根据料仓号修改对应物料
public static void UpdateBin ( Pmt_Bin bin )
{
try
{
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 strSql = "UPDATE Pmt_Bin SET Bin_Code=@Bin_Code,Material_ID=@MaterialId,LimitWeight=@LimitWeight,Station_Weight_Medium=@Station_Weight_Medium,Station_Weight_Low=@Station_Weight_Low,Station_Weight_Advance=@Station_Weight_Advance,Station_Speed_Hight=@Station_Speed_Hight,Station_Speed_Medium=@Station_Speed_Medium,Station_Speed_Low=@Station_Speed_Low,IF_FLAG=@IfFlag WHERE Bin_Serial=@BinSerial" ;
dbHelper . CommandText = strSql ;
dbHelper . AddParameter ( "@Bin_Code" , bin . Bin_Code ) ;
dbHelper . AddParameter ( "@MaterialId" , bin . Material_ID ) ;
dbHelper . AddParameter ( "@BinSerial" , bin . Bin_Serial ) ;
dbHelper . AddParameter ( "@LimitWeight" , bin . LimitWeight ) ;
dbHelper . AddParameter ( "@Station_Weight_Medium" , bin . Station_Weight_Medium ) ;
dbHelper . AddParameter ( "@Station_Weight_Low" , bin . Station_Weight_Low ) ;
dbHelper . AddParameter ( "@Station_Weight_Advance" , bin . Station_Weight_Advance ) ;
dbHelper . AddParameter ( "@Station_Speed_Hight" , bin . Station_Speed_Hight ) ;
dbHelper . AddParameter ( "@Station_Speed_Medium" , bin . Station_Speed_Medium ) ;
dbHelper . AddParameter ( "@Station_Speed_Low" , bin . Station_Speed_Low ) ;
dbHelper . AddParameter ( "@IfFlag" , 1 ) ;
dbHelper . ExecuteNonQuery ( ) ;
}
catch ( Exception ex )
{
ICSharpCode . Core . LoggingService < BinHelper > . Error ( "修改料仓信息异常:" + ex . Message , ex ) ;
}
}
# endregion
#region 获取bin对象集合
public static List < Pmt_Bin > GetBinList ( )
{
DataTable userTable = GetBin ( ) ;
List < Pmt_Bin > lst = new List < Pmt_Bin > ( ) ;
foreach ( DataRow row in userTable . Rows )
{
Pmt_Bin bin = new Pmt_Bin
{
Bin_Serial = Mesnac . Basic . DataProcessor . RowValue ( row , "Bin_Serial" , 0 ) ,
Material_ID = Mesnac . Basic . DataProcessor . RowValue ( row , "Material_ID" , String . Empty ) ,
Bin_Name = Mesnac . Basic . DataProcessor . RowValue ( row , "Bin_Name" , String . Empty ) ,
Bin_Code = Mesnac . Basic . DataProcessor . RowValue ( row , "Bin_Code" , String . Empty ) ,
Station_Weight_Medium = decimal . Parse ( Mesnac . Basic . DataProcessor . RowValue ( row , "Station_Weight_Medium" , 0.000 ) . ToString ( "f3" ) ) ,
Station_Weight_Low = decimal . Parse ( Mesnac . Basic . DataProcessor . RowValue ( row , "Station_Weight_Low" , 0.000 ) . ToString ( "f3" ) ) ,
Station_Weight_Advance = decimal . Parse ( Mesnac . Basic . DataProcessor . RowValue ( row , "Station_Weight_Advance" , 0.000 ) . ToString ( "f3" ) ) ,
Station_Speed_Hight = decimal . Parse ( Mesnac . Basic . DataProcessor . RowValue ( row , "Station_Speed_Hight" , 0.000 ) . ToString ( "f3" ) ) ,
Station_Speed_Medium = decimal . Parse ( Mesnac . Basic . DataProcessor . RowValue ( row , "Station_Speed_Medium" , 0.000 ) . ToString ( "f3" ) ) ,
Station_Speed_Low = decimal . Parse ( Mesnac . Basic . DataProcessor . RowValue ( row , "Station_Speed_Low" , 0.000 ) . ToString ( "f3" ) )
} ;
lst . Add ( bin ) ;
}
return lst ;
}
# endregion
#region 获取bin对象集合
public static List < BinList > GetBinMaterialList ( )
{
DataTable userTable = GetBinDataTable ( ) ;
List < BinList > lst = new List < BinList > ( ) ;
foreach ( DataRow row in userTable . Rows )
{
BinList bin = new BinList
{
Bin_Serial = Mesnac . Basic . DataProcessor . RowValue ( row , "Bin_Serial" , 0 ) ,
Material_ID = Mesnac . Basic . DataProcessor . RowValue ( row , "Material_ID" , String . Empty ) ,
Material_name = Mesnac . Basic . DataProcessor . RowValue ( row , "Material_name" , String . Empty ) ,
Bin_Name = Mesnac . Basic . DataProcessor . RowValue ( row , "Bin_Name" , String . Empty ) ,
Bin_Code = Mesnac . Basic . DataProcessor . RowValue ( row , "Bin_Code" , String . Empty ) ,
Station_Weight_Medium = decimal . Parse ( Mesnac . Basic . DataProcessor . RowValue ( row , "Station_Weight_Medium" , 0.000 ) . ToString ( "f3" ) ) ,
Station_Weight_Low = decimal . Parse ( Mesnac . Basic . DataProcessor . RowValue ( row , "Station_Weight_Low" , 0.000 ) . ToString ( "f3" ) ) ,
Station_Weight_Advance = decimal . Parse ( Mesnac . Basic . DataProcessor . RowValue ( row , "Station_Weight_Advance" , 0.000 ) . ToString ( "f3" ) ) ,
Station_Speed_Hight = decimal . Parse ( Mesnac . Basic . DataProcessor . RowValue ( row , "Station_Speed_Hight" , 0.000 ) . ToString ( "f3" ) ) ,
Station_Speed_Medium = decimal . Parse ( Mesnac . Basic . DataProcessor . RowValue ( row , "Station_Speed_Medium" , 0.000 ) . ToString ( "f3" ) ) ,
Station_Speed_Low = decimal . Parse ( Mesnac . Basic . DataProcessor . RowValue ( row , "Station_Speed_Low" , 0.000 ) . ToString ( "f3" ) )
} ;
lst . Add ( bin ) ;
}
return lst ;
}
# endregion
#region 根据ID获取料仓
/// <summary>
/// 获取所有物料对象集合
/// </summary>
/// <returns></returns>
public static Pmt_Bin GetFristBinInfo ( 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 . ClearParameter ( ) ;
dbHelper . CommandType = CommandType . Text ;
string sqlstr = "SELECT top 1 * FROM Pmt_Bin where Bin_Serial=@Bin_Serial" ;
dbHelper . CommandText = sqlstr ;
dbHelper . AddParameter ( "@Bin_Serial" , Id ) ;
DataTable dt = dbHelper . ToDataTable ( ) ;
Pmt_Bin bin = null ;
if ( dt ! = null & & dt . Rows . Count > 0 )
{
bin = ConvertDataToBin ( dt . Rows [ 0 ] ) ;
}
return bin ;
}
# endregion
#region 根据料仓号实时更新料仓物料重量
public static void UpdateBinWeight ( Pmt_Bin bin )
{
try
{
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 strSql = "UPDATE Pmt_Bin SET BinWeight=@BinWeight WHERE Bin_Serial=@BinSerial" ;
dbHelper . CommandText = strSql ;
dbHelper . AddParameter ( "@BinSerial" , bin . Bin_Serial ) ;
dbHelper . AddParameter ( "@BinWeight" , bin . BinWeight ) ;
dbHelper . ExecuteNonQuery ( ) ;
}
catch ( Exception ex )
{
ICSharpCode . Core . LoggingService < BinHelper > . Error ( "修改料仓信息异常:" + ex . Message , ex ) ;
}
}
# endregion
#region 根据料仓号实时更新料仓物料重量
public static bool SaveDataToBDb ( LR_weigh lrWeigh , LR_plan lrPlan , Pmt_Bin bin , Hw_BinAlarm alarm )
{
try
{
IFreeSql fsql = FreeHelper . Instance ;
{
fsql . Transaction ( ( ) = >
{
var affrows = fsql . Insert < LR_weigh > ( lrWeigh ) . ExecuteAffrows ( ) ;
//判断是否插入成功
if ( affrows < 1 )
throw new Exception ( "存盘物料详细数据失败!" ) ; //抛出异常,回滚事务,事务退出
affrows = fsql . Insert < LR_plan > ( lrPlan ) . ExecuteAffrows ( ) ;
if ( affrows < 1 )
throw new Exception ( "存盘计划详细数据失败!" ) ; //抛出异常,回滚事务,事务退出
affrows = fsql . Update < Pmt_Bin > ( bin . Bin_Serial ) . Set ( a = > a . BinWeight , bin . BinWeight ) . ExecuteAffrows ( ) ;
if ( affrows < 1 )
throw new Exception ( "更新仓库库存容量数据失败!" ) ; //抛出异常,回滚事务,事务退出
affrows = fsql . Insert < Hw_BinAlarm > ( alarm ) . ExecuteAffrows ( ) ;
if ( affrows < 1 )
throw new Exception ( "物料出库存盘数据失败!" ) ; //抛出异常,回滚事务,事务退出
} ) ;
return true ;
}
}
catch ( Exception ex )
{
ICSharpCode . Core . LoggingService < BinHelper > . Error ( "执行小料存盘事务信息异常:" + ex . Message , ex ) ;
return false ;
}
}
# endregion
/// <summary>
/// 把数据行对象转换为下校秤配方数据实体对象
/// </summary>
/// <param name="dr">要转换的数据行</param>
/// <returns>返回转换后的实体对象</returns>
public static Pmt_Bin ConvertDataToBin ( DataRow row )
{
if ( row ! = null )
{
Pmt_Bin bin = new Pmt_Bin ( ) ;
bin . Bin_Serial = Mesnac . Basic . DataProcessor . RowValue ( row , "Bin_Serial" , 0 ) ;
bin . Bin_Name = Mesnac . Basic . DataProcessor . RowValue ( row , "Bin_Name" , String . Empty ) ;
bin . Bin_Code = Mesnac . Basic . DataProcessor . RowValue ( row , "Bin_Code" , String . Empty ) ;
bin . Material_ID = Mesnac . Basic . DataProcessor . RowValue ( row , "Material_ID" , String . Empty ) ;
bin . BinWeight = Convert . ToDecimal ( Mesnac . Basic . DataProcessor . RowValue ( row , "BinWeight" , String . Empty ) ) ;
bin . LimitWeight = Convert . ToDecimal ( Mesnac . Basic . DataProcessor . RowValue ( row , "LimitWeight" , String . Empty ) ) ;
bin . Station_Weight_Medium = Convert . ToDecimal ( Mesnac . Basic . DataProcessor . RowValue ( row , "Station_Weight_Medium" , String . Empty ) ) ;
bin . Station_Weight_Low = Convert . ToDecimal ( Mesnac . Basic . DataProcessor . RowValue ( row , "Station_Weight_Low" , String . Empty ) ) ;
bin . Station_Weight_Advance = Convert . ToDecimal ( Mesnac . Basic . DataProcessor . RowValue ( row , "Station_Weight_Advance" , String . Empty ) ) ;
bin . Station_Speed_Hight = Convert . ToDecimal ( Mesnac . Basic . DataProcessor . RowValue ( row , "Station_Speed_Hight" , String . Empty ) ) ;
bin . Station_Speed_Medium = Convert . ToDecimal ( Mesnac . Basic . DataProcessor . RowValue ( row , "Station_Speed_Medium" , String . Empty ) ) ;
bin . Station_Speed_Low = Convert . ToDecimal ( Mesnac . Basic . DataProcessor . RowValue ( row , "Station_Speed_Low" , String . Empty ) ) ;
return bin ;
}
else
{
return null ;
}
}
}
}