c#测试产能统筹

c#测试产能统筹using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;//Process类名称空间


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //连接共享目录server1(192.167.10.1)
        public static bool connectShareDoc(string path,string userName,String passWord)
        {
            bool Flag = false;
            Process proc = new Process();
            try
            {
                proc.StartInfo.FileName = "cmd.exe";
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardInput = true;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.CreateNoWindow = true;
                proc.Start();
                string dosLine = @"net use " + path + " /User:" + userName + " " + passWord;
                proc.StandardInput.WriteLine(dosLine);
                proc.StandardInput.WriteLine("exit");
                while (!proc.HasExited)
                {
                    proc.WaitForExit(1000);
                }
                string errormsg = proc.StandardError.ReadToEnd();
                proc.StandardError.Close();
                if (string.IsNullOrEmpty(errormsg))
                {
                    Flag = true;
                }
                else
                {
                    MessageBox.Show(errormsg);
                    Flag = false;
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                Flag = false;
            }
            finally
            {
                proc.Close();
                proc.Dispose();
            }
            return Flag;  
        }
        //断开网络
        public bool DisposeConnect()
        {
            bool Flag = false;
            Process proc = new Process();
            try
            {
                proc.StartInfo.FileName = "cmd.exe";
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardInput = true;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.CreateNoWindow = true;
                proc.Start();
                string dosLine = @"net use * /d /y";
                proc.StandardInput.WriteLine(dosLine);
                proc.StandardInput.WriteLine("exit");
                while(!proc.HasExited)
                {
                    proc.WaitForExit(1000);
                }
                string errormsg = proc.StandardError.ReadToEnd();
                proc.StandardError.Close();
                if(string.IsNullOrEmpty(errormsg))
                {
                    Flag = true;
                }
                else
                {
                    Flag = false;
                    MessageBox.Show(errormsg);
                }
            }
            catch(Exception ex)
            {
                Flag = false;
                MessageBox.Show(ex.Message);
            }
            finally
            {
                proc.Close();
                proc.Dispose();
            }
            return Flag;
        }


        //判断文件是否存在
        public bool FindCfg(string CfgFileName)
        {
            bool IsCfg = false;
            FileInfo finfo = new FileInfo(CfgFileName);
            IsCfg = finfo.Exists;
            if (IsCfg)
                return true;
            return false;
        }


        //初始化ListView1 And ListView2
        public void InitListView(ListView Table)
        {
            Table.Columns.Add("NO.",60,HorizontalAlignment.Left);
            Table.Columns.Add("姓名",95,HorizontalAlignment.Left);
            Table.Columns.Add("工号",95,HorizontalAlignment.Left);
            Table.Columns.Add("7:00~8:00",90,HorizontalAlignment.Left);
            Table.Columns.Add("8:00~9:00",90,HorizontalAlignment.Left);
            Table.Columns.Add("9:00~10:00",90,HorizontalAlignment.Left);
            Table.Columns.Add("10:00~11:00", 90, HorizontalAlignment.Left);
            Table.Columns.Add("11:00~12:00", 90, HorizontalAlignment.Left);
            Table.Columns.Add("12:00~13:00", 90, HorizontalAlignment.Left);
            Table.Columns.Add("13:00~14:00", 90, HorizontalAlignment.Left);
            Table.Columns.Add("14:00~15:00", 90, HorizontalAlignment.Left);
            Table.Columns.Add("15:00~16:00", 90, HorizontalAlignment.Left);
            Table.Columns.Add("16:00~17:00", 90, HorizontalAlignment.Left);
            Table.Columns.Add("17:00~18:00", 90, HorizontalAlignment.Left);
            Table.Columns.Add("18:00~19:00", 90, HorizontalAlignment.Left);
            Table.Columns.Add("19:00~20:00", 90, HorizontalAlignment.Left);
            Table.Columns.Add("20:00~21:00", 90, HorizontalAlignment.Left);
            Table.Columns.Add("21:00~22:00", 90, HorizontalAlignment.Left);
            Table.Columns.Add("22:00~23:00", 90, HorizontalAlignment.Left);
            Table.Columns.Add("23:00~24:00", 90, HorizontalAlignment.Left);
            Table.Columns.Add("...              ...", 100, HorizontalAlignment.Left);
        }


        //默认设置控件
        public void DefaultValues(ComboBox Key1,ComboBox Key2)
        {
            Key1.Items.Add("白班");
            Key1.Items.Add("夜班");
            Key1.SelectedIndex = 0;
            Key1.Enabled = false;
            Key2.Items.Add("白班");
            Key2.Items.Add("夜班");
            Key2.SelectedIndex = 0;
            Key2.Enabled = false;
        }


        //读取线体信息(lineinfo)
        public void Rdlineinfoset(string pathFile,ComboBox lineStr1,ComboBox lineStr2)
        {
            try
            {
                FileStream fs = new FileStream(pathFile,FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                string line;
                while((line=sr.ReadLine())!=null)
                {
                    lineStr1.Items.Add(line.Trim());
                    lineStr2.Items.Add(line.Trim());
                }
                lineStr1.SelectedIndex = 4;
                lineStr2.SelectedIndex = 4;
                sr.Close();
                fs.Close();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        //读取上传目录文本文件名称
        public void GetTextFileName(string SourceTextPath,ComboBox Key1,ComboBox Key2)
        {
           try
           {
               DirectoryInfo dinfo = new DirectoryInfo(SourceTextPath); //实例化DirectoryInfo对象
               //获取指定目录下的所有子目录及文件类型
               FileSystemInfo[] fsinfos = dinfo.GetFileSystemInfos();
               foreach (FileSystemInfo fsinfo in fsinfos)
               {
                   if (fsinfo is FileInfo)
                   {
                       if (Path.GetExtension(fsinfo.Name) == @".txt")
                       {
                           Key1.Items.Add(fsinfo.Name);
                           Key2.Items.Add(fsinfo.Name);
                       }
                   }
               }
               Key1.SelectedIndex = 0;
               Key2.SelectedIndex = 0;
           }
           catch(Exception ex)
           {
               MessageBox.Show(ex.Message);
           }


        }


        //读取Text配置路径
        public string stringPath(string cfgName)
        {
            string PathStr = string.Empty;
            try
            {
                FileStream fs = new FileStream(cfgName,FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                PathStr = sr.ReadLine().Trim();
                sr.Close();
                fs.Close();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return PathStr;
        }


        bool status = false; //网络连接状况
        bool lineinfost = false;//线体lineinfo.cfg信息读取
        //bool employeeifst = false;//员工信息读取
        bool ReadPath = false;//测试数据源文本名称获取
        private void Form1_Load(object sender, EventArgs e)
        {
            DefaultValues(comboBox5,comboBox6);//设置班别
            status=connectShareDoc(@"\\server1\data\TestUpload2\Data\Passed", "admin", "test");
            if (status)
            {
                radioButton1.Text = "服务器连接状态:良好";
                radioButton1.BackColor = Color.Green;
                lineinfost = FindCfg(@"\\Server1\data\TestCapacityCfg\lineinfo.cfg");
                if(lineinfost)
                {
                    radioButton2.Text = "lineinfo.cfg读取中...";
                    radioButton2.BackColor = Color.Green;
                    radioButton3.Text = "待处理"+comboBox1.Text.Trim()+".cfg配置";
                    radioButton3.BackColor = Color.Yellow;
                    InitListView(listView1);//设置表格A
                    InitListView(listView2); //设置表格B
                    Rdlineinfoset(@"\\Server1\data\TestCapacityCfg\lineinfo.cfg", comboBox1, comboBox3);//读取lineinfo线体信息并设置
                    string SrPathStr = stringPath(@"\\Server1\data\TestCapacityCfg\Path.cfg");//读取设置的路径配置文本信息
                    if (SrPathStr != string.Empty) ReadPath = true;//如果路径文本读取OK则设置全局变量ReadPath=true;
                    GetTextFileName(SrPathStr,comboBox2,comboBox4);
                }
                else
                {
                    radioButton2.Text = "未找到lineinfo.cfg配置文件";
                    radioButton2.BackColor = Color.Red;
                }
            }
            else
            {
                DisposeConnect();
                radioButton1.Text = "服务器断开状态";
                radioButton1.BackColor = Color.Red;
            }
        }


        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {


        }


        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            radioButton3.Text = "待处理"+comboBox1.Text.Trim()+".cfg 配置";
        }


        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            radioButton3.Text = "待处理"+comboBox3.Text.Trim()+".cfg 配置";
        }


        private void listView2_SelectedIndexChanged(object sender, EventArgs e)
        {
            listView2.FullRowSelect = true;
            if (this.listView2.SelectedItems.Count > 0)
            {
                listView2.SelectedItems[0].SubItems[0].ForeColor = Color.Blue;
                //先清除原有格式
                foreach (ListViewItem item in listView2.Items)
                {
                    item.ForeColor = Color.Black;
                }
                foreach (ListViewItem item in listView2.Items)
                {
                    item.BackColor = Color.White;
                    Font f = new Font(Control.DefaultFont, FontStyle.Regular);
                    item.Font = f;
                }
                //加粗字体
                Font f2 = new Font(Control.DefaultFont, FontStyle.Bold);
                listView2.SelectedItems[0].SubItems[0].Font = f2;
                //设置选中行背景颜色
                listView2.SelectedItems[0].BackColor = Color.LightBlue;
                listView2.SelectedItems[0].Selected = false;
            }
        }


        //读取员工信息
        public void ReadEmployeeInfo(string EmployeeifFile,List <string> Eyykey)
        {
            try
            {
                radioButton3.Text = comboBox3.Text.Trim() + ".cfg 配置调用中...";
                radioButton3.BackColor = Color.Green;
                FileStream fs = new FileStream(EmployeeifFile, FileMode.Open);
                StreamReader sr = new StreamReader(fs,Encoding.Default);//改变读取文本中文乱码现象
                //StreamReader sr=new StreamReader(fs,Encodeing.UTF8);
                string Temp;
                while ((Temp = sr.ReadLine()) != null)
                {
                    //Array = Temp.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    Eyykey.Add(Temp);
                }
                sr.Close();
                fs.Close();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }


        //设置员工信息到ListView
        public void FillEmloyeeInfo(ListView Key,List <string> Key1)
        {
            int n = 1;
            //int x=0;
            ListViewItem item = new ListViewItem();
            foreach(string str in Key1)
            {
                string[] Array = str.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                //item = Key.Items.Insert(x, n.ToString());
                item = Key.Items.Add(n.ToString());
                item.SubItems.Add(Array[0].Trim());
                item.SubItems.Add(Array[1].Trim());
                for (int s = 0; s < 18; s++)
                    item.SubItems.Add("0");
                n++;
            }
            item = Key.Items.Add("");
            item = Key.Items.Add("");
            item = Key.Items.Add("");
            for (int y = 0; y < 20;y++)
            { 
                if (y > 1)
                    item.SubItems.Add("0");
                else
                    item.SubItems.Add("");
            }
        }


        //读取OK测试数据
        public void ReadOkTestData(string PathFile,List <string> Key)
        {
            try
            {
                FileStream fs = new FileStream(PathFile,FileMode.Open);
                StreamReader sr = new StreamReader(fs, Encoding.Default);
                string Temp;
                while((Temp=sr.ReadLine())!=null)
                {
                    string []Array = Temp.Split(new string[] { ","," "}, StringSplitOptions.RemoveEmptyEntries);
                    if(Array[7]==ReadSystemNowDate)
                    {
                        Key.Add(Temp);
                    }
                }
                sr.Close();
                fs.Close();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        List <string> Ey1=new List<string>();//定义存储A区读取员工信息
        List <string> Ey2=new List<string>();//定义存储B区读取员工信息
        List<string> TestDataSource1 = new List<string>();//读取A区测试数据
        List<string> TestDataSource2 = new List<string>();//读取B区测试数据
        int[][] TestNumListA=new int[20][];//测试产能数据A
        int[][] TestNumListB=new int[20][];//测试产能数据B
        //int EmoyeeNumberA=0;
        //int EmoyeeNumberB=0;
        string ReadSystemNowDate = DateTime.Now.ToString(@"yyyy/MM/dd");
        string AFilePath;//存储A区对应机型文件路径
        string BFilePath;//存储B区对应机型文件路径
        private void button2_Click(object sender, EventArgs e)
        {
            try 
            {


                if (status == true && lineinfost == true && ReadPath == true)
                {
                    string SourcePath = @"\\Server1\data\TestCapacityCfg\"+comboBox1.Text.Trim() + ".cfg";//读取配置文本存储位置
                    if(File.Exists(SourcePath)==true)
                    {
                        comboBox1.Enabled = false;
                        comboBox2.Enabled = false;
                        button2.Enabled = false;
                        button1.Enabled = true;
                        ReadEmployeeInfo(SourcePath, Ey1);//读取员工信息并储存于Employee1字符串数组中
                        FillEmloyeeInfo(listView1, Ey1);//设置员工信息
                        AFilePath = @"\\server1\data\TestUpload2\Data\Passed\" + comboBox2.Text.Trim();
                        timer1.Enabled = true;
                        radioButton2.Text = "";
                        radioButton3.Text = "";
                        radioButton2.BackColor = Color.White;
                        radioButton3.BackColor = Color.White;
                    }
                    else
                    {
                        MessageBox.Show("A区员工信息配置文件:"+SourcePath+"未找到!!");
                    }
                }
            
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }


        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                if (status == true && lineinfost == true && ReadPath == true)
                {
                    string SourcePath1 [email protected]"\\Server1\data\TestCapacityCfg\"+comboBox3.Text.Trim() + ".cfg";//读取配置文本存储位置
                    if (File.Exists(SourcePath1) == true)
                    {
                        comboBox3.Enabled = false;
                        comboBox4.Enabled = false;
                        button4.Enabled = false;
                        button3.Enabled = true;
                        ReadEmployeeInfo(SourcePath1, Ey2);//读取员工信息并储存于Employee2字符串数组中
                        FillEmloyeeInfo(listView2, Ey2);//设置员工信息
                        BFilePath = @"\\server1\data\TestUpload2\Data\Passed\" + comboBox4.Text.Trim();
                        timer2.Enabled = true;
                        radioButton2.Text = "";
                        radioButton3.Text = "";
                        radioButton2.BackColor = Color.White;
                        radioButton3.BackColor = Color.White;
                    }
                    else
                    {
                        MessageBox.Show("B区员工信息配置文件:" + SourcePath1 + "未发现!!");
                    }


                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            listView1.Clear();//清空A区数据表,数据
            InitListView(listView1);//初始化表头
            Ey1.Clear();//清空A区工号信息
            timer1.Enabled = false;
            label11.Text = "";
            label11.BackColor = Color.White;
            ti1 = Startsecond;
            button1.Enabled = false;
            comboBox1.Enabled = true;
            comboBox2.Enabled = true;
            button2.Enabled = true;
        }


        private void button3_Click(object sender, EventArgs e)
        {
            listView2.Clear();//清空B区数据表,数据
            InitListView(listView2);//初始化表头
            Ey2.Clear();//清空B区工号信息
            timer2.Enabled = false;
            label12.Text = "";
            label12.BackColor = Color.White;
            ti2 = Startsecond;
            button3.Enabled = false;
            comboBox3.Enabled = true;
            comboBox4.Enabled = true;
            button4.Enabled = true;


        }


        static int Startsecond = 180;
        int ti1 = Startsecond;//时针1延时180S 3分钟
        int ti2 = Startsecond;//时针2延时180S 3分钟
        private void timer1_Tick(object sender, EventArgs e) //A区延时,时针
        {
            label11.Text = "测试产能统筹.A 剩余" + ti1.ToString() + "秒刷新产能";
            label11.BackColor = Color.Gold;
            if(ti1==0)
            {
                timer1.Enabled = false;
                timer3.Enabled = true;
                ti1 = Startsecond;
                label11.BackColor = Color.Green;
                label11.Text = "测试产能统筹.A 测试产能刷新中...";
            }
            else
                ti1--;
        }


        private void timer2_Tick(object sender, EventArgs e)//B区延时,时针
        {
            label12.Text = "测试产能统筹.B 剩余" + ti2.ToString() + "秒刷新产能";
            label12.BackColor = Color.Gold;
            if (ti2 == 0)
            {
                timer2.Enabled = false;
                timer4.Enabled = true;
                ti2 = Startsecond;
                label12.BackColor = Color.Green;
                label12.Text = "测试产能统筹.B 测试产能刷新中...";
            }
            else
                ti2--;
        }


        public bool MatchingTestData(int ArrayIndexX,string HourStr,int [][] EmployTestCapacityKey)
        {
            string[] HourTemp ={ "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24" };
            int y=0;
            foreach(string HrTstr in HourTemp)
            {
                if(HourStr==HrTstr)
                {
                    EmployTestCapacityKey[ArrayIndexX][y] = (EmployTestCapacityKey[ArrayIndexX][y]) + 1;
                    return true;
                }
                else
                    y++;
            }
            return true;
        }
        public void ReadEmployeeTestcapacity(List<string> EmployeeInFoKey,List<string> EmployeeTestDataKey,int [][] EmployTestCapacityKey)
        {


            foreach(string TsDt in EmployeeTestDataKey)
            {
                int x = 0;
                string[] ArrayTD = TsDt.Split(new string[] {","," "},StringSplitOptions.RemoveEmptyEntries);//截取测试数据字符串以“,”," "分隔
                foreach(string EyIf in EmployeeInFoKey)
                {
                    string[] ArrayEI = EyIf.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);//截取员工信息字符串以“,”分隔
                    if (ArrayTD[5].Trim() == ArrayEI[1].Trim())
                    {
                        string[] TesTDataHour = ArrayTD[8].Split(new string[] {":"},StringSplitOptions.RemoveEmptyEntries);//截取测试数据中时间字符串并以":"分隔
                        MatchingTestData(x, TesTDataHour[0], EmployTestCapacityKey);
                    }
                    else
                        x++;
                }
            }
        }


        public void InitialNumber(List<string>EmployeeTestDataKey,int [][]Key)
        {
            //int x = 0;
            int y = 0;
            foreach(string str in EmployeeTestDataKey)
            {
                Key[y] = new int[19] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                 y++;
            }
            Key[y] = new int[19] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        }


        public void TotalCapacityList(int [][]Key,List<string>Key2,int Rowlength)
        {
            int x = 0;
            foreach(string str in Key2)
            {
                int total = 0;
                for(int y=0;y<Rowlength;y++)
                {
                    if (y == Rowlength - 1) Key[x][y] = total;
                    else total = total + Key[x][y];
                }
                x++;
            }


            for(int i=0;i<Rowlength;i++)
            {
                int n=0;
                int total1 = 0;
                foreach(string str in Key2)
                {
                    if(n<x)
                        total1 = total1 + Key[n][i];
                    n++;
                }
                Key[n][i] = total1;
            }
        }


        public void FullListViewTestCapacity(ListView Key,int [][]Key2,List<string> Key3)
        {
            TotalCapacityList(Key2, Key3, 18);
            int x = 0;
            foreach(string str in Key3)
            {
                for (int y=0,n = 3; y < 18;y++,n++ )
                {
                    Key.Items[x].SubItems[n].Text = Key2[x][y].ToString().Trim();
                }
                x++;
            }
             int l= x + 2;
             for (int y = 0, n = 3; y < 18;y++,n++)
             {
                 Key.Items[l].SubItems[n].Text = Key2[x][y].ToString().Trim();
             }
        }


        private void timer3_Tick(object sender, EventArgs e)
        {
            TestDataSource1.Clear();//清空A区读取的OK数据
            ReadOkTestData(AFilePath, TestDataSource1);//A区分析测试数据
            InitialNumber(Ey1,TestNumListA);//初始化存储产能数组
            ReadEmployeeTestcapacity(Ey1, TestDataSource1, TestNumListA);//计算产能数据
            FullListViewTestCapacity(listView1,TestNumListA,Ey1);//向A区数据表填充测试产能
            timer3.Enabled = false;
            timer1.Enabled = true;
        }


        private void timer4_Tick(object sender, EventArgs e)
        {
            TestDataSource2.Clear();//清空B区读取的OK数据
            ReadOkTestData(BFilePath, TestDataSource2);//B区分析测试数据
            InitialNumber(Ey2, TestNumListB);//初始化B区存储产能数组
            ReadEmployeeTestcapacity(Ey2,TestDataSource2,TestNumListB);//计算产能数据
            FullListViewTestCapacity(listView2,TestNumListB,Ey2);//向B区数据表填充测试产能
            timer4.Enabled = false;
            timer2.Enabled = true;
        }


        private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            listView1.FullRowSelect = true;
            if (this.listView1.SelectedItems.Count > 0)
            {
                listView1.SelectedItems[0].SubItems[0].ForeColor = Color.Blue;
                //先清除原有格式
                foreach (ListViewItem item in listView1.Items)
                {
                    item.ForeColor = Color.Black;
                }
                foreach (ListViewItem item in listView1.Items)
                {
                    item.BackColor = Color.White;
                    Font f = new Font(Control.DefaultFont, FontStyle.Regular);
                    item.Font = f;
                }
                //加粗字体
                Font f2 = new Font(Control.DefaultFont, FontStyle.Bold);
                listView1.SelectedItems[0].SubItems[0].Font = f2;
                //设置选中行背景颜色
                listView1.SelectedItems[0].BackColor = Color.LightBlue;
                listView1.SelectedItems[0].Selected = false;
            }           
        }
    }
}