whyclj
2020-10-26 bdda55c79398d4bb836b50d155828cca2b9efb99
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
package com.dev_speedcollect;
 
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
 
import com.battmonitor.base.Com;
import com.battmonitor.sql.MysqlConnPool;
import com.battmonitor.sql.Sql_Mysql;
import com.dev_fbs9600s.data.FBS9600S_ComBase;
 
public class FBS9600S_SpeedCollect_Task_SQL {
    
    /**
     *     ´´½¨ÀúÊ·Êý¾ÝÐÅÏ¢±í
     * @param pool
     */
    public static void createBattCurrDataInf(MysqlConnPool pool) {
        String sql_str = "CREATE TABLE IF NOT EXISTS "+Sql_Mysql.BattCurrDataInf_Table+" (" + 
                "  num bigint(20) NOT NULL AUTO_INCREMENT," + 
                "  BattGroupId int(11) NOT NULL DEFAULT '100001' COMMENT 'µç³Ø×éid'," + 
                "  test_record_count int(11) NOT NULL DEFAULT '1' COMMENT '¼Ç¼±ÊÊý'," + 
                "  test_starttime datetime(3) NOT NULL DEFAULT '2000-01-01 00:00:00.000' COMMENT '¼Ç¼ʱ¼ä'," + 
                "  record_num int(11) NOT NULL DEFAULT '0' COMMENT '¼Ç¼±ÊÊý'," + 
                "  start_curr float NOT NULL DEFAULT '0' COMMENT '¿ªÊ¼µçÁ÷'," + 
                "  end_curr float NOT NULL DEFAULT '0' COMMENT '½áÊøµçÁ÷'," + 
                "  note varchar(64) NOT NULL DEFAULT ''," + 
                "  PRIMARY KEY (num)," + 
                "  KEY index_battgroupid (BattGroupId) USING BTREE," + 
                "  KEY index_test_record_count (test_record_count) USING BTREE" + 
                ") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;";
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        try {
            sql.sqlMysqlExecute(sql_str);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }
    }
    
    /**
     *     ´´½¨ÀúÊ·ÏêϸÐÅÏ¢±í
     * @param pool
     */
    public static void createBattCurrData(MysqlConnPool pool,int BattGroupId) {
        String sql_str = "CREATE TABLE IF NOT EXISTS "+Sql_Mysql.BattCurrData_Table+BattGroupId+" (" + 
                "  num bigint(20) NOT NULL AUTO_INCREMENT," + 
                "  BattGroupId int(11) NOT NULL DEFAULT '10001' COMMENT 'µç³Ø×éid'," + 
                "  group_curr float NOT NULL DEFAULT '0' COMMENT '×é¶ËµçÁ÷'," + 
                "  test_record_count int(11) NOT NULL DEFAULT '1' COMMENT '¼Ç¼±ÊÊý'," + 
                "  test_start_time datetime(3) NOT NULL DEFAULT '2000-01-01 00:00:00.000' COMMENT '²âÊÔ¿ªÊ¼Ê±¼ä'," + 
                "  record_time datetime(3) NOT NULL DEFAULT '2000-01-01 00:00:00.000' COMMENT '¼Ç¼ʱ¼ä'," + 
                "  record_num int(11) NOT NULL DEFAULT '1' COMMENT 'µ±Ç°¼Ç¼±ÊÊý'," + 
                "  note varchar(32) NOT NULL DEFAULT '' COMMENT '±¸ÓÃ'," + 
                "  PRIMARY KEY (num)," + 
                "  KEY index_battgroupid (BattGroupId) USING BTREE," + 
                "  KEY index_test_record_count (test_record_count) USING BTREE" + 
                ") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;";
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        try {
            sql.sqlMysqlExecute(sql_str);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }
    }
    
    /**
     *     ²åÈëµçÁ÷ÀúʷʼþÊý¾Ý
     * @param pool
     * @param data
     */
    public static void insertBattCurrData(MysqlConnPool pool,CurrData data) {
        //²éѯÀúʷʼþ´ÎÊý
        String sql_str_sel = "SELECT MAX(test_record_count) as test_record_count  FROM db_batt_testdata.tb_battcurrdata_inf WHERE BattGroupId = " + data.BattGroupId + " FOR UPDATE ";            
        ResultSet res = null;
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        res = sql.sqlMysqlQuery(sql_str_sel);
        int max_test_record_count = 1;
        try {
            if(res.next()) {
                max_test_record_count = res.getInt("test_record_count")+1;
            }
            Date test_starttime = new Date(data.record_time.getTime()-3000);
            System.out.println("test_record_count :"+max_test_record_count);
            String sql_str_inf = "INSERT INTO "+Sql_Mysql.BattCurrDataInf_Table+"(BattGroupId,test_record_count,test_starttime,record_num,start_curr,end_curr) VALUES("+data.BattGroupId+","+max_test_record_count+",'"+Com.getDateTimeFormat(test_starttime, Com.DTF_YMDhmsS)+"',"+6000+","+data.currs[0]+","+data.currs[data.currs.length-1]+");";
            String sql_str_id = " INSERT INTO " + Sql_Mysql.BattCurrData_Table + data.BattGroupId+"(BattGroupId,group_curr,test_record_count,test_start_time,record_time,record_num) VALUES ";
            for(int i = 0;i<data.currs.length;i++) {
                if(i>0) {
                    sql_str_id += ",";
                }
                Date now = new Date(test_starttime.getTime() + i*50);
                sql_str_id +="("+data.BattGroupId+","+data.currs[i]+","+max_test_record_count+",'"+Com.getDateTimeFormat(test_starttime, Com.DTF_YMDhmsS)+"','"+Com.getDateTimeFormat(now, Com.DTF_YMDhmsS)+"',"+(i+1)+")";
            }
            
            ArrayList<String> sql_strs = new ArrayList<String>();
            sql_strs.add(sql_str_inf);
            sql_strs.add(sql_str_id);
            sql.makeManualCommit(sql_strs);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if(null != res) {
                try {
                    res.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            sql.close_con();
        }
    }
    
    /**
     *     ²éѯËٲɻ㼯Æ÷µÄ²ÎÊý
     * @param pool
     * @param param
     */
    public static void querySpeedCollectParam(MysqlConnPool pool,SpeedCollectParam param) {
        String sql_str = "SELECT * FROM " + Sql_Mysql.FBS9100SetParam_Table + " WHERE dev_id =  " + param.dev_id;
        ResultSet res = null;
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        res = sql.sqlMysqlQuery(sql_str);
        try {
            if(res.next()) {
                param.op_cmd = res.getInt("op_cmd");
                param.CurrClampRange = res.getInt("MonomerLowCount");            //»ô¶ûÁ¿³Ì
                param.DeltaCurrLimit = res.getInt("MonomerVol_LOW");            //µçÁ÷±ä»¯·§Öµ
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if(null != res) {
                try {
                    res.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            sql.close_con();
        }
    }
    
    /**
     *     ÐÞ¸ÄËٲɻ㼯Æ÷µÄ²ÎÊý
     * @param pool
     * @param param
     */
    public static void updateSpeedCollectParam(MysqlConnPool pool,SpeedCollectParam param) {
        if(
           param.op_cmd == FBS9600S_ComBase.OP_CMD_GetSpeedParam ||
           param.op_cmd == FBS9600S_ComBase.OP_CMD_GetSpeedParam_ACK ||
           param.op_cmd == FBS9600S_ComBase.OP_CMD_SetSpeedParam ||
           param.op_cmd == FBS9600S_ComBase.OP_CMD_SetSpeedParam_ACK     
        ) {
            String sql_str = "UPDATE " + Sql_Mysql.FBS9100SetParam_Table + " SET op_cmd = "+param.op_cmd+",MonomerLowCount = "+param.CurrClampRange+",MonomerVol_LOW ="+param.DeltaCurrLimit+" WHERE dev_id = " + param.dev_id;
            Sql_Mysql sql = new Sql_Mysql(pool.getConn());
            try {
                sql.sqlMysqlExecute(sql_str);            
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                sql.close_con();
            }
        }
    }
    
    
    /**
     *     ²åÈë»ò¸üÐÂËٲɻ㼯Æ÷µÄ²ÎÊý
     * @param pool
     * @param param
     */
    public static void InsertOrUpdateSpeedCollectParam(MysqlConnPool pool,SpeedCollectParam param) {
        String sql_str_sel = " SELECT * FROM " + Sql_Mysql.FBS9100SetParam_Table + " WHERE dev_id =  " + param.dev_id;
        String sql_str_upd = " UPDATE " + Sql_Mysql.FBS9100SetParam_Table + " SET op_cmd = "+param.op_cmd+",MonomerLowCount = "+param.CurrClampRange+",MonomerVol_LOW ="+param.DeltaCurrLimit+" WHERE dev_id = " + param.dev_id;;
        String sql_str_ins = " INSERT INTO " + Sql_Mysql.FBS9100SetParam_Table + "(dev_id) VALUE("+param.dev_id+")";
        ResultSet res = null;
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        res = sql.sqlMysqlQuery(sql_str_sel);
        try {
            if(res.next()) {
                sql.sqlMysqlExecute(sql_str_upd);
            }else {
                sql.sqlMysqlExecute(sql_str_ins);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if(null != res) {
                try {
                    res.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            sql.close_con();
        }
    }
}