/** * */ package com.xcgl.dataview.service.impl; import java.text.DecimalFormat; import java.text.ParseException; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.activiti.engine.impl.util.json.JSONObject; import org.apache.log4j.Logger; import org.jeecgframework.core.common.exception.BusinessException; import org.jeecgframework.core.util.DateUtils; import org.jeecgframework.p3.core.common.utils.DateUtil; import org.jeecgframework.p3.core.utils.common.StringUtils; import org.jeecgframework.web.system.service.SystemService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; //import com.alibaba.druid.sql.dialect.oracle.ast.clause.SubqueryFactoringClause.Entry; import com.xcgl.dataview.entity.DeviceStatus; import com.xcgl.dataview.entity.DeviceStatusSumDto; import com.xcgl.dataview.entity.DeviceType; import com.xcgl.dataview.entity.GonglvDto; import com.xcgl.dataview.entity.NenghaoDto; import com.xcgl.dataview.entity.NenghaoType; import com.xcgl.dataview.entity.SensorLiveTextDto; import com.xcgl.dataview.entity.WarningSumDto; import com.xcgl.dataview.service.DataviewServiceI; import com.xcgl.utils.XcglDateUtils; import cn.com.lzt.accessory.controller.AccessoryController; /** * @author xzx * */ @Service("dataviewService") public class DataviewServiceImpl implements DataviewServiceI { private static final Logger logger = Logger.getLogger(AccessoryController.class); @Autowired private SystemService systemService; /** * 获取最新的全部设备瞬时功率数据 * @param valuetime 取值时间戳 * @return * @throws Exception */ @Override public List getGonglvData(Date valuetime) throws BusinessException{ List list = new ArrayList<>(); StringBuilder gvSql = new StringBuilder(); gvSql.append(" SELECT "); gvSql.append(" id,code,name,value,recordtime"); gvSql.append(" FROM (\r\n" + "select deviceid as id ,devicename as name, de.code as code,param_value as value,record.recordtime from p_sensor_record record \r\n" + "left join p_device de on record.deviceid = de.id\r\n" + "where record.typeunit = \"KW\" \r\n" + //只有单位是千瓦的设备才是可以监测瞬时功率 "order by record.recordtime desc) a "); gvSql.append(" group by id "); List> result = systemService.findForJdbc(gvSql.toString()); if(result != null && result.size() > 0) { for(Map res : result) { GonglvDto gldto = new GonglvDto(); gldto.setId(res.get("id").toString()); gldto.setCode(res.get("code").toString()); gldto.setName(res.get("name").toString()); gldto.setValue(Double.valueOf(res.get("value").toString())); list.add(gldto); } }else { list.add(GonglvDto.getDemoInstance("1#变压器")); list.add(GonglvDto.getDemoInstance("2#变压器")); list.add(GonglvDto.getDemoInstance("舞台照明1")); list.add(GonglvDto.getDemoInstance("舞台照明2")); list.add(GonglvDto.getDemoInstance("主楼照明")); list.add(GonglvDto.getDemoInstance("热泵1")); list.add(GonglvDto.getDemoInstance("热泵2")); list.add(GonglvDto.getDemoInstance("热泵3")); list.add(GonglvDto.getDemoInstance("热泵4")); list.add(GonglvDto.getDemoInstance("变电所动力照明柜")); list.add(GonglvDto.getDemoInstance("北区办公及改造楼照明及空调")); list.add(GonglvDto.getDemoInstance("两栋小楼办公室电源")); list.add(GonglvDto.getDemoInstance("VRV室外空调机")); } return list; } /** * 获取过去monthcount个月连续的月能耗数据,当前月份为7, 获取1-6月份的月能耗 * 算法概述: * 1、先查询每个设备的平均功率(算数平均值) * 2、按月计算所有设备平均功率合计 * 3、累计平均功率 * 24 * 30(31)= 月能耗 * @param monthcount * @return * @throws BusinessException */ @Override public List getLastContinuousNenghaoData(int monthcount)throws BusinessException{ List list = new ArrayList<>(); StringBuilder nhSql = new StringBuilder(); Date qryDate = DateUtil.alterMonty(DateUtil.getCurrDate(), 0- monthcount); nhSql.append("select sum(value) as total ,month from "); nhSql.append(" ( SELECT "); nhSql.append(" pointid, pointname, pointcode,AVG(param_value) as value ,left(recordtime,7) as month"); nhSql.append(" FROM p_sensor_record "); //查询条件为当前月份往前退n个月,本月不查询 nhSql.append(" where typeunit = \"KW\" and left(recordtime,7) >= \"").append(DateUtils.formatDate(qryDate, "yyyy-MM")).append("\" "); nhSql.append(" and left(recordtime,7) < \"").append( DateUtils.formatDate(DateUtil.getCurrDate(), "yyyy-MM")).append("\""); nhSql.append(" group by id ) tmp "); nhSql.append(" group by month"); List> result = systemService.findForJdbc(nhSql.toString()); if(result != null && result.size() > 0) { for(Map res : result) { int year = Integer.parseInt(res.get("month").toString().substring(0,4)); int month = Integer.parseInt(res.get("month").toString().substring(5,7)); double totleGV = Double.parseDouble( res.get("total").toString()); double gh = (month == 1 ||month == 3 || month == 5|| month == 7 || month == 8 ||month == 10 ||month == 12) ? totleGV * 31 : ((month == 2 && year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? totleGV * 29 :( month == 2 ? totleGV * 28: totleGV * 30)); list.add(NenghaoDto.getDemoInstance(NenghaoType.MONTH_NENGHAO, year, month, 1, gh)); } }else { NenghaoDto month_1 = NenghaoDto.getDemoInstance(NenghaoType.MONTH_NENGHAO, 2018, 1, 1, 21230.0); NenghaoDto month_2 = NenghaoDto.getDemoInstance(NenghaoType.MONTH_NENGHAO, 2018, 2, 1, 18911.7); NenghaoDto month_3 = NenghaoDto.getDemoInstance(NenghaoType.MONTH_NENGHAO, 2018, 3, 1, 21033.3); NenghaoDto month_4 = NenghaoDto.getDemoInstance(NenghaoType.MONTH_NENGHAO, 2018, 4, 1, 18122.8); NenghaoDto month_5 = NenghaoDto.getDemoInstance(NenghaoType.MONTH_NENGHAO, 2018, 5, 1, 18278.9); NenghaoDto month_6 = NenghaoDto.getDemoInstance(NenghaoType.MONTH_NENGHAO, 2018, 6, 1, 32330.6); list.add(month_1); list.add(month_2); list.add(month_3); list.add(month_4); list.add(month_5); list.add(month_6); } return list; } @Override public HashMap getNenghaoSumData(boolean needUnit)throws BusinessException{ HashMap nengHaoRiYueJi = new HashMap (); StringBuilder nhSql = new StringBuilder(); //本季度第一天 Date curQuarterDate = XcglDateUtils.getCurrentQuarterStartTime(); nhSql.append("select sum(value) as total ,day ,month from \r\n" + " ( SELECT \r\n" + " pointid, pointname, pointcode,AVG(param_value) as value ,left(recordtime,10) as day,left(recordtime,7) as month\r\n" + " FROM p_sensor_record \r\n" + " where typeunit = 'KW' and left(recordtime,10) >= '").append(DateUtils.formatDate(curQuarterDate, "yyyy-MM-dd")).append("'\r\n"); nhSql.append(" and left(recordtime,10) <= '").append(DateUtils.formatDate(new Date(), "yyyy-MM-dd")).append("'\r\n"); nhSql.append(" group by pointid,day ) tmp \r\n" + " group by day"); List> result = systemService.findForJdbc(nhSql.toString()); HashMap monthNenghao = new HashMap (); DecimalFormat df = new DecimalFormat("#"); if(result != null && result.size() > 0) { for(Map res : result) { String month = res.get("month").toString(); String day = res.get("day").toString(); //每日功耗(根据每日功率平均值*24小时计算所得) Double dayTotleGV = new Double(res.get("total").toString()) * 24; if(day.equals(DateUtils.formatDate( new Date(), "yyyy-MM-dd"))) { nengHaoRiYueJi.put("rinenghao", df.format(dayTotleGV)+ (needUnit ? "/kwh":"")); } if(monthNenghao.containsKey(month)) { monthNenghao.put(month, monthNenghao.get(month).doubleValue()+dayTotleGV.doubleValue()); }else { monthNenghao.put(month, dayTotleGV); } } //计算月能耗 if(monthNenghao.containsKey(DateUtils.formatDate(new Date(), "yyyy-MM"))) { nengHaoRiYueJi.put("yuenenghao", df.format(monthNenghao.get(DateUtils.formatDate(new Date(), "yyyy-MM")))+ (needUnit ? "/kwh":"")); } //计算季能耗 double jinenghao = 0; for (java.util.Map.Entry entry : monthNenghao.entrySet()) { jinenghao += entry.getValue(); } nengHaoRiYueJi.put("jinenghao", df.format(jinenghao)+ (needUnit ? "/kwh":"")); } if(!nengHaoRiYueJi.containsKey("rinenghao")) nengHaoRiYueJi.put("rinenghao", "0.00"+ (needUnit ? "/kwh":"")); if(!nengHaoRiYueJi.containsKey("yuenenghao")) nengHaoRiYueJi.put("yuenenghao", "0.00"+ (needUnit ? "/kwh":"")); if(!nengHaoRiYueJi.containsKey("jinenghao")) nengHaoRiYueJi.put("jinenghao", "0.00" +(needUnit ? "/kwh":"")); return nengHaoRiYueJi; } @Override public HashMap getNenghaoSumDataByProject(boolean needUnit,String projectid)throws BusinessException{ HashMap nengHaoRiYueJi = new HashMap (); StringBuilder nhSql = new StringBuilder(); //本季度第一天 Date curQuarterDate = XcglDateUtils.getCurrentQuarterStartTime(); nhSql.append("select sum(value) as total ,day ,month from \r\n" + " ( SELECT \r\n" + " pointid, pointname, pointcode,AVG(param_value) as value ,left(recordtime,10) as day,left(recordtime,7) as month\r\n" + " FROM p_sensor_record \r\n" + " where projectid = '"+projectid+"' " + " and typeunit = 'KW' and left(recordtime,10) >= '").append(DateUtils.formatDate(curQuarterDate, "yyyy-MM-dd")).append("'\r\n"); nhSql.append(" and left(recordtime,10) <= '").append(DateUtils.formatDate(new Date(), "yyyy-MM-dd")).append("'\r\n"); nhSql.append(" group by pointid,day ) tmp \r\n" + " group by day"); List> result = systemService.findForJdbc(nhSql.toString()); HashMap monthNenghao = new HashMap (); DecimalFormat df = new DecimalFormat("#"); if(result != null && result.size() > 0) { for(Map res : result) { String month = res.get("month").toString(); String day = res.get("day").toString(); //每日功耗(根据每日功率平均值*24小时计算所得) Double dayTotleGV = new Double(res.get("total").toString()) * 24; if(day.equals(DateUtils.formatDate( new Date(), "yyyy-MM-dd"))) { nengHaoRiYueJi.put("rinenghao", df.format(dayTotleGV)+ (needUnit ? "/kwh":"")); } if(monthNenghao.containsKey(month)) { monthNenghao.put(month, monthNenghao.get(month).doubleValue()+dayTotleGV.doubleValue()); }else { monthNenghao.put(month, dayTotleGV); } } //计算月能耗 if(monthNenghao.containsKey(DateUtils.formatDate(new Date(), "yyyy-MM"))) { nengHaoRiYueJi.put("yuenenghao", df.format(monthNenghao.get(DateUtils.formatDate(new Date(), "yyyy-MM")))+ (needUnit ? "/kwh":"")); } //计算季能耗 double jinenghao = 0; for (java.util.Map.Entry entry : monthNenghao.entrySet()) { jinenghao += entry.getValue(); } nengHaoRiYueJi.put("jinenghao", df.format(jinenghao)+ (needUnit ? "/kwh":"")); } if(!nengHaoRiYueJi.containsKey("rinenghao")) nengHaoRiYueJi.put("rinenghao", "0.00"+ (needUnit ? "/kwh":"")); if(!nengHaoRiYueJi.containsKey("yuenenghao")) nengHaoRiYueJi.put("yuenenghao", "0.00"+ (needUnit ? "/kwh":"")); if(!nengHaoRiYueJi.containsKey("jinenghao")) nengHaoRiYueJi.put("jinenghao", "0.00" +(needUnit ? "/kwh":"")); return nengHaoRiYueJi; } /** * 设备状态日期汇总,查询时间段内,每天正常设备数、离线设备数、报警设备数 * ps、以监测点为查询维度,不以设备为查询维度 * */ @Override public List getDeviceStatusSumData(String startDate, String endDate) throws BusinessException{ // String startDay ,endDay; // try { // startDay = DateUtils.parseDate(endDate, "yyyy-MM-dd").toString(); // endDay = DateUtils.parseDate(endDate, "yyyy-MM-dd").toString(); // }catch(ParseException e) // { // logger.error(e); // throw new BusinessException(e.getMessage()); // } List list = new ArrayList<>(); StringBuilder devStatusSql = new StringBuilder(); devStatusSql.append(" select count(distinct monitor_point_id) total ,left(abn.create_date,10 ) as dataday,state"); devStatusSql.append(" from p_sensor_abnormal abn"); devStatusSql.append(" where abn.state = '").append(DeviceStatus.OFFLINE.getIndex()).append("'"); devStatusSql.append(" and left(abn.create_date,10 ) >= '").append(startDate).append("'"); devStatusSql.append(" and left(abn.create_date,10 ) <= '").append(endDate).append("'"); devStatusSql.append(" group by dataday"); devStatusSql.append(" union all "); devStatusSql.append(" select count(distinct pointid) total ,left(recordtime,10 ) as dataday,qcdstate as state "); devStatusSql.append(" from p_sensor_record record "); devStatusSql.append(" where left(recordtime,10 ) >= '").append(startDate).append("'"); devStatusSql.append(" and left(recordtime,10 ) <= '").append(endDate).append("'"); devStatusSql.append(" group by dataday,qcdstate"); List> result = systemService.findForJdbc(devStatusSql.toString()); HashMap value = new HashMap(); if (result != null && result.size() > 0) { for (Map res : result) { DeviceStatusSumDto sumDto ; if(value.containsKey(res.get("dataday").toString())) { sumDto = value.get(res.get("dataday").toString()); }else { sumDto = new DeviceStatusSumDto(); } sumDto.setValueTime(new Date()); sumDto.setSumDate(res.get("dataday").toString()); if(res.get("state").toString().equals(String.valueOf(DeviceStatus.OFFLINE.getIndex()))) { sumDto.setOfflineCount(Integer.parseInt(res.get("total").toString())); }else if (res.get("state").toString().equals(String.valueOf(DeviceStatus.NORMAL.getIndex()))){ sumDto.setNormalCount(Integer.parseInt(res.get("total").toString())); }else {//超上限和超下限都要sum到一起 sumDto.setWarningCount(sumDto.getWarningCount() + Integer.parseInt(res.get("total").toString())); } value.put(res.get("dataday").toString(),sumDto); } for (DeviceStatusSumDto sumDto : value.values()) { list.add(sumDto) ; } } else { list.add(DeviceStatusSumDto.getDemoInstance("2018-07-01")); list.add(DeviceStatusSumDto.getDemoInstance("2018-07-02")); list.add(DeviceStatusSumDto.getDemoInstance("2018-07-03")); list.add(DeviceStatusSumDto.getDemoInstance("2018-07-04")); list.add(DeviceStatusSumDto.getDemoInstance("2018-07-05")); list.add(DeviceStatusSumDto.getDemoInstance("2018-07-06")); list.add(DeviceStatusSumDto.getDemoInstance("2018-07-07")); list.add(DeviceStatusSumDto.getDemoInstance("2018-07-08")); list.add(DeviceStatusSumDto.getDemoInstance("2018-07-09")); list.add(DeviceStatusSumDto.getDemoInstance("2018-07-10")); list.add(DeviceStatusSumDto.getDemoInstance("2018-07-11")); } return list; } @Override public List getWarningSumData(String date) throws BusinessException{ List list = new ArrayList<>(); //查询一个月内的数据 Date qryDate = DateUtil.alterMonty(new Date(), -1); StringBuilder devStatusSql = new StringBuilder(); devStatusSql.append(" select count(abnormal_cnt) as count ,type.type_code as type "); devStatusSql.append(" from p_sensor_abnormal abn"); devStatusSql.append(" left join p_sensor_monitor_point point on abn.monitor_point_id = point.id"); devStatusSql.append(" left join p_device dev on point.deviceid = dev.id"); devStatusSql.append(" left join p_device_type type on dev.type_id = type.id"); devStatusSql.append(" where type_code in ('1','2','3','4','5','6')"); devStatusSql.append(" and left(abn.create_date,10 ) > \"").append(DateUtils.formatDate(qryDate, "yyyy-MM-dd")).append("\""); devStatusSql.append(" group by type.type_code"); List> result = systemService.findForJdbc(devStatusSql.toString()); if (result != null && result.size() > 0) { for (Map res : result) { WarningSumDto sumDto = new WarningSumDto(); sumDto.setSum(Integer.parseInt( res.get("count").toString())); sumDto.setDeviceType(DeviceType.values()[Integer.parseInt(res.get("type").toString())-1]); list.add(sumDto); } } else { list.add(WarningSumDto.getDemoInstance(DeviceType.FENGJIFANG)); list.add(WarningSumDto.getDemoInstance(DeviceType.JISHUIJING)); list.add(WarningSumDto.getDemoInstance(DeviceType.KONGTIAO)); list.add(WarningSumDto.getDemoInstance(DeviceType.PENLINBENG)); list.add(WarningSumDto.getDemoInstance(DeviceType.SHENGHUOBENG)); list.add(WarningSumDto.getDemoInstance(DeviceType.XIAOFANGBENG)); } return list; } @Override public List getSensorLiveTextDto() throws BusinessException{ List list = new ArrayList<>(); // 只查询当天数据 //先查出最新的(table: p_sensor_record),正常以及超上下限的数据 StringBuilder sensorLiveSql = new StringBuilder(); sensorLiveSql.append("select deviceid,devicecode,devicename,pointid,pointcode,pointname,value,recordtime,qcdstate,value from("); sensorLiveSql.append(" select record.deviceid as deviceid ,record.devicename as devicename,de.code as devicecode,pointid,pointcode,pointname,param_value as value,recordtime,record.qcdstate as qcdstate"); sensorLiveSql.append(" from p_sensor_record record "); sensorLiveSql.append(" left join p_device de on record.deviceid = de.id"); // sensorLiveSql.append(" left join p_device de on record.deviceid = de.id"); sensorLiveSql.append(" where left(recordtime,10 ) = '").append(DateUtils.formatDate(new Date(),"yyyy-MM-dd")).append("'"); sensorLiveSql.append(" order by recordtime desc) tmp"); sensorLiveSql.append(" group by pointid"); List> result = systemService.findForJdbc(sensorLiveSql.toString()); HashMap pointids = new HashMap (); if (result != null && result.size() > 0) { for (Map res : result) { SensorLiveTextDto sliveDto = new SensorLiveTextDto(); sliveDto.setDevice_code(res.get("devicecode").toString()); sliveDto.setDevice_id(res.get("deviceid").toString()); sliveDto.setDevice_name(res.get("devicename").toString()); sliveDto.setSensor_id(res.get("pointid").toString()); sliveDto.setSensor_code(res.get("pointcode").toString()); sliveDto.setSensor_name(res.get("pointname").toString()); if(StringUtils.isNotEmpty(res.get("value").toString())) { sliveDto.setValue(Double.valueOf(res.get("value").toString())); }else { continue; } sliveDto.setStatus(DeviceStatus.getDeviceStatus(Integer.parseInt(res.get("qcdstate").toString()))); try { String recordtime = res.get("recordtime").toString(); // if(recordtime.length()>19) { // recordtime = recordtime.substring(0, 19); // } sliveDto.setValueTime(DateUtils.parseDate(recordtime, "yyyy-MM-dd HH:mm:ss")); } catch (ParseException e) { logger.error(e); } list.add(sliveDto); pointids.put(res.get("pointid").toString(),res.get("pointid").toString()); } } // 查询离线数据 sensorLiveSql = new StringBuilder(); sensorLiveSql.append( "select deviceid,devicecode,devicename,pointid,pointcode,pointname,value,recordtime,qcdstate,value from("); sensorLiveSql.append( " select point.deviceid as deviceid ,dev.name as devicename,dev.code as devicecode,monitor_point_id as pointid,"); sensorLiveSql.append( " point.code as pointcode, point.name as pointname, '' as value,abn.create_date as recordtime,abn.state as qcdstate"); sensorLiveSql.append(" from p_sensor_abnormal abn"); sensorLiveSql.append(" left join p_sensor_monitor_point point on abn.monitor_point_id = point.id"); sensorLiveSql.append(" left join p_device dev on point.deviceid = dev.id"); sensorLiveSql.append(" where abn.state = '").append(DeviceStatus.OFFLINE.getIndex()).append("'"); sensorLiveSql.append(" and left(abn.create_date,10 ) = '").append(DateUtils.formatDate(new Date(),"yyyy-MM-dd")).append("'"); sensorLiveSql.append(" order by abn.create_date desc) tmp"); sensorLiveSql.append(" group by pointid"); result = systemService.findForJdbc(sensorLiveSql.toString()); if (result != null && result.size() > 0) { for (Map res : result) { if(pointids.containsKey(res.get("pointid").toString())) continue; SensorLiveTextDto sliveDto = new SensorLiveTextDto(); sliveDto.setDevice_code(res.get("devicecode").toString()); sliveDto.setDevice_id(res.get("deviceid").toString()); sliveDto.setDevice_name(res.get("devicename").toString()); sliveDto.setSensor_id(res.get("pointid").toString()); sliveDto.setSensor_code(res.get("pointcode").toString()); sliveDto.setSensor_name(res.get("pointname").toString()); if(StringUtils.isNotEmpty(res.get("value").toString())) { sliveDto.setValue(Double.valueOf(res.get("value").toString())); }else { continue; } sliveDto.setStatus(DeviceStatus.values()[Integer.parseInt(res.get("qcdstate").toString())-1]); try { sliveDto.setValueTime(DateUtils.parseDate(res.get("recordtime").toString(), "yyyy-MM-dd HH:mm:ss")); } catch (ParseException e) { logger.error(e); } list.add(sliveDto); } } return list; } @Override public Map getDeviceCode2ValueMap() throws BusinessException{ List list = getSensorLiveTextDto(); if(list != null && list.size()>0) { Map map = new HashMap(); for(SensorLiveTextDto dto : list) { String deviceCode = dto.getSensor_code(); Double value = dto.getValue(); map.put(deviceCode, JSONObject.doubleToString(value)); } return map; } return null; } }