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

import com.alibaba.fastjson.JSON;
import com.gic.dubbo.entity.ProviderLocalTag;
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.commons.lang3.StringUtils;
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;
import java.util.UUID;

/**
 * @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());
        dealEventType.add(MaterialInteractRecordEventType.ADD_SHOPPING_CART.getCode());
    }

    @Override
    public void onMessage(ConsumerRecord<String, String> consumerRecord) {
        ProviderLocalTag providerLocalTag = ProviderLocalTag.tag.get();
        providerLocalTag.traceId = UUID.randomUUID().toString();
        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;
        }
        dataConvert(recordMessageBO);
        interactRecordMessageService.dealRecord(recordMessageBO);
    }

    /**
     * 数据清洗
     */
    public void dataConvert(InteractRecordMessageBO recordMessageBO) {

        if (!MaterialInteractRecordEventType.VISIT_PRODUCT.getCode().equals(recordMessageBO.getEventType())) {
            return;
        }
        if (StringUtils.isBlank(recordMessageBO.getClerkId()) && StringUtils.isNotBlank(recordMessageBO.getRefUrl())) {
            // pages/hb/material/material-root/material-root?contentMaterialId=517690074188505121&storeId=ff8080818499a5f301849ceb9f870035&channelSource=5&clerkId=bcbfff6c8e2e40aaac795a99d0dde2aa
            String refUrl = recordMessageBO.getRefUrl();
            int indexOf = StringUtils.indexOf(refUrl, "clerkId=");
            if (indexOf != -1) {
                String clerkId = StringUtils.substring(refUrl, indexOf + 8, indexOf + 40);
                recordMessageBO.setClerkId(clerkId);
            }
        }
    }
}
