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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.common.constatnt.RedisConstants;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.BayonetPicturesMapper;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.dataobject.BayonetPicturesDO;
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.springframework.data.redis.core.StringRedisTemplate;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * 卡口图片主题mqtt
 * 暂且不使用
 * @author zws
 * @date 2024/8/29
 */

//@MqttTopic(topic = "event/+/traffic",messageClass = BayonetPicturesDO.class)
public class BayonetPicturesTopicHandler implements MqttMessageHandler<BayonetPicturesDO> {

    @Resource
    private BayonetPicturesMapper bayonetPicturesMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public void handleText(BayonetPicturesDO message) {
        message.setCreateTime(new Date());
        //缓存到redis
        String carPlate = message.getPlateNumber();
        String redisKey = RedisConstants.REDIS_BAYONET_PICTURES + carPlate;
        String messageJson = JSON.toJSONString(message, SerializerFeature.WriteMapNullValue);
        stringRedisTemplate.opsForValue().set(redisKey,messageJson,24, TimeUnit.HOURS);
        //保存到数据库
        bayonetPicturesMapper.insert(message);

    }
}
