package com.nms.server.thread.change;

import java.util.Iterator;
import java.util.List;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import com.nms.server.bean.NodeModel;
import com.nms.server.bean.SetInfo;
import com.nms.server.common.Common;
import com.nms.server.common.Constants;
import com.nms.server.dao.CommonDao;
import com.nms.server.service.ChangeService;
import com.nms.server.service.CommonService;
import com.nms.server.thread.change.socket.ChangeSocketThread;
import com.nms.server.thread.socket.SocketCMD;
import com.nms.server.util.DateUtil;

/**
 *  节点是否报主动告警：
 *    更新其影响的所有的节点是否报主动告警（通讯不成功的，保存入库，后续再进行通信）
 *  @date 2014-5-26
 *	@author hyx
 */
public class ChangeForActiveAlarmInfo implements Runnable{
	private final Logger logger = Logger.getLogger(ChangeForActiveAlarmInfo.class);
	private String datas;
	private Long recordId;
	
	/**
	 * @param datas：true：NC报主动告警；false：NC不报主动告警
	 */
	public ChangeForActiveAlarmInfo(Long recordId,String datas){
		this.datas = datas;
		this.recordId = recordId;
	}

	public void run() {
//		Thread.currentThread().setName("NC是否报主动告警 变更");
		Thread.currentThread().setName("Whether NC Reports Active Alarm Changes");
		
		CommonDao dao = null;
		 
		try {
			dao = new CommonDao();
			ChangeService service = new ChangeService(dao);
			
			if(StringUtils.isBlank(datas)){
				logger.warn("Whether NC reports active alarm change invalid data："+datas);
				if(recordId!=null){
					service.deleteEventRecordByIds(recordId.toString());//删除数据库中记录的通信信息
				}
				return ;
			}
			
			JSONObject jObject = JSONObject.fromObject(datas);
			Boolean showAutoAlarm = (Boolean)jObject.get("showAutoAlarm");
			String webHandleTime = (String)jObject.get("webHandleTime");
			
			
//			Boolean isStartActiveAlarm = datas.equalsIgnoreCase("true") ? true : false;
			
			//组织数据
			logger.debug("下发NC是否报主动告警操作："+showAutoAlarm+" web端操作时间："+webHandleTime);
			
    		JSONObject jsonObj = new JSONObject();
    		jsonObj.put("showAutoAlarm", showAutoAlarm);
    		jsonObj.put("webHandleTime", webHandleTime);
    		this.changeActiveAlarmStartInfo(jsonObj, service);
    		
    		
    		if(recordId!=null){
				service.deleteEventRecordByIds(recordId.toString());
			}
		} catch (Exception e) {
			logger.error("",e);
		}finally{
			if(dao!=null){
				dao.close();
				dao=null;
			}
		}
	}
	
	public void changeActiveAlarmStartInfo(JSONObject jObject,CommonService service){
			
		//变更范围的节点IP
		List<NodeModel> ipList = service.getAllEffectiveNodeInfo();
		
		logger.debug("NC是否报主动告警 节点变更数量"+ipList.size());
		Iterator<NodeModel> ipIte = ipList.iterator();
		while (ipIte.hasNext()) {
			NodeModel ip = ipIte.next();
			 //仅向0服务器节点发送变更信息：0：服务器
			if(ip!= null && ip.getNodeIp()!= null && ip.getNodeType().longValue()==0l && Common.hasIpInIpSegment(ip.getNodeIp())){
	    		Common.runChangeRunnable(new ChangeSocketThread(null,ip.getNodeIp(),Constants.SSL_CLIENT_PORT,SocketCMD.WEB_NOTICE_ACTIVE_ALARM_START_ALERT,jObject));
			}else{
//				downLatch.countDown();
			}
		}
		
	}
	
    /**
     * Agent端通知变更方法
     * @time Sep 20, 2011-2:33:37 PM
     * @param ip
     * @param jObject
     */
//    public void sendToClient(String ip,Object jObject) {
//    	try {
//    		Common.runChangeRunnable(new ChangeSocketThread(null,ip,Constants.SSL_CLIENT_PORT,"char:updateConfig",jObject));
//		} catch (Exception e) {
//			logger.debug("配置变更下发"+ip+"异常"+e.getMessage());
//		}
//	}
//	public static void main(String [] args) {
////		String[] datas = new String[]{"202","401"};
//		CommonDao dao = new CommonDao();
//		CommonService service = new CommonService(dao);
//		SetInfo old = service.getSetInfoById(226l);
//		SetInfo news = service.getSetInfoById(1l);
//		OperationsUpdateBySetInfo thread = new OperationsUpdateBySetInfo(old,news);
//		thread.run();
//		
//	}
}
