Newer
Older
RbFreqStand / RbFreqStandMeasure / R_DataBase / Service / Impl / DetectionItemServiceImpl.cs
yangqianqian on 6 May 2021 17 KB counter ui
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.Reflection;

namespace Casic.Birmm.RbFreqStandMeasure.R_DataBase.Service.Impl
{

    class DetectionItemServiceImpl : DetectionItemService
    {
        public long add(long deviceId, string startTime, string endTime, string stability, string accuracy, string bootFeature, string ageRate, String interval, string channelNo, string stability1, string stability10, string stability20, string stability100)
        {
            long detectionId = -1;
            int iRetval = -1;
            try
            {
                if (DbConnectService.mySqlConnect.State == ConnectionState.Closed)
                {
                    LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "addDetectionItem : 数据库链接断开");
                    iRetval = DbConnectService.openDb();
                    if (iRetval != 0)
                    {
                        LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "重连失败!");
                        return iRetval;
                    }
                }

                string sQry = "INSERT INTO r_detection_item (DEVICE_ID,START_TIME,END_TIME,STABILITY,ACCURACY,BOOT_FEATURE,AGE_RATE,STABILITY_INTERVAL,CHANNEL,STABILITY_1,STABILITY_10,STABILITY_20,STABILITY_100)" +
                    " VALUES (@DEVICE_ID,@START_TIME,@END_TIME,@STABILITY,@ACCURACY,@BOOT_FEATURE,@AGE_RATE,@INTERVAL,@CHANNEL,@STABILITY_1,@STABILITY_10,@STABILITY_20,@STABILITY_100)";

                MySqlCommand cmd = new MySqlCommand(sQry, DbConnectService.mySqlConnect);
                cmd.Parameters.Add("@DEVICE_ID", MySqlDbType.Int64, 20).Value = deviceId;
                cmd.Parameters.Add("@START_TIME", MySqlDbType.String, 50).Value = startTime;
                cmd.Parameters.Add("@END_TIME", MySqlDbType.String, 50).Value = endTime;
                cmd.Parameters.Add("@STABILITY", MySqlDbType.String, 50).Value = stability;
                cmd.Parameters.Add("@ACCURACY", MySqlDbType.String, 50).Value = accuracy;
                cmd.Parameters.Add("@BOOT_FEATURE", MySqlDbType.String, 50).Value = bootFeature;
                cmd.Parameters.Add("@AGE_RATE", MySqlDbType.String, 50).Value = ageRate;
                cmd.Parameters.Add("@INTERVAL", MySqlDbType.String, 30).Value = interval;
                cmd.Parameters.Add("@CHANNEL", MySqlDbType.String, 30).Value = channelNo;
                cmd.Parameters.Add("@STABILITY_1", MySqlDbType.String, 30).Value = stability1;
                cmd.Parameters.Add("@STABILITY_10", MySqlDbType.String, 30).Value = stability10;
                cmd.Parameters.Add("@STABILITY_20", MySqlDbType.String, 30).Value = stability20;
                cmd.Parameters.Add("@STABILITY_100", MySqlDbType.String, 30).Value = stability100;

                cmd.ExecuteNonQuery();

                sQry = "UPDATE r_device SET STATUSID=@STATUSID, CHANNEL=@CHANNEL where ID=@ID";
                cmd = new MySqlCommand(sQry, DbConnectService.mySqlConnect);
                cmd.Parameters.Add("@ID", MySqlDbType.Int64, 20).Value = deviceId;
                cmd.Parameters.Add("@STATUSID", MySqlDbType.String, 20).Value = "2";
                cmd.Parameters.Add("@CHANNEL", MySqlDbType.String, 20).Value = channelNo;

                cmd.ExecuteNonQuery();

                sQry = "SELECT max(ID) from r_detection_item";
                cmd = new MySqlCommand(sQry, DbConnectService.mySqlConnect);

               
                using (MySqlDataReader aReader = cmd.ExecuteReader())
                {
                    if (aReader.Read()) detectionId = Convert.ToInt64(aReader.GetString(0));
                }
                cmd.Dispose();
                iRetval = 0;
            }
            catch (MySqlException e)
            {
                LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "addDetectionItem: " + e.Message);
                iRetval = -1;
            }
            return detectionId;
        }

        public int update(long deviceId, string startTime, string endTime, string stability, string accuracy, string bootFeature, string ageRate, String interval, string counterDetec)
        {
            int iRetval = -1;
            try
            {
                if (DbConnectService.mySqlConnect.State == ConnectionState.Closed)
                {
                    LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "updateDetectionItem : 数据库链接断开");
                    iRetval = DbConnectService.openDb();
                    if (iRetval != 0)
                    {
                        LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "重连失败!");
                        return iRetval;
                    }
                }

                string sQry = "UPDATE r_detection_item  SET START_TIME=@START_TIME,END_TIME=@END_TIME,STABILITY=@STABILITY,ACCURACY=@ACCURACY,BOOT_FEATURE=@BOOT_FEATURE,AGE_RATE=@AGE_RATE,STABILITY_INTERVAL=@INTERVAL,COUNTER_DETEC=@COUNTER_DETEC WHERE DEVICE_ID = " + deviceId;

                MySqlCommand cmd = new MySqlCommand(sQry, DbConnectService.mySqlConnect);
                cmd.Parameters.Add("@START_TIME", MySqlDbType.String, 50).Value = startTime;
                cmd.Parameters.Add("@END_TIME", MySqlDbType.String, 50).Value = endTime;
                cmd.Parameters.Add("@STABILITY", MySqlDbType.String, 10).Value = stability;
                cmd.Parameters.Add("@ACCURACY", MySqlDbType.String, 10).Value = accuracy;
                cmd.Parameters.Add("@BOOT_FEATURE", MySqlDbType.String, 10).Value = bootFeature;
                cmd.Parameters.Add("@AGE_RATE", MySqlDbType.String, 10).Value = ageRate;
                cmd.Parameters.Add("@INTERVAL", MySqlDbType.String, 30).Value = interval;
                cmd.Parameters.Add("@COUNTER_DETEC", MySqlDbType.String, 30).Value = counterDetec;

                cmd.ExecuteNonQuery();

                cmd.Dispose();
                iRetval = 0;
            }
            catch (MySqlException e)
            {
                LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "updateDetectionItem: " + e.Message);
                iRetval = -1;
            }
            return iRetval;
        }

        public int updateDetecStatus(long detectionId, string stability, string accuracy, string bootFeature, string ageRate, string counterDetec, string stability1, string stability10, string stability20, string stability100)
        {
            int iRetval = -1;

            try
            {
                if (DbConnectService.mySqlConnect.State == ConnectionState.Closed)
                {
                    LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "updateDetectionItem : 数据库链接断开");
                    iRetval = DbConnectService.openDb();
                    if (iRetval != 0)
                    {
                        LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "重连失败!");
                        return iRetval;
                    }
                }

                if (stability.Equals("")&&stability10.Equals("") && stability20.Equals("") && stability100.Equals("") && stability1.Equals("") && accuracy.Equals("") && bootFeature.Equals("") && ageRate.Equals("")) return -1;

                string sQry = "UPDATE r_detection_item SET";
                if(!stability.Equals(""))
                    sQry = sQry + " STABILITY=@STABILITY";
                if (!accuracy.Equals(""))
                    sQry = sQry + " ACCURACY=@ACCURACY";
                if (!bootFeature.Equals(""))
                    sQry = sQry + " BOOT_FEATURE=@BOOT_FEATURE";
                if (!ageRate.Equals(""))
                    sQry = sQry + " AGE_RATE=@AGE_RATE";
                if (!counterDetec.Equals(""))
                    sQry = sQry + " COUNTER_DETEC=@COUNTER_DETEC";
                if (!stability1.Equals(""))
                    sQry = sQry + " STABILITY_1=@STABILITY_1";
                if (!stability10.Equals(""))
                    sQry = sQry + " STABILITY_10=@STABILITY_10";
                if (!stability20.Equals(""))
                    sQry = sQry + " STABILITY_20=@STABILITY_20";
                if (!stability100.Equals(""))
                    sQry = sQry + " STABILITY_100=@STABILITY_100";
                sQry = sQry + " where ID = " + detectionId;


                MySqlCommand cmd = new MySqlCommand(sQry, DbConnectService.mySqlConnect);
                cmd.Parameters.Add("@STABILITY", MySqlDbType.String, 10).Value = stability;
                cmd.Parameters.Add("@ACCURACY", MySqlDbType.String, 10).Value = accuracy;
                cmd.Parameters.Add("@BOOT_FEATURE", MySqlDbType.String, 10).Value = bootFeature;
                cmd.Parameters.Add("@AGE_RATE", MySqlDbType.String, 10).Value = ageRate;
                cmd.Parameters.Add("@COUNTER_DETEC", MySqlDbType.String, 10).Value = counterDetec;
                cmd.Parameters.Add("@STABILITY_1", MySqlDbType.String, 10).Value = stability1;
                cmd.Parameters.Add("@STABILITY_10", MySqlDbType.String, 10).Value = stability10;
                cmd.Parameters.Add("@STABILITY_20", MySqlDbType.String, 10).Value = stability20;
                cmd.Parameters.Add("@STABILITY_100", MySqlDbType.String, 10).Value = stability100;

                cmd.ExecuteNonQuery();

                cmd.Dispose();
                iRetval = 0;
            }
            catch (MySqlException e)
            {
                LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "updateDetectionItem: " + e.Message);
                iRetval = -1;
            }

            return iRetval;
        }


        public int stopDetection(long deviceId, string startTime, string endTime, bool isDeleteData)
        {
            int iRetval = -1;
            try
            {
                if (DbConnectService.mySqlConnect.State == ConnectionState.Closed)
                {
                    LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "updateDetectionItem : 数据库链接断开");
                    iRetval = DbConnectService.openDb();
                    if (iRetval != 0)
                    {
                        LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "重连失败!");
                        return iRetval;
                    }
                }
                
                string sQry = "UPDATE r_device SET STATUSID="+"1"+", CHANNEL=@CHANNEL where ID=" + deviceId;
                MySqlCommand cmd = new MySqlCommand(sQry, DbConnectService.mySqlConnect);
                cmd.Parameters.Add("@CHANNEL", MySqlDbType.String, 20).Value = "";
                cmd.ExecuteNonQuery();

                if (isDeleteData)
                {
                    LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "starttime : "+ startTime + "    endtime:"+endTime);
                    sQry = "DELETE FROM r_detection_item WHERE DEVICE_ID = " + deviceId + " AND START_TIME='" + startTime + "' AND END_TIME='" + endTime+"'";
                    cmd = new MySqlCommand(sQry, DbConnectService.mySqlConnect);
                    cmd.ExecuteNonQuery();

                    sQry = "DELETE FROM r_detection WHERE DEVICE_ID = " + deviceId + " AND (LOG_TIME between'" + startTime+"' and '" + endTime + "')" ;
                    cmd = new MySqlCommand(sQry, DbConnectService.mySqlConnect);
                    cmd.ExecuteNonQuery();
                }

                cmd.Dispose();
                iRetval = 0;
            }
            catch (MySqlException e)
            {
                LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "updateDetectionItem: " + e.Message);
                iRetval = -1;
            }
            return iRetval;
        }

        public List<DetectionItem> search(long deviceId, bool isInDetection)
        {
            List<DetectionItem> detectionItemList = new List<DetectionItem>();
            try
            {
                if (DbConnectService.mySqlConnect.State == ConnectionState.Closed)
                {
                    LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "searchDetectionItem : 数据库链接断开");
                    int iRetval = DbConnectService.openDb();
                    if (iRetval != 0)
                    {
                        LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "重连失败!");
                        return null;
                    }
                }

                string sQry = "SELECT * FROM r_detection_item where DEVICE_ID = " + deviceId;
                if (isInDetection)
                {
                    sQry = sQry + " and ('" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' between START_TIME AND END_TIME)";
                }
                MySqlCommand aCommand = new MySqlCommand(sQry, DbConnectService.mySqlConnect);

                using (MySqlDataReader aReader = aCommand.ExecuteReader())
                {
                    while (aReader.Read())
                    {
                        DetectionItem detectionItem = new DetectionItem();
                        //姓名
                        if (!aReader.IsDBNull(0)) detectionItem.Id=Convert.ToInt32(aReader.GetString(0));
                        if (!aReader.IsDBNull(1)) detectionItem.DeviceId = Convert.ToInt64(aReader.GetString(1));
                        if (!aReader.IsDBNull(2)) detectionItem.StartTime = aReader.GetString(2);
                        if (!aReader.IsDBNull(3)) detectionItem.EndTime = aReader.GetString(3);
                        if (!aReader.IsDBNull(4)) detectionItem.Stability = aReader.GetString(4);
                        if (!aReader.IsDBNull(5)) detectionItem.Accuracy = aReader.GetString(5);
                        if (!aReader.IsDBNull(6)) detectionItem.BootFeature = aReader.GetString(6);
                        if (!aReader.IsDBNull(7)) detectionItem.AgeRate = aReader.GetString(7);
                        if (!aReader.IsDBNull(8)) detectionItem.Interval = aReader.GetString(8);
                        if (!aReader.IsDBNull(10)) detectionItem.Channel = aReader.GetString(10);
                        if (!aReader.IsDBNull(11)) detectionItem.Stability1 = aReader.GetString(11);
                        if (!aReader.IsDBNull(12)) detectionItem.Stability10 = aReader.GetString(12);
                        if (!aReader.IsDBNull(13)) detectionItem.Stability20 = aReader.GetString(13);
                        if (!aReader.IsDBNull(14)) detectionItem.Stability100 = aReader.GetString(14);
                        detectionItemList.Add(detectionItem);
                    }

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

        public DetectionItem searchById(long detectionId)
        {
            DetectionItem detectionItem = null;
            try
            {
                if (DbConnectService.mySqlConnect.State == ConnectionState.Closed)
                {
                    LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "searchDetectionItem : 数据库链接断开");
                    int iRetval = DbConnectService.openDb();
                    if (iRetval != 0)
                    {
                        LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "重连失败!");
                        return null;
                    }
                }

                string sQry = "SELECT * FROM r_detection_item where ID = " + detectionId;
                
                MySqlCommand aCommand = new MySqlCommand(sQry, DbConnectService.mySqlConnect);

                using (MySqlDataReader aReader = aCommand.ExecuteReader())
                {
                    while (aReader.Read())
                    {
                        detectionItem = new DetectionItem();
                        //姓名
                        if (!aReader.IsDBNull(0)) detectionItem.Id = Convert.ToInt32(aReader.GetString(0));
                        if (!aReader.IsDBNull(1)) detectionItem.DeviceId = Convert.ToInt64(aReader.GetString(1));
                        if (!aReader.IsDBNull(2)) detectionItem.StartTime = aReader.GetString(2);
                        if (!aReader.IsDBNull(3)) detectionItem.EndTime = aReader.GetString(3);
                        if (!aReader.IsDBNull(4)) detectionItem.Stability = aReader.GetString(4);
                        if (!aReader.IsDBNull(5)) detectionItem.Accuracy = aReader.GetString(5);
                        if (!aReader.IsDBNull(6)) detectionItem.BootFeature = aReader.GetString(6);
                        if (!aReader.IsDBNull(7)) detectionItem.AgeRate = aReader.GetString(7);
                        if (!aReader.IsDBNull(8)) detectionItem.Interval = aReader.GetString(8);
                        if (!aReader.IsDBNull(10)) detectionItem.Channel = aReader.GetString(10);
                        if (!aReader.IsDBNull(11)) detectionItem.Stability1 = aReader.GetString(11);
                        if (!aReader.IsDBNull(12)) detectionItem.Stability10 = aReader.GetString(12);
                        if (!aReader.IsDBNull(13)) detectionItem.Stability20 = aReader.GetString(13);
                        if (!aReader.IsDBNull(14)) detectionItem.Stability100 = aReader.GetString(14);
                    }

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

    }
}