using Mesnac.Action.ChemicalWeighing.Entity; using Mesnac.Action.ChemicalWeighing.Entity.material; using Mesnac.Action.ChemicalWeighing.Entity.station; using Mesnac.Codd.Session; using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Mesnac.Action.ChemicalWeighing.Station { public class StationHelper { #region 根据工位id获取工位详情信息列表 /// /// 根据工位id获取物料信息列表 /// /// ID /// public static DataTable GetStationSubDataTable(string 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; string strSql = "SELECT * FROM xl_station_sub where MainId = @MainId order by Id"; dbHelper.CommandText = strSql; dbHelper.ClearParameter(); dbHelper.AddParameter("@MainId", Id); DataTable table = dbHelper.ToDataTable(); if (table != null && table.Rows.Count > 0) { return table; } return null; } #endregion #region 根据工位id获取工位详情信息列表 /// /// 根据工位id获取物料信息列表 /// /// ID /// public static List GetStationSubList() { List lst = 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.CommandType = CommandType.Text; string strSql = "SELECT * FROM xl_station_sub order by StationSubName"; dbHelper.CommandText = strSql; dbHelper.ClearParameter(); DataTable table = dbHelper.ToDataTable(); if (table != null && table.Rows.Count > 0) { xl_station_sub entity = null; foreach (DataRow dr in table.Rows) { entity = ConvertDataRowToStation(dr); lst.Add(entity); } } return lst; } #endregion #region 根据工位id获取工位详情信息列表 /// /// 根据工位id获取物料信息列表 /// /// ID /// public static List GetStationSubList(string Id) { List lst = 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.CommandType = CommandType.Text; string strSql = "SELECT * FROM xl_station_sub where MainId=@MainId order by StationSubName"; dbHelper.CommandText = strSql; dbHelper.ClearParameter(); dbHelper.AddParameter("@MainId", Id); DataTable table = dbHelper.ToDataTable(); if (table != null && table.Rows.Count > 0) { xl_station_sub entity = null; foreach (DataRow dr in table.Rows) { entity = ConvertDataRowToStation(dr); lst.Add(entity); } } return lst; } #endregion /// /// 把DataRow数据行转换为xl_weigh实体对象 /// /// 要转换的数据行 /// 返回转换后的计划实体对象 public static xl_station_sub ConvertDataRowToStation(DataRow dr) { if (dr != null) { xl_station_sub station = new xl_station_sub(); station.ID = Mesnac.Basic.DataProcessor.RowValue(dr, "ID", String.Empty); station.MainId = Mesnac.Basic.DataProcessor.RowValue(dr, "MainId", String.Empty); station.StationSubName = Mesnac.Basic.DataProcessor.RowValue(dr, "StationSubName", String.Empty); station.Station_Weight_Medium = Mesnac.Basic.DataProcessor.RowValue(dr, "Station_Weight_Medium", 0.0); station.Station_Weight_Low = Mesnac.Basic.DataProcessor.RowValue(dr, "Station_Weight_Low", 0.0); station.Station_Weight_Advance = Mesnac.Basic.DataProcessor.RowValue(dr, "Station_Weight_Advance", 0.0); station.Station_Speed_Hight = Mesnac.Basic.DataProcessor.RowValue(dr, "Station_Speed_Hight", 0.0); station.Station_Speed_Medium = Mesnac.Basic.DataProcessor.RowValue(dr, "Station_Speed_Medium", 0.0); station.Station_Speed_Low = Mesnac.Basic.DataProcessor.RowValue(dr, "Station_Speed_Low", 0.0); return station; } else { return null; } } /// /// 把DataRow数据行转换为Station实体对象 /// /// 要转换的数据行 /// 返回转换后的计划实体对象 public static xl_station_sub ConvertDataRowToXlStation(DataRow dr) { if (dr != null) { xl_station_sub station = new xl_station_sub(); station.ID = Mesnac.Basic.DataProcessor.RowValue(dr, "ID", String.Empty); station.StationSubName = Mesnac.Basic.DataProcessor.RowValue(dr, "StationSubName", String.Empty); station.Station_Weight_Medium = Mesnac.Basic.DataProcessor.RowValue(dr, "Station_Weight_Medium", 0.0); station.Station_Weight_Low = Mesnac.Basic.DataProcessor.RowValue(dr, "Station_Weight_Low", 0.0); station.Station_Weight_Advance = Mesnac.Basic.DataProcessor.RowValue(dr, "Station_Weight_Advance", 0.0); station.Station_Speed_Hight = Mesnac.Basic.DataProcessor.RowValue(dr, "Station_Speed_Hight", 0.0); station.Station_Speed_Medium = Mesnac.Basic.DataProcessor.RowValue(dr, "Station_Speed_Medium", 0.0); station.Station_Speed_Low = Mesnac.Basic.DataProcessor.RowValue(dr, "Station_Speed_Low", 0.0); return station; } else { return null; } } #region 获取某种配方的版本列表 /// /// 获取某种配方的版本列表 /// /// ID /// 返回对应的版本列表 public static List GetMaterialList(string 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 Material_ID,Material_name from xl_weigh w left join xl_material m on w.Material_ID=m.ID where w.Recipe_ID=@Recipe_ID"; dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@Recipe_ID", ID); DataTable table = dbHelper.ToDataTable(); List lst = new List(); foreach (DataRow row in table.Rows) { Material recipe = new Material(); recipe.Material_ID = Mesnac.Basic.DataProcessor.RowValue(row, "Material_ID", String.Empty); recipe.Material_name = Mesnac.Basic.DataProcessor.RowValue(row, "Material_name", String.Empty); lst.Add(recipe); } return lst; } #endregion /// /// 保存工位主信息 /// /// 配方主信息对象 /// 返回当前配方的GUID public static string InsertStation(Entity.xl_station station) { 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(station.ID)) { //添加新配方 sqlstr = @"INSERT INTO xl_station(ID,StationName,End_datetime)VALUES(@ID,@StationName,@End_datetime)"; } else { //更新原有配方 sqlstr = @"UPDATE xl_station set StationName = @StationName,End_datetime =@End_datetime where ID = @ID"; } dbHelper.ClearParameter(); dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@ID", station.ID); dbHelper.AddParameter("@StationName", station.StationName); dbHelper.AddParameter("@End_datetime", station.End_datetime); dbHelper.ExecuteNonQuery(); return station.ID; } /// /// 判断某个工位是否存在(根据ID) /// /// 配方ID /// 存在返回true,不存在返回false public static bool IsExists(string 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 xl_station where ID = @ID"; dbHelper.CommandText = sqlstr; 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; } /// /// 保存工位信息 /// /// 保存工位信息 public static void InsertStationSub(Entity.xl_station_sub xlstationsub) { 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 = @"INSERT INTO xl_station_sub(ID,MainId,StationSubName,Station_Weight_Medium,Station_Weight_Low,Station_Weight_Advance,Station_Speed_Hight,Station_Speed_Medium,Station_Speed_Low,CreateDateTime)VALUES (@ID,@MainId,@StationSubName,@Station_Weight_Medium,@Station_Weight_Low,@Station_Weight_Advance,@Station_Speed_Hight,@Station_Speed_Medium,@Station_Speed_Low,@CreateDateTime)"; dbHelper.ClearParameter(); dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@ID", Guid.NewGuid().ToString()); dbHelper.AddParameter("@MainId", xlstationsub.MainId); dbHelper.AddParameter("@StationSubName", xlstationsub.StationSubName); dbHelper.AddParameter("@Station_Weight_Medium", xlstationsub.Station_Weight_Medium); dbHelper.AddParameter("@Station_Weight_Low", xlstationsub.Station_Weight_Low); dbHelper.AddParameter("@Station_Weight_Advance", xlstationsub.Station_Weight_Advance); dbHelper.AddParameter("@Station_Speed_Hight", xlstationsub.Station_Speed_Hight); dbHelper.AddParameter("@Station_Speed_Medium", xlstationsub.Station_Speed_Medium); dbHelper.AddParameter("@Station_Speed_Low", xlstationsub.Station_Speed_Low); dbHelper.AddParameter("@CreateDateTime", DateTime.Now); dbHelper.ExecuteNonQuery(); } public static bool PlanExistsRecipeID(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 strSql = "select count(Plan_Id) from xl_plan where Recipe_ID = @Recipe_ID"; dbHelper.CommandText = strSql; dbHelper.AddParameter("@Recipe_ID", recipeID); object result = dbHelper.ToScalar(); if (result != null && result != DBNull.Value && Convert.ToInt32(result) > 0) { return true; } else { return false; } } /// /// 获取工位信息 /// /// 配方名称 /// 返回符合条件的配方 public static DataTable GetStationList() { 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 * FROM xl_station"; dbHelper.CommandText = strSql; dbHelper.ClearParameter(); DataTable table = dbHelper.ToDataTable(); if (table != null && table.Rows.Count > 0) { return table; } return null; } /// /// 根据ID 获取工位信息 /// /// 配方名称 /// 返回符合条件的配方 public static Entity.xl_station GetStation(string ID) { Entity.xl_station station = new Entity.xl_station(); 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 xl_station 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) { station = ConvertDataRowToStationEntity(dr); break; } } return station; } /// /// 把DataRow的数据封装至配方实体 /// /// 配方数据行 /// 返回封装号的配方实体对象 public static Entity.xl_station ConvertDataRowToStationEntity(DataRow dr) { if (dr != null) { Entity.xl_station station = new Entity.xl_station(); station.ID = Mesnac.Basic.DataProcessor.RowValue(dr, "ID", String.Empty); station.StationName = Mesnac.Basic.DataProcessor.RowValue(dr, "StationName", String.Empty); station.End_datetime = Mesnac.Basic.DataProcessor.RowValue(dr, "End_datetime", DateTime.Now); return station; } else { return null; } } public static string UpdateStationData(Entity.xl_station station) { 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(station.ID)) { //添加新配方 sqlstr = @"INSERT INTO xl_station(ID,StationName,End_datetime)VALUES(@ID,@StationName,@End_datetime)"; } else { //更新原有配方 sqlstr = @"UPDATE xl_station set StationName = @StationName,End_datetime =@End_datetime where ID = @ID"; } dbHelper.ClearParameter(); dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@ID", station.ID); dbHelper.AddParameter("@StationName", station.StationName); dbHelper.AddParameter("@End_datetime", DateTime.Now); dbHelper.ExecuteNonQuery(); return station.ID; } public static void UpdateStationSubData(List sub) { DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); if (dbHelper == null) { throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError); } try { dbHelper.CommandType = CommandType.Text; string sqlstr = String.Empty; foreach (Entity.xl_station_sub item in sub) { //更新原有配方 sqlstr = @"UPDATE xl_station_sub set StationSubName = @StationSubName,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 where ID = @ID"; dbHelper.ClearParameter(); dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@ID", item.ID); dbHelper.AddParameter("@StationSubName", item.StationSubName); dbHelper.AddParameter("@Station_Weight_Medium", item.Station_Weight_Medium); dbHelper.AddParameter("@Station_Weight_Low", item.Station_Weight_Low); dbHelper.AddParameter("@Station_Weight_Advance", item.Station_Weight_Advance); dbHelper.AddParameter("@Station_Speed_Hight", item.Station_Speed_Hight); dbHelper.AddParameter("@Station_Speed_Medium", item.Station_Speed_Medium); dbHelper.AddParameter("@Station_Speed_Low", item.Station_Speed_Low); dbHelper.ExecuteNonQuery(); } } catch (Exception ex) { throw ex; } } public static void DelStationSubData(string sub) { DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); if (dbHelper == null) { throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError); } try { dbHelper.CommandType = CommandType.Text; string sqlstr = String.Empty; //更新原有配方 sqlstr = @"Delete from xl_station_sub where MainId = @ID"; dbHelper.ClearParameter(); dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@ID", sub); dbHelper.ExecuteNonQuery(); } catch (Exception ex) { throw ex; } } /// /// 判断某个计划是否存在 /// /// 工位名称 /// 存在返回true,否则返回false public static bool PlanExistsStation(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 strSql = "select count(Plan_Id) from xl_plan where Recipe_ID = @ID"; dbHelper.CommandText = strSql; dbHelper.AddParameter("@ID", recipeID); object result = dbHelper.ToScalar(); if (result != null && result != DBNull.Value && Convert.ToInt32(result) > 0) { return true; } else { return false; } } #region 删除工位信息 /// /// 删除工位信息 /// /// 工位ID public static void DeleteStation(string 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; string sqlstr = "delete from xl_station where ID = @ID"; dbHelper.ClearParameter(); dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@ID", ID); dbHelper.ExecuteNonQuery(); } #endregion #region 删除工位信息 /// /// 删除工位信息 /// /// 工位ID public static void DeleteStationSub(string 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; string sqlstr = "delete from xl_station_sub where MainId = @MainId"; dbHelper.ClearParameter(); dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@MainId", ID); dbHelper.ExecuteNonQuery(); } #endregion } }