using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Windows.Forms; using ICSharpCode.Core; using Mesnac.Codd.Session; using Mesnac.Action.Base; using System.Reflection; using System.Configuration; using Mesnac.Action.ChemicalWeighing.Technical.PmtRecipe.entity; namespace Mesnac.Action.ChemicalWeighing.Technical.PmtRecipe { /// /// 工艺配方辅助类 /// public class RecipeHelper { #region 静态属性定义 #region 配方码表业务 #region 配方主信息 - 获取配方类型列表 /// /// 配方主信息 - 配方类型列表 /// public static DataTable RecipeTypeList { get { if (BaseDataHelper.SysCode.ContainsKey("PmtType")) { DataTable dt = BaseDataHelper.SysCode["PmtType"]; DataTable table = BaseDataHelper.CloneDataTable(dt); return table; } else { return null; } } } #endregion #region 配方主信息 - 获取配方状态列表 /// /// 配方主信息 - 配方状态列表 /// public static DataTable RecipeStateList { get { if (BaseDataHelper.SysCode.ContainsKey("PmtState")) { DataTable dt = BaseDataHelper.SysCode["PmtState"]; DataTable table = BaseDataHelper.CloneDataTable(dt); return table; } else { return null; } } } #endregion #endregion #endregion #region 静态方法定义 #region 界面处理业务 #region 获取控件所属设备单元编号 /// /// 获取控件所属设备单元编号 /// /// 控件参数 /// 返回控件所属设备单元 public static string GetUnitNumByControl(Control c) { if (c == null) { return String.Empty; } if (c.Parent == null) { return String.Empty; } else { if (c.Parent.Tag == null) { return GetUnitNumByControl(c.Parent); } else { return c.Parent.Tag as string; } } } #endregion #region 获取控件自身设备单元编号 /// /// 获取控件自身设备单元编号 /// /// 控件参数 /// 返回控件自身设备单元 public static string GetItselfUnitNumByControl(Control c) { if (c == null) { return String.Empty; } else { if (c.Tag == null) { return String.Empty; } else { return c.Tag as string; } } } #endregion #region 网格头辅助方法-如果为0则转换为null /// /// 如果为0则转换为null /// /// /// /// public static void Is0ToNull(DataGridView dgv, int rowInx, int colInx) { if (rowInx >= 0 && colInx >= 0 && (dgv.Columns[colInx] is DataGridViewTextBoxColumn) && ( ((DataGridViewTextBoxColumn)dgv.Columns[colInx]).ValueType == typeof(double) || ((DataGridViewTextBoxColumn)dgv.Columns[colInx]).ValueType == typeof(decimal) || ((DataGridViewTextBoxColumn)dgv.Columns[colInx]).ValueType == typeof(int))) { if (dgv.Rows[rowInx].Cells[colInx].Value != null && dgv.Rows[rowInx].Cells[colInx].Value != DBNull.Value) { double d = 0.0; if (!double.TryParse(dgv.Rows[rowInx].Cells[colInx].Value.ToString(), out d)) { dgv.CancelEdit(); } else if (d == 0) { dgv.Rows[rowInx].Cells[colInx].Value = DBNull.Value; } } } } #endregion #region 网格头辅助方法-初始化网格控件事件 /// /// 初始化网格控件事件 /// /// public static void InitDataGridEvent(DataGridView grid) { grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None; grid.DataError -= grid_DataError; grid.DataError += grid_DataError; //grid.CellValueChanged -= grid_CellValueChanged; //grid.CellValueChanged += grid_CellValueChanged; grid.CellValidating -= grid_CellValidating; grid.CellValidating += grid_CellValidating; grid.CellMouseDown -= grid_CellMouseDown; grid.CellMouseDown += grid_CellMouseDown; grid.SortCompare -= grid_SortCompare; grid.SortCompare += grid_SortCompare; grid.AllowUserToAddRows = false; grid.ReadOnly = false; grid.AutoGenerateColumns = false; grid.AllowUserToResizeRows = false; grid.EditMode = System.Windows.Forms.DataGridViewEditMode.EditOnEnter; } #region 事件处理方法 private static void grid_DataError(object sender, DataGridViewDataErrorEventArgs e) { DataGridView dgv = (DataGridView)sender; dgv.CancelEdit(); e.Cancel = true; } private static void grid_CellValueChanged(object sender, DataGridViewCellEventArgs e) { DataGridView dgv = (DataGridView)sender; if (dgv.Columns[e.ColumnIndex].DataPropertyName == "MaterialName") { dgv.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Value; } Is0ToNull(dgv, e.RowIndex, e.ColumnIndex); } private static void grid_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { if (string.IsNullOrWhiteSpace(e.FormattedValue.ToString())) { return; } DataGridView dgv = (DataGridView)sender; Is0ToNull(dgv, e.RowIndex, e.ColumnIndex); } private static void grid_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) { if (e.Button == MouseButtons.Right) { if (e.RowIndex >= 0) { DataGridView dgv = (DataGridView)sender; dgv.ClearSelection(); dgv.Rows[e.RowIndex].Selected = true; dgv.CurrentCell = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex]; } } } private static void grid_SortCompare(object sender, DataGridViewSortCompareEventArgs e) { DataGridView dgv = (DataGridView)sender; e.SortResult = Convert.ToInt32(dgv.Rows[e.RowIndex1].Cells[0].Value.ToString()) - Convert.ToInt32(dgv.Rows[e.RowIndex2].Cells[0].Value.ToString()); e.Handled = true; } #endregion #endregion #region 根据GridView控件是否为只读,判断当前配方是否处于编辑状态 /// /// 根据GridView控件是否为只读,判断当前配方是否处于编辑状态 /// public static bool IsEditMode(Mesnac.Action.Base.BaseAction baseAction) { bool flag = true; List gridList = baseAction.GetTControls(); foreach (Mesnac.Controls.Default.MCDataGridView grid in gridList) { string unitNum = RecipeHelper.GetUnitNumByControl(grid); if (Basic.BasicHelper.BasEquip.StrUnitList.Contains(unitNum) && grid.ReadOnly == true) { flag = false; break; } } return flag; } #endregion #region 禁用、启用界面控件 /// /// 禁用、启用界面控件 /// /// Action对象 /// true为启用,false为禁用 public static void SetRecipeUIEnabled(Mesnac.Action.Base.BaseAction baseAction, bool flag) { foreach (Mesnac.Controls.Base.IBaseControl control in baseAction.GetAllMCControls()) { if (baseAction.DbOptionTypesIsModify(control) && !control.MCKey.Contains("[PmtRecipe].[GUID]") && !control.MCKey.Contains("[PmtRecipe].[RecipeType].Query")) { string unitNum = GetUnitNumByControl(control as Control); if (!String.IsNullOrEmpty(unitNum)) { if (Basic.BasicHelper.BasEquip.StrUnitList.Contains(unitNum.Trim())) { control.MCEnabled = flag; } } else { control.MCEnabled = flag; } } } List gridList = baseAction.GetTControls(); foreach (Mesnac.Controls.Default.MCDataGridView grid in gridList) { string unitNum = GetUnitNumByControl(grid as Control); if (Basic.BasicHelper.BasEquip.StrUnitList.Contains(unitNum.Trim())) { grid.ReadOnly = !flag; if (grid.ContextMenuStrip != null) { grid.ContextMenuStrip.Enabled = flag; } if (grid.MCKey != null && (grid.MCKey.Contains("[PmtMixWeight]") || grid.MCKey.Contains("[PmtMillWeight]"))) { if (grid.Columns.Contains("WeightID")) { grid.Columns["WeightID"].ReadOnly = true; } if (grid.Columns.Contains("MaterialCode")) { grid.Columns["MaterialCode"].ReadOnly = true; } if (grid.Columns.Contains("MaterialIKindName")) { grid.Columns["MaterialIKindName"].ReadOnly = true; } } if (grid.MCKey != null && (grid.MCKey.Contains("[PmtMixStep]") || grid.MCKey.Contains("[PmtMillStep]") || grid.MCKey.Contains("[PmtMixTempCurve]"))) { if (grid.Columns.Contains("MixStep")) { grid.Columns["MixStep"].ReadOnly = true; } if (grid.Columns.Contains("MillStep")) { grid.Columns["MillStep"].ReadOnly = true; } if (grid.Columns.Contains("SeqIndex")) { grid.Columns["SeqIndex"].ReadOnly = true; } } } } } #endregion #region 重置选中的配方 /// /// 重置选中的配方 /// /// 当前action对象 /// 要选中的配方 //public static void ResetSelectRecipe(BaseAction action, Entity.Pmt_recipe defaultRecipe) //{ // Control cmbRecipeMaterialGUID = action.GetControlById("cbMaterialGUID"); // Control cmbRecipeVersion = action.GetControlById("cbRecipeVersion"); // if (cmbRecipeMaterialGUID == null || !(cmbRecipeMaterialGUID is ComboBox)) // { // ICSharpCode.Core.LoggingService.Warn("配方管理-选择配方-缺少配方物料列表组合框控件!"); // return; // } // if (cmbRecipeVersion == null || !(cmbRecipeVersion is ComboBox)) // { // ICSharpCode.Core.LoggingService.Warn("配方管理-选择配方-缺少配方版本列表组合框控件!"); // return; // } // ComboBox cbRecipeMaterialGUID = cmbRecipeMaterialGUID as ComboBox; // ComboBox cbRecipeVersion = cmbRecipeVersion as ComboBox; // Entity.SimplePmtRecipe originalSelectRecipe = cbRecipeMaterialGUID.SelectedItem as Entity.SimplePmtRecipe; // string originalRecipeVersion = cbRecipeVersion.SelectedItem as string; // int recipeType = BaseDataHelper.GetSysValue("Recipe.LastRecipeType", 0); // int recipeState = BaseDataHelper.GetSysValue("Recipe.LastRecipeState", 1); // cbRecipeMaterialGUID.Text = String.Empty; // List lst = TechnicalHelper.GetRecipeMaterialListPY(recipeType, recipeState, String.Empty); // (cmbRecipeMaterialGUID as Mesnac.Controls.Base.IBaseControl).IsEventValid = false; // (cmbRecipeVersion as Mesnac.Controls.Base.IBaseControl).IsEventValid = false; // cbRecipeMaterialGUID.DataSource = null; // cbRecipeVersion.DataSource = null; // (cmbRecipeMaterialGUID as Mesnac.Controls.Base.IBaseControl).IsEventValid = true; // (cmbRecipeVersion as Mesnac.Controls.Base.IBaseControl).IsEventValid = true; // cbRecipeMaterialGUID.Items.Clear(); // cbRecipeVersion.Items.Clear(); // foreach (Entity.SimplePmtRecipe r in lst) // { // cbRecipeMaterialGUID.Items.Add(r); // } // if (defaultRecipe != null && !String.IsNullOrEmpty(defaultRecipe.MaterialGUID)) // { // foreach (object item in cbRecipeMaterialGUID.Items) // { // if ((item as Entity.SimplePmtRecipe).MaterialGUID == defaultRecipe.MaterialGUID) // { // cbRecipeMaterialGUID.SelectedItem = item; // break; // } // } // } // else // { // bool flag = false; // if (originalSelectRecipe != null) // { // foreach (object item in cbRecipeMaterialGUID.Items) // { // if ((item as Entity.SimplePmtRecipe).MaterialGUID.Equals(originalSelectRecipe.MaterialGUID, StringComparison.CurrentCultureIgnoreCase)) // { // cbRecipeMaterialGUID.SelectedItem = item; // flag = true; // break; // } // } // } // if (!flag) // { // //如果未设置默认选中的项,则默认选中第一项 // foreach (object item in cbRecipeMaterialGUID.Items) // { // cbRecipeMaterialGUID.SelectedItem = item; // break; // } // } // } // if (defaultRecipe != null) // { // cbRecipeVersion.SelectedItem = defaultRecipe.RecipeVersion; // } // else // { // if (!String.IsNullOrEmpty(originalRecipeVersion)) // { // cbRecipeVersion.SelectedItem = originalRecipeVersion; // } // } //} #endregion #region 设置(网格控件)的表头、字体和行样式 /// /// 设置(网格控件)的表头、字体和行样式 /// /// public static void SetDataGridViewStyle(DataGridView grid) { lock (String.Empty) { try { if (grid == null) { ICSharpCode.Core.LoggingService.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.Error("【配方管理】设置(网格控件)的表头、字体和行样式SetDataGridViewStyle失败:" + ex.Message); } } } #endregion #endregion #region 配方数据业务 #region 配方业务 #region 查询业务 #region GenerateNextRecipeCode 获取下一个可用的配方编码 (1位物料大类、2位物料细类、10位流水) /// /// 获取下一个可用的配方编码 (13位物料编码、10位版本号) /// /// 配方物料编码 /// 配方版本 /// 返回配方编码 public static string GenerateNextRecipeCode(string recipeMaterialCode, string recipeVersion) { string result = String.Format("{0}{1}", recipeMaterialCode, recipeVersion); return result; } #endregion #region GenerateNextRecipeVersion 根据配方物料GUID获取下一个可用的配方版本号 /// /// 根据配方物料GUID获取下一个可用的配方版本号 /// /// 配方物料GUID /// 返回下一个可用的配方版本号 public static string GenerateNextRecipeVersion(string recipeMaterialGUID) { DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); if (dbHelper == null) { throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError); } string sqlstr = "select MAX(RecipeVersion) from PmtRecipe where MaterialGUID = @MaterialGUID"; dbHelper.CommandType = CommandType.Text; dbHelper.ClearParameter(); dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@MaterialGUID", recipeMaterialGUID); object result = dbHelper.ToScalar(); if (Mesnac.Basic.DataProcessor.IsNullOrEmpty(result)) { return String.Format("{0:yyyyMMdd}01", DateTime.Now); } else { string recipeVersion = result.ToString(); if (String.Format("{0:yyyyMMdd}", DateTime.Now) == recipeVersion.Substring(0,8)) { int serialNum = Convert.ToInt32(recipeVersion.Substring(8, 2)); serialNum++; return String.Format("{0:yyyyMMdd}", DateTime.Now) + Mesnac.Basic.DataProcessor.FillZero(serialNum.ToString(), 2); } else { return String.Format("{0:yyyyMMdd}01", DateTime.Now); } } } #endregion #region 判断某个配方是否存在 /// /// 判断某个配方是否存在(根据ID) /// /// 配方ID /// 存在返回true,不存在返回false public static bool IsExists(string recipeID) { 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(recipe_Id) from Base_RecipeInfo where recipe_Id = @RecipeID"; dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@RecipeID", recipeID); 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; } /// /// 判断某个配方是否存在 /// /// 配方名称 /// public static bool IsExistsName(string recipeName) { 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(recipe_Name) from Base_RecipeInfo where recipe_Name = @recipe_Name"; dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@recipe_Name", recipeName); 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 根据配方类型和配方状态获取配方列表 /// /// 根据配方类型和配方状态获取配方列表 /// /// 配方类型 /// 配方状态 /// 返回符合条件的配方列表 public static DataTable GetRecipeListForSelect(int recipeType, string recipeState) { DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); if (dbHelper == null) { throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError); } string sqlstr = String.Empty; if (String.IsNullOrEmpty(recipeState)) { sqlstr = @"select * from ( select '' as GUID,'{0}' as ShowName, GETDATE() as LastModifyTime union ALL select TA.GUID,rtrim(TA.RecipeMaterialName)+'('+rtrim(isnull(TB.ItemName,''))+')'+'['+rtrim(TA.RecipeVersion)+']' as ShowName, TA.LastModifyTime from PmtRecipe TA left join SysCode TB on TA.RecipeType = TB.ItemCode where TA.DeleteFlag='0' and TA.RecipeType = @RecipeType and TB.TypeID='PmtType' ) as temp order by LastModifyTime desc"; } else { sqlstr = @"select * from ( select '' as GUID,'{0}' as ShowName, GETDATE() as LastModifyTime union ALL select TA.GUID,rtrim(TA.RecipeMaterialName)+'('+rtrim(isnull(TB.ItemName,''))+')'+'['+rtrim(TA.RecipeVersion)+']' as ShowName, TA.LastModifyTime from PmtRecipe TA left join SysCode TB on TA.RecipeType = TB.ItemCode where TA.DeleteFlag='0' and TA.RecipeType = @RecipeType and TA.RecipeState = @RecipeState and TB.TypeID='PmtType' ) as temp order by LastModifyTime desc"; } sqlstr = String.Format(sqlstr, Mesnac.Basic.LanguageHelper.PleaseSelect); dbHelper.CommandType = CommandType.Text; dbHelper.ClearParameter(); dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@RecipeType", recipeType); if (!String.IsNullOrEmpty(recipeState)) { dbHelper.AddParameter("@RecipeState", recipeState); } DataTable dt = dbHelper.ToDataTable(); return dt; } #endregion #region 根据配方物料GUID和配方版本查询配方GUID /// /// 根据配方物料GUID和配方版本查询配方GUID /// /// 配方物料GUID /// 配方版本 /// 返回对应的配方GUID public static string GetRecipeGUID(string recipeMaterialGUID, string recipeVersion) { 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 = "select GUID from PmtRecipe where MaterialGUID = @MaterialGUID and RecipeVersion = @RecipeVersion"; dbHelper.CommandText = sqlstr; dbHelper.ClearParameter(); dbHelper.AddParameter("@MaterialGUID", recipeMaterialGUID); dbHelper.AddParameter("@RecipeVersion", recipeVersion); object result = dbHelper.ToScalar(); if (!Mesnac.Basic.DataProcessor.IsNullOrEmpty(result)) { return result as string; } return String.Empty; } #endregion #region 判断是否存在某种物料的配方 /// /// 判断是否存在某种物料的配方 /// /// 配方物料GUID /// 存在返回true,不存在返回false public static bool ExistsRecipeMaterialGUID(string recipeMaterialGUID) { 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 = "select count(GUID) from PmtRecipe where DeleteFlag = '0' and MaterialGUID = @MaterialGUID"; dbHelper.CommandText = sqlstr; dbHelper.ClearParameter(); dbHelper.AddParameter("@MaterialGUID", recipeMaterialGUID); object result = dbHelper.ToScalar(); if (!Mesnac.Basic.DataProcessor.IsNullOrEmpty(result)) { int intResult = 0; int.TryParse(result.ToString(), out intResult); if (intResult > 0) { return true; } } return false; } #endregion #region 查询配方主信息 /// /// 获取所有配方列表 /// public static DataTable GetAllRecipeDT() { 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 *, case Recipe_Verify when '1' then '是' else '否' end as 'RecipeVerify' from Pmt_recipe order by End_datetime desc"; dbHelper.CommandText = sqlstr; DataTable dt = dbHelper.ToDataTable(); return dt; } /// /// 根据配方名称获取配方信息 /// /// 配方名称 /// 返回符合条件的配方 public static Base_RecipeInfo GetRecipeByName(string recipeName) { Base_RecipeInfo pmtRecipe = new Base_RecipeInfo(); 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 top 1 * FROM Base_RecipeInfo WHERE recipe_Name = @Recipe_Name"; dbHelper.CommandText = strSql; dbHelper.ClearParameter(); dbHelper.AddParameter("@Recipe_Name", recipeName); DataTable table = dbHelper.ToDataTable(); if (table != null && table.Rows.Count > 0) { foreach (DataRow dr in table.Rows) { pmtRecipe = ConvertDataRowToBaseRepiceInfo(dr); break; } } return pmtRecipe; } /// /// 根据配方ID获取Base_RecipeInfo /// /// /// public static Base_RecipeInfo GetRecipeById(string recipeId) { Base_RecipeInfo pmtRecipe = new Base_RecipeInfo(); 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 top 1 * FROM Base_RecipeInfo WHERE recipe_Id = @recipeId"; dbHelper.CommandText = strSql; dbHelper.ClearParameter(); dbHelper.AddParameter("@recipeId", recipeId); DataTable table = dbHelper.ToDataTable(); if (table != null && table.Rows.Count > 0) { foreach (DataRow dr in table.Rows) { pmtRecipe = ConvertDataRowToBaseRepiceInfo(dr); break; } } return pmtRecipe; } /// /// 获取配方主信息 /// /// 配方GUID /// 返回配方主信息DataRow public static DataRow GetRecipeByGUID(string recipeGUID) { return BaseDataHelper.GetDataRowByTableAndGUID("Pmt_recipe", recipeGUID); } /// /// 获取配方主信息 /// /// 配方GUID /// 配方版本 /// 返回配方主信息DataRow public static DataRow GetRecipeByGUID(string recipeGUID, string recipeVersion) { 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 PmtRecipe where GUID = @RecipeGUID and RecipeVersion = @RecipeVersion"; dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@RecipeGUID", recipeGUID); dbHelper.AddParameter("@RecipeVersion", recipeVersion); DataTable dt = dbHelper.ToDataTable(); if (dt != null && dt.Rows.Count > 0) { return dt.Rows[0]; } else { return null; } } /// /// 获取配方主信息 /// /// 配方物料名称 /// 配方版本 /// 返回配方主信息DataRow public static DataRow GetRecipeByRecipeMaterialName(string recipeMaterialName, string recipeVersion) { 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 PmtRecipe where RecipeMaterialName = @RecipeMaterialName and RecipeVersion = @RecipeVersion"; dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@RecipeMaterialName", recipeMaterialName); dbHelper.AddParameter("@RecipeVersion", recipeVersion); DataTable dt = dbHelper.ToDataTable(); if (dt != null && dt.Rows.Count > 0) { return dt.Rows[0]; } else { return null; } } /// /// 获取配方主信息实体对象 /// /// 配方GUID /// 返回配方主信息实体对象 public static Entity.Pmt_recipe GetRecipeEntityByGUID(string recipeGUID) { DataRow dr = GetRecipeByGUID(recipeGUID); Entity.Pmt_recipe recipe = ConvertDataRowToRecipeEntity(dr); return recipe; } /// /// 获取配方主信息实体对象 /// /// 配方GUID /// 配方版本 /// 返回配方主信息实体对象 public static Entity.Pmt_recipe GetRecipeEntityByGUID(string recipeGUID, string recipeVersion) { DataRow dr = GetRecipeByGUID(recipeGUID, recipeVersion); Entity.Pmt_recipe recipe = ConvertDataRowToRecipeEntity(dr); return recipe; } /// /// 获取配方主信息实体对象 /// /// 配方物料名称 /// 配方版本 /// 返回配方主信息实体对象 public static Entity.Pmt_recipe GetRecipeEntityByRecipeMaterialName(string recipeMaterialName, string recipeVersion) { DataRow dr = GetRecipeByRecipeMaterialName(recipeMaterialName, recipeVersion); Entity.Pmt_recipe recipe = ConvertDataRowToRecipeEntity(dr); return recipe; } /// /// 把DataRow的数据封装至配方实体 /// /// 配方数据行 /// 返回封装号的配方实体对象 public static Entity.Pmt_recipe ConvertDataRowToRecipeEntity(DataRow dr) { if (dr != null) { Entity.Pmt_recipe recipe = new Entity.Pmt_recipe(); recipe.ID = Mesnac.Basic.DataProcessor.RowValue(dr, "ID", String.Empty); recipe.Equip_Code = Mesnac.Basic.DataProcessor.RowValue(dr, "Equip_Code", String.Empty); recipe.Recipe_Code = Mesnac.Basic.DataProcessor.RowValue(dr, "Recipe_Code", String.Empty); recipe.Version = Mesnac.Basic.DataProcessor.RowValue(dr, "Version", String.Empty); recipe.Recipe_Name = Mesnac.Basic.DataProcessor.RowValue(dr, "Recipe_Name", String.Empty); recipe.Mixer_line = Mesnac.Basic.DataProcessor.RowValue(dr, "Mixer_line", String.Empty); recipe.Recipe_type = Mesnac.Basic.DataProcessor.RowValue(dr, "Recipe_type", 0); recipe.Recipe_Used = Mesnac.Basic.DataProcessor.RowValue(dr, "Recipe_Used", 0); recipe.Recipe_Verify = Mesnac.Basic.DataProcessor.RowValue(dr, "Recipe_Verify", 0); recipe.Total_Weight = Mesnac.Basic.DataProcessor.RowValue(dr, "Total_Weight", 0.0M); recipe.Total_Error = Mesnac.Basic.DataProcessor.RowValue(dr, "Total_Error", 0.0M); recipe.End_datetime = Mesnac.Basic.DataProcessor.RowValue(dr, "End_datetime", String.Empty); recipe.Remark = Mesnac.Basic.DataProcessor.RowValue(dr, "Remark", String.Empty); recipe.Attach_User = Mesnac.Basic.DataProcessor.RowValue(dr, "Attach_User", 0); recipe.GroupBags = Mesnac.Basic.DataProcessor.RowValue(dr, "GroupBags", 0); recipe.IF_FLAG = Mesnac.Basic.DataProcessor.RowValue(dr, "IF_FLAG", 0); recipe.Validity = Mesnac.Basic.DataProcessor.RowValue(dr, "Validity", 0); return recipe; } else { return null; } } #endregion #region 查询配方物料GUID /// /// 查询配方物料GUID /// /// 配方物料名称 /// 配方版本 /// 返回对应的配方物料GUID public static string GetRecipeMaterialGUID(string recipeMaterialName, string recipeVersion) { 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 = "select top 1 MaterialGUID from PmtRecipe where RecipeMaterialName = @RecipeMaterialName and RecipeVersion = @RecipeVersion"; dbHelper.CommandText = sqlstr; dbHelper.ClearParameter(); dbHelper.AddParameter("@RecipeMaterialName", recipeMaterialName); dbHelper.AddParameter("@RecipeVersion", recipeVersion); object result = dbHelper.ToScalar(); if (result != null && result != System.DBNull.Value) { return result.ToString(); } else { return String.Empty; } } #endregion #endregion #region 保存业务 #region 保存配方主信息 /// /// 保存配方主信息 /// /// 配方主信息对象 /// 返回当前配方的GUID public static string SaveRecipe(Entity.Pmt_recipe recipe) { #region 每次保存都生成一个新的配方版本 recipe.ID = String.Empty; recipe.Version = RecipeHelper.GenerateNextRecipeVersion(recipe.ID); //每次生成一个新版本 recipe.Recipe_Code = RecipeHelper.GenerateNextRecipeCode(recipe.Recipe_Code, recipe.Version); //设置配方编码为配方物料编码+配方版本号 #endregion 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 = String.Empty; if (string.IsNullOrEmpty(recipe.ID) || !IsExists(recipe.ID)) { //添加新配方 sqlstr = @"insert into PmtRecipe(GUID, MaterialGUID, RecipeMaterialCode, RecipeMaterialName, RecipeCode, RecipeName, RecipeDesc, RecipeVersion, RecipeType, RecipeState, ShelfLotCount, LotTotalWeight, CreateTime, CreateActor, LastModifyTime, LastModifyActor, DataSource, DeleteFlag) values(@GUID, @MaterialGUID, @RecipeMaterialCode, @RecipeMaterialName, @RecipeCode, @RecipeName, @RecipeDesc, @RecipeVersion, @RecipeType, @RecipeState, @ShelfLotCount, @LotTotalWeight, @CreateTime, @CreateActor, @LastModifyTime, @LastModifyActor, @DataSource, @DeleteFlag)"; recipe.ID = Guid.NewGuid().ToString().ToUpper(); } else { //更新原有配方 sqlstr = @"update PmtRecipe set MaterialGUID=@MaterialGUID, RecipeMaterialCode=@RecipeMaterialCode, RecipeMaterialName=@RecipeMaterialName, RecipeCode=@RecipeCode, RecipeName=@RecipeName, RecipeDesc=@RecipeDesc, RecipeVersion=@RecipeVersion, RecipeType=@RecipeType, RecipeState=@RecipeState, ShelfLotCount=@ShelfLotCount, LotTotalWeight=@LotTotalWeight, CreateTime=@CreateTime, CreateActor=@CreateActor, LastModifyTime=@LastModifyTime, LastModifyActor=@LastModifyActor, DataSource=@DataSource, DeleteFlag=@DeleteFlag where GUID = @GUID"; } dbHelper.ClearParameter(); dbHelper.CommandText = sqlstr; dbHelper.ExecuteNonQuery(); return recipe.ID; } #endregion /// /// 保存配方的物料信息 /// /// 保存配方的物料信息 public static void InsertWeigh(Base_RecipeMaterial pmt_Weigh) { 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 = String.Empty; if (!IsExistsRecipeMaterial(pmt_Weigh.recipeId, pmt_Weigh.materialName)) { //添加新配方 sqlstr = @"insert into Base_RecipeMaterial (recipe_Id, material_Id, material_Nme, material_Weight, put_Time, edit_User) VALUES (@recipe_Id, @material_Id, @material_Nme, @material_Weight, @put_Time, @edit_User)"; dbHelper.ClearParameter(); dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@recipe_Id", pmt_Weigh.recipeId); dbHelper.AddParameter("@material_Id", pmt_Weigh.materialId); dbHelper.AddParameter("@material_Nme", pmt_Weigh.materialName); dbHelper.AddParameter("@material_Weight", pmt_Weigh.materialWeight); dbHelper.AddParameter("@put_Time", pmt_Weigh.putTime); dbHelper.AddParameter("@edit_User", pmt_Weigh.editUser); } else { sqlstr = @"update Base_RecipeMaterial set material_Weight=@material_Weight where recipe_Id = @recipe_Id and material_Nme = @material_Nme "; dbHelper.ClearParameter(); dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@recipe_Id", pmt_Weigh.recipeId); dbHelper.AddParameter("@material_Nme", pmt_Weigh.materialName); dbHelper.AddParameter("@material_Weight", pmt_Weigh.materialWeight); } dbHelper.ExecuteNonQuery(); } /// /// 删除配方的物料信息 /// /// 配方ID public static void DelWeighByRecipeID(string recipeID) { 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 = String.Empty; sqlstr = @"delete from Base_RecipeMaterial where recipe_Id = @Recipe_ID"; dbHelper.ClearParameter(); dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@Recipe_ID", recipeID); dbHelper.ExecuteNonQuery(); } public static void DelCratParamByRecipeId(string recipeId) { 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 = String.Empty; sqlstr = @"delete from Base_RecipeCratParam where recipe_Id = @Recipe_ID"; dbHelper.ClearParameter(); dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@Recipe_ID", recipeId); dbHelper.ExecuteNonQuery(); } #endregion #region 删除业务 #region 删除配方信息 /// /// 删除配方信息 /// /// 配方ID public static void DeleteRecipe(string recipeID) { 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 = "delete from Base_RecipeInfo where recipe_Id = @ID"; dbHelper.ClearParameter(); dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@ID", recipeID); dbHelper.ExecuteNonQuery(); } #endregion #endregion #region 配方导入、导出 #region 配方导入 #region 导入配方主表 /// /// 导入配方主表 /// /// 要导入的文件名 /// 是否要清空原有数据 /// 成功返回true,失败返回false public static bool ImportPmtRecipe(string fileName, bool isClearOriginal) { try { DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); if (dbHelper == null) { throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError); } #region 清空原有数据 if (isClearOriginal) { dbHelper.CommandType = CommandType.Text; string sqlstr = "truncate table PmtRecipe"; dbHelper.ClearParameter(); dbHelper.CommandText = sqlstr; dbHelper.ExecuteNonQuery(); ICSharpCode.Core.LoggingService.Debug("在导入配方主表时,清空了原有配方主表数据!"); } #endregion #region 导入数据 int count = 0; string insertSql = @"delete from PmtRecipe where GUID=@GUID; insert into PmtRecipe(GUID, MaterialGUID, RecipeMaterialCode, RecipeMaterialName, RecipeCode, RecipeName, RecipeDesc, RecipeVersion, RecipeType, RecipeUserVersion, RecipeState, LotDoneTime, ShelfLotCount, LotTotalWeight, AuditFlag, AuditUser, AuditDateTime, StartDatetime, EndDatetime, CreateTime, CreateActor, LastModifyTime, LastModifyActor, DataSource, DeleteFlag, Remark) values(@GUID, @MaterialGUID, @RecipeMaterialCode, @RecipeMaterialName, @RecipeCode, @RecipeName, @RecipeDesc, @RecipeVersion, @RecipeType, @RecipeUserVersion, @RecipeState, @LotDoneTime, @ShelfLotCount, @LotTotalWeight, @AuditFlag, @AuditUser, @AuditDateTime, @StartDatetime, @EndDatetime, @CreateTime, @CreateActor, @LastModifyTime, @LastModifyActor, @DataSource, @DeleteFlag, @Remark)"; DataTable dt = Mesnac.Basic.SerializeHandler.Deserialize(fileName); if (dt != null && dt.Rows.Count > 0) { foreach (DataRow dr in dt.Rows) { dbHelper.ClearParameter(); dbHelper.CommandText = insertSql; dbHelper.AddParameter("@GUID", dr["GUID"]); dbHelper.AddParameter("@MaterialGUID", dr["MaterialGUID"]); dbHelper.AddParameter("@RecipeMaterialCode", dr["RecipeMaterialCode"]); dbHelper.AddParameter("@RecipeMaterialName", dr["RecipeMaterialName"]); dbHelper.AddParameter("@RecipeCode", dr["RecipeCode"]); dbHelper.AddParameter("@RecipeName", dr["RecipeName"]); dbHelper.AddParameter("@RecipeDesc", dr["RecipeDesc"]); dbHelper.AddParameter("@RecipeVersion", dr["RecipeVersion"]); dbHelper.AddParameter("@RecipeType", dr["RecipeType"]); dbHelper.AddParameter("@RecipeUserVersion", dr["RecipeUserVersion"]); dbHelper.AddParameter("@RecipeState", dr["RecipeState"]); dbHelper.AddParameter("@LotDoneTime", dr["LotDoneTime"]); dbHelper.AddParameter("@ShelfLotCount", dr["ShelfLotCount"]); dbHelper.AddParameter("@LotTotalWeight", dr["LotTotalWeight"]); dbHelper.AddParameter("@AuditFlag", dr["AuditFlag"]); dbHelper.AddParameter("@AuditUser", dr["AuditUser"]); dbHelper.AddParameter("@AuditDateTime", dr["AuditDateTime"]); dbHelper.AddParameter("@StartDatetime", dr["StartDatetime"]); dbHelper.AddParameter("@EndDatetime", dr["EndDatetime"]); dbHelper.AddParameter("@CreateTime", dr["CreateTime"]); dbHelper.AddParameter("@CreateActor", dr["CreateActor"]); dbHelper.AddParameter("@LastModifyTime", dr["LastModifyTime"]); dbHelper.AddParameter("@LastModifyActor", dr["LastModifyActor"]); dbHelper.AddParameter("@DataSource", dr["DataSource"]); dbHelper.AddParameter("@DeleteFlag", dr["DeleteFlag"]); dbHelper.AddParameter("@Remark", dr["Remark"]); dbHelper.ExecuteNonQuery(); count++; System.Threading.Thread.Sleep(2); //中间休眠2毫秒 } } #endregion ICSharpCode.Core.LoggingService.Debug("导入配方主表完毕,导入记录数:" + count + ",导入文件名:" + fileName); return true; } catch(Exception ex) { ICSharpCode.Core.LoggingService.Error("导入配方主表异常:" + ex.Message, ex); return false; } } #endregion #region 导入下辅机称量物料数据 /// /// 导入下辅机称量物料数据 /// /// 要导入的文件名 /// 是否清除原有数据 /// 成功返回true,失败返回false public static bool ImportPmtMillWeight(string fileName, bool isClearOriginal) { try { DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); if (dbHelper == null) { throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError); } #region 清空原有数据 if (isClearOriginal) { dbHelper.CommandType = CommandType.Text; string sqlstr = "truncate table PmtMillWeight"; dbHelper.ClearParameter(); dbHelper.CommandText = sqlstr; dbHelper.ExecuteNonQuery(); ICSharpCode.Core.LoggingService.Debug("在导入下辅机称量物料数据时,清空了原有下辅机称量物料数据!"); } #endregion #region 导入数据 int count = 0; string insertSql = @"delete from PmtMillWeight where GUID=@GUID or (MillWeightHeadGUID=@MillWeightHeadGUID and WeightID=@WeightID); insert into PmtMillWeight(GUID, MillWeightHeadGUID, WeightID, ActCode, MaterialGUID, MaterialCode, MaterialName, SetWeight, SetUpTolerance, SetLowTolerance, MaterialType, NeedMaterialCode, ChanYongFlag, AutoPloy, Remark) values(@GUID, @MillWeightHeadGUID, @WeightID, @ActCode, @MaterialGUID, @MaterialCode, @MaterialName, @SetWeight, @SetUpTolerance, @SetLowTolerance, @MaterialType, @NeedMaterialCode, @ChanYongFlag, @AutoPloy, @Remark)"; DataTable dt = Mesnac.Basic.SerializeHandler.Deserialize(fileName); if (dt != null && dt.Rows.Count > 0) { foreach (DataRow dr in dt.Rows) { dbHelper.ClearParameter(); dbHelper.CommandText = insertSql; dbHelper.AddParameter("@GUID", dr["GUID"]); dbHelper.AddParameter("@MillWeightHeadGUID", dr["MillWeightHeadGUID"]); dbHelper.AddParameter("@WeightID", dr["WeightID"]); dbHelper.AddParameter("@ActCode", dr["ActCode"]); dbHelper.AddParameter("@MaterialGUID", dr["MaterialGUID"]); dbHelper.AddParameter("@MaterialCode", dr["MaterialCode"]); dbHelper.AddParameter("@MaterialName", dr["MaterialName"]); dbHelper.AddParameter("@SetWeight", dr["SetWeight"]); dbHelper.AddParameter("@SetUpTolerance", dr["SetUpTolerance"]); dbHelper.AddParameter("@SetLowTolerance", dr["SetLowTolerance"]); dbHelper.AddParameter("@MaterialType", dr["MaterialType"]); dbHelper.AddParameter("@NeedMaterialCode", dr["NeedMaterialCode"]); dbHelper.AddParameter("@ChanYongFlag", dr["ChanYongFlag"]); dbHelper.AddParameter("@AutoPloy", dr["AutoPloy"]); dbHelper.AddParameter("@Remark", dr["Remark"]); dbHelper.ExecuteNonQuery(); count++; System.Threading.Thread.Sleep(2); //中间休眠2毫秒 } } #endregion ICSharpCode.Core.LoggingService.Debug("导入下辅机称量物料数据完毕,导入记录数:" + count + ",导入文件名:" + fileName); return true; } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error("导入下辅机称量物料数据异常:" + ex.Message, ex); return false; } } #endregion #endregion #region 配方导出 #region 导出配方主表 /// /// 导出配方主表 /// /// 要导出的文件名 /// 成功返回true,失败返回false public static bool ExportPmtRecipe(string fileName) { try { 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 = "select * from PmtRecipe"; DataTable dt = dbHelper.GetDataTableBySql(sqlstr); Mesnac.Basic.SerializeHandler.Serialize(dt, fileName); ICSharpCode.Core.LoggingService.Debug("导出配方主表完毕,文件名:" + fileName); return true; } catch(Exception ex) { ICSharpCode.Core.LoggingService.Error("导出配方主表异常:" + ex.Message, ex); return false; } } #endregion #region 导出下辅机称量物料数据 /// /// 导出下辅机称量物料数据 /// /// 要导出的文件名 /// 成功返回true,失败返回false public static bool ExportPmtMillWeight(string fileName) { try { 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 = "select * from PmtMillWeight"; DataTable dt = dbHelper.GetDataTableBySql(sqlstr); Mesnac.Basic.SerializeHandler.Serialize(dt, fileName); ICSharpCode.Core.LoggingService.Debug("导出下辅机称量物料数据完毕,文件名:" + fileName); return true; } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error("导出下辅机称量物料数据异常:" + ex.Message, ex); return false; } } #endregion #endregion #endregion #endregion #endregion #endregion #region 配方打印(报表) #region 获取配方基本信息 PmtRecipe /// /// 通过配方GUID获取配方基本信息 /// /// /// 返回配方基本信息 public static DataTable GetPmtRecipe(string recipeGUID) { DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); if (dbHelper == null) { return new DataTable(); } string sqlStr = @"SELECT TA.*,TB.ItemName AS RecipeTypeName,TC.ItemName AS RecipeStateName FROM PmtRecipe TA LEFT JOIN SysCode TB ON TA.RecipeType=TB.ItemCode AND TB.TypeID='PmtType' LEFT JOIN SysCode TC ON TA.RecipeState=TC.ItemCode AND TC.TypeID='PmtState' WHERE TA.GUID = '" + recipeGUID + "' "; dbHelper.ClearParameter(); dbHelper.CommandType = CommandType.Text; dbHelper.CommandText = sqlStr; DataTable dt = dbHelper.ToDataTable(); return dt; } #endregion #endregion /// /// 获取配方信息 /// /// public static DataTable GetBaseRecipeInfo(string recipeId) { 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 row_number() over (order by edit_Time) as index_Id,recipe_Id,recipe_Name,recipe_Type,recipe_Weight,edit_User,edit_Time,remark, case recipe_State when '1' then '是' else '否' end as 'recipe_State'"; sqlstr += "from Base_RecipeInfo where 1=1"; if (recipeId != "") { sqlstr += "and recipe_Id = @recipeId"; dbHelper.AddParameter("@recipeId", recipeId); } dbHelper.CommandText = sqlstr; DataTable dt = dbHelper.ToDataTable(); return dt; } /// /// 获取配方物料信息 /// /// public static DataTable GetRecipeMaterialInfo(string recipeId) { 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 row_number() over (order by edit_Time) as index_Id,* from Base_RecipeMaterial where 1=1 "; if(recipeId != "") { sqlstr += "and recipe_Id = @recipeId"; dbHelper.AddParameter("@recipeId", recipeId); } dbHelper.CommandText = sqlstr; DataTable dt = dbHelper.ToDataTable(); return dt; } /// /// 获取配方物料信息 /// /// public static List GetRecipeMaterialInfoByRecipeId(string recipeId) { List recipeMaterials = new List(); 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 row_number() over (order by material_Id) as index_Id,* from Base_RecipeMaterial where 1=1 "; if (recipeId != "") { sqlstr += "and recipe_Id = @recipeId"; dbHelper.AddParameter("@recipeId", recipeId); } dbHelper.CommandText = sqlstr; DataTable table = dbHelper.ToDataTable(); if (table != null && table.Rows.Count > 0) { foreach (DataRow dr in table.Rows) { Base_RecipeMaterial recipeMaterial = new Base_RecipeMaterial(); recipeMaterial = ConvertDataRowToBaseRepiceMaterial(dr); recipeMaterials.Add(recipeMaterial); } } return recipeMaterials; } /// /// 根据配方信息、物料信息获取工艺参数 /// /// /// /// public static DataTable GerCratParamByRecipeAndMaterial(string recipeId,string materialId) { 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 sqlstr = "select row_number() over (order by edit_Time) as index_Id,* from Base_RecipeCratParam where 1=1"; if (recipeId != "") { sqlstr += " and recipe_Id = @recipeId"; dbHelper.AddParameter("@recipeId", recipeId); } if (materialId != "") { sqlstr += " and material_Id = @materialId"; dbHelper.AddParameter("@materialId", materialId); } dbHelper.CommandText = sqlstr; DataTable dt = dbHelper.ToDataTable(); return dt; }catch(Exception ex) { return null; } } public static List GerCratParamListByRecipeAndMaterial(string recipeId, string materialId) { try { List recipeCratParams = new List(); 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 row_number() over (order by edit_Time) as index_Id,* from Base_RecipeCratParam where 1=1"; if (recipeId != "") { sqlstr += " and recipe_Id = @recipeId"; dbHelper.AddParameter("@recipeId", recipeId); } if (materialId != "") { sqlstr += " and material_Id = @materialId"; dbHelper.AddParameter("@materialId", materialId); } dbHelper.CommandText = sqlstr; DataTable table = dbHelper.ToDataTable(); if (table != null && table.Rows.Count > 0) { foreach (DataRow dr in table.Rows) { Base_RecipeCratParam recipeCratParam = new Base_RecipeCratParam(); recipeCratParam = ConvertDataRowToBaseRepiceCratParam(dr); recipeCratParams.Add(recipeCratParam); } } return recipeCratParams; } catch (Exception ex) { return null; } } /// /// 保持配方信息 /// /// /// public static string InsertRecipe(Base_RecipeInfo base_RepiceInfo) { 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 = String.Empty; if (!IsExists(base_RepiceInfo.recipeId)) { //添加新配方 sqlstr = @"insert into Base_RecipeInfo(recipe_Id, recipe_Name, recipe_Type, recipe_Weight, recipe_State, edit_User, edit_Time, remark) values (@recipe_Id, @recipe_Name, @recipe_Type, @recipe_Weight, @recipe_State, @edit_User, @edit_Time, @remark)"; } else { //更新原有配方 sqlstr = @"update Base_RecipeInfo set recipe_Id=@recipe_Id, recipe_Name=@recipe_Name, recipe_Type=@recipe_Type, recipe_Weight=@recipe_Weight, recipe_State=@recipe_State, edit_User=@edit_User, edit_Time=@edit_Time, remark=@remark where recipe_Id = @recipe_Id"; } dbHelper.ClearParameter(); dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@recipe_Id", base_RepiceInfo.recipeId); dbHelper.AddParameter("@recipe_Name", base_RepiceInfo.recipeName); dbHelper.AddParameter("@recipe_Type", base_RepiceInfo.recipeType); dbHelper.AddParameter("@recipe_Weight", base_RepiceInfo.recipeWeight); dbHelper.AddParameter("@recipe_State", base_RepiceInfo.recipeState); dbHelper.AddParameter("@edit_User", base_RepiceInfo.editUser); dbHelper.AddParameter("@edit_Time", base_RepiceInfo.editTime); dbHelper.AddParameter("@remark", base_RepiceInfo.remark); dbHelper.ExecuteNonQuery(); return base_RepiceInfo.recipeId; } /// /// Datatable转为Base_RepiceInfo /// /// /// public static Base_RecipeInfo ConvertDataRowToBaseRepiceInfo(DataRow dr) { if (dr != null) { Base_RecipeInfo recipe = new Base_RecipeInfo(); recipe.recipeId = Mesnac.Basic.DataProcessor.RowValue(dr, "recipe_Id", String.Empty); recipe.recipeName = Mesnac.Basic.DataProcessor.RowValue(dr, "recipe_Name", String.Empty); recipe.recipeState = Convert.ToInt32(Mesnac.Basic.DataProcessor.RowValue(dr, "recipe_State", String.Empty)); recipe.recipeType = Mesnac.Basic.DataProcessor.RowValue(dr, "recipe_Type", String.Empty); recipe.recipeWeight = Convert.ToDecimal(Mesnac.Basic.DataProcessor.RowValue(dr, "recipe_Weight", String.Empty)); recipe.editUser = Mesnac.Basic.DataProcessor.RowValue(dr, "edit_User", String.Empty); recipe.remark = Mesnac.Basic.DataProcessor.RowValue(dr, "remark", String.Empty); return recipe; } else { return null; } } public static Base_RecipeMaterial ConvertDataRowToBaseRepiceMaterial(DataRow dr) { if (dr != null) { Base_RecipeMaterial recipe = new Base_RecipeMaterial(); recipe.materialId = Mesnac.Basic.DataProcessor.RowValue(dr, "material_Id", String.Empty); recipe.materialName = Mesnac.Basic.DataProcessor.RowValue(dr, "material_Nme", String.Empty); recipe.recipeId = Mesnac.Basic.DataProcessor.RowValue(dr, "recipe_Id", String.Empty); recipe.materialWeight = Convert.ToDecimal(Mesnac.Basic.DataProcessor.RowValue(dr, "material_Weight", String.Empty)); recipe.putTime = Convert.ToInt32(Mesnac.Basic.DataProcessor.RowValue(dr, "put_Time", String.Empty)); return recipe; } else { return null; } } public static Base_RecipeCratParam ConvertDataRowToBaseRepiceCratParam(DataRow dr) { if (dr != null) { Base_RecipeCratParam recipe = new Base_RecipeCratParam(); recipe.paramId = Mesnac.Basic.DataProcessor.RowValue(dr, "param_Id", String.Empty); recipe.paramName = Mesnac.Basic.DataProcessor.RowValue(dr, "param_Name", String.Empty); recipe.paramValue = Mesnac.Basic.DataProcessor.RowValue(dr, "param_Value", String.Empty); recipe.recipeId = Mesnac.Basic.DataProcessor.RowValue(dr, "recipe_Id", String.Empty); recipe.materialId = Mesnac.Basic.DataProcessor.RowValue(dr, "material_Id", String.Empty); return recipe; } else { return null; } } /// /// 获取配方物料关联的工艺参数 /// /// /// /// public static DataTable GetCratParamByRecipeAndMaterial(string recipeId, string material) { 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 row_number() over (order by edit_Time) as index_Id ,* from Base_RecipeCratParam where 1=1"; if (recipeId != "") { sqlstr += " and recipe_Id = @recipeId"; dbHelper.AddParameter("@recipeId", recipeId); } if (material != "") { sqlstr += " and material_Id = @material"; dbHelper.AddParameter("@material", material); } dbHelper.CommandText = sqlstr; DataTable dt = dbHelper.ToDataTable(); return dt; } /// /// 删除配方物料关联的工艺参数 /// /// public static void DeleteCratParam(string recipeID) { 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 = "delete from Base_RecipeCratParam where recipe_Id = @recipeID "; dbHelper.ClearParameter(); dbHelper.AddParameter("@recipeID", recipeID); dbHelper.CommandText = sqlstr; dbHelper.ExecuteNonQuery(); } /// /// 判断某个配方参数是否存在(根据配方编号及参数名称) /// /// /// public static bool IsExistsCratParam(string recipeID,string param_Name) { 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(recipe_Id) from Base_RecipeCratParam where recipe_Id = @RecipeID and param_Name = @param_Name"; dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@RecipeID", recipeID); dbHelper.AddParameter("@param_Name", param_Name); 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; } /// /// 判断某个配方物料是否存在 /// /// /// /// public static bool IsExistsRecipeMaterial(string recipeID, string material_Nme) { 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(recipe_Id) from Base_RecipeMaterial where recipe_Id = @RecipeID and material_Nme = @material_Nme"; dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@RecipeID", recipeID); dbHelper.AddParameter("@material_Nme", material_Nme); 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; } /// /// 添加工艺参数 /// /// public static void InsertCratParam(Base_RecipeCratParam cratParam) { 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 = String.Empty; if (!IsExistsCratParam(cratParam.recipeId, cratParam.paramName)) { //添加新配方参数 sqlstr = @"insert into Base_RecipeCratParam (param_Id, param_Name, recipe_Id, material_Id, param_Value, edit_User) values (@param_Id, @param_Name, @recipe_Id, @material_Id,@param_Value, @edit_User)"; dbHelper.ClearParameter(); dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@param_Id", String.IsNullOrEmpty(cratParam.paramId) ? System.Guid.NewGuid().ToString("N") : cratParam.paramId); dbHelper.AddParameter("@param_Name", cratParam.paramName); dbHelper.AddParameter("@recipe_Id", cratParam.recipeId); dbHelper.AddParameter("@material_Id", cratParam.materialId); dbHelper.AddParameter("@param_Value", cratParam.paramValue); dbHelper.AddParameter("@edit_User", cratParam.editUser); } else { //更新新配方参数 sqlstr = @"update Base_RecipeCratParam set param_Value=@param_Value where recipe_Id = @recipe_Id and param_Name = @param_Name "; dbHelper.ClearParameter(); dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@param_Name", cratParam.paramName); dbHelper.AddParameter("@recipe_Id", cratParam.recipeId); dbHelper.AddParameter("@param_Value", cratParam.paramValue); } dbHelper.ExecuteNonQuery(); } #region 冠合数据库操作 /// /// 获取配方物料信息 /// /// public static List GetBaseRecipeInfo() { List recipeMaterials = new List(); 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 row_number() over (order by recipe_Name) as index_Id,* from Base_RecipeInfo where 1=1 "; dbHelper.CommandText = sqlstr; DataTable table = dbHelper.ToDataTable(); if (table != null && table.Rows.Count > 0) { foreach (DataRow dr in table.Rows) { Base_RecipeInfo recipeMaterial = new Base_RecipeInfo(); recipeMaterial = ConvertDataRowToBaseRecipeInfo(dr); recipeMaterials.Add(recipeMaterial); } } return recipeMaterials; } public static Base_RecipeInfo ConvertDataRowToBaseRecipeInfo(DataRow dr) { if (dr != null) { Base_RecipeInfo recipe = new Base_RecipeInfo(); recipe.recipeId = Mesnac.Basic.DataProcessor.RowValue(dr, "recipe_Id", String.Empty); recipe.recipeName = Mesnac.Basic.DataProcessor.RowValue(dr, "recipe_Name", String.Empty); recipe.recipeType = Mesnac.Basic.DataProcessor.RowValue(dr, "recipe_Type", String.Empty); return recipe; } else { return null; } } #endregion } }