package com.lightinit.hsdataplatform.tasks.logger;

import com.lightinit.hsdataplatform.common.*;
import com.lightinit.hsdataplatform.dictionary.DicPaasApi;
import com.lightinit.hsdataplatform.dictionary.DicRuningMonitoringType;
import com.lightinit.hsdataplatform.dictionary.DicTemplateCode;
import com.lightinit.hsdataplatform.entity.RunningMonitoringLog;
import com.lightinit.hsdataplatform.mapper.RunningMonitoringLogMapper;
import org.apache.commons.lang.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.apache.log4j.LogManager;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

@DisallowConcurrentExecution
public class FluxJob implements Job {

    org.apache.log4j.Logger log = LogManager.getLogger(FluxJob.class);

//    @Autowired
//    private RunningMonitoringLogMapper runningMonitoringLogMapper ;

    public void execute(JobExecutionContext jobExecutionContext) {
        JobDataMap map = jobExecutionContext.getJobDetail().getJobDataMap();
        String path =(String) map.get("path");
        String start = (String)map.get("alertStart");
        String end =(String) map.get("alertEnd");
        RunningMonitoringLogMapper runningMonitoringLogMapper =(RunningMonitoringLogMapper) map.get("runningMonitoringLogMapper");
        map.clear();

        String fluxUrl = ConfigUtils.getConfiguration(SysContants.PropertyFilePath.CONFIG).getString(SysContants.PropertyKey.FLUXURL) ;
        String token = ConfigUtils.getConfiguration(SysContants.PropertyFilePath.CONFIG).getString(SysContants.PropertyKey.FLUXTOKEN) ;
        String db = ConfigUtils.getConfiguration(SysContants.PropertyFilePath.CONFIG).getString(SysContants.PropertyKey.DB) ;
        String q = ConfigUtils.getConfiguration(SysContants.PropertyFilePath.CONFIG).getString(SysContants.PropertyKey.Q) ;

        Map<String,String> head = new HashMap<>() ;
        head.put("Authorization",token) ;
        List<NameValuePair> req =new ArrayList<NameValuePair>();
        req.add(new BasicNameValuePair("db",db));
        req.add(new BasicNameValuePair("q",q));
        try {
            String str = HttpTookitUtils.doPost(fluxUrl,req,CharEncoding.UTF8,head);
            Map<String, Object> map1 = HttpUtil.strToMap(str);
            List<Map<String,Object>> list = (List<Map<String, Object>>) map1.get("results");
            List<Map<String,Object>> list1 = (List<Map<String, Object>>) list.get(0).get("series");
            List<List<Object>> list2 = (List<List<Object>>) list1.get(0).get("values");
            List<Object> objects = list2.get(0);
            String date = (String) objects.get(0);
            Date date1 = new Date() ;
            Double value = (Double) objects.get(1) ;
            //插入监控流量
            RunningMonitoringLog runningMonitoringLog = new RunningMonitoringLog() ;
            runningMonitoringLog.setMonitoringType(DicRuningMonitoringType.MICROAPP);
            runningMonitoringLog.setLogTime(new Date());
            runningMonitoringLog.setCreatedAt(new Date());
            runningMonitoringLog.setTitle(DicRuningMonitoringType.MICROAPP);
            runningMonitoringLog.setDescription("流量："+value);
            runningMonitoringLogMapper.insert(runningMonitoringLog) ;
            //发送短信
            //判断是否在发短信的事件内
            Calendar corrent = Calendar.getInstance();
            int correntHuor = corrent.get(Calendar.HOUR_OF_DAY);
            int correntMinute = corrent.get(Calendar.MINUTE);
            String corr=(correntHuor<10?("0"+correntHuor):correntHuor)+":"+(correntMinute<10?("0"+correntMinute):correntMinute);
            if(corr.compareTo(start)>0 && corr.compareTo(end)<0 ){
                String phonepeople = ConfigUtils.getConfiguration(SysContants.PropertyFilePath.CONFIG).getString(SysContants.PropertyKey.PHONEPEOPLE);
                String[] phones = phonepeople.split(",");
                for (String phone : phones) {
                    ShortPhoneUtils.send(phone, DicTemplateCode.FLUX,date1,value.toString());
                }
                log.info(date1+"=====流量监控:"+value);
            }
        } catch (Exception e) {
            log.error(new Date()+"监控流量发生异常！");
        }

    }
}
