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 BaiduAdapter implements IAdvertiserAdapter {

    /**
     * https://dev2.baidu.com/content?sceneType=0&pageId=103228&nodeId=769
     *
     * @param rawData 原始数据
     * @return
     */
    @Override
    public Lead adaptCallbackData(Map<String, String> rawData) {
        Lead lead = new Lead();
        lead.setClickId(rawData.get("bd_vid"));
        lead.setAdvertiserId(AdvertiserTypeEnum.BAIDU.getCode());
        lead.setIp(rawData.get("ip"));
        lead.setIdeaId(rawData.get("aid"));
        lead.setPlanId(rawData.get("pid"));
        lead.setUnitId(rawData.get("uid"));
        lead.setAdAccountId(rawData.get("userid"));
        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.BAIDU;
    }

    @Override
    public void uploadConversionData(Map<String, Object> conversionData, Map<String, Object> params) {

    }
}

