package com.fs.newAdv.integration.adapter;

import com.fs.newAdv.domain.Lead;
import com.fs.newAdv.enums.AdvertiserTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Map;

/**
 * 百度广告适配器
 *
 * @author zhangqin
 * @date 2025-11-03
 */
@Slf4j
@Component
public class VIVOAdapter implements IAdvertiserAdapter {

    /**
     * https://datanexus.qq.com/doc/develop/guider/interface/conversion/ad_track_click
     *
     * @param rawData 原始数据
     * @return
     */
    @Override
    public Lead adaptCallbackData(Map<String, String> rawData) {
        Lead lead = new Lead();
        lead.setClickId(rawData.get("requestId"));
        lead.setAdvertiserId(AdvertiserTypeEnum.VIVO.getCode());
        lead.setIp(rawData.get("ip"));
        lead.setIdeaId(rawData.get("creativeId"));
        lead.setPlanId(rawData.get("campaignId"));
        lead.setUnitId(rawData.get("adId"));
        lead.setAdAccountId(rawData.get("advertiserId"));
        try {
            lead.setClickTs(LocalDateTime.ofInstant(
                    Instant.ofEpochMilli(Long.parseLong(rawData.get("ts"))),
                    ZoneId.systemDefault()));
        } catch (Exception e) {
            log.error("时间转换异常：", e);
        }
        return lead;
    }

    @Override
    public AdvertiserTypeEnum getAdvertiserType() {
        return AdvertiserTypeEnum.VIVO;
    }

    @Override
    public void uploadConversionData(Map<String, Object> conversionData, Map<String, Object> params) {
        // ------------------------------VIVO----------
        conversionData.put("requestId", params.get("requestId"));
        conversionData.put("creativeId", params.get("creativeId"));
    }
}

