Administrator
2022-02-28 64659e3795b4f7cbafb2eaca8fd3831485035d71
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package com.battmonitor.data_store;
 
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
 
import com.battdata_rt.BattData_RT;
import com.battdata_rt.BattStatData;
import com.battdata_rt.MonVolData;
import com.sql.MysqlConnPool;
import com.sql.Sql_Mysql;
 
 
public class InsertDataToMysql_Task_SQL {
    
    /************************************************************************************************/
    public static void get_ResTestState_From_FBS9100State_RT_RamDB_Table(MysqlConnPool conn_pool, BattData_RT bd_rt)
    {
        Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn());
        try  {
            String sql_str = "SELECT dev_res_test_state,dev_restest_count FROM " + Sql_Mysql.FBS9100State_Table
                            + " WHERE dev_id=" + bd_rt.FBSDeviceId;
            //System.out.println(sql_str);
            ResultSet res = sql.sqlMysqlQuery(sql_str);
            if(res.next()) {
                bd_rt.mTestData.batt_res_test_state = res.getInt("dev_res_test_state");
                bd_rt.mTestData.batt_res_test_cnt = res.getInt("dev_restest_count");
            }
            res.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            sql.logger.error("InsertDataToMysql_Task_SQL.get_ResTestState_From_FBS9100State_RT_RamDB_Table():" + e.toString(), e);
        } finally {
            sql.close_con();
        }
    }
    /************************************************************************************************/
    public static void get_GroupVolCurr_From_BattState_RT_RamDB_Table(MysqlConnPool conn_pool, BattData_RT bd_rt)
    {
        Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn());
        try  {
            String sql_str = "SELECT rec_datetime,online_vol,group_vol,group_curr FROM " + Sql_Mysql.BattRtState_Table
                            + " WHERE BattGroupId=" + bd_rt.BattGroupId;
            //System.out.println(sql_str);
            ResultSet res = sql.sqlMysqlQuery(sql_str);
            if(res.next()) {
                bd_rt.mTestData.updateNewDataRCTime(res.getDate("rec_datetime"));
                bd_rt.mTestData.updateGroupVolFrom_FBSDev(res.getFloat("group_vol"));
                bd_rt.mTestData.updateOnlineVolFrom_FBSDev(res.getFloat("online_vol"));
                bd_rt.mTestData.updateCurrFrom_FBSDev(res.getFloat("group_curr"));
            }
            res.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            sql.logger.error("InsertDataToMysql_Task_SQL.get_GroupVolCurr_From_BattState_RT_RamDB_Table():" + e.toString(), e);
        } finally {
            sql.close_con();
        }
    }
    /************************************************************************************************/
    public static void get_MonData_From_BattData_RT_RamDB_Table(MysqlConnPool conn_pool, BattData_RT bd_rt)
    {
        Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn());
        try  {
            String sql_str = "SELECT mon_num,mon_vol,mon_tmp,mon_res,mon_ser,mon_conn_res FROM " + Sql_Mysql.BattRtData_Table
                            + " WHERE BattGroupId=" + bd_rt.BattGroupId
                            + " ORDER BY mon_num ASC";
            //System.out.println(sql_str);
            ResultSet res = sql.sqlMysqlQuery(sql_str);
            try {
                while(res.next()) {
                    int mon_index = res.getInt("mon_num");
                    if(mon_index > 0) {
                        mon_index -= 1;
                    }
                    if(mon_index < bd_rt.al_MonVol.size()) {
                        bd_rt.al_MonVol.get(mon_index).monVol = res.getFloat("mon_vol");
                        bd_rt.al_MonVol.get(mon_index).monTmp = res.getFloat("mon_tmp");
                        bd_rt.al_MonVol.get(mon_index).monRes = res.getFloat("mon_res");
                        bd_rt.al_MonVol.get(mon_index).monSer = res.getFloat("mon_ser");
                        bd_rt.al_MonVol.get(mon_index).connRes = res.getFloat("mon_conn_res");
                    }
                }
            } catch (Exception e) {
                sql.logger.error("InsertDataToMysql_Task_SQL.get_MonData_From_BattData_RT_RamDB_Table():" + e.toString(), e);
            }
            res.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            sql.logger.error("InsertDataToMysql_Task_SQL.get_MonData_From_BattData_RT_RamDB_Table():" + e.toString(), e);
        } 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 = "";
        try {
            for(int n=0; n<mon_count; n++) {
                /*
                if((n>0) && (false == rt_data.is_al_MonVol_History_New(n)) && (tmp_test_data.recordNum > 2)) {
                    continue;
                }
                */
                String tmp_str_data = "("+ rt_data.BattGroupId + ", "
                            + tmp_test_data.testRecordCount + ", "
                            + tmp_test_data.battTestType_For_DataSave + ", "
                            + true + ", "
                            + true + ", "
                            + (tmp_test_data.recordNum + 1) + ", "
                            + "'" + tmp_test_data.getStartTestTimeString() + "', "
                            + "'" + tmp_test_data.getRecordTimeString() + "', "
                            + 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 + ")";
                if(n > 0) {
                    sql_str_data += ", ";
                }
                sql_str_data += tmp_str_data;
            }
        } catch (Exception e) {
            System.err.println("InsertDataToMysql_Task_SQL.getInsertBattTestDataStr():" + e.getMessage());
        }
        /*
        //--------------- �洢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<String> getInsertOrUpdateBattTestDataStopStr(BattData_RT rt_data)
    {
        ArrayList<String> al_sql_str = new ArrayList<String>();
        int mon_count = rt_data.MonCount;
        BattStatData tmp_test_data = rt_data.mTestData.clone();
        ArrayList<MonVolData> 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 ";
            try {
                for(int n=0; n<mon_count; n++) {
                    String sub_sql_str = "("+ rt_data.BattGroupId + ", "
                                + tmp_test_data.testRecordCount + ", "
                                + tmp_test_data.battTestType_For_DataSave + ", "
                                + true + ", "
                                + true + ", "
                                + (tmp_test_data.recordNum + 1) + ", "
                                + "'" + tmp_test_data.getStartTestTimeString() + "', "
                                + "'" + tmp_test_data.getRecordTimeString() + "', "
                                + 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 + ")";
                    if(n > 0) {
                        sql_str += ", ";
                    }
                    sql_str += sub_sql_str;
                }
            } catch (Exception e) {
                System.err.println("InsertDataToMysql_Task_SQL.getInsertOrUpdateBattTestDataStopStr():" + e.getMessage());
            }
            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='" + tmp_test_data.getRecordTimeString() + "', "
                    + "test_timelong=" + tmp_test_data.testTimeLong + ", "
                    //+ "online_vol=" + tmp_test_data.onlineVol + ", "
                    + "group_vol=" + tmp_test_data.groupVol + ", "
                    + "test_curr=" + tmp_test_data.getAvgTestCurr()/*.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; n<mon_count; n++)
            {
                if((n>0) && (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) + ", "
                    + "'" + tmp_test_data.getStartTestTimeString() + "', "
                    + tmp_test_data.testLoaderType + ", "
                    + "'" + tmp_test_data.getRecordTimeString() + "', "
                    + 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='" + tmp_test_data.getStartTestTimeString() + "', "
                    + "test_starttype=" + tmp_test_data.testLoaderType + ", "
                    + "record_time='" + tmp_test_data.getRecordTimeString() + "', "
                    + "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_starttime='" + tmp_test_data.getStartTestTimeString() + "'";
                    //+ " 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<String> getDeleteBattTestDataStr(BattData_RT rt_data)
    {
        ArrayList<String> al_str = new ArrayList<String>();
        
        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;
    }
}