package com.ruoyi.web.job;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.constant.ElasticConstants;
import com.ruoyi.framework.config.ElasticSearchClient;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Description: TODO
 * @Author: huangcheng
 * @Date: 2021/11/1
 * @Version V1.0
 */
@Component("getDynamicShip")
@Slf4j
public class GetDynamicShip {

    @Autowired
    private ElasticSearchClient client;

    /**
     * 删除ES中存在的历史数据--正常监测数据
     *
     * @param day
     */
    public void deleteEsExpire(Integer day) {
        log.info("删除ES中数据任务开始！");
        QueryBuilder queryBuilder = QueryBuilders.boolQuery()
                .must(QueryBuilders.termQuery("illegalStatus", "1"))
                .must(QueryBuilders.rangeQuery("createTime").from("2021-01-01 00:00:00").to(DateUtil.formatDateTime(DateUtil.offsetDay(new Date(), -day))));
        client.deleteByCondition(queryBuilder,
                ElasticConstants.AIS_ILLEGAL_SHIP,
                ElasticConstants.SO2_ALERT,
                ElasticConstants.HEIYAN_SHIP_RECOGNITION);
//        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中数据任务结束！");
    }
}
