package cn.amq.traffic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.util.StringUtils;

import cn.amq.basis.AmqBasisEvent;
import cn.amq.basis.AmqConst;
import cn.amq.business.AmqMainPoint;
import cn.amq.peas.TrafficEventAlarmHolder;
import cn.amq.peas.TrafficEventAlarmPeas;
import cn.base.bean.BeanHelper;
import cn.base.util.DateUtil;
import cn.base.util.RHUtil;
import cn.base.util.StringUtil;
import cn.im.trafficalarm.model.TrafficaccidentinfoBean;
/**
 * 发送交通告警事件信息amq
 * 用于发送交通告警事件信息amq
 * */
public class AmqTrafficEventAlarmService {

static Logger logger = Logger.getLogger(AmqTrafficEventAlarmService.class);
	
	/**
	 * 发送   交通告警事件 信息 AMQ
	 *
	 * * return msg 0：AMQ发送失败；1：AMQ发送成功。
	 * 
	 * */
	public String publishTrafficEventAlarmAction(String gjxh,String gjsj,String sbbh,String sbmc,String tdbh,String sjlx,String sjdj,String jcfx,String mblx,String ruleNum,String tpurllj,String bz) {
		String msg = "1";
		//step1：交通告警事件 信息AMQ
		try {
			TrafficEventAlarmHolder holder = new TrafficEventAlarmHolder();
			List<TrafficEventAlarmPeas> list = new ArrayList<TrafficEventAlarmPeas>();
			TrafficEventAlarmPeas action = new TrafficEventAlarmPeas();
			action.setGjxh(gjxh);
			action.setGjsj(gjsj);
			action.setSbbh(sbbh);
			action.setSbmc(sbmc);
			int i_tdbh = 0;
			try {
				i_tdbh = Integer.parseInt(tdbh);
			}catch(Exception ex) {
				i_tdbh = 0;
			}
			action.setTdbh(i_tdbh);
			action.setSjlx(sjlx);
			action.setSjdj(sjdj);
			action.setJcfx(jcfx);
			action.setMblx(mblx);
			action.setRuleNum(ruleNum);
			action.setTpurllj(tpurllj);
			action.setBz(bz);
			list.add(action);
			holder.setList(list);
			AmqMainPoint.publishJob.publishGeneralEvent(holder, AmqConst.AMQ_SUBJECT_trafficEventAlarm);//发送大屏上墙AMQ消息
			logger.info("******发送AMQ消息******【交通告警事件AMQ】，发送内容为：事件编号："+gjxh+"，设备编号："+sbbh+"，通道编号："+tdbh);
		}catch(Exception e) {
			e.printStackTrace();
			msg = "0";
			logger.error("******发送AMQ消息error******【交通告警事件AMQ出现异常】，发送内容为：事件编号："+gjxh+"，设备编号："+sbbh+"，通道编号："+tdbh);
		}
		return msg;
	}
	
	/**
	 * 读取 同步交通告警事件AMQ消息<br>
	 * 读取消息后续处理：1、接收到的数据写入交通告警事件信息表中；
	 * 			   2、将接收到的数据加入TrafficEventAlarmStateDataService缓存中。
	 * return msg 0：数据写入失败；1：数据写入成功。
	 * @author 
	 * @since 2018-01-11
	 * 
	 */
	public void doTrafficEventAlarmAction(AmqBasisEvent amqBasisMsg) {
		TrafficEventAlarmHolder entity = (TrafficEventAlarmHolder)amqBasisMsg;
		List<TrafficEventAlarmPeas> list = entity.getList();
		try {
			/**
			 * step1：将接收到的警情数据写入告警事件缓存中
			*/
			logger.debug("******读取AMQ消息******【交通告警事件信息AMQ】，接收告警事件数量为："+list.size());
			List list_alarm = new ArrayList();
			TrafficaccidentinfoBean alarmBean = null;
			for (TrafficEventAlarmPeas trafficPeasBean : list) {
				alarmBean = new TrafficaccidentinfoBean();
				alarmBean.setAccidentId(trafficPeasBean.getGjxh());
				alarmBean.setAccidentState("1");
				alarmBean.setDirection(trafficPeasBean.getJcfx());
				alarmBean.setEventType(trafficPeasBean.getSjlx());
				alarmBean.setOccurTime(trafficPeasBean.getGjsj());
				alarmBean.setAlarmLevel(trafficPeasBean.getSjdj());
				alarmBean.setEventBigUrl(trafficPeasBean.getTpurllj());
				alarmBean.setVdId(trafficPeasBean.getSbbh());
				alarmBean.setChannelId(String.valueOf(trafficPeasBean.getTdbh()));
				alarmBean.setDeviceId(trafficPeasBean.getBz());
				list_alarm.add(alarmBean);
			}
			//大屏amq告警信息接收缓存
			BeanHelper.getTrafficEventAlarmStateDataService().setTrafficEventAlarmStateListAdd(list_alarm);
			// 长宜应急值守
			BeanHelper.getAlarm2AlarmWorkTableService().setAlarm2AlarmWorkTableDataListListAdd(list_alarm);
			
			// 嘉兴  首页使用
			BeanHelper.getTrafficAlarmStateDataService().setTrafficAlarmStateListAdd(list_alarm);
			
			logger.debug("******读取AMQ消息******【同步预案通知AMQ】执行完成！");
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
}
