using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Data.SqlClient; using System.Reflection; using MESClassLibrary.BLL.Log; using MESClassLibrary.Model; namespace MESClassLibrary.DAL.BasicInfo { public class BarCodeDAl { public static string TableName = "tb_BarCode"; public DataTable SearchInfoByStock(string StockNo) { string sql = ""; DateTime time; try { time = GetDateTime(); if (time.Hour>=8 &&time.Hour<=23) { //sql = @"select top 1 * from " + TableName + " WITH (NOLOCK) where StockNo=@StockNo and PrintType <>2 and " + // "substring([BarCode],len( [BarCode])-10,6)= right(DATENAME(yy, GETDATE()),2)+ RIGHT('00'+CAST(MONTH(GETDATE()) AS VARCHAR(2)),2)+RIGHT('00'+CAST(Day(GETDATE()) AS VARCHAR(2)),2)" + // " order by [CreateTime] desc"; string batch1 = GetBatch1(); sql = @"select top 1 * from " + TableName + $" WITH (NOLOCK) where StockNo=@StockNo and PrintType <>2 and BatchNo='{batch1}' order by [CreateTime] desc"; //sql = @"select top 1 * from " + TableName + " where StockNo=@StockNo and PrintType <>2 and " + // "substring([BarCode],len( [BarCode])-10,6)= right(DATENAME(yy, ),2)+ RIGHT('00'+CAST(MONTH(GETDATE()) AS VARCHAR(2)),2)+RIGHT('00'+CAST(Day(GETDATE()) AS VARCHAR(2)),2)" + // " order by [CreateTime] desc"; } else { string batch2 = GetBatch2(); //sql = @"select top 1 * from " + TableName + " WITH (NOLOCK) where StockNo=@StockNo and PrintType <>2 and BatchNo= right(DATENAME(yy, DATEADD(dd,-1, GETDATE())),2)+ RIGHT('00'+CAST(MONTH(DATEADD(dd,-1, GETDATE())) AS VARCHAR(2)),2)+RIGHT('00'+CAST(Day(DATEADD(dd,-1, GETDATE())) AS VARCHAR(2)),2)" + // " order by [CreateTime] desc"; sql = @"select top 1 * from " + TableName + $" WITH (NOLOCK) where StockNo=@StockNo and PrintType <>2 and BatchNo= '{batch2}'" + " order by [CreateTime] desc"; } SqlParameter[] param = new SqlParameter[1]; param[0] = new SqlParameter("@StockNo", SqlDbType.VarChar); param[0].Value = StockNo; return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, param).Tables[0]; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); throw ex; } } public bool AddInfo(BarCodeModel md) { try { #region 添加数据 string sql = ""; SqlParameter[] param = null; sql = "INSERT INTO " + TableName + " (ID,StationID,OneBarCode,[BarCode],[StockNo],[PrintType],[CreateTime],[IsImport],StationID2,PlanID,BatchNo"; sql += ") VALUES ("; sql += "@ID,"; sql += "@StationID,"; sql += "@OneBarCode,"; sql += "@BarCode,"; sql += "@StockNo,"; sql += "@PrintType,"; sql += "(select getdate()),"; sql += "@IsImport,"; sql += "@StationID2,"; sql += "@PlanID,@BatchNo)"; #region 添加参数 param = new SqlParameter[10]; param[0] = new SqlParameter("@ID", SqlDbType.VarChar); param[0].Value = md.ID; param[1] = new SqlParameter("@StationID", SqlDbType.VarChar); param[1].Value = md.StationID; param[2] = new SqlParameter("@OneBarCode", SqlDbType.VarChar); param[2].Value = md.OneBarCode; param[3] = new SqlParameter("@BarCode", SqlDbType.VarChar); param[3].Value = md.BarCode; param[4] = new SqlParameter("@StockNo", SqlDbType.VarChar); param[4].Value = md.StockNo; param[5] = new SqlParameter("@PrintType", SqlDbType.Int); param[5].Value =md.PrintType; param[6] = new SqlParameter("@IsImport", SqlDbType.Text); param[6].Value = md.Import; param[7] = new SqlParameter("@StationID2", SqlDbType.VarChar); param[7].Value = md.StationID2; param[8] = new SqlParameter("@PlanID", SqlDbType.VarChar); param[8].Value = md.PlanID; param[9] = new SqlParameter("@BatchNo", SqlDbType.VarChar); param[9].Value = md.BatchNo; #endregion SqlHelper.ExecuteNonQuery(SqlHelper.GetConnSting(), CommandType.Text, sql, param); #endregion return true; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return false; } } public DataTable SearchInfo(string stationNo) { try { string sql = @"SELECT top 100 dbo.tb_BarCode.BarCode, dbo.tb_Product.ProductName, CASE dbo.tb_BarCode.PrintType WHEN 0 THEN '自动打印' when 1 then '手动打印' when 2 then '补打' END AS PrintType, dbo.tb_BarCode.CreateTime FROM dbo.tb_BarCode with (NOLOCK) LEFT OUTER JOIN dbo.tb_Product ON dbo.tb_BarCode.StockNo = dbo.tb_Product.StockNo LEFT OUTER JOIN dbo.tb_Station ON dbo.tb_BarCode.StationID = dbo.tb_Station.StationID where dbo.tb_Station.StationNo=@stationNo and IsDel=0 " + //" and substring([BarCode],len( [BarCode])-10,6)= right(DATENAME(yy, GETDATE()),2)+ RIGHT('00'+CAST(MONTH(GETDATE()) AS VARCHAR(2)),2)+RIGHT('00'+CAST(Day(GETDATE()) AS VARCHAR(2)),2)" + " order by [CreateTime] desc"; SqlParameter[] param = new SqlParameter[1]; param[0] = new SqlParameter("@stationNo", SqlDbType.VarChar); param[0].Value = stationNo; return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, param).Tables[0]; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return null; } } public DataTable SearchInfoAll() { try { string sql = @"SELECT dbo.tb_BarCode.BarCode, dbo.tb_Product.ProductName, CASE dbo.tb_BarCode.PrintType WHEN 0 THEN '自动打印' when 1 then '手动打印' when 2 then '补打' END AS PrintType, dbo.tb_BarCode.PrintTime FROM dbo.tb_BarCode with (NOLOCK) LEFT OUTER JOIN dbo.tb_Product ON dbo.tb_BarCode.StockNo = dbo.tb_Product.StockNo LEFT OUTER JOIN dbo.tb_Station ON dbo.tb_BarCode.StationID = dbo.tb_Station.StationID where dbo.tb_Station.StationNo=@stationNo and IsDel=0 " + //" and substring([BarCode],len( [BarCode])-10,6)= right(DATENAME(yy, GETDATE()),2)+ RIGHT('00'+CAST(MONTH(GETDATE()) AS VARCHAR(2)),2)+RIGHT('00'+CAST(Day(GETDATE()) AS VARCHAR(2)),2)" + " order by [CreateTime] desc"; return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0]; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return null; } } public DataTable SearchBarCode() { try { string sql = @"SELECT TOP (1000) dbo.tb_BarCode.BarCode, dbo.tb_Product.ProductName, CASE dbo.tb_BarCode.PrintType WHEN 0 THEN '自动打印' WHEN 1 THEN '手动打印' WHEN 2 THEN '补打' END AS PrintType, dbo.tb_BarCode.PrintTime, dbo.tb_BarCode.OneBarCode, dbo.tb_BarCode.PlanID, dbo.tb_InjectPlanReport.MaterialName, dbo.tb_InjectPlanReport.BatchNo FROM dbo.tb_BarCode with (NOLOCK) LEFT OUTER JOIN dbo.tb_InjectPlanReport ON dbo.tb_BarCode.PlanID = dbo.tb_InjectPlanReport.InjectionPlanID LEFT OUTER JOIN dbo.tb_Product ON dbo.tb_BarCode.StockNo = dbo.tb_Product.StockNo LEFT OUTER JOIN dbo.tb_Station ON dbo.tb_BarCode.StationID = dbo.tb_Station.StationID WHERE (dbo.tb_BarCode.IsDel = 0) ORDER BY dbo.tb_BarCode.CreateTime DESC"; return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0]; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return null; } } public DataTable SearchBarCodeByOne(string oneBarCode) { try { string sql = @"SELECT dbo.tb_BarCode.BarCode, dbo.tb_Product.ProductName, CASE dbo.tb_BarCode.PrintType WHEN 0 THEN '自动打印' WHEN 1 THEN '手动打印' WHEN 2 THEN '补打' END AS PrintType, dbo.tb_BarCode.PrintTime, dbo.tb_BarCode.OneBarCode,dbo.tb_BarCode.PlanID, dbo.tb_InjectPlanReport.MaterialName, dbo.tb_InjectPlanReport.BatchNo FROM dbo.tb_BarCode with (NOLOCK) LEFT OUTER JOIN dbo.tb_InjectPlanReport ON dbo.tb_BarCode.PlanID = dbo.tb_InjectPlanReport.InjectionPlanID LEFT OUTER JOIN dbo.tb_Product ON dbo.tb_BarCode.StockNo = dbo.tb_Product.StockNo LEFT OUTER JOIN dbo.tb_Station ON dbo.tb_BarCode.StationID = dbo.tb_Station.StationID WHERE dbo.tb_BarCode.IsDel = 0 and dbo.tb_BarCode.OneBarCode=@OneBarCode ORDER BY dbo.tb_BarCode.CreateTime DESC"; SqlParameter[] param = new SqlParameter[1]; param[0] = new SqlParameter("@OneBarCode", SqlDbType.VarChar); param[0].Value = oneBarCode; return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, param).Tables[0]; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return null; } } public DataTable SearchBarCodeByTwo(string BarCode) { try { // string sql = @"SELECT dbo.tb_BarCode.BarCode, dbo.tb_Product.ProductName, // CASE dbo.tb_BarCode.PrintType WHEN 0 THEN '自动打印' WHEN 1 THEN '手动打印' WHEN 2 THEN '补打' END AS PrintType, // dbo.tb_BarCode.PrintTime, dbo.tb_BarCode.OneBarCode // FROM dbo.tb_BarCode LEFT OUTER JOIN // dbo.tb_Product ON dbo.tb_BarCode.StockNo = dbo.tb_Product.StockNo LEFT OUTER JOIN // dbo.tb_Station ON dbo.tb_BarCode.StationID = dbo.tb_Station.StationID // WHERE dbo.tb_BarCode.IsDel = 0 and dbo.tb_BarCode.BarCode=@BarCode // ORDER BY dbo.tb_BarCode.CreateTime DESC"; string sql = @"SELECT dbo.tb_BarCode.BarCode, dbo.tb_Product.ProductName, CASE dbo.tb_BarCode.PrintType WHEN 0 THEN '自动打印' WHEN 1 THEN '手动打印' WHEN 2 THEN '补打' END AS PrintType, dbo.tb_BarCode.PrintTime, dbo.tb_BarCode.OneBarCode,dbo.tb_BarCode.PlanID, dbo.tb_InjectPlanReport.MaterialName, dbo.tb_InjectPlanReport.BatchNo FROM dbo.tb_BarCode with (NOLOCK) LEFT OUTER JOIN dbo.tb_InjectPlanReport ON dbo.tb_BarCode.PlanID = dbo.tb_InjectPlanReport.InjectionPlanID LEFT OUTER JOIN dbo.tb_Product ON dbo.tb_BarCode.StockNo = dbo.tb_Product.StockNo LEFT OUTER JOIN dbo.tb_Station ON dbo.tb_BarCode.StationID = dbo.tb_Station.StationID WHERE dbo.tb_BarCode.IsDel = 0 and dbo.tb_BarCode.BarCode=@BarCode ORDER BY dbo.tb_BarCode.CreateTime DESC"; SqlParameter[] param = new SqlParameter[1]; param[0] = new SqlParameter("@BarCode", SqlDbType.VarChar); param[0].Value = BarCode; return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, param).Tables[0]; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return null; } } /// /// 查找流水号 /// /// 零件号.批次 /// public DataTable SearchSerialNoByBarCode(string stockNo,string batchNo) { try { string sql = @"select top 1 * from " + TableName + " with (NOLOCK) where StockNo=@StockNo and BatchNo=@BatchNo and PrintType <> 2" + " order by [CreateTime] desc"; SqlParameter[] param = new SqlParameter[2]; param[0] = new SqlParameter("@StockNo", SqlDbType.VarChar); param[0].Value = stockNo; param[1] = new SqlParameter("@BatchNo", SqlDbType.VarChar); param[1].Value = batchNo; return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, param).Tables[0]; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return null; } } public DataTable SearchInfoByBarCode(string BarCode) { try { string sql = @"select top 1 * from " + TableName + " with (NOLOCK) where BarCode=@BarCode order by [CreateTime] desc "; SqlParameter[] param = new SqlParameter[1]; param[0] = new SqlParameter("@BarCode", SqlDbType.VarChar); param[0].Value = BarCode; return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, param).Tables[0]; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return null; } } /// /// 更改打印时间 /// /// /// public bool UpdateInfo(BarCodeModel md) { try { #region 添加数据 string sql = ""; SqlParameter[] param = null; sql = @"update tb_BarCode set PrintTime=(select getdate()) where [StationID]=@StationID and PrintTime is null"; #region 添加参数 param = new SqlParameter[1]; param[0] = new SqlParameter("@StationID", SqlDbType.VarChar); param[0].Value = md.StationID; //param[1] = new SqlParameter("@PrintTime", SqlDbType.DateTime); //param[1].Value = md.PrintTime; #endregion SqlHelper.ExecuteNonQuery(SqlHelper.GetConnSting(), CommandType.Text, sql, param); #endregion return true; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return false; } } /// /// 删除手工打印条码 /// /// /// public bool DelBarCode(BarCodeModel md) { try { #region 添加数据 string sql = ""; SqlParameter[] param = null; sql = @"update tb_BarCode set IsDel=0 where BarCode=@BarCode"; #region 添加参数 param = new SqlParameter[1]; param[0] = new SqlParameter("@BarCode", SqlDbType.VarChar); param[0].Value = md.BarCode; #endregion SqlHelper.ExecuteNonQuery(SqlHelper.GetConnSting(), CommandType.Text, sql, param); #endregion return true; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return false; } } public DateTime GetDateTime() { string sql = ""; DateTime time; DataTable dt; try { sql = @"select getdate() as time"; dt= SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0]; if (dt != null && dt.Rows.Count > 0) { time = Convert.ToDateTime(dt.Rows[0]["time"].ToString()); } else { time=DateTime.Now; } return time; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return DateTime.Now; } } public string GetBatch1() { string sql = ""; DateTime time; DataTable dt; try { sql = @"select right(DATENAME(yy, GETDATE()), 2) + RIGHT('00' + CAST(MONTH(GETDATE()) AS VARCHAR(2)), 2) + RIGHT('00' + CAST(Day(GETDATE()) AS VARCHAR(2)), 2) as time"; dt = SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0]; if (dt != null && dt.Rows.Count > 0) { return dt.Rows[0]["time"].ToString(); } else { return DateTime.Now.Year.ToString().Substring(3,2)+DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString(); } } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return DateTime.Now.Year.ToString().Substring(3, 2) + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString(); } } public string GetBatch2() { string sql = ""; DateTime time; DataTable dt; try { sql = @"select right(DATENAME(yy, DATEADD(dd,-1, GETDATE())),2)+ RIGHT('00'+CAST(MONTH(DATEADD(dd,-1, GETDATE())) AS VARCHAR(2)),2)+RIGHT('00'+CAST(Day(DATEADD(dd,-1, GETDATE())) AS VARCHAR(2)),2) as time"; dt = SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0]; if (dt != null && dt.Rows.Count > 0) { return dt.Rows[0]["time"].ToString(); } else { return DateTime.Now.Year.ToString().Substring(2, 2) + DateTime.Now.Month.ToString().PadLeft(2, '0') + DateTime.Now.AddDays(-1).Day.ToString().PadLeft(2, '0'); } } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return DateTime.Now.Year.ToString().Substring(2, 2) + DateTime.Now.Month.ToString().PadLeft(2, '0') + DateTime.Now.AddDays(-1).Day.ToString().PadLeft(2, '0'); } } /// /// 根据二维码查找一维码,并查找一维码是否存在 /// /// /// public DataTable SearchOneBarCode(string BarCode,int a) { try { string sql = ""; SqlParameter[] param = new SqlParameter[1]; if (a == 1) { sql = @"select OneBarCode from tb_BarCode where OneBarCode = @barcode"; param[0] = new SqlParameter("@barcode", SqlDbType.VarChar); param[0].Value = BarCode; } if (a == 2) { sql = @"select * from tb_BarCode where barcode = @barcode"; param[0] = new SqlParameter("@barcode", SqlDbType.VarChar); param[0].Value = BarCode; } return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, param).Tables[0]; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return null; } } } }