package com.community.huiju.service.impl;

import com.community.huiju.common.hk.code.AcsEvent;
import com.community.huiju.common.hk.code.EventDis;
import com.community.huiju.dao.MqMessageMapper;
import com.community.huiju.model.MqMessage;
import com.community.huiju.service.MqMessageServiceI;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * @author FXF
 * @date 2018-11-23
 */
@Service("mqMessageService")
public class MqMessageServiceImpl implements MqMessageServiceI {
	
	@Autowired
	private MqMessageMapper mqMessageMapper;
	
	@Override
	public void saveMqMessage(EventDis.CommEventLog parseFrom) throws InvalidProtocolBufferException, ParseException {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ByteString extInfo = parseFrom.getExtInfo();
		//额外信息
		AcsEvent.AccessEventLog exitFrom = AcsEvent.AccessEventLog.parseFrom(extInfo);
		//插入数据库
		MqMessage mqMessage = new MqMessage();
		mqMessage.setEventState(parseFrom.getEventState());
		mqMessage.setEventLevel(parseFrom.getEventLevel());
		mqMessage.setUnitIdx(parseFrom.getUnitIdx());
		mqMessage.setEventType(parseFrom.getEventType());
		mqMessage.setEventTypeName(parseFrom.getEventTypeName());
		mqMessage.setSubSysType(parseFrom.getSubSysType());
		mqMessage.setEventName(parseFrom.getEventName());
		mqMessage.setStartTime(format.parse(parseFrom.getStartTime()));
		mqMessage.setStopTime(format.parse(parseFrom.getStopTime()));
		mqMessage.setSourceIdx(parseFrom.getSourceIdx());
		mqMessage.setSourceType(parseFrom.getSourceType());
		mqMessage.setSourceName(parseFrom.getSourceName());
		mqMessage.setRegionIdx(parseFrom.getRegionIdx());
		mqMessage.setEventCode(exitFrom.getEventCode());
		mqMessage.setDeviceId(exitFrom.getDeviceId());
		mqMessage.setDeviceType(exitFrom.getDeviceType());
		mqMessage.setDeviceName(exitFrom.getDeviceName());
		mqMessage.setEventCard(exitFrom.getEventCard());
		mqMessage.setPersonId(exitFrom.getPersonId());
		mqMessage.setPersonName(exitFrom.getPersonName());
		mqMessage.setDeptId(exitFrom.getDeptId());
		mqMessage.setDeptName(exitFrom.getDeptName());
		mqMessage.setDeptCode(exitFrom.getDeptCode());
		mqMessage.setInOut(exitFrom.getInOut());
		mqMessage.setExtTxt(exitFrom.getExtTxt());
		mqMessage.setCardStatus(exitFrom.getCardStatus());
		mqMessage.setPicUrl(exitFrom.getPicUrl());
		//插入mq_message
		mqMessageMapper.insertSelective(mqMessage);
		//改变长连接表里的状态
		mqMessageMapper.updateConnectionByName();
	}
}
