package com.ruoyi.database.service;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.database.domain.PassVehicleInfo;
import com.ruoyi.database.domain.ReleaseRequestInfo;
import com.ruoyi.database.util.SimpleJsonConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 简单消息处理服务
 */
@Slf4j
@Service
public class SimpleKafkaMessageService {

    @Autowired
    private SimpleJsonConverter jsonConverter;

    @Autowired
    private PassVehicleInfoService passVehicleInfoService;

    @Autowired
    private ReleaseRequestInfoService releaseRequestInfoService;

    /**
     * 处理Kafka消息
     */
    public void processKafkaMessage(String topic, String message) {
        try {
            JSONObject json = JSON.parseObject(message);

            if (topic.contains("topic_pass32856ik8M5V")) {
                // 过车消息
                PassVehicleInfo info = jsonConverter.toPassVehicleInfo(json);
                if (info != null) {
                    passVehicleInfoService.save(info);
                    log.info("保存过车消息成功, 车牌: {}", info.getPlateNo());
                }
            } else if (topic.contains("topic_releaseRequest32856dSpuPV")) {
                // 放行请求消息
                ReleaseRequestInfo info = jsonConverter.toReleaseRequestInfo(json);
                if (info != null) {
                    releaseRequestInfoService.save(info);
                    log.info("保存放行请求成功, 车牌: {}", info.getPlateNo());
                }
            }

        } catch (Exception e) {
            log.error("处理Kafka消息失败, topic: {}, message: {}", topic, message, e);
        }
    }
}
