package com.xxl.job.executor.batch.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xxl.job.core.enums.NoEn;
import com.xxl.job.executor.batch.service.IKuaiShouCommentService;
import com.xxl.job.executor.common.entity.CtopOauthToken;
import com.xxl.job.executor.common.service.ICtopOauthTokenService;
import com.xxl.job.executor.constant.KuaishouInterfaceConstant;
import com.xxl.job.executor.utils.Check;
import com.xxl.job.executor.utils.HttpUtils;
import com.xxl.job.executor.utils.PropertiesUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 评论
 */
@Slf4j
@Service
public class IKuaiShouCommentServiceImpl implements IKuaiShouCommentService {

    @Autowired
    private ICtopOauthTokenService oauthTokenService;

    @Override
    public void shieldComment(Long accountId) {
        CtopOauthToken oauthToken = oauthTokenService.getTokenByAccountId(accountId);
        queryComments(oauthToken.getAccessToken(), accountId, 1, 500, 0);
    }

    /**
     * 查询评论
     *
     * @param
     * @return com.alibaba.fastjson.JSONArray
     * @throws
     * @author ZHAOXA
     */
    private JSONArray queryComments(String token, Long accountId, int page, int pageSize, int num) {
        String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.COMMENT_LIST;
        Map<String, String> header = new HashMap<>();
        header.put("Access-Token", token);
        header.put("Content-Type", "application/json");
        JSONObject params = new JSONObject();
        params.put("advertiser_id", accountId);
        params.put("shield_status", NoEn.NO1.valueInt());//未隐藏数据
        params.put("page", page);
        params.put("page_size", pageSize);
        String result = HttpUtils.httpPostRequest(url, params, header);
        JSONObject resultJson = JSONObject.parseObject(result);
        if (!Check.isNull(resultJson)) {
            Integer code = resultJson.getInteger("code");
            if (code == 0) {
                JSONObject dataJson = resultJson.getJSONObject("data");
                JSONArray details = dataJson.getJSONArray("details");
                if (!Check.isNull(details)) {
                    if (num <= 50) {
                        if (shieldCommentApi(token, accountId, details)) {
                            queryComments(token, accountId, page, pageSize, ++num);
                        }
                    }
                }
                return null;
            } else {
                log.error("查询评论列表数据接口返回信息错误：accountId：{}，返回信息：{}", accountId, resultJson);
            }
        }
        return null;
    }

    /**
     * 执行屏蔽接口
     *
     * @param
     * @return com.alibaba.fastjson.JSONArray
     * @throws
     * @author ZHAOXA
     */
    private boolean shieldCommentApi(String token, Long accountId, JSONArray comments) {
        if (Check.isNull(comments)) {
            return false;
        }
        String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.COMMENT_SHIELD;
        Map<String, String> header = new HashMap<>();
        header.put("Access-Token", token);
        header.put("Content-Type", "application/json");
        JSONObject params = new JSONObject();
        params.put("advertiser_id", accountId);
        params.put("shield_list", comments);
        String result = HttpUtils.httpPostRequest(url, params, header);
        JSONObject resultJson = JSONObject.parseObject(result);
        if (!Check.isNull(resultJson)) {
            Integer code = resultJson.getInteger("code");
            return code == 0;
        }
        return false;
    }
}
