蓄电池监控管理平台数据库初始化程序
DELL
2024-11-05 578a33144c0435f7479535e540a7e60622e4b232
BattMonitor_DB_Builder/src/com/database_util/DB_Alarm.java
@@ -1,865 +1,103 @@
package com.database_util;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import com.base.Com;
import com.sql.MysqlConnPool;
import com.sql.Sql_Mysql;
public class DB_Alarm {
   public static void init(MysqlConnPool pool, boolean recreate) {
      System.out.println(" db_alarm init start at " + Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms));
      createDB_ALARM(pool);
      createBattalarm_Data_History_Table(pool, recreate);
      createBattalarm_Data_Table(pool, recreate);
      createBts_Station_Event_Table(pool, recreate);
      createDevalarm_Data__History_Table(pool, recreate);
      createDevalarm_Data_Table(pool, recreate);
      createTb_Alarm_Message(pool, recreate);
      createTb_Alarm_Detail_Table(pool, recreate);            //创建告警详情
      createTb_Alarm_Relation_Table(pool, recreate);            //创建告警对应关系
      createTb_Alarm_Manual_Clear_Table(pool,recreate);
      createTb_Battalarm_Data_Verify_Table(pool, recreate);      //创建电池告警确认处理表
      createTb_Devalarm_Data_Verify_Table(pool, recreate);      //创建设备告警确认处理表
      repairBattAlarmDataHistoryTable(pool);                  //电池告警分表新增是否紧急字段
      repairDevAlarmDataHistoryTable(pool);                  //设备告警分表新增是否紧急字段
      System.out.println(" db_alarm init end at " + Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms));
   }
   /**
    *    修复设备历史告警分表中缺少新增字段  `alm_severity`-'告警重要等级[0:重要;1:紧急]'
    * @param pool
    */
   private static void repairDevAlarmDataHistoryTable(MysqlConnPool pool) {
      String sql_str_sel = " select TABLE_NAME,CREATE_TIME from information_schema.tables " +
             " where table_schema='db_alarm' " +
             " AND TABLE_NAME like 'tb_devalarm_data_history_%';";
      Sql_Mysql sql = new Sql_Mysql(pool.getConn());
      ResultSet res1 = null;
      ResultSet res2= null;
      try {
         res1 = sql.sqlMysqlTotalQuery(sql_str_sel);
         while(res1.next()) {
            String tablename = res1.getString("TABLE_NAME");
            //System.out.println("表名:"+tablename);
            res2 = null;
            //   //添加指定的alm_severity列'告警重要等级[0:重要;1:紧急]'
            res2 = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
               + " WHERE table_schema='db_alarm'"
               + " AND table_name='" + tablename + "'"
               + " AND column_name='alm_severity'");
            if(false == res2.next()) {
               sql.sqlMysqlTotalExecute(" ALTER TABLE db_alarm." + tablename
                  + " ADD COLUMN `alm_severity` int(11) NOT NULL DEFAULT '1' COMMENT '告警重要等级[0:重要;1:紧急]';");
            }
         }
      } catch (SQLException e) {
         e.printStackTrace();
      } finally {
         if(null != res1) {
            try {
               res1.close();
            } catch (SQLException e) {
               e.printStackTrace();
            }
         }
         if(null != res2) {
            try {
               res1.close();
            } catch (SQLException e) {
               e.printStackTrace();
            }
         }
         sql.close_con();
      }
   }
   /**
    *    修复电池历史告警分表中缺少新增字段  `alm_severity`-'告警重要等级[0:重要;1:紧急]'
    * @param pool
    */
   private static void repairBattAlarmDataHistoryTable(MysqlConnPool pool) {
      String sql_str_sel = " select TABLE_NAME,CREATE_TIME from information_schema.tables " +
                      " where table_schema='db_alarm' " +
                      " AND TABLE_NAME like 'tb_battalarm_data_history_%';";
      Sql_Mysql sql = new Sql_Mysql(pool.getConn());
      ResultSet res1 = null;
      ResultSet res2= null;
      try {
         res1 = sql.sqlMysqlTotalQuery(sql_str_sel);
         while(res1.next()) {
            String tablename = res1.getString("TABLE_NAME");
            //System.out.println("表名:"+tablename);
            res2 = null;
//            //添加指定的alm_severity列'告警重要等级[0:重要;1:紧急]'
            res2 = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
                  + " WHERE table_schema='db_alarm'"
                  + " AND table_name='" + tablename + "'"
                  + " AND column_name='alm_severity'");
            if(false == res2.next()) {
               sql.sqlMysqlExecute(" ALTER TABLE db_alarm." + tablename
                     + " ADD COLUMN `alm_severity` int(11) NOT NULL DEFAULT '1' COMMENT '告警重要等级[0:重要;1:紧急]';");
            }
         }
      } catch (SQLException e) {
         e.printStackTrace();
      } finally {
         if(null != res1) {
            try {
               res1.close();
            } catch (SQLException e) {
               e.printStackTrace();
            }
         }
         if(null != res2) {
            try {
               res1.close();
            } catch (SQLException e) {
               e.printStackTrace();
            }
         }
         sql.close_con();
      }
   }
   /**
    *    创建    db_alarm 数据库
    * @param pool
    */
   public static void createDB_ALARM(MysqlConnPool pool) {
      Sql_Mysql sql = new Sql_Mysql(pool.getConn());
      try {
         sql.sqlMysqlExecute("CREATE DATABASE IF NOT EXISTS " + Sql_Mysql.DB_ALARM);
      } catch (SQLException e) {
         e.printStackTrace();
      } finally {
         sql.close_con();
      }
   }
   /**
    *    创建tb_devalarm_data_history表
    */
   public static void createDevalarm_Data__History_Table(MysqlConnPool pool, boolean recreate) {
      String sql_str01 = " DROP TABLE IF EXISTS "+Sql_Mysql.Devalarm_Data__History_Table;
      String sql_str02 = " CREATE TABLE IF NOT EXISTS " + Sql_Mysql.Devalarm_Data__History_Table + " (" +
            "  num bigint(20) NOT NULL AUTO_INCREMENT," +
            "  record_id bigint(20) NOT NULL DEFAULT '0'," +
            "  dev_id int(11) NOT NULL DEFAULT '0'," +
            "  dev_ip varchar(24) NOT NULL DEFAULT '127.0.0.1'," +
            "  alm_type int(11) NOT NULL DEFAULT '0'," +
            "  alm_level int(11) NOT NULL DEFAULT '0'," +
            "  alm_start_time datetime NOT NULL DEFAULT '1982-01-01 00:00:00'," +
            "  alm_end_time datetime NOT NULL DEFAULT '1982-01-01 00:00:00'," +
            "  alm_value float NOT NULL DEFAULT '0'," +
            "  alm_is_confirmed tinyint(1) NOT NULL DEFAULT '0'," +
            "  alm_confirmed_time datetime NOT NULL DEFAULT '1982-01-01 00:00:00'," +
            "  alm_cleared_type int(11) NOT NULL DEFAULT '0'," +
            "  usr_Id int(11) NOT NULL DEFAULT '0'," +
            "  `alm_severity` int(11) NOT NULL DEFAULT '1' COMMENT '告警重要等级[0:重要;1:紧急]'," +
            "  PRIMARY KEY (num)," +
            "  KEY index_record_id (record_id)," +
            "  KEY index_dev_id (dev_id)," +
            "  KEY index_alm_type (alm_type)," +
            "  KEY index_alm_start_time (alm_start_time)," +
            "  KEY index_alm_cleared_type (alm_cleared_type)" +
            ") ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;";
      Sql_Mysql sql = new Sql_Mysql(pool.getConn());
      ResultSet res = null;
      try {
         if(true == recreate) {
            sql.sqlMysqlExecute(sql_str01);
         }
         sql.sqlMysqlExecute(sql_str02);
         //添加指定的alm_value列'告警值'
         res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
               + " WHERE table_schema='db_alarm'"
               + " AND table_name='tb_devalarm_data_history'"
               + " AND column_name='alm_value'");
         if(false == res.next()) {
            sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Devalarm_Data__History_Table
                        + " ADD COLUMN alm_value float NOT NULL DEFAULT '0';");
         }
         //添加指定的alm_severity列'告警重要等级[0:重要;1:紧急]'
         res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
               + " WHERE table_schema='db_alarm'"
               + " AND table_name='tb_devalarm_data_history'"
               + " AND column_name='alm_severity'");
         if(false == res.next()) {
            sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Devalarm_Data__History_Table
                  + " ADD COLUMN `alm_severity` int(11) NOT NULL DEFAULT '1' COMMENT '告警重要等级[0:重要;1:紧急]';");
         }
      } catch (SQLException e) {
         e.printStackTrace();
      } finally {
         if(null != res) {
            try {
               res.close();
            } catch (SQLException e) {
               e.printStackTrace();
            }
         }
         sql.close_con();
      }
   }
   /**
    *    创建tb_devalarm_data表
    * @param pool
    * @param recreate
    */
   public static void createDevalarm_Data_Table(MysqlConnPool pool, boolean recreate) {
      String sql_str01 = " DROP TABLE IF EXISTS "+Sql_Mysql.Devalarm_Data_Table;
      String sql_str02 = " CREATE TABLE IF NOT EXISTS " + Sql_Mysql.Devalarm_Data_Table + " (" +
            "  num bigint(20) NOT NULL AUTO_INCREMENT," +
            "  record_id bigint(20) NOT NULL DEFAULT '0'," +
            "  dev_id int(11) NOT NULL DEFAULT '0'," +
            "  dev_ip varchar(24) NOT NULL DEFAULT '127.0.0.1'," +
            "  alm_type int(11) NOT NULL DEFAULT '0'," +
            "  alm_level int(11) NOT NULL DEFAULT '0'," +
            "  alm_start_time datetime NOT NULL DEFAULT '1982-01-01 00:00:00'," +
            "  alm_end_time datetime NOT NULL DEFAULT '1982-01-01 00:00:00'," +
            "  alm_value float NOT NULL DEFAULT '0'," +
            "  alm_is_confirmed tinyint(1) NOT NULL DEFAULT '0'," +
            "  alm_confirmed_time datetime NOT NULL DEFAULT '1982-01-01 00:00:00'," +
            "  alm_cleared_type int(11) NOT NULL DEFAULT '0'," +
            "  usr_Id int(11) NOT NULL DEFAULT '0'," +
            "  `alm_severity` int(11) NOT NULL DEFAULT '1' COMMENT '告警重要等级[0:重要;1:紧急]'," +
            "  PRIMARY KEY (num)," +
            "  KEY index_record_id (record_id)," +
            "  KEY index_dev_id (dev_id)," +
            "  KEY index_alm_type (alm_type)," +
            "  KEY index_alm_start_time (alm_start_time)," +
            "  KEY index_alm_cleared_type (alm_cleared_type)" +
            ") ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8;";
      Sql_Mysql sql = new Sql_Mysql(pool.getConn());
      ResultSet res = null;
      try {
         if(true == recreate) {
            sql.sqlMysqlExecute(sql_str01);
         }
         sql.sqlMysqlExecute(sql_str02);
         //添加指定的alm_value列'告警值'
         res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
               + " WHERE table_schema='db_alarm'"
               + " AND table_name='tb_devalarm_data'"
               + " AND column_name='alm_value'");
         if(false == res.next()) {
            sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Devalarm_Data_Table
                        + " ADD COLUMN alm_value float NOT NULL DEFAULT '0';");
         }
         res = null;
         //添加指定的work_flag列(派单管理)
         res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
               + " WHERE table_schema='db_alarm'"
               + " AND table_name='tb_devalarm_data'"
               + " AND column_name='work_flag'");
         if(false == res.next()) {
            sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Devalarm_Data_Table
                        + " ADD COLUMN `work_flag` tinyint NOT NULL DEFAULT 0;");
         }
         res = null;
         //添加指定的msg_flag列(派单管理)
         res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
               + " WHERE table_schema='db_alarm'"
               + " AND table_name='tb_devalarm_data'"
               + " AND column_name='msg_flag'");
         if(false == res.next()) {
            sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Devalarm_Data_Table
                        + " ADD COLUMN `msg_flag` tinyint NOT NULL DEFAULT 0;");
         }
         res = null;
         //添加指定的alm_severity列(告警重要等级[0:重要;1:紧急])
         res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
               + " WHERE table_schema='db_alarm'"
               + " AND table_name='tb_devalarm_data'"
               + " AND column_name='alm_severity'");
         if(false == res.next()) {
            sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Devalarm_Data_Table
                  + " ADD COLUMN `alm_severity` int(11) NOT NULL DEFAULT '1' COMMENT '告警重要等级[0:重要;1:紧急]';");
         }
      } catch (SQLException e) {
         e.printStackTrace();
      } finally {
         sql.close_con();
      }
   }
   /**
    *    创建tb_bts_station_event表
    * @param pool
    * @param recreate
    */
   public static void createBts_Station_Event_Table(MysqlConnPool pool, boolean recreate) {
      String sql_str01 = " DROP TABLE IF EXISTS "+Sql_Mysql.Bts_Station_Event_Table;
      String sql_str02 = " CREATE TABLE IF NOT EXISTS " + Sql_Mysql.Bts_Station_Event_Table + " (" +
            "  num bigint(20) NOT NULL AUTO_INCREMENT," +
            "  station_id bigint(20) NOT NULL DEFAULT '0'," +
            "  dev_id bigint(20) NOT NULL DEFAULT '0'," +
            "  record_datetime varchar(20) NOT NULL DEFAULT '2015-12-02 12:10:00'," +
            "  station_event_type int(11) NOT NULL DEFAULT '0'," +
            "  station_event_trig int(11) NOT NULL DEFAULT '0'," +
            "  PRIMARY KEY (num)," +
            "  KEY index_dev_id (dev_id)," +
            "  KEY index_station_id (station_id)" +
            ") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
      Sql_Mysql sql = new Sql_Mysql(pool.getConn());
      try {
         if(true == recreate) {
            sql.sqlMysqlExecute(sql_str01);
         }
         sql.sqlMysqlExecute(sql_str02);
      } catch (SQLException e) {
         e.printStackTrace();
      } finally {
         sql.close_con();
      }
   }
   /**
    *    创建tb_battalarm_data_history表
    * @param pool
    * @param recreate
    */
   public static void createBattalarm_Data_History_Table(MysqlConnPool pool, boolean recreate) {
      String sql_str01 = " DROP TABLE IF EXISTS "+Sql_Mysql.Battalarm_Data_History_Table;
      String sql_str02 = " CREATE TABLE IF NOT EXISTS " + Sql_Mysql.Battalarm_Data_History_Table + " (" +
            "  num bigint(20) NOT NULL AUTO_INCREMENT," +
            "  BattGroupId int(11) NOT NULL DEFAULT '0'," +
            "  MonNum int(11) NOT NULL DEFAULT '0'," +
            "  Record_Id bigint(20) NOT NULL DEFAULT '0'," +
            "  alm_id int(11) NOT NULL DEFAULT '0'," +
            "  alm_signal_id int(11) NOT NULL DEFAULT '0'," +
            "  alm_level int(11) NOT NULL DEFAULT '0'," +
            "  alm_start_time datetime NOT NULL DEFAULT '1982-01-01 00:00:00'," +
            "  alm_end_time datetime NOT NULL DEFAULT '1982-01-01 00:00:00'," +
            "  alm_value float NOT NULL DEFAULT '0'," +
            "  alm_is_confirmed tinyint(1) NOT NULL DEFAULT '0'," +
            "  alm_confirmed_time datetime NOT NULL DEFAULT '1982-01-01 00:00:00'," +
            "  alm_cleared_type int(11) NOT NULL DEFAULT '0'," +
            "  usr_Id int(11) NOT NULL DEFAULT '0'," +
            "  `alm_severity` int(11) NOT NULL DEFAULT '0' COMMENT '告警重要等级[0:重要;1:紧急]'," +
            "  PRIMARY KEY (num)," +
            "  KEY index_battgroup_id (BattGroupId)," +
            "  KEY index_Record_Id (Record_Id)," +
            "  KEY index_alm_id (alm_id)," +
            "  KEY index_alm_start_time (alm_start_time)," +
            "  KEY index_alm_cleared_type (alm_cleared_type)," +
            "  KEY index_alm_signal_id (alm_signal_id)," +
            "  KEY index_alm_level (alm_level)" +
            ") ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;";
      Sql_Mysql sql = new Sql_Mysql(pool.getConn());
      ResultSet res = null;
      try {
         if(true == recreate) {
            sql.sqlMysqlExecute(sql_str01);
         }
         sql.sqlMysqlExecute(sql_str02);
         //添加指定的alm_severity列(告警重要等级)
         res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
               + " WHERE table_schema='db_alarm'"
               + " AND table_name='tb_battalarm_data_history'"
               + " AND column_name='alm_severity'");
         if(false == res.next()) {
            sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Battalarm_Data_History_Table
                        + " ADD COLUMN `alm_severity` int(11) NOT NULL DEFAULT '0' COMMENT '告警重要等级[0:重要;1:紧急]';");
         }
      } catch (SQLException e) {
         e.printStackTrace();
      } finally {
         if(null != res) {
            try {
               res.close();
            } catch (SQLException e) {
               e.printStackTrace();
            }
         }
         sql.close_con();
      }
   }
   /**
    *    创建tb_battalarm_data表
    * @param pool
    * @param recreate
    */
   public static void createBattalarm_Data_Table(MysqlConnPool pool, boolean recreate) {
      String sql_str01 = " DROP TABLE IF EXISTS "+Sql_Mysql.Battalarm_Data_Table;
      String sql_str02 = " CREATE TABLE IF NOT EXISTS " + Sql_Mysql.Battalarm_Data_Table + " (" +
            "  num bigint(20) NOT NULL AUTO_INCREMENT," +
            "  BattGroupId int(11) NOT NULL DEFAULT '0'," +
            "  MonNum int(11) NOT NULL DEFAULT '0'," +
            "  Record_Id bigint(20) NOT NULL DEFAULT '0'," +
            "  alm_id int(11) NOT NULL DEFAULT '0'," +
            "  alm_signal_id int(11) NOT NULL DEFAULT '0'," +
            "  alm_level int(11) NOT NULL DEFAULT '0'," +
            "  alm_start_time datetime NOT NULL DEFAULT '1982-01-01 00:00:00'," +
            "  alm_end_time datetime NOT NULL DEFAULT '1982-01-01 00:00:00'," +
            "  alm_value float NOT NULL DEFAULT '0'," +
            "  alm_is_confirmed tinyint(1) NOT NULL DEFAULT '0'," +
            "  alm_confirmed_time datetime NOT NULL DEFAULT '1982-01-01 00:00:00'," +
            "  alm_cleared_type int(11) NOT NULL DEFAULT '0'," +
            "  usr_Id int(11) NOT NULL DEFAULT '0'," +
            "  `work_flag` tinyint(4) NOT NULL DEFAULT '0'," +
            "  `msg_flag` tinyint(4) NOT NULL DEFAULT '0'," +
            "  `alm_severity` int(11) NOT NULL DEFAULT '0' COMMENT '告警重要等级[0:重要;1:紧急]'," +
            "  PRIMARY KEY (num)," +
            "  KEY index_battgroup_id (BattGroupId)," +
            "  KEY index_Record_Id (Record_Id)," +
            "  KEY index_alm_id (alm_id)," +
            "  KEY index_alm_start_time (alm_start_time)," +
            "  KEY index_alm_cleared_type (alm_cleared_type)," +
            "  KEY index_alm_signal_id (alm_signal_id)," +
            "  KEY index_alm_level (alm_level)" +
            ") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;";
      Sql_Mysql sql = new Sql_Mysql(pool.getConn());
      ResultSet res = null;
      try {
         if(true == recreate) {
            sql.sqlMysqlExecute(sql_str01);
         }
         sql.sqlMysqlExecute(sql_str02);
         //添加指定的work_flag列(派单管理)
         res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
               + " WHERE table_schema='db_alarm'"
               + " AND table_name='tb_battalarm_data'"
               + " AND column_name='work_flag'");
         if(false == res.next()) {
            sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Battalarm_Data_Table
                        + " ADD COLUMN `work_flag` tinyint NOT NULL DEFAULT 0;");
         }
         res = null;
         //添加指定的msg_flag列(派单管理)
         res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
               + " WHERE table_schema='db_alarm'"
               + " AND table_name='tb_battalarm_data'"
               + " AND column_name='msg_flag'");
         if(false == res.next()) {
            sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Battalarm_Data_Table
                        + " ADD COLUMN `msg_flag` tinyint NOT NULL DEFAULT 0;");
         }
         res = null;
         //添加指定的alm_severity 列(告警重要等级)
         res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
               + " WHERE table_schema='db_alarm'"
               + " AND table_name='tb_battalarm_data'"
               + " AND column_name='alm_severity'");
         if(false == res.next()) {
            sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Battalarm_Data_Table
                  + " ADD COLUMN `alm_severity` int(11) NOT NULL DEFAULT '0' COMMENT '告警重要等级[0:重要;1:紧急]';");
         }
      } catch (SQLException e) {
         e.printStackTrace();
      } finally {
         if(null != res) {
            try {
               res.close();
            } catch (SQLException e) {
               e.printStackTrace();
            }
         }
         sql.close_con();
      }
   }
   /**
    *
    * @Title: createTb_Alarm_Message
    * @Description: 告警短信
    * @param pool
    * @param recreate
    * @author author
    * @date 2021年9月6日
    */
   public static void createTb_Alarm_Message(MysqlConnPool pool, boolean recreate) {
      String sql_str01 = " DROP TABLE IF EXISTS "+Sql_Mysql.Tb_Alarm_Message;
      String sql_str02 = "CREATE TABLE  IF NOT EXISTS " + Sql_Mysql.Tb_Alarm_Message + " (" +
            "`id` bigint(19) NOT NULL AUTO_INCREMENT," +
                "  `phone` varchar(15) DEFAULT NULL COMMENT '短信发送至手机号'," +
                "  `full_device_name` varchar(255) DEFAULT NULL COMMENT '设备全名'," +
                "  `alarm_time_category` varchar(255) DEFAULT NULL COMMENT '告警时间段'," +
                "  `alarm_message` varchar(255) DEFAULT NULL COMMENT '告警信息'," +
                "  `alarm_ids` varchar(255) DEFAULT NULL COMMENT '短信对应的告警id'," +
                "  `create_time` datetime DEFAULT NULL," +
                "  `update_time` datetime DEFAULT NULL COMMENT '记录更新时间'," +
                "  `status` tinyint(1) DEFAULT NULL COMMENT '发送状态,0-未发送,1-发送完成,2-发送失败'," +
                "  PRIMARY KEY (`id`)" +
            ") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='告警短信';";
      Sql_Mysql sql = new Sql_Mysql(pool.getConn());
      try {
         if(true == recreate) {
            sql.sqlMysqlExecute(sql_str01);
         }
         sql.sqlMysqlExecute(sql_str02);
      } catch (SQLException e) {
         e.printStackTrace();
      } finally {
         sql.close_con();
      }
   }
   /**
    *
    * @Title: createTb_Alarm_Detail_Table
    * @Description: 告警详情
    * @param pool
    * @param recreate
    * @author author
    * @date 2021年9月6日
    */
   public static void createTb_Alarm_Detail_Table(MysqlConnPool pool, boolean recreate) {
      String sql_str01 = " DROP TABLE IF EXISTS "+Sql_Mysql.Tb_Alarm_Detail_Table;
      String sql_str02 = "CREATE TABLE  IF NOT EXISTS " + Sql_Mysql.Tb_Alarm_Detail_Table + " (" +
            "  `id` bigint(19) NOT NULL AUTO_INCREMENT," +
            "  `alm_id` varchar(15) DEFAULT NULL COMMENT '告警id'," +
            "  `alm_reason` text COMMENT '告警原因'," +
            "  `alm_advice` varchar(255) DEFAULT NULL COMMENT '告警建议'," +
            "  `alm_type` int(5) NOT NULL DEFAULT '0' COMMENT '告警来源:0电源,1设备,2电池'," +
            "  `highorlow` int(5) DEFAULT '0' COMMENT '高低告警0默认1低2高'," +
            "  PRIMARY KEY (`id`)" +
            ") ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COMMENT='告警详情';";
      Sql_Mysql sql = new Sql_Mysql(pool.getConn());
      ResultSet res = null;
      try {
         if(true == recreate) {
            sql.sqlMysqlExecute(sql_str01);
         }
         sql.sqlMysqlExecute(sql_str02);
         //添加指定的highorlow列
         res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
               + " WHERE table_schema='db_alarm'"
               + " AND table_name='tb_alarm_detail'"
               + " AND column_name='highorlow'");
         if(false == res.next()) {
            sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Tb_Alarm_Detail_Table
                        + " ADD COLUMN `highorlow` int(5) DEFAULT '0' COMMENT '高低告警0默认1低2高';");
         }
      } catch (SQLException e) {
         e.printStackTrace();
      } finally {
         if(null != res) {
            try {
               res.close();
            } catch (SQLException e) {
               e.printStackTrace();
            }
         }
         sql.close_con();
      }
   }
   /**
    *
    * @Title: createTb_Alarm_Relation_Table
    * @Description:告警对应关系
    * @param pool
    * @param recreate
    * @author author
    * @date 2021年9月6日
    */
   public static void createTb_Alarm_Relation_Table(MysqlConnPool pool, boolean recreate) {
      String sql_str01 = " DROP TABLE IF EXISTS "+Sql_Mysql.Tb_Alarm_Relation_Table;
      String sql_str02 = "CREATE TABLE  IF NOT EXISTS " + Sql_Mysql.Tb_Alarm_Relation_Table + " (" +
            "  `id` bigint(19) NOT NULL AUTO_INCREMENT," +
            "  `alm_id` varchar(255) DEFAULT NULL COMMENT '告警id'," +
            "  `alm_name` varchar(255) DEFAULT NULL COMMENT '告警名称'," +
            "  `alm_type` int(5) NOT NULL DEFAULT '0' COMMENT '告警来源:0电源,1设备,2电池'," +
            "  `alm_point` varchar(255) DEFAULT '' COMMENT '告警点'," +
            "  `highorlow` int(5) DEFAULT '0' COMMENT '高低告警0默认1低2高'," +
            "  PRIMARY KEY (`id`)" +
            ") ENGINE=InnoDB AUTO_INCREMENT=92 DEFAULT CHARSET=utf8mb4 COMMENT='告警对应关系';";
      Sql_Mysql sql = new Sql_Mysql(pool.getConn());
      ResultSet res = null;
      try {
         if(true == recreate) {
            sql.sqlMysqlExecute(sql_str01);
         }
         sql.sqlMysqlExecute(sql_str02);
         //添加指定的highorlow列
         res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
               + " WHERE table_schema='db_alarm'"
               + " AND table_name='tb_alarm_relation'"
               + " AND column_name='alm_ponit'");
         if(res.next()) {
            sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Tb_Alarm_Relation_Table
                        + " CHANGE  alm_ponit alm_point varchar(255) DEFAULT '' COMMENT '告警点';");
         }
      } catch (SQLException e) {
         e.printStackTrace();
      } finally {
         if(null != res) {
            try {
               res.close();
            } catch (SQLException e) {
               e.printStackTrace();
            }
         }
         sql.close_con();
      }
   }
   /**
    *
    * @Title: create tb_alarm_manual_clear
    * @Description:告警对应关系
    * @param pool
    * @param recreate
    * @author author
    * @date 2021年9月6日
    */
   public static void createTb_Alarm_Manual_Clear_Table(MysqlConnPool pool, boolean recreate) {
      String sql_str01 = " DROP TABLE IF EXISTS "+Sql_Mysql.Tb_Alarm_Manual_Clear_Table;
      String sql_str02 = "CREATE TABLE  IF NOT EXISTS " + Sql_Mysql.Tb_Alarm_Manual_Clear_Table + " (" +
            "  `id` int(11) NOT NULL AUTO_INCREMENT," +
            "  `station_id` varchar(10) DEFAULT NULL," +
            "  `station_name1` varchar(45) DEFAULT NULL," +
            "  `station_name2` varchar(45) DEFAULT NULL," +
            "  `station_name3` varchar(45) DEFAULT NULL," +
            "  `station_name4` varchar(45) DEFAULT NULL," +
            "  `station_name5` varchar(45) DEFAULT NULL," +
            "  `alarm_type` tinyint(4) DEFAULT NULL COMMENT '告警类型:1-电池,2-设备,3-电源'," +
            "  `batt_group_id` int(11) DEFAULT NULL," +
            "  `batt_group_name` varchar(45) DEFAULT NULL," +
            "  `mon_num` int(11) DEFAULT NULL," +
            "  `device_id` int(11) DEFAULT NULL," +
            "  `device_name` varchar(45) DEFAULT NULL," +
            "  `power_device_id` int(11) DEFAULT NULL," +
            "  `alm_start_time` datetime DEFAULT NULL," +
            "  `alm_id` int(11) DEFAULT NULL," +
            "  `alm_signal_id` int(11) DEFAULT NULL," +
            "  `reason` varchar(255) DEFAULT NULL COMMENT '恢复原因'," +
            "  `file_url` varchar(255) DEFAULT NULL COMMENT '上传的多个文件路径,用;拼接'," +
            "  `create_time` datetime DEFAULT NULL," +
            "  `deal_user_id` int(11) DEFAULT NULL," +
            "  `main_id` int(11) DEFAULT NULL COMMENT '审批流程主表id'," +
            "  PRIMARY KEY (`id`)" +
            ") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
      Sql_Mysql sql = new Sql_Mysql(pool.getConn());
      ResultSet res = null;
      try {
         if(true == recreate) {
            sql.sqlMysqlExecute(sql_str01);
         }
         sql.sqlMysqlExecute(sql_str02);
         //添加指定的 station_name4
         res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
               + " WHERE table_schema='db_alarm'"
               + " AND table_name='tb_alarm_manual_clear'"
               + " AND column_name='station_name4'");
         if(!res.next()) {
            sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Tb_Alarm_Manual_Clear_Table
                        + " ADD COLUMN `station_name4` varchar(45) DEFAULT NULL;");
         }
         res = null;
         //添加指定的 file_url
         res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
               + " WHERE table_schema='db_alarm'"
               + " AND table_name='tb_alarm_manual_clear'"
               + " AND column_name='file_url'");
         if(!res.next()) {
            sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Tb_Alarm_Manual_Clear_Table
                        + " ADD COLUMN `file_url` varchar(255) DEFAULT NULL COMMENT '上传的多个文件路径,用;拼接';");
         }
         res = null;
         //添加指定的 main_id
         res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
               + " WHERE table_schema='db_alarm'"
               + " AND table_name='tb_alarm_manual_clear'"
               + " AND column_name='main_id'");
         if(!res.next()) {
            sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Tb_Alarm_Manual_Clear_Table
                  + " ADD COLUMN `main_id` int(11) DEFAULT NULL COMMENT '审批流程主表id';");
         }
      } catch (SQLException e) {
         e.printStackTrace();
      } finally {
         if(null != res) {
            try {
               res.close();
            } catch (SQLException e) {
               e.printStackTrace();
            }
         }
         sql.close_con();
      }
   }
   /**
    *
    * @Title: create tb_battalarm_data_verify
    * @Description:电池告警确认处理表
    * @param pool
    * @param recreate
    * @author author
    * @date 2021年9月6日
    */
   public static void createTb_Battalarm_Data_Verify_Table(MysqlConnPool pool, boolean recreate) {
      String sql_str01 = " DROP TABLE IF EXISTS "+Sql_Mysql.Tb_Battalarm_Data_Verify_Table;
      String sql_str02 = "CREATE TABLE  IF NOT EXISTS " + Sql_Mysql.Tb_Battalarm_Data_Verify_Table + " (" +
            "  `num` bigint(20) NOT NULL AUTO_INCREMENT," +
            "  `BattGroupId` int(11) NOT NULL DEFAULT '0'," +
            "  `MonNum` int(11) NOT NULL DEFAULT '0'," +
            "  `Record_Id` bigint(20) NOT NULL DEFAULT '0'," +
            "  `alm_id` int(11) NOT NULL DEFAULT '0'," +
            "  `alm_signal_id` int(11) NOT NULL DEFAULT '0'," +
            "  `alm_level` int(11) NOT NULL DEFAULT '0'," +
            "  `alm_start_time` datetime NOT NULL DEFAULT '1982-01-01 00:00:00'," +
            "  `alm_end_time` datetime NOT NULL DEFAULT '1982-01-01 00:00:00'," +
            "  `alm_value` float NOT NULL DEFAULT '0'," +
            "  `alm_is_confirmed` tinyint(1) NOT NULL DEFAULT '0'," +
            "  `alm_confirmed_time` datetime NOT NULL DEFAULT '1982-01-01 00:00:00'," +
            "  `alm_cleared_type` int(11) NOT NULL DEFAULT '0'," +
            "  `usr_Id` int(11) NOT NULL DEFAULT '0'," +
            "  `work_flag` tinyint(4) NOT NULL DEFAULT '0'," +
            "  `msg_flag` tinyint(4) NOT NULL DEFAULT '0'," +
            "  `alm_severity` int(11) NOT NULL DEFAULT '0' COMMENT '告警重要等级[0:重要;1:紧急]'," +
            "  `alm_solekey` int(11) NOT NULL DEFAULT '1' COMMENT '实时告警主键'," +
            "  PRIMARY KEY (`num`)," +
            "  KEY `index_battgroup_id` (`BattGroupId`)," +
            "  KEY `index_Record_Id` (`Record_Id`)," +
            "  KEY `index_alm_id` (`alm_id`)," +
            "  KEY `index_alm_start_time` (`alm_start_time`)," +
            "  KEY `index_alm_cleared_type` (`alm_cleared_type`)," +
            "  KEY `index_alm_signal_id` (`alm_signal_id`)," +
            "  KEY `index_alm_level` (`alm_level`)" +
            ") ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='电池告警确认处理表';";
      Sql_Mysql sql = new Sql_Mysql(pool.getConn());
      ResultSet res = null;
      try {
         if(true == recreate) {
            sql.sqlMysqlExecute(sql_str01);
         }
         sql.sqlMysqlExecute(sql_str02);
         //添加指定的highorlow列
         res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
               + " WHERE table_schema='db_alarm'"
               + " AND table_name='tb_battalarm_data_verify'"
               + " AND column_name='alm_solekey'");
         if(!res.next()) {
            sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Tb_Battalarm_Data_Verify_Table
                        + "  ADD COLUMN `alm_solekey` int(11) NOT NULL DEFAULT '1' COMMENT '实时告警主键';");
         }
      } catch (SQLException e) {
         e.printStackTrace();
      } finally {
         if(null != res) {
            try {
               res.close();
            } catch (SQLException e) {
               e.printStackTrace();
            }
         }
         sql.close_con();
      }
   }
   /**
    *
    * @Title: create tb_devalarm_data_verify
    * @Description:设备告警确认处理表
    * @param pool
    * @param recreate
    * @author author
    * @date 2021年9月6日
    */
   public static void createTb_Devalarm_Data_Verify_Table(MysqlConnPool pool, boolean recreate) {
      String sql_str01 = " DROP TABLE IF EXISTS "+Sql_Mysql.Tb_Devalarm_Data_Verify_Table;
      String sql_str02 = "CREATE TABLE  IF NOT EXISTS " + Sql_Mysql.Tb_Devalarm_Data_Verify_Table + " (" +
            "  `num` bigint(20) NOT NULL AUTO_INCREMENT," +
            "  `record_id` bigint(20) NOT NULL DEFAULT '0'," +
            "  `dev_id` int(11) NOT NULL DEFAULT '0'," +
            "  `dev_ip` varchar(24) NOT NULL DEFAULT '127.0.0.1'," +
            "  `alm_type` int(11) NOT NULL DEFAULT '0'," +
            "  `alm_level` int(11) NOT NULL DEFAULT '0'," +
            "  `alm_start_time` datetime NOT NULL DEFAULT '1982-01-01 00:00:00'," +
            "  `alm_end_time` datetime NOT NULL DEFAULT '1982-01-01 00:00:00'," +
            "  `alm_value` float NOT NULL DEFAULT '0'," +
            "  `alm_is_confirmed` tinyint(1) NOT NULL DEFAULT '0'," +
            "  `alm_confirmed_time` datetime NOT NULL DEFAULT '1982-01-01 00:00:00'," +
            "  `alm_cleared_type` int(11) NOT NULL DEFAULT '0'," +
            "  `usr_Id` int(11) NOT NULL DEFAULT '0'," +
            "  `work_flag` tinyint(4) NOT NULL DEFAULT '0'," +
            "  `msg_flag` tinyint(4) NOT NULL DEFAULT '0'," +
            "  `alm_severity` int(11) NOT NULL DEFAULT '1' COMMENT '告警重要等级[0:重要;1:紧急]'," +
            "  `alm_solekey` int(11) NOT NULL DEFAULT '1' COMMENT '实时告警主键'," +
            "  PRIMARY KEY (`num`)," +
            "  KEY `index_record_id` (`record_id`)," +
            "  KEY `index_dev_id` (`dev_id`)," +
            "  KEY `index_alm_type` (`alm_type`)," +
            "  KEY `index_alm_start_time` (`alm_start_time`)," +
            "  KEY `index_alm_cleared_type` (`alm_cleared_type`)" +
            ") ENGINE=InnoDB AUTO_INCREMENT=16726 DEFAULT CHARSET=utf8 COMMENT='设备告警确认处理表';";
      Sql_Mysql sql = new Sql_Mysql(pool.getConn());
      ResultSet res = null;
      try {
         if(true == recreate) {
            sql.sqlMysqlExecute(sql_str01);
         }
         sql.sqlMysqlExecute(sql_str02);
         //添加指定的highorlow列
         res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
               + " WHERE table_schema='db_alarm'"
               + " AND table_name='tb_devalarm_data_verify'"
               + " AND column_name='alm_solekey'");
         if(!res.next()) {
            sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Tb_Devalarm_Data_Verify_Table
                        + "  ADD COLUMN `alm_solekey` int(11) NOT NULL DEFAULT '1' COMMENT '实时告警主键';");
         }
      } catch (SQLException e) {
         e.printStackTrace();
      } finally {
         if(null != res) {
            try {
               res.close();
            } catch (SQLException e) {
               e.printStackTrace();
            }
         }
         sql.close_con();
      }
   }
}
package com.database_util;
import java.sql.SQLException;
import com.sql.MysqlConnPool;
import com.sql.Sql_Mysql;
public class DB_Alarm {
   public static void init(MysqlConnPool pool, boolean recreate) {
      //创建数据库
      createDB_Alarm(pool);
      createAlarm_Param_Table(pool,recreate);
      createDevalarm_Data_Table(pool, recreate);
   }
   /**
    *   创建 tb_devalarm_data
    * @param pool
    * @param recreate
    */
   public static void createDevalarm_Data_Table(MysqlConnPool pool, boolean recreate) {
      String sql_str01 = " DROP TABLE IF EXISTS "+Sql_Mysql.Devalarm_Data_Table;
      String sql_str02 = "CREATE TABLE IF NOT EXISTS " + Sql_Mysql.Devalarm_Data_Table + " (" +
            "  `num` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增主键'," +
            "  `dev_id` int(11) NOT NULL DEFAULT '0' COMMENT '设备ID'," +
            "  `alm_id` int(11) NOT NULL DEFAULT '0' COMMENT '告警ID'," +
            "  `alm_signal_id` int(11) NOT NULL DEFAULT '1' COMMENT '告警类型【1-上限告警  2-下限告警】'," +
            "  `alm_starttime` datetime NOT NULL DEFAULT '2000-01-01 00:00:00' COMMENT '告警开始时间'," +
            "  `alm_value` float NOT NULL DEFAULT '0' COMMENT '告警值'," +
            "  `alm_is_confirmed` tinyint(4) NOT NULL DEFAULT '0' COMMENT '告警是否确认'," +
            "  `confirmed_uid` int(11) NOT NULL DEFAULT '0' COMMENT '确认用户ID'," +
            "  `confirmed_time` datetime NOT NULL DEFAULT '2000-01-01 00:00:00' COMMENT '确认时间'," +
            "  `alm_endtime` datetime NOT NULL DEFAULT '2000-01-01 00:00:00' COMMENT '告警结束时间'," +
            "  `alm_cleared_type` int(11) NOT NULL DEFAULT '0' COMMENT '告警记录状态[0-实时告警  1-历史告警  2-取消告警]'," +
            "  PRIMARY KEY (`num`)," +
            "  KEY `idx_dev_id` (`dev_id`) USING BTREE," +
            "  KEY `idx_alm_id` (`alm_id`) USING BTREE" +
            ") ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='设备实时告警记录表';";
      Sql_Mysql sql = new Sql_Mysql(pool.getConn());
      try {
         if(true == recreate) {
            sql.sqlMysqlExecute(sql_str01);
         }
         sql.sqlMysqlExecute(sql_str02);
      } catch (SQLException e) {
         e.printStackTrace();
      } finally {
         sql.close_con();
      }
   }
   /**
    *   创建 tb_alarm_param
    * @param pool
    * @param recreate
    */
   public static void createAlarm_Param_Table(MysqlConnPool pool, boolean recreate) {
      String sql_str01 = " DROP TABLE IF EXISTS "+Sql_Mysql.Alarm_Param_Table;
      String sql_str02 = "CREATE TABLE IF NOT EXISTS " + Sql_Mysql.Alarm_Param_Table + " (" +
            "  `num` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增主键'," +
            "  `dev_id` int(11) NOT NULL DEFAULT '0' COMMENT '设备ID'," +
            "  `alm_id` int(11) NOT NULL DEFAULT '0' COMMENT '告警id'," +
            "  `alm_name` varchar(255) NOT NULL DEFAULT '' COMMENT '告警名称'," +
            "  `alm_high_level` int(11) NOT NULL DEFAULT '4' COMMENT '上限告警等级'," +
            "  `alm_high_coe` float NOT NULL DEFAULT '0' COMMENT '上限告警阈值'," +
            "  `alm_high_en` tinyint(4) NOT NULL DEFAULT '0' COMMENT '上限告警使能'," +
            "  `alm_low_level` int(11) NOT NULL DEFAULT '4' COMMENT '下限告警等级'," +
            "  `alm_low_coe` float NOT NULL DEFAULT '0' COMMENT '下限告警使能'," +
            "  `alm_low_en` tinyint(4) NOT NULL DEFAULT '0' COMMENT '下限告警使能'," +
            "  PRIMARY KEY (`num`)," +
            "  KEY `idx_alm_id` (`alm_id`) USING BTREE," +
            "  KEY `idx_dev_id` (`dev_id`) USING BTREE" +
            ") ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='设备告警参数表';";
      Sql_Mysql sql = new Sql_Mysql(pool.getConn());
      try {
         if(true == recreate) {
            sql.sqlMysqlExecute(sql_str01);
         }
         sql.sqlMysqlExecute(sql_str02);
      } catch (SQLException e) {
         e.printStackTrace();
      } finally {
         sql.close_con();
      }
   }
   public static void createDB_Alarm(MysqlConnPool pool) {
      Sql_Mysql sql = new Sql_Mysql(pool.getConn());
      try {
         sql.sqlMysqlExecute("CREATE DATABASE IF NOT EXISTS " + Sql_Mysql.DB_ALARM);
      } catch (SQLException e) {
         e.printStackTrace();
      } finally {
         sql.close_con();
      }
   }
}