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

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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class UpdateProjectServiceImpl implements IUpdateProjectService {

    @Override
    public JSONObject updateGroupStatus(CtopOauthToken oauthToken, Map<String,Object> requestBody) {

        //请求头**
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Access-Token", oauthToken.getAccessToken());
        log.info(oauthToken.getAccessToken());
        //请求参数
        JSONObject params = new JSONObject();
        params.put("advertiser_id", requestBody.get("accountId"));
        List<Long> groupIds= new ArrayList<>();
        groupIds.add((Long) requestBody.get("groupId"));
        params.put("group_ids", groupIds);
        params.put("opt_status",requestBody.get("optStatus"));
        log.info(params.toJSONString());

        return JSONObject.parseObject(HttpUtils.httpPostRequest(UpdateProjectUrlConstant.ADLAB_GROUP_STATUS_UPDATE, params, headers));
    }

    @Override
    public JSONObject updateADStatus(CtopOauthToken oauthToken, JSONObject requestBody) {

        //请求头**
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Access-Token", oauthToken.getAccessToken());
        //请求参数
        JSONObject params = new JSONObject();
        params.put("advertiser_id", requestBody.getLong("accountId"));
        params.put("ad_id", requestBody.getLong("adId"));
        params.put("ad_bid", requestBody.getLong("adBid"));
        params.put("group_bid", requestBody.getLong("groupBid"));
        params.put("opt_status", requestBody.getLong("optStatus"));

        return JSONObject.parseObject(HttpUtils.httpPostRequest(UpdateProjectUrlConstant.ADLAB_AD_STATUS_UPDATE, params, headers));
    }
}
