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

    /**
     * https://open.oceanengine.com/labels/7/docs/1696710655781900
     *
     * @param rawData
     * @return
     */
    @Override
    public Lead adaptCallbackData(Map<String, String> rawData) {
        Lead lead = new Lead();
        lead.setClickId(rawData.get("track_id"));
        lead.setAdvertiserId(AdvertiserTypeEnum.OCEANENGINE.getCode());
        if (StrUtil.isNotEmpty(rawData.get("cid"))) {
            lead.setIdeaId(rawData.get("cid"));
        } else {
            lead.setIdeaId(rawData.get("promotion_id"));
        }

        if (StrUtil.isNotEmpty(rawData.get("aid"))) {
            lead.setPlanId(rawData.get("aid"));
        } else {
            lead.setIdeaId(rawData.get("project_id"));
        }
        lead.setUnitId(rawData.get("campaign_id"));
        lead.setAdAccountId(rawData.get("userid"));
        lead.setIp(rawData.get("ip"));
        try {
            lead.setClickTs(LocalDateTime.ofInstant(
                    Instant.ofEpochMilli(Long.parseLong(rawData.get("ts"))),
                    ZoneId.systemDefault()));
        } catch (Exception e) {
            log.error("时间转换异常");
        }
        return lead;
    }

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

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

    }
}

