package com.gic.haoban.manage.service.service.content.message;

import com.alibaba.fastjson.JSON;
import com.gic.haoban.manage.api.enums.content.MaterialInteractRecordEventType;
import com.gic.haoban.manage.service.pojo.bo.content.message.InteractRecordMessageBO;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.listener.MessageListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author MUSI
 * @Date 2023/4/5 9:49 PM
 * @Description
 * @Version
 **/
@Slf4j
public class InteractRecordMessageListener implements MessageListener<String, String> {

    @Autowired
    InteractRecordMessageService interactRecordMessageService;

    private static final List<Integer> dealEventType = new ArrayList<>();

    static {
        dealEventType.add(MaterialInteractRecordEventType.VISIT_PRODUCT.getCode());
        dealEventType.add(MaterialInteractRecordEventType.VISIT_MATERIAL.getCode());
    }

    @Override
    public void onMessage(ConsumerRecord<String, String> consumerRecord) {
        String value = consumerRecord.value();

        log.info("【InteractRecordMessageListener】处理埋点事件 {}", value);
        InteractRecordMessageBO recordMessageBO = JSON.parseObject(value, InteractRecordMessageBO.class);
        if (recordMessageBO == null) {
            log.info("互动记录解析异常 {}", value);
            return;
        }
        if (recordMessageBO.getEventType() == null || !dealEventType.contains(recordMessageBO.getEventType())) {
            log.info("素材埋点事件类型非 素材相关类型 {}", recordMessageBO.getEventType());
            return;
        }
        interactRecordMessageService.dealRecord(recordMessageBO);
    }
}
