using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MESClassLibrary.BLL.BasicInfo;
using System.Net.NetworkInformation;
using System.IO;
using GlacialComponents.Controls;
using MESClassLibrary.Model;
using grdesLib;
using grproLib;
using System.Configuration;
using System.Diagnostics;
using System.Diagnostics.Eventing.Reader;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using MESClassLibrary.BLL.Log;
using MESClassLibrary.DAL;
using MESClassLibrary.EFModel;

namespace InjectionPC
{
    public partial class FrmBarCodeNew : Form
    {
        private string _printTemplateName = string.Empty;
        private DataTable _stationTable = null;

        public FrmBarCodeNew()
        {
            InitializeComponent();
        }
        private bool _weightOutLine = false;
        public string oldTime = "", newTime = "",tempTime="";
        public static bool IsStart = true;
        public string planID = "";
        public static string code_g = "";
        private SynchronizationContext _syncontext = SynchronizationContext.Current;

        private void FrmBarCodeNew_Load(object sender, EventArgs e)
        {
            try
            {
                _printTemplateName = Application.StartupPath + "\\Report\\111 - 副本.grf";
                var serverUrl = "http://10.60.101.10:8013/";
                var updateXmlFileName = "InjectionUpdate.xml";
                var updater = new AutoUpdater();
                //if (updater.CheckUpdateLoad(serverUrl, updateXmlFileName))
                //{
                //    Environment.Exit(0);
                //}

                //string serialNo  = BasicDAL.GetSjBarCodeSerialNo("2200000175240319", "240716");
                _syncontext = SynchronizationContext.Current;
                lbVer.Text = Application.ProductVersion;
                Control.CheckForIllegalCrossThreadCalls = false;
                Thread t = new Thread(new ThreadStart(TimeGo));
                t.Start();
                timer1.Interval = Program.interVal;
                timer1.Enabled = true;
                label6.Text = Program.station;
                label8.Text = Program.OperatorName;
                panel3.Visible = false;
                textBox3.Text = "";
                //comboBox1.Text = "A班";
                initProduct();

                initBarCode();
                SetBatchText();

                ////DateTime aa = GetDateTime();
                //DateTime aa = BasicDAL.GetServerTime();

                //if (aa.Hour >= 8 && aa.Hour < 20)
                //{
                //    Program.Shift = "A班";
                //    Program.ProductDate = aa.ToString("yyyy-MM-dd");
                //}
                //else
                //{
                //    Program.Shift = "B班";
                //    if (aa.Hour >= 0 && aa.Hour < 8)
                //    {
                //        Program.ProductDate =aa.AddDays(-1).ToString("yyyy-MM-dd");
                //        //Program.ProductDate = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
                //    }
                //    else
                //    {
                //        Program.ProductDate = aa.ToString("yyyy-MM-dd");
                //        //Program.ProductDate = DateTime.Now.ToString("yyyy-MM-dd");
                //    }
                //}
                //comboBox1.Text = Program.Shift;

                //textBox1.Text = Program.ProductDate.Substring(2, 2) + Program.ProductDate.Substring(5, 2) +
                //                Program.ProductDate.Substring(8, 2);

                if (IsStart == false)
                {
                    timer1.Enabled = false;
                    label12.Text = "开始打印";
                    label12.BackColor = System.Drawing.Color.Gray;
                }

                else
                {
                    timer1.Enabled = true;
                    label12.Text = "暂停打印";
                    label12.BackColor = System.Drawing.Color.DeepSkyBlue;
                }

                StationBLL sbll = new StationBLL();
                _stationTable = sbll.SearchAllInfo();
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(),MethodBase.GetCurrentMethod());
            }
            
        }

        private void initProduct()
        {
            comboBox2.Items.Clear();

            ProductBLL bll=new ProductBLL();

            DataTable dt = bll.SearchInfoByType("2000", Program.station);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    comboBox2.Items.Add(dt.Rows[i]["ProductName"].ToString());
                }
            }
            dt.Dispose();
        }

        private void initBarCode ()
        {
            BarCodeBLL bll=new BarCodeBLL();

            glacialList1.Items.Clear();
            DataTable dt = bll.SearchInfo(Program.station);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    glacialList1.Items.Add(i.ToString());
                    glacialList1.Items[i].SubItems[0].Text = dt.Rows[i]["BarCode"].ToString();
                    glacialList1.Items[i].SubItems[1].Text = dt.Rows[i]["ProductName"].ToString();
                    glacialList1.Items[i].SubItems[2].Text = dt.Rows[i]["PrintType"].ToString();
                    glacialList1.Items[i].SubItems[3].Text = dt.Rows[i]["CreateTime"].ToString() == "" ? "" : Convert.ToDateTime(dt.Rows[i]["CreateTime"].ToString()).ToString("yyyy-MM-dd HH:mm:ss");
                    //glacialList1.Items[i].SubItems[3].Text = dt.Rows[i]["CreateTime"].ToString() == "" ? "" : dt.Rows[i]["CreateTime"].ToString("yyyy-MM-dd HH:mm:ss.fff"); 
                    //glacialList1.Refresh();
                }
            }
        }

        private void label9_Click(object sender, EventArgs e)
        {
            //this.Hide();
            Form fr=new FrmQuality();
            fr.Show();
        }

        private void TimeGo()
        {
            System.Timers.Timer timer = new System.Timers.Timer();
            timer.Interval = 1000;
            timer.Enabled = true;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Tick);
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            _syncontext.Send(rst1 =>
            {
                label2.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                Thread.Sleep(500);
            }, null);
            
        }
       
        private void timer1_Tick(object sender, EventArgs e)
        {
            BarCodeBLL Barbll = new BarCodeBLL();
            BarCodeModel md = new BarCodeModel();
            timer1.Stop();
            Ping p1 = new Ping();
            bool status = false;
            string code = "",OneCode="";
            //NewOneCode 左门槛   NewOneCode1右门槛
            string newcode = "", newcode1 = "", NewOneCode = "", NewOneCode1 = "", printerName = "", rfilename = "", reportName="";
            string[] part=new string[3];
            GridppReport report = new GridppReport();
            long a = 0;
            
            ProductOfInjectionBLL pbll = new ProductOfInjectionBLL();
            ProductOfInjectionModel pmd = new ProductOfInjectionModel();
            InjectionPlanBLL bll = new InjectionPlanBLL();
            BarCodeBLL brbll = new BarCodeBLL();
            ProductBLL productBll =new ProductBLL();

            DataTable pdt3 = null;

            SetBatchText();
           
           
            
            //LogHelper.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            try
            {
                PingReply reply = p1.Send(Program.IP); //发送主机名或Ip地址
                StringBuilder sbuilder;
                if (reply.Status == IPStatus.Success || Program.WeightOutLine==true)
                {
                    //读取称重文件
#warning 测试时将WeightOutLine设置为true可以进行下去
                    if (Program.WeightOutLine == true)
                    {
                        status = true;
                    }
                    else
                    {
                        status = Upload.DoConnComputer(Program.IP, Program.WeightFolder, Program.WeightUser, Program.WeightPsw);
                    }
                    if (status)
                    {
                        //DirectoryInfo theFolder = new DirectoryInfo(@"\\192.168.1.16\aa");
                        //DirectoryInfo theFolder = new DirectoryInfo(@"\\" + Program.IP + "\\UserData");
                        DirectoryInfo theFolder = new DirectoryInfo(@"\\" + Program.IP + "\\" + Program.WeightFolder);
                        //string filename = theFolder.ToString() + "\\ProdHisFile.csv";
                        string filename = theFolder.ToString() + "\\" + Program.WeightFile;
#warning 测试时将WeightOutLine设置为true可以进行下去
                        if (Program.WeightOutLine == true)
                        {
                            filename = Application.StartupPath + "\\Report\\称重.xlsx";
                            oldTime = "2024-06-24 13:22:08";
                        }
                        FileInfo fl = new FileInfo(filename);
                        

                        if (oldTime == "" || oldTime == "none")
                        {
                            oldTime = fl.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
                            tempTime = oldTime;
                        }
                        else
                        {
                            newTime = fl.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
                            tempTime=newTime;
                            TimeSpan s =  Convert.ToDateTime(tempTime)  - Convert.ToDateTime(oldTime);
                            if (s.TotalSeconds<50)
                            {
                                timer1.Interval = Program.interVal;
                                timer1.Start();
                                return;
                            }
                            else
                            {
                                if (newTime != oldTime)
                                {
                                    #region 生成打印条码  1、当前计划的存货代码2、barcode表根据存货代码查找今天的流水

                                    DataTable dt = bll.SearchInfoByName(Program.station);//获取当前计划
                                    if (dt != null && dt.Rows.Count > 0)
                                    {
                                        planID = dt.Rows[0]["InjectionPlanID"].ToString();
                                        DataTable dt2 = null;
                                        if (dt.Rows[0]["ProductName"].ToString().IndexOf("门槛") != -1)
                                        {
                                            //插入两条数据,一个左门槛,一个右门槛
                                            //dt2 = brbll.SearchInfoByStock(dt.Rows[0]["StockNo"].ToString().Trim());//BarCode最大值
                                            //if (dt2 != null && dt2.Rows.Count > 0)
                                            //{
                                            //    code = dt2.Rows[0]["BarCode"].ToString().Trim();
                                            //    newcode = code.Substring(0, code.Length - 4) +
                                            //              (Convert.ToInt32(code.Substring(code.Length - 4, 4)) + 1).ToString()
                                            //              .PadLeft(4, '0');
                                            //    OneCode = dt2.Rows[0]["OneBarCode"].ToString().Trim();
                                            //    NewOneCode = OneCode.Substring(0, OneCode.Length - 4) + (Convert.ToInt32(OneCode.Substring(OneCode.Length - 4, 4)) + 1).ToString().PadLeft(4, '0');
                                            //}
                                            //else
                                            //{
                                            //    ReloadProduceDate();
                                            //    //按照二维码的生成规则
                                            //    newcode = dt.Rows[0]["PartNo"].ToString().Trim() + "." + Program.ProductDate.Substring(2, 2) + Program.ProductDate.Substring(5, 2) + Program.ProductDate.Substring(8, 2) + "." + "0001";
                                            //    NewOneCode = dt.Rows[0]["StockNo"].ToString().Trim() + Program.ProductDate.Substring(2, 2) + Program.ProductDate.Substring(5, 2) + Program.ProductDate.Substring(8, 2) + "0001";
                                            //}

                                            string batch = Program.ProductDate.Substring(2, 2) + Program.ProductDate.Substring(5, 2) + Program.ProductDate.Substring(8, 2);
                                            NewOneCode = BasicDAL.GetSjBarCodeSerialNo(dt.Rows[0]["StockNo"].ToString().Trim(), batch);
                                            newcode = dt.Rows[0]["PartNo"].ToString().Trim() + "." + batch + "." + NewOneCode.Substring(16, 4);

                                            ProductBLL prbll = new ProductBLL();
                                            DataTable prdt = null;
                                            if (dt.Rows[0]["ProductName"].ToString().IndexOf("左") != -1)
                                            {
                                                #region 计划为左门槛
                                                //左门槛,同时生成一个右门槛条码

                                                //t_Product内容
                                                prdt = prbll.SearchInfoByProductName(
                                                                                        dt.Rows[0]["ProductName"].ToString().TrimEnd()
                                                                                            .Substring(0, dt.Rows[0]["ProductName"].ToString().TrimEnd().Length - 3) + "右门槛");
                                                if (prdt != null && prdt.Rows.Count > 0)
                                                {
                                                    //根据存货代码,查询tb_BarCode最大值
                                                    //dt2 = brbll.SearchInfoByStock(prdt.Rows[0]["StockNo"].ToString().Trim());
                                                    //if (dt2 != null && dt2.Rows.Count > 0)
                                                    //{
                                                    //    code = dt2.Rows[0]["BarCode"].ToString().Trim();
                                                    //    newcode1 = code.Substring(0, code.Length - 4) +
                                                    //              (Convert.ToInt32(code.Substring(code.Length - 4, 4)) + 1).ToString()
                                                    //              .PadLeft(4, '0');
                                                    //    OneCode = dt2.Rows[0]["OneBarCode"].ToString().Trim();
                                                    //    NewOneCode1 = OneCode.Substring(0, OneCode.Length - 4) + (Convert.ToInt32(OneCode.Substring(OneCode.Length - 4, 4)) + 1).ToString().PadLeft(4, '0');
                                                    //}
                                                    //else
                                                    //{
                                                    //    //按照二维码的生成规则
                                                    //    newcode1 = prdt.Rows[0]["PartNo"].ToString().Trim() + "." + Program.ProductDate.Substring(2, 2) + Program.ProductDate.Substring(5, 2) + Program.ProductDate.Substring(8, 2) + "." + "0001";
                                                    //    NewOneCode1 = prdt.Rows[0]["StockNo"].ToString().Trim() + Program.ProductDate.Substring(2, 2) + Program.ProductDate.Substring(5, 2) + Program.ProductDate.Substring(8, 2) + "0001";
                                                    //}

                                                    string batch1 = Program.ProductDate.Substring(2, 2) + Program.ProductDate.Substring(5, 2) + Program.ProductDate.Substring(8, 2);
                                                    NewOneCode1 = BasicDAL.GetSjBarCodeSerialNo(prdt.Rows[0]["StockNo"].ToString().Trim(), batch1);
                                                    newcode1 = prdt.Rows[0]["PartNo"].ToString().Trim() + "." + batch1 + "." + NewOneCode1.Substring(16, 4);

                                                    #region 如果顺序号不一致取最大的
                                                    //if (Convert.ToInt32(newcode.Substring(newcode.Length - 4, 4)) !=
                                                    //                                            Convert.ToInt32(newcode1.Substring(newcode1.Length - 4, 4)))
                                                    //{
                                                    //    if (Convert.ToInt32(newcode.Substring(newcode.Length - 4, 4)) > Convert.ToInt32(newcode1.Substring(newcode1.Length - 4, 4)))
                                                    //    {
                                                    //        newcode1 = newcode1.Substring(0, newcode1.Length - 4) +
                                                    //                   newcode.Substring(newcode.Length - 4, 4);
                                                    //        NewOneCode1 = NewOneCode1.Substring(0, NewOneCode1.Length - 4) + newcode.Substring(newcode.Length - 4, 4);
                                                    //    }
                                                    //    else
                                                    //    {
                                                    //        newcode = newcode.Substring(0, newcode.Length - 4) + newcode1.Substring(newcode1.Length - 4, 4);
                                                    //        NewOneCode = NewOneCode.Substring(0, NewOneCode.Length - 4) + newcode1.Substring(newcode1.Length - 4, 4);
                                                    //    }
                                                    //}
                                                    #endregion

                                                    #region 插入tb_barcode表记录

                                                    part = newcode.Split('.');
                                                    md.ID = Guid.NewGuid().ToString();
                                                    md.StationID = dt.Rows[0]["StationID"].ToString();
                                                    md.OneBarCode = NewOneCode;
                                                    md.BarCode = newcode;
                                                    md.StockNo = dt.Rows[0]["StockNo"].ToString();
                                                    md.PrintType = 0;
                                                    md.PlanID = planID;
                                                    md.BatchNo = textBox1.Text;

                                                    DataTable pdt = productBll.SearchIsImportByStockNo(NewOneCode.Substring(0, 10));
                                                    if (pdt != null && pdt.Rows.Count > 0)
                                                    {
                                                        if (pdt.Rows[0]["isImport"].ToString() == "1")
                                                        {
                                                            md.Import = "国产料Kingfa";
                                                        }
                                                        else
                                                        {
                                                            md.Import = "";
                                                        }
                                                    }

                                                    if (Barbll.Add_Info(md))
                                                    {
                                                        #region 打印条码   左

                                                        //filename = @"D:\111 - 副本.grf";
                                                        filename = _printTemplateName;
                                                        DataTable headTable = new DataTable();
                                                        headTable.Columns.Add("StationNo");
                                                        headTable.Columns.Add("OneBarCode");
                                                        headTable.Columns.Add("BarCode");
                                                        headTable.Columns.Add("ProductName");
                                                        headTable.Columns.Add("IsImport");
                                                        DataRow row = headTable.NewRow();
                                                        row["StationNo"] = Program.station;

                                                        row["OneBarCode"] = NewOneCode;
                                                        row["BarCode"] = newcode;
                                                        row["ProductName"] = dt.Rows[0]["ProductName"]+";";
                                                        row["IsImport"] = md.Import;
                                                        reportName = ConfigurationManager.AppSettings["Printer1"];
                                                        headTable.Rows.Add(row);

                                                        //ReportHelper rp = new ReportHelper(filename, headTable, null,
                                                        //    (int)GRPaperOrientation.grpoDefault, 1, reportName);
                                                        //rp.Report.Print(false);

                                                        //string filename_label = Application.StartupPath + "\\Report\\111 - 副本.grf";
                                                        MyReport report2 = new MyReport(filename, headTable, null, reportName);
                                                        report2.Report.Printer.PrinterName = reportName;
                                                        report2.Report.Print(false);

                                                        #endregion

                                                        LogHelper.WriteLog("打印时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ";" + NewOneCode);

                                                        Barbll.Update_Info(md);
                                                    }
                                                    else
                                                    {
                                                        MessageBox.Show("连接数据库超时!");
                                                        timer1.Interval = Program.interVal;
                                                        timer1.Start();
                                                        return;
                                                    }

                                                    #endregion

                                                    #region 插入tb_barcode表记录,另一侧

                                                    part = newcode.Split('.');
                                                    md.ID = Guid.NewGuid().ToString();
                                                    md.StationID = dt.Rows[0]["StationID"].ToString();
                                                    md.OneBarCode = NewOneCode1;
                                                    md.BarCode = newcode1;
                                                    md.StockNo = prdt.Rows[0]["StockNo"].ToString().Trim();
                                                    md.PrintType = 0;
                                                    md.PlanID = planID;
                                                    md.BatchNo = textBox1.Text;

                                                    DataTable pdt1 = productBll.SearchIsImportByStockNo(NewOneCode.Substring(0, 10));
                                                    if (pdt1 != null && pdt1.Rows.Count > 0)
                                                    {
                                                        if (pdt1.Rows[0]["isImport"].ToString() == "1")
                                                        {
                                                            md.Import = "国产料Kingfa";
                                                        }
                                                        else
                                                        {
                                                            md.Import = "";
                                                        }
                                                    }

                                                    if (Barbll.Add_Info(md))
                                                    {
                                                        #region 打印条码  右

                                                        //filename = @"D:\111 - 副本.grf";
                                                        filename = _printTemplateName;
                                                        DataTable headTable1 = new DataTable();
                                                        headTable1.Columns.Add("StationNo");
                                                        headTable1.Columns.Add("OneBarCode");
                                                        headTable1.Columns.Add("BarCode");
                                                        headTable1.Columns.Add("ProductName");
                                                        headTable1.Columns.Add("IsImport");
                                                        DataRow row1 = headTable1.NewRow();
                                                        row1["StationNo"] = Program.station;
                                                        row1["OneBarCode"] = NewOneCode1;
                                                        row1["BarCode"] = newcode1;
                                                        row1["ProductName"] = dt.Rows[0]["ProductName"].ToString().TrimEnd()
                                                                                  .Substring(0, dt.Rows[0]["ProductName"].ToString().TrimEnd().Length - 3) + "右门槛"+";";
                                                        row1["IsImport"] = md.Import;
                                                        reportName = ConfigurationManager.AppSettings["Printer2"];
                                                        headTable1.Rows.Add(row1);

                                                        //ReportHelper rp1 = new ReportHelper(filename, headTable1, null,
                                                        //    (int)GRPaperOrientation.grpoDefault, 1, reportName);
                                                        //rp1.Report.Print(false);

                                                        MyReport report2 = new MyReport(filename, headTable1, null, reportName);
                                                        report2.Report.Printer.PrinterName = reportName;
                                                        report2.Report.Print(false);

                                                        #endregion

                                                        LogHelper.WriteLog("打印时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ";" + NewOneCode1);

                                                        Barbll.Update_Info(md);
                                                    }
                                                    else
                                                    {
                                                        MessageBox.Show("连接数据库超时!");
                                                        timer1.Interval = Program.interVal;
                                                        timer1.Start();
                                                        return;
                                                    }

                                                    #endregion
                                                }
                                                #endregion
                                            }

                                            else
                                            {
                                                #region 计划为右门槛

                                                prdt = prbll.SearchInfoByProductName(
                                                                                        dt.Rows[0]["ProductName"].ToString().TrimEnd()
                                                                                            .Substring(0, dt.Rows[0]["ProductName"].ToString().TrimEnd().Length - 3) + "左");
                                                if (prdt != null && prdt.Rows.Count > 0)
                                                {
                                                    //dt2 = brbll.SearchInfoByStock(prdt.Rows[0]["StockNo"].ToString().Trim());
                                                    //if (dt2 != null && dt2.Rows.Count > 0)
                                                    //{
                                                    //    code = dt2.Rows[0]["BarCode"].ToString().Trim();
                                                    //    newcode1 = code.Substring(0, code.Length - 4) +
                                                    //              (Convert.ToInt32(code.Substring(code.Length - 4, 4)) + 1).ToString()
                                                    //              .PadLeft(4, '0');
                                                    //    OneCode = dt2.Rows[0]["OneBarCode"].ToString().Trim();
                                                    //    NewOneCode1 = OneCode.Substring(0, OneCode.Length - 4) + (Convert.ToInt32(OneCode.Substring(OneCode.Length - 4, 4)) + 1).ToString().PadLeft(4, '0');
                                                    //}
                                                    //else
                                                    //{
                                                    //    ReloadProduceDate();
                                                    //    //按照二维码的生成规则
                                                    //    newcode1 = prdt.Rows[0]["PartNo"].ToString().Trim() + "." + Program.ProductDate.Substring(2, 2) + Program.ProductDate.Substring(5, 2) + Program.ProductDate.Substring(8, 2) + "." + "0001";
                                                    //    NewOneCode1 = prdt.Rows[0]["StockNo"].ToString().Trim() + Program.ProductDate.Substring(2, 2) + Program.ProductDate.Substring(5, 2) + Program.ProductDate.Substring(8, 2) + "0001";
                                                    //}

                                                    string batch1 = Program.ProductDate.Substring(2, 2) + Program.ProductDate.Substring(5, 2) + Program.ProductDate.Substring(8, 2);
                                                    NewOneCode1 = BasicDAL.GetSjBarCodeSerialNo(prdt.Rows[0]["StockNo"].ToString().Trim(), batch1);
                                                    newcode1 = prdt.Rows[0]["PartNo"].ToString().Trim() + "." + batch1 + "." + NewOneCode1.Substring(16, 4);


                                                    #region 如果顺序号不一致取最大的
                                                    //if (Convert.ToInt32(newcode.Substring(newcode.Length - 4, 4)) !=
                                                    //    Convert.ToInt32(newcode1.Substring(newcode1.Length - 4, 4)))
                                                    //{
                                                    //    if (Convert.ToInt32(newcode.Substring(newcode.Length - 4, 4)) > Convert.ToInt32(newcode1.Substring(newcode1.Length - 4, 4)))
                                                    //    {
                                                    //        newcode1 = newcode1.Substring(0, newcode1.Length - 4) +
                                                    //                   newcode.Substring(newcode.Length - 4, 4);
                                                    //        NewOneCode1 = NewOneCode1.Substring(0, NewOneCode1.Length - 4) + newcode.Substring(newcode.Length - 4, 4);
                                                    //    }
                                                    //    else
                                                    //    {
                                                    //        newcode = newcode.Substring(0, newcode.Length - 4) + newcode1.Substring(newcode1.Length - 4, 4);
                                                    //        NewOneCode = NewOneCode.Substring(0, NewOneCode.Length - 4) + newcode1.Substring(newcode1.Length - 4, 4);
                                                    //    }
                                                    //}
                                                    #endregion

                                                    #region 插入tb_barcode表记录

                                                    part = newcode.Split('.');
                                                    md.ID = Guid.NewGuid().ToString();
                                                    md.StationID = dt.Rows[0]["StationID"].ToString();
                                                    md.OneBarCode = NewOneCode;
                                                    md.BarCode = newcode;
                                                    md.StockNo = dt.Rows[0]["StockNo"].ToString();
                                                    md.PrintType = 0;
                                                    md.PlanID = planID;
                                                    md.BatchNo = textBox1.Text;

                                                    DataTable pdt = productBll.SearchIsImportByStockNo(NewOneCode.Substring(0, 10));
                                                    if (pdt != null && pdt.Rows.Count > 0)
                                                    {
                                                        if (pdt.Rows[0]["isImport"].ToString() == "1")
                                                        {
                                                            md.Import = "国产料Kingfa";
                                                        }
                                                        else
                                                        {
                                                            md.Import = "";
                                                        }
                                                    }

                                                    if (Barbll.Add_Info(md))
                                                    {
                                                        #region 打印条码  右

                                                        //filename = @"D:\111 - 副本.grf";
                                                        filename = _printTemplateName;
                                                        DataTable headTable = new DataTable();
                                                        headTable.Columns.Add("StationNo");
                                                        headTable.Columns.Add("OneBarCode");
                                                        headTable.Columns.Add("newcode");
                                                        headTable.Columns.Add("ProductName");
                                                        headTable.Columns.Add("IsImport");
                                                        DataRow row = headTable.NewRow();
                                                        row["StationNo"] = Program.station;
                                                        row["OneBarCode"] = NewOneCode;
                                                        row["BarCode"] = newcode;
                                                        row["ProductName"] = dt.Rows[0]["ProductName"].ToString()+";";
                                                        row["IsImport"] = md.Import;
                                                        reportName = ConfigurationManager.AppSettings["Printer2"];
                                                        headTable.Rows.Add(row);

                                                        //ReportHelper rp = new ReportHelper(filename, headTable, null,
                                                        //    (int)GRPaperOrientation.grpoDefault, 1, reportName);
                                                        //rp.Report.Print(false);

                                                        MyReport report2 = new MyReport(filename, headTable, null, reportName);
                                                        report2.Report.Printer.PrinterName = reportName;
                                                        report2.Report.Print(false);
                                                        #endregion

                                                        LogHelper.WriteLog("打印时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ";" + NewOneCode);

                                                        Barbll.Update_Info(md);
                                                    }
                                                    else
                                                    {
                                                        MessageBox.Show("连接数据库超时!");
                                                        timer1.Interval = Program.interVal;
                                                        timer1.Start();
                                                        return;
                                                    }

                                                    #endregion

                                                    #region 插入tb_barcode表记录

                                                    part = newcode.Split('.');
                                                    md.ID = Guid.NewGuid().ToString();
                                                    md.StationID = dt.Rows[0]["StationID"].ToString();
                                                    md.OneBarCode = NewOneCode1;
                                                    md.BarCode = newcode1;
                                                    md.StockNo = prdt.Rows[0]["StockNo"].ToString();
                                                    md.PrintType = 0;
                                                    md.PlanID = planID;
                                                    md.BatchNo = textBox1.Text;

                                                    DataTable pdt2 = productBll.SearchIsImportByStockNo(NewOneCode.Substring(0, 10));
                                                    if (pdt2 != null && pdt2.Rows.Count > 0)
                                                    {
                                                        if (pdt2.Rows[0]["isImport"].ToString() == "1")
                                                        {
                                                            md.Import = "国产料Kingfa";
                                                        }
                                                        else
                                                        {
                                                            md.Import = "";
                                                        }
                                                    }

                                                    if (Barbll.Add_Info(md))
                                                    {
                                                        #region 打印条码  左

                                                        //filename = @"D:\111 - 副本.grf";
                                                        filename = _printTemplateName;
                                                        DataTable headTable1 = new DataTable();
                                                        headTable1.Columns.Add("StationNo");
                                                        headTable1.Columns.Add("OneBarCode");
                                                        headTable1.Columns.Add("newcode");
                                                        headTable1.Columns.Add("ProductName");
                                                        headTable1.Columns.Add("IsImport");
                                                        DataRow row1 = headTable1.NewRow();
                                                        row1["StationNo"] = Program.station;
                                                        row1["OneBarCode"] = NewOneCode1;
                                                        row1["BarCode"] = newcode1;
                                                        row1["ProductName"] = dt.Rows[0]["ProductName"].ToString().TrimEnd()
                                                                                  .Substring(0, dt.Rows[0]["ProductName"].ToString().TrimEnd().Length - 3) + "左门槛"+";";
                                                        row1["IsImport"] = md.Import;
                                                        reportName = ConfigurationManager.AppSettings["Printer1"];
                                                        headTable1.Rows.Add(row1);

                                                        //ReportHelper rp1 = new ReportHelper(filename, headTable1, null,
                                                        //    (int)GRPaperOrientation.grpoDefault, 1, reportName);
                                                        //rp1.Report.Print(false);

                                                        MyReport report2 = new MyReport(filename, headTable1, null, reportName);
                                                        report2.Report.Printer.PrinterName = reportName;
                                                        report2.Report.Print(false);
                                                        #endregion

                                                        LogHelper.WriteLog("打印时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ";" + NewOneCode1);

                                                        Barbll.Update_Info(md);
                                                    }
                                                    else
                                                    {
                                                        MessageBox.Show("连接数据库超时!");
                                                        timer1.Interval = Program.interVal;
                                                        timer1.Start();
                                                        return;
                                                    }

                                                    #endregion

                                                }
                                                #endregion
                                            }
                                        }
                                        else
                                        {
                                            #region 不是门槛

                                            //dt2 = brbll.SearchInfoByStock(dt.Rows[0]["StockNo"].ToString().Trim());
                                            //if (dt2 != null && dt2.Rows.Count > 0)
                                            //{
                                            //    code = dt2.Rows[0]["BarCode"].ToString().Trim();
                                            //    newcode = code.Substring(0, code.Length - 4) +
                                            //              (Convert.ToInt32(code.Substring(code.Length - 4, 4)) + 1).ToString()
                                            //              .PadLeft(4, '0');
                                            //    OneCode = dt2.Rows[0]["OneBarCode"].ToString().Trim();
                                            //    NewOneCode = OneCode.Substring(0, OneCode.Length - 4) + (Convert.ToInt32(OneCode.Substring(OneCode.Length - 4, 4)) + 1).ToString().PadLeft(4, '0');
                                            //}
                                            //else
                                            //{
                                            //    ReloadProduceDate();
                                            //    //按照二维码的生成规则
                                            //    newcode = dt.Rows[0]["PartNo"].ToString().Trim() + "." + Program.ProductDate.Substring(2, 2) + Program.ProductDate.Substring(5, 2) + Program.ProductDate.Substring(8, 2) + "." + "0001";
                                            //    NewOneCode = dt.Rows[0]["StockNo"].ToString().Trim() + Program.ProductDate.Substring(2, 2) + Program.ProductDate.Substring(5, 2) + Program.ProductDate.Substring(8, 2) + "0001";
                                            //}

                                            string batch = Program.ProductDate.Substring(2, 2) + Program.ProductDate.Substring(5, 2) + Program.ProductDate.Substring(8, 2);
                                            NewOneCode = BasicDAL.GetSjBarCodeSerialNo(dt.Rows[0]["StockNo"].ToString().Trim(), batch);
                                            newcode = dt.Rows[0]["PartNo"].ToString().Trim() + "." + batch + "." + NewOneCode.Substring(16, 4);


                                            //dt2.Dispose();
                                            //插入tb_barcode表记录

                                            part = newcode.Split('.');
                                            md.ID = Guid.NewGuid().ToString();
                                            md.StationID = dt.Rows[0]["StationID"].ToString();
                                            md.OneBarCode = NewOneCode;
                                            md.BarCode = newcode;
                                            md.StockNo = dt.Rows[0]["StockNo"].ToString();
                                            md.PrintType = 0;
                                            md.PlanID = planID;
                                            md.BatchNo = textBox1.Text;

                                            DataTable pdt = productBll.SearchIsImportByStockNo(NewOneCode.Substring(0, 10));//查tb_Product表信息
                                            if (pdt != null && pdt.Rows.Count > 0)
                                            {
                                                if (pdt.Rows[0]["isImport"].ToString() == "1")
                                                {
                                                    md.Import = "国产料Kingfa";
                                                }
                                                else
                                                {
                                                    md.Import = "";
                                                }
                                            }
                                            if (Barbll.Add_Info(md))
                                            {
                                                #region 打印条码

                                                //filename = @"D:\111 - 副本.grf";
                                                filename = _printTemplateName;
                                                DataTable headTable = new DataTable();
                                                headTable.Columns.Add("StationNo");
                                                headTable.Columns.Add("OneBarCode");
                                                headTable.Columns.Add("BarCode");
                                                headTable.Columns.Add("ProductName");
                                                headTable.Columns.Add("IsImport");
                                                DataRow row = headTable.NewRow();
                                                row["StationNo"] = Program.station;

                                                row["OneBarCode"] = NewOneCode;
                                                row["BarCode"] = newcode;
                                                row["ProductName"] = dt.Rows[0]["ProductName"]+";";
                                                row["IsImport"] = md.Import;
                                                reportName = ConfigurationManager.AppSettings["Printer1"];
                                                headTable.Rows.Add(row);

                                                ReportHelper rp = new ReportHelper(filename, headTable, null,
                                                    (int)GRPaperOrientation.grpoDefault, 1, reportName);
                                                rp.Report.Print(false);

                                                #endregion

                                                LogHelper.WriteLog("打印时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ";" + NewOneCode);

                                                Barbll.Update_Info(md);//更新打印时间
                                            }
                                            else
                                            {
                                                MessageBox.Show("连接数据库超时!");
                                                timer1.Interval = Program.interVal;
                                                timer1.Start();
                                                return;
                                            }

                                            #endregion
                                        }
                                    }

                                    #endregion
                                    oldTime = newTime;
                                }
                            }
                        }

                        initBarCode();
                        
                    }
                    else
                    {
                        MessageBox.Show("连接称重仪失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        timer1.Interval = Program.interVal;
                        timer1.Start();
                    }
                }
                else
                {
                    MessageBox.Show("连接称重仪失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    timer1.Interval = Program.interVal;
                    timer1.Start();
                }
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK);

                //MessageBox.Show("数据库连接失败!", "提示", MessageBoxButtons.OK);
                timer1.Interval = Program.interVal;
                timer1.Start();

            }
            timer1.Interval = Program.interVal;
            timer1.Start();
        }
        //void ReloadProduceDate()
        //{
        //    if (DateTime.Parse(Program.ProductDate).ToString("yyyy-MM-dd") != DateTime.Now.ToString("yyyy-MM-dd"))
        //    {
        //        FrmLogin.SetProductDate();
        //    }
        //}

        private void label12_Click(object sender, EventArgs e)
        {
            try
            {
                if (label12.Text == "暂停打印")
                {
                    DialogResult result = MessageBox.Show("确认暂停打印?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    if (result == DialogResult.OK)
                    {
                        timer1.Stop();
                        IsStart = false;
                        label12.Text = "开始打印";
                        label12.BackColor = System.Drawing.Color.Gray;
                    }
                    else
                    {
                        return;
                    }
                }

                else
                {
                    DialogResult result = MessageBox.Show("确认开始打印?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    if (result == DialogResult.OK)
                    {
                        IsStart = true;
                        timer1.Start();
                        label12.Text = "暂停打印";
                        label12.BackColor = System.Drawing.Color.DeepSkyBlue;
                    }
                    else
                    {
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
            }
        }

        /// <summary>
        /// 手工打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void label19_Click(object sender, EventArgs e)
        {
            string code = "",stationID="",OneCode="";
            string newcode = "", partNo = "", stockNo = "", NewOneCode = "", filename = "", reportName = "", ProductName="";

            try
            {
                #region 数据合法性判断

                if (comboBox2.Text.Trim() == "")
                {
                    MessageBox.Show("请选择产品!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                if (textBox1.Text.Trim() == "")
                {
                    MessageBox.Show("请输入批次!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                if (textBox1.Text.Trim().Length != 6)
                {
                    MessageBox.Show("批次格式不正确!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                if (textBox2.Text.Trim() == "")
                {
                    MessageBox.Show("请输入数量!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (!IsNumeric(textBox2.Text.Trim()))
                {
                    MessageBox.Show("数量格式不正确!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                if (Convert.ToInt32(textBox2.Text.Trim()) > 10)
                {
                    MessageBox.Show("数量不能大于10!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                #endregion

                #region 获取工位编号

                //StationBLL sbll = new StationBLL();
                //DataTable sdt = sbll.SearchInfoByNo(Program.station);
                //if (sdt != null && sdt.Rows.Count > 0)
                //{
                //    stationID = sdt.Rows[0]["StationID"].ToString();
                //}
                //sdt.Dispose();

                List<DataRow> stationList = _stationTable.AsEnumerable().Where(p => p.Field<string>("StationNo") == Program.station).ToList();
                if (stationList != null && stationList.Count > 0)
                {
                    stationID = stationList[0]["StationID"].ToString();
                }

                #endregion

                DialogResult result = MessageBox.Show("确定手工打印条码?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (result == DialogResult.OK)
                {
                    #region 生成条码
                    SetBatchText();
                    ProductBLL pbll = new ProductBLL();

                    //查询零件号
                    DataTable pdt = pbll.SearchInfoByName(comboBox2.Text, "2000");//tb_product
                    if (pdt != null && pdt.Rows.Count > 0)
                    {
                        partNo = pdt.Rows[0]["PartNo"].ToString().Trim();
                        stockNo = pdt.Rows[0]["StockNo"].ToString().Trim();
                        //ProductName = pdt.Rows[0]["ProductName"].ToString().Trim();
                    }

                    pdt.Dispose();



                    //根据零件号、批次号查找

                    BarCodeBLL bll = new BarCodeBLL();
                    BarCodeModel md = new BarCodeModel();

                    for (int i = 1; i <= Convert.ToInt32(textBox2.Text.Trim()); i++)
                    {

                        string batch = textBox1.Text.Trim();
                        NewOneCode = BasicDAL.GetSjBarCodeSerialNo(stockNo, batch);
                        newcode = partNo + "." + batch + "." + NewOneCode.Substring(16, 4);
                        string[] part = newcode.Split('.');
                        //存入tb_BarCode表
                        md.ID = Guid.NewGuid().ToString();
                        md.StationID = stationID;
                        md.OneBarCode = NewOneCode;
                        md.BarCode = newcode;

                        md.StockNo = stockNo;
                        md.PrintType = 1;
                        md.BatchNo = textBox1.Text.Trim();

                        DataTable pdt1 = pbll.SearchIsImportByStockNo(NewOneCode.Substring(0, 10));
                        if (pdt1 != null && pdt1.Rows.Count > 0)
                        {
                            if (pdt1.Rows[0]["isImport"].ToString() == "1")
                            {
                                md.Import = "国产料Kingfa";
                            }
                            else
                            {
                                md.Import = "";
                            }
                        }

                        if (bll.Add_Info(md))
                        {
                            #region 打印条码

                            //filename = @"D:\111 - 副本.grf";
                            filename = _printTemplateName;
                            DataTable headTable = new DataTable();
                            headTable.Columns.Add("StationNo");
                            headTable.Columns.Add("OneBarCode");
                            headTable.Columns.Add("BarCode");
                            headTable.Columns.Add("ProductName");
                            headTable.Columns.Add("IsImport");
                            DataRow row = headTable.NewRow();
                            row["StationNo"] = Program.station;

                            row["OneBarCode"] = NewOneCode;
                            row["BarCode"] = newcode;
                            row["ProductName"] = comboBox2.Text + ";";
                            row["IsImport"] = md.Import;
                            if (comboBox2.Text.Contains("右"))
                            {
                                reportName = ConfigurationManager.AppSettings["Printer2"];
                            }
                            else
                            {
                                reportName = ConfigurationManager.AppSettings["Printer1"];
                            }

                            headTable.Rows.Add(row);

                            ReportHelper rp = new ReportHelper(filename, headTable, null,
                                (int)GRPaperOrientation.grpoDefault, 1, reportName);
                            rp.Report.Print(false);

                            #endregion

                            LogHelper.WriteLog("打印时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ";" + NewOneCode);

                            bll.Update_Info(md);
                        }
                        else
                        {
                            MessageBox.Show("连接数据库超时!");
                            return;
                        }
                    }




                    // DataTable dt = bll.SearchSerialNoByBarCode(stockNo, textBox1.Text.Trim()); //tb_barCode  stockNo和批次
                    ////tb_barcode有历史记录
                    // if (dt != null && dt.Rows.Count > 0)
                    // {
                    //     OneCode = dt.Rows[0]["OneBarCode"].ToString().Trim();
                    //     code = dt.Rows[0]["BarCode"].ToString().TrimEnd();
                    //     for (int i = 1; i <= Convert.ToInt32(textBox2.Text.Trim()); i++)
                    //     {
                    //         newcode = code.Substring(0, code.Length - 4) +
                    //                   (Convert.ToInt32(code.Substring(code.Length - 4, 4)) + i)
                    //                   .ToString()
                    //                   .PadLeft(4, '0');
                    //         NewOneCode = OneCode.Substring(0, OneCode.Length - 4) +
                    //                      (Convert.ToInt32(OneCode.Substring(OneCode.Length - 4, 4)) + i).ToString()
                    //                      .PadLeft(4, '0');




                    //         string[] part = newcode.Split('.');
                    //         //存入tb_BarCode表
                    //         md.ID = Guid.NewGuid().ToString();
                    //         md.StationID = stationID;
                    //         md.OneBarCode = NewOneCode;
                    //         md.BarCode = newcode;

                    //         md.StockNo = stockNo;
                    //         md.PrintType = 1;
                    //         md.BatchNo=textBox1.Text.Trim();

                    //         DataTable pdt1 = pbll.SearchIsImportByStockNo(NewOneCode.Substring(0, 10));
                    //         if (pdt1 != null && pdt1.Rows.Count > 0)
                    //         {
                    //             if (pdt1.Rows[0]["isImport"].ToString() == "1")
                    //             {
                    //                 md.Import = "国产料Kingfa";
                    //             }
                    //             else
                    //             {
                    //                 md.Import = "";
                    //             }
                    //         }

                    //         if ( bll.Add_Info(md))
                    //         {
                    //             #region 打印条码

                    //             filename = @"D:\111 - 副本.grf";
                    //             filename = _printTemplateName;
                    //             DataTable headTable = new DataTable();
                    //             headTable.Columns.Add("StationNo");
                    //             headTable.Columns.Add("OneBarCode");
                    //             headTable.Columns.Add("BarCode");
                    //             headTable.Columns.Add("ProductName");
                    //             headTable.Columns.Add("IsImport");
                    //             DataRow row = headTable.NewRow();
                    //             row["StationNo"] = Program.station;

                    //             row["OneBarCode"] = NewOneCode;
                    //             row["BarCode"] = newcode;
                    //             row["ProductName"] = comboBox2.Text + ";";
                    //             row["IsImport"] = md.Import;
                    //             if (comboBox2.Text.Contains("右"))
                    //             {
                    //                 reportName = ConfigurationManager.AppSettings["Printer2"];
                    //             }
                    //             else
                    //             {
                    //                 reportName = ConfigurationManager.AppSettings["Printer1"];
                    //             }

                    //             headTable.Rows.Add(row);

                    //             ReportHelper rp = new ReportHelper(filename, headTable, null,
                    //                 (int)GRPaperOrientation.grpoDefault, 1, reportName);
                    //             rp.Report.Print(false);

                    //             #endregion

                    //             LogHelper.WriteLog("打印时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ";" + NewOneCode);

                    //             bll.Update_Info(md);
                    //         }
                    //         else
                    //         {
                    //             MessageBox.Show("连接数据库超时!");
                    //             return;
                    //         }

                    //         #region 更新产量表、计划表

                    //         //ProductOfInjectionBLL InjectionBll = new ProductOfInjectionBLL();
                    //         //ProductOfInjectionModel pmd = new ProductOfInjectionModel();


                    //         //pmd.ID = Guid.NewGuid().ToString();
                    //         //pmd.StationID = md.StationID;
                    //         //pmd.ProductDate = Program.ProductDate;
                    //         //pmd.ClassName = Program.Shift;
                    //         //pmd.StockNo = md.StockNo;
                    //         //pmd.PartNo = part[0];
                    //         //pmd.ProductCount = 1;
                    //         //pmd.BadCount = 0;
                    //         //pmd.PlanID = planID;
                    //         //DataTable pdt1 = InjectionBll.SearchCountByInfo(md.StationID, Program.ProductDate, comboBox1.Text,
                    //         //    md.StockNo, part[0]);

                    //         //if (pdt1 != null && pdt1.Rows.Count > 0)
                    //         //{
                    //         //    //跟新
                    //         //    InjectionBll.Update_Info(pmd);
                    //         //}
                    //         //else
                    //         //{

                    //         //    //插入
                    //         //    InjectionBll.Add_Info(pmd);
                    //         //}
                    //         //pdt1.Dispose();

                    //         #region 注释掉

                    //         //InjectionPlanBLL ibll = new InjectionPlanBLL();
                    //         //InjectionPlanModel imd = new InjectionPlanModel();

                    //         //imd.CompleteCount = 1;
                    //         //imd.StationID = md.StationID;
                    //         //imd.StockNo = md.StockNo;
                    //         //ibll.UpdateCount(imd);

                    //         #endregion

                    //         #endregion
                    //     }
                    // }
                    // else
                    // {
                    //     for (int i = 1; i <= Convert.ToInt32(textBox2.Text.Trim()); i++)
                    //     {
                    //         newcode = partNo + "." + textBox1.Text.Trim() + "." + i.ToString().PadLeft(4, '0');
                    //         NewOneCode = stockNo + textBox1.Text.Trim() + i.ToString().PadLeft(4, '0');
                    //         string[] part = newcode.Split('.');
                    //         //存入tb_BarCode表
                    //         md.ID = Guid.NewGuid().ToString();
                    //         md.StationID = stationID;
                    //         md.StockNo = stockNo;
                    //         md.OneBarCode = NewOneCode;
                    //         md.BarCode = newcode;
                    //         md.PrintType = 1;
                    //         md.BatchNo = textBox1.Text;

                    //         DataTable pdt1 = pbll.SearchIsImportByStockNo(NewOneCode.Substring(0, 10)); //tb_Product表 是否进口(Import)
                    //         if (pdt1 != null && pdt1.Rows.Count > 0)
                    //         {
                    //             if (pdt1.Rows[0]["isImport"].ToString() == "1")
                    //             {
                    //                 md.Import = "国产料Kingfa";
                    //             }
                    //             else
                    //             {
                    //                 md.Import = "";
                    //             }
                    //         }

                    //         if ( bll.Add_Info(md))
                    //         {
                    //             #region 打印条码

                    //             filename = @"D:\111 - 副本.grf";
                    //             filename = _printTemplateName;
                    //             DataTable headTable = new DataTable();
                    //             headTable.Columns.Add("StationNo");
                    //             headTable.Columns.Add("OneBarCode");
                    //             headTable.Columns.Add("BarCode");
                    //             headTable.Columns.Add("ProductName");
                    //             headTable.Columns.Add("IsImport");
                    //             DataRow row = headTable.NewRow();
                    //             row["StationNo"] = Program.station;

                    //             row["OneBarCode"] = NewOneCode;
                    //             row["BarCode"] = newcode;
                    //             row["ProductName"] = comboBox2.Text + ";";
                    //             row["IsImport"] = md.Import;
                    //             if (comboBox2.Text.Contains("右"))
                    //             {
                    //                 reportName = ConfigurationManager.AppSettings["Printer2"];
                    //             }
                    //             else
                    //             {
                    //                 reportName = ConfigurationManager.AppSettings["Printer1"];
                    //             }

                    //             headTable.Rows.Add(row);

                    //             ReportHelper rp = new ReportHelper(filename, headTable, null,
                    //                 (int)GRPaperOrientation.grpoDefault, 1, reportName);
                    //             rp.Report.Print(false);

                    //             #endregion

                    //             LogHelper.WriteLog("打印时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ";" + NewOneCode);

                    //             bll.Update_Info(md);
                    //         }
                    //         else
                    //         {
                    //             MessageBox.Show("连接数据库超时!");
                    //             return;
                    //         }

                    //         #region 更新产量表、计划表

                    //         //ProductOfInjectionBLL InjectionBll = new ProductOfInjectionBLL();
                    //         //ProductOfInjectionModel pmd = new ProductOfInjectionModel();

                    //         //pmd.ID = Guid.NewGuid().ToString();
                    //         //pmd.StationID = md.StationID;
                    //         //pmd.ProductDate = Program.ProductDate;
                    //         //pmd.ClassName = Program.Shift;
                    //         //pmd.StockNo = md.StockNo;
                    //         //pmd.PartNo = part[0];
                    //         //pmd.ProductCount = 1;
                    //         //pmd.BadCount = 0;
                    //         //pmd.PlanID = planID;
                    //         //DataTable pdt1 = InjectionBll.SearchCountByInfo(md.StationID, Program.ProductDate, comboBox1.Text,
                    //         //    md.StockNo, part[0]);

                    //         //if (pdt1 != null && pdt1.Rows.Count > 0)
                    //         //{
                    //         //    //跟新
                    //         //    InjectionBll.Update_Info(pmd);
                    //         //}
                    //         //else
                    //         //{

                    //         //    //插入
                    //         //    InjectionBll.Add_Info(pmd);
                    //         //}
                    //         //pdt1.Dispose();

                    //         #region 注释掉

                    //         //InjectionPlanBLL ibll = new InjectionPlanBLL();
                    //         //InjectionPlanModel imd = new InjectionPlanModel();

                    //         //imd.CompleteCount = 1;
                    //         //imd.StationID = md.StationID;
                    //         //imd.StockNo = md.StockNo;
                    //         //ibll.UpdateCount(imd);

                    //         #endregion

                    //         #endregion

                    //     }
                    // }

                    #endregion

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

        /// <summary>
        /// 补打条码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void label18_Click(object sender, EventArgs e)
        {
            textBox3.Text = "";
            panel3.Visible = true;
        }


        //public DateTime GetDateTime()
        //{
        //    string sql = "";
        //    DateTime time;
        //    DataTable dt;
        //    try
        //    {
        //        sql = @"select  convert(char(23),getdate(),121) as time";

        //        dt = SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0];
        //        if (dt != null && dt.Rows.Count > 0)
        //        {
        //            //time = Convert.ToDateTime(dt.Rows[0]["time"].ToString());
        //            time = DateTime.ParseExact(dt.Rows[0]["time"].ToString(), "yyyy-MM-dd HH:mm:ss.fff",
        //                System.Globalization.CultureInfo.CurrentCulture);

        //        }
        //        else
        //        {
        //            time = DateTime.Now;
        //        }
        //        return time;
        //    }
        //    catch (Exception ex)
        //    {
        //        LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
        //        return DateTime.Now;
        //    }
        //}

   

        /// <summary>
        /// 切换一下计划
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void label13_Click(object sender, EventArgs e)
        {
            string stationID = "",nextPlan="";
            InjectionPlanBLL bll = new InjectionPlanBLL();
            InjectionPlanModel md = new InjectionPlanModel();

            try
            {
                #region 获取下一计划

                DataTable dt = bll.NextSearchInfoByName(Program.station);
                if (dt != null && dt.Rows.Count > 1)
                {
                    nextPlan = dt.Rows[1]["ProductName"].ToString();
                }
                else
                {
                    nextPlan = "暂无下一计划,请联系计划员";
                }

                #endregion
                DialogResult result = MessageBox.Show("确定切换下一计划:"+nextPlan, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (result == DialogResult.OK)
                {
                    #region 获取工位编号

                    //StationBLL sbll = new StationBLL();
                    //DataTable sdt = sbll.SearchInfoByNo(Program.station);
                    //if (sdt != null && sdt.Rows.Count > 0)
                    //{
                    //    stationID = sdt.Rows[0]["StationID"].ToString();
                    //}
                    //sdt.Dispose();

                    List<DataRow> stationList = _stationTable.AsEnumerable().Where(p => p.Field<string>("StationNo") == Program.station).ToList();
                    if (stationList != null && stationList.Count > 0)
                    {
                        stationID = stationList[0]["StationID"].ToString();
                    }
                    #endregion

                    #region 更新计划完成

                    md.StationID = stationID;
                    bll.UpdateFinish(md);

                    #endregion
                    MessageBox.Show("操作成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("取消操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
            }
            
            
        }

        /// <summary>
        /// 手动删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void label20_Click(object sender, EventArgs e)
        {
            BarCodeBLL bll=new BarCodeBLL();
            BarCodeModel md=new BarCodeModel();

            try
            {
                if (glacialList1.SelectedItems.Count == 0)
                {
                    MessageBox.Show("请选择要补打的条码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    GlacialComponents.Controls.GLItem gv;
                    gv = glacialList1.SelectedItems[0] as GlacialComponents.Controls.GLItem;
                    md.BarCode = gv.Text;

                    bll.DelBarCode(md);
                    initBarCode();
                }
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
            }
            
        }

        private void label21_Click(object sender, EventArgs e)
        {
            Form fr=new FrmDown();
            fr.Show();
            //this.Hide();
        }

        private void label2_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private void textBox2_Click(object sender, EventArgs e)
        {
            //Process.Start(@"C:\windows\system32\osk.exe");
            ShowInputPanel();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox3.Text.Trim() != ConfigurationManager.AppSettings["Psw"])
            {
                MessageBox.Show("密码不正确!", "提示", MessageBoxButtons.OK);
                textBox3.SelectAll();
                return;
            }
            else
            {
                panel3.Visible = false;
                textBox3.Text = "";
                string code = "", productName = "", reportName = "";
                BarCodeModel md = new BarCodeModel();
                BarCodeBLL bll = new BarCodeBLL();
                ProductBLL pbll = new ProductBLL();
                try
                {
                    if (code_g == "")
                    {
                        MessageBox.Show("请选择要补打的条码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    else
                    {
                        //GlacialComponents.Controls.GLItem gv;
                        //gv = glacialList1.SelectedItems[0] as GlacialComponents.Controls.GLItem;
                        //code = gv.Text;

                        DataTable dt = bll.SearchInfoByBarCode(code_g);
                        if (dt != null && dt.Rows.Count > 0)
                        {
                            md.ID = Guid.NewGuid().ToString();
                            md.BarCode = code_g;
                            md.StationID = dt.Rows[0]["StationID"].ToString();
                            md.StockNo = dt.Rows[0]["StockNo"].ToString();
                            md.OneBarCode = dt.Rows[0]["OneBarCode"].ToString();
                            md.PrintType = 2;
                            //md.CreateTime = GetDateTime();

                            DataTable pdt = pbll.SearchIsImportByStockNo(md.StockNo);
                            if (pdt != null && pdt.Rows.Count > 0)
                            {
                                productName = pdt.Rows[0]["ProductName"].ToString();
                                if (pdt.Rows[0]["isImport"].ToString() == "1")
                                {
                                    md.Import = "国产料Kingfa";
                                }
                                else
                                {
                                    md.Import = "";
                                }
                            }

                        }
                        dt.Dispose();
                        if (bll.Add_Info(md))
                        {
                            #region 打印条码

                            //string filename = @"D:\111 - 副本.grf";
                            string filename = _printTemplateName;
                            DataTable headTable = new DataTable();
                            headTable.Columns.Add("StationNo");
                            headTable.Columns.Add("OneBarCode");
                            headTable.Columns.Add("BarCode");
                            headTable.Columns.Add("ProductName");
                            headTable.Columns.Add("IsImport");
                            DataRow row = headTable.NewRow();
                            row["StationNo"] = Program.station;
                            row["OneBarCode"] = md.OneBarCode;
                            row["BarCode"] = code_g;
                            row["ProductName"] = productName + ";";
                            row["IsImport"] = md.Import;
                            if (productName.Contains("右"))
                            {
                                reportName = ConfigurationManager.AppSettings["Printer2"];
                            }
                            else
                            {
                                reportName = ConfigurationManager.AppSettings["Printer1"];
                            }

                            headTable.Rows.Add(row);

                            ReportHelper rp = new ReportHelper(filename, headTable, null,
                                (int)GRPaperOrientation.grpoDefault, 1, reportName);
                            rp.Report.Print(false);

                            #endregion

                            bll.Update_Info(md);
                        }
                        else
                        {
                            MessageBox.Show("连接数据库超时!");
                            return;
                        }

                        glacialList1.SelectedItems.Clear();
                        initBarCode();
                    }
                }
                catch (Exception ex)
                {
                    LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox3.Text = "";
            panel3.Visible = false;
        }

        private void textBox3_Click(object sender, EventArgs e)
        {
            ShowInputPanel();
        }

        private void FrmBarCode_FormClosed(object sender, FormClosedEventArgs e)
        {
            Environment.Exit(0);
        }

        private void glacialList1_Click(object sender, EventArgs e)
        {
            code_g = "";
            if (glacialList1.SelectedItems.Count == 0)
            {
                code_g = "";
            }
            else
            {
                GlacialComponents.Controls.GLItem gv;
                gv = glacialList1.SelectedItems[0] as GlacialComponents.Controls.GLItem;
                code_g = gv.Text;
            }
        }


        private const Int32 WM_SYSCOMMAND = 274;

        private const UInt32 SC_CLOSE = 61536;

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]

        private static extern bool PostMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]

        private static extern bool PostMessage(IntPtr hWnd, int Msg, uint wParam, uint lParam);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]

        private static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]

        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]

        private static extern int RegisterWindowMessage(string lpString);



        //显示屏幕键盘

        public static int ShowInputPanel()
        {

            try
            {

                dynamic file = "C:\\Program Files\\Common Files\\microsoft shared\\ink\\TabTip.exe";

                if (!System.IO.File.Exists(file))

                    return -1;

                Process.Start(file);

                //return SetUnDock(); //不知SetUnDock()是什么,所以直接注释返回1

                return 1;

            }

            catch (Exception)
            {

                return 255;

            }

        }

        
        private static bool IsNumeric(string str)
        {
            System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^[0-9]\d*$");
            return reg1.IsMatch(str);
        }
        /// <summary>
        /// 设置班组和批次
        /// </summary>
        void SetBatchText()
        {
            // DateTime aa = GetDateTime();
            DateTime aa = BasicDAL.GetServerTime();

            if (aa.Hour >= 8 && aa.Hour < 20)
            {
                Program.Shift = "A班";
                Program.ProductDate = aa.ToString("yyyy-MM-dd");
            }
            else
            {
                Program.Shift = "B班";
                if (aa.Hour >= 0 && aa.Hour < 8)
                {
                    Program.ProductDate = aa.AddDays(-1).ToString("yyyy-MM-dd");
                    //Program.ProductDate = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
                }
                else
                {
                    Program.ProductDate = aa.ToString("yyyy-MM-dd");
                    //Program.ProductDate = DateTime.Now.ToString("yyyy-MM-dd");
                }
            }
            comboBox1.Text = Program.Shift;
            textBox1.Text = Program.ProductDate.Substring(2, 2) + Program.ProductDate.Substring(5, 2) +
                            Program.ProductDate.Substring(8, 2);
        }
    

    }
}