package com.ruoyi.zzb.study.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.zzb.study.common.UserAgentUtil;
import com.ruoyi.zzb.study.domain.StudyCrawlerStatus;
import com.ruoyi.zzb.study.domain.StudyDocInfo;
import com.ruoyi.zzb.study.mapper.StudyCrawlerStatusMapper;
import com.ruoyi.zzb.study.mapper.StudyDocInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Slf4j
@Service
@DataSource(value = DataSourceType.SLAVE)
public class CrondCrawlerService {

    @Autowired
    private StudyDocInfoMapper studyDocInfoMapper;

    @Autowired
    private StudyCrawlerStatusMapper studyCrawlerStatusMapper;

    @Value("${crawler.xxyd.url}")
    private String xxydUrl;

    @Value("${crawler.llqy.url}")
    private String llqyUrl;
    /**
     * 学习园地爬虫
     */
    @Scheduled(cron = "${crawler.xxyd.crondTime}")
    public int crawlerDocGawXXYD() throws IOException {

        log.info("{} ： {} ms",  "GAW学习园地调度任务开始！");
        Connection conn = Jsoup.connect(xxydUrl);
        Document document = conn.timeout(10000).maxBodySize(0).userAgent(UserAgentUtil.getRandomAgent()).get();

        LambdaQueryWrapper<StudyCrawlerStatus> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(StudyCrawlerStatus::getModuleCode,"study");
        queryWrapper.eq(StudyCrawlerStatus::getServiceCode,"xxyd");
        //获取上次状态位，爬虫时间
        Long statusTime = studyCrawlerStatusMapper.selectOne(queryWrapper).getStatusTime();
        System.err.println("xxyd--status"+statusTime);



        //处理爬虫文章

        //过滤新数据，写入

//        StudyDocInfo studyDocInfo = new StudyDocInfo();
//        studyDocInfoMapper.insert();


        //结束后更新爬虫状态位
        log.info("{} ： {} 条",  "GAW学习园地调度任务结束！");

        return 1;
    }

    /**
     * 理论前沿爬虫
     */
    @Scheduled(cron = "${crawler.llqy.crondTime}")
    public int crawlerDocGawLLQY() throws IOException {

        log.info("{} ： {} ms",  "GAW理论前沿调度任务开始！");
        Connection conn = Jsoup.connect(llqyUrl);
        Document document = conn.timeout(10000).maxBodySize(0).userAgent(UserAgentUtil.getRandomAgent()).get();
        //获取上次状态位，爬虫时间
        LambdaQueryWrapper<StudyCrawlerStatus> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(StudyCrawlerStatus::getModuleCode,"study");
        queryWrapper.eq(StudyCrawlerStatus::getServiceCode,"llqy");
        //获取上次状态位，爬虫时间
        Long statusTime = studyCrawlerStatusMapper.selectOne(queryWrapper).getStatusTime();
        System.err.println("llqy--status"+statusTime);

        //处理爬虫文章

        //过滤新数据，写入

//        StudyDocInfo studyDocInfo = new StudyDocInfo();
//        studyDocInfoMapper.insert();


        //结束后更新爬虫状态位
        log.info("{} ： {} 条",  "GAW理论前沿调度任务结束！");
        return 1;
    }
}
