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
package com.dec.fbs9100;
 
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import com.base.Com;
import com.battdata_rt.BattData_RT;
 
public class BTS61850_Task_Thread_SQL {
 
    /**
     *     判断当前历史实时数据表是否存在
     * @param conn_pool
     * @param dev_id
     * @param date
     * @return    true:已存在    false:不存在 
     */
    public static boolean checkHistoryIfNotExistsTable(MysqlConnPool conn_pool,int BattGroupId) {
        Sql_Mysql sql = new Sql_Mysql(conn_pool);
        String tname;
        try {
            sql.sqlMysqlUseDB(Sql_Mysql.DB_BATT_HISTORY);
            tname = Sql_Mysql.Tb_Batt_RealData + BattGroupId + "_" + Com.getDateTimeFormat(new Date(), Com.DTF_YM);
            return sql.sqlMysqlCheckIfTableExist(tname);
        } catch (SQLException e) {
            sql.logger.error("checkHistoryIfNotExistsTable():" + e.toString(), e);
        } finally {
            sql.close_con();
        }
        return false;
    }
    
    /**
     * 创建指定的电池组历史数据记录表
     * @param pool
     * @param BattGroupId    电池组id
     * @param time 记录时间
     */
    public static void CreateTb_BattRealDataTable(MysqlConnPool pool,int BattGroupId,Date time) {
        String  sql_str = "CREATE TABLE IF NOT EXISTS " + Sql_Mysql.Tb_Batt_RealData+BattGroupId+"_"+Com.getDateTimeFormat(time, Com.DTF_YM) + " (" + 
                "  `num` bigint(20) NOT NULL AUTO_INCREMENT," + 
                "  `BattGroupId` int(11) NOT NULL DEFAULT '1'," + 
                "  `recrod_time` datetime NOT NULL DEFAULT '2000-01-01 00:00:00'," + 
                "  `group_vol` float NOT NULL DEFAULT '0'," + 
                "  `online_vol` float NOT NULL DEFAULT '0'," + 
                "  `group_curr` float NOT NULL DEFAULT '0'," + 
                "  `group_tmp` float NOT NULL DEFAULT '0'," + 
                "  `batt_state` int(11) NOT NULL DEFAULT '0'," + 
                "  `batt_test_type` int(11) NOT NULL DEFAULT '0'," + 
                "  `batt_test_starttime` datetime NOT NULL DEFAULT '2000-01-01 00:00:00'," + 
                "  `batt_test_tlong` int(11) NOT NULL DEFAULT '0'," + 
                "  `batt_test_cap` float NOT NULL DEFAULT '0'," + 
                "  `mon_num` int(11) NOT NULL DEFAULT '1'," + 
                "  `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'," + 
                "  `mon_JH_curr` float NOT NULL DEFAULT '0'," + 
                "  `note` varchar(255) NOT NULL DEFAULT ''," + 
                "  PRIMARY KEY (`num`)," + 
                "  KEY `index_battgroupid` (`BattGroupId`) USING BTREE," + 
                "  KEY `index_record_time` (`recrod_time`) USING BTREE" + 
                ") ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;";
        Sql_Mysql sql = new Sql_Mysql(pool);
        try {
            sql.sqlMysqlExecute(sql_str);
        } catch (SQLException e) {
            sql.logger.error("CreateTb_BattRealDataTable():" + e.toString(), e);
        } finally {
            sql.close_con();
        }
    }
    
    /**
     *     删除修改时间超时的历史数据表[没有修改时间的表不会有应影响]
     */
    public static void deleteHistoryData(MysqlConnPool conn_pool,Date deldate) {
        String sql_select_strs = " select TABLE_NAME,UPDATE_TIME " + 
                                 " from information_schema.tables " + 
                                 " where table_schema='db_batt_history' " + 
                                 " AND TABLE_NAME like 'tb_batt_realdata_%' " + 
                                 " AND CREATE_TIME <= '"+Com.getDateTimeFormat(deldate, Com.DTF_YMDhms)+"';" ;
        String sql_delete_strs = " DROP TABLE IF EXISTS ";
        Sql_Mysql sql = new Sql_Mysql(conn_pool);
        ResultSet res = null;
        int count = 0;
        try {
            res = sql.sqlMysqlTotalQuery(sql_select_strs);
            while(res.next()) {
                if(count > 0) {
                    sql_delete_strs += ",";
                }
                
                sql_delete_strs += "db_batt_history." + res.getString("TABLE_NAME");
                sql.logger.warn("删除:"+res.getString("TABLE_NAME")+"\t at "+Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms));
                count++;
            }
            if(count >0) {
                sql.sqlMysqlTotalExecute(sql_delete_strs);
            }
            
        } catch (SQLException e) {
            sql.logger.error("deleteHistoryData():" + e.toString(), e);
        } finally {
            if(null != res) {
                try {
                    res.close();
                } catch (SQLException e) {
                    sql.logger.error("deleteHistoryData():" + e.toString(), e);
                }
            }
            sql.close_con();
        }
    }
    
    /**
     *     更新电池设备的Ukey信息
     * @param m_ConnPool
     * @param param
     */
    public static void UpdateBattInfTable(MysqlConnPool m_ConnPool,FBS9100_StatAndParam param) {
        String sql_str = " UPDATE " + Sql_Mysql.BattInf_Table + ""
                + " SET PublicKeyX = '" + param.publicKeyX /* +"',KeyID = '" + param.keyID */ + "',SerialNumber = '"+param.serialNumber+"'"
                + " WHERE FBSDeviceId = " + param.dev_id;
        
        Sql_Mysql sql = new Sql_Mysql(m_ConnPool);
        try {
            sql.sqlMysqlExecute(sql_str);
        } catch (SQLException e) {
            sql.logger.error("BTS61850_Task_Thread_SQL.UpdateBattInfTable():" + e.toString(), e);
        } finally {
            sql.close_con();
        }
    }
 
    public static void updateBattRtState_Table(MysqlConnPool pool,FBS9100_VCData vc_data,BattData_RT[] battdata) {
        for(int i=0;i<battdata.length;i++) {
            if(null == battdata[i]) {
                continue;
            }
            String sql_str = " UPDATE " + Sql_Mysql.BattRsAlarm_Table + ""
                    + " SET record_time = '"+Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms)+"'"
                    + ",groupvol_alm = " + vc_data.groupvolalm[i]
                    + ",test_end_cap=" + vc_data.test_end_cap[i]
                    + ",test_end_vol=" + vc_data.test_end_vol[i]
                    + ",test_timelong= " + vc_data.test_timelong[i] +
                    " WHERE BattGroupId = " + battdata[i].BattGroupId;
            Sql_Mysql sql = new Sql_Mysql(pool);
            try {
                sql.sqlMysqlExecute(sql_str);
            } catch (SQLException e) {
                sql.logger.error("BTS61850_Task_Thread_SQL.updateBattRtState_Table():" + e.toString(), e); 
            } finally {
                sql.close_con();
            }
            
        }
    }
 
    /**
     * 查询设备的uKeyID
     * @param m_ConnPool
     * @param dev_id
     * @return
     */
    public static String queryDeviceUkeyID(MysqlConnPool m_ConnPool, int dev_id) {
        String ukeyID = "";
        String sql_str = "SELECT KeyID FROM " + Sql_Mysql.BattInf_Table+ " WHERE FBSDeviceId = " + dev_id + " LIMIT 1";
        Sql_Mysql sql = new Sql_Mysql(m_ConnPool);
        ResultSet res = null;
        try {
            res = sql.sqlMysqlQuery(sql_str);
            if(res.next()) {
                ukeyID = res.getString("KeyID").trim();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if(null != res) {
                try {
                    res.close();
                } catch (SQLException e) {
                    sql.logger.error("BTS61850_Task_Thread_SQL.queryDeviceUkeyID():" + e.toString(), e); 
                }
            }
            sql.close_con();
        }
        return ukeyID;
    }
}