using MESClassLibrary.BLL.Log;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Text;
using MESClassLibrary.Model;
namespace MESClassLibrary.DAL.ZPPlan
{
public class ZPPlanDAL
{
public DataTable PageInfo(int pageIndex, int pageSize, string startTime, string endTime, string partNo)
{
try
{
int skip = (pageIndex - 1) * pageSize;
string sql =
@"select * FROM[dbo].[tb_ZPPlan]
where CreatTime <= @StartTime and CreateTime >= @EndTime and PartNo = @PartNo
order by CreateTime desc";
sql += " offset " + skip + " rows ";
sql += " fetch next " + pageSize + " rows only";
SqlParameter[] param = new SqlParameter[] {
new SqlParameter("@StartTime", SqlDbType.VarChar)
{
Value = startTime
},
new SqlParameter("@EndTime", SqlDbType.VarChar)
{
Value = endTime
},
};
return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0];
}
catch (Exception ex)
{
LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
return null;
}
}
///
/// 获取工位正在生产、未生产的计划
///
///
///
public DataTable GetProductingPlan(string station)
{
try
{
// string sql = @"SELECT top 1 dbo.tb_Station.StationNo,dbo.tb_ZPPlan.ID, dbo.tb_ZPPlan.OrderNo, dbo.tb_ZPPlan.PartNo, dbo.tb_Product.ProductName,
// dbo.tb_ZPPlan.OrderCount, dbo.tb_ZPPlan.ProductCount, dbo.tb_ZPPlan.IsFinish, dbo.tb_ZPPlan.BadCount,dbo.tb_ZPPlan.CreateTime
// FROM dbo.tb_ZPPlan LEFT OUTER JOIN
// dbo.tb_Product ON dbo.tb_ZPPlan.PartNo = dbo.tb_Product.PartNo LEFT OUTER JOIN
// dbo.tb_Station ON dbo.tb_ZPPlan.StationID = dbo.tb_Station.StationID
// where dbo.tb_Station.StationNo =@station and dbo.tb_ZPPlan.IsFinish<>3
// order by dbo.tb_ZPPlan.CreateTime ";
string sql = @"SELECT TOP 1 * FROM v_ZPPlan
WHERE [StationNo]=@station and IsFinish<>3
ORDER BY [CreateTime] ";
SqlParameter[] param = new SqlParameter[1];
param[0] = new SqlParameter("@station", SqlDbType.VarChar);
param[0].Value = station;
return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, param).Tables[0];
}
catch (Exception ex)
{
LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
return null;
}
}
public DataTable GetProductingPlan1(string station,string orderNo)
{
try
{
string sql = @"SELECT dbo.tb_Station.StationNo,dbo.tb_ZPPlan.ID, dbo.tb_ZPPlan.OrderNo, dbo.tb_ZPPlan.PartNo, dbo.tb_Product.ProductName,
dbo.tb_ZPPlan.OrderCount, dbo.tb_ZPPlan.ProductCount, dbo.tb_ZPPlan.IsFinish, dbo.tb_ZPPlan.BadCount,dbo.tb_ZPPlan.CreateTime
FROM dbo.tb_ZPPlan LEFT OUTER JOIN
dbo.tb_Product ON dbo.tb_ZPPlan.PartNo = dbo.tb_Product.PartNo LEFT OUTER JOIN
dbo.tb_Station ON dbo.tb_ZPPlan.StationID = dbo.tb_Station.StationID
where dbo.tb_Station.StationNo =@station and dbo.tb_ZPPlan.IsFinish<>3
AND dbo.tb_ZPPlan.OrderNo=@orderNo";
SqlParameter[] param = new SqlParameter[2];
param[0] = new SqlParameter("@station", SqlDbType.VarChar);
param[0].Value = station;
param[1] = new SqlParameter("@orderNo", SqlDbType.VarChar);
param[1].Value = orderNo;
return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, param).Tables[0];
}
catch (Exception ex)
{
LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
return null;
}
}
///
/// 获取工位上一计划、下一计划
///
///
///
public DataTable GetPlan(string station,int flag,string planTime)
{
try
{
string sql = "";
if (flag == 1) //上一计划
{
// sql = @"SELECT top 1 dbo.tb_Station.StationNo,dbo.tb_ZPPlan.ID, dbo.tb_ZPPlan.OrderNo, dbo.tb_ZPPlan.PartNo, dbo.tb_Product.ProductName,
// dbo.tb_ZPPlan.OrderCount, dbo.tb_ZPPlan.ProductCount, dbo.tb_ZPPlan.IsFinish, dbo.tb_ZPPlan.BadCount,dbo.tb_ZPPlan.CreateTime
// FROM dbo.tb_ZPPlan LEFT OUTER JOIN
// dbo.tb_Product ON dbo.tb_ZPPlan.PartNo = dbo.tb_Product.PartNo LEFT OUTER JOIN
// dbo.tb_Station ON dbo.tb_ZPPlan.StationID = dbo.tb_Station.StationID
// where dbo.tb_Station.StationNo =@station and dbo.tb_ZPPlan.IsFinish<>3 and dbo.tb_ZPPlan.CreateTime < DATEADD(SECOND,-1, @planTime)
// order by dbo.tb_ZPPlan.CreateTime desc";
sql = @"SELECT TOP 1 * FROM v_ZPPlan
WHERE [StationNo]=@station AND IsFinish <>3
AND CreateTime < DATEADD(SECOND,-1, @planTime)
ORDER BY [CreateTime] desc";
}
if (flag == 2) //下一计划
{
// sql = @"SELECT top 1 dbo.tb_Station.StationNo,dbo.tb_ZPPlan.ID, dbo.tb_ZPPlan.OrderNo, dbo.tb_ZPPlan.PartNo, dbo.tb_Product.ProductName,
// dbo.tb_ZPPlan.OrderCount, dbo.tb_ZPPlan.ProductCount, dbo.tb_ZPPlan.IsFinish, dbo.tb_ZPPlan.BadCount,dbo.tb_ZPPlan.CreateTime
// FROM dbo.tb_ZPPlan LEFT OUTER JOIN
// dbo.tb_Product ON dbo.tb_ZPPlan.PartNo = dbo.tb_Product.PartNo LEFT OUTER JOIN
// dbo.tb_Station ON dbo.tb_ZPPlan.StationID = dbo.tb_Station.StationID
// where dbo.tb_Station.StationNo =@station and dbo.tb_ZPPlan.IsFinish<>3 and dbo.tb_ZPPlan.CreateTime > DATEADD(SECOND,1, @planTime)
// order by dbo.tb_ZPPlan.CreateTime ";
sql = @"SELECT TOP 1 * FROM v_ZPPlan
WHERE [StationNo]=@station AND IsFinish <>3
AND CreateTime > DATEADD(SECOND,1, @planTime)
ORDER BY [CreateTime]";
}
SqlParameter[] param = new SqlParameter[2];
param[0] = new SqlParameter("@station", SqlDbType.VarChar);
param[0].Value = station;
param[1] = new SqlParameter("@planTime", SqlDbType.VarChar);
param[1].Value = planTime;
return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, param).Tables[0];
}
catch (Exception ex)
{
LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
return null;
}
}
public bool updateQty(ZPPlanModel md)
{
try
{
string sql = @"update tb_ZPPlan set ProductCount=ProductCount+1 where ID=@ID";
SqlParameter[] param = new SqlParameter[1];
param[0] = new SqlParameter("@ID", SqlDbType.VarChar);
param[0].Value = md.ID;
SqlHelper.ExecuteNonQuery(SqlHelper.GetConnSting(), CommandType.Text, sql, param);
return true;
}
catch (Exception ex)
{
LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
return false;
}
}
public bool updateBad(ZPPlanModel md)
{
try
{
string sql = @"update tb_ZPPlan set ProductCount=ProductCount-1,BadCount=BadCount+1 where ID=@ID";
SqlParameter[] param = new SqlParameter[1];
param[0] = new SqlParameter("@ID", SqlDbType.VarChar);
param[0].Value = md.ID;
SqlHelper.ExecuteNonQuery(SqlHelper.GetConnSting(), CommandType.Text, sql, param);
return true;
}
catch (Exception ex)
{
LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
return false;
}
}
public DataTable SearchPlanInfo(string planID)
{
try
{
string sql = @"SELECT dbo.tb_ZPPlan.PartNo, dbo.tb_Product.ProductName
FROM dbo.tb_ZPPlan LEFT OUTER JOIN
dbo.tb_Product ON dbo.tb_ZPPlan.PartNo = dbo.tb_Product.PartNo
where ID='" + planID + "'";
return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0];
}
catch (Exception ex)
{
LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
return null;
}
}
public DataTable SearchOrderNo(string orderno)
{
try
{
string sql = @"select top 1 * from dbo.tb_ZPPlan
where OrderNo like '" + orderno + @"%'
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;
}
}
}
}