package com.nokia.alarm.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nokia.alarm.entity.ResultMap;
import com.nokia.alarm.util.ExportUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class AlarmSaveToGPandPGService {

    @Autowired
    private ApiCallService apiCallService;

    @Autowired
    private ResultMapService resultMapService;

    @Autowired
    private SmsSendService smsSendService;

    public void newAlarm(Date date) {
        long start = System.currentTimeMillis();
        // 获取新增告警
        List<ResultMap> results = apiCallService.getNewAlarm(date);
        log.debug("新增告警 {} 时间 获取耗时 {} 毫秒...", date, (System.currentTimeMillis() - start));
        if (results == null || results.size() == 0) {
            log.info("新增告警 {} 时间 没有数据...", date);
            return;
        }
        // 使用gpload入库到GP数据库
        // 写入文件
        String dString = ExportUtil.saveToCsv(results, date);
        // gpload
        String gploadCommand = "sh /data1/s130/alarm/gpload/alarm.sh " + dString;
        log.info("新增告警gpload:--{}", gploadCommand);
        try {
            start = System.currentTimeMillis();
            int exitCode = Runtime.getRuntime().exec(gploadCommand).waitFor();
            log.info("新增告警gpload完成...{}, 耗时 {} 毫秒...", exitCode, System.currentTimeMillis() - start);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("新增告警gpload失败: {}", e.getMessage());
            // 发送短信
            smsSendService.send("13231899751", date + "新增告警gpload失败, 请检查!");
        }
        // 批量插入到PG数据库
        try {
            start = System.currentTimeMillis();
            resultMapService.pgBatchInsert(results, date);
            log.debug("新增告警 {} 时间 入库PG完成, 耗时 {} 毫秒...", date, (System.currentTimeMillis() - start));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("新增告警 入库PG失败: {}", e.getMessage());
            // 发送短信
            smsSendService.send("13231899751", date + "新增告警入库PG失败, 请检查!");
        }
    }

    public void updateAlarm(Date date) {
        long start = System.currentTimeMillis();
        // 获取取消告警
        List<ResultMap> results = apiCallService.getCanceledAlarm(date);
        log.debug("消除告警 {} 时间 获取耗时 {} 毫秒...", date, (System.currentTimeMillis() - start));
        if (results == null || results.size() == 0) {
            log.info("消除告警 {} 时间 没有数据...", date);
            return; 
        }
        // 使用gpload入库到GP数据库
        // 写入文件
        String dString = ExportUtil.saveToCsv(results, date);
        // gpload
        String gploadCommand = "sh /data1/s130/alarm/gpload/alarm.sh " + dString;
        log.info("消除告警gpload:--{}", gploadCommand);
        try {
            start = System.currentTimeMillis();
            int exitCode = Runtime.getRuntime().exec(gploadCommand).waitFor();
            log.info("消除告警gpload完成...{}, 耗时 {} 毫秒...", exitCode, System.currentTimeMillis() - start);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("消除告警gpload失败: {}", e.getMessage());
            // 发送短信
            smsSendService.send("13231899751", date + "消除告警gpload失败, 请检查!");
        }
        // 批量插入到PG数据库
        try {
            start = System.currentTimeMillis();
            resultMapService.pgBatchInsert(results, date);
            log.debug("消除告警 {} 时间 入库PG完成, 耗时 {} 毫秒...", date, (System.currentTimeMillis() - start));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("消除告警 入库PG失败: {}", e.getMessage());
            // 发送短信
            smsSendService.send("13231899751", date + "消除告警入库PG失败, 请检查!");
        }
    }
}
