Newer
Older
RbFreqStand / RbFreqStandMeasure / R_DataBase / Service / Impl / DetectionServiceImpl.cs
yangqianqian on 14 May 2021 6 KB s
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)
        {
            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 = DateTime.Now;
                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";
                MySqlCommand aCommand = new MySqlCommand(sQry, DbConnectService.mySqlConnect);

                using (MySqlDataReader aReader = aCommand.ExecuteReader())
                {
                    while (aReader.Read())
                    {
                        Model.Detection detection = new Model.Detection();
                        //姓名
                        if (!aReader.IsDBNull(0)) detection.Id = Convert.ToInt64(aReader.GetString(0));
                        if (!aReader.IsDBNull(1)) detection.DeviceId = Convert.ToInt64(aReader.GetString(1));
                        if (!aReader.IsDBNull(2)) detection.LogTime = Convert.ToDateTime(aReader.GetString(2)).ToString("yyyy-MM-dd HH:mm:ss");
                        if (!aReader.IsDBNull(3)) detection.Frequency = aReader.GetString(3);
                        if (!aReader.IsDBNull(4)) detection.DetectionItem = aReader.GetString(4);

                        detectionDtoList.Add(detection);
                    }

                    aCommand.Dispose();
                }
            }
            catch (MySqlException ex)
            {
                LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "searchDetectionItem: " + ex.Message);
                detectionDtoList = null;
            }
            return detectionDtoList;
        }

    }
}