using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace InjectionPC
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        /// 

        public static SqlConnection DBConn;   //数据库连接串
        public static string station;
        public static string OperatorName;
        public static string IP;
        public static string Shift;   //班次
        public static string ProductDate;
        public static string RemoteIP;
        public static string PicturePath;
        public static string WeightFolder;
        public static string WeightFile;
        public static string WeightUser;
        public static string WeightPsw;

        //true 注塑机离线. 用于测试
        public static bool WeightOutLine=false;

        public static int interVal;

        [STAThread]
        static void Main()
        {
            bool isRuned;
            System.Threading.Mutex mutex = new System.Threading.Mutex(true, "OnlyRunOneInstance", out isRuned);

            //处理UI线程异常

            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

            //处理非UI线程异常

            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);


            if (isRuned)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new FrmLogin());
                mutex.ReleaseMutex();
            }
            else
            {
                MessageBox.Show("程序已启动!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            //Application.Run(new Form1());
        }

        /// <summary>判断指定字符串是否包含有汉字</summary>
        /// <param name="StrChineseString">指定的字符串</param>
        /// <returns>若包含有汉字则返回True,否则返回False</returns>
        public static bool InChinese1(string StrChineseString)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(StrChineseString, @"[/u4e00-/u9fa5]+");
        }

        public static void GetCode(string code, out string stockNo, out string batchNo, out string partNo)
        {
            //解析塑料粒子条码,长度为20的为一维码22000000821906090201,否则为二维码
            //二维码样例Z-340.180411.000001;5000;S35001;20180411;P1710401.[#Line#];180411;
            //第一个分号之前的数据,即Z-340.180411.000001; Z-340为零件号,180411为批次号,000001为流水号
            //一维码前十位为零件号,tb_Product PartNo,11~16位为批次 

            stockNo = "";     //存货代码
            batchNo = "";    //批次
            partNo = "";    //零件号
            if (code.Contains(".") == false)
            {
                //一维码
                stockNo = code.Substring(0, 10);
                batchNo = code.Substring(10, 6);
            }
            else
            {
                //二维码
                string[] strs = code.Split(';');
                if (strs.Length > 0)
                {
                    string str = strs[0];
                    string[] props = str.Split('.');
                    if (props.Length >= 3)
                    {
                        partNo = props[0];
                        batchNo = props[1];
                    }
                }
            }
        }
        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            string str = GetExceptionMsg(e.Exception, e.ToString());
            MessageBox.Show(str);
            //LogManager.WriteLog(str);
        }



        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
            MessageBox.Show(str);
            //LogManager.WriteLog(str);
        }
        static string GetExceptionMsg(Exception ex, string backStr)

        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("****************************异常文本****************************");

            sb.AppendLine("【出现时间】:" + DateTime.Now.ToString());

            if (ex != null)
            {
                sb.AppendLine("【异常类型】:" + ex.GetType().Name);

                sb.AppendLine("【异常信息】:" + ex.Message);

                sb.AppendLine("【堆栈调用】:" + ex.StackTrace);

            }
            else
            {

                sb.AppendLine("【未处理异常】:" + backStr);

            }

            sb.AppendLine("***************************************************************");

            return sb.ToString();
        }
    }
}