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

import com.alibaba.fastjson.JSON;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;
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 com.gic.haoban.manage.service.service.content.adaptor.MessageConvertHelper;
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.Objects;
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;
    @Autowired
    MessageConvertHelper messageConvertHelper;

    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();

        InteractRecordMessageBO recordMessageBO = messageConvertHelper.convertMessageBo(value);
        if (recordMessageBO == null) {
            log.info("互动记录解析异常 {}", value);
            return;
        }
        // 过滤环境
        Config config = ConfigService.getAppConfig();
        String envKey = config.getProperty("bury.point.env.app.key", "");
        if (!StringUtils.equalsIgnoreCase(envKey, recordMessageBO.getAppKey())) {
            log.info("埋点数据环境不正确 config.env: {}, appKey: {}", envKey, recordMessageBO.getAppKey());
            return;
        }
        if (recordMessageBO.getEventType() == null || !dealEventType.contains(recordMessageBO.getEventType())) {
            log.info("埋点事件类型非相关类型 {}", recordMessageBO.getEventType());
            return;
        }
        dataConvert(recordMessageBO);
        String clerkId = recordMessageBO.getClerkId();
        if (Objects.equals(clerkId, "-1")) {
            log.info("导购为空不处理");
            return;
        }
        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);
            }
        }
    }
}
