package com.huaxu.service; import cn.hutool.core.collection.CollectionUtil; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.huaxu.common.StringUtils; import com.huaxu.dto.*; import com.huaxu.entity.DayReportEntity; import com.huaxu.util.UserUtil; import org.springframework.beans.factory.annotation.Autowired; import java.util.*; import java.util.stream.Collectors; /** * 报表业务处理 * @author lihui * @date 2020-3-26 */ public abstract class AbstractReportService, T> extends ServiceImpl { @Autowired protected SceneService sceneService; @Autowired protected DeviceService deviceService; @Autowired protected DeviceParmService deviceParmService; /** * 报表数据处理 * @param queryDto * @param devices * @param reportPage */ public void reportDataHandle(ReportQueryDto queryDto, List devices, Page reportPage, boolean isPipe){ // 组装查询参数 ReportDto reportDto = getQueryReportDto(queryDto, devices, isPipe); // 设置报表标题 Map mapsTitle = new LinkedHashMap<>(); Map mapsAttributeTitle = new LinkedHashMap<>(); // 获取标题 List reportTitleDtos = getReportTitle(reportDto, mapsTitle, mapsAttributeTitle); // 固定参数项 List reportList = findReport(reportDto); int total = 0; // 单个属性值 for (ReportDto item : reportPage.getRecords()) { item.setCollectDate(formateCollectDate(queryDto, item)); item.setDeviceIds(devices); if (total == 0){ item.setReportTitle(reportTitleDtos); } // 固定参数项 calcUsage(item, reportList); //动态参数项 List reportAttributeDtos = this.findAttributeList(item); Map mapData = new LinkedHashMap<>(); for (ReportAttributeDto itemData : reportAttributeDtos) { if (!mapData.containsKey(itemData.getAttributeName())){ mapData.put(itemData.getAttributeName(), itemData); } } // 获取统计维度的值 item.setDataMapValues(getStatisticalDimensionsValue(mapsTitle, mapData, mapsAttributeTitle)); total ++; } } /** * 查询场景下的所有设备信息 * @param ids 场景ID * @return */ public List findDevices(Long[] ids){ List devices = new ArrayList<>(); DeviceDto deviceDto = new DeviceDto(); for (Long id : ids) { deviceDto.setSceneIds(sceneService.findByParentIdsLike(id)); if (deviceDto.getSceneIds().size() > 0){ devices.addAll(deviceService.selectList(deviceDto)); } } return devices; } /** * ids转换场景集合对象 * @param ids 场景ID * @return */ public List devicesToList(Long[] ids){ List devices = new ArrayList<>(); for (Long id : ids) { DeviceDto deviceDto = new DeviceDto(); deviceDto.setId(id); devices.add(deviceDto); } return devices; } /** * 1000汇算 * @param value * @return */ public double doubleRoundValue(double value){ return (double) Math.round(value * 1000) / 1000; } /** * 组装参数 * @param queryDto * @param devices * @return */ public ReportDto getQueryReportDto(ReportQueryDto queryDto, List devices, boolean isPipe){ ReportDto reportDto = new ReportDto(); reportDto.setYear(queryDto.getYear()); reportDto.setMonth(queryDto.getMonth()); reportDto.setDay(queryDto.getDay()); if (isPipe){ Integer[] parmType = null; if (queryDto.getType() == 1) { parmType = new Integer[]{13}; } else if (queryDto.getType() == 2){ parmType = new Integer[]{14}; } else if (queryDto.getType() == 3){ parmType = new Integer[]{7, 9, 11}; } reportDto.setDeviceIds(devices); reportDto.setParmType(Arrays.asList(parmType)); } else { reportDto.setDeviceIds(devices); reportDto.setParentSceneIds(queryDto.getIds()); } reportDto.setUserId(UserUtil.getCurrentUser().getId()); reportDto.setStartTime(queryDto.getStartTime()); reportDto.setEndTime(queryDto.getEndTime()); return reportDto; } //根据设备ID查询报表测点信息 protected ReportPageQueryDto getReportPageQuery(ReportQueryDto queryDto, List devices, boolean isPipe){ ReportPageQueryDto dto = new ReportPageQueryDto(); dto.setYear(queryDto.getYear()); dto.setMonth(queryDto.getMonth()); dto.setDay(queryDto.getDay()); dto.setTenantId(UserUtil.getCurrentUser().getTenantId()); dto.setDeviceIds(devices); // 管网和其他有点不一样 if (isPipe){ Integer[] parmType = null; if (queryDto.getType() == 1) { parmType = new Integer[]{13}; } else if (queryDto.getType() == 2){ parmType = new Integer[]{14}; } else if (queryDto.getType() == 3){ parmType = new Integer[]{7, 9, 11}; } dto.setTypeIds(Arrays.asList(parmType)); dto.setType(queryDto.getType()); } else { dto.setParentSceneIds(queryDto.getIds()); } dto.setStartTime(queryDto.getStartTime()); dto.setEndTime(queryDto.getEndTime()); return dto; } /** * 获取报表标题 * @param reportDto * @param mapsTitle * @param mapsAttributeTitle * @return */ public List getReportTitle(ReportDto reportDto, Map mapsTitle, Map mapsAttributeTitle){ // 获取设备参数属性 List reportAttributeDtos2 = deviceParmService.findAttributeNameList(reportDto); int numCount = 0; for (ReportAttributeDto title : reportAttributeDtos2) { if (!mapsTitle.containsKey(title.getAttributeName())){ mapsTitle.put(title.getAttributeName(), numCount++); mapsAttributeTitle.put(title.getAttributeName(),title); } } // 设置标题 Map deviceChildrenMap = new HashMap<>(); mapsAttributeTitle.forEach((k, v) -> { // 判断开关是否开启 if (v.isShowOnOff()){ setReportTitle(v, deviceChildrenMap); } }); // 转换list返回 return deviceChildrenMap.values().stream().collect(Collectors.toList()); } /** * 获取统计维度的值 * @param mapsTitle * @param mapData * @param mapsAttributeTitle * @return */ public List> getStatisticalDimensionsValue( Map mapsTitle, Map mapData, Map mapsAttributeTitle){ // 判断是否有需要展示的字段 boolean haveShowTitle = false; List> dataMapValues = new ArrayList<>(); for (String key : mapsTitle.keySet()) { ReportAttributeDto reportAttributeDto = mapsAttributeTitle.get(key); // 过滤掉没有开启开关的 if (!reportAttributeDto.isShowOnOff()) { continue; } Map map = new LinkedHashMap<>(); map.put("attributeName", mapData.get(key) == null ? key : mapData.get(key).getAttributeName()); if (reportAttributeDto.isShowMaxValue()) { map.put("maxValue" + mapsTitle.get(key), mapData.get(key) == null || mapData.get(key).getMaxValue() == null ? "-" : doubleRoundValue(mapData.get(key).getMaxValue())); haveShowTitle = true; } if (reportAttributeDto.isShowMinValue()) { map.put("minValue" + mapsTitle.get(key), mapData.get(key) == null || mapData.get(key).getMinValue() == null ? "-" : doubleRoundValue(mapData.get(key).getMinValue())); haveShowTitle = true; } if (reportAttributeDto.isShowSumValue()) { map.put("sumValue" + mapsTitle.get(key), mapData.get(key) == null || mapData.get(key).getSumValue() == null ? "-" : doubleRoundValue(mapData.get(key).getSumValue())); haveShowTitle = true; } if (reportAttributeDto.isShowLatestValue()) { map.put("latestValue" + mapsTitle.get(key), mapData.get(key) == null || mapData.get(key).getLatestValue() == null ? "-" : doubleRoundValue(mapData.get(key).getLatestValue())); haveShowTitle = true; } if (reportAttributeDto.isShowAvgValue()) { map.put("avgValue" + mapsTitle.get(key), mapData.get(key) == null || mapData.get(key).getAvgValue() == null ? "-" : doubleRoundValue(mapData.get(key).getAvgValue())); haveShowTitle = true; } if (!haveShowTitle){ continue; } haveShowTitle = false; dataMapValues.add(map); } return dataMapValues; } /** * 设置报表标题 * @param v * @param deviceChildrenMap */ private void setReportTitle(ReportAttributeDto v, Map deviceChildrenMap){ String classify = StringUtils.isEmpty(v.getClassify()) ? v.getAttributeName() : v.getClassify(); ReportTitleDto reportTitleDto = deviceChildrenMap.get(classify); if (reportTitleDto == null) { reportTitleDto = new ReportTitleDto(); reportTitleDto.setClassify(classify); } List deviceChildrenList = reportTitleDto.getDeviceChildren(); if (CollectionUtil.isEmpty(deviceChildrenList)){ deviceChildrenList = new ArrayList<>(); } ReportTitleDto.DeviceChildren deviceChildren = new ReportTitleDto.DeviceChildren(); deviceChildren.setName(v.getAttributeName()); // 检查是否有设置维度 List statisticalDimensions = new ArrayList<>(); if (v.isShowMaxValue()){ statisticalDimensions.add(getStatisticalDimension("showMaxValue", "最大值")); } if (v.isShowMinValue()){ statisticalDimensions.add(getStatisticalDimension("showMinValue", "最小值")); } if (v.isShowSumValue()){ statisticalDimensions.add(getStatisticalDimension("showSumValue", "合计值")); } if (v.isShowLatestValue()){ statisticalDimensions.add(getStatisticalDimension("showLatestValue", "最新值")); } if (v.isShowAvgValue()){ statisticalDimensions.add(getStatisticalDimension("showAvgValue", "平均值")); } // 有设置维度,添加相关信息 if (v.isShowMaxValue() || v.isShowMinValue() || v.isShowSumValue() || v.isShowLatestValue() || v.isShowAvgValue()){ deviceChildren.setDimensionList(statisticalDimensions); deviceChildrenList.add(deviceChildren); reportTitleDto.setDeviceChildren(deviceChildrenList); deviceChildrenMap.put(classify, reportTitleDto); } } /** * 获取统计维度 * @param key * @param name * @return */ private ReportTitleDto.StatisticalDimension getStatisticalDimension(String key, String name){ ReportTitleDto.StatisticalDimension statisticalDimension = new ReportTitleDto.StatisticalDimension(); statisticalDimension.setDimensionKey(key); statisticalDimension.setDimensionName(name); return statisticalDimension; } /** * @Author lihui * @Description 格式化日期 * @Date 16:38 2021/4/15 * @Param [queryDto, item] * @return java.lang.String **/ private String formateCollectDate(ReportQueryDto queryDto, ReportDto item){ // 0日 1月 2年 if (queryDto.getReportType() == 0) { return item.getYear() + "-" + String.format("%02d", item.getMonth()) + "-" + String.format("%02d", item.getDay()) + " " + String.format("%02d", item.getHour()) + ":00:00"; } if (queryDto.getReportType() == 1 || queryDto.getReportType() == 3) { return item.getYear() + "-" + String.format("%02d", item.getMonth()) + "-" + String.format("%02d", item.getDay()); } if (queryDto.getReportType() == 2) { return item.getYear() + "-" + String.format("%02d", item.getMonth()); } return null; } /** * 查询报表 * @param reportDto * @return */ abstract List findReport(ReportDto reportDto); /** * 计算值 * @param item * @param reportDtos */ abstract void calcUsage(ReportDto item, List reportDtos); /** * 查询属性 * @param item * @return */ abstract List findAttributeList(ReportDto item); }