using System; using System.Collections.Generic; using System.Linq; using System.Text; using Mesnac.Action.Base; using System.Windows.Forms; using Mesnac.Codd.Session; using System.Data; using Mesnac.Action.Feeding.Qingquan.Technology; namespace Mesnac.Action.Feeding.Qingquan.SynchroData { #region 网络同步原材料 /// /// 原材料同步 /// public class MaterialSynchronous : FeedingAction, IAction { /// /// 物料同步完成事件 /// public static event EventHandler MaterialSynchronousComplete; public void Run(RuntimeParameter runtime) { base.RunIni(runtime); #region 判断与验证 if (base.NetType == NetTypes.Local) { //此系统版本为单机版,不能进行数据同步... ShowMsg(Language(32), Language(1), System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information); return; } if (!PlanCommon.IsCanConnectServer()) { //连接网络数据库失败... ShowMsg(base.Language(33)); runtime.IsReturn = true; return; } //从网络导入最新的原材料信息,您确认? if (MessageBox.Show(Language(35), Language(1), MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { string kk = Language(35); return; } #endregion DbHelper dbHelperLocal = NewDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); if (dbHelperLocal == null) { return; } DbHelper dbHelperServer = NewDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Server); if (dbHelperServer == null) { return; } try { #region 1、同步该机台配方中用到的物料 //dbHelperLocal.BeginTransaction(); //1、清空本地库物料表数据 dbHelperLocal.ClearParameter(); dbHelperLocal.CommandType = CommandType.Text; string strSql1 = "Truncate table [pmt_material]"; dbHelperLocal.CommandText = strSql1; dbHelperLocal.ExecuteNonQuery(); //2、从网络库获取本机配方对应的物料信息 dbHelperServer.ClearParameter(); dbHelperServer.CommandType = CommandType.Text; string strSql2 = "select distinct Pmt_Weight.mater_code as MaterialCode,Pmt_Weight.Weight_Type as WeightType,Pmt_Weight.mater_name as MaterialName"; strSql2 += " from Pmt_Weight,Pmt_material"; strSql2 += " where Pmt_Weight.Equip_Code = @EquipCode"; strSql2 += " and Pmt_Weight.mater_code = Pmt_Weight.mater_code and isnull(Pmt_Weight.mater_code,'')<>'' "; dbHelperServer.CommandText = strSql2; dbHelperServer.AddParameter("@EquipCode", base.CurrEquipCode); string materialCode = String.Empty; string weightType = String.Empty; string materialName = String.Empty; string strSql3 = "if not exists(select ObjId from [pmt_material] where [mater_code]=@MaterialCode) "; strSql3 += "insert into [pmt_material]([mater_code],[mater_type],[mater_name]) values(@MaterialCode,@MaterialType,@MaterialName)"; using (IDataReader reader = dbHelperServer.ToDbDataReader()) { if (reader != null) { while (reader.Read()) { materialCode = reader["MaterialCode"] as string; weightType = reader["WeightType"] as string; materialName = reader["MaterialName"] as string; string materialType = Global.GetMaterialTypeByWeightType(weightType); if (String.IsNullOrEmpty(materialType)) { ICSharpCode.Core.LoggingService.Warn("同步物料时,存在其他类型的物料!"); continue; } dbHelperLocal.CommandType = CommandType.Text; dbHelperLocal.ClearParameter(); dbHelperLocal.CommandText = strSql3; dbHelperLocal.AddParameter("@MaterialCode", materialCode); dbHelperLocal.AddParameter("@MaterialType", materialType); dbHelperLocal.AddParameter("@MaterialName", materialName); dbHelperLocal.ExecuteNonQuery(); } reader.Close(); reader.Dispose(); } } #endregion #region 2、导入所有炭黑 油 // #endregion #region 3、导入配方表物料数据 string strSql4 = "select distinct Mater_Code,Mater_name"; strSql4 += " from [Pmt_Recipe] where Equip_Code = @RecipeEquipCode"; strSql4 += " group by Mater_Code,Mater_name"; dbHelperServer.ClearParameter(); dbHelperServer.CommandText = strSql4; dbHelperServer.AddParameter("@RecipeEquipCode", base.CurrEquipCode); using (IDataReader reader2 = dbHelperServer.ToDbDataReader()) { string materialType2 = "胶料"; //物料类型已确定 if (reader2 != null) { while (reader2.Read()) { materialCode = reader2["RecipeMaterialCode"] as string; materialName = reader2["RecipeMaterialName"] as string; dbHelperLocal.CommandType = CommandType.Text; dbHelperLocal.ClearParameter(); dbHelperLocal.CommandText = strSql3; dbHelperLocal.AddParameter("@MaterialCode", materialCode); dbHelperLocal.AddParameter("@MaterialType", materialType2); dbHelperLocal.AddParameter("@MaterialName", materialName); dbHelperLocal.ExecuteNonQuery(); } reader2.Close(); reader2.Dispose(); } } #endregion #region 触发物料同步完成事件 //触发物料同步完成事件 if (MaterialSynchronous.MaterialSynchronousComplete != null) { MaterialSynchronous.MaterialSynchronousComplete(null, System.EventArgs.Empty); } #endregion //dbHelperLocal.CommitTransaction(); } catch (Exception ex) { //dbHelperLocal.RollbackTransaction(); string msg = "同步原材料失败:" + ex.Message; ShowMsg(msg, Language(1), System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information); } finally { //dbHelperLocal.CloseConnection(); } } #region 下载指定物料到本地库 /// /// 下载指定物料到本地库 /// /// 本地数据连接对象 /// 物料编码 /// 物料名称 /// 物料类型 /// 成功返回true,失败返回false public static bool DownLoadMaterial(DbHelper localHelper, string materCode, string materName, string materType) { try { string strSql = "delete from [pmt_material] where [mater_code]=@MaterialCode;"; strSql += "insert into [pmt_material]([mater_code],[mater_name],[mater_type]) values(@MaterialCode,@MaterialName,@MaterialType)"; localHelper.CommandType = CommandType.Text; localHelper.ClearParameter(); localHelper.CommandText = strSql; localHelper.AddParameter("@MaterialCode", materCode); localHelper.AddParameter("@MaterialName", materName); localHelper.AddParameter("@MaterialType", materType); localHelper.ExecuteNonQuery(); #region 触发物料同步完成事件 //触发物料同步完成事件 if (MaterialSynchronous.MaterialSynchronousComplete != null) { MaterialSynchronous.MaterialSynchronousComplete(null, System.EventArgs.Empty); } #endregion return true; } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error("从网路中下载物料到本地库失败:" + ex.Message); return false; } } #endregion #region 从网络库中下载日罐物料参数表中用到的物料数据 /// /// 从网络库中下载日罐物料参数表中用到的物料数据 /// /// 本地库数据连接对象 /// 网络库数据连接对象 /// 机台号 /// 成功返回true,失败返回false public static bool DownLoadMaterialBySytJar(DbHelper localHelper, DbHelper serverHelper, string equipCode) { try { string strServerSql = @"select a.Mater_code as mater_code,b.Mater_name as mater_name,a.Jar_type as mater_type from Pmt_wm a left join Pmt_material b on a.Mater_code=b.Mater_code"; strServerSql += " where Equip_code=@EquipCode"; serverHelper.CommandType = CommandType.Text; serverHelper.ClearParameter(); serverHelper.CommandText = strServerSql; serverHelper.AddParameter("@EquipCode", equipCode); DataTable materTable = serverHelper.ToDataTable(); if (materTable != null && materTable.Rows != null && materTable.Rows.Count > 0) { foreach (DataRow row in materTable.Rows) { string materCode = Mesnac.Basic.DataProcessor.RowValue(row, "mater_code", String.Empty); string materName = Mesnac.Basic.DataProcessor.RowValue(row, "mater_name", String.Empty); string materType = Mesnac.Basic.DataProcessor.RowValue(row, "mater_type", String.Empty); if (!String.IsNullOrEmpty(materCode)) { string strLocalSql = "delete from [pmt_material] where [mater_code]=@MaterialCode;"; strLocalSql += "insert into [pmt_material]([mater_code],[mater_name],[mater_type]) values(@MaterialCode,@MaterialName,@MaterialType)"; localHelper.CommandType = CommandType.Text; localHelper.ClearParameter(); localHelper.CommandText = strLocalSql; localHelper.AddParameter("@MaterialCode", materCode); localHelper.AddParameter("@MaterialName", materName); localHelper.AddParameter("@MaterialType", materType); localHelper.ExecuteNonQuery(); } } } #region 触发物料同步完成事件 //触发物料同步完成事件 if (MaterialSynchronous.MaterialSynchronousComplete != null) { MaterialSynchronous.MaterialSynchronousComplete(null, System.EventArgs.Empty); } #endregion return true; } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error("从网路中下载物料到本地库失败:" + ex.Message); return false; } } #endregion #region 从网络库中下载配方物料到本地库 /// /// 从网络库中下载配方物料到本地库 /// /// 本地库数据访问对象 /// 网络库数据访问对象 /// 机台号 /// 配方物料代码 /// 配方版本 /// public static bool DownLoadRecipeMaterial(DbHelper localHelper, DbHelper serverHelper, string equipCode, string recipeMaterCode, string recipeVersionId) { try { #region 下载配方的称量物料 string strSql1 = @"select distinct Pmt_Weight.mater_code as MaterialCode,Pmt_Weight.Weight_Type as WeightType,Pmt_Weight.mater_name as MaterialName from Pmt_Weight,Pmt_material where Pmt_Weight.Equip_Code =@equipCode and Pmt_Weight.Father_code = @RecipeMaterialCode and Edt_Code= @RecipeVersionID and Pmt_Weight.mater_code = Pmt_material.mater_code"; serverHelper.CommandType = CommandType.Text; serverHelper.ClearParameter(); serverHelper.CommandText = strSql1; serverHelper.AddParameter("@equipCode", equipCode.Trim()); serverHelper.AddParameter("@RecipeMaterialCode", recipeMaterCode.Trim()); serverHelper.AddParameter("@RecipeVersionID", recipeVersionId.Trim()); string materialCode = String.Empty; string weightType = String.Empty; string materialName = String.Empty; string strSql3 = "delete from [pmt_material] where [mater_code]=@MaterialCode "; strSql3 += "insert into [pmt_material]([mater_code],[mater_type],[mater_name]) values(@MaterialCode,@MaterialType,@MaterialName)"; using (IDataReader reader = serverHelper.ToDbDataReader()) { if (reader != null) { while (reader.Read()) { materialCode = reader["MaterialCode"] as string; weightType = reader["WeightType"] as string; materialName = reader["MaterialName"] as string; string materialType = Global.GetMaterialTypeByWeightType(weightType); if (String.IsNullOrEmpty(materialType)) { ICSharpCode.Core.LoggingService.Warn("同步物料时,存在其他类型的物料!"); continue; } localHelper.CommandType = CommandType.Text; localHelper.ClearParameter(); localHelper.CommandText = strSql3; localHelper.AddParameter("@MaterialCode", materialCode); localHelper.AddParameter("@MaterialType", materialType); localHelper.AddParameter("@MaterialName", materialName); localHelper.ExecuteNonQuery(); } reader.Close(); reader.Dispose(); } } #endregion #region 下载配方数据 string strSql2 = @"select distinct Mater_Code as RecipeMaterialCode,Mater_name as RecipeMaterialName from [Pmt_Recipe] where Equip_Code = @equipCode and Mater_Code = @RecipeMaterialCode and Edt_Code = @RecipeVersionID group by Mater_Code,Mater_name"; serverHelper.CommandType = CommandType.Text; serverHelper.ClearParameter(); serverHelper.CommandText = strSql2; serverHelper.AddParameter("@equipCode", equipCode.Trim()); serverHelper.AddParameter("@RecipeMaterialCode", recipeMaterCode.Trim()); serverHelper.AddParameter("@RecipeVersionID", recipeVersionId.Trim()); using (IDataReader reader2 = serverHelper.ToDbDataReader()) { string materialType2 = "胶料"; //物料类型已确定 if (reader2 != null) { while (reader2.Read()) { materialCode = reader2["RecipeMaterialCode"] as string; materialName = reader2["RecipeMaterialName"] as string; localHelper.CommandType = CommandType.Text; localHelper.ClearParameter(); localHelper.CommandText = strSql3; localHelper.AddParameter("@MaterialCode", materialCode); localHelper.AddParameter("@MaterialType", materialType2); localHelper.AddParameter("@MaterialName", materialName); localHelper.ExecuteNonQuery(); } reader2.Close(); reader2.Dispose(); } } #endregion #region 触发物料同步完成事件 //触发物料同步完成事件 if (MaterialSynchronous.MaterialSynchronousComplete != null) { MaterialSynchronous.MaterialSynchronousComplete(null, System.EventArgs.Empty); } #endregion return true; } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error("从网路中下载配方物料到本地库失败:" + ex.Message); return false; } } #endregion } #endregion }