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 BoxDAL
    {
        public static string TableName = "tb_Box_247";
        public bool AddInfo(BoxModel md)
        {
            try
            {
                #region 添加数据
                string sql = "";
                SqlParameter[] param = null;
                sql = "INSERT INTO " + TableName + " (ID,BoxNo,PartNo,PartName,BatchNo,BoxCount,Des,Flag,Validity,ValidityDate";
                sql += ") VALUES (";
                sql += "@ID,";
                sql += "@BoxNo,";
                sql += "@PartNo,";
                sql += "@PartName,";
                sql += "@BatchNo,";
                sql += "@BoxCount,";
                sql += "@Des,";
                sql += "@Flag,";
                sql += "@Validity,";
                sql += "@ValidityDate)";

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

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

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

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

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

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

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

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

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

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

                #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 SearchByNo(string PartNo,string BatchNo)
        {
            try
            {
                string sql = @"SELECT top 1 * from " + TableName + " where PartNo=@PartNo and SUBSTRING(BoxNo,13,6)=@BatchNo order by CreateTime";
                SqlParameter[] param = new SqlParameter[2];

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

                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 bool UpdateInfo(BoxModel md)
        {
            try
            {
                string sql = "";
                SqlParameter[] param = null;
                sql = "update " + TableName + "  set PrintTime=(select getdate()) where PartNo=@PartNo and PrintTime is null";
                
                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 DataTable SearchBox(string partno)
        {
            try
            {
                string sql = @"SELECT top 1 * from " + TableName + " where PartNo=@PartNo and Flag=0 order by CreateTime";
                SqlParameter[] param = null;

                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 DataTable SearchBoxIsExsit(string box)
        {
            try
            {
                string sql = @"SELECT  * from " + TableName + " where BoxNo=@BoxNo";
                SqlParameter[] param = null;

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

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