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

namespace MESClassLibrary.DAL.PunchAndWeld
{
    public class PunchPlanMFADAL
    {
        /// <summary>
        /// 后保
        /// </summary>
        /// <param name="DeviceNo"></param>
        /// <returns></returns>
        public DataTable SearchPlan(string DeviceNo)
        {
            try
            {
                string sql = "";

                if (DeviceNo == "BMPTJQSB-05-094")
                {
                    sql =
                        @"select  CarModelCode,productionNumber,itemNumber ,[description]  ,assemblyDate ,BillNo ,
                        partType ,ColorName,BarCode,Sort ,ColorCode ,CheckTime  from t_JIS_Assemble 
                        where IsCheck = 1 and PreCheck = 0 and IsOnline =0 and CarmodelCode = 'Z177-S' order by CheckTime";
                }
                else if (DeviceNo == "BMPTJQSB-05-099")
                {
                    sql =
                        @"select  CarModelCode,productionNumber ,itemNumber,[description]  ,assemblyDate ,BillNo ,
                        partType ,ColorName ,BarCode ,Sort,ColorCode ,CheckTime  from t_JIS_Assemble 
                        where IsCheck = 1 and PreCheck = 0 and IsOnline =0 and CarmodelCode = 'Z177-AMG' order by CheckTime";
                }
                else if (DeviceNo == "BMPTJQSB-05-166")
                {
                    sql =
                        @"select  CarModelCode,productionNumber ,itemNumber ,[description]  ,assemblyDate ,BillNo ,
                        partType ,ColorName ,BarCode ,Sort ,ColorCode,CheckTime  from t_JIS_Assemble 
                        where IsCheck = 1 and PreCheck = 0 and IsOnline =0 and CarmodelCode = 'X247-S' 
                        union 
                        select  CarModelCode,productionNumber ,itemNumber ,[description]  ,assemblyDate ,BillNo ,
                        partType ,ColorName ,BarCode ,Sort ,ColorCode,CheckTime  from t_JIS_Assemble 
                        where IsCheck = 1 and PreCheck = 0 and IsOnline =0 and CarmodelCode = 'H247-S'
                        union
                        select  CarModelCode,productionNumber ,itemNumber ,[description]  ,assemblyDate ,BillNo ,
                        partType ,ColorName ,BarCode ,Sort ,ColorCode,CheckTime  from t_JIS_Assemble 
                        where IsCheck = 1 and PreCheck = 0 and IsOnline =0 and CarmodelCode = 'X243-S'
                        union
                        select  CarModelCode,productionNumber ,itemNumber ,[description]  ,assemblyDate ,BillNo ,
                        partType ,ColorName ,BarCode ,Sort ,ColorCode,CheckTime  from t_JIS_Assemble 
                        where IsCheck = 1 and PreCheck = 0 and IsOnline =0 and CarmodelCode = 'H243-S'
                        union
                        select  CarModelCode,productionNumber ,itemNumber ,[description]  ,assemblyDate ,BillNo ,
                        partType ,ColorName ,BarCode ,Sort ,ColorCode,CheckTime  from t_JIS_Assemble 
                        where IsCheck = 1 and PreCheck = 0 and IsOnline =0 and CarmodelCode = 'X243-A'
                        order by CheckTime";
                }
                
                return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting1(), CommandType.Text, sql, null).Tables[0];
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }

        /// <summary>
        /// 后保
        /// </summary>
        /// <param name="DeviceNo"></param>
        /// <returns></returns>
        public bool InsertPlan(string DeviceNo)
        {
            try
            {
                PunchMFAModel md=new PunchMFAModel();
                string sql = "";

                SqlParameter[] param = new SqlParameter[13];

                DataTable dt = SearchPlan(DeviceNo);
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        md.CarModelCode = dt.Rows[i]["CarModelCode"].ToString();
                        md.productionNumber = dt.Rows[i]["productionNumber"].ToString();
                        md.itemNumber = dt.Rows[i]["itemNumber"].ToString();
                        md.description = dt.Rows[i]["description"].ToString();
                        md.assemblyDate = Convert.ToDateTime(dt.Rows[i]["assemblyDate"].ToString());
                        md.BillNo = dt.Rows[i]["BillNo"].ToString();
                        md.partType = dt.Rows[i]["partType"].ToString();
                        md.ColorName = dt.Rows[i]["ColorName"].ToString();
                        md.BarCode = dt.Rows[i]["BarCode"].ToString();
                        md.Sort = Convert.ToInt32(dt.Rows[i]["Sort"].ToString());
                        md.ColorCode = dt.Rows[i]["ColorCode"].ToString();
                        md.CheckTime = Convert.ToDateTime(dt.Rows[i]["CheckTime"].ToString());
                        md.DeviceNo = DeviceNo;

                        sql =
                            @"IF NOT  EXISTS(SELECT * FROM [dbo].[tb_PunchMFAPlan] WHERE productionNumber=@productionNumber AND Sort=@Sort) INSERT INTO [dbo].[tb_PunchMFAPlan] ([CarModelCode],[productionNumber],[itemNumber],
                              [description],[assemblyDate],[BillNo],[partType],[ColorName],[BarCode],[Sort],[ColorCode],[CheckTime],[DeviceNo]) VALUES (@CarModelCode,@productionNumber,@itemNumber,@description,@assemblyDate,
                                @BillNo,@partType,@ColorName,@BarCode,@Sort,@ColorCode,@CheckTime,@DeviceNo)";

                        param[0] = new SqlParameter("CarModelCode", SqlDbType.VarChar);
                        param[0].Value = md.CarModelCode;

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

                        param[2] = new SqlParameter("itemNumber", SqlDbType.VarChar);
                        param[2].Value = md.itemNumber;

                        param[3] = new SqlParameter("@description", SqlDbType.VarChar);
                        param[3].Value = md.description;

                        param[4] = new SqlParameter("assemblyDate", SqlDbType.DateTime);
                        param[4].Value = md.assemblyDate;

                        param[5] = new SqlParameter("@BillNo", SqlDbType.VarChar);
                        param[5].Value = md.BillNo;

                        param[6] = new SqlParameter("partType", SqlDbType.VarChar);
                        param[6].Value = md.partType;

                        param[7] = new SqlParameter("@ColorName", SqlDbType.VarChar);
                        param[7].Value = md.ColorName;

                        param[8] = new SqlParameter("BarCode", SqlDbType.VarChar);
                        param[8].Value = md.BarCode;

                        param[9] = new SqlParameter("@Sort", SqlDbType.Int);
                        param[9].Value = md.Sort;

                        param[10] = new SqlParameter("ColorCode", SqlDbType.VarChar);
                        param[10].Value = md.ColorCode;

                        param[11] = new SqlParameter("@CheckTime", SqlDbType.DateTime);
                        param[11].Value = md.CheckTime;

                        param[12] = new SqlParameter("DeviceNo", SqlDbType.VarChar);
                        param[12].Value = md.DeviceNo;

                        SqlHelper.ExecuteNonQuery(SqlHelper.GetConnSting(), CommandType.Text, sql, param);
                    }
                }

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

        public DataTable SearchMFAPlan(string DeviceNo)
        {
            try
            {
                string sql = @"select top 1 * from [dbo].[tb_PunchMFAPlan] where IsFinish=0 and DeviceNo='"+ DeviceNo+"' order by ID";

                return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0];
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }

        public bool UpFinish(int ID)
        {
            try
            {
                string sql = @"update tb_PunchMFAPlan set IsFinish=1 where ID=" + ID;

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

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

        /// <summary>
        /// 前保
        /// </summary>
        /// <param name="DeviceNo"></param>
        /// <returns></returns>
        public DataTable SearchPlan_F(string DeviceNo)
        {
            try
            {
                string sql = "";

                if (DeviceNo == "BMPTJQSB-05-093")
                {
                    sql =
                        @"select  CarModelCode,productionNumber,itemNumber ,[description]  ,assemblyDate ,BillNo ,
                        partType ,ColorName,BarCode,Sort ,ColorCode ,CheckTime  from t_JIS_Assemble 
                        where IsCheck = 1 and PreCheck = 0 and IsOnline =0 and CarmodelCode = 'Z177-S' order by CheckTime";
                }
                else if (DeviceNo == "BMPTJQSB-05-098")
                {
                    sql =
                        @"select  CarModelCode,productionNumber ,itemNumber,[description]  ,assemblyDate ,BillNo ,
                        partType ,ColorName ,BarCode ,Sort,ColorCode ,CheckTime  from t_JIS_Assemble 
                        where IsCheck = 1 and PreCheck = 0 and IsOnline =0 and CarmodelCode = 'Z177-AMG' order by CheckTime";
                }
                else if (DeviceNo == "BMPTJQSB-05-166")
                {
//                    sql =
//                        @"select  CarModelCode,productionNumber ,itemNumber ,[description]  ,assemblyDate ,BillNo ,
//                        partType ,ColorName ,BarCode ,Sort ,ColorCode,CheckTime  from t_JIS_Assemble 
//                        where IsCheck = 1 and PreCheck = 0 and IsOnline =0 and CarmodelCode = 'X247-S' 
//                        union 
//                        select  CarModelCode,productionNumber ,itemNumber ,[description]  ,assemblyDate ,BillNo ,
//                        partType ,ColorName ,BarCode ,Sort ,ColorCode,CheckTime  from t_JIS_Assemble 
//                        where IsCheck = 1 and PreCheck = 0 and IsOnline =0 and CarmodelCode = 'H247-S'
//                        union
//                        select  CarModelCode,productionNumber ,itemNumber ,[description]  ,assemblyDate ,BillNo ,
//                        partType ,ColorName ,BarCode ,Sort ,ColorCode,CheckTime  from t_JIS_Assemble 
//                        where IsCheck = 1 and PreCheck = 0 and IsOnline =0 and CarmodelCode = 'X243-S'
//                        order by CheckTime";
                    sql =
                        @"select  CarModelCode,productionNumber ,itemNumber ,[description]  ,assemblyDate ,BillNo ,
                        partType ,ColorName ,BarCode ,Sort ,ColorCode,CheckTime  from t_JIS_Assemble 
                        where IsCheck = 1 and PreCheck = 0 and IsOnline =0 and CarmodelCode = 'X247-S' 
                        union 
                        select  CarModelCode,productionNumber ,itemNumber ,[description]  ,assemblyDate ,BillNo ,
                        partType ,ColorName ,BarCode ,Sort ,ColorCode,CheckTime  from t_JIS_Assemble 
                        where IsCheck = 1 and PreCheck = 0 and IsOnline =0 and CarmodelCode = 'X243-S'
                        order by CheckTime";
                }
                else if (DeviceNo == "BMPTJQSB-05-123")
                {
                    sql =
                        @"select  CarModelCode,productionNumber ,itemNumber,[description]  ,assemblyDate ,BillNo ,
                        partType ,ColorName ,BarCode ,Sort,ColorCode ,CheckTime  from t_JIS_Assemble 
                        where IsCheck = 1 and PreCheck = 0 and IsOnline =0 and CarmodelCode = 'H247-S' order by CheckTime";
                }

                return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting2(), CommandType.Text, sql, null).Tables[0];
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }

        /// <summary>
        /// 前保
        /// </summary>
        /// <param name="DeviceNo"></param>
        /// <returns></returns>
        public bool InsertPlan_F(string DeviceNo)
        {
            try
            {
                PunchMFAModel md = new PunchMFAModel();
                string sql = "";

                SqlParameter[] param = new SqlParameter[13];

                DataTable dt = SearchPlan_F(DeviceNo);
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        md.CarModelCode = dt.Rows[i]["CarModelCode"].ToString();
                        md.productionNumber = dt.Rows[i]["productionNumber"].ToString();
                        md.itemNumber = dt.Rows[i]["itemNumber"].ToString();
                        md.description = dt.Rows[i]["description"].ToString();
                        md.assemblyDate = Convert.ToDateTime(dt.Rows[i]["assemblyDate"].ToString());
                        md.BillNo = dt.Rows[i]["BillNo"].ToString();
                        md.partType = dt.Rows[i]["partType"].ToString();
                        md.ColorName = dt.Rows[i]["ColorName"].ToString();
                        md.BarCode = dt.Rows[i]["BarCode"].ToString();
                        md.Sort = Convert.ToInt32(dt.Rows[i]["Sort"].ToString());
                        md.ColorCode = dt.Rows[i]["ColorCode"].ToString();
                        md.CheckTime = Convert.ToDateTime(dt.Rows[i]["CheckTime"].ToString());
                        md.DeviceNo = DeviceNo;

                        sql =
                            @"IF NOT  EXISTS(SELECT * FROM [dbo].[tb_PunchNGCCPlan] WHERE productionNumber=@productionNumber AND Sort=@Sort) INSERT INTO [dbo].[tb_PunchNGCCPlan] ([CarModelCode],[productionNumber],[itemNumber],
                              [description],[assemblyDate],[BillNo],[partType],[ColorName],[BarCode],[Sort],[ColorCode],[CheckTime],[DeviceNo]) VALUES (@CarModelCode,@productionNumber,@itemNumber,@description,@assemblyDate,
                                @BillNo,@partType,@ColorName,@BarCode,@Sort,@ColorCode,@CheckTime,@DeviceNo)";

                        param[0] = new SqlParameter("CarModelCode", SqlDbType.VarChar);
                        param[0].Value = md.CarModelCode;

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

                        param[2] = new SqlParameter("itemNumber", SqlDbType.VarChar);
                        param[2].Value = md.itemNumber;

                        param[3] = new SqlParameter("@description", SqlDbType.VarChar);
                        param[3].Value = md.description;

                        param[4] = new SqlParameter("assemblyDate", SqlDbType.DateTime);
                        param[4].Value = md.assemblyDate;

                        param[5] = new SqlParameter("@BillNo", SqlDbType.VarChar);
                        param[5].Value = md.BillNo;

                        param[6] = new SqlParameter("partType", SqlDbType.VarChar);
                        param[6].Value = md.partType;

                        param[7] = new SqlParameter("@ColorName", SqlDbType.VarChar);
                        param[7].Value = md.ColorName;

                        param[8] = new SqlParameter("BarCode", SqlDbType.VarChar);
                        param[8].Value = md.BarCode;

                        param[9] = new SqlParameter("@Sort", SqlDbType.Int);
                        param[9].Value = md.Sort;

                        param[10] = new SqlParameter("ColorCode", SqlDbType.VarChar);
                        param[10].Value = md.ColorCode;

                        param[11] = new SqlParameter("@CheckTime", SqlDbType.DateTime);
                        param[11].Value = md.CheckTime;

                        param[12] = new SqlParameter("DeviceNo", SqlDbType.VarChar);
                        param[12].Value = md.DeviceNo;

                        SqlHelper.ExecuteNonQuery(SqlHelper.GetConnSting(), CommandType.Text, sql, param);
                    }
                }

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

        public DataTable NGCC(string DeviceNo)
        {
            try
            {
                string sql = @"select top 1 * from [dbo].[tb_PunchNGCCPlan] where IsFinish=0 and DeviceNo='" + DeviceNo + "' order by ID";

                return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0];
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }

        public bool UpFinish_F(int ID)
        {
            try
            {
                string sql = @"update tb_PunchNGCCPlan set IsFinish=1 where ID=" + ID;

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

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