充放电一体机FGCD通信程序【二期初版】
whyclxw
2021-09-01 48b0d8bf2db9a5a0950946ad03544504633a86d0
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
package com.dev.fgcd.comm;
 
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import com.battmonitor.sql.Com;
import com.battmonitor.sql.MysqlConnPool;
import com.battmonitor.sql.Sql_Mysql;
import com.dev.fgcd.data.FGCD_TestFBOFile;
import com.dev.fgcd.data.FGCD_TestFBOFile.FBOFile;
import com.dev.fgcd.fbo.FboData;
import com.dev.fgcd.fbo.FboDataInf;
import com.mchange.v2.resourcepool.ResourcePoolListener;
 
public class FGCD_Task_Thread_SQL {
      
    /**
     *     ²åÈëFBOÀúÊ·Êý¾Ý
     * @param pool
     * @param file
     * @param fbo
     * @param datas
     * @return
     */
    public static boolean insertFBODataIntoTable(MysqlConnPool pool,FBOFile file,FboDataInf fbo,List<FboData> datas,int BattGoupId) {
        boolean success = false;
        if(fbo.getTestTimeLong() > 0) {
            createTb_FboTestData_Table(pool, BattGoupId);
            int max_test_record_count = quereyBattMaxTestRecordCount(pool, BattGoupId);
            ArrayList<String> sql_strs = new ArrayList<>();
            sql_strs.add("INSERT INTO " + Sql_Mysql.Tb_FboTestData_Inf_Table + 
                    "(BattGroupId,test_record_count,test_starttime,test_timelong,device,test_type,hourrate,save_interval,monomervol,moncapstd,test_curr,test_cap,monvol_limit,gourpvol_limit,mon_count,monvol_limitcount,stop_type,download_time) " + 
                    " VALUES(" + BattGoupId
                    + "," +max_test_record_count
                    + ",'" + Com.getDateTimeFormat(fbo.TestStartTime.getFBODateTime(), Com.DTF_YMDhms)
                    + "'," + fbo.getTestTimeLong()
                    + "," + fbo.Device
                    + "," + fbo.DataType 
                    + "," + fbo.HourRate
                     + "," + fbo.SaveInterval
                    + "," + fbo.MonomerVol
                    + "," + fbo.STDCap
                    + "," + fbo.TestCur 
                    + "," + fbo.TestCap
                    + "," + fbo.MVLLimit
                     + "," + fbo.SumVLLimit
                    + "," + fbo.BattSum
                    + "," + fbo.MVLLimitCount
                    + "," + fbo.StopType
                    + ",'" + Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms)
                    + "')");
            for(int i = 0;i<datas.size();i++) {
                FboData data = datas.get(i);
                //System.out.println("²âÊÔʱ³¤"+data.m_TestTime.getTestTimeString());
                String sql_str = "INSERT INTO "+Sql_Mysql.Tb_FboTestData_Table+BattGoupId+"(BattGroupId,test_record_count,test_starttime,test_timelong,record_time,record_num,online_vol,sum_vol,test_curr,test_cap,mon_num,mon_vol) " + 
                                 " VALUES";
                Date record_time = new Date(fbo.TestStartTime.getFBODateTime().getTime()+data.m_TestTime.getTestTimeLong()*1000);
                if(data.BattSum > 0) {
                    //Óе¥Ìåʱ
                    for(int k=0;k<data.BattSum && k<data.SingleVol.length;k++) {
                        sql_strs.add(sql_str + "("
                                + "" + BattGoupId
                                + "," + max_test_record_count
                                + ",'" + Com.getDateTimeFormat(fbo.TestStartTime.getFBODateTime(), Com.DTF_YMDhms)
                                + "'," + data.m_TestTime.getTestTimeLong()
                                + ",'" + Com.getDateTimeFormat(record_time, Com.DTF_YMDhms)
                                + "'," + (i+1)
                                + "," + data.OnlineVol
                                + "," + data.SumVoltage
                                + "," + data.SumCurrent
                                + "," + data.AllCap
                                + "," + (k+1)
                                + "," + data.SingleVol[k]
                                + ")");
                    }                    
                }else {        
                    //ûÓе¥ÌåÊý¾Ýʱ
                    sql_strs.add(sql_str + "("
                            + "" + BattGoupId
                            + "," + max_test_record_count
                            + ",'" + Com.getDateTimeFormat(fbo.TestStartTime.getFBODateTime(), Com.DTF_YMDhms)
                            + "'," + data.m_TestTime.getTestTimeLong()
                            + ",'" + Com.getDateTimeFormat(record_time, Com.DTF_YMDhms)
                            + "'," + (i+1)
                            + "," + data.OnlineVol
                            + "," + data.SumVoltage
                            + "," + data.SumCurrent
                            + "," + data.AllCap
                            + "," + 0
                            + "," + 0
                            + ")");
                }            
            }        
            Sql_Mysql sql = new Sql_Mysql(pool);
            ResultSet res = null;
            try {
                res = sql.sqlMysqlQuery(" SELECT * " + 
                        " FROM " + Sql_Mysql.Tb_FboTestData_Inf_Table +
                        " WHERE BattGroupId = "+BattGoupId+" AND test_starttime = '"+Com.getDateTimeFormat(fbo.TestStartTime.getFBODateTime(), Com.DTF_YMDhms)+"'");
                if(!res.next()) {                
                    success = sql.makeManualCommit(sql_strs);
                }else {
                    //System.out.println("ÒÑ´æÔÚµ±Ç°²âÊÔÊý¾Ý");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                if(null != res) {
                    try {
                        res.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                sql.close_con();
            }
        }    
        return success;
    }
    //²åÈëFBO¡ª¡ªinfÐÅÏ¢
    public static boolean insertFBOInfIntoTable(MysqlConnPool pool,FboDataInf fbo,int BattGoupId,int max_test_record_count) {
        boolean flag=true;
        if(fbo.getTestTimeLong() > 0) {
            String sql_str="INSERT INTO " + Sql_Mysql.Tb_FboTestData_Inf_Table + 
                    "(BattGroupId,test_record_count,test_starttime,test_timelong,device,test_type,hourrate,save_interval,monomervol,moncapstd,test_curr,test_cap,monvol_limit,gourpvol_limit,mon_count,monvol_limitcount,stop_type,download_time) " + 
                    " VALUES(" + BattGoupId
                    + "," +max_test_record_count
                    + ",'" + Com.getDateTimeFormat(fbo.TestStartTime.getFBODateTime(), Com.DTF_YMDhms)
                    + "'," + fbo.getTestTimeLong()
                    + "," + fbo.Device
                    + "," + fbo.DataType 
                    + "," + fbo.HourRate
                     + "," + fbo.SaveInterval
                    + "," + fbo.MonomerVol
                    + "," + fbo.STDCap
                    + "," + fbo.TestCur 
                    + "," + fbo.TestCap
                    + "," + fbo.MVLLimit
                     + "," + fbo.SumVLLimit
                    + "," + fbo.BattSum
                    + "," + fbo.MVLLimitCount
                    + "," + fbo.StopType
                    + ",'" + Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms)
                    + "')";
            Sql_Mysql sql = new Sql_Mysql(pool);
            ResultSet res = null;
            try {
                res = sql.sqlMysqlQuery(" SELECT * " + 
                        " FROM " + Sql_Mysql.Tb_FboTestData_Inf_Table +
                        " WHERE BattGroupId = "+BattGoupId+" AND test_starttime = '"+Com.getDateTimeFormat(fbo.TestStartTime.getFBODateTime(), Com.DTF_YMDhms)+"'");
                if(!res.next()) {
                    //Èç¹û²»´æÔÚÔòÌí¼Óinf±í¼Ç¼
                    sql.sqlMysqlExecute(sql_str);
                }else {
                    //System.out.println("ÒÑ´æÔÚµ±Ç°²âÊÔÊý¾Ý");
                    flag=false;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                if(null != res) {
                    try {
                        res.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                sql.close_con();
            }
        }
        return flag;    
    }    
    
    //²åÈëFBOÀúÊ·Êý¾Ý
    public static boolean insertFBODataIntoTableByOneSql(MysqlConnPool pool,FboDataInf fbo,FboData data,int BattGoupId,int record_num,int max_test_record_count) {
        boolean success = false;
        if(fbo.getTestTimeLong() > 0) {
            createTb_FboTestData_Table(pool, BattGoupId);
            ArrayList<String> sql_strs = new ArrayList<>();
            String sql_str = "INSERT INTO "+Sql_Mysql.Tb_FboTestData_Table+BattGoupId+"(BattGroupId,test_record_count,test_starttime,test_timelong,record_time,record_num,online_vol,sum_vol,test_curr,test_cap,mon_num,mon_vol) " + 
                             " VALUES";
            Date record_time = new Date(fbo.TestStartTime.getFBODateTime().getTime()+data.m_TestTime.getTestTimeLong()*1000);
            if(data.BattSum > 0) {
                //Óе¥Ìåʱ
                for(int k=0;k<data.BattSum && k<data.SingleVol.length;k++) {
                    sql_strs.add(sql_str + "("
                            + "" + BattGoupId
                            + "," + max_test_record_count
                            + ",'" + Com.getDateTimeFormat(fbo.TestStartTime.getFBODateTime(), Com.DTF_YMDhms)
                            + "'," + data.m_TestTime.getTestTimeLong()
                            + ",'" + Com.getDateTimeFormat(record_time, Com.DTF_YMDhms)
                            + "'," + record_num
                            + "," + data.OnlineVol
                            + "," + data.SumVoltage
                            + "," + data.SumCurrent
                            + "," + data.AllCap
                            + "," + (k+1)
                            + "," + data.SingleVol[k]
                            + ")");
                }                    
            }else {        
                //ûÓе¥ÌåÊý¾Ýʱ
                sql_strs.add(sql_str + "("
                        + "" + BattGoupId
                        + "," + max_test_record_count
                        + ",'" + Com.getDateTimeFormat(fbo.TestStartTime.getFBODateTime(), Com.DTF_YMDhms)
                        + "'," + data.m_TestTime.getTestTimeLong()
                        + ",'" + Com.getDateTimeFormat(record_time, Com.DTF_YMDhms)
                        + "'," +record_num
                        + "," + data.OnlineVol
                        + "," + data.SumVoltage
                        + "," + data.SumCurrent
                        + "," + data.AllCap
                        + "," + 0
                        + "," + 0
                        + ")");
            }
            Sql_Mysql sql = new Sql_Mysql(pool);
            success = sql.makeManualCommit(sql_strs);
            sql.close_con();
        }    
        return success;
    }    
    
    /**
     *     ´´½¨FBOÀúÊ·Êý¾Ý_ID±í
     * @param pool
     * @param BattGroupId
     */
    public static void createTb_FboTestData_Table(MysqlConnPool pool,int BattGroupId) {
        String sql_str = " CREATE TABLE IF NOT EXISTS "+Sql_Mysql.Tb_FboTestData_Table+BattGroupId+" (" + 
                "  `num` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Ö÷¼ü'," + 
                "  `BattGroupId` int(11) NOT NULL DEFAULT '0' COMMENT 'µç³Ø×éID'," + 
                "  `test_record_count` int(11) NOT NULL DEFAULT '1' COMMENT '²âÊÔ±ÊÊý'," + 
                "  `test_starttime` datetime NOT NULL DEFAULT '2000-01-01 00:00:00' COMMENT '²âÊÔ¿ªÊ¼Ê±¼ä'," + 
                "  `test_timelong` int(11) NOT NULL," + 
                "  `record_time` datetime NOT NULL," + 
                "  `record_num` int(11) NOT NULL DEFAULT '1' COMMENT '¼Ç¼±ÊÊý'," + 
                "  `online_vol` float NOT NULL DEFAULT '0' COMMENT 'ÔÚÏßµçѹ'," + 
                "  `sum_vol` float NOT NULL DEFAULT '0' COMMENT '×ܵçѹ'," + 
                "  `test_curr` float NOT NULL DEFAULT '0' COMMENT '²âÊÔµçÁ÷'," + 
                "  `test_cap` float NOT NULL DEFAULT '0' COMMENT '²âÊÔÈÝÁ¿'," + 
                "  `mon_num` int(11) NOT NULL DEFAULT '0' COMMENT 'µ¥Ìå±àºÅ'," + 
                "  `mon_vol` float NOT NULL DEFAULT '0' COMMENT 'µ¥Ìåµçѹ'," + 
                "  PRIMARY KEY (`num`)" + 
                ") ENGINE=InnoDB DEFAULT CHARSET=utf8; ";
        Sql_Mysql sql = new Sql_Mysql(pool);
        try {
            sql.sqlMysqlExecute(sql_str);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }
    }
    
    /**
     *     ²éѯµ±Ç°µç³Ø×鵱ǰ×î´óµÄ¼Ç¼±ÊÊý+1
     * @param pool
     * @param BattGroupId
     * @return
     */
    public static int quereyBattMaxTestRecordCount(MysqlConnPool pool,int BattGroupId) {
        int test_record_count = 0;
        String sql_str = " SELECT MAX(test_record_count) as test_record_count " + 
                         " FROM " + Sql_Mysql.Tb_FboTestData_Inf_Table +
                         " WHERE BattGroupId = " + BattGroupId;
        Sql_Mysql sql = new Sql_Mysql(pool);
        ResultSet res = null;
        try {
            res = sql.sqlMysqlQuery(sql_str);
            if(res.next()) {
                test_record_count = res.getInt("test_record_count");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(null != res) {
                try {
                    res.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            sql.close_con();
        }
        return (test_record_count+1);
    }
 
    //Îļþµ¼Èë½ø¶ÈÐÞ¸Ä
    public static void updateFBOGressByOneSql(MysqlConnPool pool,int file_sum,int file_block,FGCD_TestFBOFile fbofile) {
        String sql_str = " update " + Sql_Mysql.Tb_Fgcd_Filedownload_Table +
                         " set now_data_block1="+file_block+",total_data_block1="+file_sum+" WHERE BattGroupId = " + fbofile.battgroupid+" and dev_id="+fbofile.dev_id;
        Sql_Mysql sql = new Sql_Mysql(pool);
        try {
            sql.sqlMysqlExecute(sql_str);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }
    }
 
    public static void updateFBOFIleExportFlag(MysqlConnPool pool, FGCD_TestFBOFile fbofile) {
        String sql_str = " Update  " + Sql_Mysql.Tb_Fgcd_Filedownload_Table +
                         " Set ";
        for(int i=0;i<fbofile.files.length;i++) {
            if(i>0) {
                sql_str += ",";
            }
            sql_str += " download_flag"+(i+1)+" = "+fbofile.files[i].download_flag+""
                     + " ,download_state"+(i+1)+" =" +fbofile.files[i].download_state;
        }
        sql_str += " WHERE dev_id = " + fbofile.dev_id;
        Sql_Mysql sql = new Sql_Mysql(pool);
        try {
            sql.sqlMysqlExecute(sql_str);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }
    }
    
    /**
     *     ¸üе±Ç°ÏÂÔØ×´Ì¬,ÒÔ¼°Òì³£½áÊøÔ­Òò
     * @param pool
     * @param file
     */
    public static void updateFgcd_Filedownload_TableCmdByDevId(MysqlConnPool pool,FGCD_TestFBOFile file) {
        String sql_str = " UPDATE " + Sql_Mysql.Tb_Fgcd_Filedownload_Table +
                         " SET op_cmd =  " + file.op_cmd +
                         " ,stop_reason =  " + file.getStop_reason() +
                         " WHERE dev_id =  " + file.dev_id;
        Sql_Mysql sql = new Sql_Mysql(pool);
        try {
            sql.sqlMysqlExecute(sql_str);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }
    }
 
    /**
     *     ¸üе±Ç°É豸µÄÏÂÔØ×´Ì¬
     * @param pool
     * @param fbofile
     */
    public static void updateFgcd_FiledownloadState_TableByDevId(MysqlConnPool pool, FGCD_TestFBOFile fbofile) {
        String sql_str_base = " update " + Sql_Mysql.Tb_Fgcd_Filedownload_Table + " Set stop_reason = " + fbofile.getStop_reason();
        for(int i=0;i<fbofile.files.length;i++) {
            FBOFile file = fbofile.files[i];
            sql_str_base += " ,download_flag"+(i+1) + " = "+file.download_flag+","
                          + " download_state"+(i+1) + " = "+file.download_state;
        }
        sql_str_base += " WHERE dev_id = " + fbofile.dev_id;
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        try {
            sql.sqlMysqlExecute(sql_str_base);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }
    }
}