9873efc0 张雷

奥体项目数据报表时间修改

1 个父辈 2aa800a1
......@@ -193,4 +193,15 @@ public class OlympicCenterController {
return result;
}
@ApiOperation(value="获取历史报表数据", notes="获取历史报表数据")
@GetMapping(value = "/getHistoryReportData")
public Result<List<Map<String,Object>>> getHistoryReportData(String dataType,String loction){
Result<List<Map<String,Object>>> result = new Result<List<Map<String,Object>>>();
List<Map<String,Object>> list = new ArrayList<>();
list = olympicCenterService.getHistoryReportData(dataType, loction);
result.setResult(list);
result.setSuccess(true);
return result;
}
}
......
......@@ -37,4 +37,6 @@ public interface IOlympicCenterService {
List<Map<String, Object>> getPumpRealTimeReport(String sourceType);
List<Map<String, Object>> getHistoryReportClos(String dataType, String loction);
List<Map<String, Object>> getHistoryReportData(String dataType, String loction);
}
......
......@@ -1306,6 +1306,52 @@ public class OlympicCenterServiceImpl implements IOlympicCenterService {
return list;
}
@Override
public List<Map<String, Object>> getHistoryReportData(String dataType, String loction) {
List<Map<String, Object>> list = new ArrayList<>();
List<WhatStructData> closList = new ArrayList<>();
Map<String, Object> monitorMap = new HashMap<>();
if("flow".equals(dataType)){
closList = whatStructDataMapper.getFlowList(loction);
}else if("pressure".equals(dataType)){
closList = whatStructDataMapper.getPressureList(loction);
}else if("temperature".equals(dataType)){
closList = whatStructDataMapper.getTemperatureList(loction);
}else{
return list;
}
String pgField = "";
if(closList.size() == 0){
return list;
}else{
for (WhatStructData whatStructData:closList) {
pgField = pgField +","+whatStructData.getMonitorId();
if(whatStructData.getMonitorId()!=null){
monitorMap.put(whatStructData.getMonitorId(),whatStructData.getId());
}
}
}
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -30);
String startDate = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
String start = startDate + " 00:00:00";
String endDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
String end = endDate + " 23:59:59";
List<Map<String, Object>> dataList = factoryInfoService.queryReportData(AT_ID, pgField, start, end,"hour");
for (Map<String, Object> map : dataList) {
Map<String, Object> newMap = new HashMap<>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
if("time".equals(entry.getKey())){
newMap.put(entry.getKey(),entry.getValue());
}else {
newMap.put(monitorMap.get(entry.getKey()).toString(),entry.getValue());
}
}
list.add(newMap);
}
return list;
}
private String getStatus1(Map<String, Object> map, List<Map<String, Object>> list, String pumpId) {
String status = "停止";
Map<String, Object> bean = list.get(0);
......
......@@ -12,4 +12,17 @@ public interface IFmPgQueryService {
List<Map<String, Object>> queryMonitorForReport(String factoryId, String fields);
/**
*
* @param departId 厂区编号
* @param fields 要获取的字段集合,字段间以逗号分隔,例如 a,b,c
* @param startTime 开始时间 当传入""的时候,获取time最大的一条记录
* @param endTime 结束时间 当传入""的时候,获取time最大的一条记录
* @param tableType 表类型 0实时 1计算
* @return
*/
public List<Map<String,Object>> queryFactoryInfos(String departId,String fields,String startTime,String endTime,String tableType);
public List<Map<String, Object>> queryReportData(String departId,String fields,String startTime,String endTime,String reportType);
}
......
package com.skua.modules.monitor.service.impl;
import com.skua.core.context.SpringContextUtils;
import com.skua.core.util.ConvertUtils;
import com.skua.core.util.DateUtils;
import com.skua.core.util.DoubleOperaUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import com.skua.modules.monitor.service.IFmPgQueryService;
......@@ -134,4 +138,228 @@ public class FmPgQueryServiceImpl implements IFmPgQueryService {
result = pgDb.queryForList(sql);
return result;
}
@Override
public List<Map<String, Object>> queryFactoryInfos(String departId, String fields, String startTime,String endTime, String tableType) {
//获取要查询的指标
String fieldSqlPart = "";
String[] fieldsArr = fields.split(",");
for (int i = 0; i < fieldsArr.length; ++i){
fieldSqlPart = fieldSqlPart + (",'"+fieldsArr[i]+"'");
}
if(!StringUtils.isEmpty(fieldSqlPart)) {
fieldSqlPart = fieldSqlPart.substring(1);
}
//获取数据采集pg数据库,实时数据获取统一从数据采集pg库获取
JdbcTemplate pgDB = (JdbcTemplate)SpringContextUtils.getBean("pg-db");
//主库数据源
JdbcTemplate masterDB = (JdbcTemplate)SpringContextUtils.getBean("master");
//封装最终查询结果
List<Map<String,Object>> result = new ArrayList<Map<String,Object>>();
//计算数据同样从pg数据库中获取
List<Map<String, Object>> factoryDesign = masterDB.queryForList("select * from sys_factory_device where depart_id = '"+departId+"'");
if(factoryDesign==null||factoryDesign.size()==0) {
return result;
}
//根据厂id获取到分表表名
String countTableName = ConvertUtils.getString(factoryDesign.get(0).get("device_id"));
//如果查询数据为实时数据,则直接查询pg数据库
if(tableType.equals("0")) {
//实时数据只能获取近24小时的,判断开始时间结束时间,如果存在跨天情况,则需要查询多张表数据
try {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat formatter2 = new SimpleDateFormat("yyyyMMdd");
Date date = formatter.parse(startTime);
String dateString = formatter2.format(date);
String tsStartTime = String.valueOf(DateUtils.getMillis(DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm:ss"))/1000);
String tsEndTime = String.valueOf(DateUtils.getMillis(DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm:ss"))/1000);
//当实时表获取不到数据时,默认从最新数据记录表获取,当pg主库宕机时,自动切换备库查询
List<Map<String, Object>> dataList = null;
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 < ? ";
try {
dataList = pgDB.queryForList(sql,tsStartTime,tsEndTime);
} catch (DataAccessException e) {
//当存在数据库宕机时,切换备库,兼容老项目不涉及到数采备库的情况
Object pgDbSlaveObject = SpringContextUtils.getBean("pg-db-slave");
if(pgDbSlaveObject!=null) {
pgDB = (JdbcTemplate) pgDbSlaveObject;
dataList = pgDB.queryForList(sql,tsStartTime,tsEndTime);
}
}
//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);
dataRowToCellByTime(result,dataList);
} catch (ParseException e) {
e.printStackTrace();
}
}else {
//查询厂对应表的计算数据
try {
String tsStartTime = String.valueOf(DateUtils.getMillis(DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm:ss"))/1000);
String tsEndTime = String.valueOf(DateUtils.getMillis(DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm:ss"))/1000);
List<Map<String, Object>> dataList = null;
try {
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);
} catch (DataAccessException e) {
//切换到备库数据源
Object pgDbSlaveObject = SpringContextUtils.getBean("pg-db-slave");
if(pgDbSlaveObject!=null) {
pgDB = (JdbcTemplate) pgDbSlaveObject;
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);
}
}
dataRowToCellByTime(result,dataList);
} catch (Exception e1) {
e1.printStackTrace();
}
}
return result;
}
/**
* pg获取高表数据转换为mysql的宽表数据结构,[{ts:1,A:10},{ts:1,B:10},{ts:1,C:10}]转为[{ts:1,A:10,B:10,C:10}]
* @param result
* @param dataList
* @return
*/
private List<Map<String,Object>> dataRowToCellByTime(List<Map<String,Object>> result,List<Map<String, Object>> dataList){
//存储时间排序集合
List<String> orderTsList = new ArrayList<String>();
//将同一时间数据在内从中行转列
Map<String,Map<String,Object>> resultMap = new HashMap<String,Map<String,Object>>();
//对历史结果按照时间进行关联
for(Map<String, Object> tempMap : dataList) {
String key = tempMap.get("time").toString();
Map<String,Object> fieldDataMap = resultMap.get(key);
if(fieldDataMap==null||fieldDataMap.size()==0) {
//将时间戳加入到集合内
orderTsList.add(key);
fieldDataMap = new HashMap<String,Object>();
fieldDataMap.put(tempMap.get("monitorId").toString(), tempMap.get("monitorValue"));
resultMap.put(key, fieldDataMap);
}else {
fieldDataMap.put(tempMap.get("monitorId").toString(), tempMap.get("monitorValue"));
}
}
//重新构造新的结果集对象,存储行转列数据
for(String order : orderTsList) {
Map<String,Object> map = resultMap.get(order);
map.put("time", order);
result.add(map);
}
return result;
}
/**
* 计算分库分钟、天、月数据,计算结果为按照时间粒度累加的数据,同时会统计累加次数,如果要求平均值,请在自己逻辑内使用获取结果/累加次数
* @param listMap 分库基础数据
* @param timePeriod 时间类型【 mm分钟 d天 m月 y年】
* @param fields 指标字段,字段间要用英文逗号分隔,例如 a,b,c
* @return
*/
public List<Map<String, Object>> handleResult(List<Map<String, Object>> listMap,String timePeriod,String fields){
//如果timePeriod是d时间粒度是天,m时间粒度是月
List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
//字段切割
String[] fieldArragTemp = fields.split(",");
List<String> fieldArrag = new ArrayList<String>();
for(String field : fieldArragTemp) {
if(!StringUtils.isEmpty(field)) {
fieldArrag.add(field);
}
}
String pattern="";
if(timePeriod.equals("mm")) {
pattern="yyyy-MM-dd HH:mm";
}else if(timePeriod.equals("d")) {
pattern="yyyy-MM-dd";
}else if(timePeriod.equals("m")) {
pattern="yyyy-MM";
}else if(timePeriod.equals("y")) {
pattern="yyyy";
}
for(Map<String,Object> map:listMap) {
String groupTag = DateUtils.formatDate(DateUtils.getDate(Long.valueOf(map.get("time").toString()+"000")), pattern);
boolean is_have=false;
for(Map<String,Object> resultMap:resultList) {
//获取处理结果中的时间
String resultGroupTag = DateUtils.formatDate(DateUtils.getDate(Long.valueOf(resultMap.get("time").toString()+"000")), pattern);
if(String.valueOf(groupTag).equals(String.valueOf(resultGroupTag))) {
for(String key:fieldArrag) {
//循环累加
int v = Integer.valueOf(resultMap.get(key+"_tag").toString());
resultMap.put(key, DoubleOperaUtils.bigDecimalRound(ConvertUtils.getDouble(resultMap.get(key), 0.00)+ConvertUtils.getDouble(map.get(key), 0.00), 2));
v=v+1;
resultMap.put(key+"_tag", v);
}
is_have=true;
break;
}
}
if(!is_have) {
Map<String,Object> newmap = new HashMap<String,Object>();
//加入时间
newmap.put("time_tag", 1);
newmap.put("time", map.get("time"));
for(String key:fieldArrag) {
newmap.put(key+"_tag", 1);
newmap.put(key, map.get(key));
}
resultList.add(newmap);
}
}
return resultList;
}
/**
* 工艺报表数据获取
* @param departId
* @param fields
* @param startTime
* @param endTime
* @param reportType
* @return
*/
@Override
public List<Map<String, Object>> queryReportData(String departId, String fields, String startTime, String endTime, String reportType) {
String[] fieldArray = fields.split(",");
List<Map<String, Object>> list = queryFactoryInfos(departId,fields,startTime,endTime,"1");
String pattern = "yyyy-MM-dd HH:mm";
if("day".equals(reportType)) {
pattern = "yyyy-MM-dd";
list = handleResult(list,"d",fields);
}else if("month".equals(reportType)) {
pattern = "yyyy-MM";
list = handleResult(list,"m",fields);
}
//如果报表类型是日报表或月报表,则对解决求平均
if(reportType.equals("day")||reportType.equals("month")) {
for(Map<String, Object> map : list) {
//除time字段外,求平均值
for(String field : fieldArray) {
Object value = map.get(field);
Object valueSumCount = map.get(field+"_tag");
//用平均值改写原来合计值
map.put(field, DoubleOperaUtils.bigDecimalRound(ConvertUtils.getDouble(value, 0.00)/ConvertUtils.getDouble(valueSumCount, 1.00), 2));
}
//格式化时间
map.put("time", DateUtils.formatDate(DateUtils.getCalendar(Long.parseLong(ConvertUtils.getString(map.get("time"))+"000")), pattern));
}
}else {
for(Map<String, Object> map : list) {
//格式化时间
map.put("time", DateUtils.formatDate(DateUtils.getCalendar(Long.parseLong(ConvertUtils.getString(map.get("time"))+"000")), pattern));
}
}
List<Map<String, Object>> resultList = new ArrayList<Map<String,Object>>();
for(Map<String, Object> map : list) {
Map<String,Object> newMap = new HashMap<String,Object>();
Set<String> keys = map.keySet();
for(String key : keys) {
if(!key.endsWith("_tag")) {
newMap.put(key, map.get(key));
}
}
resultList.add(newMap);
}
return resultList;
}
}
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!