package com.cyksj.service.order.post.impl;

import cn.hutool.core.net.URLDecoder;
import com.cyksj.common.util.IoKit;
import com.cyksj.common.util.J11HttpC;
import com.cyksj.common.util.Jsons;
import com.cyksj.common.util.StringUtil;
import com.cyksj.mapper.order.OrderDonPostMapper;
import com.cyksj.model.request.BaiDuConversionType;
import com.cyksj.service.order.post.AbstractPostDataService;
import com.cyksj.service.order.post.PostDataFactory;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chan
 * @className BaiDuPostDataService
 * @description 百度回传
 * @date 2021/02/22 3:35 下午
 */

@Slf4j
@RequiredArgsConstructor
@Service("BaiDuPostDataService")
public class BaiDuPostDataService extends AbstractPostDataService {


    private static final String TOKEN = "Xt2AxQZGQLuxOQcluG5KgY6dIbKOIZrq@TmUNbyvcY4Uyu1GE05AogX3kWQVZKwn0";

    private static final String BAIDU_OCPC_URL = "https://ocpc.baidu.com/ocpcapi/api/uploadConvertData";

    @Override
    public void postData(Long orderId,String callback, String evenType) throws Exception {
        log.info("百度回传入参id：{}", orderId);
        // 编辑一条转化数据
        BaiDuConversionType cv = new BaiDuConversionType();
        // 设置落地页url
        cv.setLogidUrl(URLDecoder.decode(callback, IoKit.Charsets.UTF_8.getCharset()));
        // 设置转化类型
        cv.setNewType(Integer.parseInt(evenType));
        //设置时间戳
        cv.setConvertTime(System.currentTimeMillis()/1000);

        List<BaiDuConversionType> baiDuConversionTypes = new ArrayList<>();
        baiDuConversionTypes.add(cv);
        Map<String,Object>  data = new HashMap<>(2);
        // 设置API接口回传Token
        data.put("token", TOKEN);
        // 设置API接口回传conversionTypes
        data.put("conversionTypes", baiDuConversionTypes);
        // 发送的完整请求数据

        log.info("百度回传 params:{}",data);
        // 向百度发送数据
        HttpResponse<byte[]> res =
                J11HttpC.custom()
                        .ofPost()
                        .url(BAIDU_OCPC_URL)
                        .headers(J11HttpC.ReqType.raw_json)
                        .body(HttpRequest.BodyPublishers.ofString(Jsons.toJson(data), IoKit.Charsets.UTF_8.getCharset()))
                        .send(HttpResponse.BodyHandlers.ofByteArray());

        Map<String, Object> resMap = Jsons.parseObject(res.body(), Map.class);
        log.info("百度回传：{}", resMap);

        Map<String, Object> map = (Map<String, Object>) resMap.get("header");
        if (0 == (Integer.parseInt(StringUtil.getString(map.get("status"))))) {
            //修改记录状态
            orderPostMapper.updateStatus(orderId);
            log.info("回传记录修改状态");
        }

    }

    @PostConstruct
    public void init() {
        PostDataFactory.add(this,this.getClass().getSimpleName());
    }

}
