package com.fs.ad.apis;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fs.ad.dy.vo.DyDataVo;
import lombok.extern.slf4j.Slf4j;

import java.time.Instant;

@Slf4j
public class DyApiUtil {
    private static final String URL = "https://analytics.oceanengine.com/api/v2/conversion";

    public static void dataBack(String clickId, String vid, String dyType){
        DyDataVo vo = DyDataVo.builder().event_type(dyType).outer_event_id(vid).context(DyDataVo.DyContext.builder().ad(DyDataVo.DyContext.DyAd.builder().callback(clickId).build()).build()).build();
        vo.setTimestamp(Instant.now().toEpochMilli());
        HttpRequest request = HttpUtil.createPost(URL);
        request.body(JSON.toJSONString(vo));
        HttpResponse execute = request.execute();
        JSONObject result = JSON.parseObject(execute.body());
        if(result.getInteger("code") != 0){
            log.error("抖音上传失败：{}", result.getString("message"));
        }else{
            log.info("抖音上传成功");
        }
    }
    public static void main(String[] args) {
        String clickId = "B.ezpH241vFUgYLNheuhOp1S12czUywa8aRoZynGsLpFn5fmOU9NdeGkWnPos0VzVGJSxjiCzO64Y3R9il5vt83OWRokMPUSIcxRC9mbcvh48uPR517ae5I5GMNfe5txXsBB";
        String vid = "1833266361549963";
        dataBack(clickId, vid, "work_wechat_added");
    }
}
