package com.nokia.alarm.service;

import com.alibaba.fastjson.JSON;
import com.hl.opnc.AbilityResponse;
import com.hl.opnc.ResponseBody;
import com.hl.opnc.invoke.AbilityInvokeManager;
import com.hl.opnc.invoke.InvokeManager;
import com.nokia.alarm.entity.MessageCfg;
import com.nokia.alarm.entity.ResultInfo;
import com.nokia.alarm.entity.ResultMap;
import com.nokia.alarm.util.ExportUtil;
import com.nokia.alarm.util.SDKUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class AlarmSaveToFileService {
    @Value("${httpurl:http://192.168.70.125:12090/esb/HEB_AlarmCustomQuery/0?authcode=SEVCX1VDSUFQI0BhdXRoQCNTWnFsVk9QeA}")
    private String methodUrl;
    @Value("${appId:0f9bf497549343e68ca36b4caffbc8c4}")
    private String appId;
    @Value("${secret:087b1ceb698140cd8c65543f5ace0490}")
    private String secret;

    public void getHttpSave(Date date) {
        log.info("查询新增告警开始=====================================================");
        InvokeManager manager = new AbilityInvokeManager(appId, secret);
        MessageCfg cfg = SDKUtil.getBody(date);
        AbilityResponse response = manager.post(methodUrl, null, JSON.toJSONString(cfg));
        try {
            String responseStr = response.getRespStr();
            ResponseBody resObj;
            try {
                resObj = JSON.parseObject(responseStr, ResponseBody.class);
            } catch (Exception e) {
                log.error("返回消息json解析错误: {}", responseStr);
                e.printStackTrace();
                return;
            }
            String body = (String) resObj.getUNI_NET_BODY();
//            log.info(body);
            // body = body.replace("null", "\"\"");
            ResultInfo info;
            try {
                info = JSON.parseObject(body, ResultInfo.class);
            } catch (Exception e) {
                log.error("返回消息json解析错误: {}", responseStr);
                e.printStackTrace();
                return;
            }
            int rows = info.getTotal();
            log.info("新增告警返回数据{}条", rows);
            List<ResultMap> results = info.getResult();
            // 写入文件
            String dString = ExportUtil.saveToCsv(results);
            // gpload
            String gploadCommand = "sh /data1/s130/alarm/gpload/alarm.sh " + dString;
            log.info("新增告警goload:----{}", gploadCommand);
            try {
                int exitCode = Runtime.getRuntime().exec(gploadCommand).waitFor();
                log.info("新增告警gpload完成。。。。。{}", exitCode);
            } catch (InterruptedException e) {
                e.printStackTrace();
                log.error("新增告警gpload失败: {}", e.getMessage());
            }
            log.info("查询新增告警完成=====================================================");
        } catch (Exception e) {
            log.info("查询新增告警出错=====================================================");
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    public void getHttpUpdate(Date date) {
        log.info("查询消除告警开始=====================================================");
        InvokeManager manager = new AbilityInvokeManager(appId, secret);
        MessageCfg cfg = SDKUtil.gethistoryBody(date);
        AbilityResponse response = manager.post(methodUrl, null, JSON.toJSONString(cfg));
        try {
            String responseStr = response.getRespStr();
            ResponseBody resObj;
            try {
                resObj = JSON.parseObject(responseStr, ResponseBody.class);
            } catch (Exception e) {
                log.error("返回消息json解析错误: {}", responseStr);
                e.printStackTrace();
                return;
            }
            String body = (String) resObj.getUNI_NET_BODY();
            // body = body.replace("null", "\"\"");
            ResultInfo info;
            try {
                info = JSON.parseObject(body, ResultInfo.class);
            } catch (Exception e) {
                log.error("返回消息json解析错误: {}", responseStr);
                e.printStackTrace();
                return;
            }
            int rows = info.getTotal();
            log.info("消除告警返回数据{}条", rows);
            List<ResultMap> results = info.getResult();
            // 写入文件
            String dString = ExportUtil.saveToCsv(results);
            // gpload
            String gploadCommand = "sh /data1/s130/alarm/gpload/alarm.sh " + dString;
            log.info("消除告警goload:----{}", gploadCommand);
            try {
                int exitCode = Runtime.getRuntime().exec(gploadCommand).waitFor();
                log.info("消除告警gpload完成。。。。。{}", exitCode);
            } catch (InterruptedException e) {
                e.printStackTrace();
                log.error("消除告警gpload失败: {}", e.getMessage());
            }
            log.info("查询消除告警完成=====================================================");
        } catch (Exception e) {
            log.info("查询消除告警出错=====================================================");
            e.printStackTrace();
        }
    }
}
