package com.xxl.job.executor.batch.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xxl.job.executor.batch.service.IKuaishouRealTimeDataService;
import com.xxl.job.executor.constant.KuaishouInterfaceConstant;
import com.xxl.job.executor.utils.Check;
import com.xxl.job.executor.utils.HttpUtils;
import com.xxl.job.executor.utils.PropertiesUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class KuaishouRealTimeDataServiceImpl implements IKuaishouRealTimeDataService {
    /**
     * 获取计划实时数据
     *
     * @param accountId
     * @param token
     * @param campaignId
     * @param startDate
     * @param endDate
     * @return
     */
    @Override
    public JSONArray getCampaignDate(Long accountId, String token, Long campaignId, String startDate, String endDate) {
        String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.CAMPAIGN_REPORT;
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Access-Token", token);
        Map<String, Object> param = new HashMap<>();
        param.put("start_date", startDate);
        param.put("end_date", endDate);
        param.put("advertiser_id", accountId);
        param.put("temporal_granularity", "DAILY");
        param.put("page_size", 2000);
        param.put("page", 1);

        JSONArray array = new JSONArray();
        array.add(campaignId);
        param.put("campaign_ids", array);
        try {
            String result = HttpUtils.httpPostRequest(url, param, headers);
            System.err.println(result);
            JSONObject jsonObject = JSONObject.parseObject(result);
            if (!Check.isNull(jsonObject)) {
                Integer code = jsonObject.getInteger("code");
                if (code == 0) {
                    JSONObject data = jsonObject.getJSONObject("data");
                    if (!Check.isNull(data)) {
                        return data.getJSONArray("details");
                    }

                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取组实时数据
     *
     * @param accountId
     * @param token
     * @param unitId
     * @param startDate
     * @param endDate
     * @return
     */
    @Override
    public JSONArray getUnitDate(Long accountId, String token, Long unitId, String startDate, String endDate) {
        String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.GTOUP_REPORT;
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Access-Token", token);
        Map<String, Object> param = new HashMap<>();
        param.put("start_date", startDate);
        param.put("end_date", endDate);
        param.put("advertiser_id", accountId);
        param.put("temporal_granularity", "DAILY");
        param.put("page_size", 2000);
        param.put("page", 1);

        JSONArray array = new JSONArray();
        array.add(unitId);
        param.put("unit_ids", array);
        try {
            String result = HttpUtils.httpPostRequest(url, param, headers);
            JSONObject jsonObject = JSONObject.parseObject(result);
            if (!Check.isNull(jsonObject)) {
                Integer code = jsonObject.getInteger("code");
                if (code == 0) {
                    JSONObject data = jsonObject.getJSONObject("data");
                    if (!Check.isNull(data)) {
                        return data.getJSONArray("details");
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
