package com.fs.newAdv.integration.adapter;

import cn.hutool.core.util.StrUtil;
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 OPPOAdapter 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("request_id"));
        lead.setAdvertiserId(AdvertiserTypeEnum.OPPO.getCode());
        lead.setIp(rawData.get("ip"));
        if (StrUtil.isNotEmpty(rawData.get("dynamic_creative_id"))){
            lead.setIdeaId(rawData.get("dynamic_creative_id"));
        }else {
            lead.setIdeaId(rawData.get("smart_dynamic_creative_id"));
        }
        if (StrUtil.isNotEmpty(rawData.get("project_id"))){
            lead.setPlanId(rawData.get("project_id"));
        }else {
            lead.setPlanId(rawData.get("smart_project_id"));
        }
        lead.setAdAccountId(rawData.get("account_id"));
        try {
            lead.setClickTs(LocalDateTime.ofInstant(
                    Instant.ofEpochMilli(Long.parseLong(rawData.get("click_time"))),
                    ZoneId.systemDefault()));
        } catch (Exception e) {
            log.error("时间转换异常");
        }
        return lead;
    }

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

    @Override
    public void uploadConversionData(Map<String, Object> conversionData, Map<String, Object> params) {
        // ------------------------------oppo参数----------
        conversionData.put("lbid", params.get("lbid"));
        conversionData.put("pageId", params.get("pageId"));
    }
}

