日志解析+调用Web Service服务保存至数据库,再调用服务查询数据

拜拜、爱过 提交于 2020-03-17 01:16:04

某厂面试归来,发现自己落伍了!>>>

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

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ServiceReference1.WebService1SoapClient ws = new ServiceReference1.WebService1SoapClient();
            string sWhere = "";
            if (textBox1.Text != "")
            {
                sWhere +=" AND [USER] = '" +textBox1.Text+"'";
            }
            else
            {
                MessageBox.Show("此字段为必填项!");
                textBox1.Focus();
                return;
            }
            if (dateTimePicker1.Text != "")
            {
                sWhere += " AND  CONVERT(varchar(100), DATE_TIME,105)> '" + dateTimePicker1.Value.ToString("yyyy-MM-dd hh:mm:ss") + "'";
            }

            if (dateTimePicker1.Text == "")
            {
                sWhere += " AND CONVERT(varchar(100), DATE_TIME,105)<'" + dateTimePicker2.Value.ToString("yyyy-MM-dd hh:mm:ss") + "'";
            }
            sWhere += " order by DATE_TIME";
            

            ServiceReference1.LOCATION[] arrLocation = ws.GetLOCATIONList(sWhere);
            dataGridView1.DataSource = arrLocation;
          
        }


        public static void SaveLogText()
        {
            List<string[]> listUser = new List<string[]>();
            List<string[]> listLocation = new List<string[]>();
            //List<FileInfo> listFile = GetFilesName();
            //foreach (FileInfo fileInfo in listFile)
            //{
            //    if (fileInfo.FullName.ToLower().Contains("log*.txt"))
            //    {
            string text = System.IO.File.ReadAllText(@"D:\TEST\LogUser.txt", Encoding.UTF8);

                    string[] arrRow = text.Split(new string[] { "\r\n" }, StringSplitOptions.None);

                    List<string> listUserTemp = new List<string>();
                    ServiceReference1.WebService1SoapClient ws = new ServiceReference1.WebService1SoapClient();
                    
                    Dictionary<string,string> DicDevUser = new  Dictionary<string,string>();
                    string sUserTemp = "";
                    string sDevIDTemp = "";
                    //int countLocationChange = 0;//记录
                    foreach (string sRow in arrRow)
                    {
                        //
                        int indexID = sRow.IndexOf("ID:");
                        int indexUSER = sRow.IndexOf("用户:");
                        int indexNORT = sRow.IndexOf("北纬:");
                        int indexEAST = sRow.IndexOf("东经:");
                        int index = sRow.IndexOf('\t');
                        
                        if (sRow.Contains("上线") && sRow.Contains("用户"))
                        {
                            sUserTemp = sRow.Substring(indexUSER + 3, 11);
                            sDevIDTemp = sRow.Substring(indexID + 3, 4);//设备IDindexID
                            DicDevUser.Add(sDevIDTemp,sUserTemp);
                        }
                        else if (sRow.Contains("离线") && sRow.Contains("用户"))
                        {
                            sDevIDTemp = sRow.Substring(indexID + 3, 4);//设备IDindexID
                            DicDevUser.Remove(sDevIDTemp);

                        }
                        string sDateTime = sRow.Substring(0, index);
                        string sDevID = sRow.Substring(indexID + 3,4 );//设备IDindexID + 5
                        string[] arrUser = new string[4];
                        string[] arrLocation = new string[5];
                        if (sRow.Contains("用户"))
                        {
                            
                            string sStatus = sRow.Substring(index + 1, 2);//index + 3
                            string sUser = sRow.Substring(indexUSER + 3, 11);//用户

                            arrUser.SetValue(sDateTime, 0);
                            arrUser.SetValue(sStatus, 1);
                            arrUser.SetValue(sDevID, 2);
                            arrUser.SetValue(sUser, 3);
                            listUser.Add(arrUser);

                        }
                        else
                        {
                            string sNORT = sRow.Substring(indexNORT + 3, 9);//北纬
                            string sEAST = sRow.Substring(indexEAST + 3, 14);//东经

                            arrLocation.SetValue(sDateTime, 0);
                            arrLocation.SetValue(sDevID, 1);
                            arrLocation.SetValue(sNORT, 2);
                            arrLocation.SetValue(sEAST, 3);
                            string sUser1 = DicDevUser.FirstOrDefault(t => t.Key == sDevID).Value;

                            arrLocation.SetValue(string.IsNullOrEmpty(sUser1) == true ? " " : sUser1, 4);
                            listLocation.Add(arrLocation);
                        }
                    }
                    int countOn = 0;//统计上下线总次数
                    foreach (string[] item in listUser)
                    {
                        SqlParameter[] para = {
                        new SqlParameter("@DATE_TIME",SqlDbType.DateTime),
                        new SqlParameter("@STATUS",SqlDbType.VarChar,50),
                        new SqlParameter("@RES_ID",SqlDbType.VarChar,50),
                        new SqlParameter("@USER",SqlDbType.VarChar,50),
                        new SqlParameter("@NORTERN",SqlDbType.VarChar,50),
                        new SqlParameter("@EAST",SqlDbType.VarChar,50)
                        };
                        para[0].Value = item[0].ToString();
                        para[1].Value = item[1].ToString();
                        para[2].Value = item[2].ToString();
                        para[3].Value = item[3].ToString();
                        para[4].Value = " ";
                        para[5].Value = " ";
                        int iResult = DBHelper.ExecuteDataPara("insert into T_LOCATION (DATE_TIME,STATUS,RES_ID,[USER],NORTERN,EAST) values (@DATE_TIME,@STATUS,@RES_ID,@USER,@NORTERN,@EAST)", para);
                        countOn += iResult;
                    }
                    int countLocation = 0;  //统计更新位置次数
                    foreach (string[] item in listLocation)
                    {
                        SqlParameter[] para = {
                        new SqlParameter("@DATE_TIME",SqlDbType.DateTime),
                        new SqlParameter("@RES_ID",SqlDbType.VarChar,50),
                        new SqlParameter("@NORTERN",SqlDbType.VarChar,50),
                        new SqlParameter("@EAST",SqlDbType.VarChar,50),
                        new SqlParameter("@USER",SqlDbType.VarChar,50),
                        new SqlParameter("@STATUS",SqlDbType.VarChar,50)
                        };
                        para[0].Value = item[0].ToString();
                        para[1].Value = item[1].ToString();
                        para[2].Value = item[2].ToString();
                        para[3].Value = item[3].ToString();
                        para[4].Value = item[4].ToString();
                        para[5].Value = "更新位置";
                        int iResult = DBHelper.ExecuteDataPara("insert into T_LOCATION (DATE_TIME,RES_ID,NORTERN,EAST,[USER],STATUS) values (@DATE_TIME,@RES_ID,@NORTERN,@EAST,@USER,@STATUS)", para);
                        countLocation += iResult;

                    }
                //}
            //}
            //return;
        }

        public static List<FileInfo> GetFilesName()
        {
            DirectoryInfo dir = new DirectoryInfo("D:\\TEST");
            FileInfo[] files = dir.GetFiles();

            List<FileInfo> listFile = new List<FileInfo>();
            for (int i = 0; i < files.Length; i++)
            {
                listFile.Add(files[i]);
            }
            return listFile;
        }

        public static void test()
        {
            
            
        }
        public static List<MODEL.LOCATION> GetLOCATIONList(string SWhere)
        {
            DataSet ds = DBHelper.GetDataSet("select ID, DATE_TIME,RES_ID,[USER] ,STATUS,NORTERN,EAST  from T_LOCATION where 1 =1  " + SWhere);
            if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
            {
                MODEL.LOCATION MyUser = null;
                List<MODEL.LOCATION> MyUserList = new List<MODEL.LOCATION>();
                foreach (DataRow item in ds.Tables[0].Rows)
                {
                    MyUser = new MODEL.LOCATION();
                    MyUser.ID =Convert.ToInt32( item["ID"].ToString());
                    MyUser.DATE_TIME = item["DATE_TIME"].ToString();
                    MyUser.USER = item["USER"].ToString();
                    MyUser.STATUS = item["STATUS"].ToString();
                    MyUser.RES_ID = item["RES_ID"].ToString();
                    MyUser.NORTERN = item["NORTERN"].ToString();
                    MyUser.EAST = item["EAST"].ToString();
                    MyUserList.Add(MyUser);
                }
                return MyUserList;
            }
            else
            {
                return null;
            }
        }

    }
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!