DELL
2024-12-19 6ab9c7858c234f51719acc3e514a1af769da578b
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
package com.dev.lock.comm;
 
import java.sql.ResultSet;
import java.sql.SQLException;
 
import com.base.Com;
import com.dev.lock.data.ElectLock_ComBase;
import com.dev.lock.data.ElectLock_Ctl_Log;
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.setComm_count(res.getInt("comm_count"));                //'通信计数',
                state.setErr_tol_count(res.getInt("err_tol_count"));        //'总错误计数',
            }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,int lock_id,ElectLock_State state) {
        String sql_str = "UPDATE " + Sql_Mysql.Lock_Inf_Table + 
                        " Set lock_state = " +  state.getLock_state() +
                        " 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 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 "
                        + " 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"));
                
            }
        } 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) VALUES("
                            + "" + log.lock_id
                            + "," + log.ctl_type + ""
                            + "," + log.ctl_result + ""
                            + ",'" + Com.getDateTimeFormat(log.ctl_time, Com.DTF_YMDhms) + "'"
                            + "," + log.ctl_id_card + ""
                            + ");";
        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();
        }
    }
}