package com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.mqtt.handler;

import com.cico.nest.lcdp.boot.core.qxsd.domain.event.EventProcessStatus;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.common.constatnt.SdgkConstants;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.EventDetailMapper;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.dataobject.EventDetailDO;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.mqtt.handler.dto.SdgkEventPrcessMqDTO;
import com.cico.nest.lcdp.framework.mq.mqtt.core.annoations.MqttTopic;
import com.cico.nest.lcdp.framework.mq.mqtt.core.handler.MqttMessageHandler;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import java.util.Date;
import java.util.Objects;

/**
 * 接收隧道管控上报交通事件处置状态变更主题mqtt
 * topic = notice/你们提供的companyId/videoEventConfirm
 * @author rfx
 * @date 2024/11/17
 */

@MqttTopic(topic = "notice/qxsd/videoEventConfirm",messageClass = SdgkEventPrcessMqDTO.class)
public class SdgkTrafficEventProcessHandler implements MqttMessageHandler<SdgkEventPrcessMqDTO> {

    @Resource
    private EventDetailMapper eventDetailMapper;

    @Override
    public void handleText(SdgkEventPrcessMqDTO message) {
        if (message== null
                || StringUtils.isEmpty(message.getEventId())
                || !SdgkConstants.MQ_COMPANY_ID.equals(message.getCompanyId()) ){
            return;
        }
        // 查询 数据库中事件
        //查找本地已有事件
        EventDetailDO detailDO = eventDetailMapper.selectByThirdPartyId(message.getEventId());
        if(!Objects.isNull(detailDO) ){
            int eventStatus = EventProcessStatus.convert2EventStatus(message.getStatus());
            detailDO.setEventStatus(eventStatus);
            detailDO.setEventProcessStatus(message.getStatus());
            detailDO.setUpdateTime(new Date());
            eventDetailMapper.updateById(detailDO);
        }
    }
}
