package cn.com.ctop.toutiao.modules.tool.service.impl;

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.utils.HttpUtils;
import cn.com.ctop.toutiao.modules.tool.constants.InterestActionUrlConstant;
import cn.com.ctop.toutiao.modules.tool.service.IByteDanceInterestActionService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service
public class ByteDanceInterestActionServiceImpl implements IByteDanceInterestActionService {

    @Override
    public Map<String, Object> queryInterestActionCategory(CtopOauthToken token, JSONObject requestBody) {

        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Access-Token", token.getAccessToken());

        JSONObject params=new JSONObject();
        params.put("advertiser_id",requestBody.getLong("accountId"));
        params.put("action_scene",requestBody.getJSONArray("actionScene"));
        params.put("action_days",requestBody.getInteger("actionDays"));
        JSONObject jsonObject = JSONObject.parseObject(HttpUtils.httpGetRequest(InterestActionUrlConstant.INTEREST_ACTION_CATEGORY, headers,params));
        Map<String,Object> resultMap=new HashMap<>();
        if(jsonObject.getInteger("code")!=0){
            log.error("查询行为类目失败==》accountId:{},message:{}", token.getAccountId(), jsonObject.getString("message"));
            resultMap.put("code", -1);
            resultMap.put("message", jsonObject.getString("message"));
            return resultMap;
        }
        resultMap.put("code", 0);
        return resultMap;

    }

    @Override
    public Map<String, Object> queryInterestActionKeyword(CtopOauthToken token, JSONObject requestBody) {

        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Access-Token", token.getAccessToken());

        JSONObject params=new JSONObject();
        params.put("advertiser_id",requestBody.getLong("accountId"));
        params.put("query_words",requestBody.getString("queryWords"));
        params.put("action_scene",requestBody.getJSONArray("actionScene"));
        params.put("action_days",requestBody.getInteger("actionDays"));

        JSONObject jsonObject = JSONObject.parseObject(HttpUtils.httpGetRequest(InterestActionUrlConstant.INTEREST_ACTION_KEYWORD, headers,params));
        Map<String,Object> resultMap=new HashMap<>();
        if(jsonObject.getInteger("code")!=0){
            log.error("查询行为关键词失败==》accountId:{},message:{}", token.getAccountId(), jsonObject.getString("message"));
            resultMap.put("code", -1);
            resultMap.put("message", jsonObject.getString("message"));
            return resultMap;
        }
        resultMap.put("code", 0);
        return resultMap;
    }

    @Override
    public Map<String, Object> queryInterestActionInterestCategory(CtopOauthToken token, JSONObject requestBody) {

        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Access-Token", token.getAccessToken());

        JSONObject params=new JSONObject();
        params.put("advertiser_id",requestBody.getLong("accountId"));

        JSONObject jsonObject = JSONObject.parseObject(HttpUtils.httpGetRequest(InterestActionUrlConstant.INTEREST_ACTION_INTEREST_CATEGORY, headers,params));
        Map<String,Object> resultMap=new HashMap<>();
        if(jsonObject.getInteger("code")!=0){
            log.error("查询行为类目失败==》accountId:{},message:{}", token.getAccountId(), jsonObject.getString("message"));
            resultMap.put("code", -1);
            resultMap.put("message", jsonObject.getString("message"));
            return resultMap;
        }
        resultMap.put("code", 0);
        return resultMap;
    }

    @Override
    public Map<String, Object> queryInterestActionInterestKeyword(CtopOauthToken token, JSONObject requestBody) {

        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Access-Token", token.getAccessToken());

        JSONObject params=new JSONObject();
        params.put("advertiser_id",requestBody.getLong("accountId"));
        params.put("action_scene",requestBody.getJSONArray("actionScene"));
        params.put("action_days",requestBody.getInteger("actionDays"));
        JSONObject jsonObject = JSONObject.parseObject(HttpUtils.httpGetRequest(InterestActionUrlConstant.INTEREST_ACTION_CATEGORY, headers,params));
        Map<String,Object> resultMap=new HashMap<>();
        if(jsonObject.getInteger("code")!=0){
            log.error("查询行为类目失败==》accountId:{},message:{}", token.getAccountId(), jsonObject.getString("message"));
            resultMap.put("code", -1);
            resultMap.put("message", jsonObject.getString("message"));
            return resultMap;
        }
        resultMap.put("code", 0);
        return resultMap;
    }

    @Override
    public Map<String, Object> queryInterestActionId2Word(CtopOauthToken token, JSONObject requestBody) {
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Access-Token", token.getAccessToken());

        JSONObject params=new JSONObject();
        params.put("advertiser_id",requestBody.getLong("accountId"));
        params.put("action_scene",requestBody.getJSONArray("actionScene"));
        params.put("action_days",requestBody.getInteger("actionDays"));
        JSONObject jsonObject = JSONObject.parseObject(HttpUtils.httpGetRequest(InterestActionUrlConstant.INTEREST_ACTION_CATEGORY, headers,params));
        Map<String,Object> resultMap=new HashMap<>();
        if(jsonObject.getInteger("code")!=0){
            log.error("查询行为类目失败==》accountId:{},message:{}", token.getAccountId(), jsonObject.getString("message"));
            resultMap.put("code", -1);
            resultMap.put("message", jsonObject.getString("message"));
            return resultMap;
        }
        resultMap.put("code", 0);
        return resultMap;
    }

    @Override
    public Map<String, Object> queryInterestActionKeywordSuggest(CtopOauthToken token, JSONObject requestBody) {
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Access-Token", token.getAccessToken());

        JSONObject params=new JSONObject();
        params.put("advertiser_id",requestBody.getLong("accountId"));
        params.put("action_scene",requestBody.getJSONArray("actionScene"));
        params.put("action_days",requestBody.getInteger("actionDays"));
        JSONObject jsonObject = JSONObject.parseObject(HttpUtils.httpGetRequest(InterestActionUrlConstant.INTEREST_ACTION_CATEGORY, headers,params));
        Map<String,Object> resultMap=new HashMap<>();
        if(jsonObject.getInteger("code")!=0){
            log.error("查询行为类目失败==》accountId:{},message:{}", token.getAccountId(), jsonObject.getString("message"));
            resultMap.put("code", -1);
            resultMap.put("message", jsonObject.getString("message"));
            return resultMap;
        }
        resultMap.put("code", 0);
        return resultMap;
    }
}
