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

import com.cico.nest.lcdp.boot.core.qxsd.application.service.EventDetailService;
import com.cico.nest.lcdp.boot.core.qxsd.domain.event.EventSource;
import com.cico.nest.lcdp.boot.core.qxsd.domain.event.EventTypeAllEnum;
import com.cico.nest.lcdp.boot.core.qxsd.domain.gateway.EventGateway;
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.SdgkEventMqReceiveDTO;
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 lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;

import java.util.Date;
import java.util.Objects;

/**
 * 接收隧道管控上报新交通事件主题mqtt
 *
 * @author rfx
 * @date 2024/11/17
 */
@Slf4j
@MqttTopic(topic = "tunnel-event/qxsd",messageClass = SdgkEventMqReceiveDTO.class)
public class SdgkTrafficEventHandler implements MqttMessageHandler<SdgkEventMqReceiveDTO> {

    @Resource
    private EventDetailMapper eventDetailMapper;
    @Resource
    private EventDetailService eventDetailService;

    @Value("${qxsd.tunnel.down-id}")
    private String tunnelId; //后期要改不能写死
    @Resource
    private EventGateway eventGateway;

    @Override
    public void handleText(SdgkEventMqReceiveDTO message) {
        if (message== null || StringUtils.isEmpty(message.getEventId())){
            return;
        }
        try {
            //去判重 判断跟隧道管控事件去重  如果隧道管控同车同类型事件已存在 则标记重复
            EventTypeAllEnum eventTypeEnum = EventTypeAllEnum.getBySdgkCode(message.getEventType());
            //todo 这里后面需要优化，收到的事件来自哪个隧道哪个洞，根据隧道名和方向名字来判断。
            EventDetailDO eventDetailDO = null;
            try {
                Integer milestone = Integer.valueOf(message.getPileNumber());
                boolean hasSameEvent = eventDetailService.judgeSameEvent(null, message.getEventId(), eventTypeEnum.code,milestone,message.getAlarmTime(), EventSource.SDGK.code);
                eventDetailDO = SdgkEventMqReceiveDTO.convert2DO(message);
                if (hasSameEvent){
                    eventDetailDO.setIsRepeat(1);
                }
            } catch (NumberFormatException e) {
                log.error("收到全息事件判断事件重复失败，代码异常。");
            }
            eventDetailDO.setTunnelId(tunnelId);
            //查找本地已有事件
            EventDetailDO detailDO = eventDetailMapper.selectByThirdPartyId(eventDetailDO.getThirdPartyId());
            if(Objects.isNull(detailDO) ){
                eventDetailDO.setReportTime(eventDetailDO.getIsRepeat()!=1?new Date():null);
                eventDetailMapper.insert(eventDetailDO);
            }else{
                eventDetailDO.setId(detailDO.getId());
                eventDetailMapper.updateById(eventDetailDO);
            }
            //发送给前端数据库
            eventGateway.pushRealTimeEventData(tunnelId);
        } catch (NumberFormatException e) {
            log.error("接收隧道管控事件的上报处理异常，{}",e.getMessage());
        }

    }
}
