Newer
Older
newfiber-termite / newfiber-termites / newfiber-termites-dataup / src / main / java / com / newfiber / termite / schedule / HeatMapSchedule.java
@xiongkai xiongkai 9 days ago 8 KB 统计bug修复
package com.newfiber.termite.schedule;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.newfiber.common.core.constant.HttpStatus;
import com.newfiber.common.core.domain.R;
import com.newfiber.common.core.utils.file.FileUtils;
import com.newfiber.common.webgis.utils.GeometryUtils;
import com.newfiber.common.webgis.utils.RainUtils;
import com.newfiber.system.api.RemoteFileService;
import com.newfiber.system.api.domain.SysFile;
import com.newfiber.termite.domain.DeviceInfo;
import com.newfiber.termite.domain.ProjectInfo;
import com.newfiber.termite.domain.request.deviceInfo.DeviceInfoQueryRequest;
import com.newfiber.termite.domain.request.statistic.SiteWarnTotalCountStatisticRequest;
import com.newfiber.termite.domain.response.statistic.SiteTotalWarnCount;
import com.newfiber.termite.service.IDeviceInfoService;
import com.newfiber.termite.service.IStatisticsService;
import com.newfiber.termite.service.impl.ProjectInfoServiceImpl;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.locationtech.jts.geom.Geometry;
import org.springframework.stereotype.Component;

@Component
@SuppressWarnings("unchecked")
public class HeatMapSchedule {

    @Resource
    private ProjectInfoServiceImpl projectInfoService;

    @Resource
    private IStatisticsService statisticsService;

    @Resource
    private IDeviceInfoService deviceInfoService;

    @Resource
    private RemoteFileService remoteFileService;

//    @Scheduled(cron = "0 0 0 * * ? ")
    public void heatMapGenerator(){
//        ProjectInfoQueryRequest projectInfoQueryRequest = new ProjectInfoQueryRequest();
//        projectInfoQueryRequest.setProjectCode("20241115");
//        List<ProjectInfo> projectInfoList = projectInfoService.selectList(projectInfoQueryRequest);

        List<ProjectInfo> projectInfoList = projectInfoService.selectWithGeometry();
        String filePath = FileUtil.getTmpDirPath() + RandomUtil.randomString(6);

        for(ProjectInfo projectInfo : projectInfoList){
            try{
                HashMap<String, String> geometryMap = JSONObject.parseObject(projectInfo.getGeometrys(), HashMap.class);
                List<SiteTotalWarnCount> siteTotalWarnCountList = statisticsService.siteWarnTotalCountStatistic(new SiteWarnTotalCountStatisticRequest(projectInfo.getProjectCode()));
                List<DeviceInfo> deviceInfoList = deviceInfoService.selectList(projectInfo.getProjectCode());
                List<String> filePathList = new ArrayList<>();
                HashMap<String, FieldExplain> fieldExplainHashMap = new HashMap<>();

                for(Entry<String, String> entry : geometryMap.entrySet()){
                    List<DeviceInfo> subDeviceInfoList = deviceInfoList.stream().filter(t -> entry.getKey().equals(t.getGeometryNumber())).collect(Collectors.toList());
                    List<String> subSnList = subDeviceInfoList.stream().map(DeviceInfo::getSn).collect(Collectors.toList());

                    List<SiteTotalWarnCount> subSiteTotalWarnCountList = siteTotalWarnCountList.stream().filter(t -> subSnList.contains(t.getSn())).collect(Collectors.toList());
                    double[][] rainData = new double[subDeviceInfoList.size()][3];

                    for(int i = 0; i < subDeviceInfoList.size(); i++){
                        DeviceInfo deviceInfo = subDeviceInfoList.get(i);

                        String lon = deviceInfo.getLonandlat().split(",")[0];
                        String lat = deviceInfo.getLonandlat().split(",")[1];

                        rainData[i][0] = Double.parseDouble(lon);
                        rainData[i][1] = Double.parseDouble(lat);

                        Optional<SiteTotalWarnCount> optionalSiteTotalWarnCount = subSiteTotalWarnCountList.stream().filter(t -> t.getSn().equals(deviceInfo.getSn())).findFirst();
                        rainData[i][2] = optionalSiteTotalWarnCount.map(siteTotalWarnCount -> siteTotalWarnCount.getCount().doubleValue()).orElse(0.0);
                    }

                    String fileName = "";
                    try {
                        fileName = RainUtils.weatherWarning(rainData, entry.getValue(), filePath,1);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    if (StringUtils.isBlank(fileName)){
                        continue;
                    }

                    File imageFile = new File(fileName);
                    R<SysFile> sysFileR = remoteFileService.upload(FileUtils.getMultipartFile(imageFile));
                    if(HttpStatus.SUCCESS == sysFileR.getCode()){
                        filePathList.add(sysFileR.getData().getUrl());
                    }

                    Geometry geometry = GeometryUtils.wkt2Geometry(entry.getValue());
                    double[] geometryLimit = GeometryUtils.getBboxCoordinatesByGeometry(geometry);
                    String heatMapCoordinate = geometryLimit[0] + "," + geometryLimit[1] + "," +geometryLimit[2] + "," +geometryLimit[3];
                    BufferedImage bufferedImage = ImageIO.read(imageFile);
                    String heatMapResolution = bufferedImage.getWidth() + "," + bufferedImage.getHeight();
                    fieldExplainHashMap.put(entry.getKey(), new FieldExplain(heatMapCoordinate, heatMapResolution));
                }

                if(CollectionUtils.isNotEmpty(filePathList)){
                    ProjectInfo projectInfoCondition = new ProjectInfo();
                    projectInfoCondition.setId(projectInfo.getId());
                    projectInfoCondition.setHeatmap(String.join("\\|", filePathList));
                    projectInfoCondition.setFieldExplain(JSONObject.toJSONString(fieldExplainHashMap));
                    projectInfoService.updateById(projectInfoCondition);
                    System.out.println(projectInfo.getProjectName() + " : " + projectInfoCondition.getHeatmap());
                    System.out.println(projectInfo.getProjectName() + " : " + JSONObject.toJSONString(fieldExplainHashMap));
                }
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }

    public void geometryGenerator(){
        List<ProjectInfo> projectInfoList = projectInfoService.selectWithCenterPoint();
        List<String> projectCodeList = projectInfoList.stream().map(ProjectInfo::getProjectCode).collect(Collectors.toList());

        DeviceInfoQueryRequest deviceInfoQueryRequest = new DeviceInfoQueryRequest();
        deviceInfoQueryRequest.setProjectCodeList(projectCodeList);
        List<DeviceInfo> deviceInfoList = deviceInfoService.selectList(deviceInfoQueryRequest);
        deviceInfoList = deviceInfoList.stream().filter(t -> null != t.getProjectCode()).collect(Collectors.toList());

        for(ProjectInfo projectInfo : projectInfoList){
            List<DeviceInfo> projectDeviceInfoList = deviceInfoList.stream().filter(t -> t.getProjectCode().equals(projectInfo.getProjectCode())).collect(Collectors.toList());

            Map<String, List<DeviceInfo>> deviceGeometryMap = projectDeviceInfoList.stream().collect(Collectors.groupingBy(DeviceInfo::getGeometryNumber));
            HashMap<String, String> geometryMap = new HashMap<>();

            for(Entry<String, List<DeviceInfo>> entry : deviceGeometryMap.entrySet()){
                try{
                    List<String> pointList = entry.getValue().stream().map(DeviceInfo::getLonandlat).collect(Collectors.toList());
                    String geometry = GeometryUtils.point2Polygon(pointList);
                    geometryMap.put(entry.getKey(), geometry);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            if(!geometryMap.isEmpty()){
                System.out.printf("projectName : %s ; wtk : %s%n", projectInfo.getProjectName(), JSONObject.toJSONString(geometryMap));
                ProjectInfo projectInfoCondition = new ProjectInfo();
                projectInfoCondition.setId(projectInfo.getId());
                projectInfoCondition.setGeometrys(JSONObject.toJSONString(geometryMap));
                projectInfoService.updateById(projectInfoCondition);
            }

        }

    }

    @Data
    @AllArgsConstructor
    static class FieldExplain{
        String heatMapCoordinate;
        String heatMapResolution;
    }
}