using Casic.Birmm.RbFreqStandMeasure.R_DataBase.Dto; using Casic.Birmm.RbFreqStandMeasure.R_DataBase.Model; using Casic.Birmm.RbFreqStandMeasure.Tools; using MySql.Data.MySqlClient; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Reflection; using System.Text; using static System.Net.Mime.MediaTypeNames; namespace Casic.Birmm.RbFreqStandMeasure.R_DataBase.Service.Impl { class DetectionServiceImpl : DetectionService { public int add(long deviceId, string frequency, string detecItemType, long detectionItemId, DateTime time) { int iRetval = -1; try { if (DbConnectService.mySqlConnect.State == ConnectionState.Closed) { LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "addDetection : 数据库链接断开"); iRetval = DbConnectService.openDb(); if (iRetval != 0) { LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "重连失败!"); return iRetval; } } string sQry = "INSERT INTO r_detection (DEVICE_ID,LOG_TIME,FREQUENCY,DETECTION_ITEM,DETECTION_ITEM_ID) " + "values(@DEVICE_ID,@LOG_TIME,@FREQUENCY,@DETECTION_ITEM,@DETECTION_ITEM_ID)"; MySqlCommand cmd = new MySqlCommand(sQry, DbConnectService.mySqlConnect); cmd.Parameters.Add("@DEVICE_ID", MySqlDbType.Int64, 20).Value = deviceId; cmd.Parameters.Add("@FREQUENCY", MySqlDbType.String, 255).Value = frequency; cmd.Parameters.Add("@LOG_TIME", MySqlDbType.DateTime, 0).Value = time; cmd.Parameters.Add("@DETECTION_ITEM", MySqlDbType.String, 0).Value = detecItemType; cmd.Parameters.Add("@DETECTION_ITEM_ID", MySqlDbType.Int64, 0).Value = detectionItemId; cmd.ExecuteNonQuery(); cmd.Dispose(); iRetval = 0; } catch (MySqlException ex) { LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "addDetection : " + ex.Message); iRetval = -1; } return iRetval; } public int updateFrequency(long detectionId, string fre) { int iRetval = -1; try { if (DbConnectService.mySqlConnect.State == ConnectionState.Closed) { LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "updateFrequency : 数据库链接断开"); iRetval = DbConnectService.openDb(); if (iRetval != 0) { LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "重连失败!"); return iRetval; } } string sQry = "UPDATE r_detection SET FREQUENCY=@FREQUENCY WHERE ID = " + detectionId; LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "updateFrequency sQry:"+ sQry); MySqlCommand cmd = new MySqlCommand(sQry, DbConnectService.mySqlConnect); cmd.Parameters.Add("@FREQUENCY", MySqlDbType.String, 50).Value = fre; cmd.ExecuteNonQuery(); cmd.Dispose(); iRetval = 0; } catch (MySqlException ex) { LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "updateDeviced : " + ex.Message); } return iRetval; } public List<Detection> search(long deviceId, string detectionType, string startTime, string endTime, long detectionItemId) { List<Detection> detectionDtoList = new List<Detection>(); try { if (DbConnectService.mySqlConnect.State == ConnectionState.Closed) { LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "searchDetection : 数据库链接断开"); int iRetval = DbConnectService.openDb(); if (iRetval != 0) { LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "重连失败!"); return null; } } //string sQry = "SELECT * FROM r_detection where DEVICE_ID = " + deviceId +" and DETECTION_ITEM='" + detectionType + "' and (LOG_TIME BETWEEN '" + startTime + "' and '"+ endTime + "') and DETECTION_ITEM_ID="+detectionItemId+" order by LOG_TIME"; string sQry = "SELECT * FROM r_detection where DEVICE_ID = " + deviceId + " and DETECTION_ITEM='" + detectionType + "' and DETECTION_ITEM_ID=" + detectionItemId + " order by LOG_TIME"; MySqlCommand aCommand = new MySqlCommand(sQry, DbConnectService.mySqlConnect); using (MySqlDataReader aReader = aCommand.ExecuteReader()) { while (aReader.Read()) { Detection detection = new Detection(); if (!aReader.IsDBNull(0)) detection.Id = Convert.ToInt64(aReader.GetString(0)); // id if (!aReader.IsDBNull(1)) detection.DeviceId = Convert.ToInt64(aReader.GetString(1)); // devName if (!aReader.IsDBNull(2)) detection.LogTime = aReader.GetString(2); // devCode if (!aReader.IsDBNull(3)) detection.Frequency = aReader.GetString(3); // devType if (!aReader.IsDBNull(4)) detection.DetectionItem = aReader.GetString(4); // devModel detectionDtoList.Add(detection); } aReader.Close(); } aCommand.Dispose(); } catch (MySqlException ex) { LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "searchDetectionItem: " + ex.Message); detectionDtoList = null; } return detectionDtoList; } } }