package com.blueland.sys.timetask.telnet;


import java.util.Date;
import java.util.Map;

import cn.hutool.core.date.DateUtil;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Service;
import com.blueland.core.util.HttpUtils;
import com.blueland.core.util.LogUtil;
import com.blueland.core.util.StringUtil;
import common.constant.CommonConstant;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
/**
 * 测试定时任务
 */
@Service("telnetTask")
@Slf4j
@Data @EqualsAndHashCode(callSuper=false)
public class TelnetTask implements Job {
	
	public TelnetTask() {
		
	}
	
	public TelnetTask(String authorization, String url,String bizId,String bizType) {
		super();
		this.authorization = authorization;
		this.url = url;
		this.bizId = bizId;
		this.bizType = bizType;
	}

	//jwt权限
	private String authorization;
	private String url;
	//业务ID
	private String bizId;
	//业务类型
	private String bizType;

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		log.info("telnetTask is run....,date="+ DateUtil.formatDateTime(new Date()));
		
		JobDataMap data = context.getJobDetail().getJobDataMap();
		//这里MAP里面的key是SystimetaskRest里面的固定写法，如果需要其他参数，参照rest中写法
		Map<String,String> dataMap = (Map<String, String>) data.get("data");
		String bizId = dataMap.get("bizId");
		String bizType = dataMap.get("bizType");
		String url = dataMap.get("url");
		String authorization = dataMap.get("authorization");
		log.info("authorization:"+dataMap.get("authorization"));
		log.info("url:"+dataMap.get("url"));
		String result = "";
		String status = CommonConstant.STATUS_FAILED;
		try {
			result = HttpUtils.getInstance().getForObject(url, null, String.class, authorization);
			log.info("result：" + result);
			if(StringUtil.notEmpty(result)) {
				if(result.indexOf("成功") != -1 || result.indexOf("200") != -1 ) {
					status = CommonConstant.STATUS_SUCCESS;
				}
			}
		}catch(Exception e) {
			e.printStackTrace();
			status = CommonConstant.STATUS_FAILED;
			result = e.getMessage();
		}finally {
			//添加系统操作日志
			LogUtil.addSysOplog(null, bizId,bizType, CommonConstant.OPTYPE_SYNC, result, status, "sys", url);
		}
	}
	
}
