package com.battdata_rt; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.Date; import com.base.Com; import com.sql.MysqlConnPool; import com.sql.Sql_Mysql; public class BattData_RT_SQL { /** * ´´½¨tb_batt_rtdata±í²¢ÇÒ¸ù¾Ý¼¯ºÏbd_al£¬Ïò±íÖÐÌí¼Óµç³Ø×éidºÍmon_num Öµ * @param conn_pool * @param bd_al */ public static void createBattData_RT_RamDB_Table(MysqlConnPool conn_pool, int bg_id, int batt_cnt) { String tb_name = "`db_ram_batt_rt`.`tb_batt_rtdata_" + bg_id + "`"; String sql_str0 = "DELETE FROM " + tb_name + " WHERE " + tb_name + ".num>0"; String sql_str1 = "CREATE TABLE IF NOT EXISTS " + tb_name + " " + "( `num` BIGINT NOT NULL AUTO_INCREMENT, " + "`BattGroupId` INT NOT NULL DEFAULT 0, " + "`rec_datetime` DATETIME NOT NULL DEFAULT '1982-01-01 00:00:00', " + "`mon_num` INT NOT NULL DEFAULT 0, " + "`mon_vol` FLOAT NOT NULL DEFAULT 0, " + "`mon_tmp` FLOAT NOT NULL DEFAULT 0, " + "`mon_res` FLOAT NOT NULL DEFAULT 0, " + "`mon_ser` FLOAT NOT NULL DEFAULT 0, " + "`mon_conn_res` FLOAT NOT NULL DEFAULT 0, " + "`mon_cap` FLOAT NOT NULL DEFAULT 0, " + "`batt_count` INT NOT NULL DEFAULT 0, " + "`online_vol` FLOAT NOT NULL DEFAULT 0, " + "`group_vol` FLOAT NOT NULL DEFAULT 0, " + "`group_tmp` FLOAT NOT NULL DEFAULT 0, " + "`group_curr` FLOAT NOT NULL DEFAULT 0, " + "`batt_state` INT NOT NULL DEFAULT 0, " + "`batt_test_type` INT NOT NULL DEFAULT 0, " + "`batt_test_starttime` DATETIME NOT NULL DEFAULT '1982-01-01 00:00:00', " + "`batt_test_recordtime` DATETIME NOT NULL DEFAULT '1982-01-01 00:00:00', " + "`batt_test_tlong` INT NOT NULL DEFAULT 0, " + "`batt_test_cap` FLOAT NOT NULL DEFAULT 0, " + "`batt_real_cap` FLOAT NOT NULL DEFAULT 0, " + "`batt_rest_cap` FLOAT NOT NULL DEFAULT 0, " + "`batt_rest_power1_time` INT NOT NULL DEFAULT 0, " + "`batt_rest_power2_time` INT NOT NULL DEFAULT 0, " + "INDEX index_BattGroupId (`BattGroupId`), " + "PRIMARY KEY (`num`)) " + "ENGINE=MEMORY DEFAULT CHARSET=utf8;"; String sql_str2 = "INSERT INTO " + tb_name + " " + "(BattGroupId," + "mon_num)" + " VALUES "; int cnt = 0; for(int mc=0; mc 0) { sql_str2 += ","; } sql_str2 += "(" + bg_id + "," + (mc+1) + ")"; cnt++; } Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn()); try { sql.sqlMysqlExecute(sql_str1); ResultSet res_t = sql.sqlMysqlQuery("SELECT COUNT(*) FROM " + tb_name); if(res_t.next()) { int cnt_row = res_t.getInt(1); if(cnt_row != batt_cnt) { sql.sqlMysqlExecute(sql_str0); sql.sqlMysqlExecute(sql_str2); } } else { sql.sqlMysqlExecute(sql_str2); } } catch (SQLException e) { e.printStackTrace(); } finally { sql.close_con(); } } /************************************************************************************************/ public static void updateBattState_RT_RamDB_Table(MysqlConnPool conn_pool, BattData_RT bd_rt) { Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn()); try { Date dt_test_Start = new Date(); Date dt_record = new Date(); dt_test_Start.setTime(bd_rt.getTestStartTime()); dt_record.setTime(bd_rt.getTestRecordTime()); String sql_str = "UPDATE " + Sql_Mysql.BattRtState_Table + " SET " + " rec_datetime='" + Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms) + "'," + " batt_count=" + bd_rt.MonCount + "," + " online_vol=" + bd_rt.getOnlineVol() + "," + " group_vol=" + bd_rt.getGroupVol() + "," + " group_tmp=" + bd_rt.getGroupTmp() + "," + " group_curr=" + bd_rt.getTestCurr() + "," + " batt_state=" + bd_rt.getBattState() + "," + " batt_test_type=" + bd_rt.getBattTestType() + "," + " batt_test_starttime='" + Com.getDateTimeFormat(dt_test_Start, Com.DTF_YMDhms) + "'," + " batt_test_recordtime='" + Com.getDateTimeFormat(dt_record, Com.DTF_YMDhms) + "'," + " batt_test_tlong=" + bd_rt.getTestTimeLong() + "," + " batt_test_cap=" + bd_rt.getTestCap() + "," + " batt_real_cap=" + bd_rt.getBattRealCap() + "," + " batt_rest_cap=" + bd_rt.getBattRestCap() + "," + " batt_rest_power1_time=" + bd_rt.getBattRestTime() + " WHERE BattGroupId=" + bd_rt.BattGroupId; //System.out.println(sql_str); sql.sqlMysqlExecute(sql_str); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { sql.close_con(); } } /************************************************************************************************/ /** * ¸üÐÂtb_batt_rtdata±íÖеÄÊý¾Ý * @param conn_pool * @param bd_al */ public static void updateBattData_RT_RamDB_Table(MysqlConnPool conn_pool, BattData_RT bd_rt) { Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn()); try { String sql_str = "UPDATE " + Sql_Mysql.BattRtData_Table + " SET " + " rec_datetime='" + Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms) + "'"; float tmp_dt = 0; String sql_str_vol = " "; for(int n=0; n 1) { sql_str_vol = ", mon_vol = CASE mon_num " + sql_str_vol + " ELSE mon_vol END "; } String sql_str_tmp = " "; for(int n=0; n 1) { sql_str_tmp = ", mon_tmp = CASE mon_num " + sql_str_tmp + " ELSE mon_tmp END "; } String sql_str_res = " "; for(int n=0; n 1) { sql_str_res = ", mon_res = CASE mon_num " + sql_str_res + " ELSE mon_res END "; } String sql_str_ser = " "; for(int n=0; n 1) { sql_str_ser = ", mon_ser = CASE mon_num " + sql_str_ser + " ELSE mon_ser END "; } String sql_str_2 = sql_str_vol + sql_str_tmp + sql_str_res + sql_str_ser; if(sql_str_2.trim().length() > 1) { sql_str_2 += " WHERE BattGroupId=" + bd_rt.BattGroupId; /* + " AND mon_num IN ("; for(int n=0; n 0) { sql_str_2 += ","; } sql_str_2 += (n+1); } sql_str_2 += ")"; */ } else { sql_str_2 = " WHERE BattGroupId=" + bd_rt.BattGroupId; } sql_str += sql_str_2; //System.out.println(sql_str); sql.sqlMysqlExecute(sql_str); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { sql.close_con(); } } /************************************************************************************************/ /** * ´´½¨µç³Ø×é²âÊÔÏà¹ØµÄÊý¾Ý±í * @param tb_name * @return */ public static String getCreateBattTestDataStr(String tb_name) { String sql_str = "CREATE TABLE IF NOT EXISTS " + tb_name + " " + "( `num` BIGINT NOT NULL AUTO_INCREMENT, " + "`BattGroupId` INT NOT NULL DEFAULT 0, " + "`test_record_count` INT NOT NULL DEFAULT 0, " + "`test_type` INT NOT NULL DEFAULT 0, " + "`data_new` BOOLEAN NOT NULL DEFAULT false, " + "`data_available` BOOLEAN NOT NULL DEFAULT false, " + "`record_num` INT NOT NULL DEFAULT 0, " + "`test_starttime` DATETIME NOT NULL DEFAULT '1982-01-01 00:00:00', " + "`record_time` DATETIME NOT NULL DEFAULT '1982-01-01 00:00:00', " + "`test_timelong` INT NOT NULL DEFAULT 0, " + "`online_vol` FLOAT NOT NULL DEFAULT 0, " + "`group_vol` FLOAT NOT NULL DEFAULT 0, " + "`test_curr` FLOAT NOT NULL DEFAULT 0, " + "`test_cap` FLOAT NOT NULL DEFAULT 0, " + "`mon_num` INT NOT NULL DEFAULT 0, " + "`mon_vol` FLOAT NOT NULL DEFAULT 0, " + "`mon_tmp` FLOAT NOT NULL DEFAULT 0, " + "INDEX index_test_record_count (`test_record_count`), " + "PRIMARY KEY (`num`));"; return sql_str; } /** * Ïòtb_batttestdata_idÊý¾Ý¿âÖÐÌí¼Ó²âÊÔÊý¾Ý * @param rt_data * @return */ public static String getInsertBattTestDataStr(BattData_RT rt_data) { int mon_count = rt_data.MonCount; BattStatData tmp_test_data = rt_data.mTestData.clone(); String sql_str = "INSERT INTO tb_BattTestData_" + rt_data.BattGroupId + " " + "(BattGroupId, " + "test_record_count, " + "test_type, " + "data_new, " + "data_available, " + "record_num, " + "test_starttime, " + "record_time, " + "test_timelong, " + "online_vol, " + "group_vol, " + "test_curr, " + "test_cap, " + "mon_num, " + "mon_vol, " + "mon_tmp)" + " VALUES "; String sql_str_data = ""; for(int n=0; n0) && (false == rt_data.is_al_MonVol_History_New(n)) && (tmp_test_data.recordNum > 2)) continue; if(n > 0) sql_str_data += ", "; sql_str_data += "("+ rt_data.BattGroupId + ", " + tmp_test_data.testRecordCount + ", " + tmp_test_data.battTestType_For_DataSave + ", " + true + ", " + true + ", " + (tmp_test_data.recordNum + 1) + ", " + "'" + Com.getDateTimeFormat(tmp_test_data.startTestTime, Com.DTF_YMDhms) + "', " + "'" + Com.getDateTimeFormat(tmp_test_data.recordTime, Com.DTF_YMDhms) + "', " + tmp_test_data.testTimeLong + ", " + tmp_test_data.onlineVol + ", " + tmp_test_data.groupVol + ", " + tmp_test_data.testCurr + ", " + tmp_test_data.testCap + ", " + (n+1) + ", " + rt_data.get_al_MonVol_History(n) + ", " + rt_data.al_MonVol.get(n).monTmp + ")"; } /* //--------------- ´æ´¢BPM7100×é¶ËÎÂ¶È ----------------// if(main_MonitorServer_FBS9100S.is_app_for_bpm7100_only) { if(sql_str_data.trim().length() > 0) { sql_str_data += ","; } sql_str_data += "("+ rt_data.BattGroupId + ", " + tmp_test_data.testRecordCount + ", " + tmp_test_data.battTestType_For_DataSave + ", " + true + ", " + true + ", " + (tmp_test_data.recordNum + 1) + ", " + "'" + Com.getDateTimeFormat(tmp_test_data.startTestTime, Com.DTF_YMDhms) + "', " + "'" + Com.getDateTimeFormat(tmp_test_data.recordTime, Com.DTF_YMDhms) + "', " + tmp_test_data.testTimeLong + ", " + tmp_test_data.onlineVol + ", " + tmp_test_data.groupVol + ", " + tmp_test_data.testCurr + ", " + tmp_test_data.testCap + ", " + (100010) + ", " + tmp_test_data.groupTmp + ")"; } */ return (sql_str + sql_str_data); } /** * Ïòtb_batttestdatastop_id±íÖÐÌí¼ÓÊý¾Ý»òÕ߸üÐÂÊý¾Ý * @param rt_data * @return */ public static ArrayList getInsertOrUpdateBattTestDataStopStr(BattData_RT rt_data) { ArrayList al_sql_str = new ArrayList(); int mon_count = rt_data.MonCount; BattStatData tmp_test_data = rt_data.mTestData.clone(); ArrayList tmp_mon_vol = rt_data.al_MonVol_History; rt_data.makeMaxMinMonVol(tmp_mon_vol); if(false == rt_data.MysqlDataResInfTable_Exist) { String sql_str = "INSERT INTO " + "tb_BattTestDataStop_" + rt_data.BattGroupId + " (BattGroupId, " + "test_record_count, " + "test_type, " + "data_new, " + "data_available, " + "record_num, " + "test_starttime, " + "record_time, " + "test_timelong, " //+ "online_vol, " + "group_vol, " + "test_curr, " + "test_cap, " + "mon_num, " + "mon_vol)" + " VALUES "; for(int n=0; n 0) sql_str += ", "; sql_str += "("+ rt_data.BattGroupId + ", " + tmp_test_data.testRecordCount + ", " + tmp_test_data.battTestType_For_DataSave + ", " + true + ", " + true + ", " + (tmp_test_data.recordNum + 1) + ", " + "'" + Com.getDateTimeFormat(tmp_test_data.startTestTime, Com.DTF_YMDhms) + "', " + "'" + Com.getDateTimeFormat(tmp_test_data.recordTime, Com.DTF_YMDhms) + "', " + tmp_test_data.testTimeLong + ", " //+ tmp_test_data.onlineVol + ", " + tmp_test_data.groupVol + ", " + tmp_test_data.testCurr + ", " + tmp_test_data.testCap + ", " + (n+1) + ", " + tmp_mon_vol.get(n).monVol + ")"; } al_sql_str.add(sql_str); } else { String sql_str = "UPDATE " + "tb_BattTestDataStop_" + rt_data.BattGroupId + " SET " + " data_new=" + true + ", " + "data_available=" + true + ", " + "record_num=" + (tmp_test_data.recordNum + 1) + ", " + "record_time='" + Com.getDateTimeFormat(tmp_test_data.recordTime, Com.DTF_YMDhms) + "', " + "test_timelong=" + tmp_test_data.testTimeLong + ", " //+ "online_vol=" + tmp_test_data.onlineVol + ", " + "group_vol=" + tmp_test_data.groupVol + ", " + "test_curr=" + tmp_test_data.testCurrAbsMax + ", " + "test_cap=" + tmp_test_data.testCap + " " + " WHERE " + " BattGroupId=" + rt_data.BattGroupId + " AND " + " test_record_count=" + tmp_test_data.testRecordCount; al_sql_str.add(sql_str); String temp_str = ""; sql_str = "UPDATE " + "tb_BattTestDataStop_" + rt_data.BattGroupId + " SET " + " mon_vol = CASE mon_num"; for(int n=0; n0) && (false == rt_data.is_al_MonVol_History_New(n)) && (tmp_test_data.recordNum > 2)) continue; if(n > 0) { temp_str += ","; } temp_str += (n+1); sql_str += " WHEN " + (n+1) + " THEN " + rt_data.get_al_MonVol_History(n); } sql_str += " END WHERE mon_num IN (" + temp_str + ")" + " AND BattGroupId=" + rt_data.BattGroupId + " AND " + " test_record_count=" + tmp_test_data.testRecordCount; al_sql_str.add(sql_str); } return al_sql_str; } /** * Ïòtb_batttestdata_inf±íÖÐÌí¼ÓÊý¾Ý»òÕ߸üÐÂÊý¾Ý * @param rt_data * @return */ public static String getInsertOrUpdateBattTestDataInfStr(BattData_RT rt_data, int test_stoptype_t) { String sql_str = " "; BattStatData tmp_test_data = rt_data.mTestData.clone(); tmp_test_data.test_stoptype = test_stoptype_t; if(false == rt_data.MysqlRecordInf_Exist) { sql_str = "INSERT INTO " + Sql_Mysql.BattTestDataInf_Table + " (BattGroupId, " + "test_record_count, " + "test_type, " + "record_time_interval, " + "data_new, " + "data_available, " + "record_num, " + "test_starttime, " + "test_starttype, " + "record_time, " + "test_timelong, " + "test_stoptype, " + "group_vol, " + "test_curr, " + "test_cap, " + "max_monnum, " + "max_monvol, " + "min_monnum, " + "min_monvol) " + " VALUES " + "("+ rt_data.BattGroupId + ", " + tmp_test_data.testRecordCount + ", " + tmp_test_data.battTestType_For_DataSave + ", " + rt_data.mSaveDataTimeInterval + ", " + true + ", " + true + ", " + (tmp_test_data.recordNum + 1) + ", " + "'" + Com.getDateTimeFormat(tmp_test_data.startTestTime, Com.DTF_YMDhms) + "', " + tmp_test_data.testLoaderType + ", " + "'" + Com.getDateTimeFormat(tmp_test_data.recordTime, Com.DTF_YMDhms) + "', " + tmp_test_data.testTimeLong + ", " + tmp_test_data.test_stoptype + ", " + tmp_test_data.groupVol + ", " + tmp_test_data.testCurr + ", " + tmp_test_data.testCap + ", " + rt_data.mMaxMonNum + ", " + rt_data.mMaxMonVol + ", " + rt_data.mMinMonNum + ", " + rt_data.mMinMonVol + ") "; } else { sql_str = "UPDATE " + Sql_Mysql.BattTestDataInf_Table + " SET " + "test_type=" + tmp_test_data.battTestType_For_DataSave + ", " + "record_time_interval=" + rt_data.mSaveDataTimeInterval + ", " + "data_new=" + true + ", " + "data_available=" + true + ", " + "record_num=" + (tmp_test_data.recordNum + 1) + ", " + "test_starttime='" + Com.getDateTimeFormat(tmp_test_data.startTestTime, Com.DTF_YMDhms) + "', " + "test_starttype=" + tmp_test_data.testLoaderType + ", " + "record_time='" + Com.getDateTimeFormat(tmp_test_data.recordTime, Com.DTF_YMDhms) + "', " + "test_timelong=" + tmp_test_data.testTimeLong + ", " + "test_stoptype=" + tmp_test_data.test_stoptype + ", " + "group_vol=" + tmp_test_data.groupVol + ", " + "test_curr=" + tmp_test_data.testCurrAbsMax + ", " + "test_cap=" + tmp_test_data.testCap + ", " + "max_monnum=" + rt_data.mMaxMonNum + ", " + "max_monvol=" + rt_data.mMaxMonVol + ", " + "min_monnum=" + rt_data.mMinMonNum + ", " + "min_monvol=" + rt_data.mMinMonVol + " " + " WHERE " + " BattGroupId=" + rt_data.BattGroupId + " AND " + " test_record_count=" + tmp_test_data.testRecordCount; } return sql_str; } /** * ¸ù¾Ý²âÊÔrt_dataÊý¾Ý¶ÔÏó¹¹Ôìɾ³ýÔÚtb_batttestdata_id,tb_testdatastop_id,tb_batttestdata_infÖеIJâÊԼǼµÄSQLÓï¾ä¼¯ºÏ * @param rt_data * @return */ public static ArrayList getDeleteBattTestDataStr(BattData_RT rt_data) { ArrayList al_str = new ArrayList(); BattStatData tmp_test_data = rt_data.mTestData.clone(); String sql_str = "DELETE FROM tb_BattTestData_" + rt_data.BattGroupId + " WHERE test_record_count=" + tmp_test_data.testRecordCount; al_str.add(sql_str); sql_str = "DELETE FROM tb_BattTestDataStop_" + rt_data.BattGroupId + " WHERE test_record_count=" + tmp_test_data.testRecordCount; al_str.add(sql_str); sql_str = "DELETE FROM " + Sql_Mysql.BattTestDataInf_Table + " WHERE " + " BattGroupId=" + rt_data.BattGroupId + " AND " + " test_record_count=" + tmp_test_data.testRecordCount; al_str.add(sql_str); return al_str; } /************************************************************************************************/ public static void queryBattState_From_RT_Table(MysqlConnPool conn_pool, BattData_RT bd_rt) { Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn()); try { String sql_str = "SELECT * FROM " + Sql_Mysql.BattRtState_Table + " WHERE BattGroupId=" + bd_rt.BattGroupId; //System.out.println(sql_str); ResultSet rs_t = sql.sqlMysqlQuery(sql_str); if(rs_t.next()) { bd_rt.mTestData.battTestState = rs_t.getByte("batt_test_type"); if((BattStatData.BATTDATA_DISCHARGE == bd_rt.mTestData.battTestState) || (BattStatData.BATTDATA_CHARGE == bd_rt.mTestData.battTestState)) { bd_rt.mTestData.battTestType_For_DataSave = bd_rt.mTestData.battTestState; bd_rt.mTestData.testLoaderType = rs_t.getByte("test_loadertype"); bd_rt.mTestData.recordNum = rs_t.getInt("test_recordnum"); bd_rt.mTestData.onlineVol = rs_t.getFloat("online_vol"); bd_rt.mTestData.groupVol = rs_t.getFloat("group_vol"); bd_rt.mTestData.groupTmp = rs_t.getFloat("group_tmp"); bd_rt.mTestData.testCurr = rs_t.getFloat("group_curr"); bd_rt.mTestData.TestCurr_RT = bd_rt.mTestData.testCurr; bd_rt.mTestData.battState = rs_t.getByte("batt_state"); bd_rt.mTestData.startTestTime.setTime(rs_t.getTimestamp("batt_test_starttime").getTime()); bd_rt.mTestData.recordTime.setTime(rs_t.getTimestamp("batt_test_recordtime").getTime()); bd_rt.mTestData.testTimeLong = rs_t.getInt("batt_test_tlong"); bd_rt.mTestData.testCap = rs_t.getFloat("batt_test_cap"); bd_rt.mTestData.battRealCap = rs_t.getFloat("batt_real_cap"); bd_rt.mTestData.battRestCap = rs_t.getFloat("batt_rest_cap"); bd_rt.mTestData.battRestTime = rs_t.getInt("batt_rest_power1_time"); } } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { sql.close_con(); } } /************************************************************************************************/ public static int queryTestRecordCountMax_From_tb_batttestdata_inf(MysqlConnPool conn_pool, int bg_id) { int rec_count = 0; Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn()); try { String sql_str = "SELECT MAX(test_record_count) FROM " + Sql_Mysql.BattTestDataInf_Table + " WHERE BattGroupId=" + bg_id; //System.out.println(sql_str); ResultSet rs_t = sql.sqlMysqlQuery(sql_str); if(rs_t.next()) { rec_count = rs_t.getInt(1); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { sql.close_con(); } return rec_count; } /************************************************************************************************/ }