奥体项目数据报表时间修改
正在显示
5 个修改的文件
包含
300 行增加
和
0 行删除
... | @@ -193,4 +193,15 @@ public class OlympicCenterController { | ... | @@ -193,4 +193,15 @@ public class OlympicCenterController { |
193 | return result; | 193 | return result; |
194 | } | 194 | } |
195 | 195 | ||
196 | @ApiOperation(value="获取历史报表数据", notes="获取历史报表数据") | ||
197 | @GetMapping(value = "/getHistoryReportData") | ||
198 | public Result<List<Map<String,Object>>> getHistoryReportData(String dataType,String loction){ | ||
199 | Result<List<Map<String,Object>>> result = new Result<List<Map<String,Object>>>(); | ||
200 | List<Map<String,Object>> list = new ArrayList<>(); | ||
201 | list = olympicCenterService.getHistoryReportData(dataType, loction); | ||
202 | result.setResult(list); | ||
203 | result.setSuccess(true); | ||
204 | return result; | ||
205 | } | ||
206 | |||
196 | } | 207 | } | ... | ... |
... | @@ -37,4 +37,6 @@ public interface IOlympicCenterService { | ... | @@ -37,4 +37,6 @@ public interface IOlympicCenterService { |
37 | List<Map<String, Object>> getPumpRealTimeReport(String sourceType); | 37 | List<Map<String, Object>> getPumpRealTimeReport(String sourceType); |
38 | 38 | ||
39 | List<Map<String, Object>> getHistoryReportClos(String dataType, String loction); | 39 | List<Map<String, Object>> getHistoryReportClos(String dataType, String loction); |
40 | |||
41 | List<Map<String, Object>> getHistoryReportData(String dataType, String loction); | ||
40 | } | 42 | } | ... | ... |
... | @@ -1306,6 +1306,52 @@ public class OlympicCenterServiceImpl implements IOlympicCenterService { | ... | @@ -1306,6 +1306,52 @@ public class OlympicCenterServiceImpl implements IOlympicCenterService { |
1306 | return list; | 1306 | return list; |
1307 | } | 1307 | } |
1308 | 1308 | ||
1309 | @Override | ||
1310 | public List<Map<String, Object>> getHistoryReportData(String dataType, String loction) { | ||
1311 | List<Map<String, Object>> list = new ArrayList<>(); | ||
1312 | List<WhatStructData> closList = new ArrayList<>(); | ||
1313 | Map<String, Object> monitorMap = new HashMap<>(); | ||
1314 | if("flow".equals(dataType)){ | ||
1315 | closList = whatStructDataMapper.getFlowList(loction); | ||
1316 | }else if("pressure".equals(dataType)){ | ||
1317 | closList = whatStructDataMapper.getPressureList(loction); | ||
1318 | }else if("temperature".equals(dataType)){ | ||
1319 | closList = whatStructDataMapper.getTemperatureList(loction); | ||
1320 | }else{ | ||
1321 | return list; | ||
1322 | } | ||
1323 | String pgField = ""; | ||
1324 | if(closList.size() == 0){ | ||
1325 | return list; | ||
1326 | }else{ | ||
1327 | for (WhatStructData whatStructData:closList) { | ||
1328 | pgField = pgField +","+whatStructData.getMonitorId(); | ||
1329 | if(whatStructData.getMonitorId()!=null){ | ||
1330 | monitorMap.put(whatStructData.getMonitorId(),whatStructData.getId()); | ||
1331 | } | ||
1332 | } | ||
1333 | } | ||
1334 | Calendar cal = Calendar.getInstance(); | ||
1335 | cal.add(Calendar.DATE, -30); | ||
1336 | String startDate = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()); | ||
1337 | String start = startDate + " 00:00:00"; | ||
1338 | String endDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date()); | ||
1339 | String end = endDate + " 23:59:59"; | ||
1340 | List<Map<String, Object>> dataList = factoryInfoService.queryReportData(AT_ID, pgField, start, end,"hour"); | ||
1341 | for (Map<String, Object> map : dataList) { | ||
1342 | Map<String, Object> newMap = new HashMap<>(); | ||
1343 | for (Map.Entry<String, Object> entry : map.entrySet()) { | ||
1344 | if("time".equals(entry.getKey())){ | ||
1345 | newMap.put(entry.getKey(),entry.getValue()); | ||
1346 | }else { | ||
1347 | newMap.put(monitorMap.get(entry.getKey()).toString(),entry.getValue()); | ||
1348 | } | ||
1349 | } | ||
1350 | list.add(newMap); | ||
1351 | } | ||
1352 | return list; | ||
1353 | } | ||
1354 | |||
1309 | private String getStatus1(Map<String, Object> map, List<Map<String, Object>> list, String pumpId) { | 1355 | private String getStatus1(Map<String, Object> map, List<Map<String, Object>> list, String pumpId) { |
1310 | String status = "停止"; | 1356 | String status = "停止"; |
1311 | Map<String, Object> bean = list.get(0); | 1357 | Map<String, Object> bean = list.get(0); | ... | ... |
... | @@ -12,4 +12,17 @@ public interface IFmPgQueryService { | ... | @@ -12,4 +12,17 @@ public interface IFmPgQueryService { |
12 | 12 | ||
13 | List<Map<String, Object>> queryMonitorForReport(String factoryId, String fields); | 13 | List<Map<String, Object>> queryMonitorForReport(String factoryId, String fields); |
14 | 14 | ||
15 | /** | ||
16 | * | ||
17 | * @param departId 厂区编号 | ||
18 | * @param fields 要获取的字段集合,字段间以逗号分隔,例如 a,b,c | ||
19 | * @param startTime 开始时间 当传入""的时候,获取time最大的一条记录 | ||
20 | * @param endTime 结束时间 当传入""的时候,获取time最大的一条记录 | ||
21 | * @param tableType 表类型 0实时 1计算 | ||
22 | * @return | ||
23 | */ | ||
24 | public List<Map<String,Object>> queryFactoryInfos(String departId,String fields,String startTime,String endTime,String tableType); | ||
25 | |||
26 | public List<Map<String, Object>> queryReportData(String departId,String fields,String startTime,String endTime,String reportType); | ||
27 | |||
15 | } | 28 | } | ... | ... |
1 | package com.skua.modules.monitor.service.impl; | 1 | package com.skua.modules.monitor.service.impl; |
2 | 2 | ||
3 | import com.skua.core.context.SpringContextUtils; | 3 | import com.skua.core.context.SpringContextUtils; |
4 | import com.skua.core.util.ConvertUtils; | ||
5 | import com.skua.core.util.DateUtils; | ||
6 | import com.skua.core.util.DoubleOperaUtils; | ||
4 | import org.apache.commons.lang.StringUtils; | 7 | import org.apache.commons.lang.StringUtils; |
8 | import org.apache.shiro.dao.DataAccessException; | ||
5 | import org.springframework.jdbc.core.JdbcTemplate; | 9 | import org.springframework.jdbc.core.JdbcTemplate; |
6 | import org.springframework.stereotype.Service; | 10 | import org.springframework.stereotype.Service; |
7 | import com.skua.modules.monitor.service.IFmPgQueryService; | 11 | import com.skua.modules.monitor.service.IFmPgQueryService; |
... | @@ -134,4 +138,228 @@ public class FmPgQueryServiceImpl implements IFmPgQueryService { | ... | @@ -134,4 +138,228 @@ public class FmPgQueryServiceImpl implements IFmPgQueryService { |
134 | result = pgDb.queryForList(sql); | 138 | result = pgDb.queryForList(sql); |
135 | return result; | 139 | return result; |
136 | } | 140 | } |
141 | |||
142 | |||
143 | @Override | ||
144 | public List<Map<String, Object>> queryFactoryInfos(String departId, String fields, String startTime,String endTime, String tableType) { | ||
145 | //获取要查询的指标 | ||
146 | String fieldSqlPart = ""; | ||
147 | String[] fieldsArr = fields.split(","); | ||
148 | for (int i = 0; i < fieldsArr.length; ++i){ | ||
149 | fieldSqlPart = fieldSqlPart + (",'"+fieldsArr[i]+"'"); | ||
150 | } | ||
151 | if(!StringUtils.isEmpty(fieldSqlPart)) { | ||
152 | fieldSqlPart = fieldSqlPart.substring(1); | ||
153 | } | ||
154 | //获取数据采集pg数据库,实时数据获取统一从数据采集pg库获取 | ||
155 | JdbcTemplate pgDB = (JdbcTemplate)SpringContextUtils.getBean("pg-db"); | ||
156 | //主库数据源 | ||
157 | JdbcTemplate masterDB = (JdbcTemplate)SpringContextUtils.getBean("master"); | ||
158 | //封装最终查询结果 | ||
159 | List<Map<String,Object>> result = new ArrayList<Map<String,Object>>(); | ||
160 | //计算数据同样从pg数据库中获取 | ||
161 | List<Map<String, Object>> factoryDesign = masterDB.queryForList("select * from sys_factory_device where depart_id = '"+departId+"'"); | ||
162 | if(factoryDesign==null||factoryDesign.size()==0) { | ||
163 | return result; | ||
164 | } | ||
165 | //根据厂id获取到分表表名 | ||
166 | String countTableName = ConvertUtils.getString(factoryDesign.get(0).get("device_id")); | ||
167 | //如果查询数据为实时数据,则直接查询pg数据库 | ||
168 | if(tableType.equals("0")) { | ||
169 | //实时数据只能获取近24小时的,判断开始时间结束时间,如果存在跨天情况,则需要查询多张表数据 | ||
170 | try { | ||
171 | SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); | ||
172 | SimpleDateFormat formatter2 = new SimpleDateFormat("yyyyMMdd"); | ||
173 | Date date = formatter.parse(startTime); | ||
174 | String dateString = formatter2.format(date); | ||
175 | String tsStartTime = String.valueOf(DateUtils.getMillis(DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm:ss"))/1000); | ||
176 | String tsEndTime = String.valueOf(DateUtils.getMillis(DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm:ss"))/1000); | ||
177 | //当实时表获取不到数据时,默认从最新数据记录表获取,当pg主库宕机时,自动切换备库查询 | ||
178 | List<Map<String, Object>> dataList = null; | ||
179 | String sql = "select ts as time,nm as monitorId,v as monitorValue from xsinsert"+dateString+" where nm in("+fieldSqlPart.toString()+") and factoryname = '"+countTableName+"' and ts >= ? and ts < ? "; | ||
180 | try { | ||
181 | dataList = pgDB.queryForList(sql,tsStartTime,tsEndTime); | ||
182 | } catch (DataAccessException e) { | ||
183 | //当存在数据库宕机时,切换备库,兼容老项目不涉及到数采备库的情况 | ||
184 | Object pgDbSlaveObject = SpringContextUtils.getBean("pg-db-slave"); | ||
185 | if(pgDbSlaveObject!=null) { | ||
186 | pgDB = (JdbcTemplate) pgDbSlaveObject; | ||
187 | dataList = pgDB.queryForList(sql,tsStartTime,tsEndTime); | ||
188 | } | ||
189 | } | ||
190 | //List<Map<String, Object>> dataList = pgDB.queryForList("select ts as time,nm as monitorId,v as monitorValue from xsinsert"+dateString+" where nm in("+fieldSqlPart.toString()+") and factoryname = '"+countTableName+"' and ts >= ? and ts < ? ",tsStartTime,tsEndTime); | ||
191 | dataRowToCellByTime(result,dataList); | ||
192 | } catch (ParseException e) { | ||
193 | e.printStackTrace(); | ||
194 | } | ||
195 | }else { | ||
196 | //查询厂对应表的计算数据 | ||
197 | try { | ||
198 | String tsStartTime = String.valueOf(DateUtils.getMillis(DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm:ss"))/1000); | ||
199 | String tsEndTime = String.valueOf(DateUtils.getMillis(DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm:ss"))/1000); | ||
200 | List<Map<String, Object>> dataList = null; | ||
201 | try { | ||
202 | dataList = pgDB.queryForList("select ts as time,nm as monitorId,v as monitorValue from "+countTableName+"_count where nm in("+fieldSqlPart+") and ts>=? and ts <? ",tsStartTime,tsEndTime); | ||
203 | } catch (DataAccessException e) { | ||
204 | //切换到备库数据源 | ||
205 | Object pgDbSlaveObject = SpringContextUtils.getBean("pg-db-slave"); | ||
206 | if(pgDbSlaveObject!=null) { | ||
207 | pgDB = (JdbcTemplate) pgDbSlaveObject; | ||
208 | dataList = pgDB.queryForList("select ts as time,nm as monitorId,v as monitorValue from "+countTableName+"_count where nm in("+fieldSqlPart+") and ts>=? and ts <? ",tsStartTime,tsEndTime); | ||
209 | } | ||
210 | } | ||
211 | dataRowToCellByTime(result,dataList); | ||
212 | } catch (Exception e1) { | ||
213 | e1.printStackTrace(); | ||
214 | } | ||
215 | } | ||
216 | return result; | ||
217 | } | ||
218 | |||
219 | /** | ||
220 | * pg获取高表数据转换为mysql的宽表数据结构,[{ts:1,A:10},{ts:1,B:10},{ts:1,C:10}]转为[{ts:1,A:10,B:10,C:10}] | ||
221 | * @param result | ||
222 | * @param dataList | ||
223 | * @return | ||
224 | */ | ||
225 | private List<Map<String,Object>> dataRowToCellByTime(List<Map<String,Object>> result,List<Map<String, Object>> dataList){ | ||
226 | //存储时间排序集合 | ||
227 | List<String> orderTsList = new ArrayList<String>(); | ||
228 | //将同一时间数据在内从中行转列 | ||
229 | Map<String,Map<String,Object>> resultMap = new HashMap<String,Map<String,Object>>(); | ||
230 | //对历史结果按照时间进行关联 | ||
231 | for(Map<String, Object> tempMap : dataList) { | ||
232 | String key = tempMap.get("time").toString(); | ||
233 | Map<String,Object> fieldDataMap = resultMap.get(key); | ||
234 | if(fieldDataMap==null||fieldDataMap.size()==0) { | ||
235 | //将时间戳加入到集合内 | ||
236 | orderTsList.add(key); | ||
237 | fieldDataMap = new HashMap<String,Object>(); | ||
238 | fieldDataMap.put(tempMap.get("monitorId").toString(), tempMap.get("monitorValue")); | ||
239 | resultMap.put(key, fieldDataMap); | ||
240 | }else { | ||
241 | fieldDataMap.put(tempMap.get("monitorId").toString(), tempMap.get("monitorValue")); | ||
242 | } | ||
243 | } | ||
244 | //重新构造新的结果集对象,存储行转列数据 | ||
245 | for(String order : orderTsList) { | ||
246 | Map<String,Object> map = resultMap.get(order); | ||
247 | map.put("time", order); | ||
248 | result.add(map); | ||
249 | } | ||
250 | return result; | ||
251 | } | ||
252 | /** | ||
253 | * 计算分库分钟、天、月数据,计算结果为按照时间粒度累加的数据,同时会统计累加次数,如果要求平均值,请在自己逻辑内使用获取结果/累加次数 | ||
254 | * @param listMap 分库基础数据 | ||
255 | * @param timePeriod 时间类型【 mm分钟 d天 m月 y年】 | ||
256 | * @param fields 指标字段,字段间要用英文逗号分隔,例如 a,b,c | ||
257 | * @return | ||
258 | */ | ||
259 | public List<Map<String, Object>> handleResult(List<Map<String, Object>> listMap,String timePeriod,String fields){ | ||
260 | //如果timePeriod是d时间粒度是天,m时间粒度是月 | ||
261 | List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>(); | ||
262 | //字段切割 | ||
263 | String[] fieldArragTemp = fields.split(","); | ||
264 | List<String> fieldArrag = new ArrayList<String>(); | ||
265 | for(String field : fieldArragTemp) { | ||
266 | if(!StringUtils.isEmpty(field)) { | ||
267 | fieldArrag.add(field); | ||
268 | } | ||
269 | } | ||
270 | String pattern=""; | ||
271 | if(timePeriod.equals("mm")) { | ||
272 | pattern="yyyy-MM-dd HH:mm"; | ||
273 | }else if(timePeriod.equals("d")) { | ||
274 | pattern="yyyy-MM-dd"; | ||
275 | }else if(timePeriod.equals("m")) { | ||
276 | pattern="yyyy-MM"; | ||
277 | }else if(timePeriod.equals("y")) { | ||
278 | pattern="yyyy"; | ||
279 | } | ||
280 | for(Map<String,Object> map:listMap) { | ||
281 | String groupTag = DateUtils.formatDate(DateUtils.getDate(Long.valueOf(map.get("time").toString()+"000")), pattern); | ||
282 | boolean is_have=false; | ||
283 | for(Map<String,Object> resultMap:resultList) { | ||
284 | //获取处理结果中的时间 | ||
285 | String resultGroupTag = DateUtils.formatDate(DateUtils.getDate(Long.valueOf(resultMap.get("time").toString()+"000")), pattern); | ||
286 | if(String.valueOf(groupTag).equals(String.valueOf(resultGroupTag))) { | ||
287 | for(String key:fieldArrag) { | ||
288 | //循环累加 | ||
289 | int v = Integer.valueOf(resultMap.get(key+"_tag").toString()); | ||
290 | resultMap.put(key, DoubleOperaUtils.bigDecimalRound(ConvertUtils.getDouble(resultMap.get(key), 0.00)+ConvertUtils.getDouble(map.get(key), 0.00), 2)); | ||
291 | v=v+1; | ||
292 | resultMap.put(key+"_tag", v); | ||
293 | } | ||
294 | is_have=true; | ||
295 | break; | ||
296 | } | ||
297 | } | ||
298 | if(!is_have) { | ||
299 | Map<String,Object> newmap = new HashMap<String,Object>(); | ||
300 | //加入时间 | ||
301 | newmap.put("time_tag", 1); | ||
302 | newmap.put("time", map.get("time")); | ||
303 | for(String key:fieldArrag) { | ||
304 | newmap.put(key+"_tag", 1); | ||
305 | newmap.put(key, map.get(key)); | ||
306 | } | ||
307 | resultList.add(newmap); | ||
308 | } | ||
309 | } | ||
310 | return resultList; | ||
311 | } | ||
312 | /** | ||
313 | * 工艺报表数据获取 | ||
314 | * @param departId | ||
315 | * @param fields | ||
316 | * @param startTime | ||
317 | * @param endTime | ||
318 | * @param reportType | ||
319 | * @return | ||
320 | */ | ||
321 | @Override | ||
322 | public List<Map<String, Object>> queryReportData(String departId, String fields, String startTime, String endTime, String reportType) { | ||
323 | String[] fieldArray = fields.split(","); | ||
324 | List<Map<String, Object>> list = queryFactoryInfos(departId,fields,startTime,endTime,"1"); | ||
325 | String pattern = "yyyy-MM-dd HH:mm"; | ||
326 | if("day".equals(reportType)) { | ||
327 | pattern = "yyyy-MM-dd"; | ||
328 | list = handleResult(list,"d",fields); | ||
329 | }else if("month".equals(reportType)) { | ||
330 | pattern = "yyyy-MM"; | ||
331 | list = handleResult(list,"m",fields); | ||
332 | } | ||
333 | //如果报表类型是日报表或月报表,则对解决求平均 | ||
334 | if(reportType.equals("day")||reportType.equals("month")) { | ||
335 | for(Map<String, Object> map : list) { | ||
336 | //除time字段外,求平均值 | ||
337 | for(String field : fieldArray) { | ||
338 | Object value = map.get(field); | ||
339 | Object valueSumCount = map.get(field+"_tag"); | ||
340 | //用平均值改写原来合计值 | ||
341 | map.put(field, DoubleOperaUtils.bigDecimalRound(ConvertUtils.getDouble(value, 0.00)/ConvertUtils.getDouble(valueSumCount, 1.00), 2)); | ||
342 | } | ||
343 | //格式化时间 | ||
344 | map.put("time", DateUtils.formatDate(DateUtils.getCalendar(Long.parseLong(ConvertUtils.getString(map.get("time"))+"000")), pattern)); | ||
345 | } | ||
346 | }else { | ||
347 | for(Map<String, Object> map : list) { | ||
348 | //格式化时间 | ||
349 | map.put("time", DateUtils.formatDate(DateUtils.getCalendar(Long.parseLong(ConvertUtils.getString(map.get("time"))+"000")), pattern)); | ||
350 | } | ||
351 | } | ||
352 | List<Map<String, Object>> resultList = new ArrayList<Map<String,Object>>(); | ||
353 | for(Map<String, Object> map : list) { | ||
354 | Map<String,Object> newMap = new HashMap<String,Object>(); | ||
355 | Set<String> keys = map.keySet(); | ||
356 | for(String key : keys) { | ||
357 | if(!key.endsWith("_tag")) { | ||
358 | newMap.put(key, map.get(key)); | ||
359 | } | ||
360 | } | ||
361 | resultList.add(newMap); | ||
362 | } | ||
363 | return resultList; | ||
364 | } | ||
137 | } | 365 | } | ... | ... |
-
请 注册 或 登录 后发表评论