package org.jeecg.modules.xxl;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;

import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.adweb.common.constant.AdwebConstant;
import org.jeecg.modules.adweb.common.util.CommonUtil;
import org.jeecg.modules.adweb.seo.service.dataforseo.DataForSEOService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * DataForSEO Serp查询及同步任务，{@link DataForSEOService}
 *
 * @author wfansh
 */
@Slf4j
@Component
public class DataForSEOJob {

    @Autowired private DataForSEOService dataForSEOService;

    /**
     * 查询长尾关键词，Serp更新频率较低，建议三天左右执行一次
     *
     * @param siteCodes
     * @return
     */
    @XxlJob("runLongTailKeywordsSerpTasksHandler")
    public ReturnT<String> runLongTailKeywordsSerpTasksHandler(String siteCodes) {
        log.info("执行长尾词Serp查询..., site codes = {}", siteCodes);
        dataForSEOService.runKeywordsSerpTasks(
                CommonUtil.splitAndTrim(siteCodes, ","),
                AdwebConstant.KEYWORD_TYPE_LONG_TAIL,
                Integer.MAX_VALUE);
        log.info("执行长尾词Serp查询结束");

        return ReturnT.SUCCESS;
    }

    /**
     * 查询指定关键词，Serp更新频率较低，建议三天左右执行一次
     *
     * @param siteCodes
     * @return
     */
    @XxlJob("runAppointKeywordsSerpTasksHandler")
    public ReturnT<String> runAppointKeywordsSerpTasksHandler(String siteCodes) {
        log.info("执行指定词Serp查询..., site codes = {}", siteCodes);
        dataForSEOService.runKeywordsSerpTasks(
                CommonUtil.splitAndTrim(siteCodes, ","),
                AdwebConstant.KEYWORD_TYPE_APPOINT,
                Integer.MAX_VALUE);
        log.info("执行指定词Serp查询结束");

        return ReturnT.SUCCESS;
    }

    /**
     * 同步Serp查询结果，基于Redis中的剩余任务 - 负载较低，建议一天执行多次
     *
     * @return
     */
    @XxlJob("syncKeywordsSerpResultsHandler")
    public ReturnT<String> syncKeywordsSerpResultsHandler(String param) {
        log.info("同步关键词Serp查询结果...");
        dataForSEOService.syncKeywordsSerpResults();
        log.info("同步关键词Serp查询结果结束");

        return ReturnT.SUCCESS;
    }
}
