package digital.laboratory.platform.entrustment.handler;

import digital.laboratory.platform.common.core.entity.PersonFaceIdentifyInfo;
import digital.laboratory.platform.common.core.util.Msg;
import digital.laboratory.platform.common.core.util.R;
import digital.laboratory.platform.sewage.entity.PersonalIdentityVerifier;
import digital.laboratory.platform.sewage.feign.RemotePersonalIdentityVerifierService;
import digital.laboratory.platform.sewage.feign.RemoteWebSocketService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


@Slf4j
@Component
public class ActiveMQListener {
	private static final Logger logger = LoggerFactory.getLogger(ActiveMQListener.class);
	@Resource
	private RemoteWebSocketService webSocketService;
	@Resource
	private RemotePersonalIdentityVerifierService personalIdentityVerifierService;

	/**
	 *@Description: three MQ 接收方法
	 *@Param:
	 *@return:
	 *@Author: gyDeBug
	 *@date: 2021/9/30
	 **/
	@JmsListener(destination = "${config.msgListener.hardwareTopicName}" ,containerFactory = "activeMQJmsListenerContainerFactory")
	public void receiveActiveMQ(Msg msg)throws Exception {
		Object obj =msg.getData();
		if (obj!=null) {
			if (obj instanceof PersonFaceIdentifyInfo) {
				//== 收到人脸识别消息
				PersonFaceIdentifyInfo info = (PersonFaceIdentifyInfo)obj;
				//查询该智能设备绑定的客户端电脑IP
				try{
					R<PersonalIdentityVerifier> data = personalIdentityVerifierService.getById(info.getDeviceId());
					if(data.getCode() == 200){
						String deviceIp = data.getData().getBindIp();
						//推送消息到人脸智能设备绑定的电脑
						R result = webSocketService.sendFaceRecognitionData(deviceIp,info);
						logger.info("推送消息结果："+result.getMsg());
					}else{
						logger.info("==============服务调用异常！");
					}
				}catch (Exception e){
					logger.info("==============服务调用异常！");
				}
			}
		}
	}
}
