|
@@ -0,0 +1,294 @@
|
|
|
|
+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 org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+
|
|
|
|
+import java.util.*;
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 报表业务处理
|
|
|
|
+ * @author lihui
|
|
|
|
+ * @date 2020-3-26
|
|
|
|
+ */
|
|
|
|
+public abstract class AbstractReportService<M extends BaseMapper<T>, 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<DeviceDto> devices, Page<ReportDto> reportPage, boolean isPipe){
|
|
|
|
+ // 组装查询参数
|
|
|
|
+ ReportDto reportDto = getQueryReportDto(queryDto, devices, isPipe);
|
|
|
|
+ // 设置报表标题
|
|
|
|
+ Map<String, Integer> mapsTitle = new LinkedHashMap<>();
|
|
|
|
+ Map<String, ReportAttributeDto> mapsAttributeTitle = new LinkedHashMap<>();
|
|
|
|
+ // 获取标题
|
|
|
|
+ List<ReportTitleDto> reportTitleDtos = getReportTitle(reportDto, mapsTitle, mapsAttributeTitle);
|
|
|
|
+ // 固定参数项
|
|
|
|
+ List<ReportDto> reportList = findReport(reportDto);
|
|
|
|
+ // 单个属性值
|
|
|
|
+ for (ReportDto item : reportPage.getRecords()) {
|
|
|
|
+ item.setCollectDate(item.getYear() + "-" + String.format("%02d", item.getMonth()) + "-" + String.format("%02d", item.getDay()) + " " + String.format("%02d", item.getHour()) + ":00");
|
|
|
|
+ item.setDeviceIds(devices);
|
|
|
|
+ item.setReportTitle(reportTitleDtos);
|
|
|
|
+ // 固定参数项
|
|
|
|
+ calcUsage(item, reportList);
|
|
|
|
+ //动态参数项
|
|
|
|
+ List<ReportAttributeDto> reportAttributeDtos = this.findAttributeList(item);
|
|
|
|
+ Map<String, ReportAttributeDto> mapData = new LinkedHashMap<>();
|
|
|
|
+ for (ReportAttributeDto itemData : reportAttributeDtos) {
|
|
|
|
+ if (!mapData.containsKey(itemData.getAttributeName())){
|
|
|
|
+ mapData.put(itemData.getAttributeName(), itemData);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // 获取统计维度的值
|
|
|
|
+ item.setDataMapValues(getStatisticalDimensionsValue(mapsTitle, mapData, mapsAttributeTitle));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 查询场景下的所有设备信息
|
|
|
|
+ * @param ids 场景ID
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public List<DeviceDto> findDevices(Long[] ids){
|
|
|
|
+ List<DeviceDto> 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<DeviceDto> devicesToList(Long[] ids){
|
|
|
|
+ List<DeviceDto> 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<DeviceDto> devices, boolean isPipe){
|
|
|
|
+ ReportDto reportDto = new ReportDto();
|
|
|
|
+ reportDto.setYear(queryDto.getYear());
|
|
|
|
+ reportDto.setMonth(queryDto.getMonth());
|
|
|
|
+ reportDto.setDay(queryDto.getDay());
|
|
|
|
+ if (isPipe){
|
|
|
|
+ reportDto.setDeviceIds(devices);
|
|
|
|
+ reportDto.setParmType(queryDto.getType() == 1 ? 13 : 14);
|
|
|
|
+ } else {
|
|
|
|
+ reportDto.setDeviceIds(devices);
|
|
|
|
+ reportDto.setParentSceneIds(queryDto.getIds());
|
|
|
|
+ }
|
|
|
|
+ return reportDto;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取报表标题
|
|
|
|
+ * @param reportDto
|
|
|
|
+ * @param mapsTitle
|
|
|
|
+ * @param mapsAttributeTitle
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public List<ReportTitleDto> getReportTitle(ReportDto reportDto, Map<String, Integer> mapsTitle,
|
|
|
|
+ Map<String, ReportAttributeDto> mapsAttributeTitle){
|
|
|
|
+ // 获取设备参数属性
|
|
|
|
+ List<ReportAttributeDto> 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<String, ReportTitleDto> 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<Map<String, Object>> getStatisticalDimensionsValue( Map<String, Integer> mapsTitle, Map<String, ReportAttributeDto> mapData, Map<String, ReportAttributeDto> mapsAttributeTitle){
|
|
|
|
+ // 判断是否有需要展示的字段
|
|
|
|
+ boolean haveShowTitle = false;
|
|
|
|
+ List<Map<String, Object>> dataMapValues = new ArrayList<>();
|
|
|
|
+ for (String key : mapsTitle.keySet()) {
|
|
|
|
+ ReportAttributeDto reportAttributeDto = mapsAttributeTitle.get(key);
|
|
|
|
+ // 过滤掉没有开启开关的
|
|
|
|
+ if (!reportAttributeDto.isShowOnOff()) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ Map<String, Object> 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<String, ReportTitleDto> 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<ReportTitleDto.DeviceChildren> deviceChildrenList = reportTitleDto.getDeviceChildren();
|
|
|
|
+ if (CollectionUtil.isEmpty(deviceChildrenList)){
|
|
|
|
+ deviceChildrenList = new ArrayList<>();
|
|
|
|
+ }
|
|
|
|
+ ReportTitleDto.DeviceChildren deviceChildren = new ReportTitleDto.DeviceChildren();
|
|
|
|
+ deviceChildren.setName(v.getAttributeName());
|
|
|
|
+
|
|
|
|
+ // 检查是否有设置维度
|
|
|
|
+ List<ReportTitleDto.StatisticalDimension> 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;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 查询报表
|
|
|
|
+ * @param reportDto
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ abstract List<ReportDto> findReport(ReportDto reportDto);
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 计算值
|
|
|
|
+ * @param item
|
|
|
|
+ * @param reportDtos
|
|
|
|
+ */
|
|
|
|
+ abstract void calcUsage(ReportDto item, List<ReportDto> reportDtos);
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 查询属性
|
|
|
|
+ * @param item
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ abstract List<ReportAttributeDto> findAttributeList(ReportDto item);
|
|
|
|
+
|
|
|
|
+}
|