DELL
2025-02-11 b885f89b291e583f274d7e250a947801095bf259
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package com.dev.lock.comm;
 
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import com.base.Com;
import com.dev.lock.data.ElectLock_AuthIdcard;
import com.dev.lock.data.ElectLock_ComBase;
import com.dev.lock.data.ElectLock_Ctl_Log;
import com.dev.lock.data.ElectLock_Inf;
import com.dev.lock.data.ElectLock_Report;
import com.dev.lock.data.ElectLock_State;
import com.sql.MysqlConnPool;
import com.sql.Sql_Mysql;
 
public class Lock_Task_SQL {
 
    
    public static void insertLock_Rt_Table(MysqlConnPool pool,int lock_id,ElectLock_State state) {
        String sql_str_sel = "SELECT * FROM " + Sql_Mysql.Lock_Rt_Table + " WHERE lock_id = " + lock_id;
        String sql_str_ins = "INSERT INTO "  + Sql_Mysql.Lock_Rt_Table +  "(lock_id,client_ip) VALUES("+lock_id+",'" + state.getClient_ip() + "');";
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        ResultSet res = null;
        try {
            res = sql.sqlMysqlQuery(sql_str_sel);
            if(res.next()) {
                
                state.setLock_state(res.getInt("lock_state"));
                
                state.setComm_count(res.getInt("comm_count"));                //'通信计数',
                state.setErr_tol_count(res.getInt("err_tol_count"));        //'总错误计数',
                System.out.println("lock_state:" +state.getLock_state());
            }else {
                sql.sqlMysqlExecute(sql_str_ins);
            }
        } 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();
        }
    }
    
    
    public static void updateLock_Rt_Table(MysqlConnPool pool,int lock_id,ElectLock_State state) {
        String sql_str = "UPDATE " + Sql_Mysql.Lock_Rt_Table + 
                        " Set record_time = NOW() " + 
                        ",gprs_sn = '' " + 
                        ",client_ip = '" +  state.getClient_ip() + "'" +
                        ",already_id_count = " +  state.getAlready_id_count() +
                        ",max_id_count = " +  state.getMax_id_count() +
                        ",lock_state = " +  state.getLock_state() +
                        ",lock_version = '" +  state.getLock_version() + "'" +
                        ",unlock_type = " +  state.getUnlock_type() +
                        ",unlock_id = " +  state.getUnlock_id() + 
                        ",lock_addr = " +  state.getLock_addr() +
                        ",comm_count = " +  state.getComm_count() +
                        ",err_tol_count = " +  state.getErr_tol_count() +
                        ",err_count = " + state.getErr_count() +
                        " WHERE lock_id = " + lock_id;
        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();
        }
    } 
    
    /**
     *     更新 tb_lock_inf 表中锁的状态
     * @param pool
     * @param lock_id
     * @param state
     */
    public static void updateLock_Inf_Table(MysqlConnPool pool,ElectLock_Inf inf,ElectLock_State state) {
        String sql_str = "UPDATE " + Sql_Mysql.Lock_Inf_Table + 
                        " Set lock_state = " +  state.getLock_state() +
                        " ,lock_online =  " + state.getLock_online() +
                        " ,lock_mac =  '" + state.getLockMacAddr() + "'" +
                        " ,lock_open_count =  " + state.getLock_open_count() + 
                        " ,last_update_time = '" + Com.getDateTimeFormat(state.getLast_update_time(), Com.DTF_YMDhms) + "'" +
                        " WHERE lock_id = " + inf.getLock_id();
        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 lock_id
     * @param param
     */
    public static void queryElectLockControlCmd(MysqlConnPool pool,int lock_id,ElectLock_State param) {
        String sql_str =  " SELECT op_cmd,id_card_set,lock_addr_set,ctl_uname "
                        + " FROM " + Sql_Mysql.Lock_Rt_Table + ""
                        + " WHERE lock_id = " + lock_id;
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        ResultSet res = null;
        try {
            res = sql.sqlMysqlQuery(sql_str);
            if(res.next()) {
                param.setOp_cmd(res.getInt("op_cmd"));
                param.setId_card_set(res.getInt("id_card_set"));
                param.setLock_addr_set(res.getInt("lock_addr_set"));
                param.setCtl_uname(res.getString("ctl_uname"));
                
            }
        } 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 state
     */
    public static void updateElectLockControlCmd(MysqlConnPool pool, int lock_id, ElectLock_State state) {
        String sql_str = "UPDATE " + Sql_Mysql.Lock_Rt_Table + 
                " Set op_cmd =  " + state.getOp_cmd() +
                " WHERE lock_id = " + lock_id;
        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 lock_id
     * @param state
     */
    public static void insertLock_Ctl_Log_Table(MysqlConnPool pool,ElectLock_Ctl_Log log) {
        String sql_str_ins = "INSERT INTO "  + Sql_Mysql.Lock_Ctl_Log_Table +  ""
                            + "    (lock_id,ctl_type,ctl_result,ctl_time,ctl_id_card,ctl_uname) VALUES("
                            + "" + log.lock_id
                            + "," + log.ctl_type + ""
                            + "," + log.ctl_result + ""
                            + ",'" + Com.getDateTimeFormat(log.ctl_time, Com.DTF_YMDhms) + "'"
                            + "," + log.ctl_id_card + ""
                            + ",'" + log.ctl_uname + "'"
                            + ");";
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        try {
                sql.sqlMysqlExecute(sql_str_ins);
        } catch (SQLException e) {
            sql.logger.error(e.toString(),e);
        } finally {
            sql.close_con();
        }
    }
 
 
    public static List<ElectLock_AuthIdcard> queryAuth_Idcard_Table(MysqlConnPool pool, int client_id) {
        List<ElectLock_AuthIdcard> batch_auth = new ArrayList<>();
        String sql_str = "SELECT DISTINCT(tb_key_inf.key_id),lock_id,state,tb_auth_idcard.create_time,key_number,ctl_uname " + 
                        " FROM " + Sql_Mysql.Key_Inf_Table + "," + Sql_Mysql.Auth_Idcard_Table +
                        " WHERE tb_key_inf.key_id = tb_auth_idcard.key_id AND lock_id = " + client_id + " AND key_type = 1 ";
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        ResultSet res = null;
        try {
            res = sql.sqlMysqlQuery(sql_str);
            while(res.next()) {
                ElectLock_AuthIdcard auth = new ElectLock_AuthIdcard();
                auth.key_id = res.getInt("key_id");                    //'钥匙id',
                auth.lock_id = res.getInt("lock_id");                //'锁具id',
                auth.state = res.getInt("state");                    //'1:移除id卡,2添加id卡,3已处理',
                auth.create_time = res.getTimestamp("create_time");    //'创建时间'
                auth.key_number = res.getString("key_number");        //ID卡号
 
                auth.ctl_uname = res.getString("ctl_uname");        //操作人姓名
                
                batch_auth.add(auth);
            }
        } 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 batch_auth;
    }
 
 
    /**
     *     将当前批量操作记录移动到历史表
     * @param pool
     * @param tmp_auth
     */
    public static void moveAuth_Idcard_Table(MysqlConnPool pool, ElectLock_AuthIdcard tmp_auth) {
        ArrayList<String> sql_strs = new ArrayList<>();
        String sql_str_ins = "INSERT INTO " + Sql_Mysql.Auth_Idcard_His_Table + "(key_id,lock_id,state,create_time,end_time,auth_result)" + 
                "(" + 
                "    SELECT key_id,lock_id,state,create_time,NOW()," + tmp_auth.auth_result +
                "    FROM " + Sql_Mysql.Auth_Idcard_Table +
                "    WHERE lock_id = " + tmp_auth.lock_id + " AND key_id = " + tmp_auth.key_id + 
                "    AND create_time = '" + Com.getDateTimeFormat(tmp_auth.create_time, Com.DTF_YMDhms) + "' " + 
                ");";
        String sql_str_del = "DELETE FROM " + Sql_Mysql.Auth_Idcard_Table + " WHERE lock_id = " + tmp_auth.lock_id + " AND key_id = " + tmp_auth.key_id + " AND create_time = '" + Com.getDateTimeFormat(tmp_auth.create_time, Com.DTF_YMDhms) + "'";
        System.out.println(sql_str_ins);
        sql_strs.add(sql_str_ins);
        sql_strs.add(sql_str_del);
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        try {
            sql.makeManualCommit(sql_strs);
        } catch (Exception e) {
            sql.logger.error(e.toString(),e);
        } finally {
            sql.close_con();
        }
    }
 
 
    /**
     *     查询ID钥匙的归属人姓名
     * @param pool
     * @param client_id
     * @param last_unlock_id
     * @return
     */
    public static String queryKeyInfByKeyNumberTable(MysqlConnPool pool, int keyNumber) {
        String uname = "";
        String sql_str =  " SELECT * FROM " + Sql_Mysql.Key_Inf_Table
                        + " WHERE key_number = " + keyNumber;
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        ResultSet res = null;
        try {
            //System.out.println("====="+sql_str);
            res = sql.sqlMysqlQuery(sql_str);
            if(res.next()) {
                uname = res.getString("uname");
                //System.out.println("uname:" + uname);
            }
        } 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 uname;
    }
 
 
    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 = 11001";
        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 = 11001";
        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 queryLock_Rt_TableByLockId(MysqlConnPool pool,int lock_id,ElectLock_State state) {
        String sql_str = " SELECT * FROM " + Sql_Mysql.Lock_Rt_Table + " WHERE lock_id = " + lock_id;
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        ResultSet res = null;
        try {
            res = sql.sqlMysqlQuery(sql_str);
            if(res.next()) {
                state.setGprs_sn(res.getString("gprs_sn"));                    //'gprs模块SN码',
                state.setClient_ip(res.getString("client_ip"));                //'设备IP地址',
                state.setAlready_id_count(res.getInt("already_id_count"));    //'当前锁已授权卡数量',
                state.setMax_id_count(res.getInt("max_id_count"));            //'授权卡数量存储上限',
                state.setLock_state(res.getInt("lock_state"));                //'电子锁状态[0-关 1-开]',
                state.setLock_version(res.getString("lock_version"));        //'软件版本号',
                state.setUnlock_type(res.getInt("unlock_type"));            //'上一次开锁方式[0-485开锁 1-刷卡开锁 2-蓝牙开锁 3-DI开锁]',
                state.setUnlock_id(res.getInt("unlock_id"));                //'上一次刷开开锁卡号[仅刷开开锁时有效]',
                state.setLock_addr(res.getInt("lock_addr"));                //'设备地址',
                state.setComm_count(res.getInt("comm_count"));                //'通信计数',
                state.setErr_tol_count(res.getInt("err_tol_count"));        //'总错误计数',
//                state.setErr_count(res.getInt("err_count"));                //'连续错误计数',
                state.setOp_cmd(res.getInt("op_cmd"));                        //'控制命令',
                state.setId_card_set(res.getInt("id_card_set"));            //'授权ID卡号',
                state.setLock_addr_set(res.getInt("lock_addr_set"));;        //'设置设备地址[慎用]',
                
            }
        } 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();
        }
        
    }
    
    public static void queryLock_Report_TableByLockId(MysqlConnPool pool,int lock_id,ElectLock_Report state,int now_year) {
        String sql_str = " SELECT * FROM " + Sql_Mysql.Lock_Report_Table + " WHERE lock_id = " + lock_id + " AND record_year = " + now_year;
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        ResultSet res = null;
        try {
            res = sql.sqlMysqlQuery(sql_str);
            if(res.next()) {
                
                state.record_year = res.getInt("record_year");        //统计年份',
                state.month1 = res.getInt("month1");            //1月开锁次数',
                state.month2 = res.getInt("month2");            //2月开锁次数',
                state.month3 = res.getInt("month3");            //3月开锁次数',
                state.month4 = res.getInt("month4");            //4月开锁次数',
                state.month5 = res.getInt("month5");            //5月开锁次数',
                state.month6 = res.getInt("month6");            //6月开锁次数',
                state.month7 = res.getInt("month7");            //7月开锁次数',
                state.month8 = res.getInt("month8");            //8月开锁次数',
                state.month9 = res.getInt("month9");            //9月开锁次数',
                state.month10 = res.getInt("month10");            //10月开锁次数',
                state.month11 = res.getInt("month11");            //11月开锁次数',
                state.month12 = res.getInt("month12");            //12月开锁次数',
                state.quarter1 = res.getInt("quarter1");        //第一季度开锁次数',
                state.quarter2 = res.getInt("quarter2");        //第二季度开锁次数',
                state.quarter3 = res.getInt("quarter3");        //第三季度开锁次数',
                state.quarter4 = res.getInt("quarter4");        //第四季度开锁次数',
                state.year_count = res.getInt("year_count");        //本年开锁次数',
            }
        } 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();
        }
        
    }
    
    /**
     * 插入远程开锁失败记录
     */
    public static void insertRemoteOpenFailAlarm(MysqlConnPool pool,int lock_id) {
        String sql_str = "INSERT INTO " + Sql_Mysql.Lock_Alm_Temp_Table + "(lock_id,alm_source,alm_start_time) VALUES(" + lock_id + "," + ElectLock_ComBase.ALM_Source_PlatForm + ",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();
        }
    }
}