whycxzp
2025-04-30 4fce1788cfb5bc37484060b068daf6303a584058
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
package com.whyc.service;
 
import com.github.pagehelper.PageInfo;
import com.whyc.dto.Response;
import com.whyc.mapper.CallBack;
import com.whyc.pojo.db_power_history.BattRealTimeDataHistory;
import com.whyc.util.SubTablePageInfoUtil;
import com.whyc.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
 
@Service
public class BattRealTimeDataHistoryService {
 
    @Autowired
    private SubTablePageInfoUtil subTablePageInfoUtil;
 
    @Autowired
    private MybatisSqlExecuteService sqlExecuteService;
 
    public Response<PageInfo> getPage(int pageNum, int pageSize, int battGroupId,
                                                               String startTime, String endTime) throws ParseException, InterruptedException {
 
        PageInfo<Object> pageInfo = subTablePageInfoUtil.getPageInfoByMonthTable(pageNum, pageSize,
                ThreadLocalUtil.parse(startTime, 1), ThreadLocalUtil.parse(endTime, 1),
                "db_power_history",
                "tb_batt_realdata_" + battGroupId,
                new BattRealTimeDataHistory()
        );
        return new Response<PageInfo>().set(1, pageInfo);
    }
 
    /**
     * 充电结束时间的定位逻辑为:
     * _电池组id表中,记录时间大于dischargeEndTime且电流为0的记录中,顺序取出第一个记录.
     * 考虑到充电结束时间可能跨表,所以需要查询下一个月的表.
     * @return
     */
    public BattRealTimeDataHistory getChargeEnd(int battGroupId, Calendar dischargeEndTime) {
        //查询的年月为dischargeEndTime的年月
        int year = dischargeEndTime.get(Calendar.YEAR);
        int month = dischargeEndTime.get(Calendar.MONTH);
        //String tableName = "tb_batt_realdata_" + battGroupId + "_" + year + "_" + (month+1);
        //查询dischargeEndTime的年月加上一个月
        int yearOfNextMonth = year;
        int monthOfNextMonth = month;
        if (month == 11) {
            yearOfNextMonth++;
            monthOfNextMonth = 0;
        }else{
            monthOfNextMonth++;
        }
        String battGroupIdTable = getBattGroupIdTable(battGroupId, dischargeEndTime.getTime());
        String sql = "select * from db_power_history.tb_batt_realdata_" + battGroupIdTable + " where record_time > '" + dischargeEndTime.getTime() + "' and group_curr = 0 limit 1";
        List<BattRealTimeDataHistory> list = sqlExecuteService.executeQuery_call(sql, new CallBack() {
 
            @Override
            public List getResults(ResultSet rs) throws SQLException {
                List<BattRealTimeDataHistory> list = new LinkedList<>();
                //如果存在记录
                while (rs.next()) {
                    BattRealTimeDataHistory his = new BattRealTimeDataHistory();
                    his.setBattGroupId(rs.getInt("binf_id"));
                    his.setRecordTime(rs.getTimestamp("record_time"));
                    his.setOnlineVol(rs.getFloat("online_vol"));
                    his.setGroupVol(rs.getFloat("group_vol"));
                    his.setGroupTmp(rs.getFloat("group_tmp"));
                    his.setGroupCurr(rs.getFloat("group_curr"));
 
                    list.add(his);
                }
                return list;
 
            }
        });
        //如果不存在记录,则查询下一个月的表
        if (list.size() == 0) {
            //String tableNameNextMonth = "tb_batt_realdata_" + battGroupId + "_" + yearOfNextMonth + "_" + (monthOfNextMonth+1);
            dischargeEndTime.add(Calendar.MONTH, 1);
            String battGroupITableNameNextMonth = getBattGroupIdTable(battGroupId, dischargeEndTime.getTime());
            //还原dischargeEndTime
            dischargeEndTime.add(Calendar.MONTH, -1);
            String sqlNextMonth = "select * from db_power_history." + battGroupITableNameNextMonth + " where record_time > '" + dischargeEndTime.getTime() + "' and group_curr = 0 limit 1";
            List<BattRealTimeDataHistory> listNextMonth = sqlExecuteService.executeQuery_call(sqlNextMonth, new CallBack() {
 
                @Override
                public List getResults(ResultSet rs) throws SQLException {
                    List<BattRealTimeDataHistory> list = new LinkedList<>();
                    //如果存在记录
                    while (rs.next()) {
                        BattRealTimeDataHistory his = new BattRealTimeDataHistory();
                        his.setBattGroupId(rs.getInt("binf_id"));
                        his.setRecordTime(rs.getTimestamp("record_time"));
                        his.setOnlineVol(rs.getFloat("online_vol"));
                        his.setGroupVol(rs.getFloat("group_vol"));
                        his.setGroupTmp(rs.getFloat("group_tmp"));
                        his.setGroupCurr(rs.getFloat("group_curr"));
 
                        list.add(his);
                    }
                    return list;
 
                }
            });
            return listNextMonth.size() == 0 ? null : listNextMonth.get(0);
        }else{
            return list.get(0);
        }
    }
 
    public List<BattRealTimeDataHistory> getFcVolList(int battGroupId, Date testStartTime) {
        String battGroupIdTable = getBattGroupIdTable(battGroupId, testStartTime);
        String sql = "select * from db_power_history.tb_batt_realdata_" + battGroupIdTable + " where record_time < '" + testStartTime + "' and  batt_test_type = 1 order by num desc limit 104";
        List<BattRealTimeDataHistory> fcVolList = sqlExecuteService.executeQuery_call(sql, new CallBack() {
 
            @Override
            public List getResults(ResultSet rs) throws SQLException {
                List<BattRealTimeDataHistory> list = new LinkedList<>();
                //如果存在记录
                while (rs.next()) {
                    BattRealTimeDataHistory his = new BattRealTimeDataHistory();
                    his.setMonNum(rs.getInt("mon_num"));
                    his.setMonVol(rs.getFloat("mon_vol"));
                    his.setMonTmp(rs.getFloat("mon_tmp"));
 
                    list.add(his);
                }
                return list;
 
            }
        });
        return fcVolList;
    }
 
    public List<BattRealTimeDataHistory> getPreVolList(int battGroupId, Date testStartTime) {
        String battGroupIdTable = getBattGroupIdTable(battGroupId, testStartTime);
        String sql = "select * from db_power_history.tb_batt_realdata_" + battGroupIdTable + " where record_time = '" + testStartTime + "' order by mon_num asc limit 104";
        List<BattRealTimeDataHistory> fcVolList = sqlExecuteService.executeQuery_call(sql, new CallBack() {
 
            @Override
            public List getResults(ResultSet rs) throws SQLException {
                List<BattRealTimeDataHistory> list = new LinkedList<>();
                //如果存在记录
                while (rs.next()) {
                    BattRealTimeDataHistory his = new BattRealTimeDataHistory();
                    his.setMonNum(rs.getInt("mon_num"));
                    his.setMonVol(rs.getFloat("mon_vol"));
                    his.setMonTmp(rs.getFloat("mon_tmp"));
 
                    list.add(his);
                }
                return list;
 
            }
        });
        return fcVolList;
    }
 
    public List<BattRealTimeDataHistory> getRecordList(int battGroupId, Date recordTime) {
        String battGroupIdTable = getBattGroupIdTable(battGroupId, recordTime);
        String sql = "select * from db_power_history.tb_batt_realdata_" + battGroupIdTable + " where record_time >= '" + recordTime + "' order by num asc limit 104";
        List<BattRealTimeDataHistory> fcVolList = sqlExecuteService.executeQuery_call(sql, new CallBack() {
 
            @Override
            public List getResults(ResultSet rs) throws SQLException {
                List<BattRealTimeDataHistory> list = new LinkedList<>();
                //如果存在记录
                while (rs.next()) {
                    BattRealTimeDataHistory his = new BattRealTimeDataHistory();
                    his.setMonNum(rs.getInt("mon_num"));
                    his.setMonVol(rs.getFloat("mon_vol"));
                    his.setMonTmp(rs.getFloat("mon_tmp"));
 
                    list.add(his);
                }
                return list;
 
            }
        });
        return fcVolList;
    }
 
    public Date getChargeStartTime(int battGroupId, Date recordTime) {
        String battGroupIdTable = getBattGroupIdTable(battGroupId, recordTime);
        String sql = "select record_time from db_power_history.tb_batt_realdata_" + battGroupIdTable + " where record_time > '" + recordTime + "' and batt_test_type = 2 order limit 1";
        List<Date> timeList = sqlExecuteService.executeQuery_call(sql, new CallBack() {
 
            @Override
            public List getResults(ResultSet rs) throws SQLException {
                List<Date> list = new LinkedList<>();
                //如果存在记录
                while (rs.next()) {
                    Date recordTime = rs.getTimestamp("record_time");
 
                    list.add(recordTime);
                }
                return list;
 
            }
        });
        return timeList.get(0);
    }
 
    public List<BattRealTimeDataHistory> getFcVolListAfter(int battGroupId, Date testStartTime) {
        String battGroupIdTable = getBattGroupIdTable(battGroupId, testStartTime);
        String sql = "select * from db_power_history.tb_batt_realdata_" + battGroupIdTable + " where record_time > '" + testStartTime + "' and  batt_test_type = 1 order by num desc limit 104";
        List<BattRealTimeDataHistory> fcVolList = sqlExecuteService.executeQuery_call(sql, new CallBack() {
 
            @Override
            public List getResults(ResultSet rs) throws SQLException {
                List<BattRealTimeDataHistory> list = new LinkedList<>();
                //如果存在记录
                while (rs.next()) {
                    BattRealTimeDataHistory his = new BattRealTimeDataHistory();
                    his.setMonNum(rs.getInt("mon_num"));
                    his.setMonVol(rs.getFloat("mon_vol"));
                    his.setMonTmp(rs.getFloat("mon_tmp"));
 
                    list.add(his);
                }
                return list;
 
            }
        });
        return fcVolList;
    }
 
    private String getBattGroupIdTable(int battGroupId, Date time) {
        Calendar timeCalendar = Calendar.getInstance();
        timeCalendar.setTime(time);
        int year = timeCalendar.get(Calendar.YEAR);
        int month = timeCalendar.get(Calendar.MONTH)+1;
        if (month < 10){
            return battGroupId + "_" + year + "_0" + month;
        }else{
            return battGroupId + "_" + month + "_" + month;
        }
    }
 
    public List<BattRealTimeDataHistory> getFirstRecordList(String tableName) {
        String sql = "select * from db_power_history." + tableName + " limit 104";
        List<BattRealTimeDataHistory> list = sqlExecuteService.executeQuery_call(sql, new CallBack() {
 
            @Override
            public List getResults(ResultSet rs) throws SQLException {
                List<BattRealTimeDataHistory> list = new LinkedList<>();
                //如果存在记录
                while (rs.next()) {
                    BattRealTimeDataHistory his = new BattRealTimeDataHistory();
                    his.setTestStartTime(rs.getTimestamp("test_start_time"));
                    his.setMonNum(rs.getInt("mon_num"));
                    his.setMonVol(rs.getFloat("mon_vol"));
                    his.setMonRes(rs.getFloat("mon_res"));
                    his.setMonTmp(rs.getFloat("mon_tmp"));
 
                    list.add(his);
                }
                return list;
 
            }
        });
        return list;
    }
 
    /**
     * select * from tb_batt_realdata_100001_2025_04 where record_time in
     * (SELECT MIN(record_time) AS min_time FROM tb_batt_realdata_100001_2025_04
     * GROUP BY DATE(record_time))
     *
     * @param tableName
     * @param timeAgo
     * @return
     */
    public List<BattRealTimeDataHistory> getFirstRecordListOfDay(String tableName, Date timeAgo) {
        String sqlExtra;
        if(timeAgo!=null){
            sqlExtra = " where record_time > '" + timeAgo + "'";
        }else{
            sqlExtra = "";
        }
        String sql = "select * from db_power_history." + tableName + " where record_time in (SELECT MIN(record_time) FROM db_power_history." + tableName + sqlExtra +" GROUP BY DATE(record_time))";
        List<BattRealTimeDataHistory> list = sqlExecuteService.executeQuery_call(sql, new CallBack() {
 
            @Override
            public List getResults(ResultSet rs) throws SQLException {
                List<BattRealTimeDataHistory> list = new LinkedList<>();
                //如果存在记录
                while (rs.next()) {
                    BattRealTimeDataHistory his = new BattRealTimeDataHistory();
                    his.setTestStartTime(rs.getTimestamp("test_start_time"));
                    his.setRecordTime(rs.getTimestamp("record_time"));
                    his.setMonNum(rs.getInt("mon_num"));
                    his.setMonVol(rs.getFloat("mon_vol"));
                    his.setMonRes(rs.getFloat("mon_res"));
                    his.setMonTmp(rs.getFloat("mon_tmp"));
 
                    list.add(his);
                }
                return list;
 
            }
        });
        return list;
    }
}