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

namespace MESClassLibrary.DAL.Mistake247
{
    public class AssemblyDAL
    {
        public static string TableName = "tb_Assembly_247";
        public static string TableName1 = "tb_Assembly_243";

        public bool AddInfo(AssemblyModel md)
        {
            try
            {
                #region 添加数据
                string sql = "";
                SqlParameter[] param = null;
                sql = "INSERT INTO " + TableName + " (ID,PartNo,PartName,BatchNo,Color,ColorNo,PartType,TapeBatchNo,Validity,ValidityDate,Des,Flag,DeviceNo";
                sql += ") VALUES (";
                sql += "@ID,";
                sql += "@PartNo,";
                sql += "@PartName,";
                sql += "@BatchNo,";
                sql += "@Color,";
                sql += "@ColorNo,";
                sql += "@PartType,";
                sql += "@TapeBatchNo,";
                sql += "@Validity,";
                sql += "@ValidityDate,";
                sql += "@Des,";
                sql += "@Flag,";
                sql += "@DeviceNo)";

                #region 添加参数
                param = new SqlParameter[13];
                param[0] = new SqlParameter("@ID", SqlDbType.VarChar);
                param[0].Value = md.ID;

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

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

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

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

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

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

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

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

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

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

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

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

                #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 UpdateInfo(AssemblyModel md)
        {
            string sql = "";
            SqlParameter[] param = null;

            try
            {
                sql = "update " + TableName + " set PrintTime=(select getdate()) where substring (PartNo,1,11)= @PartNo and (PrintTime is null or PrintTime='')";

                param = new SqlParameter[1];
                param[0] = new SqlParameter("@PartNo", SqlDbType.VarChar);
                param[0].Value = md.PartNo.Substring(0,11);

                SqlHelper.ExecuteNonQuery(SqlHelper.GetConnSting(), CommandType.Text, sql, param);
                return true;
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return false;
            }
            
        }

        public DataTable SearchCount(string PartNo)
        {
            try
            {
                string sql = @"SELECT count(*) as sum from tb_Assembly_247 where PartNo=@PartNo and IsPrintBox=0";
                SqlParameter[] param = new SqlParameter[1];

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


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

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

        public bool UpdatePrintFlag(AssemblyModel md)
        {
            string sql = "";
            SqlParameter[] param = null;

            try
            {
                sql = "update " + TableName + " set IsPrintBox=1 where PartNo=@PartNo and IsPrintBox =0";

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

                SqlHelper.ExecuteNonQuery(SqlHelper.GetConnSting(), CommandType.Text, sql, param);
                return true;
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return false;
            }
        }

        public bool UpdatePrintFlag1(string  partno)
        {
            string sql = "";
            SqlParameter[] param = null;

            try
            {
                sql = "update " + TableName + " set IsPrintBox=1 where PartNo=@PartNo and IsPrintBox =0";

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

                SqlHelper.ExecuteNonQuery(SqlHelper.GetConnSting(), CommandType.Text, sql, param);
                return true;
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return false;
            }
        }

        public DataTable searchPartNo(string partNo)
        {
            try
            {
                string sql = @"SELECT top 1 PartNo from tb_Assembly_247 where PartNo like '" + partNo + "%' and Flag=0 order by CreateTime desc";
                //SqlParameter[] param = new SqlParameter[1];

                //param[0] = new SqlParameter("@PartNo", SqlDbType.VarChar);
                //param[0].Value = partNo;


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

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

        /// <summary>
        /// 根据总成条码查询是否进行视觉检测
        /// </summary>
        /// <param name="barcode"></param>
        /// <returns></returns>
        public DataTable IsVisual(string barcode)
        {
            try
            {
                string sql = @"select ToVisual from " + TableName + " where PartNo='" + barcode + @"'";
                return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0];
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(),MethodBase.GetCurrentMethod());
                return null;
            }
        }

        public DataTable SearchPrintByDeviceNo(string DeviceNo1)
        {
            try
            {
                string sql =
                    @"select * from " + TableName + " where ToVisual=1 and DeviceNo ='" + DeviceNo1 + @"' ";
                return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0];
               
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }

        public DataTable GetDeviceNo(string DeviceNo1, string DeviceNo2, string DeviceNo3, string DeviceNo4)
        {
            try
            {
                string sql =
                    @"select top 1 * from " + TableName + " where ToVisual=0 and DeviceNo in ('" + DeviceNo1 + @"','" + DeviceNo2 + @"','" + DeviceNo3 + @"','" + DeviceNo4 + @"') and PrintTime is null  order by FinishTime 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 IsHavePrint(string DeviceNo1, string DeviceNo2, string DeviceNo3, string DeviceNo4)
        {
            string sql =
                @"select * from " + TableName + " where ToVisual=0 and DeviceNo in ('" + DeviceNo1 + @"','" + DeviceNo2 + @"','" + DeviceNo3 + @"','" + DeviceNo4 + @"') and isNull(PrintTime,'')=''  order by CreateTime desc";
            return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0];

        }

        public bool UpdatePrintByID(string id)
        {
            try
            {
                string sql = @"update " + TableName + " set PrintTime=(select getdate()) where ID='" + id + @"'";

                SqlHelper.ExecuteNonQuery(SqlHelper.GetConnSting(), CommandType.Text, sql, null);
                return true;
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(),MethodBase.GetCurrentMethod());
                return false;
            }
        }

        #region 243

        public DataTable searchPartNo1(string partNo)
        {
            try
            {
                string sql = @"SELECT top 1 * from tb_Assembly_243 where PartNo like '%" + partNo + "%' and Flag=0 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 bool AddInfo1(AssemblyModel md)
        {
            try
            {
                #region 添加数据
                string sql = "";
                SqlParameter[] param = null;
                sql = "INSERT INTO " + TableName1 + " (ID,PartNo,PartName,BatchNo,Color,ColorNo,PartType,TapeBatchNo,Validity,ValidityDate,Des,Flag";
                sql += ") VALUES (";
                sql += "@ID,";
                sql += "@PartNo,";
                sql += "@PartName,";
                sql += "@BatchNo,";
                sql += "@Color,";
                sql += "@ColorNo,";
                sql += "@PartType,";
                sql += "@TapeBatchNo,";
                sql += "@Validity,";
                sql += "@ValidityDate,";
                sql += "@Des,";
                sql += "@Flag)";

                #region 添加参数
                param = new SqlParameter[12];
                param[0] = new SqlParameter("@ID", SqlDbType.VarChar);
                param[0].Value = md.ID;

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

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

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

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

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

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

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

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

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

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

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

                #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 UpdateInfo1(AssemblyModel md)
        {
            string sql = "";
            SqlParameter[] param = null;

            try
            {
                sql = "update " + TableName1 + " set PrintTime=(select getdate()) where substring (PartNo,1,11)= @PartNo and (PrintTime is null or PrintTime='')";

                param = new SqlParameter[1];
                param[0] = new SqlParameter("@PartNo", SqlDbType.VarChar);
                param[0].Value = md.PartNo.Substring(0, 11);

                SqlHelper.ExecuteNonQuery(SqlHelper.GetConnSting(), CommandType.Text, sql, param);
                return true;
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return false;
            }

        }

        #endregion
    }
}