Newer
Older
newfiber-termite / newfiber-termites / newfiber-termites-dataup / src / main / java / com / newfiber / termite / schedule / HeatMapSchedule.java
@xiongkai xiongkai 16 days ago 6 KB 代码修改
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.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.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.locationtech.jts.geom.Geometry;
import org.springframework.scheduling.annotation.Scheduled;
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 generator(){
        List<ProjectInfo> projectInfoList = projectInfoService.selectWithGeometry();
        String filePath = FileUtil.getTmpDirPath() + RandomUtil.randomString(6);

        for(ProjectInfo projectInfo : projectInfoList){
            if(!"20241115".equals(projectInfo.getProjectCode())){
                continue;
            }
            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);

                        if("20241115".equals(projectInfo.getProjectCode())){
//                            System.out.print(deviceInfo.getLocation() + "," + rainData[i][2]) ;
                            System.out.print(rainData[i][0] + "," + rainData[i][1] + "," + rainData[i][2]) ;
                            System.out.println();
                        }
                    }

                    String fileName = RainUtils.weatherWarning(rainData, entry.getValue(), filePath,1);

                    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));
//                    projectInfoService.updateById(projectInfoCondition);
                    System.out.println(projectInfo.getProjectName() + " : " + projectInfoCondition.getHeatmap());
                    System.out.println(projectInfo.getProjectName() + " : " + JSONObject.toJSONString(fieldExplainHashMap));
                }
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }

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