whyclj
2020-10-20 e9f26f7b367518b4087ff34b336aa3f2fbc0fd79
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
package com.dev.data;
 
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
 
import com.base.Com;
import com.sql.MysqlConnPool;
import com.sql.Sql_Mysql;
 
public class Dev_UDPServer_Thread_SQL {
    
    public void initDB(MysqlConnPool conn_pool) {
        
        CreateDb_Equipstand(conn_pool);                //´´½¨db_equipstandÊý¾Ý¿â
        
        CreateDb_Equip_History(conn_pool);            //´´½¨ÀúʷʵʱÊý¾Ý¼Ç¼Êý¾Ý¿â
        
        CreateDb_Equip_Alarm(conn_pool);             //´´½¨¸æ¾¯Êý¾Ý¿â
        
        CreateTb_device_inf(conn_pool);                //´´½¨É豸ÐÅÏ¢±í 
        
        createTb_DevData_RtTable(conn_pool);
        
        
    }
    
    /**
       *     ´´½¨Á¬½Ó״̬±í
     * @param conn_pool
     */
    public void createTb_DevData_RtTable(MysqlConnPool conn_pool) {
        String sql_str = " CREATE TABLE IF NOT EXISTS "+Sql_Mysql.Tb_DevData_RtTable+" ( " + 
                "  num bigint(20) NOT NULL AUTO_INCREMENT," + 
                "  dev_id int(11) NOT NULL DEFAULT '0'," + 
                "  record_time datetime NOT NULL DEFAULT '2000-01-01 00:00:00'," + 
                "  note varchar(255) NOT NULL DEFAULT ''," + 
                "  PRIMARY KEY (`num`)," + 
                "  UNIQUE KEY `index_dev_id` (`dev_id`) USING BTREE" + 
                ") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
        
        Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn());
        try {
            sql.sqlMysqlExecute(sql_str);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }
    }
    
    /**
     *     ²åÈëÉ豸״̬¼Ç¼±í
     */
    public static void insertOrUpdateTb_DevData_RtTable(MysqlConnPool conn_pool,List<Device_inf> devices) {
        String sql_str = " REPLACE INTO " + Sql_Mysql.Tb_DevData_RtTable + "(dev_id,record_time) values ";
        for(int i = 0;i<devices.size();i++) {
            if(i>0) {
                sql_str += ",";
            }
            sql_str += "("+devices.get(i).dev_id+",'"+Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms)+"')";
        }
        Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn());
        try {
            sql.sqlMysqlExecute(sql_str);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }        
    }
 
    /**
     *     ¸üÐÂÉ豸µÄ¼Ç¼ʱ¼ä
     * @param conn_pool
     * @param dev
     */
    public static void updateDeviceDate(MysqlConnPool conn_pool,Device_inf dev) {
        String sql_str = " UPDATE "+Sql_Mysql.Tb_DevData_RtTable+" SET record_time = '"+Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms)+"' WHERE dev_id = " + dev.dev_id;
        Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn());
        try {
            sql.sqlMysqlExecute(sql_str);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }
        
    }
    
    
    /**
     *     ²åÈëÉ豸ÐÅÏ¢
     * @param conn_pool
     */
    public void insertTb_device_inf(MysqlConnPool conn_pool,List<Device_inf> devices) {
        String sql_str = " INSERT INTO " + Sql_Mysql.Tb_Device_InfTable + "(dev_name,dev_id,sys_type,sys_name,analog_count,switch_count,state_count,param_count,note) VALUES";
        Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn());
        Device_inf device = null;
        //System.out.println("===="+max_dev_id+"====");
        int count = 0;
        for(int i=0;i<devices.size();i++) {
            device = devices.get(i);
            Device_inf temp = isExistsDevice(conn_pool, device.getDev_id());
            if(null != temp) {
                device.setDev_id(temp.getDev_id());
                device.setConfigsDevId(temp.getDev_id());
                try {
                    //µ±Ç°É豸ÒÑ´æÔÚ£»¸üе±Ç°É豸Öи÷¸ö±äÁ¿ÔÚÊý¾Ý¿âÖеÄÊýÁ¿
                    String sql_str_update = " update " +Sql_Mysql.Tb_Device_InfTable
                            + " SET analog_count="+device.getAnalog_count()+","
                            + " switch_count="+device.getSwitch_count()+","
                            + " state_count="+device.getState_count()+","
                            + " param_count="+device.data_count+","
                            + " sys_type = '"+device.sys_type+"',"
                            + " sys_name = '"+device.sys_name+"',"
                            + " note='' "
                            + " WHERE dev_id = '"+device.getDev_id()+"';";
                    if(device.dev_id < 2000) {
                        //×Óվϵͳ
                        for(int k = 0;k<device.datamean.size();k++) {
                            sql_str_update = " update " +Sql_Mysql.Tb_Device_InfTable
                                    + " SET analog_count="+device.getAnalog_count()+","
                                    + " switch_count="+device.getSwitch_count()+","
                                    + " state_count="+device.getState_count()+","
                                    + " param_count="+device.data_count+","
                                    + " sys_type = '"+device.sys_type+"',"
                                    + " sys_name = '"+device.sys_name+"',"
                                    + " match_type = "+device.datamean.get(k).match_type+","
                                    + " match_str = '"+device.datamean.get(k).match_str+"',"
                                    + " note='' "
                                    + " WHERE dev_name = '"+device.datamean.get(k).dev_name+"' AND dev_id = "+device.dev_id;
                            sql.sqlMysqlExecute(sql_str_update);
                        }
                    }else {                        
                        //System.out.println(sql_str_update);
                        sql.sqlMysqlExecute(sql_str_update);
                    }
                
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    
                }
                continue;
            }
            
            if(count > 0) {
                sql_str += ",";
            }
            if(device.dev_id < 2000) {
                //×Óվϵͳ
                for(int k = 0;k<device.datamean.size();k++) {
                    if(count > 0 && k >0) {
                        sql_str += ",";
                    }
                    sql_str += "("
                            + "'" + device.datamean.get(k).dev_name + "',"
                            + "" + device.datamean.get(k).dev_id + ","
                            + "" + device.getSys_type() + ","
                            + "'" + device.getSys_name() + "',"
                            + "" + device.getAnalog_count() + ","
                            + "" + + device.getSwitch_count() + ","
                            + "" + + device.getState_count() + ","
                            + "" + + device.data_count + ","
                            + "''"
                            + ")";    
                    count++;
                }
            }else {                
                sql_str += "("
                        + "'" + device.getDev_name() + "',"
                        + "" + device.getDev_id() + ","
                        + "" + device.getSys_type() + ","
                        + "'" + device.getSys_name() + "',"
                        + "" + device.getAnalog_count() + ","
                        + "" + + device.getSwitch_count() + ","
                        + "" + + device.getState_count() + ","
                        + "" + + device.data_count + ","
                        + "''"
                        + ")";
            }
            count ++;
        }
        try {
            if(count > 0) {
                System.out.println(sql_str);
                sql.sqlMysqlExecute(sql_str);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }
    }
    
    /**
     *     ´´½¨É豸ÐÅÏ¢±í
     * @param conn_pool
     */
    public void CreateTb_device_inf(MysqlConnPool conn_pool) {
        String sql_str1 = " CREATE TABLE IF NOT EXISTS  " +Sql_Mysql.Tb_Device_InfTable+ " (" + 
                "  num bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'Ö÷¼ü'," + 
                "  dev_name varchar(126) NOT NULL DEFAULT '' COMMENT 'É豸ÀàÐÍ'," + 
                "  dev_id int(11) NOT NULL DEFAULT '1' COMMENT 'É豸id'," + 
                "  sys_type int(11) NOT NULL DEFAULT '0' COMMENT 'ϵͳÀàÐÍ'," + 
                "  sys_name varchar(64) NOT NULL DEFAULT '' COMMENT 'ϵͳÃû³Æ'," +
                "  match_type int(11) NOT NULL DEFAULT '0' COMMENT 'Æ¥ÅäÀàÐÍ0:ÎÞ¹æÔò;1:µÈÓÚÆ¥Å䣻2²»µÈÓÚÆ¥Åä'," + 
                "  match_str  varchar(64) NOT NULL DEFAULT '' COMMENT 'Æ¥Åä×Ö·û´®'," + 
                "  analog_count int(11) NOT NULL DEFAULT '0' COMMENT 'Ä£ÄâÁ¿ÊýÁ¿'," + 
                "  switch_count int(11) NOT NULL DEFAULT '0' COMMENT '¿ª¹ØÁ¿ÊýÁ¿'," + 
                "  state_count int(11) NOT NULL DEFAULT '0' COMMENT '״̬Á¿ÊýÁ¿'," + 
                "  param_count int(11) NOT NULL DEFAULT '0' COMMENT '²ÎÊý×ÜÊýÁ¿'," + 
                "  note varchar(255) NOT NULL DEFAULT ''," +
                "  dev_str varchar(32) NOT NULL DEFAULT ''," + 
                "  PRIMARY KEY (`num`)" + 
                ") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;";
        
        Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn());
        ResultSet res = null;
        try {
            sql.sqlMysqlExecute(sql_str1);
            //Ìí¼ÓΨһË÷Òý
            res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
                    + " WHERE table_schema='db_equipstand'"
                    + " AND table_name='tb_device_inf'"
                    + " AND column_name='param_count'");
            if(false == res.next()) {
                sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Tb_Device_InfTable
                                + " ADD COLUMN param_count int(11) NOT NULL DEFAULT '0' COMMENT '²ÎÊý×ÜÊýÁ¿';");
            }
            //Ìí¼ÓÆ¥Å乿Ôò
            res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
                    + " WHERE table_schema='db_equipstand'"
                    + " AND table_name='tb_device_inf'"
                    + " AND column_name='match_type'");
            if(false == res.next()) {
                sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Tb_Device_InfTable
                                + " ADD COLUMN match_type int(11) NOT NULL DEFAULT '0' COMMENT 'Æ¥ÅäÀàÐÍ0:ÎÞ¹æÔò;1:µÈÓÚÆ¥Å䣻2²»µÈÓÚÆ¥Åä';");
            }
            
            //Ìí¼ÓÆ¥Åä×Ö·û´®
            res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
                    + " WHERE table_schema='db_equipstand'"
                    + " AND table_name='tb_device_inf'"
                    + " AND column_name='match_str'");
            if(false == res.next()) {
                sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Tb_Device_InfTable
                                + " ADD COLUMN match_str  varchar(64) NOT NULL DEFAULT '' COMMENT 'Æ¥Åä×Ö·û´®';");
            }
            
            //Ìí¼Ó±êʶ×Ö·û´®
            res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
                    + " WHERE table_schema='db_equipstand'"
                    + " AND table_name='tb_device_inf'"
                    + " AND column_name='dev_str'");
            if(false == res.next()) {
                sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Tb_Device_InfTable
                                + " ADD COLUMN dev_str varchar(32) NOT NULL DEFAULT '';");
            }
        } catch (SQLException e) {
            //e.printStackTrace();
        } finally {
            if(res != null) {
                try {
                    res.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            sql.close_con();
        }
    }
    
    /**
     *     ´´½¨db_equipstandÊý¾Ý¿â
     * @param conn_pool
     */
    public void CreateDb_Equipstand(MysqlConnPool conn_pool) {
        Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn());
        try {
            sql.sqlMysqlExecute("CREATE DATABASE IF NOT EXISTS " + Sql_Mysql.DB_Equipstand);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }
    }
    
    
    /**
     *     ´´½¨db_equip_history ÀúʷʵʱÊý¾Ý¿â
     * @param conn_pool
     */
    public void CreateDb_Equip_History(MysqlConnPool conn_pool) {
        Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn());
        try {
            sql.sqlMysqlExecute("CREATE DATABASE IF NOT EXISTS " + Sql_Mysql.DB_Equip_History);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }
    }
    
    /**
     *     ´´½¨db_equip_history É豸¸æ¾¯Êý¾Ý¿â
     * @param conn_pool
     */
    public void CreateDb_Equip_Alarm(MysqlConnPool conn_pool) {
        Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn());
        try {
            sql.sqlMysqlExecute("CREATE DATABASE IF NOT EXISTS " + Sql_Mysql.DB_Equip_Alarm);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }
    }
    
    /**
     *     Åжϵ±Ç°É豸ÊÇ·ñÔÚÊý¾Ý¿âÖдæÔÚ
     * @param conn_pool
     * @param dev_name
     * @return    
     */
    public Device_inf isExistsDevice(MysqlConnPool conn_pool,int dev_id) {
        String sql_str = " SELECT * FROM db_equipstand.tb_device_inf  WHERE dev_id = '" + dev_id + "';";
        Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn());
        ResultSet res = sql.sqlMysqlQuery(sql_str);
        Device_inf dev = null;
        try {
            if(res.next()) {
                dev = new Device_inf();
                dev.dev_id = res.getInt("dev_id");            //É豸ID
                
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if(res != null) {
                try {
                    res.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            sql.close_con();
        }
        return dev;
    }
    
    
    /**
     *     »ñÈ¡Êý¾Ý¿âÖÐ×î´óµÄÉ豸ID+1
     * @param conn_pool
     * @return
     */
    public int getMaxDev_Id(MysqlConnPool conn_pool) {
        int max_dev_id = 1001;
        String sql_str = " SELECT MAX(dev_id) as dev_id FROM " + Sql_Mysql.Tb_Device_InfTable;
        Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn());
        ResultSet res = sql.sqlMysqlQuery(sql_str);
        try {
            if(res.next()) {
                int dev_id = res.getInt("dev_id");
                if(dev_id > 0) {
                    max_dev_id = dev_id+1;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if(res != null) {
                try {
                    res.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                sql.close_con();
            }
        }
        return max_dev_id;
    }
    
    /**
     *     »ñÈ¡Êý¾Ý¿âÖÐ×î´óµÄÉ豸ID+1
     * @param conn_pool
     * @return
     */
    public int getMaxSys_type(MysqlConnPool conn_pool) {
        int max_sys_type = 1;
        String sql_str = " SELECT MAX(sys_type) as sys_type FROM " + Sql_Mysql.Tb_Device_InfTable;
        Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn());
        ResultSet res = sql.sqlMysqlQuery(sql_str);
        try {
            if(res.next()) {
                max_sys_type = res.getInt("sys_type")+1;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if(res != null) {
                try {
                    res.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                sql.close_con();
            }
        }
        return max_sys_type;
    }
 
    /**
     *     ´´½¨É豸ʵʱ״̬±í,ÒÔ¼°´´½¨×´Ì¬¶ÔÓ¦±í
     * @param gB_MysqlConnPool
     * @param devices
     */
    public void initRtDataCfgTable(MysqlConnPool conn_pool, List<Device_inf> devices) {
        for(int i=0;i<devices.size();i++) {
            Device_inf dev = devices.get(i);
            //´´½¨É豸״̬±í
            String sql_str = "CREATE TABLE IF NOT EXISTS "+Sql_Mysql.Tb_DevData_Rt+dev.dev_id+" (" + 
                    "  num bigint(20) NOT NULL AUTO_INCREMENT," + 
                    "  dev_id int NOT NULL DEFAULT '"+dev.dev_id+"'," + 
                    "  data_type int NOT NULL DEFAULT '0'," + 
                    "  param_count int NOT NULL DEFAULT '0'," + 
                    "  record_time datetime NOT NULL DEFAULT '2000-01-01 00:00:00',"; 
                    
            for(int j=0;j<dev.cfgdatas.size();j++) {
                sql_str += " state"+(j+1)+" float NOT NULL DEFAULT '"+dev.cfgdatas.get(j).defalutval+"',";
            }
            sql_str += "  PRIMARY KEY (`num`)" + 
                    ") ENGINE=myisam ROW_FORMAT=DYNAMIC CHARSET=utf8;";
            //´´½¨ÅäÖÃÐÅÏ¢±í
            String sql_str_cfg = " CREATE TABLE IF NOT EXISTS " +Sql_Mysql.Tb_DevData_Rt_Cfg+ "(" + 
                    "  num bigint(20) NOT NULL AUTO_INCREMENT," + 
                    "  dev_id int NOT NULL DEFAULT '1001'," + 
                    "  data_type int NOT NULL DEFAULT '1'," + 
                    "  param_count int NOT NULL DEFAULT '0'," + 
                    "  record_time varchar(20) NOT NULL DEFAULT '2000-01-01 00:00:00'," + 
                    "  PRIMARY KEY (`num`)" + 
                    ") ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;";
            Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn());
            ResultSet res = null;
            try {
                //´´½¨É豸״̬±í(ÒÔ¼°²¹È«×Ö¶Î)
                sql.sqlMysqlExecute(sql_str);
                //´´½¨É豸ÅäÖÃÐÅÏ¢±í
                sql.sqlMysqlExecute(sql_str_cfg);
                //Ìí¼Ódata_type×Ö¶Î
                res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
                        + " WHERE table_schema='db_equipstand'"
                        + " AND table_name='tb_devdata_rt_"+(dev.dev_id)+"'"
                        + " AND column_name='data_type'");
                if(false == res.next()) {
                    sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Tb_DevData_Rt+dev.dev_id
                                    + " ADD COLUMN data_type int NOT NULL DEFAULT '0'  after dev_id");
                }
                res = null;
                //Ìí¼Ódata_type×Ö¶Î
                res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
                        + " WHERE table_schema='db_equipstand'"
                        + " AND table_name='tb_devdata_rt_"+(dev.dev_id)+"'"
                        + " AND column_name='param_count'");
                if(false == res.next()) {
                    sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Tb_DevData_Rt+dev.dev_id
                                    + " ADD COLUMN param_count int NOT NULL DEFAULT '0'  after data_type");
                }
                for(int j=0;j<dev.cfgdatas.size();j++) {
                    res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
                            + " WHERE table_schema='db_equipstand'"
                            + " AND table_name='tb_devdata_rt_"+(dev.dev_id)+"'"
                            + " AND column_name='state"+(j+1)+"'");
                    if(false == res.next()) {
                        sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Tb_DevData_Rt+dev.dev_id
                                        + " ADD COLUMN `state"+(j+1)+"` float DEFAULT '"+dev.cfgdatas.get(j).defalutval+"'");
                    }
                }
                res = sql.sqlMysqlQuery("SELECT dev_id FROM "+Sql_Mysql.Tb_DevData_Rt+dev.dev_id+" WHERE dev_id = '" + dev.dev_id + "' AND data_type = 2");
                if(!res.next()) {
                    //²»´æÔÚ״̬ÐÅϢʱ²åÈëĬÈÏÊý¾Ý
                    sql.sqlMysqlExecute("INSERT INTO "+Sql_Mysql.Tb_DevData_Rt+dev.dev_id+"(data_type,param_count,dev_id,record_time) VALUES('2','"+dev.data_count+"',"+dev.dev_id+",'"+Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms)+"');");;
                }
                
                
                /******¿ªÊ¼´´½¨ÅäÖñí******************************************************************************************************************************/
                //Ìí¼Ódata_type×Ö¶Î
                res = sql.sqlMysqlQuery(" SELECT * FROM information_schema.columns "
                        + " WHERE table_schema='db_equipstand'"
                        + " AND table_name='tb_devdata_rt_cfg'"
                        + " AND column_name='data_type'");
                if(false == res.next()) {
                    sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Tb_DevData_Rt_Cfg
                                    + " ADD COLUMN data_type int NOT NULL DEFAULT '0'  after dev_id");
                }
                res = null;
                //Ìí¼Ódata_type×Ö¶Î
                res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
                        + " WHERE table_schema='db_equipstand'"
                        + " AND table_name='tb_devdata_rt_cfg'"
                        + " AND column_name='param_count'");
                if(false == res.next()) {
                    sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Tb_DevData_Rt_Cfg
                                    + " ADD COLUMN param_count int NOT NULL DEFAULT '0'  after data_type");
                }
                for(int j=0;j<dev.cfgdatas.size();j++) {
                    res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
                            + " WHERE table_schema='db_equipstand'"
                            + " AND table_name='tb_devdata_rt_cfg'"
                            + " AND column_name='state"+(j+1)+"'");
                    if(false == res.next()) {
                        String data_type = "varchar(30)";
                        if(j>400) {
                            data_type = "varchar(15)";
                        }
                        
                        sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Tb_DevData_Rt_Cfg
                                        + " ADD COLUMN `state"+(j+1)+"` "+data_type+" NOT NULL DEFAULT ''");
                    }
                }
                
                
                
                //´´½¨É豸״̬±í(ÒÔ¼°²¹È«×Ö¶Î)
                res = sql.sqlMysqlQuery(" SELECT dev_id FROM "+Sql_Mysql.Tb_DevData_Rt_Cfg+" WHERE dev_id = '" + dev.dev_id + "' AND data_type = " + ChildStoodParse_Thread.Config_DataType);
                if(res.next()) {
                    //´æÔÚÅäÖÃ(¸üÐÂÅäÖÃÐÅÏ¢)
                    sql_str = " UPDATE " + Sql_Mysql.Tb_DevData_Rt_Cfg +
                              " SET record_time = '¼Ç¼ʱ¼ä' "+",param_count='"+dev.data_count+"'";
                    for(int k = 0;k<dev.cfgdatas.size();k++) {
                        ConfigData cfg = dev.cfgdatas.get(k);
                        sql_str += ",state"+(k+1)+"='"+dev.cfgdatas.get(k).data_value+"&&"+cfg.vartype+(cfg.vartype == 1?"&&"+cfg.unit:"")+"'";
                    }
                    sql.sqlMysqlExecute(sql_str+" WHERE dev_id = " + dev.dev_id + " AND data_type = "+ChildStoodParse_Thread.Config_DataType);
                }else {
                    //²»´æÔÚÅäÖÃÐÅÏ¢
                    sql_str = "INSERT INTO "+Sql_Mysql.Tb_DevData_Rt_Cfg+"(data_type,param_count,dev_id,record_time";
                    String sql_str_end = " VALUES('1','"+dev.data_count+"','"+dev.dev_id+"','¼Ç¼ʱ¼ä'";
                    for(int k = 0;k<dev.cfgdatas.size();k++) {
                        ConfigData cfg = dev.cfgdatas.get(k);
                        sql_str += ",state"+(k+1);
                        sql_str_end +=",'"+dev.cfgdatas.get(k).data_value+"&&"+cfg.vartype+(cfg.vartype == 1?"&&"+cfg.unit:"")+"'";
                    }
                    sql.sqlMysqlExecute(sql_str+") "+sql_str_end+");");                    
                }    
                
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                if(res != null) {
                    try {
                        res.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                sql.close_con();
            }
        }        
    }
    
    
    /**
     *     ´´½¨É豸ʵʱ״̬±í,ÒÔ¼°´´½¨×´Ì¬¶ÔÓ¦±í
     * @param gB_MysqlConnPool
     * @param devices
     */
    public void initRtDataTable(MysqlConnPool conn_pool, List<Device_inf> devices) {
        for(int i=0;i<devices.size();i++) {
            Device_inf dev = devices.get(i);
            //System.err.println(dev.dev_id+":"+dev.cfgdatas.size());
            String sql_str = "CREATE TABLE IF NOT EXISTS "+Sql_Mysql.Tb_DevData_Rt+dev.dev_id+" (" + 
                    "  num bigint(20) NOT NULL AUTO_INCREMENT," + 
                    "  dev_id int NOT NULL DEFAULT '"+dev.dev_id+"'," + 
                    "  record_time varchar(20) NOT NULL DEFAULT '2000-01-01 00:00:00',"; 
                    
            for(int j=0;j<dev.cfgdatas.size();j++) {
                sql_str += " state"+(j+1)+" varchar(8) NOT NULL DEFAULT '"+dev.cfgdatas.get(j).defalutval+"',";
            }
            sql_str += "  PRIMARY KEY (`num`)" + 
                    ") ENGINE=myisam ROW_FORMAT=DYNAMIC CHARSET=utf8;";
            Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn());
            ResultSet res = null;
            try {
                //´´½¨É豸״̬±í(ÒÔ¼°²¹È«×Ö¶Î)
                sql.sqlMysqlExecute(sql_str);
                for(int j=0;j<dev.cfgdatas.size();j++) {
                    res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
                            + " WHERE table_schema='db_equipstand'"
                            + " AND table_name='tb_devdata_rt_"+(dev.dev_id)+"'"
                            + " AND column_name='state"+(j+1)+"'");
                    if(false == res.next()) {
                        sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Tb_DevData_Rt+dev.dev_id
                                        + " ADD COLUMN `state"+(j+1)+"` varchar(8) DEFAULT '"+dev.cfgdatas.get(j).defalutval+"'");
                    }
                    
                }
                res = sql.sqlMysqlQuery("SELECT dev_id FROM "+Sql_Mysql.Tb_DevData_Rt+dev.dev_id+" WHERE dev_id = '" + dev.dev_id + "'");
                if(!res.next()) {
                    //²»´æÔÚʱ²åÈëĬÈÏÊý¾Ý
                    sql.sqlMysqlExecute("INSERT INTO "+Sql_Mysql.Tb_DevData_Rt+dev.dev_id+"(dev_id,record_time) VALUES("+dev.dev_id+",'"+Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms)+"');");;
                }
                
                /******¿ªÊ¼´´½¨ÅäÖñí******************************************************************************************************************************/
                sql_str = "CREATE TABLE IF NOT EXISTS "+Sql_Mysql.Tb_DevData_Rt_Cfg+dev.dev_id+" (" + 
                        "  num bigint(20) NOT NULL AUTO_INCREMENT," + 
                        "  dev_id varchar(12) NOT NULL DEFAULT '"+dev.dev_id+"'," + 
                        "  record_time varchar(12) NOT NULL DEFAULT '¼Ç¼ʱ¼ä',"; 
                        
                for(int j=0;j<dev.cfgdatas.size();j++) {
                    sql_str += "  state"+(j+1)+" varchar(20) NOT NULL DEFAULT '"+dev.cfgdatas.get(j).data_value+"',";
                }
                sql_str += "  PRIMARY KEY (`num`)" + 
                        ") ENGINE=myisam DEFAULT CHARSET=utf8;";
                //´´½¨É豸״̬±í(ÒÔ¼°²¹È«×Ö¶Î)
                sql.sqlMysqlExecute(sql_str);
                for(int j=0;j<dev.cfgdatas.size();j++) {
                    res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
                            + " WHERE table_schema='db_equipstand'"
                            + " AND table_name='tb_devdata_rt_cfg_"+(dev.dev_id)+"'"
                            + " AND column_name='state"+(j+1)+"'");
                    if(false == res.next()) {
                        sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Tb_DevData_Rt_Cfg+dev.dev_id
                                        + " ADD COLUMN `state"+(j+1)+"` varchar(32) DEFAULT '"+dev.cfgdatas.get(j).data_value+"'");
                    }
                    
                }
                res = sql.sqlMysqlQuery("SELECT dev_id FROM "+Sql_Mysql.Tb_DevData_Rt_Cfg+dev.dev_id+" WHERE dev_id = '" + dev.dev_id + "'");
                if(res.next()) {
                    //´æÔÚÅäÖÃ(¸üÐÂÅäÖÃÐÅÏ¢)
                    sql_str = " UPDATE " + Sql_Mysql.Tb_DevData_Rt_Cfg + dev.dev_id +
                              " SET record_time = '¼Ç¼ʱ¼ä' ";
                    for(int k = 0;k<dev.cfgdatas.size();k++) {
                        sql_str += ",state"+(k+1)+"='"+dev.cfgdatas.get(k).data_value+"'";
                    }
                    sql.sqlMysqlExecute(sql_str+" WHERE dev_id = " + dev.dev_id);
                }else {
                    //²»´æÔÚÅäÖÃÐÅÏ¢
                    sql_str = " INSERT INTO " + Sql_Mysql.Tb_DevData_Rt_Cfg + dev.dev_id + "(dev_id,record_time";
                    String sql_str_end = " VALUES('"+dev.dev_id+"','¼Ç¼ʱ¼ä'";
                    for(int k = 0;k<dev.cfgdatas.size();k++) {
                        sql_str += ",state"+(k+1);
                        sql_str_end +=",'"+dev.cfgdatas.get(k).data_value+"'";
                    }
                    sql.sqlMysqlExecute(sql_str+") "+sql_str_end+");");                    
                }                
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                if(res != null) {
                    try {
                        res.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                sql.close_con();
            }
        }        
    }    
    
    /**
     *     ´´½¨ÀúʷʵʱÊý¾Ý±í
     * @param conn_pool
     * @param devices
     */
    public static void createDevData_HistoryTable(MysqlConnPool conn_pool,Device_inf dev,String date_str) {
        String tname = "tb_devdata_history_" + date_str + "_" + dev.dev_id;
        String sql_str = " CREATE TABLE IF NOT EXISTS " + Sql_Mysql.Tb_DevData_historyTable+date_str + "_" + dev.dev_id + "(" + 
                    "  `num` bigint(20) NOT NULL AUTO_INCREMENT," + 
                    "  `dev_id` int NOT NULL DEFAULT '1004'," + 
                    "  `data_type` int NOT NULL DEFAULT '0'," + 
                    "  `param_count` int NOT NULL DEFAULT '0'," + 
                    "  `record_time` datetime NOT NULL DEFAULT '1970-01-01 00:00:00'," + 
                    "  PRIMARY KEY (`num`)" + 
                ") ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC; ";
        Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn());
        ResultSet res = null;
        try {
            sql.sqlMysqlExecute(sql_str);
            //²¹È«ËùÓÐ×Ö¶Î
            sql_str = "";
            int colcount = 0;
            sql_str += "ALTER TABLE " + Sql_Mysql.Tb_DevData_historyTable+date_str + "_" + dev.dev_id;
            for(int j=0;j<dev.cfgdatas.size();j++) {
                res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
                        + " WHERE table_schema='db_equip_history'"
                        + " AND table_name='"+tname+"'"
                        + " AND column_name='state"+(j+1)+"'");
                if(false == res.next()) {
                    if(colcount > 0) {
                        sql_str += ",";
                    }
                    sql_str += " ADD COLUMN `state"+(j+1)+"` float NOT NULL DEFAULT '"+dev.cfgdatas.get(j).defalutval+"'";
                    colcount ++;
                    //sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Tb_DevData_historyTable+date_str + "_" + dev.dev_id
                    //    + " ADD COLUMN `state"+(j+1)+"` float DEFAULT '"+dev.cfgdatas.get(j).defalutval+"';");
                }
            }
            if(colcount > 0) {
                //System.out.println(sql_str);
                sql.sqlMysqlExecute(sql_str);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if(res != null) {
                try {
                    res.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            sql.close_con();
        }
    }
    
    /**
     *     Åжϵ±Ç°ÀúʷʵʱÊý¾Ý±íÊÇ·ñ´æÔÚ
     * @param conn_pool
     * @param dev_id
     * @param date
     * @return    true:ÒÑ´æÔÚ    false:²»´æÔÚ 
     */
    public static boolean checkHistoryIfNotExistsTable(MysqlConnPool conn_pool,int dev_id,Date date) {
        Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn());
        String tname;
        try {
            sql.sqlMysqlUseDB(Sql_Mysql.DB_Equip_History);
            tname = Sql_Mysql.Tb_DevData_historyTable + Com.getDateTimeFormat(date, Com.DTF_Y_M_D) + "_" + dev_id;
            return sql.sqlMysqlCheckIfTableExist(tname);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }
        return false;
    }
    
    /**
     *     ´´½¨É豸ʵʱºÍÀúÊ·¸æ¾¯¼Ç¼±í
     * @param conn_pool
     * @param device
     */
    public static void createDevAlarmDataTable(MysqlConnPool conn_pool,Device_inf device) {
        String sql_str_alarm = " CREATE TABLE IF NOT EXISTS " +Sql_Mysql.Tb_DevAlarm_Data+device.dev_id+ " (" + 
                "  num bigint(20) NOT NULL AUTO_INCREMENT," + 
                "  dev_id int(11) NOT NULL DEFAULT '0' COMMENT 'É豸id'," + 
                "  alarm_name varchar(24) NOT NULL DEFAULT '' COMMENT '×Ö¶ÎÃû³Æ'," + 
                "  alarm_value float NOT NULL DEFAULT '0' COMMENT '¸æ¾¯Öµ'," + 
                "  alarm_type int(11) NOT NULL DEFAULT '0' COMMENT '¸æ¾¯ÀàÐÍ'," + 
                "  alarm_starttime datetime NOT NULL DEFAULT '2000-01-01 00:00:00' COMMENT '¸æ¾¯¿ªÊ¼Ê±¼ä'," + 
                "  alarm_endtime datetime NOT NULL DEFAULT '2000-01-01 00:00:00' COMMENT '¸æ¾¯½áÊøÊ±¼ä'," + 
                "  alarm_index int(11) NOT NULL DEFAULT '0' COMMENT '¸æ¾¯Ï±ê,¶ÔÓ¦stateX'," + 
                "  note varchar(24) NOT NULL DEFAULT '' COMMENT '±¸ÓÃ×Ö¶Î'," + 
                "  PRIMARY KEY (`num`)," +
                "  KEY `index_dev_id` (`dev_id`) USING BTREE " + 
                ") ENGINE=InnoDB DEFAULT CHARSET=utf8;";            //¸æ¾¯ÊµÊ±¼Ç¼±í
        String str_sql_alm_his = "CREATE TABLE IF NOT EXISTS " +Sql_Mysql.Tb_DevAlarm_Data_History+device.dev_id+ " (" + 
                "  num bigint(20) NOT NULL AUTO_INCREMENT," + 
                "  dev_id int(11) NOT NULL DEFAULT '0' COMMENT 'É豸id'," + 
                "  alarm_name varchar(24) NOT NULL DEFAULT '' COMMENT '×Ö¶ÎÃû³Æ'," + 
                "  alarm_value float NOT NULL DEFAULT '0' COMMENT '¸æ¾¯Öµ'," + 
                "  alarm_type int(11) NOT NULL DEFAULT '0' COMMENT '¸æ¾¯ÀàÐÍ'," + 
                "  alarm_starttime datetime NOT NULL DEFAULT '2000-01-01 00:00:00' COMMENT '¸æ¾¯¿ªÊ¼Ê±¼ä'," + 
                "  alarm_endtime datetime NOT NULL DEFAULT '2000-01-01 00:00:00' COMMENT '¸æ¾¯½áÊøÊ±¼ä'," + 
                "  alarm_index int(11) NOT NULL DEFAULT '0' COMMENT '¸æ¾¯Ï±ê,¶ÔÓ¦stateX'," + 
                "  note varchar(24) NOT NULL DEFAULT '' COMMENT '±¸ÓÃ×Ö¶Î'," + 
                "  PRIMARY KEY (`num`)," + 
                "  KEY `index_dev_id` (`dev_id`) USING BTREE " + 
                ") ENGINE=InnoDB DEFAULT CHARSET=utf8;";            //¸æ¾¯ÀúÊ·¼Ç¼±í
        
        Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn());
        try {
            //System.out.println(sql_str_alarm);
            sql.sqlMysqlExecute(sql_str_alarm);
            sql.sqlMysqlExecute(str_sql_alm_his);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }
    }
    
}