package com.ruoyi.quartz.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.quartz.service.HuaWeiDataRespService;
import com.ruoyi.system.domain.TaskData;
import com.ruoyi.system.service.ITaskDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 华为广告回归数据查询
 */
@Slf4j
@Service
public class HuaWeiDataRespServiceImpl implements HuaWeiDataRespService {

    @Autowired
    private ITaskDataService taskDataService;

    @Override
    public void getTodayData() {
        String authorization = this.getToken();
        String clientId = "1262989161703450304";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        JSONObject contentObject = new JSONObject();
        contentObject.put("startDate", format.format(new Date()));
        contentObject.put("endDate",format.format(new Date()));
        List<String> taskIds = new ArrayList<>();
        taskIds.add("108366615");
        JSONObject appJosn = new JSONObject();
        appJosn.put("appIds", taskIds);
        contentObject.put("filtering", appJosn);
        HttpRequest httpRequest = HttpUtil.createPost("https://connect-api.cloud.huawei.com/api/marketing-api/v1/report/ad/subtask");
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("client_id", clientId);
        headerMap.put("Authorization", authorization);
        httpRequest.addHeaders(headerMap);
        httpRequest.body(contentObject.toJSONString());
        try {
            HttpResponse response = httpRequest.execute();
            log.info(response.body());
            JSONObject taskJson = JSONObject.parseObject(response.body());
            if("20770001".equals(taskJson.getString("code"))){
                JSONObject dataJson = taskJson.getJSONArray("datas").getJSONObject(0);
                TaskData taskData = new TaskData();
                taskData.setClick(dataJson.getLong("click"));
                taskData.setDownload(dataJson.getLong("download"));
                taskData.setCostDate(dataJson.getString("statDate"));
                taskData.setExposure(dataJson.getLong("exposure"));
                taskData.setClickRate(dataJson.getString("clickRate"));
                taskData.setDownloadRate(dataJson.getString("downloadRate"));
                taskData.setTotalCost(dataJson.getString("cost"));
                taskData.setSourceApp("华为");
                taskData.setDownloadAvgCost(dataJson.getString("downloadAverageCost"));
                taskData.setCreateTime(new Date());
                taskDataService.insertTaskData(taskData);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public String getToken(){
        JSONObject contentJson = new JSONObject();
        contentJson.put("grant_type","client_credentials");
        contentJson.put("client_id","1262989161703450304");
        contentJson.put("client_secret","57A572E30208BF67AD2FD19B3F58C0B07FFBD4AE3AB775617138BD69B85AB7C3");
        String result = HttpUtil.post("https://connect-api.cloud.huawei.com/api/oauth2/v1/token", contentJson.toJSONString());
        JSONObject resultJson = JSONObject.parseObject(result);
        return "Bearer " + resultJson.getString("access_token");
    }
}
