package com.tzld.piaoquan.api.component;

import com.alibaba.fastjson.JSONObject;
import com.tzld.piaoquan.growth.common.component.HttpPoolClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 推荐API服务
 */
@Component
@Slf4j
public class RecommendApiService {

    @Autowired
    private HttpPoolClient httpPoolClient;

    @Value("${recommend.api.host:http://101.37.174.139:80}")
    private String recommendApiHost;

    /**
     * 根据rootSourceId获取文章信息
     */
    public JSONObject getArticleByRootSourceId(String rootSourceId) {
        try {
            String url = recommendApiHost + "/article/getArticleByRootSourceId?rootSourceId=" + rootSourceId;
            String response = httpPoolClient.get(url);
            JSONObject res = JSONObject.parseObject(response);
            if (res.getInteger("code") == 0 && res.getJSONObject("data") != null) {
                return res.getJSONObject("data");
            }
        } catch (Exception e) {
            log.error("获取文章信息失败, rootSourceId={}", rootSourceId, e);
        }
        return null;
    }
    /**
     * 获取最近N天 rootSourceId
     */
    public List<String> getArticleByRootSourceId(String rootSourceIdPrefix, Integer days) {
        try {
            String url = recommendApiHost + "/article/getNearDaysRootSourceIds?rootSourceIdPrefix=" + rootSourceIdPrefix + "&days=" + days;
            String response = httpPoolClient.get(url);
            JSONObject res = JSONObject.parseObject(response);
            if (res.getInteger("code") == 0 && res.getJSONArray("data") != null) {
                return res.getJSONArray("data").toJavaList(String.class);
            }
        } catch (Exception e) {
            log.error("获取文章信息失败, rootSourceIdPrefix={} days={}", rootSourceIdPrefix, days, e);
        }
        return null;
    }

    /**
     * 根据rootSourceId获取合作方文章信息
     */
    public String getCooperateArticleIdByRootSourceId(String rootSourceId) {
        try {
            String url = recommendApiHost + "/article/cooperate/getArticleIdByRootSourceId?rootSourceId=" + rootSourceId;
            String response = httpPoolClient.get(url);
            JSONObject res = JSONObject.parseObject(response);
            if (res.getInteger("code") == 0 && res.getString("data") != null) {
                return res.getString("data");
            }
        } catch (Exception e) {
            log.error("获取文章信息失败, rootSourceId={}", rootSourceId, e);
        }
        return null;
    }

    /**
     * 根据rootSourceId获取视频ID信息
     */
    public Long getVideoIdByRootSourceId(String rootSourceId) {
        try {
            String url = recommendApiHost + "/article/getVideoIdByRootSourceId?rootSourceId=" + rootSourceId;
            String response = httpPoolClient.get(url);
            JSONObject res = JSONObject.parseObject(response);
            if (res.getInteger("code") == 0 && res.getLong("data") != null) {
                return res.getLong("data");
            }
        } catch (Exception e) {
            log.error("获取文章信息失败, rootSourceId={}", rootSourceId, e);
        }
        return null;
    }

    /**
     * 检查rootSourceId是否存在
     */
    public Boolean checkExistRootSourceId(String rootSourceId) {
        try {
            String url = recommendApiHost + "/api/checkExistRootSourceId?rootSourceId=" + rootSourceId;
            String response = httpPoolClient.get(url);
            JSONObject res = JSONObject.parseObject(response);
            if (res != null && res.getInteger("code") == 0) {
                return res.getBoolean("data");
            }
        } catch (Exception e) {
            log.error("检查rootSourceId是否存在失败, rootSourceId={}", rootSourceId, e);
        }
        return false;
    }
}
