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.Controls.Base; using Mesnac.Action.Feeding.BasicInfo; using Mesnac.Basic; namespace Mesnac.Action.Feeding.Technology { public class RecipeSaveAs : FeedingAction , IAction { private IBaseControl _sender = null; private string oldMaterCode = String.Empty; private string oldMaterName = String.Empty; public void Run(RuntimeParameter runtime) { base.RunIni(runtime); this._sender = runtime.BaseControl; int recipeObjId = 0; List controls = base.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "[pmt_recipe].[ObjID]"); foreach (DbMCControl control in controls) { if (control.BaseControl is ComboBox) { if (control.BaseControl.MCValue != null) { if (int.TryParse(control.BaseControl.MCValue.ToString(), out recipeObjId)) { if (recipeObjId > 0) { DbHelper dbHelper = base.NewDbHelper(control.DesignSource); if (dbHelper == null) { ICSharpCode.Core.LoggingService.Warn("配方另存为...失败,获取本地数据库连接失败!"); runtime.IsReturn = true; //终止执行 return; } DataTable recipeTable = this.GetRecipeData(dbHelper, recipeObjId); //获取配方主信息 if (recipeTable != null && recipeTable.Rows.Count > 0) { DataRow row = recipeTable.Rows[0]; this.oldMaterCode = DataProcessor.RowValue(row, "mater_code", String.Empty).Trim(); this.oldMaterName = DataProcessor.RowValue(row, "mater_name", String.Empty).Trim(); string newMaterCode = String.Empty; string newMaterName = String.Empty; FrmRecipeSaveAs frmRecipeSaveAs = new FrmRecipeSaveAs(this.oldMaterCode, this.oldMaterName); frmRecipeSaveAs.ShowDialog(this._sender.MCRoot as Control); if (frmRecipeSaveAs.DialogResult == DialogResult.OK) { newMaterCode = frmRecipeSaveAs.MaterCode.Trim(); newMaterName = frmRecipeSaveAs.MaterName.Trim(); //保存配方主信息 int newRecipeObjId = this.CopyRecipeInfo(dbHelper, row, newMaterCode, newMaterName); if (newRecipeObjId > 0) { bool result = true; //保存密炼信息 if (!this.CopyRecipeMixingData(dbHelper, recipeObjId, newRecipeObjId, newMaterCode)) { result = false; } //保存称量信息 if (!this.CopyRecipeWeightData(dbHelper, recipeObjId, newRecipeObjId, newMaterCode)) { result = false; } if (result) { //配方另存成功! MessageBox.Show(Language(289), Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information); base.DBLog("配方管理", "配方另存", "原配方编码:" + this.oldMaterCode +";原配方名称:" + this.oldMaterName + "->另存配方编码:" + newMaterCode + ";另存配方名称:" + newMaterName); } } } else { runtime.IsReturn = true; //终止执行 } frmRecipeSaveAs.Dispose(); return; } break; } } } } } if (recipeObjId == 0) { //配方获取失败,请先选择一个配方! MessageBox.Show(LanguageService.Instance.Read(285), LanguageService.Instance.Read(1), MessageBoxButtons.OK, MessageBoxIcon.Information); runtime.IsReturn = true; //终止执行 } } /// /// 复制配方主信息 /// /// 本地数据访问类 /// 要复制的配方ID /// 另存的配方编码 /// 另存的配方名称 /// 另存成功返回新配方的配方ID,否则返回0 public int CopyRecipeInfo(DbHelper localHelper, DataRow oldRcipeDataRow, string newMaterCode, string newMaterName) { int newRecipeObjId = 0; try { oldRcipeDataRow["mater_code"] = newMaterCode; oldRcipeDataRow["mater_name"] = newMaterName; localHelper.CommandType = System.Data.CommandType.Text; localHelper.ClearParameter(); string strSql = String.Empty; strSql = "insert into pmt_recipe({0}) values({1});select SCOPE_IDENTITY();"; StringBuilder sbField = new StringBuilder(); StringBuilder sbParameter = new StringBuilder(); for (int i = 0; i < oldRcipeDataRow.Table.Columns.Count; i++) { DataColumn col = oldRcipeDataRow.Table.Columns[i]; if (col.ColumnName.Trim().Equals("ObjId", StringComparison.CurrentCultureIgnoreCase)) { continue; } if (String.IsNullOrEmpty(sbField.ToString())) { sbField.Append(col.ColumnName); } else { sbField.Append("," + col.ColumnName); } if (String.IsNullOrEmpty(sbParameter.ToString())) { sbParameter.Append("@" + col.ColumnName); } else { sbParameter.Append(",@" + col.ColumnName); } localHelper.AddParameter("@" + col.ColumnName, oldRcipeDataRow[col]); } strSql = String.Format(strSql, sbField.ToString(), sbParameter.ToString()); localHelper.CommandText = strSql; object result = localHelper.ToScalar(); if (result != null && result != System.DBNull.Value) { int.TryParse(result.ToString(), out newRecipeObjId); } } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error("配方主信息另存为失败:" + ex.Message); //配方主信息另存为失败: MessageBox.Show(Language(286) + ex.Message, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information); } return newRecipeObjId; } #region 复制配方混炼信息 /// /// 复制配方混炼信息 /// /// 本地数据访问类 /// 要复制的配方ID /// 另存为配方的配方ID /// 另存为配方的配方编码 public bool CopyRecipeMixingData(DbHelper localHelper, int oldRecipeObjId, int newRecipeId, string newMaterCode) { try { DataTable dt_mix = this.GetRecipeMixingData(localHelper, oldRecipeObjId); localHelper.CommandType = System.Data.CommandType.Text; foreach (DataRow row in dt_mix.Rows) { localHelper.ClearParameter(); string strSql = String.Empty; strSql = "insert into pmt_mix({0}) values({1});"; StringBuilder sbField = new StringBuilder(); StringBuilder sbParameter = new StringBuilder(); for (int i = 0; i < dt_mix.Columns.Count; i++) { DataColumn col = dt_mix.Columns[i]; if (col.ColumnName.Trim().Equals("ObjId", StringComparison.CurrentCultureIgnoreCase)) { continue; } if (String.IsNullOrEmpty(sbField.ToString())) { sbField.Append(col.ColumnName); } else { sbField.Append("," + col.ColumnName); } if (String.IsNullOrEmpty(sbParameter.ToString())) { sbParameter.Append("@" + col.ColumnName); } else { sbParameter.Append(",@" + col.ColumnName); } if (col.ColumnName.Trim().Equals("RecipeObjID", StringComparison.CurrentCultureIgnoreCase)) { localHelper.AddParameter("@" + col.ColumnName, newRecipeId); } else if (col.ColumnName.Trim().Equals("father_code", StringComparison.CurrentCultureIgnoreCase)) { localHelper.AddParameter("@" + col.ColumnName, newMaterCode); } else { localHelper.AddParameter("@" + col.ColumnName, row[col]); } } strSql = String.Format(strSql, sbField.ToString(), sbParameter.ToString()); localHelper.CommandText = strSql; localHelper.ExecuteNonQuery(); } return true; } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error("配方密炼信息另存为失败:" + ex.Message); //配方密炼信息另存为失败: MessageBox.Show(Language(287) + ex.Message, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information); return false; } } #endregion #region 复制配方称量信息 /// /// 复制配方称量信息 /// /// 本地数据访问类 /// 要复制的配方ID /// 另存为配方的配方ID /// 另存为配方的配方编码 public bool CopyRecipeWeightData(DbHelper localHelper, int oldRecipeObjId, int newRecipeId, string newMaterCode) { try { DataTable dt_weight = this.GetRecipeWeightData(localHelper, oldRecipeObjId); localHelper.CommandType = System.Data.CommandType.Text; foreach (DataRow row in dt_weight.Rows) { localHelper.ClearParameter(); string strSql = String.Empty; strSql = "insert into pmt_weigh({0}) values({1});"; StringBuilder sbField = new StringBuilder(); StringBuilder sbParameter = new StringBuilder(); for (int i = 0; i < dt_weight.Columns.Count; i++) { DataColumn col = dt_weight.Columns[i]; if (col.ColumnName.Trim().Equals("ObjId", StringComparison.CurrentCultureIgnoreCase)) { continue; } if (String.IsNullOrEmpty(sbField.ToString())) { sbField.Append(col.ColumnName); } else { sbField.Append("," + col.ColumnName); } if (String.IsNullOrEmpty(sbParameter.ToString())) { sbParameter.Append("@" + col.ColumnName); } else { sbParameter.Append(",@" + col.ColumnName); } if (col.ColumnName.Trim().Equals("RecipeObjID", StringComparison.CurrentCultureIgnoreCase)) { localHelper.AddParameter("@" + col.ColumnName, newRecipeId); } else if (col.ColumnName.Trim().Equals("father_code", StringComparison.CurrentCultureIgnoreCase)) { localHelper.AddParameter("@" + col.ColumnName, newMaterCode); } else { localHelper.AddParameter("@" + col.ColumnName, row[col]); } } strSql = String.Format(strSql, sbField.ToString(), sbParameter.ToString()); localHelper.CommandText = strSql; localHelper.ExecuteNonQuery(); } return true; } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error("配方称量信息另存为失败:" + ex.Message); //配方称量信息另存为失败: MessageBox.Show(Language(288) + ex.Message, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information); return false; } } #endregion #region 获取配方主信息 /// /// 获取配方主信息 /// /// 本地数据连接对象 /// 配方Id /// 返回对应的配方数据 public DataTable GetRecipeData(DbHelper localHelper, int recipeId) { localHelper.ClearParameter(); string strSql = String.Empty; strSql = "select * from pmt_recipe where ObjId=@RecipeId"; localHelper.CommandText = strSql; localHelper.AddParameter("@RecipeId", recipeId); DataTable dt_recipe = localHelper.ToDataTable(); return dt_recipe; } #endregion #region 获取配方密炼信息 /// /// 获取配方密炼信息 /// /// 本地数据连接对象 /// 配方Id /// 返回对应的配方密炼数据 public DataTable GetRecipeMixingData(DbHelper localHelper, int recipeId) { localHelper.ClearParameter(); string strSql = String.Empty; strSql = "select * from pmt_mix where RecipeObjId = @RecipeId"; localHelper.CommandText = strSql; localHelper.AddParameter("@RecipeId", recipeId); DataTable dt_mix = localHelper.ToDataTable(); return dt_mix; } #endregion #region 获取配方称量数据 /// /// 获取配方称量数据 /// /// 本地数据连接对象 /// 配方Id /// 返回对应的配方称量数据 public DataTable GetRecipeWeightData(DbHelper localHelper, int recipeId) { localHelper.ClearParameter(); string strSql = String.Empty; strSql = "select * from pmt_weigh where RecipeObjId = @RecipeId"; localHelper.CommandText = strSql; localHelper.AddParameter("@RecipeId", recipeId); DataTable dt_weight = localHelper.ToDataTable(); return dt_weight; } #endregion } }