package com.ruoyi.web.job; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.convert.Convert; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.StrUtil; import com.ruoyi.common.constant.ElasticConstants; import com.ruoyi.framework.config.ElasticSearchClient; import com.ruoyi.system.domain.IllegalInfo; import com.ruoyi.system.service.IAisInfoService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; /** * @Description: TODO * @Author: huangcheng * @Date: 2021/11/1 * @Version V1.0 */ @Component("getDynamicShip") @Slf4j public class GetDynamicShip { @Autowired private IAisInfoService aisInfoService; @Autowired private ElasticSearchClient client; private static final int SIZE = 50; public void get() { log.info("获取船舶实时位置任务开始!"); // 拉取一天内需要获取经纬度的船舶 Set shipMmsi = new HashSet<>(); Map equalsCondition = new HashMap<>(); equalsCondition.put("illegalStatus", 2); List orderBy = new ArrayList<>(); orderBy.add("-createTime"); Map rangeCondition = new HashMap<>(); String startTime = DateUtil.formatDateTime(DateUtil.offsetDay(new Date(), -1)); rangeCondition.put("createTime", StrUtil.concat(true, "[", startTime, ",", DateUtil.now(), "]")); Map xianyiMap = client.searchDocument(equalsCondition, rangeCondition, orderBy, 1, 500, ElasticConstants.AIS_ILLEGAL_SHIP, ElasticConstants.SO2_ALERT, ElasticConstants.HEIYAN_SHIP_RECOGNITION); List xianyiList = Convert.toList(IllegalInfo.class, xianyiMap.get("pageList")); for (IllegalInfo illegalInfo : xianyiList) { shipMmsi.add(StrUtil.isBlank(illegalInfo.getMmsi()) ? illegalInfo.getAisMmsi() : illegalInfo.getMmsi()); } equalsCondition.put("illegalStatus", 3); Map weiguiMap = client.searchDocument(equalsCondition, rangeCondition, orderBy, 1, 100, ElasticConstants.AIS_ILLEGAL_SHIP, ElasticConstants.SO2_ALERT, ElasticConstants.HEIYAN_SHIP_RECOGNITION); List weiguiList = Convert.toList(IllegalInfo.class, xianyiMap.get("pageList")); for (IllegalInfo illegalInfo : weiguiList) { shipMmsi.add(StrUtil.isBlank(illegalInfo.getMmsi()) ? illegalInfo.getAisMmsi() : illegalInfo.getMmsi()); } equalsCondition.put("illegalStatus", 1); rangeCondition.put("createTime", StrUtil.concat(true, "[", DateUtil.formatDateTime(DateUtil.offsetHour(new Date(), -2)), ",", DateUtil.now(), "]")); Map zhengchangMap = client.searchDocument(equalsCondition, rangeCondition, orderBy, 1, 50, ElasticConstants.AIS_ILLEGAL_SHIP, ElasticConstants.SO2_ALERT, ElasticConstants.HEIYAN_SHIP_RECOGNITION); List zhengchangList = Convert.toList(IllegalInfo.class, xianyiMap.get("pageList")); for (IllegalInfo illegalInfo : zhengchangList) { shipMmsi.add(StrUtil.isBlank(illegalInfo.getMmsi()) ? illegalInfo.getAisMmsi() : illegalInfo.getMmsi()); } int startIndex = 0; int endIndex; int size = shipMmsi.size(); while (startIndex < size) { //尾部的位置 endIndex = startIndex + SIZE; //尾部位置不能超出范围,否则就取集合的长度 endIndex = Math.min(endIndex, size); //截取 List subList = CollUtil.sub(shipMmsi, startIndex, endIndex); aisInfoService.getDynamicShipInfo(String.join(",", subList)); //计算下次截取的开始位置 startIndex = endIndex; } log.info("获取船舶实时位置任务结束!"); } }