using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using MESClassLibrary.BLL.Log;
using MESClassLibrary.EFModel;
using MESClassLibrary.Model;
using System.Reflection;

namespace MESClassLibrary.DAL.BasicInfo
{
    public class InjectionPlanDAL
    {
        public static string TableName = "tb_InjectionPlan";
        
        
        /// <summary>
        /// 获取当前计划
        /// </summary>
        /// <param name="StationID"></param>
        /// <returns></returns>
        public DataTable SearchPlanByStation(string StationID)
        {
            try
            {
                string sql = @"SELECT top 1 dbo.tb_InjectionPlan.InjectionPlanID, dbo.tb_InjectionPlan.PartNo, dbo.tb_Station.StationNo, 
                                    dbo.tb_InjectionPlan.BeginTime, dbo.tb_InjectionPlan.PlanCount, dbo.tb_InjectionPlan.EndTime, 
                                    dbo.tb_InjectionPlan.StockNo, dbo.tb_InjectionPlan.PlanDate, dbo.tb_InjectionPlan.CompleteCount, 
                                    dbo.tb_InjectionPlan.ProductNature, dbo.tb_Product.ColorName, dbo.tb_Product.Rows, dbo.tb_Product.Cols, 
                                    dbo.tb_Product.Layers, dbo.tb_Product.PicturePath, dbo.tb_Station.StationID
                                    FROM  dbo.tb_InjectionPlan LEFT OUTER JOIN
                                    dbo.tb_Station ON dbo.tb_InjectionPlan.StationID = dbo.tb_Station.StationID LEFT OUTER JOIN
                                    dbo.tb_Product ON dbo.tb_InjectionPlan.StockNo = dbo.tb_Product.StockNo
                                    WHERE (dbo.tb_Station.StationNo = @StationID) AND ((dbo.tb_InjectionPlan.IsFinish IS NULL OR dbo.tb_InjectionPlan.IsFinish=0))
                                    ORDER BY dbo.tb_InjectionPlan.BeginTime";

                SqlParameter[] param = new SqlParameter[1];
                param[0] = new SqlParameter("@StationID", SqlDbType.VarChar);
                param[0].Value = StationID;

                return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, param).Tables[0];

            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }
        
        /// <summary>
        /// 获取下一计划
        /// </summary>
        /// <param name="StationID"></param>
        /// <returns></returns>
        public DataTable NextSearchPlanByStation(string StationID)
        {
            try
            {
                string sql = @"SELECT top 2  dbo.tb_Product.ProductName, dbo.tb_Station.StationNo, dbo.tb_InjectionPlan.BeginTime, dbo.tb_InjectionPlan.PlanCount,dbo.tb_Product.ProductID,dbo.tb_InjectionPlan.InjectionPlanID, 
                        dbo.tb_InjectionPlan.EndTime, dbo.tb_InjectionPlan.StockNo, dbo.tb_InjectionPlan.PlanDate, 
                        dbo.tb_InjectionPlan.CompleteCount, dbo.tb_Product.PartNo, dbo.tb_Product.ProductName, dbo.tb_Product.ColorName, 
                        dbo.tb_Product.Rows, dbo.tb_Product.Cols, dbo.tb_Product.Layers, dbo.tb_Product.PicturePath,dbo.tb_Station.StationID
                        FROM  dbo.tb_InjectionPlan LEFT OUTER JOIN
                        dbo.tb_Station ON dbo.tb_InjectionPlan.StationID = dbo.tb_Station.StationID LEFT OUTER JOIN
                        dbo.tb_Product ON dbo.tb_InjectionPlan.StockNo = dbo.tb_Product.StockNo
                        WHERE (dbo.tb_Station.StationNo = @StationID) AND ((dbo.tb_InjectionPlan.IsFinish IS NULL OR dbo.tb_InjectionPlan.IsFinish=0))
                        ORDER BY dbo.tb_InjectionPlan.BeginTime";

                SqlParameter[] param = new SqlParameter[1];
                param[0] = new SqlParameter("@StationID", SqlDbType.VarChar);
                param[0].Value = StationID;

                return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, param).Tables[0];

            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }

        public bool UpdateFinish(InjectionPlanModel md)
        {
            try
            {
                #region 添加数据
                string sql = "";
                SqlParameter[] param = null;
                sql = @"update tb_InjectionPlan set IsFinish=1 ,FinishTime=getdate() 
                    where [InjectionPlanID]=( select top 1 [InjectionPlanID] from [dbo].[tb_InjectionPlan] where  (IsFinish is null or IsFinish=0 )and StationID=@StationID order by BeginTime asc)";

                #region 添加参数
                param = new SqlParameter[1];
                param[0] = new SqlParameter("@StationID", SqlDbType.VarChar);
                param[0].Value = md.StationID;
                #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 SearchByPage(int pageIndex, int pageSize, string stationID, string stockNo)
        {
            try
            {

                string sql = "select * FROM[dbo].[tb_InjectionPlan] ";
                sql += " where 1=1 ";
                if (!string.IsNullOrEmpty(stationID))
                {
                    sql += " and StationID = '" + stationID + "' ";
                }
                if (!string.IsNullOrEmpty(stockNo))
                {
                    sql += " and StockNo like '%" + stockNo + "%' ";
                }
                sql += " order by BeginTime ";
                sql += " offset " + ((pageIndex - 1) * pageSize) + " rows ";
                sql += " fetch next " + pageSize + " rows only ";

                return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0];

            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }
        public DataTable SearchByTime(int pageIndex, int pageSize, string stationID, string stockNo, string StartTime, string EndTime)
        {
            try
            {

                string sql = "select * FROM[dbo].[tb_InjectionPlan] where BeginTime>='" + StartTime + "' and BeginTime<='" + EndTime + "' ";
                if (!string.IsNullOrEmpty(stationID))
                {
                    sql += " and StationID = '" + stationID + "' ";
                }
                if (!string.IsNullOrEmpty(stockNo))
                {
                    sql += " and StockNo like '%" + stockNo + "%' ";
                }
                sql += " order by BeginTime ";
                sql += " offset " + ((pageIndex - 1) * pageSize) + " rows ";
                sql += " fetch next " + pageSize + " rows only ";

                return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0];

            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }


        public int SearchByTimeCount(string stationID, string stockNo, string StartTime, string EndTime)
        {
            try
            {

                string sql = "select * FROM[dbo].[tb_InjectionPlan] where BeginTime>='" + StartTime + "' and EndTime<='" + EndTime + "' ";
                if (!string.IsNullOrEmpty(stationID))
                {
                    sql += " and StationID = '" + stationID + "' ";
                }
                if (!string.IsNullOrEmpty(stockNo))
                {
                    sql += " and StockNo like '%" + stockNo + "%' ";
                }

                return SqlHelper.ExecuteNonQuery(SqlHelper.GetConnSting(), CommandType.Text, sql);

            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return 0;
            }
        }



    }
}