package org.dromara.elevator.service.message.impl;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.json.utils.JsonUtils;
import org.dromara.elevator.constant.TopicConstant;
import org.dromara.elevator.domain.bo.EBehaviorMessageBo;
import org.dromara.elevator.domain.message.RealMsgReq;
import org.dromara.elevator.domain.vo.EElevatorInfoVo;
import org.dromara.elevator.service.IEElevatorInfoService;
import org.dromara.elevator.service.message.RealMsgService;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 实时消息接口
 *
 * @author ym
 * @version 1.0.0
 * @date 2024-08-21 16:19
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class RealMsgServiceImpl implements RealMsgService {

    private final KafkaTemplate<String, String> kafkaTemplate;

    private final IEElevatorInfoService elevatorInfoService;

    /**
     * 实时消息:
     * {
     * "msg": {
     *     "id": 5948381568698368,
     *     "obj": {
     *         "code": "TIM2424071563",
     *         "currRunDis": -1214,
     *         "door": 1,
     *         "floor": "1",
     *         "level": 1,
     *         "registerCode": "30134201152011090003",
     *         "runState": 34,
     *         "speed": 0
     *     },
     *     "sendTime": "2024-08-28 11:02:20",
     *     "title": "实时",
     *     "type": 11
     * },
     * "secretkey": "18C96E1F26EF0946EA8E6E0E4D493CEE"
     * }
     */
    @Override
    public void realMsg(RealMsgReq req) {
        RealMsgReq.RealMsgInfo realMsgInfo = req.getMsg().getObj();
        if (Objects.isNull(realMsgInfo)) {
            return;
        }
        if (Objects.isNull(realMsgInfo.getFloor()) || Objects.isNull(realMsgInfo.getCurrRunDis())) {
            return;
        }

        EElevatorInfoVo info = elevatorInfoService.selectByRegisterCode(realMsgInfo.getRegisterCode());
        if (Objects.isNull(info)) {
//            log.info("实时消息,电梯注册编号:{}, 未查询到相关数据", realMsgInfo.getRegisterCode());
            return;
        }

        EBehaviorMessageBo message = new EBehaviorMessageBo();
        message.setCreateBy(1L);
        message.setDeviceCode(realMsgInfo.getCode());
        message.setRegisterCode(realMsgInfo.getRegisterCode());
        message.setElevatorNum(info.getElevatorNum());
        message.setElevatorName(info.getElevatorName());
        message.setDeptId(info.getDeptId());
        message.setLat(info.getLat());
        message.setLng(info.getLng());
        message.setHappenTime(DateUtils.parseDate(req.getMsg().getSendTime()));
        message.setFloor(realMsgInfo.getFloor());
        message.setLevelStatus(realMsgInfo.getLevel());
        message.setRunStatus(realMsgInfo.getRunState());
        message.setDoorStatus(realMsgInfo.getDoor());
        message.setSpeed(realMsgInfo.getSpeed());
        message.setCurrRunDis(realMsgInfo.getCurrRunDis());
        message.setPassengerStatus(realMsgInfo.getPassengerStatus());
        message.setEnvData(JsonUtils.toJsonString(realMsgInfo.getEnvData()));
        // 推送Kafka
        kafkaTemplate.send(TopicConstant.ELEVATOR_REAL_TOPIC, JsonUtils.toJsonString(message));
    }
}
