package cn.com.ctop.kuaishou.modules.batch.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.Check;
import cn.com.ctop.common.module.utils.HttpUtils;
import cn.com.ctop.common.module.utils.KuaishouInterfaceConstant;
import cn.com.ctop.common.module.utils.PropertiesUtils;
import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouActionBarText;
import cn.com.ctop.kuaishou.modules.batch.mapper.KuaiShouActionBarTextMapper;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouActionBarTextService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 快手-行动号召文案
 * @Author: jeecg-boot
 * @Date: 2019-08-15
 * @Version: V1.0
 */
@Service
@Slf4j
public class KuaiShouActionBarTextServiceImpl extends ServiceImpl<KuaiShouActionBarTextMapper, KuaiShouActionBarText> implements IKuaiShouActionBarTextService {
    @Autowired
    private ICtopOauthTokenService tokenService;
    @Autowired
    private KuaiShouActionBarTextMapper actionBarTextMapper;

    @Override
    public void getActionBarText() {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("media_id", 2);
        queryWrapper.orderByDesc("update_time");
        queryWrapper.last("limit 1");
        CtopOauthToken token = tokenService.getOne(queryWrapper);
        List list = new ArrayList();
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.ACTION_BAR_TEXT_LIST;
        Map<String, String> headers = new HashMap<>();
        headers.put("Access-Token", token.getAccessToken());
        headers.put("Content-Type", "application/json");
        JSONObject requestJson = new JSONObject();
        requestJson.put("advertiser_id", token.getAccountId());
        for (int i = 0; i < list.size(); i++) {
            requestJson.remove("campaign_type");
            requestJson.put("campaign_type", list.get(i));
            String result = HttpUtils.kuaiShouhttpPostRequest(url, requestJson.toJSONString(), headers);
            JSONObject resultJson = JSONObject.parseObject(result);
            Integer code = resultJson.getInteger("code");
            if (code == 0) {
                JSONObject data = resultJson.getJSONObject("data");
                if (!Check.isNull(data)) {
                    Integer campaign_type = data.getInteger("campaign_type");
                    JSONArray action_bar_text = data.getJSONArray("action_bar_text");
                    if (!Check.isNull(action_bar_text)) {
                        Map<String, Object> deleteMap = new HashMap<>();
                        deleteMap.put("type", campaign_type);
                        actionBarTextMapper.deleteByMap(deleteMap);
                        for (int j = 0; j < action_bar_text.size(); j++) {
                            String actionBarText = action_bar_text.getString(j);
                            KuaiShouActionBarText kuaiShouActionBarText = new KuaiShouActionBarText();
                            kuaiShouActionBarText.setActionBarText(actionBarText);
                            kuaiShouActionBarText.setType(campaign_type);
                            actionBarTextMapper.insertSelective(kuaiShouActionBarText);
                        }
                    }
                }

            }
        }


    }
}
