package com.ruoyi.web.service.Impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.constant.ElasticConstants;
import com.ruoyi.framework.config.ElasticSearchClient;
import com.ruoyi.system.domain.SyncPost;
import com.ruoyi.system.mapper.GasCommonMapper;
import com.ruoyi.web.service.IGasCommonService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: TODO
 * @Author: huangcheng
 * @Date: 2021/8/30
 * @Version V1.0
 */
@Service
@Slf4j
public class GasCommonServiceImpl implements IGasCommonService {

    @Autowired
    private GasCommonMapper gasCommonMapper;
    @Autowired
    private ElasticSearchClient client;

    public final static Map<String, String> orgMap = new HashMap<String, String>() {{
        put("南京三桥", "南京海事局");
        put("南京四桥", "南京海事局");
        put("润扬大桥", "扬州海事局");
        put("泰州大桥", "泰州海事局");
        put("江阴大桥", "江阴海事局");
        put("苏通大桥", "常熟海事局");
    }};

    @SneakyThrows
    @Override
    @Async
    public void sync(SyncPost syncPost) {
        String schema = syncPost.getSchema();
        String table = syncPost.getTable();
        if (StrUtil.isNotBlank(syncPost.getStartTime())) {
            syncPost.setStartTime(syncPost.getStartTime() + " 00:00:00");
        }
        if (StrUtil.isNotBlank(syncPost.getEndTime())) {
            syncPost.setEndTime(syncPost.getEndTime() + " 23:59:59");
        }
        if (StrUtil.equalsIgnoreCase(schema, "ais_database") && (StrUtil.equalsIgnoreCase(table, "sem_instrument_test") || StrUtil.equalsIgnoreCase(table, "sem_instrument"))) {
            // 嗅探站点信息
            List<Map<String, Object>> map = gasCommonMapper.querySemInstrument(table);
            for (Map<String, Object> stringObjectMap : map) {
                saveOrUpdate(ElasticConstants.AIS_SEM_INSTRUMENT, Convert.toStr(stringObjectMap.get("id")), stringObjectMap);
            }
        } else if (StrUtil.equalsIgnoreCase(schema, "ais_database") && StrUtil.equalsIgnoreCase(table, "illegal_ship")) {
            // 嗅探违规船舶信息
            List<Map<String, Object>> map = gasCommonMapper.queryIllegalShip(syncPost);
            for (Map<String, Object> stringObjectMap : map) {
                stringObjectMap.put("mmsi", stringObjectMap.get("shipMmsi"));
                stringObjectMap.put("createTime", stringObjectMap.get("peakTime"));
                stringObjectMap.put("illegalStatus", 1);
                stringObjectMap.put("illegalType", "xiutan");
                stringObjectMap.put("uploadFlag", 0);
                stringObjectMap.put("orgName", orgMap.get(Convert.toStr(stringObjectMap.get("monitorPointName"))));
                saveOrUpdate(ElasticConstants.AIS_ILLEGAL_SHIP, Convert.toStr(stringObjectMap.get("id")), stringObjectMap);
            }
        } else if (StrUtil.equalsIgnoreCase(schema, "heiyan") && StrUtil.equalsIgnoreCase(table, "ship_recognition")) {
            // 黑烟嫌疑船舶
            List<Map<String, Object>> map = gasCommonMapper.queryShipRecognition(syncPost);
            for (Map<String, Object> stringObjectMap : map) {
                stringObjectMap.put("illegalStatus", 1);
                stringObjectMap.put("uploadFlag", 0);
                saveOrUpdate(ElasticConstants.HEIYAN_SHIP_RECOGNITION, Convert.toStr(stringObjectMap.get("id")), stringObjectMap);
            }
        } else if (StrUtil.equalsIgnoreCase(schema, "so2") && StrUtil.equalsIgnoreCase(table, "alert")) {
            // 光谱嫌疑船舶
            List<Map<String, Object>> map = gasCommonMapper.queryAlert(syncPost);
            for (Map<String, Object> stringObjectMap : map) {
                stringObjectMap.put("illegalStatus", 1);
                stringObjectMap.put("illegalType", "guangpu");
                stringObjectMap.put("uploadFlag", 0);
                stringObjectMap.put("orgName", orgMap.get(Convert.toStr(stringObjectMap.get("monitorPointName"))));
                saveOrUpdate(ElasticConstants.SO2_ALERT, Convert.toStr(stringObjectMap.get("id")), stringObjectMap);
            }
        } else if (StrUtil.equalsIgnoreCase(schema, "so2") && StrUtil.equalsIgnoreCase(table, "monitor_point")) {
            // 光谱监测点
            List<Map<String, Object>> map = gasCommonMapper.queryMonitorPoint(syncPost);
            for (Map<String, Object> stringObjectMap : map) {
                saveOrUpdate(ElasticConstants.SO2_MONITOR_POINT, Convert.toStr(stringObjectMap.get("id")), stringObjectMap);
            }
        }/* else if (StrUtil.equalsIgnoreCase(schema, "so2") && StrUtil.equalsIgnoreCase(table, "ship_static_info")) {
            // 光谱船舶信息
            List<Map<String, Object>> map = gasCommonMapper.queryShipStaticInfo(syncPost);
            for (Map<String, Object> stringObjectMap : map) {
                saveOrUpdate(ElasticConstants.SO2_SHIP_STATIC_INFO, Convert.toStr(stringObjectMap.get("id")), stringObjectMap);
            }
        }*/
    }

    @SneakyThrows
    private void saveOrUpdate(String indexName, String id, Map<String, Object> map) {
        boolean b = client.existsById(indexName, id);
        if (b) {
            // 更新操作
            client.updateDocument(indexName, id, map);
        } else {
            // 新增操作
            client.createDocument(indexName, id, map);
        }
    }

}
