package com.nms.server.bean;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import com.fang.U;
import com.nms.server.common.Common;
import com.nms.server.common.Config;
import com.nms.server.dao.OracleToMysql;

public class DetectInfo {
	
	public static final int SEQID = 0;		//seqId
	public static final int SETINFOID = 1;	//监测设置ID
	public static final int CHECKTYPE = 2;	//监测类别
	public static final int PROCESS = 3;		//进程名称（监测类别设置名称）
	public static final int STARTTIME = 4;		//监测服务启动时间
	public static final int DELAYTIME = 5;	//检测时延（秒）
	public static final int CHECKTIME = 6;		//本次检测时间
	public static final int CURRENTTIMES = 7;	//尝试次数
	public static final int NEXTCHECKTIME = 8;		//下次计划监测时间
	public static final int ARRIVETIME = 9;
	public static final int STATE = 10;				//执行状态是否成功是否正常
	public static final int SHOWNUMS =  11;	//告警序列(数据)
	public static final int PLEVEL =  12;		//告警级别(数据)
	public static final int PVALUE =  13;		//告 警 值(数据)
	public static final int DSINFO = 14;		//状态信息（描述信息）
	public static final int PDATA = 15;		//性能数据
	public static final int DETAILS = 16;//详细参数
	public static final int DELYFLAG = 17; //是否解析详细参数标志
	public static final int URGENTLEVEL =18 ; //紧急级别
	public static final int ALARMINFO =19 ;//告警信息
	public static final int SENDEMAILFLAG =20;//邮件发送标志
	public static final int STATECHANGEFLAG =21;//状态变更标志
	public static final int STATUSCHANGTIME = 22;//状态变更时间
	public static final int EMAILINFO = 23;//邮件信息
	public static final int DISKINFOLIST = 24; // net 特殊格式追加详细信息
	public static final int NETINFOLIST =25; // net 特殊格式追加详细信息
	public static final int APPENDWARNINGINFO = 26;//延迟数据，追加告警信息
	//public static final int DSB = 27;//存放字节数字格式的监测数据，用于解析异常时，存监测数据到硬盘的时候用
	
	
	/**
	 * 以下属性用于记录原监测数据
	 */
	public static final int VALID = 27;
	public static final int DETECTIONINFOID = 28;
	public static final int WAITTIME = 29;
	
	/**
	 * 从解析之后数据恢复成原数据时使用
	 */
	public static final int SHOWNUMLIST = 30;
	public static final int PLEVELLIST = 31;
	public static final int ALARMLIST = 32;
	
	/**
	 * detect_queue 的 key
	 */
	public static final String INFO_KEY = "INFO";
	public static final String DISK_STR_KEY = "DISK_STR";
	public static final String NET_STR_KEY = "NET_STR";
	/**
	 * 详细表中的外键字段名
	 */
	public static final String DETECTION_INFO_ID = "DETECTION_INFO_ID";
	
	public static final String DETECTION_INFO="detection_info";
	
	
	
	/*
	 * 该字段是针对，同一时间点多条具体数据（写多个文件），比如：网卡监测（多个网卡问题）
	 */
	private List<String> detailDatas = new LinkedList<String>();
	
	/*
	 * 针对公共数据的描述信息
	 */
	private String descInfo;
	
	/**
	 * 尝试次数 （小于最大尝试次数）
	 */
	private int testTimes;
	
	/**
	 * 监测时间
	 */
	private long checkTime;
	
	/*
	 * 该字段是针对一条数据，关联的其他表的数据；写入到一个文件的： String 是解析标识， List<String[]>是多条数据（String[]存入表中的数据）
	 * 如：系统信息监测，数据中有网卡数量，网卡数量关联着其他表（表中又有多个字段，如网卡名称，IP，子网掩码等等）
	 */
	private Map<String, List<String[]>> relatedDatas;
	
	public List<String> getDetailDatas() {
		return detailDatas;
	}
	public void setDetailDatas(List<String> detailDatas) {
		this.detailDatas = detailDatas;
	}
	public String getDescInfo() {
		return descInfo;
	}
	public void setDescInfo(String descInfo) {
		this.descInfo = descInfo;
	}
	public Map<String, List<String[]>> getRelatedDatas() {
		return relatedDatas;
	}
	public void setRelatedDatas(Map<String, List<String[]>> relatedDatas) {
		this.relatedDatas = relatedDatas;
	}
	public int getTestTimes() {
		return testTimes;
	}
	public void setTestTimes(int testTimes) {
		this.testTimes = testTimes;
	}
	public long getCheckTime() {
		return checkTime;
	}
	public void setCheckTime(long checkTime) {
		this.checkTime = checkTime;
	}

	/**
	 * DETECTION_INFO insert sql
	 * @return
	 */
	public static String getInsertInfoSql(){
		String insertInfoSql = "";
		if(Common.SEQUENCE.get(DetectInfo.DETECTION_INFO)!=null){
			insertInfoSql = "INSERT INTO DETECTION_INFO (ID, DETECTION_SET_INFO_ID, DETECTION_STATE_INFO, " +
					"PERFORMACE_DATA, CURRENT_TIMES, START_TIME, DELAY_TIME, NEXT_CHECK_TIME, " +
					"POLICE_LEVEL, DATA_CHECK_TIME, DATA_ARRIVE_TIME, DETECTIONED_STATE, " +
					"DATA_CHECK_TIME_DIGITAL, DATA_ARRIVE_TIME_DIGITAL, SEQ_ID, POLICE_EMERGENT,STATUS_CHANGE_TIME,SEQUENCE)"
					+" VALUES (?, ?, ?, ?, ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, ?, ?, ?, ?,to_date(?,'yyyy-mm-dd hh24:mi:ss'),?)";
		}else{
			insertInfoSql = "INSERT INTO DETECTION_INFO (ID, DETECTION_SET_INFO_ID, DETECTION_STATE_INFO, " +
					"PERFORMACE_DATA, CURRENT_TIMES, START_TIME, DELAY_TIME, NEXT_CHECK_TIME, " +
					"POLICE_LEVEL, DATA_CHECK_TIME, DATA_ARRIVE_TIME, DETECTIONED_STATE, " +
					"DATA_CHECK_TIME_DIGITAL, DATA_ARRIVE_TIME_DIGITAL, SEQ_ID, POLICE_EMERGENT,STATUS_CHANGE_TIME)"
					+" VALUES (?, ?, ?, ?, ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, ?, ?, ?, ?,to_date(?,'yyyy-mm-dd hh24:mi:ss'))";
		}
			
		return OracleToMysql.trans(insertInfoSql);
	}
	
	
	/**
	 * 组织detail表的insert语句
	 * @param fieldNames
	 * @param detailTableName
	 * @param tempMap
	 * @return
	 */
	public static String createDetailSql(List<String> fieldNames,
			String detailTableName, Map<String, String> tempMap) {
		String insertDetailSql;
		StringBuffer preSql = new StringBuffer();
		StringBuffer sufSql = new StringBuffer();
		preSql.append(",DETECTION_INFO_ID");
		sufSql.append(",?");
		fieldNames.add("DETECTION_INFO_ID");
		for(String tempField : tempMap.keySet()){
			fieldNames.add(tempField);
			preSql.append("," + tempField);
			sufSql.append(",?");
		}
		insertDetailSql = "insert into "+detailTableName +" ( "+ preSql.substring(1) +" ) values (" + sufSql.substring(1) +" )";
		return insertDetailSql;
	}
	
	/**
	 * 计算 detection_info 及详细表的关联主键
	 * @param checkTime 监测时间
	 * @param seqId 节点id
	 * @param setId 监测设置id
	 * @return  10位时间戳 + 5位 seqId + 3位 setId
	 */
	public static String computeId(String checkTime,String seqId,String setId){
		if(U.Strs.isAllNotBlank(checkTime,seqId,setId)){
			StringBuilder sb = new StringBuilder();
			checkTime = checkTime.substring(checkTime.length() -10);//截取时间戳 后10位
			sb.append("00000");
			sb.append(seqId);
			seqId = sb.substring(sb.length()-5);// seqId 占 5位
			sb.delete(0, sb.length());//清空sb
			sb.append("000");
			sb.append(setId);
			setId = sb.substring(sb.length() - 3); // set info id 占 3位
			sb.setLength(0);
			sb.append(checkTime);
			sb.append(seqId);
			sb.append(setId);
			return sb.toString();
		}
		return null;
	}
	
	/**
	 * 监测设置查询权限 为 1 2 3 的邮件列表 sql
	 * @return
	 */
	public static String getDetecEmailListOf123Sql(){
		StringBuilder sb = new StringBuilder();
		sb.append(" select distinct c.id, xyj1.email  ");
		sb.append(" from xt_yh_jbxx xyj1, ");
		sb.append("    (select dsi.id, ',' || dsi.contact_user_ids || ',' as ids ");
		sb.append("     from detection_set_info dsi ");
		sb.append("    where dsi.contact_user_ids is not null) c ");
		sb.append(" where  c.ids  like ('%,' || xyj1.yhid || ',%') ");
		sb.append(" and xyj1.email  is not null ");
		sb.append("  union  ");
		sb.append(" (select distinct dsi.id,xyj.email ");
		sb.append("   from detection_set_info dsi ");
		sb.append("   left join xt_js_jbxx xjj ");
		sb.append("     on dsi.create_usergroup_id = xjj.jsbh ");
		sb.append("   left join xt_yh_js_index xyji ");
		sb.append("     on xjj.jsbh = xyji.jsbh ");
		sb.append("   left join xt_yh_jbxx xyj ");
		sb.append("     on xyj.yhid = xyji.yhid ");
		sb.append("  where xyj.is_receiveemail = '0' ");
		sb.append("    and xjj.zxbz = 0 ");
		sb.append("    and xjj.type = 1 ");
		sb.append("    and xyj.zxbz = 0 ");
		sb.append("    and dsi.view_level = 2 ");
		sb.append("    and xyj.email is not null ");
		sb.append("    and dsi.create_usergroup_id is not null ");
		sb.append("    and dsi.contact_user_ids is null) ");
		sb.append(" union ");
		sb.append(" ( ");
		sb.append("    select distinct dsi.id,xyj.email from detection_set_info dsi ");
		sb.append("  left join gorup_system_table gst ");
		sb.append("    on gst.system_id = dsi.system_id ");
		sb.append("  left join xt_js_jbxx xjj  ");
		sb.append("  on gst.user_group_id = xjj.jsbh ");
		sb.append(" left join xt_yh_js_index xyji ");
		sb.append(" on xjj.jsbh = xyji.jsbh ");
		sb.append(" left join xt_yh_jbxx xyj ");
		sb.append(" on xyji.yhid = xyj.yhid ");
		sb.append(" where xyj.is_receiveemail = '0' ");
		sb.append("    and xjj.zxbz = 0 ");
		sb.append("    and xjj.type = 1 ");
		sb.append("    and xyj.zxbz = 0 ");
		sb.append("    and dsi.view_level = 3  ");
		sb.append("    and xyj.email is not null ");
		sb.append("    and dsi.contact_user_ids is null ");
		sb.append(" ) ");
		return OracleToMysql.trans(sb.toString());
	}
	
	
	/**
	 * 监测设置查看权限为 4 的 邮件列表 sql
	 * @return
	 */
	public static String getDetecEmailListOf4Sql(){
		StringBuilder sb = new StringBuilder();
		sb.append(" select distinct nt.seq_id ,xyj.email");
		sb.append("   from xt_yh_jbxx xyj ");
		sb.append("   left join xt_yh_js_index xyji ");
		sb.append("     on xyji.yhid = xyj.yhid ");
		sb.append("   left join xt_js_jbxx xjj ");
		sb.append("     on xjj.jsbh = xyji.jsbh ");
		sb.append("   left join gorup_system_table gst ");
		sb.append("     on gst.user_group_id = xjj.jsbh ");
		sb.append("   left join system_table st ");
		sb.append("     on st.system_id = gst.system_id ");
		sb.append("   left join node_table nt ");
		sb.append("     on nt.system_id = gst.system_id ");
		sb.append("   left join nodegroup_table ngt ");
		sb.append("     on ngt.group_id = nt.node_group_id ");
		sb.append("  where xyj.is_receiveemail = '0' ");
		sb.append("    and nt.node_state = 0 ");
		sb.append("    and ngt.is_valid = 1 ");
		sb.append("    and st.system_state = 0 ");
		sb.append("    and xjj.zxbz = 0 ");
		sb.append("    and xjj.type = 1 ");
		sb.append("    and xyj.zxbz = 0 ");
		return sb.toString();
	}
	
	/**
	 * detection_info 表的 insert 语句
	 * @return
	 */
	public static String getInsertDetectInfoSql(){
		String insertInfoSql="";
		if(Common.SEQUENCE.get(DetectInfo.DETECTION_INFO)==null){
			insertInfoSql = "INSERT INTO DETECTION_INFO (ID, DETECTION_SET_INFO_ID, DETECTION_STATE_INFO, " +
					"PERFORMACE_DATA, CURRENT_TIMES, START_TIME, DELAY_TIME, NEXT_CHECK_TIME, " +
					"POLICE_LEVEL, DATA_CHECK_TIME, DATA_ARRIVE_TIME, DETECTIONED_STATE, " +
					"DATA_CHECK_TIME_DIGITAL, DATA_ARRIVE_TIME_DIGITAL, SEQ_ID, POLICE_EMERGENT)"
					+" VALUES (?, ?, ?, ?, ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, ?, ?, ?, ?)";
		}else{
			 insertInfoSql = "INSERT INTO DETECTION_INFO (ID, DETECTION_SET_INFO_ID, DETECTION_STATE_INFO, " +
				"PERFORMACE_DATA, CURRENT_TIMES, START_TIME, DELAY_TIME, NEXT_CHECK_TIME, " +
				"POLICE_LEVEL, DATA_CHECK_TIME, DATA_ARRIVE_TIME, DETECTIONED_STATE, " +
				"DATA_CHECK_TIME_DIGITAL, DATA_ARRIVE_TIME_DIGITAL, SEQ_ID, POLICE_EMERGENT,SEQUENCE)"
				+" VALUES (?, ?, ?, ?, ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, ?, ?, ?, ?,?)";
		}
		return OracleToMysql.trans(insertInfoSql);
	}
	
	/**
	 * detection_warning 表的insert 语句
	 * @return
	 */
	public static String getInsertWarningSql(){
		//插入warning表
		String insertWaringSql = "INSERT INTO DETECTION_INFO_WARNING (ID, DETECTION_SET_INFO_ID, DETECTION_STATE_INFO, " +
				"PERFORMACE_DATA, CURRENT_TIMES, START_TIME, DELAY_TIME, NEXT_CHECK_TIME, " +
				"POLICE_LEVEL, DATA_CHECK_TIME, DATA_ARRIVE_TIME, DETECTIONED_STATE, " +
				"DATA_CHECK_TIME_DIGITAL, DATA_ARRIVE_TIME_DIGITAL, SEQ_ID, POLICE_EMERGENT)"
				+" VALUES (?, ?, ?, ?, ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, ?, ?, ?, ?)";
		return OracleToMysql.trans(insertWaringSql);
	}
	
	/**
	 * email_info 表的insert语句
	 * @return
	 */
	public static String getInsertEmailSql(){
		//插入email_table表
		String insertEmailSql = " insert into email_table (to_address,action_type,content,send_flag,action_ip,action_date,action_desc,send_level,CREATE_TIME)" +
				" values(?,?,?,?,?,to_date(?,'yyyy-mm-dd hh24:mi:ss'),?,?,to_date(?,'yyyy-mm-dd hh24:mi:ss'))";
		return OracleToMysql.trans(insertEmailSql);
	}
	
	/**
	 * 更新状态变更时间
	 * @return
	 */
	public static String getUpdateStatusSql(){
		String sql = "update detection_info_new din set din.status_change_time =to_date(?,'yyyy-mm-dd hh24:mi:ss')  where din.seq_id=?";
		return OracleToMysql.trans(sql);
	}
	
	public static String getSaveToNewSql(){
		String sql=" insert into detection_info_new(detection_set_info_id  ,detection_state_info  ,"
				+ "performace_data  ,current_times  ,start_time  ,wait_time  ,delay_time  ,next_check_time  ,"
				+ "police_level   ,data_check_time  ,data_arrive_time  ,detectioned_state  ,"
				+ "status_change_time  ,seq_id  ,detection_info_id,data_check_time_digital,data_arrive_time_digital      ,"
				+ "POLICE_EMERGENT  ) VALUES"
				+"(?,?,"
				+ "?,?,to_date(?,'yyyy-mm-dd hh24:mi:ss'),?,?,to_date(?,'yyyy-mm-dd hh24:mi:ss'),"
				+ "?,to_date(?,'yyyy-mm-dd hh24:mi:ss'),to_date(?,'yyyy-mm-dd hh24:mi:ss'),?,"
				+ "to_date(?,'yyyy-mm-dd hh24:mi:ss'),?,?,?,?,?)";
		return OracleToMysql.trans(sql);
	}
	
	public static String getUpdateToNewSql(){
		String sql=" update detection_info_new din set "
				
				+"din.detection_state_info = ?"

				+", din.performace_data = ?"

            +", din.current_times = ?"

            +", din.start_time = to_date(?,'yyyy-mm-dd hh24:mi:ss')"

            +", din.wait_time = ?"

           +" , din.delay_time = ?"

           +" , din.next_check_time = to_date(?,'yyyy-mm-dd hh24:mi:ss')"

           +" , din.police_level = ?"

           +" , din.data_check_time = to_date(?,'yyyy-mm-dd hh24:mi:ss')"

           +" , din.data_arrive_time = to_date(?,'yyyy-mm-dd hh24:mi:ss')"

            +", din.detectioned_state = ?"

          +"  , din.status_change_time = to_date(?,'yyyy-mm-dd hh24:mi:ss')"

           +" , detection_info_id = ?"

          +"  ,data_check_time_digital = ?"

           +" ,data_arrive_time_digital = ?"

           +" ,POLICE_EMERGENT = ?"

          +"  where din.detection_set_info_id = ?"

           +" and din.seq_id = ?";
		return OracleToMysql.trans(sql);
	}
	
	public static String getChangeWarningInfoSql(){
		String sql="update detection_info_warning set valid =0 where SEQ_ID=? and DETECTION_SET_INFO_ID=? and valid=1";
		return OracleToMysql.trans(sql);
	}
}
