package com.muyu.processing.listener;

import com.alibaba.fastjson.JSONObject;
import com.muyu.processing.basic.EventCustom;
import com.muyu.processing.basic.EventListener;
import com.muyu.processing.utils.CacheUtil;
import lombok.extern.log4j.Log4j2;
import org.apache.kafka.common.protocol.types.Field;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Map;

/**
 * 添加数据库事件
 * @Author：杨鹏
 * @Package：com.muyu.processing.listener
 * @Project：car-cloud-server
 * @name：AddDatabaseListener
 * @Date：2024/9/29 22:25
 */
@Log4j2
public class AddDatabaseListener implements EventListener {

    @Resource
    private CacheUtil cacheUtil;

    @Override
    public void onEvent(EventCustom event) {
        log.info("数据库添加");
        log.info("数据为: {}",event.getData());
        JSONObject jsonObject = event.getData();
        String vin = (String) jsonObject.get("vin");
        Map<String, Object> map = (Map<String, Object>) cacheUtil.get(vin);
        if (map != null){
            log.info("本地缓存数据为: {}",map);
            ArrayList<Object> keys = new ArrayList<>();
            ArrayList<Object> values = new ArrayList<>();
            jsonObject.forEach((key, value) ->{
                keys.add(key);
                values.add(value);
            });
            // 添加数据库
        }
    }

    @Override
    public void onApplicationEvent(EventCustom event) {
        onEvent(event);
    }
}
