ElectLock_Monitor/src/com/dev/lock/comm/Lock_Task_SQL.java
@@ -8,6 +8,7 @@
import com.base.Com;
import com.dev.lock.data.ElectLock_AuthIdcard;
import com.dev.lock.data.ElectLock_Bl;
import com.dev.lock.data.ElectLock_ComBase;
import com.dev.lock.data.ElectLock_Ctl_Log;
import com.dev.lock.data.ElectLock_Inf;
@@ -66,7 +67,7 @@
                  ",comm_count = " +  state.getComm_count() +
                  ",err_tol_count = " +  state.getErr_tol_count() +
                  ",err_count = " + state.getErr_count() +
                  " WHERE lock_id = " + lock_id;
                  " WHERE lock_id = " + state.getLock_id();
      Sql_Mysql sql = new Sql_Mysql(pool.getConn());
      try {
         sql.sqlMysqlExecute(sql_str);
@@ -86,9 +87,13 @@
   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_online =  " + ((state.getErr_count() > 4)?0:1) +
//                  " ,lock_online =  " + state.getLock_online() +
                  " ,lock_mac =  '" + state.getLockMacAddr() + "'" +
                  " ,last_update_time = '" + Com.getDateTimeFormat(inf.getLast_update_time(), Com.DTF_YMDhms) + "'" +
                  " ,bl_state =  '" + state.getBluetooth_state() + "'" +
                  " ,lock_open_count =  " + state.getLock_open_count() +
                  " ,model =  " + state.getWorkmodel() +
                  " ,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 {
@@ -108,14 +113,25 @@
    * @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 "
      String sql_str =  " SELECT op_cmd,id_card_set,lock_addr_set,ctl_uname,lock_id "
                  + " FROM " + Sql_Mysql.Lock_Rt_Table + ""
                  + " WHERE lock_id = " + lock_id;
                  + " WHERE FLOOR(lock_id/10000) = " + (lock_id/10000)
                  + " AND ("
                  + " op_cmd = " + ElectLock_ComBase.CMD_ClearIdCard
                  + " OR op_cmd = " + ElectLock_ComBase.CMD_REMOTE_OPEN_Lock
                  + " OR op_cmd = " + ElectLock_ComBase.CMD_REMOTE_ReStart
                  + " OR op_cmd = " + ElectLock_ComBase.CMD_AddIdCard
                  + " OR op_cmd = " + ElectLock_ComBase.CMD_RemoveIdCard
                  + " OR op_cmd = " + ElectLock_ComBase.CMD_OpenBluetooth
                  + " OR op_cmd = " + ElectLock_ComBase.CMD_CloseBluetooth
                  + " )";
      Sql_Mysql sql = new Sql_Mysql(pool.getConn());
      ResultSet res = null;
      try {
         res = sql.sqlMysqlQuery(sql_str);
         if(res.next()) {
            //当前需要控制的锁具ID
            param.setLock_id(res.getInt("lock_id"));
            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"));
@@ -146,7 +162,7 @@
   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;
            " WHERE lock_id = " + state.getLock_id();
      Sql_Mysql sql = new Sql_Mysql(pool.getConn());
      try {
         sql.sqlMysqlExecute(sql_str);
@@ -337,7 +353,7 @@
            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.setId_card_set(res.getLong("id_card_set"));         //'授权ID卡号',
            state.setLock_addr_set(res.getInt("lock_addr_set"));;      //'设置设备地址[慎用]',
            
         }
@@ -397,4 +413,227 @@
      }
      
   }
   /**
    *    当平台锁具模式未配置时返回-1
    *    返回0-离线模式   1-在线模式
    * @param pool
    * @return
    */
   public static int querySystemLockMode(MysqlConnPool pool) {
      int sys_model = -1;
      String sql_str_sel = "SELECT plate_model FROM " + Sql_Mysql.Plate_Info_Table;
      Sql_Mysql sql = new Sql_Mysql(pool.getConn());
      ResultSet res = null;
      try {
         res = sql.sqlMysqlQuery(sql_str_sel);
         if(res.next()) {
            sys_model = res.getInt("plate_model");
         }
      } 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 sys_model;
   }
   /**
    * 插入远程开锁失败记录
    */
   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();
      }
   }
   /**
    *    设置当前锁具离线
    * @param pool
    * @param client_id
    */
   public static void updateLock_OffLineTable(MysqlConnPool pool, int client_id) {
      String sql_str = " UPDATE " + Sql_Mysql.Lock_Inf_Table + " SET lock_online = 0 WHERE FLOOR(lock_id/10000) = " + (client_id/10000);
      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();
      }
   }
   /**
    *    插入默认的ID卡信息
    * @param pool
    * @param client_id
    */
   public static void insertLock_IdCard_Table(MysqlConnPool pool, int lock_id) {
      String sql_str_sel = " SELECT * FROM " + Sql_Mysql.Lock_IdCard_Table + " WHERE lock_id =  " + lock_id;
      String sql_str_ins = " INSERT INTO " + Sql_Mysql.Lock_IdCard_Table + "(lock_id) VALUES(" + lock_id + ");";
      Sql_Mysql sql = new Sql_Mysql(pool.getConn());
      ResultSet res = null;
      try {
         res = sql.sqlMysqlQuery(sql_str_sel);
         if(!res.next()) {
            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();
      }
   }
   /**
    *    插入默认的ID卡信息
    * @param pool
    * @param client_id
    */
   public static void insertLock_Rt_Table(MysqlConnPool pool, int lock_id) {
      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) VALUES(" + lock_id + ");";
      Sql_Mysql sql = new Sql_Mysql(pool.getConn());
      ResultSet res = null;
      try {
         res = sql.sqlMysqlQuery(sql_str_sel);
         if(!res.next()) {
            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();
      }
   }
   /**
    *    更细当前锁具的ID卡
    * @param pool
    * @param client_id
    */
   public static void updateLock_IdCard_Table(MysqlConnPool pool, ElectLock_State state) {
      StringBuffer sql_str = new StringBuffer(" UPDATE " + Sql_Mysql.Lock_IdCard_Table + " SET ");
      for(int k = 0;k<state.getId_cards().length;k++) {
         if(k > 0) {
            sql_str.append(",") ;
         }
         sql_str.append("card" + (k+1) + " = '" + state.getId_cards()[k] + "'") ;
      }
      sql_str.append(" WHERE lock_id =   " + state.getLock_id()) ;
      Sql_Mysql sql = new Sql_Mysql(pool.getConn());
      try {
         sql.sqlMysqlExecute(sql_str.toString());
      } catch (SQLException e) {
         sql.logger.error(e.toString(),e);
      } finally {
         sql.close_con();
      }
   }
   /**
    * 查询当前需要开启蓝牙的计划
    * @param pool
    * @param lock_id
    * @return
    */
   public static ElectLock_Bl queryBluetoothOpenPlan(MysqlConnPool pool,int lock_id) {
      ElectLock_Bl bl = null;
      String sql_str_sel = "SELECT * FROM " + Sql_Mysql.Lock_BL_Table
                     + " WHERE NOW() BETWEEN start_time AND DATE_ADD(start_time,INTERVAL " + ElectLock_ComBase.Lock_BluetoothCheckTimeLong + " SECOND)"
                     + " and type = 1 AND lock_id = " + lock_id;
      Sql_Mysql sql = new Sql_Mysql(pool.getConn());
      ResultSet res = null;
      try {
         res = sql.sqlMysqlQuery(sql_str_sel);
         if(res.next()) {
            bl = new ElectLock_Bl();
            bl.setLock_id(lock_id);
            bl.setCreate_time(res.getTimestamp("create_time"));
            bl.setStart_time(res.getTime("start_time"));
            bl.setStop_time(res.getTime("stop_time"));
            bl.setType(res.getInt("type"));
         }
      } 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 bl;
   }
   /**
    * 查询当前需要开启蓝牙的计划
    * @param pool
    * @param lock_id
    * @return
    */
   public static ElectLock_Bl queryBluetoothClosePlan(MysqlConnPool pool,int lock_id) {
      ElectLock_Bl bl = null;
      String sql_str_sel = "SELECT * FROM " + Sql_Mysql.Lock_BL_Table
                     + " WHERE NOW() BETWEEN stop_time AND DATE_ADD(stop_time,INTERVAL " + ElectLock_ComBase.Lock_BluetoothCheckTimeLong + " SECOND)"
                     + " and type = 1 AND lock_id = " + lock_id;
      Sql_Mysql sql = new Sql_Mysql(pool.getConn());
      ResultSet res = null;
      try {
         res = sql.sqlMysqlQuery(sql_str_sel);
         if(res.next()) {
            bl = new ElectLock_Bl();
            bl.setLock_id(lock_id);
            bl.setCreate_time(res.getTimestamp("create_time"));
            bl.setStart_time(res.getTime("start_time"));
            bl.setStop_time(res.getTime("stop_time"));
            bl.setType(res.getInt("type"));
         }
      } 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 bl;
   }
}