DELL
2025-02-11 cec8535c3bd288d555f0252e240fc19998febb84
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package com.dev.lock.alarm;
 
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
 
import com.base.Com;
import com.dev.lock.alarm.data.ElectLock_AlarmData;
import com.dev.lock.data.ElectLock_Inf;
import com.dev.lock.data.ElectLock_State;
import com.sql.MysqlConnPool;
import com.sql.Sql_Mysql;
 
public class ElectLock_Alarm_Thread_SQL {
 
    public static void updateProcess_StartTime(MysqlConnPool pool, double mVersionnum) {
        String sql_str = "UPDATE " + Sql_Mysql.Process_Survey_Table + 
                " Set Process_starttime = NOW()  " +
                " ,ProcessTime = NOW() " + 
                " ,ProcessVersion = 'V" + mVersionnum + "'"+
                " WHERE ProcessId = 11002";
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        try {
            sql.sqlMysqlExecute(sql_str);
        } catch (SQLException e) {
            sql.logger.error(e.toString(),e);
        } finally {
            sql.close_con();
        }
    }
    
    public static void updateProcess_ProcessTime(MysqlConnPool pool) {
        String sql_str = "UPDATE " + Sql_Mysql.Process_Survey_Table + 
                " Set ProcessTime = NOW() " + 
                " WHERE ProcessId = 11002";
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        try {
            sql.sqlMysqlExecute(sql_str);
        } catch (SQLException e) {
            sql.logger.error(e.toString(),e);
        } finally {
            sql.close_con();
        }
    }
 
    /**
     * 查询当前锁具的实时状态
     * @param pool
     * @param inf
     */
    public static void queryElectLockState(MysqlConnPool pool, ElectLock_Inf inf) {
        String sql_str_sql = "SELECT record_time,lock_state,now() as now_time FROM " + Sql_Mysql.Lock_Rt_Table + " WHERE lock_id = " + inf.getLock_id();
        ResultSet res = null;
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        res = sql.sqlMysqlQuery(sql_str_sql);
        try {
            ElectLock_State state = inf.lockState;
            if(res.next()) {
                
                
                state.setRecord_time(res.getTimestamp("record_time"));        //更新时间lock_state
                state.setLock_state(res.getInt("lock_state"));                //'电子锁状态[0-关 1-开]',
                state.setNow_time(res.getTimestamp("now_time"));            //当前时间    
            }else {
                state.setNow_time(new Date());            //当前时间    
            }
        } catch (SQLException e) {
            sql.logger.error(e.toString(),e);
        } finally {
            if(null != res) {
                try {
                    res.close();
                } catch (SQLException e) {
                    sql.logger.error(e.toString(),e);
                }
            }
            sql.close_con();
        }
    }
 
    /**
     * 查询当前设备是否存在当前类型的告警记录
     * @param pool
     * @param lock_id
     * @param almId
     * @return
     */
    public static ElectLock_AlarmData queryElectLockAlarmByAlmId(MysqlConnPool pool,int lock_id, int almId) {
        ElectLock_AlarmData almData = null;
        String sql_str = " SELECT lock_id,alm_id,alm_source,alm_start_time,alm_is_confirmed,alm_confirmed_time,alm_end_time"
                        + " FROM " + Sql_Mysql.Lock_Alarm_Table 
                        + " WHERE lock_id = " + lock_id + " AND alm_id = " + almId;
        ResultSet res = null;
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        try {
            //System.out.println(sql_str);
            res = sql.sqlMysqlQuery(sql_str);
            if(res.next()) {
                almData = new ElectLock_AlarmData();
                
                almData.setLock_id(res.getInt("lock_id"));
                almData.setAlm_id(res.getInt("alm_id"));
                almData.setAlm_source(res.getInt("alm_source"));                        //'告警来源[1-平台触发  2-手机APP触发  3-锁具触发]',
                almData.setAlm_start_time(res.getTimestamp("alm_start_time"));            //'告警开始时间',
                almData.setAlm_is_confirmed(res.getInt("alm_is_confirmed"));             //'告警是否确认',
                almData.setAlm_confirmed_time(res.getTimestamp("alm_confirmed_time"));    //'告警确认时间',
                almData.setAlm_end_time(res.getTimestamp("alm_end_time"));              //'告警结束时间',
                
            }
        } catch (SQLException e) {
            sql.logger.error(e.toString(),e);
        } finally {
            if(null != res) {
                try {
                    res.close();
                } catch (SQLException e) {
                    sql.logger.error(e.toString(),e);
                }
            }
            sql.close_con();
        }
        return almData;
    }
    
    /**
     * 插入实时告警
     * @param pool
     * @param lock_id
     * @param almId
     * @param alm_source
     */
    public static void insertElectLockAlarm(MysqlConnPool pool,int lock_id, int almId,int almSource) {
        String sql_str = "INSERT INTO " + Sql_Mysql.Lock_Alarm_Table 
                       + "(lock_id,alm_id,alm_source,alm_start_time,alm_end_time) "
                       + " VALUES(" + lock_id + "," + almId + "," + almSource + ",Now(),Now());";
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        try {
            sql.sqlMysqlExecute(sql_str);
        } catch (SQLException e) {
            sql.logger.error(e.toString(),e);
        } finally {
            sql.close_con();
        }
    }
 
    /**
     * 
     * @param pool
     * @param almData
     */
    public static void changeRealAlmToHisAlm(MysqlConnPool pool, ElectLock_AlarmData almData) {
        //创建当前告警开始年份的历史告警表
        String sql_str1 = "CREATE TABLE IF NOT EXISTS " + Sql_Mysql.Lock_Alarm_His_Table + Com.getDateTimeFormat(almData.getAlm_start_time(), Com.DTF_Y) + " (" + 
                "  `num` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增主键'," + 
                "  `lock_id` int(11) NOT NULL DEFAULT '1' COMMENT '锁具ID'," + 
                "  `alm_id` int(11) NOT NULL DEFAULT '119001' COMMENT '告警类型'," + 
                "  `alm_source` int(11) NOT NULL DEFAULT '1' COMMENT '告警来源[1-平台触发  2-手机APP触发  3-锁具触发]'," + 
                "  `alm_start_time` datetime NOT NULL DEFAULT '2000-01-01 00:00:00' COMMENT '告警开始时间'," + 
                "  `alm_is_confirmed` tinyint(4) NOT NULL DEFAULT '0' COMMENT '告警是否确认'," + 
                "  `alm_confirmed_time` datetime NOT NULL DEFAULT '2000-01-01 00:00:00' COMMENT '告警确认时间'," + 
                "  `alm_end_time` datetime NOT NULL DEFAULT '2000-01-01 00:00:00' COMMENT '告警结束时间'," + 
                "  PRIMARY KEY (`num`)" + 
                ") ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='" + Com.getDateTimeFormat(almData.getAlm_start_time(), Com.DTF_Y) + "年电子锁具历史告警表';";
        //2将当前的实时告警记录插入到历史告警表中  
        String sql_str2 = " INSERT INTO " + Sql_Mysql.Lock_Alarm_His_Table + Com.getDateTimeFormat(almData.getAlm_start_time(), Com.DTF_Y)
                        + "(lock_id,alm_id,alm_source,alm_start_time,alm_is_confirmed,alm_confirmed_time,alm_end_time) "
                        + "(SELECT lock_id,alm_id,alm_source,alm_start_time,alm_is_confirmed,alm_confirmed_time,Now() from " + Sql_Mysql.Lock_Alarm_Table 
                            + " WHERE lock_id = " + almData.getLock_id() + " AND alm_id = " + almData.getAlm_id() + " "
                        + ")";    
        //3将实时告警表中的告警记录清空
        String sql_str3 = " DELETE FROM "  + Sql_Mysql.Lock_Alarm_Table 
                        + " WHERE "
                        + " lock_id =" + almData.getLock_id()
                        + " AND alm_id =" + almData.getAlm_id();
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        try {
            ArrayList<String> sql_strs = new ArrayList<String>();
            
            sql_strs.add(sql_str1);
            sql_strs.add(sql_str2);
            sql_strs.add(sql_str3);
            sql.makeManualCommit(sql_strs);
        } catch (Exception e) {
            sql.logger.error(e.toString(), e);
        } finally {
            sql.close_con();
        }
    }
 
    /**
     * 将临时表中的告警记录插入实时告警表中
     * @param pool
     */
    public static void changeTempAlmToRealAlm(MysqlConnPool pool) {
        //1.查询是否存在临时告警记录
        String sql_str_sel = " SELECT COUNT(*) as alm_count FROM " + Sql_Mysql.Lock_Alm_Temp_Table;
        //2将当前的实时告警记录插入到历史告警表中  
        String sql_str2 = " INSERT INTO " + Sql_Mysql.Lock_Alarm_Table 
                        + " (lock_id,alm_id,alm_source,alm_start_time,alm_end_time) "
                        + " (SELECT lock_id,alm_id,alm_source,alm_start_time,alm_end_time from " + Sql_Mysql.Lock_Alm_Temp_Table + ")";    
        //3将实时告警表中的告警记录清空
        String sql_str3 = " DELETE FROM "  + Sql_Mysql.Lock_Alm_Temp_Table;
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        ResultSet res = null;
        try {
            res = sql.sqlMysqlQuery(sql_str_sel);    
            if(res.next()) {
                int alm_count = res.getInt("alm_count");
                //临时表中存在实时告警时,移动到实时告警表中
                if(alm_count > 0) {
                    ArrayList<String> sql_strs = new ArrayList<String>();
                    
                    sql_strs.add(sql_str2);
                    sql_strs.add(sql_str3);
                    
                    sql.makeManualCommit(sql_strs);
                }
            }
        } catch (Exception e) {
            sql.logger.error(e.toString(), e);
        } finally {
            if(null != res) {
                try {
                    res.close();
                } catch (SQLException e) {
                    sql.logger.error(e.toString(), e);
                }
            }
            sql.close_con();
        }
    }
 
    public static void delLockAlarmData(MysqlConnPool pool, Date deldate) {
        String sql_str_sel = " select TABLE_NAME,CREATE_TIME " + 
                 " FROM information_schema.tables " + 
                 " where table_schema='db_lock_alarm' " + 
                 " AND TABLE_NAME like 'tb_lock_alarm_%' " + 
                 " AND CREATE_TIME <= '"+Com.getDateTimeFormat(deldate, Com.DTF_YMDhms)+"';" ;
        
        ResultSet res = null;
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        res = sql.sqlMysqlQuery(sql_str_sel);
        int count = 0;
        StringBuffer sql_delete_strs = new StringBuffer(" DROP TABLE IF EXISTS ");
        try {
            while(res.next()) {
                if(count > 0) {
                    sql_delete_strs.append(",");
                }
                
                sql_delete_strs.append("db_lock_alarm." + res.getString("TABLE_NAME"));
                sql.logger.info("自动删除超过5年的告警表 : db_lock_alarm."+res.getString("TABLE_NAME")+"\t at "+Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms));
                count++;
            }
            if(count >0) {
                //System.out.println(sql_delete_strs.toString());
                sql.sqlMysqlExecute(sql_delete_strs.toString());
            }
        } catch (SQLException e) {
            sql.logger.error(e.toString(), e);
        } finally {
            if(null != res) {
                try {
                    res.close();
                } catch (SQLException e) {
                    sql.logger.error(e.toString(), e);
                }
            }
            sql.close_con();
        }
    }
}