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.painting
{
    public class PaintBarCodeDAL
    {
        public static string TableName = "tb_PaintBarCode";

        public DataTable SearchInfoAll(string banci,string productdate)
        {
            try
            {
                string sql = @"SELECT dbo.tb_PaintBarCode.OneBarCode, dbo.tb_Product.ProductName, dbo.tb_PaintBarCode.ColorDes, 
                                dbo.tb_PaintBarCode.Side, dbo.tb_PaintBarCode.workClass
                                FROM dbo.tb_PaintBarCode LEFT OUTER JOIN
                                dbo.tb_Product ON dbo.tb_PaintBarCode.ProductID = dbo.tb_Product.ProductID where workClass=@workClass and ProductDate=@ProductDate";
                SqlParameter[] param = new SqlParameter[2];

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

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


                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="banci"></param>
        /// <param name="productdate"></param>
        /// <returns></returns>
        public DataTable SearchBarCode(string productdate,string productID)
        {
            try
            {
                string sql = @"SELECT dbo.tb_PaintBarCode.OneBarCode, dbo.tb_Product.ProductName, dbo.tb_PaintBarCode.ColorDes, 
                                dbo.tb_PaintBarCode.Side, dbo.tb_PaintBarCode.workClass
                                FROM dbo.tb_PaintBarCode LEFT OUTER JOIN
                                dbo.tb_Product ON dbo.tb_PaintBarCode.ProductID = dbo.tb_Product.ProductID where  ProductDate=@ProductDate and dbo.tb_PaintBarCode.ProductID=@ProductID
                                order by  dbo.tb_PaintBarCode.CreateTime desc";
                SqlParameter[] param = new SqlParameter[2];

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

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

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


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

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

        public bool AddInfo(PaintBarCodeModel md)
        {
            try
            {
                #region 添加数据
                string sql = "";
                SqlParameter[] param = null;
                sql = "INSERT INTO " + TableName + " ([ID] ,[StationID] ,[ProductID],[OneBarCode],[BarCode],[StockNo],[ColorDes],[workClass],[Side] ,[CreateTime],[ProductDate]";
                sql += ") VALUES (";
                sql += "@ID,";
                sql += "@StationID,";
                sql += "@ProductID,";
                sql += "@OneBarCode,";
                sql += "@BarCode,";
                sql += "@StockNo,";
                sql += "@ColorDes,";
                sql += "@workClass,";
                sql += "@Side,";
                sql += "@CreateTime,";
                sql += "@ProductDate)";

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

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

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

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

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

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

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

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

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

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

                param[10] = new SqlParameter("@ProductDate", SqlDbType.VarChar);
                param[10].Value = md.ProductDate;
                #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 UpdatePrintTime(PaintBarCodeModel md)
        {
            try
            {
                #region 添加数据
                string sql = "";
                SqlParameter[] param = null;
                sql = @"update " + TableName + " set PrintTime=@PrintTime where ID=@ID";

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

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

                
                #endregion

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