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 com.ruoyi.system.service.IAisShipService;
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 IAisShipService aisShipService;
    @Autowired
    private ElasticSearchClient client;
    private static final int SIZE = 50;

    public void get() {
        log.info("获取船舶实时位置任务开始！");
        // 拉取一天内需要获取经纬度的船舶
        Set<String> shipMmsi = new HashSet<>();
        Map<String, Object> equalsCondition = new HashMap<>();
        equalsCondition.put("illegalStatus", 2);
        List<String> orderBy = new ArrayList<>();
        orderBy.add("-createTime");
        Map<String, Object> rangeCondition = new HashMap<>();
        String startTime = DateUtil.formatDateTime(DateUtil.offsetDay(new Date(), -1));
        rangeCondition.put("createTime", StrUtil.concat(true, "[", startTime, ",", DateUtil.now(), "]"));
        Map<String, Object> xianyiMap = client.searchDocument(equalsCondition,
                rangeCondition,
                orderBy,
                1,
                500,
                ElasticConstants.AIS_ILLEGAL_SHIP,
                ElasticConstants.SO2_ALERT,
                ElasticConstants.HEIYAN_SHIP_RECOGNITION);
        List<IllegalInfo> xianyiList = Convert.toList(IllegalInfo.class, xianyiMap.get("pageList"));
        for (IllegalInfo illegalInfo : xianyiList) {
            if (StrUtil.isNotBlank(illegalInfo.getMmsi()) && !"0".equals(illegalInfo.getMmsi())) {
                shipMmsi.add(illegalInfo.getMmsi());
            }
        }
        equalsCondition.put("illegalStatus", 3);
        Map<String, Object> weiguiMap = client.searchDocument(equalsCondition,
                rangeCondition,
                orderBy,
                1,
                100,
                ElasticConstants.AIS_ILLEGAL_SHIP,
                ElasticConstants.SO2_ALERT,
                ElasticConstants.HEIYAN_SHIP_RECOGNITION);
        List<IllegalInfo> weiguiList = Convert.toList(IllegalInfo.class, weiguiMap.get("pageList"));
        for (IllegalInfo illegalInfo : weiguiList) {
            if (StrUtil.isNotBlank(illegalInfo.getMmsi()) && !"0".equals(illegalInfo.getMmsi())) {
                shipMmsi.add(illegalInfo.getMmsi());
            }
        }
        equalsCondition.put("illegalStatus", 1);
        rangeCondition.put("createTime", StrUtil.concat(true, "[", DateUtil.formatDateTime(DateUtil.offsetHour(new Date(), -2)), ",", DateUtil.now(), "]"));
        Map<String, Object> zhengchangMap = client.searchDocument(equalsCondition,
                rangeCondition,
                orderBy,
                1,
                50,
                ElasticConstants.AIS_ILLEGAL_SHIP,
                ElasticConstants.SO2_ALERT,
                ElasticConstants.HEIYAN_SHIP_RECOGNITION);
        List<IllegalInfo> zhengchangList = Convert.toList(IllegalInfo.class, zhengchangMap.get("pageList"));
        for (IllegalInfo illegalInfo : zhengchangList) {
            if (StrUtil.isNotBlank(illegalInfo.getMmsi()) && !"0".equals(illegalInfo.getMmsi())) {
                shipMmsi.add(illegalInfo.getMmsi());
            }
        }
        int startIndex = 0;
        int endIndex;
        int size = shipMmsi.size();
        while (startIndex < size) {
            //尾部的位置
            endIndex = startIndex + SIZE;

            //尾部位置不能超出范围，否则就取集合的长度
            endIndex = Math.min(endIndex, size);

            //截取
            List<String> subList = CollUtil.sub(shipMmsi, startIndex, endIndex);
            aisInfoService.getDynamicShipInfo(String.join(",", subList));
            //计算下次截取的开始位置
            startIndex = endIndex;
        }
        log.info("获取船舶实时位置任务结束！");
    }

    /**
     * 删除历史某些数据
     *
     * @param day 删除多少天前的数据
     */
    public void delete(Integer day) {
        log.info("删除实时经纬度数据任务开始！");
        aisShipService.deleteByDay(day);
        log.info("删除实时经纬度数据任务开始！");
    }

    /**
     * 删除ES中存在的历史数据--正常监测数据
     *
     * @param day
     */
    public void deleteEsExpire(Integer day) {
        log.info("删除ES中数据任务开始！");
        Map<String, Object> equalsCondition = new HashMap<>();
        equalsCondition.put("illegalStatus", "1");
        Map<String, Object> rangeCondition = new HashMap<>();
        rangeCondition.put("createTime", StrUtil.concat(true, "[", "", ",", DateUtil.formatDateTime(DateUtil.offsetDay(new Date(), -day)), "]"));
        Map<String, Object> stringObjectMap = client.searchDocument(equalsCondition,
                rangeCondition,
                null,
                1,
                10000,
                ElasticConstants.AIS_ILLEGAL_SHIP,
                ElasticConstants.SO2_ALERT);
        List<IllegalInfo> pageList = Convert.toList(IllegalInfo.class, stringObjectMap.get("pageList"));
        for (IllegalInfo illegalInfo : pageList) {
            if ("xiutan".equals(illegalInfo.getIllegalType())) {
                client.deleteDocument(ElasticConstants.AIS_ILLEGAL_SHIP, illegalInfo.getId());
            } else if ("guangpu".equals(illegalInfo.getIllegalType())) {
                client.deleteDocument(ElasticConstants.SO2_ALERT, illegalInfo.getId());
            }
        }
        log.info("删除ES中数据任务结束！");
    }
}
