using CommonStorage.Model; using MySql.Data.MySqlClient; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Runtime.ConstrainedExecution; using System.Runtime.InteropServices.ComTypes; using System.Runtime.Remoting.Metadata.W3cXsd2001; using System.Text; using static Google.Protobuf.Reflection.FieldOptions.Types; namespace CommonStorage { public class DBHelper { static string connectionString = "server=localhost;port=3306;database=jf200;uid=mysqltest;password=test1234"; public static bool InsertIntoLog(string msgType, Model.LevelEnum msgLevel, string msg) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { connection.Open(); // MySqlTransaction transaction = connection.BeginTransaction(); try { using (var context = new MyDb(connection, false)) { // Passing an existing transaction to the context // context.Database.UseTransaction(transaction); context.logs.Add(new Model.Log() { MsgType = msgType, MsgLevel = msgLevel, Message = msg, CreateTime = DateTime.Now }); context.SaveChanges(); } // transaction.Commit(); } catch(Exception ex) { throw ex; } } return true; }//end InsertIntoLog public static User QueryUser(string userName) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { connection.Open(); try { using (var context = new MyDb(connection, false)) { var user = context.users.FirstOrDefault(x => x.UserName == userName); return user; } } catch (Exception ex) { throw ex; } } } /// /// 查询配置的测试项目 /// /// 产品代号 /// public static List> QueryTestPar(string ProductCode) { List> TpsDatas = new List>(); using (MySqlConnection connection = new MySqlConnection(connectionString)) { connection.Open(); try { using (var context = new MyDb(connection, false)) { var config = context.tpsConfigs.FirstOrDefault(x => x.ProductCode == ProductCode); if(string.IsNullOrEmpty(config.Values)) { TpsDatas = JsonConvert.DeserializeObject>>(config.Values); } } } catch (Exception ex) { throw ex; } } return TpsDatas; } /// /// 新增或者更新产品测试配置数据 /// /// /// public static bool AddOrUpdateTestPar(string ProductCode, List> dataItems) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { connection.Open(); try { using (var context = new MyDb(connection, false)) { var config = context.tpsConfigs.FirstOrDefault(x => x.ProductCode == ProductCode); if(config == null) { TpsConfig tpsConfig = new TpsConfig(); tpsConfig.ProductCode = ProductCode; tpsConfig.TpsName = "JF200测试程序"; tpsConfig.TestItem = "空载-负载-短路"; tpsConfig.TestProject = "常温测试"; if(dataItems != null) { tpsConfig.Values = JsonConvert.SerializeObject(dataItems); } context.tpsConfigs.Add(tpsConfig); } else { config.Values = ""; if (dataItems != null) { config.Values = JsonConvert.SerializeObject(dataItems); } } context.SaveChanges(); } } catch (Exception ex) { throw ex; } } return true; } //删除产品测试配置项 public static bool DeleteTestPar(string ProductCode) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { connection.Open(); try { using (var context = new MyDb(connection, false)) { var config = context.tpsConfigs.FirstOrDefault(x => x.ProductCode == ProductCode); if (config != null) { context.tpsConfigs.Remove(config); } context.SaveChanges(); } } catch (Exception ex) { throw ex; } } return true; } /// /// 根据筛选信息查询测试数据,不含Values,Values是表内容,以加快查询返回 /// /// 产品编号 /// 产品代号 /// 产品批次 /// 开始时间 /// 结束数据 /// public static List QueryTestData(string Serial, string ProductCode, string BarCode, DateTime startTime, DateTime stopTime) { List datas = new List(); using (MySqlConnection connection = new MySqlConnection(connectionString)) { connection.Open(); try { using (var context = new MyDb(connection, false)) { //不含Values,Values是表内容,以加快查询返回 //var query = from x in context.testDatas // where ((string.IsNullOrEmpty(Serial) || x.Serial.Contains(Serial)) && // (string.IsNullOrEmpty(ProductCode) || x.ProductCode.Equals(ProductCode)) && // (string.IsNullOrEmpty(BarCode) || x.BarCode.Equals(BarCode)) && // x.TestTime >= startTime && // x.TestTime <= stopTime) // select new TestDataBase // { // ID = x.ID, // BarCode = x.BarCode, // DataName = x.DataName, // TestTime = x.TestTime, // ProductCode = x.ProductCode, // ProductName = x.ProductName, // ProductNum = x.ProductNum, // Serial = x.Serial, // IsPass = x.IsPass, // Tester = x.Tester, // TestProject = x.TestProject // }; var query = from x in context.testDatas where ((string.IsNullOrEmpty(Serial) || x.Serial.Contains(Serial)) && (x.ProductCode.Equals(ProductCode)) && (string.IsNullOrEmpty(BarCode) || x.BarCode.Equals(BarCode)) && x.TestTime >= startTime && x.TestTime <= stopTime) select new TestDataBase { ID = x.ID, BarCode = x.BarCode, DataName = x.DataName, TestTime = x.TestTime, ProductCode = x.ProductCode, ProductName = x.ProductName, ProductNum = x.ProductNum, Serial = x.Serial, IsPass = x.IsPass, Tester = x.Tester, TestProject = x.TestProject }; datas = query.ToList(); } } catch (Exception ex) { throw ex; } } return datas; } /// /// 查询测试数据,ID是QueryTestData查出来的数据ID /// 用于数据查询,带Values /// /// 数据ID /// public static TestData QueryTestData(long ID) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { connection.Open(); try { using (var context = new MyDb(connection, false)) { var query = context.testDatas.FirstOrDefault(x => x.ID == ID); return query; } } catch (Exception ex) { throw ex; } } return null; } /// /// 根据ID删除指定的测试数据 /// /// /// public static bool DeleteTestData(long ID) { List ids = new List(); ids.Add(ID); return DeleteTestDatas(ids); } /// /// 根据ID删除指定的测试数据,可以一次删除多条 /// /// public static bool DeleteTestDatas(List ids) { if(ids == null) return false; using (MySqlConnection connection = new MySqlConnection(connectionString)) { connection.Open(); try { using (var context = new MyDb(connection, false)) { foreach (var id in ids) { var query = context.testDatas.FirstOrDefault(x => x.ID == id); if(query != null) { context.testDatas.Remove(query); } } context.SaveChanges(); } return true; } catch (Exception ex) { throw ex; } } } /// /// 添加测试项 /// /// /// public static bool AddTestData(TestData testData) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { connection.Open(); try { using (var context = new MyDb(connection, false)) { context.testDatas.Add(testData); context.SaveChanges(); return true; } } catch (Exception ex) { throw ex; } } return true; } /// /// 查询日志 /// /// 工位号 /// 开始时间 /// 截止时间 /// public static List QueryLog(string workstation, DateTime startTime, DateTime stopTime) { List logItems = new List(); using (MySqlConnection connection = new MySqlConnection(connectionString)) { connection.Open(); try { using (var context = new MyDb(connection, false)) { //不含Values,Values是表内容,以加快查询返回 var query = from x in context.logs where ((string.IsNullOrEmpty(workstation) || x.Workstation.Equals(workstation)) && x.CreateTime >= startTime && x.CreateTime <= stopTime) select x; logItems = query.ToList(); } } catch (Exception ex) { throw ex; } } return logItems; } }//class DBHelper }