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

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.common.module.utils.HttpUtils;
import cn.com.ctop.common.module.utils.PropertiesUtils;
import cn.com.ctop.toutiao.modules.material.entity.ByteDanceCreativeWordPackage;
import cn.com.ctop.toutiao.modules.material.mapper.ByteDanceCreativeWordPackageMapper;
import cn.com.ctop.toutiao.modules.material.service.IByteDanceCreativeWordPackageService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@Service
public class ByteDanceCreativeWordPackageServiceImpl extends ServiceImpl<ByteDanceCreativeWordPackageMapper, ByteDanceCreativeWordPackage> implements IByteDanceCreativeWordPackageService {

    @Autowired
    private ICtopOauthTokenService tokenService;
    @Resource
    private ByteDanceCreativeWordPackageMapper byteDanceCreativeWordPackageMapper;

    @Override
    public Map<String, Object> getCreativeWordPackage(){
        String accountId = "1649600126891015";
        JSONObject data = new JSONObject();

        //1：获取token
        CtopOauthToken token = tokenService.getOauthTokenByAccountId(accountId);
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Access-Token", token.getAccessToken());

        data.put("advertiser_id", token.getAccountId());
//        data.put("creative_word_ids","");  不填，默认返回所有创意词包
        String url = PropertiesUtils.getValue("bytedance_config", "bytedance_api_url") + PropertiesUtils.getValue("bytedance_config", "bytedance_v2_creative_word_select");
        String result = HttpUtils.httpGetRequest(url,headers,data);
        JSONObject jsonObject = JSONObject.parseObject(result);
        saveCreativeWordPackageToDb(jsonObject);
        return null;
    }

    public void saveCreativeWordPackageToDb(JSONObject result){
        List<ByteDanceCreativeWordPackage> resultList = new ArrayList<ByteDanceCreativeWordPackage>();
        Integer code = result.getInteger("code");
        if(code != null && 0 == code){
            JSONObject data = result.getJSONObject("data");
            JSONArray dataJSONArray = data.getJSONArray("creative_word");
            for (int i = 0 ; i < dataJSONArray.size() ; i++) {
                JSONObject creativeWord = (JSONObject)dataJSONArray.get(i);
                ByteDanceCreativeWordPackage byteDanceCreativeWordPackage = new ByteDanceCreativeWordPackage(creativeWord);
                resultList.add(byteDanceCreativeWordPackage);
            }
            byteDanceCreativeWordPackageMapper.replaceBatch(resultList);
        }
    }
    @Override
    public ByteDanceCreativeWordPackage selectCreativeWordPackageByName(String name){
        return byteDanceCreativeWordPackageMapper.selectCreativeWordPackageByName(name);
    }
}
