lxw
2020-07-11 9db52f2f2dd3665fe9da1ae5657e0167c3a34d40
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
package com.fgkj.dao.impl;
 
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import com.fgkj.dao.BaseDAO;
import com.fgkj.dao.CallBack;
import com.fgkj.dao.DAOHelper;
import com.fgkj.db.DBUtil;
import com.fgkj.db.IDatabaseName;
import com.fgkj.dto.Battresdata_inf;
import com.fgkj.dto.Batttestdata_inf;
import com.fgkj.dto.Batttestdatastop;
import com.sun.org.apache.commons.logging.impl.NoOpLog;
 
public class Battresdata_infDAOImpl implements BaseDAO,CallBack{
 
    public List getResults(ResultSet rs) {
        List list=new ArrayList();
        try {
            while(rs.next()){
                Battresdata_inf b=new Battresdata_inf();
                b.setNum(rs.getInt("num"));
                b.setBattGroupId(rs.getInt("BattGroupId"));
                b.setTest_record_count(rs.getInt("test_record_count"));
                b.setTest_record_count_ex(rs.getInt("test_record_count_ex"));
                b.setTest_type(rs.getInt("test_type"));
                b.setData_new(rs.getFloat("data_new"));
                b.setData_available(rs.getInt("data_available"));
                b.setTest_starttime(rs.getTimestamp("test_starttime"));
                b.setTest_starttime_ex(rs.getTimestamp("test_starttime_ex"));
                b.setTest_devtype(rs.getInt("test_devtype"));
                b.setRecord_time(rs.getTimestamp("record_time"));
                b.setGroup_vol(rs.getFloat("group_vol"));
                b.setTest_curr(rs.getFloat("test_curr"));
                b.setUpload_usr_id(rs.getInt("upload_usr_id"));
                list.add(b);
                //System.out.println(b);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } 
        return list;
    }
    
    public boolean add(Object obj) {
        Battresdata_inf binf=(Battresdata_inf) obj;
        String sql="insert into db_batt_testdata.tb_battresdata_inf(BattGroupId,test_record_count,test_record_count_ex,test_type,data_new,data_available,test_starttime,test_starttime_ex,test_devtype,record_time,group_vol," +
                "test_curr,upload_usr_id) values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
        Boolean bl=DAOHelper.executeUpdate(DBUtil.getConn(), sql, new Object[]{binf.getBattGroupId(),binf.getTest_record_count(),binf.getTest_record_count_ex(),
            binf.getTest_type(),binf.getData_new(),binf.getData_available(),binf.getTest_starttime(),binf.getTest_starttime_ex(),binf.getTest_devtype(),binf.getRecord_time(),binf.getGroup_vol(),
            binf.getTest_curr(),binf.getUpload_usr_id()});
        return bl;
    }
 
    public boolean update(Object obj) {
        Battresdata_inf binf=(Battresdata_inf) obj;
        String sql="update db_batt_testdata.tb_battresdata_inf set BattGroupId=?,test_record_count=?,test_record_count_ex=?,test_type=?,data_new=?,data_available=?,test_starttime=?,test_starttime_ex=?,test_devtype=?,record_time=?,group_vol=?," +
                "test_curr=?,upload_usr_id=? where num=?";
        Boolean bl=DAOHelper.executeUpdate(DBUtil.getConn(), sql, new Object[]{binf.getBattGroupId(),binf.getTest_record_count(),binf.getTest_record_count_ex(),
            binf.getTest_type(),binf.getData_new(),binf.getData_available(),binf.getTest_starttime(),binf.getTest_starttime_ex(),binf.getTest_devtype(),binf.getRecord_time(),binf.getGroup_vol(),
            binf.getTest_curr(),binf.getUpload_usr_id(),binf.getNum()});
        return bl;
    }
 
    //6.1电池充放电删除操作
    public boolean del(Object obj) {
        Battresdata_inf rinf=(Battresdata_inf) obj;
        String sql="delete from db_batt_testdata.tb_battresdata_inf where num=?";
        Boolean bl=DAOHelper.executeUpdate(DBUtil.getConn(), sql, new Object[]{rinf.getNum()});
        return bl;
    }
    //电池充/放电数据管理删除
    public String delPro(Object obj) {
        Battresdata_inf rinf=(Battresdata_inf) obj;
        String sql="delete from db_batt_testdata.tb_battresdata_inf where num="+rinf.getNum();
        return sql;
    }
 
    public List searchAll() {
        String sql="select * from db_batt_testdata.tb_battresdata_inf";
    //    System.out.println(sql);
        List list=DAOHelper.executeQuery(sql, DBUtil.getConn(), null, new Battresdata_infDAOImpl());
        return list;
    }
    //1.1测试battgroupid在battresdata_inf中是否存在
    public List judge(Object obj){
                Battresdata_inf bti=(Battresdata_inf)obj;
                String sql="select table_name from `INFORMATION_SCHEMA`.`TABLES` " +
                        "where table_name ='tb_battresdata_"+bti.getBattGroupId()+"'and TABLE_SCHEMA='db_batt_testdata'";
                return DAOHelper.executeQuery(sql, DBUtil.getConn(), null, new CallBack() {
                    
                    public List getResults(ResultSet rs) {
                        List list=new ArrayList();
                        try {
                            while(rs.next()){
                                String name=rs.getString("table_name");
                                list.add(name);
                                //System.out.println(binf);    
                            }
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                        return list;
                    }
                });
                
            }
 
    //battgroupid是不是存在battresdata_inf中
    public List serchByCondition(Object obj) {
            Battresdata_inf btd=(Battresdata_inf)obj;
                String sql="select num,BattGroupId,test_record_count,test_record_count_ex,test_type,data_new,data_available,test_starttime,test_starttime_ex,test_devtype,record_time,group_vol,test_curr,upload_usr_id from db_batt_testdata.tb_battresdata_inf" +
                        " where battgroupid=?";
            //    System.out.println(sql);
                return DAOHelper.executeQuery(sql, DBUtil.getConn(), new Object[]{btd.getBattGroupId()}, new Battresdata_infDAOImpl());
                
        }
 
    //6.2根据battgroupi和test_starttime查test_record_count
    public List serchTest_record_count(Object obj) {
                Battresdata_inf rinf=(Battresdata_inf)obj;
                String sql="select test_record_count  " +
                        "from db_batt_testdata.tb_battresdata_inf where BattGroupId=?  and (test_starttime)=(?) ";
                return DAOHelper.executeQuery(sql, DBUtil.getConn(), new Object[]{rinf.getBattGroupId(),rinf.getTest_starttime()}, new CallBack() {
                    
                    public List getResults(ResultSet rs) {
                        List list=new ArrayList();
                        try {
                            while(rs.next()){
                                Battresdata_inf rinf=new Battresdata_inf();
                                
                                rinf.setTest_record_count(rs.getInt("test_record_count"));
                                list.add(rinf);
                                //System.out.println(rinf);    
                            }
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                        return list;
                    }
                });
            }
 
    //6.4.1根据条件查询符合条件的测试完成的电池组(蓄电池电导测试完成率)
        /*
         * data_available 中存放层次
         */
    public List serchComplete(Object obj){
        Battresdata_inf rinf=(Battresdata_inf) obj;
        String sql="";
        String baseSql="select distinct(tb_battresdata_inf.battgroupid)  from db_batt_testdata.tb_battresdata_inf,db_battinf.tb_battinf " +
                   "where  tb_battresdata_inf.BattGroupId=db_battinf.tb_battinf.BattGroupId " +
                   "and (test_starttime)>=(?) and (test_starttime)<=(?) ";
        //上传方式
        String usrSqlT="and upload_usr_id!=? " ;
        String usrSqlU="and upload_usr_id!=0 and upload_usr_id!=? " ;
        String usrSqlF="and upload_usr_id=? " ;
        if(rinf.getUpload_usr_id()==100){//全部
            baseSql+=usrSqlT;
        }else{
            if(rinf.getUpload_usr_id()==0){
                baseSql+=usrSqlF;
            }else{
                baseSql+=usrSqlU;
            }
        }
        String levelSql=" and db_battinf.tb_battinf.stationname4 like ? and db_battinf.tb_battinf.stationname3 like ? and db_battinf.tb_battinf.stationname2 like ? and db_battinf.tb_battinf.stationname1 like ?  ";//分组层次
        sql=baseSql+levelSql;
        
        //System.out.println(sql);
        List list=DAOHelper.executeQuery(sql, DBUtil.getConn(), new Object[]{rinf.getTest_starttime(),rinf.getTest_starttime_ex(),rinf.getUpload_usr_id(),"%"+rinf.getStationname4()+"%","%"+rinf.getStationname3()+"%","%"+rinf.getStationname2()+"%","%"+rinf.getStationname1()+"%"}, new CallBack() {
            
            public List getResults(ResultSet rs) {
                List list=new ArrayList();
                try {
                    while(rs.next()){
                        Battresdata_inf rinf=new Battresdata_inf();
                        rinf.setBattGroupId(rs.getInt("battGroupId"));
                        list.add(rinf);
                    }
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return list;
            }
        });
        return list;
    }
 
    //6.4.3根据条件查询符合条件的测试完成的电池组(蓄电池电导合格率)
        /*
         * data_available 中存放层次
         */
    public List serchSer(Object obj){
        Battresdata_inf rinf=(Battresdata_inf) obj;
        String sql="";
        String baseSql="select distinct(tb_battresdata_inf.battgroupid),test_starttime,test_record_count  from db_batt_testdata.tb_battresdata_inf,db_battinf.tb_battinf " +
                   "where  tb_battresdata_inf.BattGroupId=db_battinf.tb_battinf.BattGroupId " +
                   //"and upload_usr_id=? "+
                   "and (test_starttime)>=(?) and (test_starttime)<=(?) ";
        //上传方式
        String usrSqlT=" and upload_usr_id!=? " ;
        String usrSqlU=" and upload_usr_id!=0 and  upload_usr_id!=? " ;
        String usrSqlF=" and upload_usr_id=? " ;
        if(rinf.getUpload_usr_id()==100){//全部
            baseSql+=usrSqlT;
        }else{
            if(rinf.getUpload_usr_id()==0){
                baseSql+=usrSqlF;
            }else{
                baseSql+=usrSqlU;
            }
        }
        String levelSql=" and db_battinf.tb_battinf.stationname4 like ? and db_battinf.tb_battinf.stationname3 like ? and db_battinf.tb_battinf.stationname2 like ? and db_battinf.tb_battinf.stationname1 like ?  ";//分组层次
        sql=baseSql+levelSql;
        
        String endSql=" order by tb_battresdata_inf.battgroupid asc,test_starttime desc ";
        sql=sql+=endSql;
        //System.out.println(sql);
        List list=DAOHelper.executeQuery(sql, DBUtil.getConn(), new Object[]{rinf.getTest_starttime(),rinf.getTest_starttime_ex(),rinf.getUpload_usr_id(),"%"+rinf.getStationname4()+"%","%"+rinf.getStationname3()+"%","%"+rinf.getStationname2()+"%","%"+rinf.getStationname1()+"%"}, new CallBack() {
            public List getResults(ResultSet rs) {
                List list=new ArrayList();
                int id=0;
                    try {
                        while(rs.next()){
                            Battresdata_inf rinf=new Battresdata_inf();
                            if(id==rs.getInt("battGroupId")){   //时间倒序取最近的一次
                                continue;
                            }else{
                                rinf.setBattGroupId(rs.getInt("battGroupId"));
                                id=rs.getInt("battGroupId");
                            }    
                            rinf.setTest_record_count(rs.getInt("test_record_count"));
                            rinf.setTest_starttime(rs.getTimestamp("test_starttime"));
                            list.add(rinf);
                        }    
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }  
                return list;
            }
        });
        return list;
    }
 
    public List serchByInfo(Object obj) {
        // TODO Auto-generated method stub
        return null;
    }
    public static void main(String[] args) {
        Battresdata_infDAOImpl bstd=new Battresdata_infDAOImpl();
        Battresdata_inf bst=new Battresdata_inf();
        bst.setTest_record_count(1);
        List<Battresdata_inf> list=bstd.serchByCondition(bst);
        System.out.println(list.size());
        for(Battresdata_inf b:list){
            System.out.println(b);
        }
//        List<Battresdata_inf> list=bstd.searchAll();
//        for(Battresdata_inf b:list){
//            System.out.println(b);
//        }
    }
 
}