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.yk.utils.MapStrUtil;
import com.fs.ad.yk.vo.DataBackVo;

import java.util.*;

import static com.fs.ad.yk.utils.SignUtils.sign;

public class YouKuApiUtils {

    private static final String URL = "https://missile.youku.com/api/ad/conv/v2";


    public static void sendYk(List<DataBackVo> vo){
        vo.parallelStream().forEach(YouKuApiUtils::dataBack);
    }
    public static void dataBack(DataBackVo vo){
        Map<String, Object> data = new HashMap<>();
        data.put("event_type", "wechat");
        data.put("event_time", System.currentTimeMillis());
        data.put("track_id", vo.getTrackId());
        data.put("creative_id", vo.getCreativeId());
        Map<String, Object> param = new HashMap<>();
        param.put("appKey", vo.getKey());
        param.put("content", data);
        param.put("signature", sign(vo.getKey(), vo.getToken(),  MapStrUtil.toStr(data)));
        HttpRequest request = HttpUtil.createPost(URL).header("Content-Type", "application/json").body(JSON.toJSONString(param));
        HttpResponse execute = request.execute();
        JSONObject result = JSON.parseObject(execute.body());
        System.out.println(result);
    }

    public static void main(String[] args) {
        sendYk(Collections.singletonList(DataBackVo.builder().trackId("c545ef64630041f88fd784a75974753a").creativeId("110001").build()));
    }

}
