using System.Data.SqlClient; using Remontor.SCCMsearch; using Remontor.Setting; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Reflection; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Xml.Linq; using static Remontor.Seacher.SeahcLocal; using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel; using System.Threading; using System.Windows.Forms; namespace Remontor.Seacher { internal class SeachSCCM : ISeaherMetod { private SqlConnection Connection; private IAuthType AuthType; //private List comps = new List(); private ResultUpdate Update; private bool enable; private string error; private Mode mode; private System.Threading.Timer TimerDisconnect; private enum Mode{ PCname, Login, Username } public SeachSCCM() { if (SGlobalSetting.settingSCCM.windowsAuth) { AuthType = new AuthWin(); } else { AuthType = new AuthLogin(); } } public SeachSCCM(bool AuthMetod, string User = "", string Pass = "") { if (AuthMetod) { AuthType = new AuthWin(); } else { AuthType = new AuthLogin(User, Pass); } } public void Change(ResultUpdate sender, string seach) { Activate(); Update = sender; if (Connection != null && Connection.State == ConnectionState.Open) { List result; if (seach.Length > 2) { result = ResultSeach(seach); Update(result); } else { result = new List(); result.Add(new NoResult("Введите запрос, к бд подключенно")); Update(result); } } else { List result = new List(); result.Add(new NoResult(error)); Update(result); } //Activate(); //Update = sender; //if (Connection != null && Connection.State == ConnectionState.Open) //{ // List result; // if (seach.Length > 2) // { // result = ResultSeach(seach); // int itemHeight = 14; // if(enable) itemHeight = 40; // Update(result, enable, itemHeight); // } // else // { // result = new List(); // result.Add("Введите запрос, к бд подключенно"); // Update(result, false, 14); // } //} //else //{ // List result = new List(); // result.Add(error); // Update(result, false,14); //} } private string QueryBilder(string query) { string result; Regex regexCyrillic = new Regex(@"\p{IsCyrillic}+", RegexOptions.IgnoreCase); Regex regexNumrable = new Regex(@"\d+", RegexOptions.IgnoreCase); MatchCollection jjj = regexNumrable.Matches(query); if (regexCyrillic.Matches(query).Count > 0) { result = "SELECT TOP (15) " + "dbo._RES_COLL_SMS00001.Name," + " dbo._RES_COLL_SMS00001.UserName," + " dbo.v_R_User.Full_User_Name0," + " dbo._RES_COLL_SMS00001.LastActiveTime" + " FROM dbo.v_R_User " + "JOIN dbo._RES_COLL_SMS00001 ON dbo.v_R_User.User_Name0=dbo._RES_COLL_SMS00001.UserName" + " WHERE LOWER(dbo.v_R_User.Full_User_Name0) LIKE LOWER(N'%" + query + "%')"; mode = Mode.Username; } else if(regexNumrable.Matches(query).Count > 0) { result = "SELECT TOP (15) dbo._RES_COLL_SMS00001.Name, dbo._RES_COLL_SMS00001.UserName, dbo.v_R_User.Full_User_Name0, dbo._RES_COLL_SMS00001.LastActiveTime FROM dbo._RES_COLL_SMS00001 LEFT JOIN dbo.v_R_User ON dbo._RES_COLL_SMS00001.UserName = dbo.v_R_User.User_Name0 WHERE LOWER(dbo._RES_COLL_SMS00001.Name) LIKE LOWER(N'%" + query + "%')"; mode = Mode.PCname; } else { result = "SELECT TOP (15) dbo._RES_COLL_SMS00001.Name, dbo._RES_COLL_SMS00001.UserName, dbo.v_R_User.Full_User_Name0, dbo._RES_COLL_SMS00001.LastActiveTime FROM dbo._RES_COLL_SMS00001 LEFT JOIN dbo.v_R_User ON dbo._RES_COLL_SMS00001.UserName = dbo.v_R_User.User_Name0 WHERE LOWER(dbo._RES_COLL_SMS00001.Name) LIKE LOWER(N'%" + query + "%') OR LOWER(dbo._RES_COLL_SMS00001.UserName) LIKE LOWER(N'%" + query + "%')"; mode = Mode.Login; } //MessageBox.Show(mode.ToString()); return result; } //public List ResultSeach(string seach) //{ // int y = 0; // comps.Clear(); // List result = new List(); // if (Connection.State == ConnectionState.Open && Connection != null) // { // try // { // string sql = QueryBilder(seach); // SqlCommand sqlCom = new SqlCommand(sql, Connection); // // Connection.Open(); // sqlCom.ExecuteNonQuery(); // SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlCom); // DataTable dt = new DataTable(); // dataAdapter.Fill(dt); // DataRow[] myData = dt.Select(); // for (int i = 0; i < myData.Length; i++) // { // HistoryResult itemResult = new HistoryResult(item.GetComp(), item.GetStart()); // result.Add(itemResult); // IComp comp = new CompId(myData[i].ItemArray[0].ToString()); // comp.SetName(myData[i].ItemArray[1].ToString()); // comp.SetDescription(myData[i].ItemArray[2].ToString()); // comps.Add(comp); // result.Add("ПК:"+ comp.GetNetNameStr() + " Логин:" + comp.GetName() + "\r\n" + comp.GetDescription() + "\r\nLastLogin:" + myData[i].ItemArray[3].ToString()); // ++y; // } // enable = true; // } // catch // { // y = 1; // enable = false; // result.Clear(); // result.Add("Ошибка выполнения запроса"); // } // } // if (y == 0) // { // enable = false; // result.Add("Ничего не найдено"); // } // TimerDisconnect.Change(90000, 90000); // return result; //} public List ResultSeach(string seach) { int y = 0; // comps.Clear(); List result = new List(); if (Connection.State == ConnectionState.Open && Connection != null) { try { string sql = QueryBilder(seach); SqlCommand sqlCom = new SqlCommand(sql, Connection); // Connection.Open(); sqlCom.ExecuteNonQuery(); SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlCom); DataTable dt = new DataTable(); dataAdapter.Fill(dt); DataRow[] myData = dt.Select(); for (int i = 0; i < myData.Length; i++) { // result.Add(itemResult); IComp comp = new CompId(myData[i].ItemArray[0].ToString()); comp.SetName(myData[i].ItemArray[1].ToString()); comp.SetDescription(myData[i].ItemArray[2].ToString()); // comps.Add(comp); string quest = ""; switch (mode) { case Mode.Login: quest = comp.GetName(); break; case Mode.Username: quest = comp.GetDescription(); break; case Mode.PCname: quest = comp.GetNetNameStr(); break; } string buffStr ="NOT"; if (myData[i].ItemArray[3] is DateTime) { buffStr = myData[i].ItemArray[3].ToString(); } else { buffStr = "Нет данных"; } SCCMResult itemResult = new SCCMResult(comp, buffStr, quest); result.Add(itemResult); ++y; } } catch { y = 1; //result.Clear(); result.Add(new NoResult("Ошибка обработки")); } } if (y == 0) { enable = false; result.Add(new NoResult("Ничего не найдено")); } TimerDisconnect.Change(90000, 90000); return result; } public void Activate() { if (Connection == null) { string stringConnect = "server=" + SGlobalSetting.settingSCCM.server + ";database=" + SGlobalSetting.settingSCCM.dataBase + ";" + AuthType.AuthString(); try { Connection = new SqlConnection(stringConnect); Connection.Open(); Console.WriteLine("Подключились"); error = "Подключенно"; TimerCallback TimerDelegate = new TimerCallback(Deactivate); TimerDisconnect = new System.Threading.Timer(TimerDelegate, null, 90000,90000); } catch { Console.WriteLine("Повторное бы подключение"); error = "Не удалось подключиться к базе"; } } } public void Deactivate(object obj) { Connection.Close(); Connection = null; TimerDisconnect.Dispose(); // List result = new List(); // result.Add(new NoResult("Введите запрос")); // Update(result); } //public IComp Result(int index) //{ // return comps[index]; //} //public string ResultString(int index) //{ // switch (mode) // { // case Mode.Login: // return comps[index].GetName(); // case Mode.Username: // return comps[index].GetDescription(); // case Mode.PCname: // return comps[index].GetNetNameStr(); // } // return ""; //} public string CheckConnect(string server, string basa) { if (Connection == null) { string stringConnect = "server=" + server + ";database=" + basa + ";" + AuthType.AuthString(); try { Connection = new SqlConnection(stringConnect); Connection.Open(); error = "Подключенно"; } catch { error = "Не удалось подключиться к серверу"; } } if (Connection.State == ConnectionState.Open && Connection != null) { try { string sql = "SELECT TOP (1) * FROM dbo._RES_COLL_SMS00001"; SqlCommand sqlCom = new SqlCommand(sql, Connection); sqlCom.ExecuteNonQuery(); SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlCom); DataTable dt = new DataTable(); dataAdapter.Fill(dt); DataRow[] myData = dt.Select(); if(myData.Length > 0) { error = "Соединие успешно установленно"; } } catch { error = "Ошибка выполнения запроса \nКакая то не правильная базаю"; } } return error; } } }