whyclj
2020-09-30 a4bd287cd7e43b1920fc0b44b0af5cd0172cdfc6
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
package com.dev.restartplan;
 
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import com.base.Com;
import com.sql.MysqlConnPool;
import com.sql.Sql_Mysql;
 
public class Dev_Restart_Plan_Thread_SQL {
    
    public static void init(MysqlConnPool pool) {
        
        //´´½¨Éè±¸ÖØÆô¼Æ»®±í
        createDev_Restart_PlanTable(pool);
        
        
    }
    
    
    /**
     * 
     * ´´½¨Éè±¸ÖØÆô¼Æ»®±í
     * @param pool
     * @param recreate
     */
    public static void createDev_Restart_PlanTable(MysqlConnPool pool) {
        String sql_str = "CREATE TABLE IF NOT EXISTS " + Sql_Mysql.Dev_Restart_Plan_Table + " (" + 
                "  num bigint(20) NOT NULL AUTO_INCREMENT," + 
                "  dev_id int(11) NOT NULL DEFAULT '0' COMMENT 'É豸id'," + 
                "  restart_starttime datetime NOT NULL DEFAULT '2000-01-01 00:00:00' COMMENT 'ÖØÆô¿ªÊ¼Ê±¼ä'," + 
                "  last_restarttime datetime NOT NULL DEFAULT '2000-01-01 00:00:00' COMMENT 'ÉÏÒ»´ÎÖØÆôʱ¼ä'," + 
                "  restart_cycle int(11) NOT NULL DEFAULT '1' COMMENT 'ÖØÆôÖÜÆÚ'," + 
                "  restart_en int(11) NOT NULL DEFAULT '0' COMMENT 'ÖØÆôʹÄÜ  0:²»ÆôÓࣻ1:ÆôÓÃ'," + 
                "  note varchar(255) NOT NULL DEFAULT '' COMMENT '±¸ÓÃ'," + 
                "  PRIMARY KEY (`num`)," + 
                "  UNIQUE KEY `index_dev_id` (`dev_id`) USING BTREE" + 
                ") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        ResultSet res =null;
        try {
            sql.sqlMysqlExecute(sql_str);
            
            //Ìí¼ÓÉÏÒ»´ÎÖØÆôʱ¼ä×Ö¶Î
            res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
                    + " WHERE table_schema='web_site'"
                    + " AND table_name='tb_dev_restart_plan'"
                    + " AND column_name='last_restarttime'");
            if(false == res.next()) {
                sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.Dev_Restart_Plan_Table 
                        + " ADD COLUMN last_restarttime datetime NOT NULL DEFAULT '2000-01-01 00:00:00' COMMENT 'ÉÏÒ»´ÎÖØÆôʱ¼ä';");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }
    }
    
    /**
     * ²éѯÓÐЧµÄÖØÆô¼Æ»®Êý¾Ý
     * @param pool
     * @return
     */
    public static List<Dev_Restart_Plan> queryEffectRestartPlan(MysqlConnPool pool){
        String sql_str = " SELECT * FROM " + Sql_Mysql.Dev_Restart_Plan_Table +
                         " WHERE restart_en =1 AND dev_id in(" + 
                            "SELECT FBSDeviceId FROM " + Sql_Mysql.BattInf_Table + 
                         ")";
        List<Dev_Restart_Plan> plans = new ArrayList<Dev_Restart_Plan>();
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        ResultSet res = null;
        try {
            res = sql.sqlMysqlQuery(sql_str);
            Dev_Restart_Plan plan;
            while(res.next()) {
                plan = new Dev_Restart_Plan();
                plan.setDev_id(res.getInt("dev_id"));
                plan.setRestart_cycle(res.getInt("restart_cycle"));
                plan.setRestart_starttime(res.getTimestamp("restart_starttime"));
                plan.setLast_restarttime(res.getTimestamp("last_restarttime"));
                plan.setRestart_en(res.getInt("restart_en"));
                
                long timelong = calculTimelong(plan.restart_starttime, new Date());
                if(timelong < 0) {
                    //¼Æ»®¿ªÊ¼Ê±¼äÉÐδµ½,Ìø¹ýµ±Ç°¼Æ»®
                    continue;
                }else {
                    long lastlong = calculTimelong(plan.last_restarttime, new Date());
                    //Ö´ÐÐÖÐµÄ·Åµç¼Æ»®£¨°üº¬ÒѾ­Íê³ÉµÄ·Åµç·Åµç¼Æ»®£©
                    int day_count =(int) timelong / (60*60*24);                //ºÍ¼Æ»®Ê±¼ä¿ªÊ¼Ïà²î¶àÉÙÌì
                    int sec_count = (int) timelong%(60*60*24);                //ºÍ¼Æ»®Ê±¼äÏà²îÃëÊý
                    int circle = day_count / plan.restart_cycle;
                    int remain = day_count % plan.restart_cycle;
                    
                    //Ïà¸ôÌìÊýΪÖÜÆÚµÄÕûÊý±¶£¬²¢ÇÒÔÚ2·ÖÖÓÄÚʱ;²¢ÇÒ2·ÖÖÓÄÚûÓÐÖØÆô³É¹¦¹ýÉ豸
                    if(remain == 0 && sec_count <120 && lastlong > 120) {
                        plans.add(plan);
                    }                    
                }
                
            }            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(res != null) {
                try {
                    res.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            sql.close_con();
        }        
        return plans;
    }
    
    /**
     *     ¸üе±Ç°Éè±¸ÖØÆô¼Æ»®ÉÏÒ»´ÎÖØÆôʱ¼ä
     * @param pool
     * @param plans
     */
    public static void updateRestartPlan(MysqlConnPool pool, List<Dev_Restart_Plan> plans) {
        if(plans.size() > 0) {
            String sql_str = "";
            for(int i=0;i<plans.size();i++) {
                Dev_Restart_Plan plan = plans.get(i);
                sql_str += " UPDATE "+Sql_Mysql.Dev_Restart_Plan_Table+" SET last_restarttime = '"+Com.getDateTimeFormat(plan.last_restarttime, Com.DTF_YMDhms)+"' WHERE dev_id = "+plan.dev_id+";";            
            }
            Sql_Mysql sql = new Sql_Mysql(pool.getConn());
            try {
                sql.sqlMysqlExecute(sql_str);
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                sql.close_con();
            }
        }
    }
 
    //·µ»ØÖ¸¶¨Ê±¼ä¼ä¸ô¼äµÄÃëÊý
    public static long calculTimelong(Date start,Date end) {
        return (end.getTime() - start.getTime())/1000;
    }
 
 
}